Is there go up line character? (Opposite of n)
Question:
I would like to overwrite something on a line above in a serial console. Is there a character that allows me to move up?
Answers:
No, not really easily, for that you’d have to use something like the curses library, especially if you want to have more control over cursor placement and do more things programatically.
Here’s a link for the Python docs on Programming with Curses, and this short tutorial/example might be of interest too.
I just found this note in the docs in case you are using Windows:
No one has made a Windows port of the curses module. On a Windows
platform, try the Console module written by Fredrik Lundh. The Console
module provides cursor-addressable text output, plus full support for
mouse and keyboard input, and is available from
http://effbot.org/zone/console-index.htm.
I believe for C++ there is the NCurses library, the linked page has a section on moving the cursor if you want to poke around with C++. Also there’s the NCurses Programming HowTo.
Long time ago I used the curses library with C quite successfully.
Update:
I missed the part about running this on a terminal/serially, for that the ANSI escape sequence, especially for a simple task like yours, will be easiest and I agree with @SvenMarnach solution for this.
Most terminals understand ANSI escape codes. The relevant codes for this use case:
" 33[F"
– move cursor to the beginning of the previous line
" 33[A"
– move cursor up one line
Example (Python):
print(" 33[FMy text overwriting the previous line.")
I may be wrong but :
#include <windows.h>
void gotoxy ( int column, int line )
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
in windows standard console.
for i in range(10):
print("Loading" + "." * i)
doSomeTimeConsumingProcessing()
sys.stdout.write(" 33[F") # Cursor up one lin
Try this in Python and replace doSomeTimeConsumingProcessing() with any routine needed, and hope it helps
Carriage return can be used to go to the beginning of line, and ANSI code ESC A
("
I would like to overwrite something on a line above in a serial console. Is there a character that allows me to move up?
No, not really easily, for that you’d have to use something like the curses library, especially if you want to have more control over cursor placement and do more things programatically.
Here’s a link for the Python docs on Programming with Curses, and this short tutorial/example might be of interest too.
I just found this note in the docs in case you are using Windows:
No one has made a Windows port of the curses module. On a Windows
platform, try the Console module written by Fredrik Lundh. The Console
module provides cursor-addressable text output, plus full support for
mouse and keyboard input, and is available from
http://effbot.org/zone/console-index.htm.
I believe for C++ there is the NCurses library, the linked page has a section on moving the cursor if you want to poke around with C++. Also there’s the NCurses Programming HowTo.
Long time ago I used the curses library with C quite successfully.
Update:
I missed the part about running this on a terminal/serially, for that the ANSI escape sequence, especially for a simple task like yours, will be easiest and I agree with @SvenMarnach solution for this.
Most terminals understand ANSI escape codes. The relevant codes for this use case:
" 33[F"
– move cursor to the beginning of the previous line" 33[A"
– move cursor up one line
Example (Python):
print(" 33[FMy text overwriting the previous line.")
I may be wrong but :
#include <windows.h>
void gotoxy ( int column, int line )
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
in windows standard console.
for i in range(10):
print("Loading" + "." * i)
doSomeTimeConsumingProcessing()
sys.stdout.write(" 33[F") # Cursor up one lin
Try this in Python and replace doSomeTimeConsumingProcessing() with any routine needed, and hope it helps
Carriage return can be used to go to the beginning of line, and ANSI code ESC A
("