The Accidental Rubyist

invalid byte sequence in UTF-8

Displaying colored documents / text in ncurses

leave a comment »

I have wanted to show documents or text with coloring (like syntax highlighting) for some time. Here is a quick take on this. The image below shows 2 textviews. One has text colored using the TMUX format that I liked. The next document is the output of dooby which uses ANSI escape sequences.

ANSI escape sequences don’t allow us to close a "span", all previous colors are disabled whenever a new color is specified. The tmux-format allows us to nest colors, so when a color ends, the previous one can continue.
The tmux format uses syntax such as : "#[fg=green, bg=yellow, bold]". I’ve added a "#[end]" to signify end of a span. I still have to work on substringing text with such markup but the basics are in place.

Any new markup format supplies a parse_format which yields either the text or the color and attributes. The rest is handled by rbcurse which currently converts the text to an intermediate format, an array containing text, color and attribute. This is used for printing colored text. Any further operations, such as finding the length of a line, or slicing the marked-up text happens in this internal format coded once.

Advertisements

Written by totalrecall

November 8, 2011 at 2:41 pm

Posted in ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: