Is shutil.get_terminal_size useless?

Chris Angelico rosuav at gmail.com
Sat Jan 28 06:50:07 EST 2017


On Sat, Jan 28, 2017 at 9:49 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> The terminal size doesn't change just because I'm piping output to another
> process. Using the terminal size as a proxy for "being piped" is sheer
> insanity.

In a sense, there _is no_ terminal size when you're being piped to
another process. Likewise if you're running in some detached
(non-terminal) context, or if you're being run over some remote link
that hasn't transmitted terminal size info, etc, etc, etc, etc. It's
not a proxy for "being piped" - it's that when your output isn't going
to a terminal, asking "what is my terminal size" isn't particularly
productive.

Would you expect that a process started from systemd is told about the
size of the terminal in which you ran "systemctl start servicename"? I
doubt it. Would you expect a cronjob to use the terminal size when you
most recently edited crontab? No. So why should a program that's being
piped into something else automatically assume the size of the other
program's terminal? You might well be a completely background process.

You can still ask the concrete question about terminal size, and
that's in the 'os' module. The 'shutil' module also looks at
environment variables, and probably would be the place to respond to
TELNET NAWS if that has any sort of primary-level support.

ChrisA


More information about the Python-list mailing list