Browse code

Allow specifying number of lines to show

John Hawthorn authored on 31/08/2014 03:05:29
Showing 1 changed files

  • fzy.c index 0e2350e..85b29fa 100644
... ...
@@ -10,6 +10,10 @@
10 10
 
11 11
 int flag_show_scores = 0;
12 12
 
13
+size_t num_lines = 10;
14
+size_t scrolloff = 1;
15
+
16
+
13 17
 #define INITIAL_CAPACITY 1
14 18
 int choices_capacity = 0;
15 19
 int choices_n = 0;
... ...
@@ -85,17 +89,14 @@ void run_search(char *needle){
85 89
 	qsort(choices_sorted, choices_available, sizeof(size_t), cmpchoice);
86 90
 }
87 91
 
88
-#define NUMLINES 10
89
-#define SCROLLOFF 1
90
-
91 92
 #define SEARCH_SIZE_MAX 4096
92 93
 int search_size;
93 94
 char search[SEARCH_SIZE_MAX + 1] = {0};
94 95
 
95 96
 void clear(tty_t *tty){
96 97
 	tty_setcol(tty, 0);
97
-	int line = 0;
98
-	while(line++ < NUMLINES){
98
+	size_t line = 0;
99
+	while(line++ < num_lines){
99 100
 		tty_newline(tty);
100 101
 	}
101 102
 	tty_moveup(tty, line-1);
... ...
@@ -129,16 +130,16 @@ void draw_match(tty_t *tty, const char *choice, int selected){
129 130
 
130 131
 void draw(tty_t *tty){
131 132
 	size_t start = 0;
132
-	if(current_selection + SCROLLOFF >= NUMLINES){
133
-		start = current_selection + SCROLLOFF - NUMLINES + 1;
134
-		if(start + NUMLINES >= choices_available){
135
-			start = choices_available - NUMLINES;
133
+	if(current_selection + scrolloff >= num_lines){
134
+		start = current_selection + scrolloff - num_lines + 1;
135
+		if(start + num_lines >= choices_available){
136
+			start = choices_available - num_lines;
136 137
 		}
137 138
 	}
138 139
 	const char *prompt = "> ";
139 140
 	tty_setcol(tty, 0);
140 141
 	tty_printf(tty, "%s%s", prompt, search);
141
-	for(size_t i = start; i < start + NUMLINES; i++){
142
+	for(size_t i = start; i < start + num_lines; i++){
142 143
 		tty_newline(tty);
143 144
 		if(i < choices_available){
144 145
 			size_t choice_idx = choices_sorted[i];
... ...
@@ -148,7 +149,7 @@ void draw(tty_t *tty){
148 149
 		}
149 150
 	}
150 151
 	tty_clearline(tty);
151
-	tty_moveup(tty, NUMLINES);
152
+	tty_moveup(tty, num_lines);
152 153
 	tty_setcol(tty, strlen(prompt) + strlen(search));
153 154
 	tty_flush(tty);
154 155
 }
... ...
@@ -225,6 +226,7 @@ void run(tty_t *tty){
225 226
 
226 227
 static const char *usage_str = ""
227 228
 "USAGE: fzy [OPTION]...\n"
229
+" -l, --lines              Specify how many lines of results to show\n"
228 230
 " -e, --show-matches=QUERY output the sorted matches of QUERY\n"
229 231
 " -s, --show-scores        show the scores of each match\n"
230 232
 " -h, --help     display this help and exit\n"
... ...
@@ -232,11 +234,11 @@ static const char *usage_str = ""
232 234
 
233 235
 void usage(const char *argv0){
234 236
 	fprintf(stderr, usage_str, argv0);
235
-	exit(EXIT_FAILURE);
236 237
 }
237 238
 
238 239
 static struct option longopts[] = {
239 240
 	{ "show-matches", required_argument, NULL, 'e' },
241
+	{ "lines", required_argument, NULL, 'l' },
240 242
 	{ "show-scores", no_argument, NULL, 's' },
241 243
 	{ "version", no_argument, NULL, 'v' },
242 244
 	{ "help",    no_argument, NULL, 'h' },
... ...
@@ -246,7 +248,7 @@ static struct option longopts[] = {
246 248
 int main(int argc, char *argv[]){
247 249
 	char *initial_query = NULL;
248 250
 	char c;
249
-	while((c = getopt_long(argc, argv, "vhse:", longopts, NULL)) != -1){
251
+	while((c = getopt_long(argc, argv, "vhse:l:", longopts, NULL)) != -1){
250 252
 		switch(c){
251 253
 			case 'v':
252 254
 				printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
... ...
@@ -257,14 +259,27 @@ int main(int argc, char *argv[]){
257 259
 			case 'e':
258 260
 				initial_query = optarg;
259 261
 				break;
262
+			case 'l':
263
+				{
264
+					int l;
265
+					if(sscanf(optarg, "%d", &l) != 1 || l < 3){
266
+						fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
267
+						fprintf(stderr, "Must be integer in range 3..\n");
268
+						usage(argv[0]);
269
+						exit(EXIT_FAILURE);
270
+					}
271
+					num_lines = l;
272
+				}
273
+				break;
260 274
 			case 'h':
261 275
 			default:
262 276
 				usage(argv[0]);
263
-				exit(EXIT_FAILURE);
277
+				exit(EXIT_SUCCESS);
264 278
 		}
265 279
 	}
266 280
 	if(optind != argc){
267 281
 		usage(argv[0]);
282
+		exit(EXIT_FAILURE);
268 283
 	}
269 284
 
270 285
 	resize_choices(INITIAL_CAPACITY);