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

Cameron Simpson cs at cskk.id.au
Sun Feb 24 19:05:21 EST 2019


On 24Feb2019 17:48, boB Stepp <robertvstepp at gmail.com> wrote:
>On Sun, Feb 24, 2019 at 2:52 PM Mats Wichmann <mats at wichmann.us> wrote:
>> If it's snippets you want, I always look at programcreek.  These are
>> always part of something bigger so they may not fit your request to have
>> them be something you can run.
>>
>> https://www.programcreek.com/python/example/57429/curses.is_term_resized
>
>Thanks for the link!  It looks useful for future research!

Seconded. I did not know about this either!

>However,
>in the context of the current discussion, especially after Cameron's
>revelations, I cannot help but wonder if the writers of the three code
>snippets did not truly understand resizeterm()?  Especially the first
>example is very similar to my testing script.  But I did not go to the
>full-fledge programs to see if there was something unusual going on,
>so I may be overly harsh is my first impression.

I think the first two snippets definitely don't. The third I'm less sure 
about. resizeterm is easy to misunderstand.

Anyway, I should add a few remarks:

1: the use case for resizeterm() is for when curses has not noticed a 
resize. This can happen in remote terminal environments or where for 
some very weird reason the curses programme isn't in the terminal's 
control group. Most remote facilities (eg ssh and telnet) try to 
propagate terminal resize information, so remote curses stays informed.

2: much more useful is is_term_resized(). Many curses programmes need to 
know the termianl size (from getmaxyx()) in order to size their output 
(crop long lines, scale subwindows, what have you). So you might 
reasonably keep a copy of the result of getmaxyx() at the start of the 
programme:

  tty_y, tty_x = getmaxyx()

and then call:

  if is_term_resized(tty_y, tty_x):
    # update size
    tty_y, tty_x = getmaxyx()
    # recompute the sizes for various displayed things
    # redisplay everything...

at suitable times, because is_term_resized() is exactly for checking if 
the actual size (known by curses) matches some notional size (tty_y, 
tty_x, known by you, the programmer).

All is is_term_resized, resizeterm and the "internal" resize_term 
functions are recent additions :-) From "man 3 resizeterm":

  This extension of ncurses was introduced in mid-1995.  It  was  
  adopted in NetBSD curses (2001) and PDCurses (2003).

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


More information about the Tutor mailing list