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