Replacing curses (Was: Re: Problem with curses and UTF-8)

Joel Hedlund joel.hedlund at gmail.com
Thu Feb 9 03:29:55 EST 2006


> The code for handling window resizing isn't jumping out at 
> me but I'll keep looking.

(...jumping out, rather unexpectedly!)

You might be interested in an ongoing discussion that I and Grant Edwards are holding in this newsgroup on the subject "Best way of finding terminal width/height?".

Thread summary:

There's a function that Chuck Blake wrote for detecting the current terminal size, available here:

http://pdos.csail.mit.edu/~cblake/cls/cls.py

You'll only need the first two functions for this task (ioctl_GWINSZ and terminal_size).

To monitor changes in window size, have a look at the signal module (in standard library). You can attach a monitor function to the signal signal.SIGWINCH, like so:

signal.signal(signal.SIGWINCH, report_terminal_size_change)

or, in context:

-------------------------------------------------------------------
#!/usr/bin/python
from cls import terminal_size

current_terminal_size = terminal_size()

_bTerminalSizeChanged = False

def report_terminal_size_change(signum, frame):
    global _bTerminalSizeChanged
    _bTerminalSizeChanged = True

def update_terminal_size():
    global _bTerminalSizeChanged, current_terminal_size
    current_terminal_size = terminal_size()
    _bTerminalSizeChanged = False

signal.signal(signal.SIGWINCH, report_terminal_size_change) 

while True:
    if _bTerminalSizeChanged:
        update_terminal_size()
        print current_terminal_size
----------------------------------------------------------------------

Take care!
/Joel



More information about the Python-list mailing list