Proper way(s) to write to the output port in a console application?

Hi, Racket Discourse.

I am busy fiddling around with a command-line script, and I am wondering what I am doing wrong with the "dynamic" display to the terminal's output. I am running on a kali linux instance, for what it is worth.

Below is an example of the code for and running of the animated text:

Which is required and started from the main script.

screen-capture-01

This works fine and has the intended effect. Now I would like to add the number of seconds and minutes the particular command has been busy, but the "backspace" character-method from the above does not seem to work all the way.

Notice in the output how the leftover 01s remain there after the okay when waiting ends:

screen-capture-02

I can imagine that the #\backspaces might not be doing what I expect, since text can be tricky to say the least.

How do you usually do dynamic text animations in Racket cli applications?

#\backspace, or the ASCII "\b" character, moves the cursor one step leftward but does not otherwise have more effects. Therefore to erase the old content, the code would need to print spaces:

$ printf "abcdefg\b\b\b\bO\n"
abcOefg
$ printf "abcdefg\b\b\b\b    \b\b\b\bO\n"
abcO   
1 Like

Ah, that makes perfect sense. TIL.

This does the trick.

(define erase (make-string 18 #\backspace))
(define space (make-string 18 #\space))
(write-string erase)
(write-string space)
(write-string erase)

Thank you!

PS in the future it helps to share textual codeblocks rather than screenshots.

1 Like