Search code examples
console-applicationcarriage-returnj

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?


Solution

  • 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