Browse code

show selection info (with -i option)

Ondrej Martinek authored on 16/03/2019 04:54:08 • John Hawthorn committed on 28/12/2019 07:17:18
Showing 4 changed files

... ...
@@ -16,3 +16,4 @@
16 16
 #define DEFAULT_PROMPT "> "
17 17
 #define DEFAULT_NUM_LINES 10
18 18
 #define DEFAULT_WORKERS 0
19
+#define DEFAULT_SHOW_INFO 0
... ...
@@ -19,6 +19,7 @@ static const char *usage_str =
19 19
     " -s, --show-scores        Show the scores of each match\n"
20 20
     " -0, --read-null          Read input delimited by ASCII NUL characters\n"
21 21
     " -j, --workers NUM        Use NUM workers for searching. (default is # of CPUs)\n"
22
+    " -i, --show-info          Show selection info line\n"
22 23
     " -h, --help     Display this help and exit\n"
23 24
     " -v, --version  Output version information and exit\n";
24 25
 
... ...
@@ -36,6 +37,7 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'
36 37
 				   {"version", no_argument, NULL, 'v'},
37 38
 				   {"benchmark", optional_argument, NULL, 'b'},
38 39
 				   {"workers", required_argument, NULL, 'j'},
40
+				   {"show-info", no_argument, NULL, 'i'},
39 41
 				   {"help", no_argument, NULL, 'h'},
40 42
 				   {NULL, 0, NULL, 0}};
41 43
 
... ...
@@ -51,13 +53,14 @@ void options_init(options_t *options) {
51 53
 	options->prompt          = DEFAULT_PROMPT;
52 54
 	options->workers         = DEFAULT_WORKERS;
53 55
 	options->input_delimiter = '\n';
56
+	options->show_info       = DEFAULT_SHOW_INFO;
54 57
 }
55 58
 
56 59
 void options_parse(options_t *options, int argc, char *argv[]) {
57 60
 	options_init(options);
58 61
 
59 62
 	int c;
60
-	while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:", longopts, NULL)) != -1) {
63
+	while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:i", longopts, NULL)) != -1) {
61 64
 		switch (c) {
62 65
 			case 'v':
63 66
 				printf("%s " VERSION " © 2014-2018 John Hawthorn\n", argv[0]);
... ...
@@ -108,6 +111,9 @@ void options_parse(options_t *options, int argc, char *argv[]) {
108 111
 				}
109 112
 				options->num_lines = l;
110 113
 			} break;
114
+			case 'i':
115
+				options->show_info = 1;
116
+				break;
111 117
 			case 'h':
112 118
 			default:
113 119
 				usage(argv[0]);
... ...
@@ -12,6 +12,7 @@ typedef struct {
12 12
 	const char *prompt;
13 13
 	unsigned int workers;
14 14
 	char input_delimiter;
15
+	int show_info;
15 16
 } options_t;
16 17
 
17 18
 void options_init(options_t *options);
... ...
@@ -20,7 +20,7 @@ static void clear(tty_interface_t *state) {
20 20
 
21 21
 	tty_setcol(tty, 0);
22 22
 	size_t line = 0;
23
-	while (line++ < state->options->num_lines) {
23
+	while (line++ < state->options->num_lines + (state->options->show_info ? 1 : 0)) {
24 24
 		tty_newline(tty);
25 25
 	}
26 26
 	tty_clearline(tty);
... ...
@@ -90,9 +90,16 @@ static void draw(tty_interface_t *state) {
90 90
 			start = available - num_lines;
91 91
 		}
92 92
 	}
93
+
93 94
 	tty_setcol(tty, 0);
94 95
 	tty_printf(tty, "%s%s", options->prompt, state->search);
95 96
 	tty_clearline(tty);
97
+
98
+	if (options->show_info) {
99
+		tty_printf(tty, "\n[%lu/%lu]", choices->available, choices->size);
100
+		tty_clearline(tty);
101
+	}
102
+
96 103
 	for (size_t i = start; i < start + num_lines; i++) {
97 104
 		tty_printf(tty, "\n");
98 105
 		tty_clearline(tty);
... ...
@@ -101,14 +108,9 @@ static void draw(tty_interface_t *state) {
101 108
 			draw_match(state, choice, i == choices->selection);
102 109
 		}
103 110
 	}
104
-	if (num_lines > 0) {
105
-		tty_moveup(tty, num_lines);
106
-	}
107 111
 
108
-	tty_setcol(tty, 0);
109
-	fputs(options->prompt, tty->fout);
110
-	for (size_t i = 0; i < state->cursor; i++)
111
-		fputc(state->search[i], tty->fout);
112
+	tty_moveup(tty, num_lines + (options->show_info ? 1 : 0));
113
+	tty_setcol(tty, strlen(options->prompt) + state->cursor);
112 114
 	tty_flush(tty);
113 115
 }
114 116