systemSetup

system setup, configuration and dotfiles
git clone https://noulin.net/git/systemSetup.git
Log | Files | Refs | README | LICENSE

fzy.c (1954B)


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