Color your terminal's output

Johannes Pfau spam at example.com
Sun Oct 9 06:31:13 PDT 2011


Jens Mueller wrote:
>Johannes Pfau wrote:
>> Jens Mueller wrote:
>> >Johannes Pfau wrote:
>> >> 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
>
>Yes. Maybe it's too simple compared to wget's.
>
>> >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
>
>That looks very useful. Is this difficult to add?
>Can't one just delete the entire line and replace it with an
>appropriately updated one? Is this too naive?

Yes it's implemented exactly that way. But I think having it in a
library is useful nevertheless.

Here's what I have so far:
https://gist.github.com/1273678

Some improvements to be done:
-Autodetect terminal width
-test on windows
-use isatty and produce useful output for non-ttys
-refactoring, docs
 
>This would be easy to implement on Windows as well, I suppose.
It should be quite portable. I think "\r" should work on windows
terminals. If not, the only needed function is to reset the cursor to
the beginning of the current line.
>
>Jens


-- 
Johannes Pfau



More information about the Digitalmars-d mailing list