11 * Listen for the user input and call the appropriate functions.
14 input_get(Buffer
*buffer
, int offset
, int tty_fd
, Opt
*opt
)
16 FILE *tty_fp
= fopen("/dev/tty", "r");
18 /* receive one character at a time from the terminal */
19 struct termios termio_old
= set_terminal(tty_fd
);
21 /* get input char by char from the keyboard. */
22 while (input_key(fgetc(tty_fp
), buffer
, opt
)) {
23 draw_screen(buffer
, offset
, tty_fd
, opt
);
26 /* resets the terminal to the previous state. */
27 tcsetattr(tty_fd
, TCSANOW
, &termio_old
);
33 * Perform action associated with key
36 input_key(char key
, Buffer
*buffer
, Opt
*opt
)
38 if (key
== opt
->validate_key
) {
39 action_print_selection(buffer
, opt
);
46 draw_clear(opt
->lines
);
50 buffer
->input
[0] = '\0';
51 buffer
->current
= buffer
->first
;
52 filter_lines(buffer
, opt
);
53 action_jump(buffer
, BOTH
, opt
);
57 action_remove_word_input(buffer
);
58 filter_lines(buffer
, opt
);
62 case CONTROL('H'): /* backspace */
63 buffer
->input
[strlen(buffer
->input
) - 1] = '\0';
64 filter_lines(buffer
, opt
);
66 if (!buffer
->current
->matches
) {
67 action_jump(buffer
, BOTH
, opt
);
72 action_jump(buffer
, NEXT
, opt
);
76 action_jump(buffer
, PREV
, opt
);
79 case CONTROL('I'): /* tab */
80 strcpy(buffer
->input
, buffer
->current
->content
);
81 filter_lines(buffer
, opt
);
85 case CONTROL('J'): /* enter */
86 action_print_selection(buffer
, opt
);
90 action_add_character(buffer
, key
, opt
);
97 * Set the current line to the next/previous/closest matching line.
100 action_jump(Buffer
*buffer
, int direction
, Opt
*opt
)
102 Line
* line
= buffer
->current
;
104 if (direction
== BOTH
) {
105 line
= matching_next(buffer
->current
);
106 line
= (line
) ? line
: matching_prev(buffer
->current
);
107 } else if (direction
== NEXT
) {
108 line
= matching_next(line
);
109 } else if (direction
== PREV
) {
110 line
= matching_prev(line
);
113 buffer
->current
= (line
) ? line
: buffer
->current
;
115 if (opt
->print_numbers
)
116 action_print_selection(buffer
, opt
);
120 * Send the selection to stdout.
123 action_print_selection(Buffer
*buffer
, Opt
*opt
)
125 fputs("\r\033[K", stderr
);
127 if (opt
->print_numbers
) {
128 printf("%d\n", buffer
->current
->number
);
133 puts(buffer
->current
->content
);
139 * Remove the last word from the buffer's input
142 action_remove_word_input(Buffer
*buffer
)
144 size_t length
= strlen(buffer
->input
) - 1;
147 for (i
= length
; i
>= 0 && isspace(buffer
->input
[i
]); i
--) {
148 buffer
->input
[i
] = '\0';
151 length
= strlen(buffer
->input
) - 1;
152 for (i
= length
; i
>= 0 && !isspace(buffer
->input
[i
]); i
--) {
153 buffer
->input
[i
] = '\0';
158 * Add a character to the buffer input and filter lines again.
161 action_add_character(Buffer
*buffer
, char key
, Opt
*opt
)
163 size_t length
= strlen(buffer
->input
);
166 buffer
->input
[length
] = key
;
167 buffer
->input
[length
+ 1] = '\0';
170 filter_lines(buffer
, opt
);
172 if (!buffer
->current
->matches
) {
173 action_jump(buffer
, BOTH
, opt
);
176 if (!buffer
->current
->matches
) {
177 buffer
->current
= buffer
->empty
;