[Tutor] curses on linux

Michael Janssen Janssen@rz.uni-frankfurt.de
Wed Jun 25 14:23:02 2003


On Tue, 24 Jun 2003, lonetwin wrote:

> Hi,
>     I plan to write a little app. on linux that uses the curses module.
> Here's what I'd like to do:
>
> a) Start up my application. (ie: all the initscr & stuff)
> b) Open another curses based application "embedded" within mine (actually
> I'm planning to fire-up the 'links' browser). By embedded I mean either it
> is displayed in one sub-window/pad on the screen, or it is displayed as it
> normally should with a key binding that'll allow me to switch between the
> embedded app and mine.
> c) I should be able to move between these two apps.

I can't say, how to switch between applications (after going to "links" -
wouldn't be it the problem of the links programm to switch back?). Just go
to another app and exit and are back in orig app can be done with
os.system().

You need to do some steps, to clear the screen for the external app. Here
is my function calling an external editor like emacs (found via try and
error):

def ExternalEditor(toy):
    """Clean the screen for external editor and rebuild"""
    stdscr.keypad(0); curses.echo() ; curses.nocbreak()
    os.system(external_editor+" "+toy["file"]+" 2> /dev/null")
    try: toy["data"] = file(toy["file"]).read()
    except: toy["data"] = "no file yet"
    stdscr.keypad(1); curses.noecho() ; curses.cbreak()
    stdscr.clear()
    stdscr.refresh()


"toy" is just a dictionary providing some data. The lines:

stdscr.keypad(0); curses.echo() ; curses.nocbreak()
os.system(Your Command)
stdscr.keypad(1); curses.noecho() ; curses.cbreak()
stdscr.clear()
stdscr.refresh()

seems essential for me.


Michael




>
>      However, I can't figure out how to do this. I tried using
> curses.def_[prog|shell]_mode, and curses.reset_[prog|shell]_mode, but that
> doesn't seem to work. From the Ncurses faq I learnt
> [ http://dickey.his.com/ncurses/ncurses.faq.html#handle_piping ]
> ---------------------------------------------
> Redirecting I/O to/from a Curses application
> In principle, you should be able to pipe to/from a curses application.
> However, there are caveats:
>
>     * Some (very old) curses implementations did not allow redirection of
> the screen. Ncurses, like Solaris curses, consistently writes all output to
> the standard output. You can pipe the output to a file, or use tee to show
> the output while redirecting.
>     * Ncurses obtains the screen size by first using the environment
> variables LINES and COLS (unless you have disabled it with the use_env
> call), then trying to query the output file pointer, and then (finally) the
> terminal description. If you are redirecting output, then a query based on
> the file pointer will always fail, resulting in the terminal description's
> size.
> * Similarly, you can redirect input to an ncurses application. However, I
> have observed that the use of setvbuf (for better output buffering)
> interferes with the use of stream I/O on GNU/Linux (and possibly other
> platforms). Invoking setvbuf may (depending on the implementation) cause
> buffered stream input to be discarded. Ncurses does not use buffered input,
> however you may have an application that mixes buffered input with a curses
> session.
> ---------------------------------------------
>     How do I translate that to python ??
> Right now, I've cooked up something ugly using the pty module (pty.spawn()
> function). However, it's far from what I'd like to do.
>     I'd post the code if anyone is interested, I didn't do it here, 'cos
> it's kinda big.
>
> any suggestions ??
>
> Peace
> Steve
>
> An idealist is one who helps the other fellow to make a profit.
>                 -- Henry Ford
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>