Properly overwrite current line in terminal
In most terminals, if you haven't printed a newline character (or line feed;
\n), printing a carriage return (
\r) will reset your cursor to the beginning of the line so that subsequent characters overwrite what you've already output on the current line.
However, if you don't output enough characters to fully overwrite the previous contents of the line, the remaining characters will stay there. So, for example, the following pseudocode:
would result in
I'm wondering: is there any way to actually clear these remaining characters? I could simply keep track of them and then overwrite them with spaces, but that would, a) require me to keep track of them, and, b) still have trailing space characters, which isn't ideal, and I'd prefer not to do (I'm looking for a general solution that I can use whenever I come across this problem in the future). Any advice would be great; thanks!
terminal formatting overwrite carriage-return linefeed