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

if STREAM.isatty():

On 8/29/2019 8:23 AM, Rhodri James wrote:
> On 29/08/2019 04:40, Hongyi Zhao wrote:
>> Hi,
>> I read the following code:
>> https://github.com/shichao-an/homura/blob/master/homura.py
>> On the line 265-266, it said:
>> ???????? if STREAM.isatty():
>> ???????????? p = (self.progress_template + '\r') % params
>> What's mean by using the above two lines?? Can I not use them?
> If you don't understand them, you definitely shouldn't use them.? So 
> let's fix that :-)
> "isatty()" is a method of the io.IOBase class that checks to see if the 
> stream is connected to a tty, or to use less jargon, if it is 
> interactive.? "tty" was I think originally an abbreviation for 
> "teletype", but nowadays it refers to any terminal, anywhere you get a 
> command line prompt.? See 
> https://docs.python.org/3/library/io.html#io.IOBase.isatty

That says "Return True if the stream is interactive (i.e., connected to 
a terminal/tty device)."  AFAIK, the docs do not define 'interactive' or 
'terminal/tty device'.  The Linux 'isatty(fildes)' man page says "return 
1 if fildes is associated with a terminal".  I don't know it 'terminal' 
is defined anywhere.

I take 'interactive' as meaning that the stream is one of at least two 
streams associated with one device such that if the program writes a 
prompt to an output stream, a response might be received on the 
corresponding input stream.  In python, 'input(prompt)' writes 'prompt' 
to sys.stdout, reads sys.stdin until '\n' is received, and returns the 
preceding string.  Note that devices differ on whether type-ahead works, 
and that input calls might never return.

When a 'terminal' receives \r, it can ignore it, display something, or 
act on it by putting the cursor at the beginning of the line if .  After 
moving to the beginning of the line, subsequent chars can overwrite, 
replace, or be inserted before existing characters, except that \t and 
\n may get special treatment.

The code above assumes 'return' followed by replacement.  If that 
assumption is wrong, as with the original ttys, '\r' instead of '\n' is 
the wrong choice of terminator.

> which will write "1% complete" to the terminal and then put the cursor 
> (the "writing point") back to the start of the line.? Since there is no 
> carriage return character, it does *not* move on to a new line.

I presume you meant either 'no newline' or 'just a carriage return'

Terry Jan Reedy