Searching for a working example of a curses application that resizes in xterm

Discussion in 'Python' started by schwerdy@web.de, Sep 19, 2005.

  1. Guest

    Hi together,

    can someone provide an example of a curses application that works in a
    xterm that can be resized?

    I could not find any working example yet...

    Thanks in advance,
    Sebastian 'Schwerdy' Schwerdhöfer
     
    , Sep 19, 2005
    #1
    1. Advertising

  2. Am Mon, 19 Sep 2005 03:40:38 -0700 schrieb schwerdy:

    > Hi together,
    >
    > can someone provide an example of a curses application that works in a
    > xterm that can be resized?
    >
    > I could not find any working example yet...


    Hi,

    You should find this in the C source of "mutt" or "less".

    HTH,
    Thomas


    --
    Thomas Güttler, http://www.thomas-guettler.de/
    E-Mail: guettli (*) thomas-guettler + de
    Spam Catcher:
     
    Thomas Guettler, Sep 19, 2005
    #2
    1. Advertising

  3. Re: Searching for a working example of a curses application thatresizesin xterm

    wrote:

    > can someone provide an example of a curses application that works in a
    > xterm that can be resized?


    googling for "python sigwinch curses" brings up a considerable
    number of related stuff, including

    http://dbforums.com/archive/97/2002/07/4/439799

    and

    http://groups.google.com/group/comp.lang.python/msg/70fa595d97e90040

    and a couple of related bug reports and discussions about why
    they're not bugs, plus the source code for a couple of curses-
    based Python programs.

    </F>
     
    Fredrik Lundh, Sep 19, 2005
    #3
  4. On 2005-09-19, <> wrote:

    > can someone provide an example of a curses application that works in a
    > xterm that can be resized?


    I'm afraid I can't post the entire program, but what you need
    to do is to catch the WINCH signal and set a flag which is
    checked by your main "event" loop and handled. Here's the
    basic C code:

    static void sigwinchHandler(int sig)
    {
    (void) sig;
    sigwinchReceived = 1;
    }

    static void clipWindows(void)
    {
    tCh *ch;
    int x,y,rows,cols;

    for (ch = chlist; ch != NULL; ch = ch->next)
    {
    getbegyx(ch->win,y,x);
    getmaxyx(ch->win,rows,cols);
    clipwin(&rows,&cols,&y,&x);
    moveWindow(ch,rows,cols,y,x);
    }
    }


    main()
    {
    [...]
    sigact.sa_handler = sigwinchHandler;
    s = sigaction(SIGWINCH, &sigact, NULL);
    assert(s==0);


    // start up curses
    while (1)
    {
    // do stuff
    if (sigwinchReceived)
    {
    struct winsize size;
    if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
    {
    resizeterm(size.ws_row, size.ws_col);
    clipWindows();
    }
    sigwinchReceived = 0;
    }
    }
    }

    --
    Grant Edwards grante Yow! If I am elected no
    at one will ever have to do
    visi.com their laundry again!
     
    Grant Edwards, Sep 19, 2005
    #4
  5. Guest

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

    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)
    ------
     
    , Sep 20, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kalvin

    Tables automatically resizes

    Kalvin, Aug 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    387
    eruess
    Aug 8, 2003
  2. David Dorward

    Re: resizng text as the browser resizes

    David Dorward, Aug 6, 2003, in forum: HTML
    Replies:
    3
    Views:
    353
    Toby A Inkster
    Aug 6, 2003
  3. Toby A Inkster

    Re: resizng text as the browser resizes

    Toby A Inkster, Aug 6, 2003, in forum: HTML
    Replies:
    1
    Views:
    333
    Woolly Mittens
    Aug 6, 2003
  4. Replies:
    0
    Views:
    659
  5. jimmycrack

    Expanding Table, then resizes... HTML / PHP

    jimmycrack, Aug 30, 2010, in forum: HTML and CSS
    Replies:
    0
    Views:
    523
    jimmycrack
    Aug 30, 2010
Loading...

Share This Page