get the terminal's size
Karen Shaeffer
klsshaeffer at gmail.com
Tue Jan 15 23:37:39 EST 2019
That will tell you the terminal size at the time Python was started.
If the terminal size has changed while Python was running, those
environment variables will be wrong. You need to use the TIOCGWINSZ
ioctl call:
http://www.delorie.com/djgpp/doc/libc/libc_495.html
And to detect the size changes (so you know _when_ you need to do the
above), you need to attach a signal handler for the WINCH signal.
Hi,
I'm running a python 3 interpreter on linux. I'm actually ssh'd into the
terminal
on a headless server. And so my terminal is my local laptop terminal
window, with
the python interpreter running on the remote linux box terminal,
communicating
over an ssh connection.
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> print(f"{shutil.get_terminal_size()}\n")
os.terminal_size(columns=118, lines=63)
>>> print(f"{shutil.get_terminal_size()}\n")
os.terminal_size(columns=133, lines=63)
>>> print(f"{shutil.get_terminal_size()}\n")
os.terminal_size(columns=118, lines=65)
>>> print(f"{shutil.get_terminal_size()}\n")
os.terminal_size(columns=118, lines=63)
With the python interpreter running on the remote terminal, I have resized
the terminal window on my local laptop several times. And each time, the
remote
python interpreter knows about the change, correctly printing the new size.
I
have done nothing with environment variables. I have not used a signal
handler
for the WINCH signal. It just works.
Karen.
More information about the Python-list
mailing list