[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

get the terminal's size

On 2019-01-16, Karen Shaeffer <klsshaeffer at gmail.com> wrote:

[fixed quoting and formatting]

>> 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.
> I'm running a python 3 interpreter on linux. I'm actually ssh'd into
> the terminal on a headless server. [...]
> [...]
> 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.

Yes, we know that works on Python3.  The discussion was about what to
do on Python2.

    $ python2
    Python 2.7.15 (default, Sep 12 2018, 15:19:18)
    [GCC 7.3.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import shutil
    >>> shutil.get_terminal_size()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'get_terminal_size'

Grant Edwards               grant.b.edwards        Yow! I'm having a BIG BANG
                                  at               THEORY!!