How to read an incomplete form field ncurses C++

落花浮王杯 提交于 2019-12-02 03:56:25

问题


I have a code that read a form field using ncurses (C++), but i can't show a value when the form field isn't full typed.

#include <form.h>
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

WINDOW *chatwin, *entrywin;
FIELD  *field[1];
FORM   *fo;

void quit(void)
{
    int i;
    unpost_form(fo);
    free_form(fo);

    free_field(field[0]);
    free_field(field[1]);

    delwin(chatwin);
    endwin();
}

int main(void)
{
    int xsize, ysize;
    int charinput, i;
    char inputstring[200];
    char ttime[10];
    initscr();
    atexit(quit);
    clear();
    noecho();
    curs_set(1);
    cbreak();
    keypad(stdscr, TRUE);

    getmaxyx(stdscr, ysize, xsize);

    start_color();
    use_default_colors();
    init_pair(1, COLOR_YELLOW, COLOR_BLUE);
    init_pair(2, COLOR_BLUE, COLOR_WHITE);

    chatwin = newwin((ysize - 8), (xsize-21), 6, 21);

    entrywin = newwin(1, (xsize-21), (ysize - 1), 21);

    field[0] = new_field(1, (xsize - 21), 0, 0, 0, 10);
    field[1] = 0;
    set_form_win(fo, entrywin);
    fo = new_form(field);
    post_form(fo); 
    field_opts_on(field[0], O_STATIC);
    set_field_fore(field[0], COLOR_PAIR(2));
    set_field_back(field[0], COLOR_PAIR(2));

    refresh();
    wrefresh(chatwin);
    wrefresh(entrywin);

    while((charinput=getch()) != KEY_END)
    {
        switch(charinput)
        {
            case 10:
                snprintf(inputstring, 200, "%s", field_buffer(field[0], 0));
                struct tm *akttime;
                time_t second;
                time(&second);
                akttime = localtime(&second);
                strftime(ttime, 10, "%H:%M:%S", akttime);
                wprintw(chatwin, "<%s> %s|\n", ttime, inputstring);
                wrefresh(chatwin);
                set_field_buffer(field[0], 0, "");
                wrefresh(entrywin);
                break;

            default:
                form_driver(fo, charinput);
                wrefresh(entrywin);
        }
    } 

    return(0);
}

OUTPUT

Obs.: The second line was typed: ejheeh, but the output was empty

<02:31:42> dddddddkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
                     kkkkkkkkkkk|

<02:31:45>
                                |

<02:31:51> ddddddddddddddddddddddddddddddddddddddddddddddddddddfffffff
                     fffffffffff|

回答1:


Typically the ncurses form library does not synchronize the field buffer until you leave the field. Since you are attempting to read the buffer before leaving the field the buffer does not have the current content.

A simple way to force the buffer to synchronize without actually moving to the next field is to force field validation -- this will synchronize and then run any validation functions.

Add this line at the begining of your case 10:

form_driver(fo, REQ_VALIDATION);

You might want to check for validation errors:

if (form_driver(fo, REQ_VALIDATION) != E_OK) {
    // do something
}


来源:https://stackoverflow.com/questions/18493449/how-to-read-an-incomplete-form-field-ncurses-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!