an example to demo escape sequence
tags: learning terminal software
content
ESC = "\x1b"
# Clear screen and move cursor to (10, 10)
print(f"{ESC}[2J{ESC}[10;10H", end="", flush=True)
print("X", end="", flush=True)
time.sleep(1)
# Move cursor down 1 row
print(f"{ESC}[1B", end="", flush=True)
time.sleep(1)
# Move cursor down 1 row again
print(f"{ESC}[1B", end="", flush=True)-
this program sends bytes to stdout, and if i run this program in a terminal, terminal is the stdout
-
terminal then interprets the bytes it receives:
- receives
\x1b[2J, terminal clears the screen - receives
ESC[10;10H, terminal renders cursor at Row 10 Col 10 - receives
ESC[1B, terminal renders cursor 1 col down
- receives
-
flush=Trueis needed because there’s no\nnew line printed, and usually stdout is line-buffered (only sends out when there’s new line)