From 15b29a48af2175f620d0acad00bac59d2d5d8630 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 31 Jan 2017 18:06:42 -0800 Subject: Pass options to choices_init --- Makefile | 2 +- src/choices.c | 3 ++- src/choices.h | 3 ++- src/fzy.c | 2 +- src/options.c | 5 +++-- src/options.h | 1 + test/fzytest.c | 17 +++++++++++------ 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index e75f20e..c0a37b5 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ INSTALL_DATA=${INSTALL} -m 644 LIBS=-lpthread OBJECTS=src/fzy.o src/match.o src/tty.o src/choices.o src/options.o src/tty_interface.o -TESTOBJECTS=test/fzytest.c src/match.o src/choices.o +TESTOBJECTS=test/fzytest.c src/match.o src/choices.o src/options.o all: fzy diff --git a/src/choices.c b/src/choices.c index 6e34fd6..01480cb 100644 --- a/src/choices.c +++ b/src/choices.c @@ -5,6 +5,7 @@ #include #include +#include "options.h" #include "choices.h" #include "match.h" @@ -96,7 +97,7 @@ static void choices_reset_search(choices_t *c) { c->results = NULL; } -void choices_init(choices_t *c) { +void choices_init(choices_t *c, options_t *options) { c->strings = NULL; c->results = NULL; diff --git a/src/choices.h b/src/choices.h index f5f9892..e8a598b 100644 --- a/src/choices.h +++ b/src/choices.h @@ -4,6 +4,7 @@ #include #include "match.h" +#include "options.h" struct scored_result { score_t score; @@ -26,7 +27,7 @@ typedef struct { unsigned int worker_count; } choices_t; -void choices_init(choices_t *c); +void choices_init(choices_t *c, options_t *options); void choices_fread(choices_t *c, FILE *file); void choices_destroy(choices_t *c); void choices_add(choices_t *c, const char *choice); diff --git a/src/fzy.c b/src/fzy.c index 3b47475..461f022 100644 --- a/src/fzy.c +++ b/src/fzy.c @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) { options_parse(&options, argc, argv); choices_t choices; - choices_init(&choices); + choices_init(&choices, &options); choices_fread(&choices, stdin); if (options.benchmark) { diff --git a/src/options.c b/src/options.c index 9e66122..41dbba1 100644 --- a/src/options.c +++ b/src/options.c @@ -30,10 +30,11 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e' {"show-scores", no_argument, NULL, 's'}, {"version", no_argument, NULL, 'v'}, {"benchmark", optional_argument, NULL, 'b'}, + {"workers", required_argument, NULL, 'j'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}}; -void options_set_defaults(options_t *options) { +void options_init(options_t *options) { /* set defaults */ options->benchmark = 0; options->filter = NULL; @@ -46,7 +47,7 @@ void options_set_defaults(options_t *options) { } void options_parse(options_t *options, int argc, char *argv[]) { - options_set_defaults(options); + options_init(options); int c; while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:", longopts, NULL)) != -1) { diff --git a/src/options.h b/src/options.h index 1f461bc..f8a44a0 100644 --- a/src/options.h +++ b/src/options.h @@ -12,6 +12,7 @@ typedef struct { const char *prompt; } options_t; +void options_init(options_t *options); void options_parse(options_t *options, int argc, char *argv[]); #endif diff --git a/test/fzytest.c b/test/fzytest.c index d79ef69..7fe6fb2 100644 --- a/test/fzytest.c +++ b/test/fzytest.c @@ -7,6 +7,7 @@ #include "../config.h" #include "match.h" #include "choices.h" +#include "options.h" int testsrun = 0, testsfailed = 0, assertionsrun = 0; @@ -21,6 +22,8 @@ int testsrun = 0, testsfailed = 0, assertionsrun = 0; #define assert_streq(a, b) assert(!strcmp(a, b)) +static options_t default_options; + void runtest(void (*test)()) { testsrun++; test(); @@ -160,7 +163,7 @@ void test_positions_exact() { void test_choices_empty() { choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); assert(choices.size == 0); assert(choices.available == 0); assert(choices.selection == 0); @@ -176,7 +179,7 @@ void test_choices_empty() { void test_choices_1() { choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); choices_add(&choices, "tags"); choices_search(&choices, ""); @@ -201,7 +204,7 @@ void test_choices_1() { void test_choices_2() { choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); choices_add(&choices, "tags"); choices_add(&choices, "test"); @@ -253,7 +256,7 @@ void test_choices_without_search() { /* Before a search is run, it should return no results */ choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); assert(choices.available == 0); assert(choices.selection == 0); @@ -273,7 +276,7 @@ void test_choices_without_search() { /* Regression test for segfault */ void test_choices_unicode() { choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); choices_add(&choices, "Edmund Husserl - Méditations cartésiennes - Introduction a la phénoménologie.pdf"); choices_search(&choices, "e"); @@ -283,7 +286,7 @@ void test_choices_unicode() { void test_choices_large_input() { choices_t choices; - choices_init(&choices); + choices_init(&choices, &default_options); int N = 100000; char *strings[N]; @@ -323,6 +326,8 @@ int main(int argc, char *argv[]) { * If we have no debugger running, we should ignore it */ signal(SIGTRAP, ignore_signal); + options_init(&default_options); + runtest(test_match); runtest(test_relative_scores); runtest(test_exact_scores); -- cgit v1.2.3