如何让readline()在ncurses窗口中工作?

时间:2018-01-15 09:32:32

标签: c++ c terminal readline ncurses

我有一个小型测试程序,如下所示,试图让ncurses窗口与readline()一起使用。

当我拨打readline()时,输入有效,但我看不到它。不知何故,回声消失了。使用getch()(如果您没有#define READLINE),则输入显示在错误的位置。

有没有办法让readline()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

0 个答案:

没有答案