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