Browse code

Pass options to choices_init

John Hawthorn authored on 01/02/2017 02:06:42
Showing 7 changed files

... ...
@@ -13,7 +13,7 @@ INSTALL_DATA=${INSTALL} -m 644
13 13
 
14 14
 LIBS=-lpthread
15 15
 OBJECTS=src/fzy.o src/match.o src/tty.o src/choices.o src/options.o src/tty_interface.o
16
-TESTOBJECTS=test/fzytest.c src/match.o src/choices.o
16
+TESTOBJECTS=test/fzytest.c src/match.o src/choices.o src/options.o
17 17
 
18 18
 all: fzy
19 19
 
... ...
@@ -5,6 +5,7 @@
5 5
 #include <unistd.h>
6 6
 #include <errno.h>
7 7
 
8
+#include "options.h"
8 9
 #include "choices.h"
9 10
 #include "match.h"
10 11
 
... ...
@@ -96,7 +97,7 @@ static void choices_reset_search(choices_t *c) {
96 97
 	c->results = NULL;
97 98
 }
98 99
 
99
-void choices_init(choices_t *c) {
100
+void choices_init(choices_t *c, options_t *options) {
100 101
 	c->strings = NULL;
101 102
 	c->results = NULL;
102 103
 
... ...
@@ -4,6 +4,7 @@
4 4
 #include <stdio.h>
5 5
 
6 6
 #include "match.h"
7
+#include "options.h"
7 8
 
8 9
 struct scored_result {
9 10
 	score_t score;
... ...
@@ -26,7 +27,7 @@ typedef struct {
26 27
 	unsigned int worker_count;
27 28
 } choices_t;
28 29
 
29
-void choices_init(choices_t *c);
30
+void choices_init(choices_t *c, options_t *options);
30 31
 void choices_fread(choices_t *c, FILE *file);
31 32
 void choices_destroy(choices_t *c);
32 33
 void choices_add(choices_t *c, const char *choice);
... ...
@@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
19 19
 	options_parse(&options, argc, argv);
20 20
 
21 21
 	choices_t choices;
22
-	choices_init(&choices);
22
+	choices_init(&choices, &options);
23 23
 	choices_fread(&choices, stdin);
24 24
 
25 25
 	if (options.benchmark) {
... ...
@@ -30,10 +30,11 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'
30 30
 				   {"show-scores", no_argument, NULL, 's'},
31 31
 				   {"version", no_argument, NULL, 'v'},
32 32
 				   {"benchmark", optional_argument, NULL, 'b'},
33
+				   {"workers", required_argument, NULL, 'j'},
33 34
 				   {"help", no_argument, NULL, 'h'},
34 35
 				   {NULL, 0, NULL, 0}};
35 36
 
36
-void options_set_defaults(options_t *options) {
37
+void options_init(options_t *options) {
37 38
 	/* set defaults */
38 39
 	options->benchmark = 0;
39 40
 	options->filter = NULL;
... ...
@@ -46,7 +47,7 @@ void options_set_defaults(options_t *options) {
46 47
 }
47 48
 
48 49
 void options_parse(options_t *options, int argc, char *argv[]) {
49
-	options_set_defaults(options);
50
+	options_init(options);
50 51
 
51 52
 	int c;
52 53
 	while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:", longopts, NULL)) != -1) {
... ...
@@ -12,6 +12,7 @@ typedef struct {
12 12
 	const char *prompt;
13 13
 } options_t;
14 14
 
15
+void options_init(options_t *options);
15 16
 void options_parse(options_t *options, int argc, char *argv[]);
16 17
 
17 18
 #endif
... ...
@@ -7,6 +7,7 @@
7 7
 #include "../config.h"
8 8
 #include "match.h"
9 9
 #include "choices.h"
10
+#include "options.h"
10 11
 
11 12
 int testsrun = 0, testsfailed = 0, assertionsrun = 0;
12 13
 
... ...
@@ -21,6 +22,8 @@ int testsrun = 0, testsfailed = 0, assertionsrun = 0;
21 22
 
22 23
 #define assert_streq(a, b) assert(!strcmp(a, b))
23 24
 
25
+static options_t default_options;
26
+
24 27
 void runtest(void (*test)()) {
25 28
 	testsrun++;
26 29
 	test();
... ...
@@ -160,7 +163,7 @@ void test_positions_exact() {
160 163
 
161 164
 void test_choices_empty() {
162 165
 	choices_t choices;
163
-	choices_init(&choices);
166
+	choices_init(&choices, &default_options);
164 167
 	assert(choices.size == 0);
165 168
 	assert(choices.available == 0);
166 169
 	assert(choices.selection == 0);
... ...
@@ -176,7 +179,7 @@ void test_choices_empty() {
176 179
 
177 180
 void test_choices_1() {
178 181
 	choices_t choices;
179
-	choices_init(&choices);
182
+	choices_init(&choices, &default_options);
180 183
 	choices_add(&choices, "tags");
181 184
 
182 185
 	choices_search(&choices, "");
... ...
@@ -201,7 +204,7 @@ void test_choices_1() {
201 204
 
202 205
 void test_choices_2() {
203 206
 	choices_t choices;
204
-	choices_init(&choices);
207
+	choices_init(&choices, &default_options);
205 208
 	choices_add(&choices, "tags");
206 209
 	choices_add(&choices, "test");
207 210
 
... ...
@@ -253,7 +256,7 @@ void test_choices_without_search() {
253 256
 	/* Before a search is run, it should return no results */
254 257
 
255 258
 	choices_t choices;
256
-	choices_init(&choices);
259
+	choices_init(&choices, &default_options);
257 260
 
258 261
 	assert(choices.available == 0);
259 262
 	assert(choices.selection == 0);
... ...
@@ -273,7 +276,7 @@ void test_choices_without_search() {
273 276
 /* Regression test for segfault */
274 277
 void test_choices_unicode() {
275 278
 	choices_t choices;
276
-	choices_init(&choices);
279
+	choices_init(&choices, &default_options);
277 280
 
278 281
 	choices_add(&choices, "Edmund Husserl - Méditations cartésiennes - Introduction a la phénoménologie.pdf");
279 282
 	choices_search(&choices, "e");
... ...
@@ -283,7 +286,7 @@ void test_choices_unicode() {
283 286
 
284 287
 void test_choices_large_input() {
285 288
 	choices_t choices;
286
-	choices_init(&choices);
289
+	choices_init(&choices, &default_options);
287 290
 
288 291
 	int N = 100000;
289 292
 	char *strings[N];
... ...
@@ -323,6 +326,8 @@ int main(int argc, char *argv[]) {
323 326
 	 * If we have no debugger running, we should ignore it */
324 327
 	signal(SIGTRAP, ignore_signal);
325 328
 
329
+	options_init(&default_options);
330
+
326 331
 	runtest(test_match);
327 332
 	runtest(test_relative_scores);
328 333
 	runtest(test_exact_scores);