Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J turns carriage return into newline

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:

  1. The J front ends turn the carriage return into a carriage return + newline.
  2. The J front end also processes carriage returns generated externally (for example by printf) into newlines.
  3. J does recognize a newline by itself as shown in the last example.

Any help?

like image 724
Alex Shroyer Avatar asked Jun 07 '15 15:06

Alex Shroyer


People also ask

Is a carriage return a new line?

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.

How do I replace a carriage return in vi?

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.

What does \r do in Java?

'\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).

What is carriage return in vi?

Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.


1 Answers

Ugly but works:

   0$ stdout shell 'printf "99 problems\rno"'
no problems

UPDATE - 50% less ugly!

Nicer to avoid calling printf from the shell:

   0$stdout 'hi world',(13{a.),'12'
12 world

UPDATE - 75% less ugly!

Thanks to a comment from @Eelvex

   0$stdout 'hi world',CR,'12'
12 world
like image 177
Alex Shroyer Avatar answered Sep 24 '22 21:09

Alex Shroyer