Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Curses and resizing windows

Reply
Thread Tools

Curses and resizing windows

 
 
Nick !
Guest
Posts: n/a
 
      02-28-2007
> Chris Share <usenet at caesium.me.uk> wrote:
> > I've been writing an application using curses, and have been trying to
> > handle resizing the terminal, if running in xterm or similar. Increasing
> > the window size is working perfectly, however shrinking it is not
> > working at all. No matter how much I shrink the window, the size
> > returned by getmaxyx() does not change. However as soon as I increase it
> > again, it works fine.

>
> > I've tracked the problem down to the fact I have created a window
> > derived from stdscr. A small script showing the effect is at the end of
> > this post.

>
> odd (thanks for the example - I don't know if this is a problem in
> ncurses or in the python interface to it, but will check/see).
>
> > Can anyone suggest how to solve this problem, that doesn't involve not
> > making a derwin of stdscr?

>
> > I've been googling for hours, but found nothing to help.

>
> > Python version is 2.3.4 on debian testing.

>
> probably should report it as a bug (so it's not overlooked).
>
> --
> Thomas E. Dickey
> http://invisible-island.net
> ftp://invisible-island.net


http://web.cs.mun.ca/~rod/ncurses/ncurses.html#xterm says "The ncurses
library does not catch [the SIGWINCH aka resizing] signal, because it
cannot in general know how you want the screen re-painted". First, is
this really true? When I make my xterms smaller they clip what is
displayed--is that a function of curses or the xterm?
Second, if true, it explains /what/ is going on--stdscr, only--, but
isn't really satisfactory; it doesn't solve the original poster's bug.

#!/usr/bin/env python
"""
curses_resize.py -> run the program without hooking SIGWINCH
curses_resize.py 1 -> run the program with hooking SIGWINCH
"""


import sys, curses, signal, time

def sigwinch_handler(n, frame):
curses.endwin()
curses.initscr()

def main(stdscr):
"""just repeatedly redraw a long string to reveal the window boundaries"""
while 1:
stdscr.insstr(0,0,"abcd"*40)
time.sleep(1)

if __name__=='__main__':
if len(sys.argv)==2 and sys.argv[1]=="1":
signal.signal(signal.SIGWINCH, sigwinch_handler)
curses.wrapper(main)

If you run this without sigwinch then the line never gets resized, but
if you do then it works fine. What we can glean from this is that
stdscr only reads off it's size upon initialization. This behaviour
may seem a bit strange, but 1) it's legacy and 2) avoids breaking the
semantics of windows (which don't change size on their own).

The "curses.initscr()" part is kind of unhappy though. It modifies the
stdscr variable without us explicitly assigning anything (but I can't
think of any other way to do it, beyond making stdscr a global, and
that feels worse) and will break if initscr() ever returns a new
Window structure instead of just updating and returning the old one.
Does anyone have any tips for how to structure this so that the screen
can actually be assigned to?

In conclusion, it's not a bug, it's a feature. Joy! The workaround is
to write a sigwinch_handler that at least does `endwin(); initscr()`.

Sorry for reviving an old thread, but it doesn't seem the issue was
ever resolved (the thread doesn't go anywhere and there's no warning
about this in the docs that I noticed).

(please CC: me if anyone cares, I'm not on the list)

-Nick Guenther
 
Reply With Quote
 
 
 
 
Thomas Dickey
Guest
Posts: n/a
 
      02-28-2007
Nick ! <> wrote:

> http://web.cs.mun.ca/~rod/ncurses/ncurses.html#xterm says "The ncurses
> library does not catch [the SIGWINCH aka resizing] signal, because it
> cannot in general know how you want the screen re-painted". First, is
> this really true? When I make my xterms smaller they clip what is


no - given that particular url is a file dating from 1995, it's something
that I overlooked in making corrections here:

http://invisible-island.net/ncurses/ncurses-intro.html

But also note where I link it from:

http://invisible-island.net/ncurses/...tional_reading

> displayed--is that a function of curses or the xterm?


both - xterm sends the signal, and curses receives it.

> Second, if true, it explains /what/ is going on--stdscr, only--, but
> isn't really satisfactory; it doesn't solve the original poster's bug.


> #!/usr/bin/env python
> """
> curses_resize.py -> run the program without hooking SIGWINCH
> curses_resize.py 1 -> run the program with hooking SIGWINCH
> """



> import sys, curses, signal, time


> def sigwinch_handler(n, frame):
> curses.endwin()
> curses.initscr()


Actually I'd expect to see curses.refresh() here - but that may be a quirk
of the python code.

In general, ncurses passes a KEY_RESIZE via the getch() call that the
application (such as python) should process. If it's not reading from
getch(), ncurses' repainting/resizing won't happen.

Also, if you add your own signal handler, ncurses' SIGWINCH catcher won't
work. (I'd recommend chaining the signal handlers, but don't know if
python supports that .

> def main(stdscr):
> """just repeatedly redraw a long string to reveal the window boundaries"""
> while 1:
> stdscr.insstr(0,0,"abcd"*40)
> time.sleep(1)


> if __name__=='__main__':
> if len(sys.argv)==2 and sys.argv[1]=="1":
> signal.signal(signal.SIGWINCH, sigwinch_handler)
> curses.wrapper(main)


--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resizing a div by resizing its borders Pil (Trustworthy from Experience) Javascript 9 04-21-2009 07:35 AM
Resizing a div by resizing its borders Proper Javascript 0 04-18-2009 08:02 PM
Re: Replacing curses (Was: Re: Problem with curses and UTF-8) Jean-Paul Calderone Python 2 02-09-2006 08:29 AM
Curses and resizing windows Chris Share Python 1 06-22-2004 10:21 AM
resizing curses window jbi130@yahoo.com Python 0 08-26-2003 05:16 PM



Advertisments