Curses, Terminal Settings

J Turner jamwt at jamwt.com
Thu Aug 28 19:13:52 CEST 2003


Hello,

I'm playing with a python-based shell, which uses (guess what?) python as
it's scripting language, for iterative invocation, environment
variables, etc.  

Everything was going well, until I moved beyond sys.readline() into
curses territory to flesh out the line-editing capabilities.  Curses
always seems to get me.

The problem I'm having is described here: 

I call the following things to get the terminal into a the state I want,
and then back out of it:

def _pysh_cursesInmode():
    curses.noecho()
    curses.raw()
    stdscr.keypad(1)
    stdscr.idlok(1)
    stdscr.scrollok(1)
    curses.nonl()

def _pysh_cursesOutmode():
    curses.echo()
    curses.noraw()
    stdscr.keypad(0)
    stdscr.idlok(0)
    stdscr.scrollok(0)
    curses.nl()

Then, if a line, or an `inline command`, is determined to be a program
invocation, the shell piggybacks onto bash for forking/exec ing, piping,
and all that jazz, with a 'bash -noprofile -c "cmd"'. 

When I call out to bash, I obviously want the terminal back in a
non-curses state.  I want to undo *all* the changes so that when bash,
ls, ps, etc, starts up, the terminal is in a sane state when they write
to stdout.

Despite calling the Outmode() function listed above, the terminal is not
in this state.  Newlines don't return the output to position 0, though
they do advance down a line.

like
    this

Here's a real example.

zsh:
(10:06:38)jamwt/ % ls
LICENSE Maildir bin     doc     lists   scrib   vault
Mail    att     devel   etc     public  tmp     web
(10:06:38)jamwt/ % ls

bash through python curses:
$ ls
LICENSE Maildir bin     doc     lists   scrib   vault
                                                     Mail    att
devel   etc     public  tmp     web
$ 

... and something like 'top' looks just terrible.  

I really don't know what else to do besides undo the changes I've made!
That seems to be what I'm doing with the mode functions above.. what am
I missing?

This is the end of the problem description.  Thanks.

Here's a look at the pre-curses shell:

(10:09:41)jamwt/ % pysh  
$ ls
LICENSE Maildir bin     doc     lists   scrib   vault
Mail    att     devel   etc     public  tmp     web
$ for item in `ls`.split("\n"): print item[:3]
> 
LIC
Mai
Mai
att
bin
dev
doc
etc
lis
pub
scr
tmp
vau
web

$ FOO = "bar"        
$ import sys       
$ BAR = sys.version
$ export FOO
$ export BAR
$ env
FTP_PASSIVE_MODE=YES

... etc ..

BAR=2.3 (#1, Jul 31 2003, 00:45:11) 
[GCC 2.95.4 20020320 [FreeBSD]]

... etc ..

FOO=bar


 - Jamie





More information about the Python-list mailing list