[Tutor] How to use "curses.resizeterm(nlines, ncols)"
Cameron Simpson
cs at cskk.id.au
Sun Feb 24 17:39:30 EST 2019
On 24Feb2019 14:30, boB Stepp <robertvstepp at gmail.com> wrote:
>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.
[...]
>> 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 [...]
>
>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.
Likely so. The important thing you may be missing is that curses
_itself_ calls resizeterm() automatically when it gets a SIGWINCH, so in
normal situations you do not need to call this function.
Because of this, getmaxyx() is always correct for the size of the
terminal.
Secondarily, resizeterm() does not make a change to the terminal itself.
Note that is_term_resized(y,x) is a _test_: it asks whether curses' idea
of the screen size does not match (y,x). (y,x) is a pair of numbers that
_you_, the programmer, is keeping track of.
If you set it once at the start of the programme:
max_y, max_x = stdscr.getmaxyx()
and never update it then is_term_resized(max_y, max_x) will report True
if the terminal has changed size. If you update the (max_y,max_x) values
regularly from getmaxyx() then they will always match and
is_term_resized will always report False.
>I created the
>following script to test things out:
[...]
I've modified your script. Please try the script appended below. The
short answer is that resizeterm() is _not_ normally useful to you, the
programmer; it will only be useful if curses does not get to notice
terminal size changes - _then_ you could use it to provide that
facility.
The script below is like yours: 'q' to quit, other keys to refresh and
retest. Notice that the version below does not update (max_y,max_x) or
call resizeterm(); initially you want to see what is_term_resized()
does. It also shows you what got tested.
Cheers,
Cameron Simpson <cs at cskk.id.au>
#!/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
tested = "is_term_resized(max_x=%d, max_y=%d)" % (max_x, max_y)
internal = "getmaxyx() => y=%d, x=%d" % stdscr.getmaxyx()
resized = curses.is_term_resized(max_y, max_x)
result = "%s => %s" % (tested, resized)
stdscr.clear()
stdscr.addstr(max_y//2, max_x//2, result)
stdscr.addstr(max_y//2 + 1, max_x//2, internal)
if curses.is_term_resized(max_y, max_x):
##max_y, max_x = stdscr.getmaxyx()
stdscr.addstr(max_y//2 + 2, max_x//2, "You resized the terminal!")
##stdscr.addstr(max_y//2 + 2, max_x//2, "Resizing your window -- NOW!")
##curses.resizeterm(max_y, max_x)
else:
stdscr.addstr(max_y//2 + 2, max_x//2, "Not resized.")
stdscr.border()
stdscr.refresh()
if __name__ == '__main__':
curses.wrapper(start_cli)
More information about the Tutor
mailing list