Displaying colored documents / text in ncurses
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.