aboutsummaryrefslogtreecommitdiff
path: root/_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md
diff options
context:
space:
mode:
Diffstat (limited to '_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md')
-rw-r--r--_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md254
1 files changed, 254 insertions, 0 deletions
diff --git a/_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md b/_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md
new file mode 100644
index 0000000..ced58bb
--- /dev/null
+++ b/_posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md
@@ -0,0 +1,254 @@
1---
2title: Trying to build a New kind of terminal emulator for the modern age
3permalink: /trying-to-build-a-new-kind-of-terminal-emulator.html
4date: 2023-01-26T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Over the past few weeks, I have been really thinking about terminal emulators,
11how we interact with computers, the separation of text-based programs and GUI
12ones. To be perfectly honest, I got pissed off one evening when I was cleaning
13up files on my computer. Normally, I go into console and do `ncdu` and check
14where the junk is. Then I start deleting stuff. Without any discrimination,
15usually. But when it comes to screenshots, I have learned that it's good to keep
16them somewhere near if I need to refer to something that I was doing. I am an
17avid screenshot taker. So at that point I checked Pictures folder and also did a
18basic search `find . -type f -name "*.jpg"` for all the JPEG files in my home
19directory and immediately got pissed off. Why can’t I see thumbnails in my
20terminal? I know why, but why in the year of 2022 this is still a problem. I am
21used to traversing my disk via terminal. I am faster, and I am more comfortable
22this way. But when it comes to visualization, I then need to revert to GUI
23applications and again find the same file to see it. I know that programs like
24`feh` and `sxiv` are available, but I would just like to see the preview. Like
25[Jupyter notebook](https://jupyter.org/) or something similar. Just having it
26inline. Part of a result.
27
28It also didn’t help that I was spending some time with the [Plan
299](https://plan9.io/plan9/) Operating system. More specifically
30[9FRONT](http://9front.org/). The way that [ACME editor](http://acme.cat-v.org/)
31handles text editing is just wonderful. Different and fresh somehow, even though
32it’s super old.
33
34So, I went on a lookout for an interesting way of visualizing results of some
35query. I found these applications to be outstanding examples of how not to be a
36captive of a predetermined way of doing things.
37
38- [Wolfram Mathematica](https://www.wolfram.com/mathematica/)
39- [Jupyter notebooks](https://jupyter.org/)
40- [Plan 9 / 9FRONT](http://www.9front.org)
41- [Temple OS](https://templeos.org/)
42- [Emacs](https://www.gnu.org/software/emacs/)
43
44My idea is not as out there as ACME is, but it is a spin on the terminal
45emulators. I like the modes that Vi/Vim provides you with. I like the way the
46Emacs does its own `M-x` `M-c`. Furthermore, I really like how Mathematica and
47Jupyter present the data in a free flowing form. And I love how Temple OS is
48basically a C interpreter on some level.
49
50> **Note:** This is part 1 of the journey. Nowhere finished yet. I am just
51> tinkering with this at the moment. This whole thing can easily spectacularly
52> fail.
53
54So I started. I knew that I wanted to have the couple of modes, but I didn’t
55like the repetition of keystrokes, so the only option was to have some sort of
56toggle and indicate to the user that they are in a special mode. Like Vi does
57for Normal and Visual mode.
58
59These modes would for the first version be:
60
61- *Preview mode* (toggle with Ctrl + P)
62 - When this mode would be enabled, the `ls` command would try to find images
63 from the results and display thumbnails from them in the terminal itself.
64 No ASCII art. Proper images. In a grid!
65- *Detach mode* (toggle with Ctrl + D)
66 - When this mode would be enabled, every command would open a new window
67 and execute that command in it. This would be useful for starting `htop`
68 in a separate window.
69
70The reason for having these modes togglable is to not ask for previews every
71time. You enable a mode and until you disable it, it behaves that way. Purely
72out of ergonomic reasons.
73
74I would like to treat every terminal I open as a session mentally. When I start
75using the terminal, I start digging deeper into the issue I am trying to
76resolve. And while I am doing this, I would like to open detached windows
77etc. A lot of these things can be done easily with something like
78[i3](https://i3wm.org/), but also that pull you out of the context of what you
79were doing. I would like to orchestrate everything from one single point.
80
81In planning for this project, I knew that I would need to use a language like C
82and a library such as [SDL2](https://www.libsdl.org/) in order to achieve the
83desired results. I had considered other options, but ultimately determined that
84[SDL2](https://www.libsdl.org/) was the best fit based on its capabilities and
85reputation in the programming community.
86
87At first, I thought the idea of a hardware accelerated terminal was a bit of a
88joke. It seemed like such a niche and unnecessary feature, especially given the
89fact that terminal emulators have been around for decades and have always relied
90on software rendering. But to be fair, [Alacritty](https://alacritty.org/) is
91doing the same thing. Well, they are doing a remarkable job at it.
92
93So, I embarked on a journey. Everything has to start somewhere. For me, it
94started with creating a window! It has to start somewhere. 🙂
95
96```c
97// Oh, Hi Mark!
98// Create the window, obviously.
99SDL_Window *window = SDL_CreateWindow(
100 WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
101 WINDOW_WIDTH, WINDOW_HEIGHT,
102 SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
103```
104
105I continued like this to get some text displayed on the screen.
106
107I noted that
108[`TTF_RenderText_Solid`](https://wiki.libsdl.org/SDL_ttf/TTF_RenderText_Solid)
109rendered text really poorly. There were no antialiasing at all. In my wisdom, I
110never checked the documentation. Well, that was a fail. To uneducated like me:
111`TTF_RenderText_Solid` renders Latin1 text at fast quality to a new 8-bit
112surface. So, that's why the texts looked like shit. No wonder.
113
114Remarks on `TTF_RenderText_Solid`: This function will allocate a new 8-bit,
115palettized surface. The surface's 0 pixel will be the colorkey, giving a
116transparent background. The 1 pixel will be set to the text color.
117
118After I replaced it with
119[`TTF_RenderText_LCD`](https://wiki.libsdl.org/SDL_ttf/TTF_RenderText_LCD) which
120renders Latin1 text at LCD subpixel quality to a new ARGB surface, the text
121started looking good. Really make sure you read the documentation. It’s actually
122good. As a side note, you can find all the documentation regarding [SDL2 on
123their Wiki](https://wiki.libsdl.org/).
124
125After that was done, I started working on displaying other things like `Preview`
126and `Detach` modes. This wasn’t really that hard. In SDL2 you can check all the
127available events with `while (SDL_PollEvent(&event) > 0)` and have a bunch of
128switch statements to determine which key is currently being pressed. More about
129keys, [SDLKey](https://documentation.help/SDL/sdlkey.html) and mroe about
130pooling the events on
131[SDL_PollEvent](https://documentation.help/SDL/sdlpollevent.html).
132
133```c
134while (SDL_PollEvent(&event) > 0)
135{
136 switch (event.type)
137 {
138 case SDL_QUIT:
139 running = false;
140 break;
141
142 case SDL_TEXTINPUT:
143 if (!meta_key_pressed)
144 {
145 strncat(input_prompt_text, event.text.text, 1);
146 update_input_prompt = true;
147 }
148 break;
149 }
150}
151```
152
153After that was somewhat working correctly, I started creating a struct that
154would hold all the commands and results and I call them Cells. Yes, I stole that
155naming idea from Jupyter.
156
157```c
158typedef struct
159{
160 char *command;
161 char *result;
162 SDL_Surface *surface;
163 SDL_Texture *texture;
164 SDL_Rect rect;
165} Cell;
166```
167
168I am at a place now where I am starting to implement scrolling. This will for
169sure be fun to code. Memory management in C is super easy. 😂
170
171I have also added a simple [INI file like
172configuration](https://en.wikipedia.org/wiki/INI_file) support. It is done in an
173[STB style of
174header](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) and maps
175to specific options supported by the terminal. It is not universal, and the code
176below demonstrates how I will use it in the future.
177
178```c
179#ifndef CONFIG_H
180#define CONFIG_H
181
182/*
183# This is a comment
184
185# This is the first configuration option
186dettach=value11111
187
188# This is the second configuration option
189preview=value22222
190
191# This is the third configuration option
192debug=value33333
193*/
194
195// Define a struct to hold the configuration options
196typedef struct
197{
198 char dettach[256];
199 char preview[256];
200 char debug[256];
201} Config;
202
203// Read the configuration file and return the options as a struct
204extern Config read_config_file(const char *filename)
205{
206 // Create a struct to hold the configuration options
207 Config config = {0};
208
209 // Open the configuration file
210 FILE *file = fopen(filename, "r");
211
212 // Read each line from the file
213 char line[256];
214 while (fgets(line, sizeof(line), file))
215 {
216 // Check if this line is a comment or empty
217 if (line[0] == '#' || line[0] == '\n')
218 continue;
219
220 // Parse the line to get the option and value
221 char option[128], value[128];
222 if (sscanf(line, "%[^=]=%s", option, value) != 2)
223 continue;
224
225 // Set the value of the appropriate option in the config struct
226 if (strcmp(option, "dettach") == 0)
227 {
228 strncpy(config.option1, value, sizeof(config.option1));
229 }
230 else if (strcmp(option, "preview") == 0)
231 {
232 strncpy(config.option2, value, sizeof(config.option2));
233 }
234 else if (strcmp(option, "debug") == 0)
235 {
236 strncpy(config.option3, value, sizeof(config.option3));
237 }
238 }
239
240 // Close the configuration file
241 fclose(file);
242
243 // Return the configuration options
244 return config;
245}
246
247#endif
248```
249
250This is as far as I managed to get for now. I have a daily job and this
251prohibits me to work on these things full time. But I should probably get back
252and finish this. At least have a simple version working out, so I can start
253testing it on my machines. Fingers crossed. 🕵️‍♂️
254