Forking Chrome to render in a terminal
January 27, 2023
I wrote about forking Chrome to turn HTML to SVG two months ago, today we're going to do something similar by making it render into a terminal.
Let me introduce you to the Carbonyl web browser!
Drawing
There isn't much you can draw in a terminal, you're guaranteed to be able to render monospace characters in a fixed grid, and that's it. Escape sequences exist to perform actions like moving the cursor, changing the text color, or mouse tracking. Some came from the days of physical terminals like the DEC VT100, others came from the xterm project.
Assuming a popular terminal emulator, we can:
- Move the cursor
- Write Unicode characters
- Set a character's background and foreground color
- Use a 6x6x6 RGB palette, or 24 bits RGB if
COLORTERM
is set thetruecolor
One of the unicode characters we can render is the lower half block element U+2584
: ▄
. Knowing that cells generally have an aspect ratio of 1:2, we can render perfectly square pixels by setting the background color to the top pixel color, and the foregound color to the bottom pixel color.