c - 如何在不丢失 linux 终端当前输入的情况下进行打印?

标签 c linux terminal

假设我们有一个在 linux 终端中运行的 c 程序,它需要不断地打印信息。购买的同时还必须能够输入文字。该程序有一个提示说 program> 所以每次程序打印它的 enoguth 只需在回车前添加 \r 然后再次打印 program > 用于避免打印信息被 prev 提示混淆,例如 program>Printed information 1

printed information 1
printed information 2 
program>my input

但是,如果您在打印某些信息时正在书写,则当前输入将被打印的信息覆盖。

printed information 1
printed information 2 
printed information 3 //program>my input has been covered
program>

对于这种情况,我尝试使用 VT100 代码像这样移动光标

//saving cursor position and move the cursor two lines up
printf("\033[s\033[2A");
//go all the wey left and then insert a new line
printf("\r\n");

log_(logger, "some information 3");
//restore cursor position
printf("\033[u");

预期的行为是这样的:

printed information 1
printed information 2
printed information 3 
program>my input

但是没有插入新行,只是覆盖了之前打印的信息。

printed information 1
printed information 3 //printed information 2 has been covered
program>my input

有没有办法在不弄乱当前输入的情况下将信息打印到 linux 终端?

最佳答案

terminfo 手册页说:

If the terminal can open a new blank line before the line where the cursor is, this should be given as il1; this is done only from the first position of a line. The cursor must then appear on the newly blank line. If the terminal can delete the line which the cursor is on, then this should be given as dl1; this is done only from the first position on the line to be deleted. Versions of il1 and dl1 which take a single parameter and insert or delete that many lines can be given as il and dl.

演示:

echo "foo bar"; sleep .5; echo "`tput cuu1``tput il1`baz`tput ll`"; sleep 1

关于c - 如何在不丢失 linux 终端当前输入的情况下进行打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187345/

相关文章:

MYSQL 恢复命令说我正在尝试恢复到不同的数据库?

c - 如何修复代码以打开代码块中命令提示符中传递的文件

java - 如何在 Java 中复制大于 4.3 GB 的文件

c++ - 用于 Linux 的 PostMessage

bash 序列 : wait for output, 然后开始下一个程序

php - 使用 phpseclib Interactive shell 显示超过 17 行

macos - objective-c : How to change option in "Security & Privacy" via terminal

c - 在结构内部,如何定义与结构本身类型相同的结构数组(动态)

c++ - 人脸识别分类器

c - C中的数组成员结构