简体   繁体   English

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

[英]How to print without loosing current input in linux terminal?

So lets say we have ac program that runs in a linux terminal that continuously needs to print information. 因此,可以说我们有一个在Linux终端中运行的ac程序,该程序不断需要打印信息。 Buy also at the same time you have to be able to input text. 同时购买还必须能够输入文本。 The program has a prompt that says program> so every time that the program prints something its enoguth with just add before a carriage return \\r and then print again program> for avoiding the printed info to be obfuscated with the prev prompt eg program>Printed information 1 . 程序有一个提示,提示为“ program>因此每次在回车符\\r之前每次添加时,该程序都会打印其内容,然后再次打印“ program>以避免已打印的信息被前一个提示所混淆,例如,“ program>Printed information 1

printed information 1
printed information 2 
program>my input

But if you are writing while some information is printed the current input would be covered by the printed info. 但是,如果您在打印某些信息的同时进行书写,则当前输入将被打印的信息覆盖。

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

For the case I tried to make use of VT100 codes for moving the cursor like this 对于这种情况,我试图利用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");

the expected behavior is this: 预期的行为是这样的:

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

but the new line isn't inserted but just covers the prev printed info. 但不会插入新行,而仅覆盖上一个打印的信息。

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

is there any way to print information to the linux terminal without messing up the current input? 有什么方法可以在不弄乱当前输入的情况下将信息打印到linux终端?

The terminfo man page says: terminfo手册页显示:

If the terminal can open a new blank line before the line where the cursor is, this should be given as il1 ; 如果终端可以在光标所在的行之前打开一个新的空白行,则应将其指定为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 ; 如果终端可以删除光标所在的行,则应将其指定为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 . 带有单个参数并插入或删除许多行的il1dl1版本可以指定为ildl

Demo: 演示:

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

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

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