I like to do use this in my scripts:
cat("Saving...")
do_something_slow()
cat("\rSaved\n")
The problem is that the carriage return is applied without erasing the rest of the line, so I get this:
Savedg...
To fix it I can do:
cat("\rSaved\033[K\n")
but it’s kind of ugly. Is there a simpler way?
I like to use
message()instead ofcat()for generating messages. For example:cat("Saving..."); cat("\rSaved\n")returns:
While:
message("Saving..."); message("\rSaved\n")returns:
Edit:
Inspired by @gauden answer, another function could be:
And then:
Note:
While
replaceMessage()behaves as expected in Linux and Rterm on Windows, it behaves strangely in my Rgui (2.15.0, Windows x64). Specifically,Saving...is never displayed, and afterSavedis displayed, the cursor is movedwidthspaces (in the example, 80 spaces) to the right. I am not sure why.