[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

'normal' shell with curses

Michael Goerz

3/4/2008 2:58:00 AM

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
11 Answers

Miki

3/4/2008 4:15:00 AM

0

Hello Michael,

> I'm trying to print out text in color. As far as I know, curses is the
> only way to do that (or not?).
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.bl.../2008/03/ansi...

HTH,
--
Miki <miki.tebeka@gmail.com>
http://pythonwise.bl...


Michael Goerz

3/4/2008 5:35:00 AM

0

Miki wrote, on 03/03/2008 11:14 PM:
> Hello Michael,
>
>> I'm trying to print out text in color. As far as I know, curses is the
>> only way to do that (or not?).
> 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/ansi...
>
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.

Dennis Lee Bieber

3/4/2008 8:55:00 AM

0

On Tue, 04 Mar 2008 00:34:53 -0500, Michael Goerz
<newsgroup898sfie@8439.e4ward.com> declaimed the following in
comp.lang.python:

> 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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Thynnus

3/4/2008 1:49:00 PM

0

On 3/3/2008 9:57 PM, Michael Goerz wrote:
> 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/Rec...

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.

--------

Michael Goerz

3/4/2008 5:06:00 PM

0

Thynnus wrote, on 03/04/2008 08:48 AM:
> On 3/3/2008 9:57 PM, Michael Goerz wrote:
>> 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/Rec...
>
> 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.source...), it didn't have any color either...
So, any way to get color in Windows?

Michael

Thynnus

3/4/2008 8:29:00 PM

0

On 3/4/2008 12:05 PM, Michael Goerz wrote:
> Thynnus wrote, on 03/04/2008 08:48 AM:
>> On 3/3/2008 9:57 PM, Michael Goerz wrote:
>>> 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/Rec...
>>
>> 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.source...), it didn't have any color either...
> So, any way to get color in Windows?
>
> Michael

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

Michael Goerz

3/4/2008 11:26:00 PM

0

Michael Goerz wrote, on 03/04/2008 12:05 PM:
> Thynnus wrote, on 03/04/2008 08:48 AM:
>> On 3/3/2008 9:57 PM, Michael Goerz wrote:
>>> 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/Rec...
>>
>> 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.source...), 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/Rec...

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

Michael

Mulligan

2/22/2014 1:47:00 AM

0

On Friday, February 21, 2014 8:46:07 PM UTC-5, SAW wrote:
> On Friday, February 21, 2014 8:42:40 PM UTC-5, SAW wrote:
>
>
>
> http://en.wikipedia.org/wiki/Speedy...

http://en.wikipedia.org/wiki/Speedy_Tr...

The Speedy Trial Clause of the Sixth Amendment to the United States Constitution provides that "[i]n all criminal prosecutions, the accused shall enjoy the right to a speedy . . . trial . . . ."[1] The Clause protects the defendant from delay between the presentation of the indictment or similar charging instrument and the beginning of trial.

Mulligan

2/22/2014 1:48:00 AM

0

On Friday, February 21, 2014 8:47:20 PM UTC-5, SAW wrote:
> On Friday, February 21, 2014 8:46:07 PM UTC-5, SAW wrote:
>
> > On Friday, February 21, 2014 8:42:40 PM UTC-5, SAW wrote:
>
> >
>
> >
>
> >
>
> > http://en.wikipedia.org/wiki/Speedy...
>
>
>
> http://en.wikipedia.org/wiki/Speedy_Tr...
>
>
>
> The Speedy Trial Clause of the Sixth Amendment to the United States Constitution provides that "[i]n all criminal prosecutions, the accused shall enjoy the right to a speedy . . . trial . . . ."[1] The Clause protects the defendant from delay between the presentation of the indictment or similar charging instrument and the beginning of trial.

In all criminal prosecutions, the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the State and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the Assistance of Counsel for his defence

Mulligan

2/22/2014 1:49:00 AM

0

On Friday, February 21, 2014 8:48:01 PM UTC-5, SAW wrote:
> On Friday, February 21, 2014 8:47:20 PM UTC-5, SAW wrote:
>
> > On Friday, February 21, 2014 8:46:07 PM UTC-5, SAW wrote:
>
> >
>
> > > On Friday, February 21, 2014 8:42:40 PM UTC-5, SAW wrote:
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > > http://en.wikipedia.org/wiki/Speedy...
>
> >
>
> >
>
> >
>
> > http://en.wikipedia.org/wiki/Speedy_Tr...
>
> >
>
> >
>
> >
>
> > The Speedy Trial Clause of the Sixth Amendment to the United States Constitution provides that "[i]n all criminal prosecutions, the accused shall enjoy the right to a speedy . . . trial . . . ."[1] The Clause protects the defendant from delay between the presentation of the indictment or similar charging instrument and the beginning of trial.
>
>
>
> In all criminal prosecutions, the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the State and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the Assistance of Counsel for his defence

No person shall be held to answer for a capital, or otherwise infamous crime, unless on a presentment or indictment of a Grand Jury, except in cases arising in the land or naval forces, or in the Militia, when in actual service in time of War or public danger; nor shall any person be subject for the same offense to be twice put in jeopardy of life or limb; nor shall be compelled in any criminal case to be a witness against himself, nor be deprived of life, liberty, or property, without due process of law;