The method to overwrite the same line is well known and I use it for countdown, but what about when the next output string has a different size and will be generated by a single print? That's the reason for my question!
I use a text countdown timer like (all on the same line):
Next activation in 5 seconds
Next activation in 4 seconds
Next activation in 3 seconds
Next activation in 2 seconds
Next activation in 1 seconds
In order not to waste unnecessary lines, I created a false way to produce the same result as the keyboard backspace button (false because it actually only writes over the top with blank spaces, giving the false impression that we return to the first position left writing in the terminal):
import sys
import time
from datetime import datetime
def main():
while True:
print('-------- test --------')
print(datetime.now().strftime('%Y/%d/%m %H:%M'))
for remaining in range(5, 0, -1):
sys.stdout.write('\r')
txt_value = f'Next activation in {remaining:2d} seconds'
sys.stdout.write(txt_value)
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\r' + len(txt_value)*' ')
sys.stdout.write('\r')
if __name__ == '__main__':
main()
Output countdown:
-------- test --------
Next activation in 5 seconds
Output after full countdown:
-------- test --------
2022/18/06 21:21
It's a pretty archaic method (at least I imagine it is), so I brought this question for improvements and professionalization of the method.
ERASE_LINE = '\x1b[2K'; sys.stdout.write(ERASE_LINE)
see ANSI Escape Sequences. \$\endgroup\$. -------- test --------
(I had to add a dot.
at the beginning because the formatting ignores whitespace at the beginning) \$\endgroup\$sys.stdout.write('\r' + ERASE_LINE)
orsys.stdout.write(ERASE_LINE + '\r')
. There are no spaces at the erased line however the cursor position does not change byERASE_LINE
itself. \$\endgroup\$