1

Let's suppose we have a terminal, and our program has printed some text:

line 1
line 2
line 3

And we seek back to the beginning of line 3. I want to print some text without it overwriting previously printed text (I don't want to use ncurse), so I want it to look like:

line 1
line 2
inserted line
line 3

If I just print a \n, I'll get weird formatting. Is there any way to do this?

For more info:
I have another thread logging some data, and a > for user input, so like this:

[info]: log 
> command

and as it logs some more, it should look like:

[info]: log 
[info]: more log 
>command

No, it different from this post. On the last line, I don't know what the user inputs, so I can't just reprint it. Maybe turning off buffering (so I can get input without the user pressing enter), and also disable character echoing so I can fully manipulate the terminal, but that would be too complicated, and I don't know who to disable echo on windows.

Ruiqi Li
  • 187
  • 14
  • 1
    Does this answer your question? [How to go up a line in Console Programs (C++)](https://stackoverflow.com/questions/10058050/how-to-go-up-a-line-in-console-programs-c) – cigien May 16 '20 at 21:55

1 Answers1

1

(I don't want to use ncurse)

There is no general way to do this. C/C++ streams are basically files and behave the same way: If you want to insert a line in a file, you need to rewrite the file from the point of insertion. The same applies to your input, which could also be coming from a file instead of a keyboard.

There are different ways to do "graphical" things depending on your OS. On Unix-like systems ncurses emits escape codes that will position a character, assuming the terminal supports those codes (there are ANSI escape codes, xterm codes, WYSE codes, vt100 codes, etc.) Under DOS the compiler has its own library calls (setCursorPosition(), gotoxy(), etc). Cursor positioning is generally considered a "graphical" operation.

Basically, using ncurses, you'd want to split the screen and set the input line at the bottom after your prompt. Then, before printing something, grab the current cursor position and scroll the screen top, print your text, and then restore the current position at the point of input.

Based on this answer - Subwindows with ncurses - it appears that ncurses makes this even simpler.

rand'Chris
  • 788
  • 4
  • 17