Using GNU Readline; how can I add ncurses in the same program?

John Zwinck picture John Zwinck · Mar 27, 2009 · Viewed 7.4k times · Source

The title is a bit more specific than my actual goal:

I have a command-line program which uses GNU Readline, primarily for command history (i.e. retrieving previous commands using up-arrow) and some other niceties. Right now the program's output appears interspersed with the user's input, which sometimes is OK but the output is asynchronous (it comes via a network connection in response to the input commands), and that gets annoying sometimes (e.g. if lines are output when the user is typing new input).

I'd like to add a feature to this program: a separate "window" for the output. I thought about using ncurses for this. But it appears from the ncurses FAQ that the two libraries are not easy to use together.

I might consider using Editline or tecla instead of Readline, but it's not clear to me if either of those will solve my problem. I'd also consider using something other than ncurses, including a library which provides both kinds of functionality (text-mode windows and command history), but I don't know what might be best.

Oh, and support for colored text might get bonus points. I suspect I may be able to do that with Readline, so maybe it's a separate concern, but if a solution to my problem also makes it easy to add a bit of color to the output, so much the better.

I'm using Ubuntu Hardy (Linux 2.6).

Answer

Ulfalizer picture Ulfalizer · Feb 25, 2015

I've now put together a simple example program on GitHub: https://github.com/ulfalizer/readline-and-ncurses.

It supports seamless and efficient terminal resizing and multibyte/combining/wide characters. The code has helpful comments.

Screenshot below:

Screenshot of program combining ncurses and readline