del_line.py
import sys
sys.stdout.write("\033[2K\033[G")
sys.stdout.flush()
sample.py
import sys, time
for i in range(10):
s = str(i)*(10-i)
sys.stdout.write("\033[2K\033[G%s" % s)
sys.stdout.flush()
time.sleep(0.5)
print()
The next output is displayed on the same line every 0.5 seconds, erasing the previous output.
0000000000
111111111
22222222
3333333
444444
55555
6666
777
88
9
You can use something called CSI to control the console screen for standard output and error output. Reference: https://en.wikipedia.org/wiki/ANSI_escape_code In addition to erasing characters, you will be able to move the cursor and change the color of characters.
The second line \ 033 [2K
of del_line.py corresponds to" EL – Erase in Line "in CSI Codes of the above Wiki, and it is a command to delete the line with the cursor. Whenever you use CSI with the message that \ 033 [
will use CSI, it will be output first.
The following 2
corresponds to the argument.
--Erase from cursor position to end of line if 0
or omitted
--If 1
, delete from the beginning of the line to the cursor position
--If 2
, delete the line with the cursor
It will be. del_line.py erases the entire line.
The final K
determines the type of CSI.
As explained in the Wiki, "EL – Erase in Line" does not change the cursor position, so if you want to output characters after the erased line, you need to manually move the cursor to the beginning of the line. There is.
This can be achieved using "CHA – Cursor Horizontal Absolute". The explanation on the Wiki says "Moves the cursor to column n (default 1)", so if you output the CSI code with n set to 1 or omitted, the cursor will move to the first column (the beginning of the line). The CSI code is CSI n G
, so replace the CSI part with \ 033 [
to make it \ 033 [G
.
Originally, I would like the characters to disappear as they are, but in Python, when outputting characters, it is saved in the buffer once and output at once at the time of line break. Therefore, even if you intend to output the CSI code for erasing characters on the source code, it will not be reflected on the actual screen. Therefore, sys.stdout.flush ()
explicitly outputs the character string stored in the buffer and reflects it on the screen.
With the above, the characters output to the console can be erased.
print("abc\033[1D\033[K")
Output result
ab
\ 033 [1D
moves the cursor to the left, and \ 033 [K
deletes from the cursor to the end of the line.
--If the length of the character strings to be displayed continuously is the same, or if the subsequent output is longer, you can overwrite it by simply moving the cursor to the beginning of the line without the erase command.
--"EL – Erase in Line" erases the line where the cursor is, so if you have a newline at the end of the previous output, use "CUU – Cursor Up" or "CPL – Cursor Previous Line" to move the cursor. You need to move up one level.
--When you redirect the output to a file, the CSI code is recorded in the file. Therefore, it is very difficult to read with an editor etc., but you can reproduce the actual display by outputting it to the console with the cat
command etc.
--Since CSI code is not limited to Python, you can control the console in the same way by outputting the same CSI code in other programming languages and shell scripts.
Recommended Posts