| ... | ... |
@@ -16,10 +16,10 @@ options_t options; |
| 16 | 16 |
#define SEARCH_SIZE_MAX 4096 |
| 17 | 17 |
static char search[SEARCH_SIZE_MAX + 1] = {0};
|
| 18 | 18 |
|
| 19 |
-static void clear(tty_t *tty) {
|
|
| 19 |
+static void clear(tty_t *tty, options_t *options) {
|
|
| 20 | 20 |
tty_setcol(tty, 0); |
| 21 | 21 |
size_t line = 0; |
| 22 |
- while (line++ < options.num_lines) {
|
|
| 22 |
+ while (line++ < options->num_lines) {
|
|
| 23 | 23 |
tty_newline(tty); |
| 24 | 24 |
} |
| 25 | 25 |
tty_clearline(tty); |
| ... | ... |
@@ -27,7 +27,7 @@ static void clear(tty_t *tty) {
|
| 27 | 27 |
tty_flush(tty); |
| 28 | 28 |
} |
| 29 | 29 |
|
| 30 |
-static void draw_match(tty_t *tty, const char *choice, int selected) {
|
|
| 30 |
+static void draw_match(tty_t *tty, const char *choice, int selected, options_t *options) {
|
|
| 31 | 31 |
int n = strlen(search); |
| 32 | 32 |
size_t positions[n + 1]; |
| 33 | 33 |
for (int i = 0; i < n + 1; i++) |
| ... | ... |
@@ -37,7 +37,7 @@ static void draw_match(tty_t *tty, const char *choice, int selected) {
|
| 37 | 37 |
|
| 38 | 38 |
size_t maxwidth = tty_getwidth(tty); |
| 39 | 39 |
|
| 40 |
- if (options.show_scores) |
|
| 40 |
+ if (options->show_scores) |
|
| 41 | 41 |
tty_printf(tty, "(%5.2f) ", score); |
| 42 | 42 |
|
| 43 | 43 |
if (selected) |
| ... | ... |
@@ -60,29 +60,29 @@ static void draw_match(tty_t *tty, const char *choice, int selected) {
|
| 60 | 60 |
tty_setnormal(tty); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 |
-static void draw(tty_t *tty, choices_t *choices) {
|
|
| 64 |
- unsigned int num_lines = options.num_lines; |
|
| 63 |
+static void draw(tty_t *tty, choices_t *choices, options_t *options) {
|
|
| 64 |
+ unsigned int num_lines = options->num_lines; |
|
| 65 | 65 |
size_t start = 0; |
| 66 | 66 |
size_t current_selection = choices->selection; |
| 67 |
- if (current_selection + options.scrolloff >= num_lines) {
|
|
| 68 |
- start = current_selection + options.scrolloff - num_lines + 1; |
|
| 67 |
+ if (current_selection + options->scrolloff >= num_lines) {
|
|
| 68 |
+ start = current_selection + options->scrolloff - num_lines + 1; |
|
| 69 | 69 |
if (start + num_lines >= choices_available(choices)) {
|
| 70 | 70 |
start = choices_available(choices) - num_lines; |
| 71 | 71 |
} |
| 72 | 72 |
} |
| 73 | 73 |
tty_setcol(tty, 0); |
| 74 |
- tty_printf(tty, "%s%s", options.prompt, search); |
|
| 74 |
+ tty_printf(tty, "%s%s", options->prompt, search); |
|
| 75 | 75 |
tty_clearline(tty); |
| 76 | 76 |
for (size_t i = start; i < start + num_lines; i++) {
|
| 77 | 77 |
tty_printf(tty, "\n"); |
| 78 | 78 |
tty_clearline(tty); |
| 79 | 79 |
const char *choice = choices_get(choices, i); |
| 80 | 80 |
if (choice) {
|
| 81 |
- draw_match(tty, choice, i == choices->selection); |
|
| 81 |
+ draw_match(tty, choice, i == choices->selection, options); |
|
| 82 | 82 |
} |
| 83 | 83 |
} |
| 84 | 84 |
tty_moveup(tty, num_lines); |
| 85 |
- tty_setcol(tty, strlen(options.prompt) + strlen(search)); |
|
| 85 |
+ tty_setcol(tty, strlen(options->prompt) + strlen(search)); |
|
| 86 | 86 |
tty_flush(tty); |
| 87 | 87 |
} |
| 88 | 88 |
|
| ... | ... |
@@ -101,11 +101,11 @@ static void emit(choices_t *choices) {
|
| 101 | 101 |
#define KEY_DEL 127 |
| 102 | 102 |
#define KEY_ESC 27 |
| 103 | 103 |
|
| 104 |
-static void run(tty_t *tty, choices_t *choices) {
|
|
| 104 |
+static void run(tty_t *tty, choices_t *choices, options_t *options) {
|
|
| 105 | 105 |
choices_search(choices, search); |
| 106 | 106 |
char ch; |
| 107 | 107 |
do {
|
| 108 |
- draw(tty, choices); |
|
| 108 |
+ draw(tty, choices, options); |
|
| 109 | 109 |
ch = tty_getchar(tty); |
| 110 | 110 |
size_t search_size = strlen(search); |
| 111 | 111 |
if (isprint(ch)) {
|
| ... | ... |
@@ -136,11 +136,11 @@ static void run(tty_t *tty, choices_t *choices) {
|
| 136 | 136 |
strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX); |
| 137 | 137 |
choices_search(choices, search); |
| 138 | 138 |
} else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */
|
| 139 |
- clear(tty); |
|
| 139 |
+ clear(tty, options); |
|
| 140 | 140 |
tty_close(tty); |
| 141 | 141 |
exit(EXIT_FAILURE); |
| 142 | 142 |
} else if (ch == KEY_CTRL('M')) { /* CR */
|
| 143 |
- clear(tty); |
|
| 143 |
+ clear(tty, options); |
|
| 144 | 144 |
|
| 145 | 145 |
/* ttyout should be flushed before outputting on stdout */ |
| 146 | 146 |
tty_close(tty); |
| ... | ... |
@@ -198,7 +198,7 @@ int main(int argc, char *argv[]) {
|
| 198 | 198 |
if (options.init_search) |
| 199 | 199 |
strncpy(search, options.init_search, SEARCH_SIZE_MAX); |
| 200 | 200 |
|
| 201 |
- run(&tty, &choices); |
|
| 201 |
+ run(&tty, &choices, &options); |
|
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
choices_destroy(&choices); |