假设我们有一个在 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 asdl1
; this is done only from the first position on the line to be deleted. Versions ofil1
anddl1
which take a single parameter and insert or delete that many lines can be given asil
anddl
.
演示:
echo "foo bar"; sleep .5; echo "`tput cuu1``tput il1`baz`tput ll`"; sleep 1
关于c - 如何在不丢失 linux 终端当前输入的情况下进行打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187345/