I would like to know if it is possible to save several cursor positions using termcaps, to be able to restore them later ?
For example :
char *c_pos_1 = tgetstr("sc", NULL); //save cursor position at position 1
later in the code
char *c_pos_2 = tgetstr("sc", NULL);
later in the code
char *c_pos_3 = tgetstr("sc", NULL);
later in the code
tputs(c_pos_2, 1, my_out); // restoring cursor at c_pos_2
and later in the code
tputs(c_pos_1, 1, my_out); //restoring cursor at c_pos_1
And if it isn't possible how to do it ?
Thank you for your help :)
You seem to be confused about at least two things: the contents of c_pos_1
, and who is responsible for storing the cursor position.
char *c_pos_1 = tgetstr("sc", NULL);
What you have in c_pos_1
is not, in any way, a representation of the cursor position. If the terminal supports saving the cursor position, then c_pos_1
points to a string that you can send to the terminal to ask the terminal to save the cursor position. To say it another way, your code
tputs(c_pos_1, 1, my_out); // restoring cursor at c_pos_2
actually has the effect of saving, not restoring, the cursor position.
The cursor position is saved in the terminal (realistically, it's stored in the process running your terminal emulator, probably xterm
or iterm
or Terminal.app
or CMD.EXE
or whatever) and is not saved in your process.
If your terminal supports an sc
string, then it also supports an rc
string which you can send it to restore the previously-saved cursor position. Again, the rc
string does not contain the cursor position. It is a string that commands your terminal (or terminal emulator) to restore the cursor position that the terminal previously saved (when you sent it the sc
string).
To save multiple cursor positions, your terminal would have to support multiple different “save cursor” and “restore cursor” command strings, but termcap doesn't have storage for multiple different “save cursor” and “restore cursor” command strings. Or your terminal would have to treat the saved positions as a stack, pushing the cursor position onto the stack each time it receives the sc
command and popping it each time it receives the rc
command. I doubt any modern, common terminal emulator does that.
The normal way to “save” and “restore” the cursor position is to keep track, in your program, of where the cursor is, by carefully tracking the effect (on the cursor) of everything you output, rather than relying on the terminal to be able to save and restore the cursor position. This is what libraries like ncurses do.