So I'm trying to have a simple terminal application with the following behavior:
On launch, the screen is cleared, and some content is written at the top of the empty screen
Every time "enter" is pressed, the screen is cleared, and new content is presented in its place
I'm trying to achieve this with ANSI escape codes, by clearing the screen and moving the cursor. Currently my approach is like this (in pseudocode)
func clearAndPrintContent() {
printf("\x[2J") // clear the entire screen
printf("\x[1;1f") // move the cursor to the top left corner of the screen
printContent() // print some lines of content to the screen
}
main {
while true {
clearAndPrintContent() // do the clear and print
readLine() // wait for enter key
}
}
What I would expect to happen is that the content here would always be written in-place: i.e. when new content is written, it would over-write the existing content. What I actually get is that all my content is written in serial. In other words, when I scroll through the scrollback, I see each "page" which my program printed, including the blank space and the content which was printed.
So for instance, if I printed the content twice, then I would like for my scrollback to look like this:
- previous scrollback -
$ run my-app
[result of second printContent from my-app (first has been over-written)]
-end of terminal output -
But what I actually get is this:
- previous scrollback -
$ run my-app
[result of first printContent from my-app]
[result of second printContent from my-app]
-end of terminal output -
How can I achieve this result?
ncurses
. – Panki Nov 07 '19 at 15:24