Color your terminal's output

Johannes Pfau spam at example.com
Sat Oct 8 02:54:40 PDT 2011


Jens Mueller wrote:
>Johannes Pfau wrote:
>> 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.
>
>Using the ANSI codes is fine with me. I assumed they aren't that
>portable but it seems fine.

I'd still use curses though, as it abstracts some things away. Finding
terminal capabilities and similar stuff.

>> >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.
>
>I believe progress bars are easy to add. Boost's progress_bar should be
>fairly easy to port. It'll be nice if you could provide a pull request
>for this. Is this feasible for you?

Sure, I'll have a look at it soon. You're talking about this
progress_display class, right?
http://www.boost.org/doc/libs/1_47_0/boost/progress.hpp

>Regarding update able text labels I'm not sure how they are typically
>used. So I would also prefer some pull request from somebody with a
>common use case.

Well, think of wget's output:
 0% [                                       ] 1.154.567    123K/s  ETA 57m 6s
 |                        |                       |          |         |
updateable label     progressbar               u-label     u-label   u-label

>I'm just pushing this color support because I'm using it in some tool.
>And it may be useful to others. I believe it's a good thing to add the
>features you need for your project via a pull request.
>
>Jens

-- 
Johannes Pfau



More information about the Digitalmars-d mailing list