Easy way to update text in terminal (instead of printing more text)?

I would like to show progress of a for loop, updating the text like like “1/100”, “2/200”, until “100/100”. The rest of the program, before or after the loop, would print text in the usual way without changing old text. How should I do this?

See GitHub - timholy/ProgressMeter.jl: Progress meter for long-running computations

In particular, this is done with ANSI escape sequences, as in this example

Linking here one example of ANSI escape sequence suggested by Steve.

And sample code here below for easier reference:

for n in 1:100
    print("Iteration number $n/100\u001b[1000D")
    sleep(0.002)  # to simulate code waiting time
end

There is also the simple print("\r") solution:

for i in 1:100
    print("\rIteration: $(i)/100")
    sleep(0.002)  # to simulate code waiting time
end
2 Likes