0

I have a small test program as follow trying to get a two window ncurses screen to work with readline().

When I call readline(), the typing works, but I can't see it. Somehow the echoing is lost. With the getch() (if you don't #define READLINE) then the typing appears at the wrong place.

Is there a way to get readline() to work along ncurse?

#include <curses.h>
#include <readline/readline.h>

#define READLINE

int main(int argc, char *argv[])
{
    WINDOW * main_win = initscr();

    int screen_width(1), screen_height(1);
    getmaxyx(main_win, screen_height, screen_width);

    wborder(main_win, 0, 0, 0, 0, 0, 0, 0, 0);
    mvwaddch(main_win, screen_height - 6, 0, ACS_LTEE);
    mvwhline(main_win, screen_height - 6, 1, ACS_HLINE, screen_width - 2);
    mvwaddch(main_win, screen_height - 6, screen_width - 1, ACS_RTEE);
    mvwprintw(main_win, 0, 2, " Output Window ");
    mvwprintw(main_win, screen_height - 6, 2, " Input Window ");
    wrefresh(main_win);

    WINDOW * win0 = newwin(screen_height - 8, screen_width - 2, 1, 1);
    WINDOW * win1 = newwin(4, screen_width - 2, screen_height - 5, 1);

    wrefresh(win0);
    wrefresh(win1);

    scrollok(win0, TRUE);
    scrollok(win1, TRUE);

    for(;;)
    {
        mvwprintw(win1, 3, 0, "tcp> ");
        wrefresh(win1);

#ifndef READLINE
        char c(getch());
        if(c == '\n')
        {
            scroll(win1);
        }
#else
        char * l(readline(""));
        char c(*l);
        if(c == '\0')
        {
            break;
        }
        scroll(win1);
#endif

        if(c == 'q')
        {
            break;
        }
    }

    endwin();
    return 0;
}

// vim: ts=4 sw=4 et
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • 2
    C or C++? It can't be both. – Barmar Jan 15 '18 at 09:34
  • I use C++ but those functions are clearly C, if you know the answer for C, it will work just fine for me. – Alexis Wilke Jan 15 '18 at 09:35
  • 1
    @Barmar why not? You can use ncurses in both – Pablo Jan 15 '18 at 09:35
  • @Pablo They're different languages, although they have many things in common. – Barmar Jan 15 '18 at 09:37
  • 2
    @AlexisWilke as you may already know readline uses internally ncurses, so it may not be very easy to integrate them with ncurses apps. Looking for *readline integration in ncurses app* in google I found this: https://github.com/ulfalizer/readline-and-ncurses this look promising. – Pablo Jan 15 '18 at 09:37
  • @Barmar I know that, I'm very active in the `c` tag here and always complain about the "C/C++" posts. However in this case I don't see any reason to be so pedantic as with SO beginners. And both ncurses and readline can be used in C and C++, so it's perfectly reasonable to ask for a solution in both. – Pablo Jan 15 '18 at 09:40
  • @Pablo, ah, I did not realize that `readline()` was using `ncurse` under the hood. That would explain some other "strange" things, though. That link seems to be my answer, though. I'll have to look at it tomorrow. It's getting late here. – Alexis Wilke Jan 15 '18 at 09:55
  • @AlexisWilke see also https://stackoverflow.com/questions/691652/using-gnu-readline-how-can-i-add-ncurses-in-the-same-program – Pablo Jan 15 '18 at 10:03
  • 1
    readline is a [*termcap* application](https://invisible-island.net/ncurses/ncurses.faq.html#uses_of_library) (can't really say it "uses" ncurses). Its initialization and hooks aren't well documented, so you'll have to read the source code to develop any non-trivial example using it. Looking at the suggested duplicate, the accepted answer doesn't actually answer the question. The other answers appear useful. – Thomas Dickey Jan 15 '18 at 11:02
  • 1
    If possible, just DON'T. Readline was developped to offer line interface command a way to easily reuse what had been typed in a previous line. It normally should not be used in curses interface – Serge Ballesta Jan 15 '18 at 12:38
  • @Pablo That worked! I have to redraw the readline string each time a character is typed, but the final result works just fine. Not only that Ulfalizer (the author) has the resize handled too. So even the resize works! – Alexis Wilke Jan 16 '18 at 06:19

0 Answers0