'normal' shell with curses

M

Michael Goerz

Hi,

I'm trying to print out text in color. As far as I know, curses is the
only way to do that (or not?). So, what I ultimately want is a curses
terminal that behaves as closely as possible as a normal terminal, i.e.
it breaks lines and scrolls automatically, so that I can implement a
function myprint(color, text) that does what print() does, only in color.

So, my first tests brought up some problems:

#!/usr/bin/python

from time import sleep
import curses
import sys

stdscr = curses.initscr()
stdscr.addstr("Initialized\n")
stdscr.refresh()
(maxlines, maxcolumns) = stdscr.getmaxyx()
try:
for counter in xrange(24):
stdscr.addstr("Hello world %s\n" % counter)
stdscr.refresh()
if counter >= maxlines:
stdscr.scroll()
stdscr.refresh()
except Exception, data:
sys.stderr.write("Exception: \n");
sys.stderr.write(str(data));
finally:
sleep(5)
curses.endwin()

Instead of scrolling, the program throws an exception. Any hints?

Also, is there a way leave the curses output on the screen after
curses.endwin(), instead of returning to the normal terminal without a
trace of curses.

Thanks,
Michael
 
M

Michael Goerz

Miki wrote, on 03/03/2008 11:14 PM:
Hello Michael,

On unix, every XTerm compatible terminal will be able to display color
using escape sequence.
(Like the one you see in the output of 'grep --color')

See the shameless plug in http://pythonwise.blogspot.com/2008/03/ansiprint.html
The escape sequence approach is definitely interesting and I'd prefer it
to curses if I can make it work. However, I ultimately would like to
support other terminals than xterm (especially Windows), that I assume
have different ANSI codes. So, two questions:
What are the most common terminals that differ in the ANSI codes they
use, and how can I detect which of those terminals the program is
running on?
Where can I find out what codes those terminals use for color?

Michael.
 
D

Dennis Lee Bieber

The escape sequence approach is definitely interesting and I'd prefer it
to curses if I can make it work. However, I ultimately would like to
support other terminals than xterm (especially Windows), that I assume
have different ANSI codes. So, two questions:

If they are different, they are NOT "ANSI codes" <G> (After all, it
is rather unlikely that the "American National Standards Institute"
would authorize /different/ escape codes for same functions.
What are the most common terminals that differ in the ANSI codes they
use, and how can I detect which of those terminals the program is
running on?
Where can I find out what codes those terminals use for color?
The biggest problem is that /Windows/ is the non-standard... Last
time I looked at the actual escape sequences, the ANSI codes were an
extension of DEC VT terminal control codes (and also used by the Amiga).
Unless you're running an old ADM-3A, Tektronix storage display tube, or
Hazeltine from the 70s, the ANSI codes are likely the most common
terminal control codes -- or you rely on termcap files for UNIX-style
OS's.

Win9x at least still supported loading of a ANSI driver for the
console; I don't believe it works with the normal WinXT console. Worse,
WinXT normally uses cmd.exe, but 16-bit command.com is still available.
And ANSI.sys probably only works with the latter (if one can recall the
command needed to load it)


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Thynnus

Hi,

I'm trying to print out text in color. As far as I know, curses is the
only way to do that (or not?). So, what I ultimately want is a curses
terminal that behaves as closely as possible as a normal terminal, i.e.
it breaks lines and scrolls automatically, so that I can implement a
function myprint(color, text) that does what print() does, only in color.

You might find the below helpful. Let us know how you make out?

--------

Python Cookbook
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475116

Title:
Using terminfo for portable color output & cursor control

Description:
The curses module defines several functions (based on terminfo) that can be
used to perform lightweight cursor control & output formatting (color, bold,
etc). These can be used without invoking curses mode (curses.initwin) or using
any of the more heavy-weight curses functionality. This recipe defines a
TerminalController class, which can make portable output formatting very
simple. Formatting modes that are not supported by the terminal are simply omitted.

--------
 
M

Michael Goerz

Thynnus wrote, on 03/04/2008 08:48 AM:
You might find the below helpful. Let us know how you make out?

--------

Python Cookbook
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475116

Title:
Using terminfo for portable color output & cursor control

Description:
The curses module defines several functions (based on terminfo) that can
be used to perform lightweight cursor control & output formatting
(color, bold, etc). These can be used without invoking curses mode
(curses.initwin) or using any of the more heavy-weight curses
functionality. This recipe defines a TerminalController class, which can
make portable output formatting very simple. Formatting modes that are
not supported by the terminal are simply omitted.

That looks *extremely* interesting. From a very brief test, it seems to
do exactly what I want!

Now, Windows seems very problematic for color output. I was using the
following as a test, based on the above recipe:

term = TerminalController()
while True:
print term.render('${YELLOW}Warning:${NORMAL}'), 'paper is crinkled'
print term.render('${RED}Error:${NORMAL}'), 'paper is ripped'

On Linux, it works fine, on Windows, it just prints white on black
(which is of course what it should do if the terminal doesn't support
color). Can anyone get the Windows cmd.exe terminal to do color? I
already tried to add device=%SystemRoot%\system32\ansi.sys to config.nt,
but that doesn't seem to do anything (neither in what I tried yesterday
with the ANSI escape codes, nor with the recipe code now). I also very
briefly tried running it on the winbash shell
(http://win-bash.sourceforge.net/), it didn't have any color either...
So, any way to get color in Windows?

Michael
 
T

Thynnus

Thynnus wrote, on 03/04/2008 08:48 AM:

That looks *extremely* interesting. From a very brief test, it seems to
do exactly what I want!

Now, Windows seems very problematic for color output. I was using the
following as a test, based on the above recipe:

term = TerminalController()
while True:
print term.render('${YELLOW}Warning:${NORMAL}'), 'paper is crinkled'
print term.render('${RED}Error:${NORMAL}'), 'paper is ripped'

On Linux, it works fine, on Windows, it just prints white on black
(which is of course what it should do if the terminal doesn't support
color). Can anyone get the Windows cmd.exe terminal to do color? I
already tried to add device=%SystemRoot%\system32\ansi.sys to config.nt,
but that doesn't seem to do anything (neither in what I tried yesterday
with the ANSI escape codes, nor with the recipe code now). I also very
briefly tried running it on the winbash shell
(http://win-bash.sourceforge.net/), it didn't have any color either...
So, any way to get color in Windows?

Michael

ipython - http://ipython.scipy.org/ - does a colored Windows interpreter, clue
there.
 
M

Michael Goerz

Michael Goerz wrote, on 03/04/2008 12:05 PM:
Thynnus wrote, on 03/04/2008 08:48 AM:

That looks *extremely* interesting. From a very brief test, it seems to
do exactly what I want!

Now, Windows seems very problematic for color output. I was using the
following as a test, based on the above recipe:

term = TerminalController()
while True:
print term.render('${YELLOW}Warning:${NORMAL}'), 'paper is crinkled'
print term.render('${RED}Error:${NORMAL}'), 'paper is ripped'

On Linux, it works fine, on Windows, it just prints white on black
(which is of course what it should do if the terminal doesn't support
color). Can anyone get the Windows cmd.exe terminal to do color? I
already tried to add device=%SystemRoot%\system32\ansi.sys to config.nt,
but that doesn't seem to do anything (neither in what I tried yesterday
with the ANSI escape codes, nor with the recipe code now). I also very
briefly tried running it on the winbash shell
(http://win-bash.sourceforge.net/), it didn't have any color either...
So, any way to get color in Windows?

Michael

This recipe seems to work very well on WinXP:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496901

So now, I'll just have to combine the two methods, which shouldn't be
too hard.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top