0

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Dreamable
  • 1
  • 1
  • Do you actually mean a Linux console, or some other terminal emulator within an X session? They have different capabilities! – Toby Speight Jul 24 '19 at 16:36
  • Your code would need to take over the displaying of the input – Chris Turner Jul 24 '19 at 16:49
  • You really need to read the characters in raw mode, then restore what was lost, and then allow editing of it (backspace, etc). It all gets very messy, which is why it generally doesn't work that way.. – Gem Taylor Jul 24 '19 at 17:47

1 Answers1

0

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
Toby Speight
  • 27,591
  • 48
  • 66
  • 103