-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtpick.c
More file actions
394 lines (325 loc) · 8.75 KB
/
tpick.c
File metadata and controls
394 lines (325 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <ncurses.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// Try to pick up GNU extensions for <fnmatch.h>.
#if defined(__linux__)
#define _GNU_SOURCE
#endif
#include <fnmatch.h>
// FNM_CASEFOLD is a GNU extension. We get smartcase if we have FNM_CASEFOLD,
// otherwise matching is case sensitive.
#ifndef FNM_CASEFOLD
#define FNM_CASEFOLD 0
#endif
/* **********************************************************************
* Usage.
*/
char *usage_message[] =
{
"tpick [OPTIONS...] [THINGS...]",
"OPTIONS:",
" -i : read things from standard input (one per line), instead of from the command line",
" -I : read things from standard input (whitespace separated), instead of from the command line",
" -p TEXT : prepend TEXT to fnmatch pattern (default is \"*\")",
" -s TEXT : append TEXT to fnmatch pattern (default is \"*\")",
" -f TEXT : set your favourite text, which is added to the search when you type ';'",
" -P : equivalent to -p \"\"",
" -S : equivalent to -s \"\"",
" -Q : disable exit (and fail) on two consecutive q characters",
" -h : output this help message",
0
};
void usage(int status)
{
int i;
FILE *file = status ? stderr : stdout;
for (i=0; usage_message[i]; i+=1)
fprintf(file,"%s\n", usage_message[i]);
exit(status);
}
/* **********************************************************************
* Start/end curses.
*/
void curses_start()
{
FILE *fd;
// Open /dev/tty directly, so that standard input and/or standard output can
// be redirected.
if ( !(fd = fopen("/dev/tty", "r+")) )
{ fprintf(stderr, "failed to open /dev/tty\n"); exit(1); }
set_term(newterm(NULL, fd, fd));
cbreak();
noecho();
clear();
raw();
keypad(stdscr, TRUE);
meta(stdscr, TRUE);
}
void curses_end()
{
clrtoeol();
refresh();
endwin();
}
/* **********************************************************************
* Utilities.
*/
void die(int err)
{
curses_end();
exit(err);
}
void fail(char *msg)
{
curses_end();
fprintf(stderr, "%s\n", msg);
exit(1);
}
void *non_null(void *ptr)
{
if ( !ptr )
{
curses_end();
fprintf(stderr, "malloc failed\n");
exit(1);
}
return ptr;
}
/* **********************************************************************
* Globals.
*/
static const char *prompt = "pattern: ";
static int prompt_len = 0;
static char *prefix = "*";
static char *suffix = "*";
static int qq_quits = 1;
static int standard_input = 0;
static char **cargv = 0;
static int cargc = 0;
static char *favourite = 0;
static int argc;
static char **argv;
static WINDOW *prompt_win = 0;
static WINDOW *search_win = 0;
static WINDOW *main_win = 0;
/* **********************************************************************
* Exit keys.
* Exit on <ESCAPE>, <CONTROL-C> or, normally, two consecutive 'q's.
*/
void quit(const int c, const char *kn)
{
static int qcnt = 0;
if ( qq_quits ) {
if ( c == 'q' ) { if ( qcnt ) die(1); else qcnt += 1; }
else qcnt = 0;
}
if ( c == 27 /* escape */ || strcmp(kn,"^C") == 0 )
die(1);
}
/* **********************************************************************
* Slurp standard input into argv/argc.
*/
#define MAX_LINE 4096
void add_thing(char *buf)
{
argv = (char **) non_null(realloc(argv,(argc+1)*sizeof(char *)));
argv[argc] = (char *) non_null(strdup(buf));
argc += 1;
}
void read_standard_input_lines()
{
char buf[MAX_LINE];
argc = 0; argv = 0;
while ( fgets(buf,MAX_LINE,stdin) )
{
char *newline = strchr(buf,'\n');
if ( newline ) newline[0] = 0;
add_thing(buf);
}
}
const char whitespace[] = " \t\b\v\r\n";
void read_standard_input_words()
{
char buf[MAX_LINE];
argc = 0; argv = 0;
while ( fgets(buf,MAX_LINE,stdin) )
{
char *tok = strtok(buf,whitespace);
while ( tok )
{
add_thing(tok);
tok = strtok(NULL,whitespace);
}
}
}
/* **********************************************************************
* Main.
*/
void display(int c, char *kn);
void re_display();
int main(int original_argc, char *original_argv[])
{
prompt_len = strlen(prompt);
argc = original_argc;
argv = original_argv;
int opt;
while ( (opt = getopt(argc, argv, "Qp:s:PSiIhf:")) != -1 )
{
switch ( opt )
{
case 'Q': qq_quits = 0; break;
case 'p': prefix = optarg; break;
case 's': suffix = optarg; break;
case 'P': prefix = ""; break;
case 'S': suffix = ""; break;
case 'i': standard_input = 1; break;
case 'I': standard_input = 2; break;
case 'f': favourite = optarg; break;
case 'h':usage(0); break;
default: usage(1);
}
}
argv += optind;
argc -= optind;
if ( standard_input && argc )
{ cargv = argv; cargc = argc; }
if ( standard_input == 1 ) read_standard_input_lines();
if ( standard_input == 2 ) read_standard_input_words();
if ( argc == 0 )
{ fprintf(stderr, "nothing from which to pick\n"); exit(1); }
signal(SIGINT, die);
signal(SIGQUIT, die);
signal(SIGTERM, die);
signal(SIGWINCH, re_display);
curses_start();
main_win = newwin(LINES-2,COLS,2,0);
prompt_win = newwin(1,prompt_len,0,0);
search_win = newwin(1,COLS-prompt_len,0,prompt_len);
waddstr(prompt_win,prompt);
re_display();
while ( 1 ) {
int c = getch();
char *kn = (char *) keyname(c);
quit(c,kn);
display(c,kn);
}
}
/* **********************************************************************
* Pattern matching.
*/
static char *fn_pattern = 0;
static int fn_flag = 0;
void fn_match_init(char *needle)
{
int i, len = strlen(needle);
// Smartcase.
fn_flag = FNM_CASEFOLD;
for (i=0; i<len && fn_flag; i+=1)
if ( isupper(needle[i]) )
fn_flag = 0;
fn_pattern = (char *) non_null(realloc(fn_pattern,strlen(prefix)+len+strlen(suffix)+1));
sprintf(fn_pattern, "%s%s%s",prefix,needle,suffix);
}
int fn_match(char *haystack)
{ return !fnmatch(fn_pattern,haystack,fn_flag); }
/* **********************************************************************
* Display and selection handling.
*/
void handle_selection(char *selection)
{
if ( cargv )
{
char **command = (char **) non_null(malloc((cargc+1)*sizeof(char *)));
memcpy(command,cargv,(cargc)*sizeof(char *));
command[cargc] = selection;
execvp(command[0],command);
fprintf(stderr, "execvp failed: %s\n", command[0]);
exit(1);
}
else
printf("%s\n", selection);
}
void re_display()
{ display(0,0); }
void display(int c, char *kn)
{
static char *selection = 0;
static char *search = 0;
static int search_len = 0;
static int offset = 0;
int i, j = 0, change = 0;
if ( c == '\n' ) {
curses_end();
if ( ! selection )
exit(1);
handle_selection(selection);
exit(0);
}
if ( !search )
search = (char *) non_null(strdup(""));
if ( c == ';' && favourite )
{
char *ptr;
for (ptr=favourite; ptr[0]; ptr+=1)
display(ptr[0],0);
return;
}
if ( kn && strcmp(kn,"KEY_DOWN") == 0 )
{ offset += 1; c = 0; }
if ( kn && strcmp(kn,"KEY_UP") == 0 )
{ offset -= 1; c = 0; }
if ( kn && strcmp(kn,"KEY_NPAGE") == 0 )
{ offset += LINES / 2; c = 0; }
if ( kn && strcmp(kn,"KEY_PPAGE") == 0 )
{ offset -= LINES / 2; c = 0; }
if ( offset < 0 )
offset = 0;
if ( c == ' ' )
{ c = '*'; kn = (char *) keyname(c); }
if ( isalnum(c) || ispunct(c) )
{ change = 1; search[search_len] = c; }
if ( c == KEY_BACKSPACE && 0 < search_len )
change = -1;
if ( change ) {
search_len += change;
search = (char *) non_null(realloc(search,search_len+1));
search[search_len] = 0;
wclear(search_win);
waddstr(search_win,search);
offset = 0;
}
wclear(main_win);
selection = 0;
fn_match_init(search);
for (i=0; i<argc && j<LINES-2; i+=1)
if ( fn_match(argv[i]) )
{
j += 1;
if ( j <= offset )
continue;
if ( selection == 0 )
selection = argv[i];
if ( j-offset == 1 ) wattron(main_win, A_REVERSE);
waddstr(main_win,argv[i]);
if ( j-offset == 1 ) wattroff(main_win, A_REVERSE);
wclrtoeol(main_win);
wmove(main_win,j-offset,0);
}
// Ensure we don't scroll off the bottom of the list.
if ( offset && j <= offset )
{
offset = j ? j-1 : 0;
re_display();
return;
}
refresh();
wrefresh(main_win);
wrefresh(prompt_win);
wrefresh(search_win);
wmove(search_win,0,search_len);
}