[Tutor] How to use "curses.resizeterm(nlines, ncols)"

boB Stepp robertvstepp at gmail.com
Sun Feb 24 15:30:33 EST 2019


On Sun, Feb 24, 2019 at 1:39 AM Cameron Simpson <cs at cskk.id.au> wrote:

> It looks like the resizeterm() function updates the curses _internal_
> records of what it believes the physcial terminal size to be.  When you
> physically resize a terminal the processes within it receive a SIGWINCH
> signal, and those which pay attention to that signal should then consult
> the terminal to find out its new size.
>
> The curses library notices this signal, and calls resizeterm() to update
> its own internal idea of the terminal size so that it knows how to draw
> correctly on the screen. It does _not_ change the terminal; it changes
> curses' beliefs _about_ the terminal.
>
> If you call resizeterm() yourself you will cause curses to act from then
> on as though the physcial terminal has the size you supplied. That may
> make for bad rendering if that size does not match reality (consider
> cursor motions "from the right edge" or "from the bottom edge" - their
> sizes are computed from where curses thinks those edges are).
>
> Test the function curses.is_term_resized(nlines,ncols), whose doco says:
>
>   Return True if resize_term() would modify the window structure, False
>   otherwise.
>
> The is_term_resized() function looks up the current physical size and
> reports False if that matches curses current beliefs, and True if it
> does not match, meaning that the physical size has changed since curses
> last set up its beliefs (for example, in some environment where the
> resize _doesn't_ propagate a SIGWINCH to the process using curses, so it
> hasn't noticed).
>
> Does this clarify things for you?

What you say makes sense and supports much of what I had concluded
from my coding experiments.  However, I still cannot get the function
call, curses.resizeterm(), to do anything meaningful, which suggests
that I still do not truly understand its usage.  I created the
following script to test things out:

#!/usr/bin/env python3

import curses

def start_cli(stdscr):
    max_y, max_x = stdscr.getmaxyx()
    stdscr.clear()
    stdscr.border()
    stdscr.addstr(2, 2, "This is the beginning!")
    stdscr.refresh()
    while True:
        char = chr(stdscr.getch())
        if char in 'Qq':
            return
        if curses.is_term_resized(max_y, max_x):
            max_y, max_x = stdscr.getmaxyx()
            stdscr.clear()
            stdscr.addstr(max_y//2, max_x//2, "You resized the terminal!")
            stdscr.addstr(max_y//2 + 1, max_x//2, "Resizing your
window -- NOW!")
            #curses.resizeterm(max_y, max_x)
            stdscr.border()
            stdscr.refresh()

if __name__ == '__main__':
    curses.wrapper(start_cli)

Notice that I have "curses.resizeterm( ..." commented out.  Whether I
comment it out or leave it in, the behavior I observe while manually
resizing my terminal window is the same.  The stdscr.border() still
tracks around the limits of the full terminal screen size.  I had also
tried not adding stdscr.border() in the if block, thinking that maybe
curses.resizeterm() would redraw the border once I refreshed the
screen, but that did not happen.

So what am I misunderstanding?  Can someone show me a code snippet
that I can run which will demonstrate the usefulness and usage of
curses.resizeterm()?

TIA!
-- 
boB


More information about the Tutor mailing list