Is shutil.get_terminal_size useless?
rosuav at gmail.com
Sat Jan 28 12:58:36 EST 2017
On Sun, Jan 29, 2017 at 3:15 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> On Sat, 28 Jan 2017 10:50 pm, Chris Angelico wrote:
>> 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.
> In which sense, and why do you think it is relevant?
> There clearly is a terminal, because that's where I'm running the code.
> Regardless of whether I pipe it to grep or cat or something else, the
> output from *that* process still ends up in the same terminal that I typed
> the command in.
No, not automatically. I've written plenty of programs that accept
input via a pipe and don't display any of it. Just because someone
types "command1 | command2", you can't assume that the terminal
command2 is outputting to is the same size as the one command1 should
be outputting to.
>> 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
> Then explain why os.get_terminal_size() returns the correct answer.
> The output might not be going to a terminal (not directly at least) but the
> question isn't "what's the size of the terminal that output is going to".
> The question is "what's the size of the terminal that this process is
> running in", and that has an answer regardless of where output is piped.
Processes aren't always running "in" terminals, though. That's my
point. A terminal is not a fundamental feature of a process.
>> Would you expect a cronjob to use the terminal size when you
>> most recently edited crontab? No.
> Of course not -- the terminal where you edited crontab is not where the
> process is running. Why would it be the least bit relevant?
So where *is* that process running? What terminal is it in? Don't you
see how similar this is to the pipe situation - sure, there might be a
terminal that the program was invoked from, but it's utterly
irrelevant to how the program actually runs.
>> You might well be a completely background process.
> And if that background process is running in a terminal? What's your point?
Background processes don't have terminal access. Whether it's a daemon
or something started as "commandname >/dev/null 2>/dev/null </dev/null
&" from bash, it doesn't have access to a terminal.
Please don't assume that every program has a controlling UI. I get
enough of that mentality from the people I support on Windows, where
the fundamental assumption of people seems to be that there's a GUI
for everything. There isn't. And there isn't always a terminal either.
Processes in the middle of pipelines *do not have* terminals.
More information about the Python-list