tags:

views:

484

answers:

6

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.

+1  A: 

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.)

Lars Wirzenius
A: 

You could just call your program from whithin rlwrap and have the functionality without the pain...

dsm
+1  A: 

Here is another discussion about this. The provided solutions are:

  1. "The 'filter()' function lets you use curses as a single-line."
  2. "You can write something equivalent in C, using setupterm to get the terminal data, and tparm, tputs for formatting and output."

Of course there is the third option to get the ncurses source code and modify it so it doesn't clear the screen anymore.

schnaader
A: 

Have you considered the simple expedient of creating a custom terminfo or termcap entry lacking a sequence to clear the screen and then switching your terminal setting to that right before running your program? You could also just use newterm() and set_term() in ncurses. This used to be easier with termcap, since you could include another terminal and override some of its capabilities.

Thomas Kammeyer
+2  A: 

It might be simpler to use an interface like readline() rather than resorting to full-blown ncurses.

Kim Reece
A: 

ncurses restores the screen on endwin(), so why is it so difficult for ncurses to have an option for initscr() to NOT clear the screen ???

Michael Pohoreski