summaryrefslogtreecommitdiff
path: root/fzy.c
blob: acb528dbcc58aebfb88f85399108db6b46f563e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <getopt.h>
#include <limits.h>

#include "match.h"
#include "tty.h"
#include "choices.h"

#include "config.h"

int flag_show_scores = 0;

size_t num_lines = 10;
size_t scrolloff = 1;

const char *prompt = "> ";

void read_choices(choices_t *c){
	char *line = NULL;
	size_t len = 0;
	ssize_t read;

	while ((read = getline(&line, &len, stdin)) != -1) {
		char *nl;
		if((nl = strchr(line, '\n')))
			*nl = '\0';

		choices_add(c, line);

		line = NULL;
	}
	free(line);
}

#define SEARCH_SIZE_MAX 4096
int search_size;
char search[SEARCH_SIZE_MAX + 1] = {0};

void clear(tty_t *tty){
	tty_setcol(tty, 0);
	size_t line = 0;
	while(line++ < num_lines){
		tty_newline(tty);
	}
	tty_moveup(tty, line-1);
	tty_flush(tty);
}

void draw_match(tty_t *tty, const char *choice, int selected){
	int n = strlen(search);
	size_t positions[n + 1];
	for(int i = 0; i < n + 1; i++)
		positions[i] = -1;

	double score = match_positions(search, choice, &positions[0]);

	size_t maxwidth = tty_getwidth(tty);

	if(flag_show_scores)
		tty_printf(tty, "(%5.2f) ", score);

	if(selected)
		tty_setinvert(tty);

	for(size_t i = 0, p = 0; choice[i] != '\0'; i++){
		if(i+1 < maxwidth){
			if(positions[p] == i){
				tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
				p++;
			}else{
				tty_setfg(tty, TTY_COLOR_NORMAL);
			}
			tty_printf(tty, "%c", choice[i]);
		}else{
			tty_printf(tty, "$");
			break;
		}
	}
	tty_setnormal(tty);
}

void draw(tty_t *tty, choices_t *choices){
	size_t start = 0;
	size_t current_selection = choices->selection;
	if(current_selection + scrolloff >= num_lines){
		start = current_selection + scrolloff - num_lines + 1;
		if(start + num_lines >= choices_available(choices)){
			start = choices_available(choices) - num_lines;
		}
	}
	tty_setcol(tty, 0);
	tty_printf(tty, "%s%s", prompt, search);
	tty_clearline(tty);
	for(size_t i = start; i < start + num_lines; i++){
		tty_printf(tty, "\n");
		tty_clearline(tty);
		const char *choice = choices_get(choices, i);
		if(choice){
			draw_match(tty, choice, i == choices->selection);
		}
	}
	tty_moveup(tty, num_lines);
	tty_setcol(tty, strlen(prompt) + strlen(search));
	tty_flush(tty);
}

void emit(choices_t *choices){
	const char *selection = choices_get(choices, choices->selection);
	if(selection){
		/* output the selected result */
		printf("%s\n", selection);
	}else{
		/* No match, output the query instead */
		printf("%s\n", search);
	}

	exit(EXIT_SUCCESS);
}

void run(tty_t *tty, choices_t *choices){
	choices_search(choices, search);
	char ch;
	do {
		draw(tty, choices);
		ch = tty_getchar(tty);
		if(isprint(ch)){
			if(search_size < SEARCH_SIZE_MAX){
				search[search_size++] = ch;
				search[search_size] = '\0';
				choices_search(choices, search);
			}
		}else if(ch == 127 || ch == 8){ /* DEL || backspace */
			if(search_size)
				search[--search_size] = '\0';
			choices_search(choices, search);
		}else if(ch == 21){ /* C-U */
			search_size = 0;
			search[0] = '\0';
			choices_search(choices, search);
		}else if(ch == 23){ /* C-W */
			if(search_size)
				search[--search_size] = '\0';
			while(search_size && !isspace(search[--search_size]))
				search[search_size] = '\0';
			choices_search(choices, search);
		}else if(ch == 14){ /* C-N */
			choices_next(choices);
		}else if(ch == 16){ /* C-P */
			choices_prev(choices);
		}else if(ch == 9){ /* TAB */
			strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
			search_size = strlen(search);
			choices_search(choices, search);
		}else if(ch == 10){ /* Enter */
			clear(tty);

			/* ttyout should be flushed before outputting on stdout */
			fclose(tty->fout);

			emit(choices);
		}else if(ch == 27){ /* ESC */
			ch = tty_getchar(tty);
			if(ch == '[' || ch == 'O'){
				ch = tty_getchar(tty);
				if(ch == 'A'){ /* UP ARROW */
					choices_prev(choices);
				}else if(ch == 'B'){ /* DOWN ARROW */
					choices_next(choices);
				}
			}
		}
	}while(1);
}

static const char *usage_str = ""
"Usage: fzy [OPTION]...\n"
" -l, --lines=LINES        Specify how many lines of results to show (default 10)\n"
" -p, --prompt=PROMPT      Input prompt (default '> ')\n"
" -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
" -t, --tty=TTY            Specify file to use as TTY device (default /dev/tty)\n"
" -s, --show-scores        Show the scores of each match\n"
" -h, --help     Display this help and exit\n"
" -v, --version  Output version information and exit\n";

void usage(const char *argv0){
	fprintf(stderr, usage_str, argv0);
}

static struct option longopts[] = {
	{ "show-matches", required_argument, NULL, 'e' },
	{ "lines", required_argument, NULL, 'l' },
	{ "tty", required_argument, NULL, 't' },
	{ "prompt", required_argument, NULL, 'p' },
	{ "show-scores", no_argument, NULL, 's' },
	{ "version", no_argument, NULL, 'v' },
	{ "benchmark", no_argument, NULL, 'b' },
	{ "help",    no_argument, NULL, 'h' },
	{ NULL,      0,           NULL, 0 }
};

int main(int argc, char *argv[]){
	int benchmark = 0;
	char *initial_query = NULL;
	char *tty_filename = "/dev/tty";
	char c;
	while((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1){
		switch(c){
			case 'v':
				printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
				exit(EXIT_SUCCESS);
			case 's':
				flag_show_scores = 1;
				break;
			case 'e':
				initial_query = optarg;
				break;
			case 'b':
				benchmark = 1;
				break;
			case 't':
				tty_filename = optarg;
				break;
			case 'p':
				prompt = optarg;
				break;
			case 'l':
				{
					int l;
					if(!strcmp(optarg, "max")){
						l = INT_MAX;
					}else if(sscanf(optarg, "%d", &l) != 1 || l < 3){
						fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
						fprintf(stderr, "Must be integer in range 3..\n");
						usage(argv[0]);
						exit(EXIT_FAILURE);
					}
					num_lines = l;
				}
				break;
			case 'h':
			default:
				usage(argv[0]);
				exit(EXIT_SUCCESS);
		}
	}
	if(optind != argc){
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	choices_t choices;
	choices_init(&choices);
	read_choices(&choices);

	if(num_lines > choices.size)
		num_lines = choices.size;

	if(benchmark){
		if(!initial_query){
			fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
			exit(EXIT_FAILURE);
		}
		for(int i = 0; i < 100; i++)
			choices_search(&choices, initial_query);
	}else if(initial_query){
		choices_search(&choices, initial_query);
		for(size_t i = 0; i < choices_available(&choices); i++){
			if(flag_show_scores)
				printf("%f\t", choices_getscore(&choices, i));
			printf("%s\n", choices_get(&choices, i));
		}
	}else{
		/* interactive */
		tty_t tty;
		tty_init(&tty, tty_filename);

		if(num_lines + 1 > tty_getheight(&tty))
			num_lines = tty_getheight(&tty) - 1;

		run(&tty, &choices);
	}

	return 0;
}