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

boB Stepp robertvstepp at gmail.com
Sun Feb 24 22:22:44 EST 2019


On Sun, Feb 24, 2019 at 4:40 PM Cameron Simpson <cs at cskk.id.au> wrote:

> 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.

> #!/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)

While playing around with the above as I manually resized the terminal
window I noticed the program crashing with a curses.ERR exception if I
shrank the window so much that the program could not place the text at
the programmed coordinates.  This makes sense.  But, as usual, this
has gotten me to wonder that if I ever use curses to write a program
that others would be using if it is worthwhile to at least warn the
users against overly shrinking their terminal window or somehow trying
to handle the resulting exception?  Or does such a user "deserve" what
he/she gets?  ~(:>))

-- 
boB


More information about the Tutor mailing list