5

A new way of drawing boxes in the terminal (possibly)

 1 year ago
source link: https://www.willmcgugan.com/blog/tech/post/ceo-just-wants-to-draw-boxes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A new way of drawing boxes in the terminal (possibly)

Not all CEOs of tech startups have the time to write code. Fortunately while Textualize is still in its development phase I have plenty of opportunity to get my hands dirty.

I get a kick out of being creative within the limitation of the terminal. I may even have come up with with an entirely new way of drawing boxes in the terminal. As bizarre as that sounds

Boxes in the terminal are not new. Rich is full of them, but they predate Rich by a long time.

CLI apps will typically use box drawing characters to build boxes out of unicode characters. There are characters for vertical lines, horizontal lines, corners, and cross pieces. With a mixture of regular, heavy, and double lines. Even rounded corners. They work like lego: throw them together in the right order and you can display exotic things such as panels and tables at the command prompt.

But there is a problem. Look what happens when you set a background color:

8c68381c-4c94-11ed-976e-f23c91845b44png.lg.3.jpeg
© 2022 Will McGugan

A Rich panel.

The background color leaks outside of the border line. It's easy to see why this happens. I've blown up a small box in this sketch:

2b960e8e-4c93-11ed-9b94-f23c91845b44png.lg.3.jpeg
© 2022 Will McGugan

Your basic terminal box.

Each character in a terminal has exactly 2 colors. No more, no less. There is a background color and a foreground color. Consequently you can't set the color inside of the border independently of the color outside of the border. So background colors are always going to bleed outside of the apparent edge.

Textual added the ability to use entirely different characters when rendering boxes. Unicode contains one eighth vertical and horizontal blocks which are fantastic for displaying terminal progress bars with apparently higher "resolution" that a single character. But they weren't great for borders. You can see why here:

3c16e532-4c96-11ed-9b94-f23c91845b44png.lg.3.jpeg
© 2022 Will McGugan

Can you arrange these to form a box?

No corners pieces. Darn. Without corners it is not possible to render a full box with independent inside and outside colors.

Turns out I was wrong. There is. I don't know why it didn't occur to me before. Have another look at the image above. It's like one of those irritating puzzles. Make a box with only those 4 characters (you can create copies and change colors). Give it a shot. I'll wait.

Boxes all the way down

Here's the solution:

2f31b26c-4c9f-11ed-9b94-f23c91845b44png.lg.3.jpeg
© 2022 Will McGugan

A box with no color bleeding.

A box with independent inside and outside colors which doesn't violate the "two colors per character rule". It's easy to imaging how you can extend this box to larger sizes, and surround other text.

It's been possible to display boxes like this for a while (literally decades). But I haven't seen it done like this in the wild. How can that be possible? Surely every combination of unicode characters in the terminal has been tried?

If no prior art emerges, I'm calling this technique "McGugan Boxes". I'd like "Inventor of McGugan boxes" to be inscribed on my tombstone.

Here's an example of McGugan Boxes being used in a Textual demo:

boxes.gif

Textual update

The next release of Textual will land on 10/24 (24th of October in US format, or "1KB day"). Featuring McGugan boxes and amazing docs.

Mark the date or join the mailing list on Textualize.io.

Dave Rahardja — 2 hours ago

This technique is similar to the usage of MouseText, a character set for the Apple //e designed to display buttons (both normal and inverse colored), rectangles, progress bars and the like in text mode. MouseText did make it into the Unicode standard in the 0x1fb00 block, but there are very few fonts that include it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK