1

This little snippet will display the h x w in the bottom right-hand corner of the screen.

from asciimatics.screen import Screen
import time

def print_dims(screen):
    (h, w) = screen.dimensions
    screen.print_at("%3d x %3d" % (h, w), w - 12, h - 1, Screen.COLOUR_BLUE, Screen.A_NORMAL)

def demo(screen):
    print_dims(screen)
    while True:
        if screen.has_resized():   # never triggered
            print_dims(screen)
        ev = screen.get_key()
        if ev in (ord('Q'), ord('q')):
            return
        time.sleep(0.1)
        screen.refresh()

Screen.wrapper(demo)

Situation

The first call to print_dims() works fine.

When I resize the window, everything on the screen is simply blanked out. The screen.has_resized() inside the loop is never triggered. But the program is still running because a 'Q' will exit correctly.

Python is running on a Linux VM, and I am using ssh/iTerm2 from MacOS (TERM="xterm-256color") to access the Linux VM.

Request for Assistance

At this point, I need a sanity check.

I don't know if something is wrong with my code, with my terminal program, or if curses wasn't compiled with resize signals. iTerm2 on MacOS is probably ok, because when I run vim in another window and resize the iTerm2 window, vim does recognize the resize.

Has anyone gotten their system to detect a screen.has_resize() within asciimatics? If so, please share your snippet and share your environment so I can narrow down exactly what I'm doing wrong.

Thank you.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
  • asciimatics doesn't use curses (whether python's curses binding handles `SIGWINCH` is a different matter). – Thomas Dickey Apr 05 '22 at 22:46
  • @ThomasDickey thx - I solved my issue by replacing asciimatics with textual and rich. With these other modules, resizing/redraw appears to be automatic/built-in. – kfmfe04 Apr 06 '22 at 04:33
  • Resizing (using SIGWINCH) and redrawing are built in to asciimatics. You just misunderstood the API. A quick tweak makes it work as expected. See the full answer... – Peter Brittain Apr 07 '22 at 20:27

1 Answers1

1

It's a subtle issue here... The Screen will maintain its dimensions until you recreate it. Your code is being triggered, but printing the same thing to the double buffer and so is not updated on the refresh.

You just need to allow the Screen object to be resized. For example...

from asciimatics.screen import Screen
import time
import sys

def print_dims(screen):
    (h, w) = screen.dimensions
    screen.print_at("%3d x %3d" % (h, w), w - 12, h - 1, Screen.COLOUR_BLUE, Screen.A_NORMAL)

def demo(screen):
    print_dims(screen)
    while True:
        if screen.has_resized():
            return
        print_dims(screen)
        ev = screen.get_key()
        if ev in (ord('Q'), ord('q')):
            sys.exit(0)
        time.sleep(0.1)
        screen.refresh()

def main():
    while True:
        Screen.wrapper(demo)

main()
Peter Brittain
  • 13,489
  • 3
  • 41
  • 57