Forking Chrome to Run in the Terminal (2023)

https://news.ycombinator.com/rss Hits: 14
Summary

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 the truecolor 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. Let's hook html2svg's output into a Rust program: fn move_cursor((x, y): (usize, usize)) { println!("\x1b[{};{}H", y + 1, x + 1) } fn set_foreground((r, g, b): (u8, u8, u8)) { println!("\x1b[38;2;{};{};{}m", r, g, b) } fn set_background((r, g, b): (u8, u8, u8)) { println!("\x1b[48;2;{};{};{}m", r, g, b) } fn print_pixels_pair( top: (u8, u8, u8), bottom: (u8, u8, u8), cursor: (usize, usize) ) { move_cursor(cursor); set_background(top); set_foreground(bottom); println!("▄"); } Not bad. To render text, we need to create a new Skia device using C++, lets call it TextCaptureDevice. We'll make it call a draw_text function written in Rust. Just like in html2svg, we need to convert glyph IDs into unicode characters. class TextCaptureDevice: public SkClipStackDevice { void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList& glyphRunList, const SkPaint&, const SkPaint& paint) override...

First seen: 2025-09-05 02:05

Last seen: 2025-09-05 15:10