예제 #1
0
파일: help_mode.c 프로젝트: ngwese/teletype
void process_help_keys(uint8_t k, uint8_t m, bool is_held_key) {
    // <down> or C-n: line down
    if (match_no_mod(m, k, HID_DOWN) || match_ctrl(m, k, HID_N)) {
        if (offset < help_length[page_no] - 8) {
            offset++;
            dirty = true;
        }
    }
    // <up> or C-p: line up
    else if (match_no_mod(m, k, HID_UP) || match_ctrl(m, k, HID_P)) {
        if (offset) {
            offset--;
            dirty = true;
        }
    }
    // <left> or [: previous page
    else if (match_no_mod(m, k, HID_LEFT) ||
             match_no_mod(m, k, HID_OPEN_BRACKET)) {
        if (page_no) {
            offset = 0;
            page_no--;
            dirty = true;
        }
    }
    // <right> or ]: next page
    else if (match_no_mod(m, k, HID_RIGHT) ||
             match_no_mod(m, k, HID_CLOSE_BRACKET)) {
        if (page_no < HELP_PAGES - 1) {
            offset = 0;
            page_no++;
            dirty = true;
        }
    }
}
예제 #2
0
void process_live_keys(uint8_t k, uint8_t m, bool is_held_key) {
    // <down> or C-n: history next
    if (match_no_mod(m, k, HID_DOWN) || match_ctrl(m, k, HID_N)) {
        if (history_line > 0) {
            history_line--;
            line_editor_set_command(&le, &history[history_line]);
        }
        else {
            history_line = -1;
            line_editor_set(&le, "");
        }
        dirty |= D_INPUT;
    }
    // <up> or C-p: history previous
    else if (match_no_mod(m, k, HID_UP) || match_ctrl(m, k, HID_P)) {
        if (history_line < history_top) {
            history_line++;
            line_editor_set_command(&le, &history[history_line]);
            dirty |= D_INPUT;
        }
    }
    // <enter>: execute command
    else if (match_no_mod(m, k, HID_ENTER)) {
        dirty |= D_MESSAGE;  // something will definitely happen
        dirty |= D_INPUT;

        tele_command_t command;

        status = parse(line_editor_get(&le), &command, error_msg);
        if (status != E_OK)
            return;  // quit, screen_refresh_live will display the error message

        status = validate(&command, error_msg);
        if (status != E_OK)
            return;  // quit, screen_refresh_live will display the error message

        if (command.length) {
            // increase history_size up to a maximum
            history_top++;
            if (history_top >= MAX_HISTORY_SIZE)
                history_top = MAX_HISTORY_SIZE - 1;

            // shuffle the history up
            // should really use some sort of ring buffer
            for (size_t i = history_top; i > 0; i--) {
                memcpy(&history[i], &history[i - 1], sizeof(command));
            }
            memcpy(&history[0], &command, sizeof(command));

            output = run_command(&scene_state, &command);
        }

        history_line = -1;
        line_editor_set(&le, "");
    }
    // [ or ]: switch to edit mode
    else if (match_no_mod(m, k, HID_OPEN_BRACKET) ||
             match_no_mod(m, k, HID_CLOSE_BRACKET)) {
        set_mode(M_EDIT);
    }
    else {  // pass the key though to the line editor
        bool processed = line_editor_process_keys(&le, k, m, is_held_key);
        if (processed) dirty |= D_INPUT;
    }
}