Color your terminal's output

Johannes Pfau spam at example.com
Fri Oct 7 01:26:26 PDT 2011


Jens Mueller wrote:
>Hi,
>
>I started writing a simple module to color terminal output some time
>ago. In a recent thread people seemed interested in having such
>functionality. I cleaned up this code and kindly ask whether such a
>module is considered a useful addition.
>
>On Posix systems it uses 4 Curses functions and on Windows systems the
>Windows API is used. I tested it on Linux (using different terminal
>emulators) and on Windows XP.
>It allows setting foreground and background colors and setting bold,
>underline, reverse and blink font faces.
>Get the code from
>https://raw.github.com/jkm/phobos/terminal/std/terminal.d
>
>To test (hopefully filling your terminal with colored output) run
>on Posix
>32 bit
>$ dmd -unittest -m32 /usr/lib/libncurses.a -run terminal.d
>64 bit
>$ dmd -unittest -m64 /usr/lib/libncurses.a -run terminal.d
>
>(The library path may need to be adjusted.)
>
>and on Windows
>$ dmd -unittest -run terminal.d
>
>At this point there are some issues that I need to figure out, namely:
>* Is there a portable way to unset font face attributes on Posix?
>* How to portably obtain the default foreground/background color on
>  Posix?
>* How to properly test such a module?
>* Possible license problems: I have no idea whether it's allowed to
>link
>  against whatever license (the curses implementation uses). In doubt I
>  need to use the license that I link against, I suppose.

You could use ANSI codes on posix to avoid a dependency on curses:
http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
But I think using curses is ok. ncurses is MIT licensed and can be
used as a dynamic library, so I don't think there are license problems.

However, I'd recommend to load ncurses dynamically with dlopen/dlsym
and fallback to simple text output if the ncurses library cannot be
loaded.

>Any help is very appreciated.
>
>Though this module is functionality-wise inferior to something like
>ncurses it conveniently allows coloring output for most use cases.

as you already use these functions:
http://linux.die.net/man/3/setupterm
it'd be nice to have wget-like progressbars and 'updateable' text
labels. Shouldn't be as fancy as full ncurses, for most use cases it's
good enough to modify the current line. +Points if it properly handles
terminal width and resizing.

>Jens


-- 
Johannes Pfau



More information about the Digitalmars-d mailing list