Browse code

Store state in tty_interface_t

John Hawthorn authored on 20/06/2016 06:17:53
Showing 4 changed files

... ...
@@ -46,7 +46,9 @@ int main(int argc, char *argv[]) {
46 46
 		if (options.num_lines + 1 > tty_getheight(&tty))
47 47
 			options.num_lines = tty_getheight(&tty) - 1;
48 48
 
49
-		tty_interface_run(&tty, &choices, &options);
49
+		tty_interface_t tty_interface;
50
+		tty_interface_init(&tty_interface, &tty, &choices, &options);
51
+		tty_interface_run(&tty_interface);
50 52
 	}
51 53
 
52 54
 	choices_destroy(&choices);
... ...
@@ -1,3 +1,6 @@
1
+#ifndef OPTIONS_H
2
+#define OPTIONS_H OPTIONS_H
3
+
1 4
 typedef struct {
2 5
 	int benchmark;
3 6
 	const char *filter;
... ...
@@ -10,3 +13,5 @@ typedef struct {
10 13
 } options_t;
11 14
 
12 15
 void options_parse(options_t *options, int argc, char *argv[]);
16
+
17
+#endif
... ...
@@ -94,9 +94,20 @@ static void emit(choices_t *choices) {
94 94
 #define KEY_DEL 127
95 95
 #define KEY_ESC 27
96 96
 
97
-void tty_interface_run(tty_t *tty, choices_t *choices, options_t *options) {
97
+void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices, options_t *options) {
98
+	state->tty = tty;
99
+	state->choices = choices;
100
+	state->options = options;
101
+
98 102
 	if (options->init_search)
99
-		strncpy(search, options->init_search, SEARCH_SIZE_MAX);
103
+		strncpy(state->search, options->init_search, SEARCH_SIZE_MAX);
104
+}
105
+
106
+void tty_interface_run(tty_interface_t *state) {
107
+	tty_t *tty = state->tty;
108
+	choices_t *choices = state->choices;
109
+	options_t *options = state->options;
110
+	char *search = state->search;
100 111
 
101 112
 	choices_search(choices, search);
102 113
 	char ch;
... ...
@@ -7,6 +7,15 @@
7 7
 
8 8
 #define SEARCH_SIZE_MAX 4096
9 9
 
10
-void tty_interface_run(tty_t *tty, choices_t *choices, options_t *options);
10
+typedef struct {
11
+	tty_t *tty;
12
+	choices_t *choices;
13
+	options_t *options;
14
+
15
+	char search[SEARCH_SIZE_MAX + 1];
16
+} tty_interface_t;
17
+
18
+void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices, options_t *options);
19
+void tty_interface_run(tty_interface_t *state);
11 20
 
12 21
 #endif