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