简体   繁体   English

是否可以使用termcap保存多个光标位置,以便以后恢复它们?

[英]Is it possible to save several cursor positions using termcaps, to be able to restore them later?

I would like to know if it is possible to save several cursor positions using termcaps, to be able to restore them later ? 我想知道是否可以使用termcaps保存多个光标位置,以便以后恢复它们?

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. 您似乎对至少两件事感到困惑: c_pos_1的内容以及谁负责存储光标位置。

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. c_pos_1c_pos_1内容都不表示光标位置。 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. 如果终端支持保存光标位置,则c_pos_1指向一个字符串,您可以将其发送到终端以要求终端保存光标位置。 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. 光标位置保存在终端中(实际上,它存储在运行终端仿真器的进程中,可能是xtermitermTerminal.appCMD.EXE或其他内容),而不保存在进程中。

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. 如果您的终端支持sc字符串,那么它也支持rc字符串,您可以发送该字符串以恢复先前保存的光标位置。 Again, the rc string does not contain the cursor position. 同样, rc字符串不包含光标位置。 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). 它是一个字符串,命令您的终端(或终端仿真器)恢复终端先前保存的光标位置(向您发送sc字符串时)。

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. 要保存多个光标位置,您的终端必须支持多个不同的“保存光标”和“还原光标”命令字符串,但是termcap不能存储多个不同的“保存光标”和“还原光标”命令字符串。 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. 否则您的终端必须将保存的位置视为堆栈,每次收到sc命令时将光标位置推入堆栈,并在每次收到rc命令时将其弹出。 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. 这就是像ncurses这样的库所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM