I am using curses to print a pretty nice console UI, and I need for that something to be dependent of the terminal size. For this, I read here, that I could use shutil.get_terminal_size
.
So I'm doing this code :
def display(self):
size_x,size_y = shutil.get_terminal_size()
print(size_x,size_y)
window_stat = curses.newwin(size_y,size_x//2-5,0,0)
window_alert = curses.newwin(size_y,size_x//2-5,0,size_x//2+5)
window_alert.addstr("\n " + self.alert2string())
window_stat.addstr("\n " + self.stat2string())
window_alert.box()
window_stat.box()
self.stdscr.refresh()
window_stat.refresh()
window_alert.refresh()
But the fast is that, it is working perfectly the first time I call the function, but if I use my mouse to change the terminal size and recall the function, the result of shutil.get_terminal_size()
will always stay the same. (120 30).
Do you have any idea of where it could come from ? (I'm running Windows actually, and I'd like it to work under all common OS)
Thanks a lot everyone !
Basically, that's because the application using shutils
is (in this case) using the Windows console api to create a buffer which has a fixed-size. In more conventional Unix-like applications (rather than shutuils
attempt at high-level and portable), one would make a SIGWINCH
handler that notifies the application of the size-change. With Windows, you'd have to get that from the event-loop — which is completely obscured by the shutils
interface.
It happens to "work" with Unix because shutils
doesn't have to actually pay attention to those notifications. The operating system's terminal driver (usually) can return the updated information.
You might file a bug-report against shutils
, to get its developers to take that into account in their design.