-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.c
More file actions
72 lines (63 loc) · 1.88 KB
/
Copy pathstate.c
File metadata and controls
72 lines (63 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdlib.h>
#include <string.h>
#include "cmenu.h"
State* new_state(const Config* config, const Input* input){
State* state = (State*) malloc(sizeof(State));
panic_if_null(state);
state->active = 0;
state->scrollIndex = 0;
state->focus = 0;
state->text[0] = '\0';
state->options_count = input->input_count;
state->options = input->options;
state->filtered_options_count = 0;
state->filtered_options = (const char**) malloc(input->input_count * sizeof(const char*));
panic_if_null(state->filtered_options);
state->show_shortcuts = false;
state->quit = false;
state->exit_status = 0;
return state;
}
void state_free(State* state){
free(state->filtered_options);
free(state);
}
void state_select_next(State* state){
state->active++;
if (state->active >= state->filtered_options_count) state->active = 0;
}
void state_select_previous(State* state){
state->active--;
if (state->active < 0) state->active = state->filtered_options_count - 1;
}
void filter_by_prefix(
const char* prefix,
const char** options,
const int options_count,
const char** filtered_options,
int* filtered_options_count
) {
if (prefix == NULL || prefix[0] == '\0') {
*filtered_options_count = options_count;
for (int i = 0; i < options_count; i++) {
filtered_options[i] = options[i];
}
return;
}
*filtered_options_count = 0;
for (int i = 0; i < options_count; i++) {
if (strncasecmp(options[i], prefix, strlen(prefix)) == 0) {
filtered_options[*filtered_options_count] = options[i];
(*filtered_options_count)++;
}
}
}
void state_filter(State* state){
filter_by_prefix(
state->text,
state->options,
state->options_count,
state->filtered_options,
&state->filtered_options_count
);
}