Searching for a working example of a curses application that resizesin xterm

schwerdy at web.de schwerdy at web.de
Tue Sep 20 02:35:03 EDT 2005


Thanks, but I had already read these discussions (hope the grammar is
correct...), and thought I understood them. But my exact problem: If I
resize a xterm, in most cases curses prints crap (if it does not crash
python). And I could not find any python-curses application that is
displayed correctly after a terminal resize.

I hope nobody will kill me, 'cause I'm a spammer; here is my example:

------
import curses

def gettermres():
    """
    returns the current terminal size in columns and lines
    """
    import struct, fcntl, sys, termios
    lines, cols = struct.unpack("HHHH",
fcntl.ioctl(sys.stdout.fileno(),termios.TIOCGWINSZ, struct.pack("HHHH",
0, 0, 0, 0)))[:2]
    return cols, lines

def splitwin(scr, extend1, axis=0, isPercent=True,
useFullScreen=False):
    """
    splits the scr in 2 windows and return both
    extend1 is the size of the first window
    axis=0 --> split horizontal
    axis=1 --> split vertical
    if isPercent is False, extend1 will be interpreted as absolut size
    """
    if isPercent and not 0 < extend1 < 100:
        raise "extend1 must be between 0 and 100"
    if not axis in (0,1):
        raise "axis must be 0 or 1"

    if useFullScreen:
        res = [0,0]
        res[1], res[0] = gettermres()
        pos = (0,0)
    else:
        res = scr.getmaxyx()
        pos = scr.getbegyx()

    size1 = list(res)
    size2 = list(res)

    if isPercent:
        totallen = float(res[axis])
        size1[axis] = int( totallen*extend1 / 100 )
    else:
        size1[axis] = extend1

    size2[axis] = res[axis] - size1[axis]
    start1 = list(pos)
    start2 = list(pos)
    start2[axis] = size1[axis]

    win1 = curses.newwin(size1[0], size1[1], start1[0], start1[1])
    win2 = curses.newwin(size2[0], size2[1], start2[0], start2[1])

    return win1, win2

def startcurses(stdscr):
    curses.use_default_colors() # transparency
    curses.init_pair(2, curses.COLOR_GREEN, -1)

    while 1:
        curses.setupterm()
        winTop, winBottom = splitwin(stdscr, 80, useFullScreen=True)
        winLeft, winRight = splitwin(winTop, 50, 1)
        winLeft.border()
        winLeft.noutrefresh()
        winRight.border()
        winRight.addstr(1,1,"hallo", curses.color_pair(2))
        winRight.noutrefresh()
        winBottom.border()
        winBottom.noutrefresh()
        curses.doupdate()
        k = winLeft.getch()
        if k == ord('q'):
            break

curses.wrapper(startcurses)
------




More information about the Python-list mailing list