Reputation: 4325
I am implementing a shell-like program where user types command (defined by me). Just like this.
>cmd
result blah blah blah
>
When I use arrow keys it outputs raw characters like ^[[A
.
>^[[A
I also notice sqlite3 behaves like this at least in the version I compiled on my computer.
How to prevent this and let <-
and ->
keys move cursor left and right?
Upvotes: 4
Views: 1344
Reputation: 290
You may take a look to Ncurses. It's a library that let you control almost everything in terminals. The specific function you want is noecho()
, which stops terminals from showing users' input.
Upvotes: 0
Reputation: 4325
I have found a slightly simpler way to do that. Run rlwrap [your program]
instead of [your program]
.
For example, it works fine with sqlite3
as rlwrap sqlite3
Upvotes: 4
Reputation: 140768
GNU Readline is a library specifically designed for this task (that is, allowing the user to edit commands typed at an interactive command-driven program). Note that this library is distributed under the GPL (not the LGPL); if that won't work for you, editline is a similar library with a BSD-style license.
I note that you say this is homework, so you might want to ask your instructor whether you are expected to implement cursor motion and line editing yourself. If so, ncurses (as mentioned by jDourlens) is the next step down in terms of abstraction, and if you really want to do everything yourself, read up on termios and the VT-220 control sequences (nearly all terminal emulators used nowadays emulate the VT220 or one of its descendants).
Upvotes: 5
Reputation: 1913
To let the user navigate in cmd with the keyboards arrows you may have to use termcaps
.
http://www.gnu.org/software/termutils/manual/termcap-1.3/html_chapter/termcap_2.html
If you want to be easier to deal with termcaps that are a bit complex you shoul use Ncurses
.
http://www.gnu.org/software/ncurses/ncurses.html
Good luck to deal with termcaps if you chosse this solution, it's some pain in the ass!
Upvotes: 2