'normal' shell with curses
Michael Goerz
newsgroup898sfie at 8439.e4ward.com
Tue Mar 4 18:26:23 EST 2008
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/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
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
More information about the Python-list
mailing list