I'm trying to implement a progress bar for a command line application, e.g.
[##### ] 50% complete
I know I can just backspace to the start of the line and overwrite, but that seems so gross. I'd rather use the carriage return to put the cursor at the first column and then overwrite.
The problem is that the J engine appears to not render the carriage return character, instead rendering a newline+carriage return.
Here is what I have tried:
echo 'hi',(10{a.),'world'
(where 10{a.
is ASCII 10, i.e. carriage return) which prints
hi
world
echo 'hi',(13{a.),'world'
(newline) which prints
hi
world
shell 'printf "%s\r%s" hi world'
which prints
hi
world
shell 'printf "%s\n%s" hi world'
which prints
hi
world
Finally, I tried all of the above in JHS instead of Jconsole, with identical results.
From this, three things are apparent:
Any help?
CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.
You can replace one character using r<CR> in normal mode. Or you can enter a "return" in command line mode by typing <C-v><CR> . Save this answer.
'\r' is the representation of the special character CR (carriage return), it moves the cursor to the beginning of the line. '\n'(line feed) moves the cursor to the next line . On windows both are combined as \r\n to indicate an end of line (ie, move the cursor to the beginning of the next line).
Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.
Ugly but works:
0$ stdout shell 'printf "99 problems\rno"'
no problems
Nicer to avoid calling printf
from the shell:
0$stdout 'hi world',(13{a.),'12'
12 world
Thanks to a comment from @Eelvex
0$stdout 'hi world',CR,'12'
12 world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With