[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Interaction of os calls and \r
- From: Andres Perera <andres.p@...>
- Date: Sat, 18 May 2013 17:47:44 -0430
On Tue, May 14, 2013 at 5:13 PM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Jose Torre-Bueno once stated:
>> Can anyone explain why this code produces a line on the console that ticks
>> up every second:
>
> You are running this on a Unix system, right?
>
>> function testw()
>> while true do
>> t = os.time()
>> io.write(t, '\r')
>> end
>> end
>>
>> As does this (with less cursor movement)
>>
>> function testw()
>> while true do
>> t = os.time()
>> io.write(t, '\r')
>> while os.time() == t do
>> io.write('\r')
>> end
>> end
>> end
>>
>> But this code produces nothing:
>>
>> function testw()
>> while true do
>> t = os.time()
>> io.write(t, '\r')
>> while os.time() == t do
>> io.write('')
>> end
>> end
>> end
>
> Using the C standard IO (which is what Lua uses), you have three modes of
> buffering, none, line and full. No buffering causes the output to be
> written immediately. Line buffering waits until an entire line is present
> before output (or the internal buffer is full). Full buffering waits until
> the internal buffer is full before writing it.
>
> Now, on Unix, output to a TTY (like the command line) defaults to line
> buffering; everything else defaults to full buffering. Also, the default
> buffer size is BUFSIZ (an implementation dependent value) but is typically
> around 8,192 bytes in size.
The criteria for default buffering differs depending on more than the
associated file. stderr, for example, defaults to no buffering
regardless of whether there's a TTY on the receiving end or not.
> '\r' on Unix causes the TTY cursor to move to the start of the current
> line (it's CARRIAGE RETURN) whereas '\n' causes the cursror to move to the
> start of the next line (LINE FEED) [1].
Actually, that facet of '\n' depends on terminal state. After `stty
-onlcr`, the ternimal will no longer automatically output a carriage
return after a newline, and consequently no longer reposition the
cursor to the start of the next line.