[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Colored output
From: |
Bruno Haible |
Subject: |
Re: Colored output |
Date: |
Sun, 27 Jan 2019 18:39:03 +0100 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-141-generic; KDE/5.18.0; x86_64; ; ) |
Hi Akim,
> I'd like to have colored diagnostics in Bison, I do feel on occasion it would
> make it easier to spot errors and warnings for instance.
>
> AFAICT, gnulib does not feature any module to help do this. Coreutils (ls)
> and GCC (maybe diffutils too) are probably good sources of inspiration, but
> am I missing something? Should I look elsewhere first? Would a gnulib module
> make sense?
----------------------------------------------------------------------------
There are two approaches to decide which colors to produce:
(A) In a first step, produce output without colors. In a second step,
add the colors, usually through regular expressions.
(B) Add the colors directly when you produce the output, through interleaved
statements that turn on or off specific attributes.
I believe that (A) is good for log files, whereas (B) is preferrable for
more complex syntax, such as JSON, XML, or programming language text.
----------------------------------------------------------------------------
There are three approaches to defining the colors and attributes for specific
syntactic roles:
(I) Hard code the escape sequences in the program.
(II) Let the user specify terminal-dependent escape sequences, e.g. through
an environment variable using an obscure syntax, or through a
configuration file.
(III) Let the user specify colors and attributes through a CSS file, and
let the program translate these colors and attribute specifications
to the terminal-dependent escape sequences.
The advantages of (III), compared to (II), are:
- The specification is terminal independent.
- Easier to change for the user.
(II) is the approach used by the coreutils for 'ls', with a helper program
called 'dircolors'. [1][2][3]
Likewise for 'diff' ([4], option '--palette').
(III) is the approach used by GNU gettext for 'msgcat' and friends [5], and
by GNU source-highlight [6]. No helper program is needed.
----------------------------------------------------------------------------
If you want the combination of (B) and (III): I'm in the process of extracting
the color and style code from GNU gettext into a library 'libtextstyle'. You
can get an impression of it and how it can be used by looking at these links:
https://www.gnu.org/software/gettext/manual/html_node/The-_002d_002dcolor-option.html
https://www.gnu.org/software/gettext/manual/html_node/The-TERM-variable.html
https://www.gnu.org/software/gettext/manual/html_node/The-_002d_002dstyle-option.html
https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=blob_plain;f=gettext-tools/styles/po-default.css;hb=HEAD
https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=blob_plain;f=gettext-tools/styles/po-vim.css;hb=HEAD
https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=blob;f=gettext-tools/src/color.h
https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=blob;f=gettext-tools/src/write-po.c
lines 156..226, 449..514.
But it's not ready yet.
Bruno
[1]
https://www.gnu.org/software/coreutils/manual/html_node/dircolors-invocation.html
[2] http://man7.org/linux/man-pages/man5/dir_colors.5.html
[3] http://www.bigsoft.co.uk/blog/2008/04/11/configuring-ls_colors
[4] https://www.gnu.org/software/diffutils/manual/html_node/diff-Options.html
[5] https://www.gnu.org/software/gettext/manual/html_node/Colorizing.html
[6]
https://www.gnu.org/software/src-highlite/source-highlight.html#Output-format-style-using-CSS