Dániel Nagy Dániel Nagy - 1 year ago 130
Swift Question

Replace printed out text

I created a command line tool which does some long actions, and I want to notify the user through print statements, like:

"10% Done"
"20% Done"


on what percentage of the operation is done. If I use println, each progress will be printed into a new line (obviously), but is there a way in swift, that I can replace the last printed text with a new one, so on the console you can read only the actual percentage, not the actual and the ones that printed before?

Answer Source

If the output goes to the Terminal then you can use the fact that \r (Carriage Return) moves the "cursor" to the start of the current line, without advancing to the next line:

print("10% done \r")
print("20% done \r")
print("100% done\n")

(But note that this does not work in the Xcode debugger console.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download