Search code examples
cncurses

NCurses initialization without clearing the screen


I am writing a program that's similar to a shell. Once started up, there's a prompt and you enter in some app-specific commands.

So far this works just fine. However, I want to add support for command history like in Bash, so the user could hit the up or down arrow and see previously entered commands.

I have included the ncurses library, and I have done a simple hello world test with getch() to make sure the up and down arrows are reported correctly.

The thing that's bothering me is that it seems to be a requirement that I call initscr() which will clear the screen in order for me to use getch().

OKAY SO THE QUESTION IS:

Does anybody know a way to use ncurses getch() function without calling initscr() first? If not, can I make it not clear the screen? Basically, I'm looking to have getch() act the same as getchar(), if that makes sense.

Thanks in advance!

EDIT: I think the best example of this is how Python runs in interactive mode.


Solution

  • Curses wants to fully control the screen, and to optimize writes to the screen, especially over slow serial lines. To do this, it needs to know what is on the screen, and the only reasonable way to do that with most terminals is to start from an empty screen and keep track of what you write to the terminal.

    Thus, I suspect (n)curses is the wrong tool for your shell. You probably need to go down a step on the abstraction layer and use terminfo and non-blocking reads from the terminal (standard input) instead.

    (This is not very helpful. Sorry.)