Browse code

Use tty_interface_t throughout

John Hawthorn authored on 20/06/2016 06:28:51
Showing 1 changed files

... ...
@@ -7,12 +7,12 @@
7 7
 #include "tty_interface.h"
8 8
 #include "../config.h"
9 9
 
10
-static char search[SEARCH_SIZE_MAX + 1];
10
+static void clear(tty_interface_t *state) {
11
+	tty_t *tty = state->tty;
11 12
 
12
-static void clear(tty_t *tty, options_t *options) {
13 13
 	tty_setcol(tty, 0);
14 14
 	size_t line = 0;
15
-	while (line++ < options->num_lines) {
15
+	while (line++ < state->options->num_lines) {
16 16
 		tty_newline(tty);
17 17
 	}
18 18
 	tty_clearline(tty);
... ...
@@ -20,7 +20,11 @@ static void clear(tty_t *tty, options_t *options) {
20 20
 	tty_flush(tty);
21 21
 }
22 22
 
23
-static void draw_match(tty_t *tty, const char *choice, int selected, options_t *options) {
23
+static void draw_match(tty_interface_t *state, const char *choice, int selected) {
24
+	tty_t *tty = state->tty;
25
+	options_t *options = state->options;
26
+	char *search = state->search;
27
+
24 28
 	int n = strlen(search);
25 29
 	size_t positions[n + 1];
26 30
 	for (int i = 0; i < n + 1; i++)
... ...
@@ -53,7 +57,11 @@ static void draw_match(tty_t *tty, const char *choice, int selected, options_t *
53 57
 	tty_setnormal(tty);
54 58
 }
55 59
 
56
-static void draw(tty_t *tty, choices_t *choices, options_t *options) {
60
+static void draw(tty_interface_t *state) {
61
+	tty_t *tty = state->tty;
62
+	choices_t *choices = state->choices;
63
+	options_t *options = state->options;
64
+
57 65
 	unsigned int num_lines = options->num_lines;
58 66
 	size_t start = 0;
59 67
 	size_t current_selection = choices->selection;
... ...
@@ -64,29 +72,31 @@ static void draw(tty_t *tty, choices_t *choices, options_t *options) {
64 72
 		}
65 73
 	}
66 74
 	tty_setcol(tty, 0);
67
-	tty_printf(tty, "%s%s", options->prompt, search);
75
+	tty_printf(tty, "%s%s", options->prompt, state->search);
68 76
 	tty_clearline(tty);
69 77
 	for (size_t i = start; i < start + num_lines; i++) {
70 78
 		tty_printf(tty, "\n");
71 79
 		tty_clearline(tty);
72 80
 		const char *choice = choices_get(choices, i);
73 81
 		if (choice) {
74
-			draw_match(tty, choice, i == choices->selection, options);
82
+			draw_match(state, choice, i == choices->selection);
75 83
 		}
76 84
 	}
77 85
 	tty_moveup(tty, num_lines);
78
-	tty_setcol(tty, strlen(options->prompt) + strlen(search));
86
+	tty_setcol(tty, strlen(options->prompt) + strlen(state->search));
79 87
 	tty_flush(tty);
80 88
 }
81 89
 
82
-static void emit(choices_t *choices) {
90
+static void emit(tty_interface_t *state) {
91
+	choices_t *choices = state->choices;
92
+
83 93
 	const char *selection = choices_get(choices, choices->selection);
84 94
 	if (selection) {
85 95
 		/* output the selected result */
86 96
 		printf("%s\n", selection);
87 97
 	} else {
88 98
 		/* No match, output the query instead */
89
-		printf("%s\n", search);
99
+		printf("%s\n", state->search);
90 100
 	}
91 101
 }
92 102
 
... ...
@@ -106,13 +116,12 @@ void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices,
106 116
 void tty_interface_run(tty_interface_t *state) {
107 117
 	tty_t *tty = state->tty;
108 118
 	choices_t *choices = state->choices;
109
-	options_t *options = state->options;
110 119
 	char *search = state->search;
111 120
 
112 121
 	choices_search(choices, search);
113 122
 	char ch;
114 123
 	do {
115
-		draw(tty, choices, options);
124
+		draw(state);
116 125
 		ch = tty_getchar(tty);
117 126
 		size_t search_size = strlen(search);
118 127
 		if (isprint(ch)) {
... ...
@@ -143,16 +152,16 @@ void tty_interface_run(tty_interface_t *state) {
143 152
 			strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
144 153
 			choices_search(choices, search);
145 154
 		} else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */
146
-			clear(tty, options);
155
+			clear(state);
147 156
 			tty_close(tty);
148 157
 			exit(EXIT_FAILURE);
149 158
 		} else if (ch == KEY_CTRL('M')) { /* CR */
150
-			clear(tty, options);
159
+			clear(state);
151 160
 
152 161
 			/* ttyout should be flushed before outputting on stdout */
153 162
 			tty_close(tty);
154 163
 
155
-			emit(choices);
164
+			emit(state);
156 165
 
157 166
 			/* Return to eventually exit successfully */
158 167
 			return;