Jump to page: 1 2 3
Thread overview
Color your terminal's output
Oct 06, 2011
Jens Mueller
Oct 07, 2011
Johannes Pfau
Oct 07, 2011
Trass3r
Oct 07, 2011
Jens Mueller
Oct 07, 2011
Trass3r
Oct 07, 2011
Jens Mueller
Oct 07, 2011
Trass3r
Oct 07, 2011
Nick Sabalausky
Oct 08, 2011
Jens Mueller
Oct 08, 2011
Johannes Pfau
Oct 08, 2011
Johannes Pfau
Oct 09, 2011
Jens Mueller
Oct 10, 2011
Marco Leise
Oct 10, 2011
Jens Mueller
Oct 07, 2011
Jens Mueller
Oct 08, 2011
Johannes Pfau
Oct 09, 2011
Jens Mueller
Oct 09, 2011
Johannes Pfau
Oct 10, 2011
Jens Mueller
Dec 13, 2011
Iain S
Dec 14, 2011
Jens Mueller
Mar 10, 2013
Axl
Mar 10, 2013
Jens Mueller
Mar 10, 2013
Axl
Mar 10, 2013
Jens Mueller
October 06, 2011
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.

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.

Jens
October 07, 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

October 07, 2011
> 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.

+1
There shouldn't be a hard dependency on curses.
October 07, 2011
Trass3r 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.
> 
> +1
> There shouldn't be a hard dependency on curses.

I had the impression that even though there is this standard how do I
know that I have a standard-compliant terminal. Can I just assume this?
I started using curses because I had the impression there may be
non-standard terminals. But this seems to be minor issue. I will change
this if people are happy with Windows and ISO/IEC 6429 compliant
terminals only.
Thanks.

Jens
October 07, 2011
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.

> >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?
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.
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
October 07, 2011
Am 07.10.2011, 14:51 Uhr, schrieb Jens Mueller <jens.k.mueller@gmx.de>:

> Trass3r 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.
>>
>> +1
>> There shouldn't be a hard dependency on curses.
>
> I had the impression that even though there is this standard how do I
> know that I have a standard-compliant terminal. Can I just assume this?
> I started using curses because I had the impression there may be
> non-standard terminals. But this seems to be minor issue. I will change
> this if people are happy with Windows and ISO/IEC 6429 compliant
> terminals only.
> Thanks.

As Johannes already said, it's perfectly possible to implement both approaches and choose at runtime.
October 07, 2011
Trass3r wrote:
> Am 07.10.2011, 14:51 Uhr, schrieb Jens Mueller <jens.k.mueller@gmx.de>:
> 
> >Trass3r 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.
> >>
> >>+1
> >>There shouldn't be a hard dependency on curses.
> >
> >I had the impression that even though there is this standard how do I
> >know that I have a standard-compliant terminal. Can I just assume this?
> >I started using curses because I had the impression there may be
> >non-standard terminals. But this seems to be minor issue. I will change
> >this if people are happy with Windows and ISO/IEC 6429 compliant
> >terminals only.
> >Thanks.
> 
> As Johannes already said, it's perfectly possible to implement both approaches and choose at runtime.

I see. You mean using curses if available and falling back to ISO/IEC 6429. So you think that supporting ISO/IEC 6429 terminals is too limited, aren't you?

Jens
October 07, 2011
> I see. You mean using curses if available and falling back to ISO/IEC
> 6429. So you think that supporting ISO/IEC 6429 terminals is too
> limited, aren't you?

Well I personally only care about bash and Windoze console. I guess these support ISO 6429?!
October 07, 2011
"Trass3r" <un@known.com> wrote in message news:op.v2zhq60v3ncmek@enigma...
>> I see. You mean using curses if available and falling back to ISO/IEC 6429. So you think that supporting ISO/IEC 6429 terminals is too limited, aren't you?
>
> Well I personally only care about bash and Windoze console. I guess these support ISO 6429?!

Windows console windows don't, oddly enough. Even though MS-DOS did if you had ANSI.SYS loaded.

And, maybe I'm wrong, but I would think that shell interpreter wouldn't have anything to do with whether or not ANSI escape codes are avilable. Isn't that more a matter of terminal program (ie, xterm, vs...umm, all those others...)


October 08, 2011
Jens Mueller wrote:
>Trass3r 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.
>> 
>> +1
>> There shouldn't be a hard dependency on curses.
>
>I had the impression that even though there is this standard how do I
>know that I have a standard-compliant terminal. Can I just assume this?
>I started using curses because I had the impression there may be
>non-standard terminals. But this seems to be minor issue. I will change
>this if people are happy with Windows and ISO/IEC 6429 compliant
>terminals only.
>Thanks.
>
>Jens

AFAIK the capabilities of a terminal are found using the terminfo
database:
http://en.wikipedia.org/wiki/Terminal_capabilities
But it seems there's no standard stand-alone library to access this
database. Curses seems the only simple way to access this database, so
I'd say:
Try to load curses at runtime, if that fails, fall back to simple
non-colored text output.

-- 
Johannes Pfau

« First   ‹ Prev
1 2 3