Skip to content

Commit

Permalink
Simplify input_delimiter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Aug 16, 2019
1 parent c4524ae commit 79e4808
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
10 changes: 2 additions & 8 deletions src/choices.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/choices.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/fzy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 79e4808

Please sign in to comment.