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

Unfortunately this seems not to work in Jupyter notebooks in vscode, it just prints a bunch of lines.

Screenshot 2024-03-02 at 17.36.13

Any suggestions?

Works for me:

image

(vscode 1.87.0 on Mac.) vscode had an issue with ANSI escapes at one point, but it was supposedly fixed years ago (vscode#112178).

That’s strange. For me it definitely prints new lines. Note that there are still open issues, which I think might be related to this, e.g.: Which terminal control/escape sequences need to be supported? · Issue #271 · timholy/ProgressMeter.jl · GitHub, and Notebook output must support more ANSI sequences · Issue #118833 · microsoft/vscode · GitHub.

I have no clue what setting might be make things different for me. I also have vscode 1.87 on Mac.