Ncurses form.h,但是如何实际使用输入呢?

问题描述 投票:0回答:1

所以,我一直在摆弄 ncurses 的 form.h,这很棒,菜单似乎可以工作。但是,只是缺少一些东西。我到底该如何处理已输入的数据?我在互联网上搜索某种形式的示例,但在所有参考文献中我都找不到任何人使用表单数据进行任何操作。当然,我们有一个很好的方法来将文本输入到表单的字段中,但随后,我们通过释放表单和字段来销毁数据,然后我们甚至不再碰触。我们绝对不会用它来做任何事。

我到底可以如何使用这些字段?我们在谈论 field_buffer 吗?

#include <form.h>

int main() {
    FIELD *field[4];
    FORM  *my_form;
    int ch;

    /* Initialize curses */
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    /* Initialize the fields */
    field[0] = new_field(1, 30, 1, 25, 0, 0);
    field[1] = new_field(1, 30, 3, 25, 0, 0);
    field[2] = new_field(1, 30, 4, 25, 0, 0);
    field[3] = new_field(1, 30, 5, 25, 0, 0);


    /* Set field options */

    for (int i = 0; i < 4; i++)
    {
        set_field_back(field[i], A_UNDERLINE);
        field_opts_off(field[i], O_AUTOSKIP);
    }


    /* Create the form and post it */
    my_form = new_form(field);
    post_form(my_form);
    refresh();

    mvprintw(0, 20, "");
    mvprintw(1, 10, "1:");
    mvprintw(2, 20, "");
    mvprintw(3, 10, "2:");
    mvprintw(4, 10, "3:");
    mvprintw(5, 10, "4:");
    mvprintw(6, 10, "Send (F1)");
    refresh();

    form_driver(my_form, REQ_FIRST_FIELD);

    /* Loop through to get user requests */
    while((ch = getch()) != KEY_F(1))
    {   switch(ch)
        {   case KEY_DOWN:
                /* Go to next field */
                form_driver(my_form, REQ_NEXT_FIELD);
                form_driver(my_form, REQ_END_LINE);
                break;
            case KEY_UP:
                /* Go to previous field */
                form_driver(my_form, REQ_PREV_FIELD);
                form_driver(my_form, REQ_END_LINE);
                break;
            case KEY_BACKSPACE:
                /* Delete last character */
                form_driver(my_form, REQ_DEL_PREV);
                break;
            case KEY_DC:
                /* Delete contents of field */
                form_driver(my_form, REQ_CLR_FIELD);
                break;
            case KEY_ENTER:
                form_driver(my_form, REQ_NEXT_FIELD);
                form_driver(my_form, REQ_END_LINE);
                break;
            default:
                /* If this is a normal character, it gets */
                /* Printed                */    
                form_driver(my_form, ch);
                break;
        }
    }
    form_driver(my_form, REQ_VALIDATION);
    for (int i = 0; i < 4; i++)
    {
        mvprintw(7+i, 20, field_buffer(field[i], 0)); // is this even workable?
    }
    /* Un post form and free the memory */
    unpost_form(my_form);
    free_form(my_form);
    for (int i = 0; i < 4; i++)
    {
        free_field(field[i]);
    }

    endwin();
    return 0;
}
c forms ncurses
1个回答
0
投票

确实如此!经过几个小时的研究和摆弄后,这是一个工作示例 - 基本上只是该英雄的 code 的精简版本,并添加了一些有用的附加内容,例如

scale_form()
form_win()
。令我惊讶的是,该文档并未涵盖除了几个最复杂的示例程序之外检索字段输入。

#include <ncurses.h>
#include <form.h>
#include <string.h>
#include <ctype.h>

static char*
trim_whitespaces (char *str)
{
    char *end;

    while(isspace(*str))
        str++;
    if(*str == 0)
        return str;
    end = str + strnlen(str, 128) - 1;
    while(end > str && isspace(*end))
        end--;
    *(end+1) = '\0';

    return str;
}

int
main (void)
{
    WINDOW *win_body;
    FIELD  *field[3];
    FORM   *form;
    int     ch,
            rows, cols, y, x;
    bool    running;

    // start ncurses
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    start_color();
    refresh();

    // fields
    char *prompt = ">> ";
    init_pair (1, COLOR_WHITE, COLOR_BLUE);
    cols = 48;
    y = 10;
    x = 10;
    field[0] = new_field(2, strlen(prompt), 0, 0, 0, 0);
    field[1] = new_field(2, cols - strlen(prompt), 0, strlen(prompt), 0, 0);
    field[2] = NULL;
    set_field_opts   (field[0], O_VISIBLE | O_PUBLIC | O_AUTOSKIP);
    set_field_opts   (field[1], O_VISIBLE | O_PUBLIC | O_EDIT | O_ACTIVE);
    set_field_buffer (field[0], 0, prompt);
    set_field_back   (field[0], COLOR_PAIR(1));
    set_field_back   (field[1], COLOR_PAIR(1));

    // form windows
    form = new_form (field);
    scale_form (form, &rows, &cols);
    win_body = newwin (rows+2, cols+2, y, x);
    box (win_body, 0, 0);
    set_form_win (form, win_body);
    set_form_sub (form, derwin (win_body, rows, cols, 1, 1));

    post_form (form);
    wrefresh (win_body);

    // get input
    running = true;
    while (running) {

        ch = getch();

        switch (ch) {
            case '\n':
                form_driver (form, REQ_VALIDATION);
                running = false;
                break;
            case KEY_BACKSPACE:
            case 127:
            case '\b':
                form_driver (form, REQ_DEL_PREV);
                break;
            case KEY_DOWN:
                form_driver (form, REQ_NEXT_FIELD);
                form_driver (form, REQ_END_LINE);
                break;
            case KEY_UP:
                form_driver (form, REQ_PREV_FIELD);
                form_driver (form, REQ_END_LINE);
                break;
            case KEY_LEFT:
                form_driver (form, REQ_PREV_CHAR);
                break;
            case KEY_RIGHT:
                form_driver (form, REQ_NEXT_CHAR);
                break;
            case KEY_DC:
                form_driver (form, REQ_DEL_CHAR);
                break;
            case KEY_HOME:
                form_driver (form, REQ_BEG_LINE);
                break;
            case KEY_END:
                form_driver (form, REQ_END_LINE);
                break;
            default:
                form_driver (form, ch);
                break;
        }

        wrefresh (form_win (form));
    }

    // print input
    mvwprintw(stdscr, 20, 10, "You entered: \"%s\"", trim_whitespaces (field_buffer (field[1], 0)));
    getch();

    unpost_form(form);
    free_form(form);
    free_field(field[0]);
    endwin();

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.