Browse code

Add -q/--query for specifying initial query

This matches fzf's -q and selecta's -s

John Hawthorn authored on 17/05/2016 02:33:44
Showing 1 changed files

  • fzy.c index d357aa6..a84b125 100644
... ...
@@ -19,7 +19,6 @@ static size_t scrolloff = 1;
19 19
 static const char *prompt = "> ";
20 20
 
21 21
 #define SEARCH_SIZE_MAX 4096
22
-static size_t search_size;
23 22
 static char search[SEARCH_SIZE_MAX + 1] = {0};
24 23
 
25 24
 static void clear(tty_t *tty) {
... ...
@@ -112,6 +111,7 @@ static void run(tty_t *tty, choices_t *choices) {
112 111
 	do {
113 112
 		draw(tty, choices);
114 113
 		ch = tty_getchar(tty);
114
+		size_t search_size = strlen(search);
115 115
 		if (isprint(ch)) {
116 116
 			if (search_size < SEARCH_SIZE_MAX) {
117 117
 				search[search_size++] = ch;
... ...
@@ -138,7 +138,6 @@ static void run(tty_t *tty, choices_t *choices) {
138 138
 			choices_prev(choices);
139 139
 		} else if (ch == KEY_CTRL('I')) { /* TAB (C-I) */
140 140
 			strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
141
-			search_size = strlen(search);
142 141
 			choices_search(choices, search);
143 142
 		} else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */
144 143
 			clear(tty);
... ...
@@ -173,6 +172,7 @@ static const char *usage_str =
173 172
     "Usage: fzy [OPTION]...\n"
174 173
     " -l, --lines=LINES        Specify how many lines of results to show (default 10)\n"
175 174
     " -p, --prompt=PROMPT      Input prompt (default '> ')\n"
175
+    " -q, --query=QUERY        Use QUERY as the initial search string\n"
176 176
     " -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
177 177
     " -t, --tty=TTY            Specify file to use as TTY device (default /dev/tty)\n"
178 178
     " -s, --show-scores        Show the scores of each match\n"
... ...
@@ -184,6 +184,7 @@ static void usage(const char *argv0) {
184 184
 }
185 185
 
186 186
 static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
187
+				   {"query", required_argument, NULL, 'q'},
187 188
 				   {"lines", required_argument, NULL, 'l'},
188 189
 				   {"tty", required_argument, NULL, 't'},
189 190
 				   {"prompt", required_argument, NULL, 'p'},
... ...
@@ -198,7 +199,7 @@ int main(int argc, char *argv[]) {
198 199
 	const char *filter = NULL;
199 200
 	const char *tty_filename = "/dev/tty";
200 201
 	char c;
201
-	while ((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1) {
202
+	while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:", longopts, NULL)) != -1) {
202 203
 		switch (c) {
203 204
 			case 'v':
204 205
 				printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
... ...
@@ -206,6 +207,9 @@ int main(int argc, char *argv[]) {
206 207
 			case 's':
207 208
 				flag_show_scores = 1;
208 209
 				break;
210
+			case 'q':
211
+				strncpy(search, optarg, SEARCH_SIZE_MAX);
212
+				break;
209 213
 			case 'e':
210 214
 				filter = optarg;
211 215
 				break;