Uriel Eli Uriel Eli - 1 month ago 13
Bash Question

Move console cursor up

I tried to create a simple clock up the top-left corner of the screen, updating every second:

def clock():
threading.Timer(1.0, clock).start()
print('\033[0;0H' + time.asctime(time.localtime()))


I've used threading and the colorama module, but it seems the escape code just move the cursor the number of pixels specified, and not to the position.




How can I move the cursor to the position
(0, 0)
?

Answer

The line and column start at 1 not 0.

print('\033[1;1H' + time.asctime(time.localtime()))

or shorter

print('\033[H' + time.asctime(time.localtime()))

You might also need to save and restore position using ESC-7 and ESC-8.

See http://ascii-table.com/ansi-escape-sequences-vt-100.php for a list of codes.

Barry