fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
fzy.c
(1790B)
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <limits.h>
6 #include <unistd.h>
7
8 #include "match.h"
9 #include "tty.h"
10 #include "choices.h"
11 #include "options.h"
12 #include "tty_interface.h"
13
14 #include "../config.h"
15
16 int main(int argc, char *argv[]) {
17 int ret = 0;
18
19 options_t options;
20 options_parse(&options, argc, argv);
21
22 choices_t choices;
23 choices_init(&choices, &options);
24
25 if (options.benchmark) {
26 if (!options.filter) {
27 fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
28 exit(EXIT_FAILURE);
29 }
30 choices_fread(&choices, stdin, options.input_delimiter);
31 for (int i = 0; i < options.benchmark; i++)
32 choices_search(&choices, options.filter);
33 } else if (options.filter) {
34 choices_fread(&choices, stdin, options.input_delimiter);
35 choices_search(&choices, options.filter);
36 for (size_t i = 0; i < choices_available(&choices); i++) {
37 if (options.show_scores)
38 printf("%f\t", choices_getscore(&choices, i));
39 printf("%s\n", choices_get(&choices, i));
40 }
41 } else {
42 /* interactive */
43
44 if (isatty(STDIN_FILENO))
45 choices_fread(&choices, stdin, options.input_delimiter);
46
47 tty_t tty;
48 tty_init(&tty, options.tty_filename);
49
50 if (!isatty(STDIN_FILENO))
51 choices_fread(&choices, stdin, options.input_delimiter);
52
53 if (options.num_lines > choices.size)
54 options.num_lines = choices.size;
55
56 int num_lines_adjustment = 1;
57 if (options.show_info)
58 num_lines_adjustment++;
59
60 if (options.num_lines + num_lines_adjustment > tty_getheight(&tty))
61 options.num_lines = tty_getheight(&tty) - num_lines_adjustment;
62
63 tty_interface_t tty_interface;
64 tty_interface_init(&tty_interface, &tty, &choices, &options);
65 ret = tty_interface_run(&tty_interface);
66 }
67
68 choices_destroy(&choices);
69
70 return ret;
71 }