使用管道或重定向时,Ncurses闪烁

时间:2019-08-18 17:19:15

标签: c input ncurses getch

使用“ unix管道”和“重定向”进行输入时,Ncurses闪烁。就是说,如果我输入了自己,但使用'|'时却没有输入或“ <”。

我认为这可能是由于getch() 延迟模式 (无延迟,半延迟和无限延迟)引起的。因此,我明确尝试设置nodelay(stdscr, FALSE);,但显然并不能解决问题。

这是最小的工作代码:

#include <ncurses.h>
#include <stdlib.h>
#include <string.h>

/* Default assumptions */
#define BUFSIZE         100
#define SELINDICATOR    ">>> "
#define MAXITEMS        LINES   /* Decides how many items are shown at a time. By default, it's (number of rows - 1) */

/* Declarations */
static void draw(char **data, short index, short selected);
static void handleInput(short *selected, short index);

int main(int argc, char *argv[]) {

    char buf[BUFSIZE], **data;
    short index = 0, selected = 1;
    size_t curSize = 0;

    /* Get the entries */
    while(fgets(buf, BUFSIZE, stdin)) {

        if(!(data = realloc(data, (curSize += sizeof(char *))))) {
            fprintf(stderr, "error reallocating memory!\n");
            exit(1);
        }

        if(!(data[index] = malloc(BUFSIZE))) {
            fprintf(stderr, "error reallocating memory!\n");
            exit(1);
        }

        strcpy(data[index], buf);
        index++;
    }

    /* Start nCurses */
    initscr();
    noecho();
    nodelay(stdscr, FALSE); // just tryin' it out if it works

    while(1) {

        draw(data, index, selected);
        handleInput(&selected, index);
    }

    /* Quit nCurses */
    endwin();

    /* Free allocated memories */
    for(short i = 0; i < index; i++)
        free(data[i]);
    free(data);

    return 0;
}

void
draw(char **data, short index, short selected) {

    static short posX = strlen(SELINDICATOR), posY; /* posY doesn't need to be static but it makes no difference and looks cleaner */

        /* Clear old garbage */
        clear();
        posY = 0;

        /* Draw line echoing inputs */
        mvaddch(posY, 0, '>');
        posY++;

        /* Draw the entries */
        for(short i = 0; posY < COLS && i < index; i++) {

            if(posY == selected) {
                mvprintw(posY, 0, SELINDICATOR);
            }

            mvprintw(posY, posX, "%s", data[i]);
            refresh();
            posY++;
        }

        /* Make the output visible */
        refresh();
}

void
handleInput(short *selected, short numOfEntries) {

    int input = getch();

    /* A whole bunch of other stuff........ */

    endwin();
    exit(0);
}

非常感谢您的努力!

2 个答案:

答案 0 :(得分:0)

由于此功能,该示例缺少某些内容

void
handleInput(short *selected, short numOfEntries) {

    int input = getch();

    /* A whole bunch of other stuff........ */

    endwin();
    exit(0);
}

运行一次后将简单退出。这就留下了很多可能性,最有可能的是您多次运行该程序,导致它初始化了屏幕(在很多终端上,切换到< em>备用屏幕)。每次都会闪烁...

答案 1 :(得分:0)

Ncurses被设计和构建为用于提供 interactive 用户界面的工具。就其从标准输入(而不是直接从终端)中读取输入的程度而言,基于ncurses的程序可能会将其输入从文件或管道重定向,但是尚不清楚为什么它很重要在这种情况下实际显示用户界面。如果这样做会导致不良的视觉效果,那么最简单的缓解措施就是在这种情况下禁用UI。

在问题提出的程序中,似乎显示UI与读取和处理输入完全分开,并且读取输入仅最小程度地依赖ncurses。修改此类程序以使其能够在UI模式和无UI模式之间切换应该非常简单,我建议您这样做。为此,您可能会发现isatty()函数对于确定标准输入(和/或标准输出)是否为终端很有用。