From 79e48082f579f6c02a55b7cff81ecee1c4127b29 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Fri, 16 Aug 2019 01:09:29 -0700 Subject: Simplify input_delimiter handling --- src/choices.c | 10 ++-------- src/choices.h | 3 +-- src/fzy.c | 8 ++++---- src/options.c | 22 +++++++++++----------- src/options.h | 2 +- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/choices.c b/src/choices.c index b6d5f0e..fe2f80b 100644 --- a/src/choices.c +++ b/src/choices.c @@ -46,7 +46,7 @@ static void *safe_realloc(void *buffer, size_t size) { return buffer; } -void choices_fread(choices_t *c, FILE *file) { +void choices_fread(choices_t *c, FILE *file, char input_delimiter) { /* Save current position for parsing later */ size_t buffer_start = c->buffer_size; @@ -75,7 +75,7 @@ void choices_fread(choices_t *c, FILE *file) { const char *line_end = c->buffer + c->buffer_size; char *line = c->buffer + buffer_start; do { - char *nl = strchr(line, c->input_delimiter); + char *nl = strchr(line, input_delimiter); if (nl) *nl++ = '\0'; @@ -114,12 +114,6 @@ void choices_init(choices_t *c, options_t *options) { c->worker_count = (int)sysconf(_SC_NPROCESSORS_ONLN); } - if (options->read_null) { - c->input_delimiter = '\0'; - } else { - c->input_delimiter = '\n'; - } - choices_reset_search(c); } diff --git a/src/choices.h b/src/choices.h index c2bbcd9..925478e 100644 --- a/src/choices.h +++ b/src/choices.h @@ -25,11 +25,10 @@ typedef struct { size_t selection; unsigned int worker_count; - char input_delimiter; } choices_t; void choices_init(choices_t *c, options_t *options); -void choices_fread(choices_t *c, FILE *file); +void choices_fread(choices_t *c, FILE *file, char input_delimiter); void choices_destroy(choices_t *c); void choices_add(choices_t *c, const char *choice); size_t choices_available(choices_t *c); diff --git a/src/fzy.c b/src/fzy.c index 6b9aa5a..c2e5600 100644 --- a/src/fzy.c +++ b/src/fzy.c @@ -27,11 +27,11 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n"); exit(EXIT_FAILURE); } - choices_fread(&choices, stdin); + choices_fread(&choices, stdin, options.input_delimiter); for (int i = 0; i < options.benchmark; i++) choices_search(&choices, options.filter); } else if (options.filter) { - choices_fread(&choices, stdin); + choices_fread(&choices, stdin, options.input_delimiter); choices_search(&choices, options.filter); for (size_t i = 0; i < choices_available(&choices); i++) { if (options.show_scores) @@ -42,13 +42,13 @@ int main(int argc, char *argv[]) { /* interactive */ if (isatty(STDIN_FILENO)) - choices_fread(&choices, stdin); + choices_fread(&choices, stdin, options.input_delimiter); tty_t tty; tty_init(&tty, options.tty_filename); if (!isatty(STDIN_FILENO)) - choices_fread(&choices, stdin); + choices_fread(&choices, stdin, options.input_delimiter); if (options.num_lines > choices.size) options.num_lines = choices.size; diff --git a/src/options.c b/src/options.c index 85eeac3..58f8fa0 100644 --- a/src/options.c +++ b/src/options.c @@ -41,16 +41,16 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e' void options_init(options_t *options) { /* set defaults */ - options->benchmark = 0; - options->filter = NULL; - options->init_search = NULL; - options->show_scores = 0; - options->scrolloff = 1; - options->tty_filename = DEFAULT_TTY; - options->num_lines = DEFAULT_NUM_LINES; - options->prompt = DEFAULT_PROMPT; - options->workers = DEFAULT_WORKERS; - options->read_null = 0; + options->benchmark = 0; + options->filter = NULL; + options->init_search = NULL; + options->show_scores = 0; + options->scrolloff = 1; + options->tty_filename = DEFAULT_TTY; + options->num_lines = DEFAULT_NUM_LINES; + options->prompt = DEFAULT_PROMPT; + options->workers = DEFAULT_WORKERS; + options->input_delimiter = '\n'; } void options_parse(options_t *options, int argc, char *argv[]) { @@ -66,7 +66,7 @@ void options_parse(options_t *options, int argc, char *argv[]) { options->show_scores = 1; break; case '0': - options->read_null = 1; + options->input_delimiter = '\0'; break; case 'q': options->init_search = optarg; diff --git a/src/options.h b/src/options.h index 7113ccf..6098a64 100644 --- a/src/options.h +++ b/src/options.h @@ -11,7 +11,7 @@ typedef struct { unsigned int scrolloff; const char *prompt; unsigned int workers; - int read_null; + char input_delimiter; } options_t; void options_init(options_t *options); -- cgit v1.2.3