views:

1097

answers:

6

For the following questions, answers may be for C/C++, C#, or Python. i would like the answers to be cross platform if possible but i realize i will probably need conio or ncurses

  1. how do i output colored text?
  2. how would i do a GUI like top or nethack where certain things are "drawn" to certain spaces in the terminal?

if possible a small oneliner code example would be great

+1  A: 

Most terminal windows understand the ANSI escape sequences, which allow coloring, cursor movement etc. You can find a list of them here.

Use of these sequences can seem a bit "old school", but you can use them in cases where curses isn't really applicable. For example, I use the folowing function in my bash scripts to display error messages in red:

color_red()
{
    echo -e "\033[01;31m$1\033[00m"
}

You can then say things like:

color_red "something has gone horribly wrong!"
exit 1
anon
+4  A: 

Yes, these are VT100 escape codes. The simplest thing is to use some flavor of Curses. Once, you choose a curses flavor it is pretty simple to do both 1 and 2.

Here's a HowTo on ncurses.

http://web.cs.mun.ca/~rod/ncurses/ncurses.html

BobbyShaftoe
+1: http://docs.python.org/library/curses.html, http://www.amk.ca/python/howto/curses/
S.Lott
A: 

Not cross platform but for Windows / C# colour, see

Color your Console text (C#)

c++

nzpcmad
+2  A: 

From this point of view, the console is in many ways just an emulation of a classic terminal device. Curses was created originally to support a way of doing common operations on different terminal types, where the actual terminal in use could be selected by the user as part of the login sequence. That heritage survives today in ncurses.

The ncurses library provides functions to call to directly position the cursor and emit text, and it is known to work for the Windows Console (where CMD.EXE runs), as well as on various *nix platform equivalents such as XTerms and the like. It probably even works with a true Dec VT100 over a serial line if you had such a thing...

The escape sequences understood by the VT100 and later models became the basis for the ANSI standard terminal. But you really don't want to have to know about that. Use ncurses and you won't have to.

Leaning on conio won't get you cross platform, as that is a DOS/Windows specific API.

Edit: Apparently the ncurses library itself is not easily built on mingw, at least as observed from a quick attempt to Google it up. However, all is not lost, as ncurses is only one of the descendants of the original curses library.

Another is PDCurses which is known to compile and run for Windows Consoles, as well as for X11 and a variety of *nix platforms.

(I was just reminded from chasing references at Wikipedia that curses came out of writing the game rogue, which is the ancestor of nethack. Some of its code was "borrowed" from the cursor management module of the vi editor, as well. So spelunking in the nethack source kit for ideas may not be a crazy idea at all...)

RBerteig
ok thanks...i am using mingw to do C/C++ work, i tried to include ncurses.h, but it said it couldnt find it, do i need to get it from somewhere?
jimi hendrix
Google implies there may be issues with ncurses and mingw. I'll edit my answer with some detail...
RBerteig
A: 

In C#, you can set the text color and the background color via the Console.ForegroundColor and Console.BackgroundColor properties, respectively. For a list of valid colors, see this MSDN doc.

Patrick