Dreamable
Dreamable

Reputation: 1

How to print without loosing current input in linux terminal?

So lets say we have a c program that runs in a linux terminal that continuously needs to print information. 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 e.g 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

//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?

Upvotes: 0

Views: 205

Answers (1)

Toby Speight
Toby Speight

Reputation: 30968

The terminfo man page says:

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.

Demo:

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

Upvotes: 0

Related Questions