Up arrow based command history in console input (C++) - c++

I am trying to build a console application that takes user input. I was able to use printf to keep the cursor in the same place, I could have used curses as well, but I can't get up-arrow command history to work. Any pointers ?

I think you want readline (www.gnu.org/software/readline/ which seems to now redirect to the maintainer site at http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html)

In addition to the mentions of the readline library, I'll also mention the BSD-licensed editline library and the rlwrap command-line wrapper tool that runs any program with a readline-based history.

As long as the GNU license is not a problem for you, I would strongly consider GNU Readline

Have a look at the GNU Readline library. It can provide input history support.

In Windows the standard console windows provide up-arrow input history -- you don't have to do anything. For other standard Windows console services see the doskey command quickhelp, and simply replace the word "command" with "line of input". It's a bit misleading, yes.
EDIT, added para: Possibly you're doing something that circumvents the standard services. I just noticed that the browser window title says "ncurses", which is not in your current question title. Perhaps that's it, but in that case, ask specifically for help with ncurses.
For *nix see the other answers.
Cheers & hth.

Related

how to repeat last command in OCaml interpreter shell

I've been trying out OCaml. Sometimes its quicker just to test out some code using the interpreter shell but, it doesn't bring up the last command when I press the 'up' key.
Its a pain when I miss type something or wish to see what a little variation would produce.
Anyone know if there is another key for it?
Thanks,
Use rlwrap:
rlwrap ocaml
ocaml itself has no readline support.
You can configure readline using ~/.inputrc.
For example, you could add such lines to it:
$if ocaml
"\C-o": "()\C-b"
"\C-n": ";;\n"
$endif
Now you can use ctrl-o and ctrl-n hotkeys in ocaml. Just try it.
You can use the improved toplevel utop.
Moreover, it includes nice completion capacities.
I used ctrl + arrow up for previous input

Cross platform, open source, console GUI

What would be the best, most portable, simplest way to create a GUI-like feeling in the command line?
Basically I'm asking for a library that allows you to change background and text colors, have a text input area on the bottom of the console, and be able to edit a certain area in the command line without having to clear the whole screen. Does such a library exist?
Example from IRSSI
Thank you.
You could use ncurses
ncurses?
Here is a HowTo: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html
ncurses ?
Though if you need MS Windows support, you may find limitations.
PDCurses or ncurses (if you care only about *nixes). irssi uses the latter.
NCurses always clears the whole screen. Is there any alternative which only changes the colors but not mess with the current console content?

Is there a plugin for Vim that would help me debugging like Visual Studio?

The reason why I'm asking this, is because I'm coding in C++, in putty/ssh and I like the fact that I can code from pretty much everywhere without having to install anything.
So I'd like to have something that could help me debugging (viewing LIVE value of a variable, breakpoints, etc)
If you think that there's no such thing in this world, is there any good technique I could use to debug in command line?
Thanks
I've used gdb for command line debugging in the past with success:
http://www.gnu.org/software/gdb/
A decent tutorial can be found at:
http://www.cs.cmu.edu/~gilpin/tutorial/
vimgdb will give what you want. I've used it for about one year. The most interesting feature is:
Hightlight current line
List item
Can show disassembly code
Step into, Step over
inspect variables, memory address
Run all the underlying gdb command is possible
And, of course, set breakpoint, conditional breakpoint etc.
Highly customizable by vim key mapping and scripts.
Actually I use checkinstall to make an rpm for it, and installed it everywhere when I need to debug on the box.
I think it have the most important features I want from a visual debugger.
Have you tried gdb ? That's pretty much the command line debugger, but it's no vim plugin.
You have a script to do that: http://www.vim.org/scripts/script.php?script_id=1954
In my humble opinion, Vim is not designed to do such things and it is a bad idea to do so.

Simple interactive prompt in C++

I work on an application that usually runs unattended. Now I need to add to it something like an interactive prompt. In the interactive mode the operator will be able to give simple commands to the application - nothing fancy, simple commands like start and stop. Parametrized commands (e.g. repeat 10) and commands history could be nice too.
Do you know, by chance, any library that helps with such tasks. I've been thinking about something that works like boost::program_options or gflags but for an interactive prompt and not for command line parameters. Any ideas?
Thanks
Readline is one the best known libraries for this
http://tiswww.case.edu/php/chet/readline/rltop.html
It is covered by GPL, so it is only possible to use in GPL-compatible programs.
I did a quick search for alternatives, and found this:
http://github.com/antirez/linenoise
I'm not sure if the following is a reasonable amount of work for what you're trying to do, but Python has a very nice Command Line Interface (CLI) building library called cmd2. If it's possible to expose the relevant parts of your apps to Python using SWIG or CTypes, then doing the rest should be easy.
Here's a nice video presentation about cmd2:
PyCon 2010:Easy command-line applications with cmd and cmd2
HTH
One possibilty is to open a TCP port and accept messages in text format. Then you can telnet to that port and issue simple commands.

Is it possible to refresh two lines of text at once using something like a CR? (C++)

Right now, I have a console application I'm working on, which is supposed to display and update information to the console at a given interval. The problem I'm having is that with a carriage return, I can only update one line of text at a time. If I use a newline, the old line can no longer be updated using a carriage return.
What can I do here?
You might be able to find a curses library variant that works on your platform.
The correct answer is to use the curses library as mentioned by Mark. But if you're on Unix-like systems and can't be bothered with curses then the quick and dirty solution is to directly print out vt100 escape sequences:
http://ascii-table.com/ansi-escape-sequences-vt-100.php
I often do this especially in scripting languages that doesn't have a curses binding.
This is a really ugly solution, but in a pinch you could always just clear the console entirely and then reprint everything. This strategy looks a bit ugly in some cases; it may make things look blinky.