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

Cameron Simpson cs at cskk.id.au
Sun Feb 24 02:32:27 EST 2019


On 23Feb2019 23:00, boB Stepp <robertvstepp at gmail.com> wrote:
>I am trying to understand the functionality that the Python module,
>curses, provides.  But I am stuck on how to use the command,
>curses.resizeterm(nlines, ncols).  At
>https://docs.python.org/3/library/curses.html#curses.resizeterm it says:
>
><quote>
>curses.resizeterm(nlines, ncols)¶
>
>Resize the standard and current windows to the specified dimensions,
>and adjusts other bookkeeping data used by the curses library that
>record the window dimensions (in particular the SIGWINCH handler).
></quote>
>
>After much experimentation -- to no good effect -- I have concluded
>that "resizeterm" does *not* mean resize the terminal window that the
>curses program is running within.  Can someone give me a working
>example of how to use this command?

I think you might misunderstand the purpose of the function; I have to 
say the doco doesn't help you much here.

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?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list