Browse code

Add non-interactive invocation

John Hawthorn authored on 20/08/2014 02:57:44
Showing 1 changed files

  • fzy.c index d36f82b..dcd8f3f 100644
... ...
@@ -223,6 +223,7 @@ void run(tty_t *tty){
223 223
 
224 224
 static const char *usage_str = ""
225 225
 "USAGE: fzy [OPTION]...\n"
226
+" -e, --show-matches=QUERY output the sorted matches of QUERY\n"
226 227
 " -s, --show-scores        show the scores of each match\n"
227 228
 " -h, --help     display this help and exit\n"
228 229
 " -v, --version  output version information and exit\n";
... ...
@@ -233,14 +234,15 @@ void usage(const char *argv0){
233 234
 }
234 235
 
235 236
 static struct option longopts[] = {
237
+	{ "show-matches", required_argument, NULL, 'e' },
236 238
 	{ "show-scores", no_argument, NULL, 's' },
237 239
 	{ "version", no_argument, NULL, 'v' },
238 240
 	{ "help",    no_argument, NULL, 'h' },
239 241
 	{ NULL,      0,           NULL, 0 }
240 242
 };
241 243
 
242
-
243 244
 int main(int argc, char *argv[]){
245
+	char *initial_query = NULL;
244 246
 	char c;
245 247
 	while((c = getopt_long(argc, argv, "vhs", longopts, NULL)) != -1){
246 248
 		switch(c){
... ...
@@ -250,6 +252,9 @@ int main(int argc, char *argv[]){
250 252
 			case 's':
251 253
 				flag_show_scores = 1;
252 254
 				break;
255
+			case 'e':
256
+				initial_query = optarg;
257
+				break;
253 258
 			case 'h':
254 259
 			default:
255 260
 				usage(argv[0]);
... ...
@@ -260,14 +265,25 @@ int main(int argc, char *argv[]){
260 265
 		usage(argv[0]);
261 266
 	}
262 267
 
263
-	tty_t tty;
264
-	tty_init(&tty);
265
-
266 268
 	resize_choices(INITIAL_CAPACITY);
267 269
 	read_choices();
268 270
 
269
-	clear(&tty);
270
-	run(&tty);
271
+	if(initial_query){
272
+		run_search(initial_query);
273
+		for(size_t i = 0; i < choices_available; i++){
274
+			size_t choice_idx = choices_sorted[i];
275
+			if(flag_show_scores)
276
+				printf("%f\t", choices_score[choice_idx]);
277
+			printf("%s\n", choices[choice_idx]);
278
+		}
279
+	}else{
280
+		/* interactive */
281
+		tty_t tty;
282
+		tty_init(&tty);
283
+
284
+		clear(&tty);
285
+		run(&tty);
286
+	}
271 287
 
272 288
 	return 0;
273 289
 }