shutil.get_terminal_size() does not change
I’m using curses to print a very nice console UI and I need it to depend on the terminal size. To do this, I read here, which I can use shutil.get_terminal_size
。
So I’m working on 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 it’s fast, the first time I call the function it works fine, but if I change the terminal size with my mouse and call the function, the result of shutil.get_terminal_size()
will always remain the same. (120 30).
Do you know where it could have come from?
(I’m actually running Windows, and I want it to run on all common operating systems).
Thank you very much all!
Solution
Basically, that’s because the application uses the shutils
Yes (in this case) use the Windows console APIs to create a buffer with a fixed size. In more traditional Unix-like applications (as opposed to the advanced and portable aspects that shutuils
attempted), a SIGWINCH
handler is made to notify the application size of changes. In Windows, you have to get it from the event loop — it’s completely obscured by the shutils
interface.
It happens to “work” on Unix because shutils
doesn’t really have to pay attention to those notifications. Terminal drivers for the operating system can (typically) return updated information.
You can submit bug reports against shutils
and have its developers take this into account in their designs.