aboutsummaryrefslogtreecommitdiff
path: root/ask.c
blob: 8dfee8e8aaa5213c1509ff1366899394939de9f2 (plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* ask - a simple command line asker
 * Copyright 2019 Bruce Hill
 * Released under the MIT License (see LICENSE for details)
 * Usage: ask [-Q|--quickpick] [-y|--yes] [-n|--no] [-p|--password]
 *   [-v|--version] [-h|--help] [-q |--query=initial] [[--prompt=]prompt [options...]]
 *    --password: password mode
 *    --quickpick: quickpick mode (exit when only one option remains)
 *    --version: print version and exit
 *    --help: print usage and exit
 *    --query: initial value to pre-populate user input
 *    --prompt: use the given prompt (displayed in bold)
 *    --yes: append "[Y/n]" to the prompt, use quickpick mode, 
 */
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <termios.h>
#include <unistd.h>

#include "bterm.h"

#define ASK_VERSION "0.2"
#define LOWERCASE(c) ('A' <= (c) && (c) <= 'Z' ? ((c) + 'a' - 'A') : (c))
#define EQ(a, b) (case_sensitive ? (a) == (b) : LOWERCASE(a) == LOWERCASE(b))
#define PASSWORD "-\\|/"

static int password = 0, quickpick = 0, case_sensitive = 0;

static inline void *memcheck(void *p)
{
    if (!p) {
        fprintf(stderr, "Memory allocation failure\n");
        exit(1);
    }
    return p;
}

/* Return length of the longest substring of patt[p:] that occurs within
 * str[s:], while still leaving enough space for the rest of `patt` to occur
 * within the rest of `str`. If there is no valid solution, return -1.
 */
static int lcs(const char *str, const char *patt, int slen, int plen, int s, int p, int *cache)
{
    if (!patt[p]) return 0;
    if (!str[s]) return -1;
    if (cache[s*plen + p]) return cache[s*plen + p];
    if (!EQ(str[s], patt[p])) return lcs(str, patt, slen, plen, s+1, p, cache);
    // Run starting here
    int run = 1;
    while (str[s+run] && patt[p+run] && EQ(str[s+run], patt[p+run])
           && lcs(str, patt, slen, plen, s+run, p+run, cache) >= 0) {
        ++run;
    }
    if (run == 0) run = -1;
    cache[s*plen + p] = run;
    return run;
}

static int matches(const char *str, const char *patt)
{
    while (*patt) {
        while (!EQ(*str, *patt)) {
            if (!*str) return 0;
            ++str;
        }
        ++str;
        ++patt;
    }
    return 1;
}

static int fputc_escaped(char c, FILE *out)
{
    static const char *escapes = "       abtnvfr             e";
    if (c > 0 && c <= '\x1b' && escapes[(int)c] != ' ') { // "\n", etc.
        fprintf(out, "\033[35m\\%c\033[37m", escapes[(int)c]);
        return 2;
    } else if (c >= 0 && !(' ' <= c && c <= '~')) { // "\x02", etc.
        fprintf(out, "\033[35m\\x%02X\033[37m", c);
        return 4;
    } else {
        fputc(c, out);
        return 1;
    }
}

static int draw_line(FILE *out, const char *option, const char *patt, int cursor)
{
    static const char *dim = "\033[22;2m", *bold = "\033[22;1m", *normal = "\033[22m";
    fputs(normal, out);
    const char *state = normal;
    const char *matchstyle = option ? bold : normal;
    if (!option) option = patt;
    size_t linelen = strlen(option), patlen = strlen(patt);
    int p = 0, run = 0;
    int *cache = calloc((linelen+1)*(patlen+1), sizeof(int));
    int backtrack = 0, to_start = 0;
    for (int i = 0; i < (int)linelen; i++) {
        if (EQ(patt[p], option[i]) &&
            run + lcs(option,patt,(int)linelen,(int)patlen,i,p,cache)
            >= lcs(option,patt,(int)linelen,(int)patlen,i+1,p,cache)) {
            if (state != matchstyle) {
                fputs(matchstyle, out);
                state = matchstyle;
            }
            int len = fputc_escaped(option[i], out);
            if (p >= cursor) backtrack += len;
            else to_start += len;
            ++run;
            ++p;
        } else {
            run = 0;
            if (state != dim) {
                fputs(dim, out);
                state = dim;
            }
            int len = fputc_escaped(option[i], out);
            if (p >= cursor) backtrack += len;
            else to_start += len;
        }
    }
    if (backtrack) fprintf(out, "\033[0m\033[%dD", backtrack);
    free(cache);
    return to_start;
}

/*
 * A basic fuzzy matcher and line inputter
 */
static char *get_input(FILE *in, FILE *out, const char *prompt, const char *initial, int nopts, char **opts)
{
    fprintf(out, "\033[K\033[0;1m%s\033[0m", prompt);
    size_t cap = initial ? strlen(initial) + 100 : 100;
    char *buf = memcheck(calloc(cap, 1));
    char *picked = NULL;
    int b = 0, len = 0;
    if (initial) {
        strcpy(buf, initial);
        len = (int)strlen(initial);
        b = len;
    }

    int start = 0, backtrack = 0;
    while (1) {
        case_sensitive = 0;
        for (const char *p = buf; *p; ++p)
            case_sensitive |= ('A' <= *p && *p <= 'Z');

        int ncandidates = 0;
        picked = NULL;
        if (buf[0]) {
            for (int i = 0; i < nopts; i++) {
                int j = (start + i) % nopts;
                if (matches(opts[j], buf)) {
                    ++ncandidates;
                    if (!picked) {
                        picked = opts[j];
                        start = j;
                    }
                }
            }
        }

        if (quickpick && ncandidates == 1 && picked)
            goto finished;

        if (backtrack) fprintf(out, "\033[%dD", backtrack);
        fputs("\033[K", out);
        if (password) {
            if (picked) fputs("\033[0;32m", out);
            else if (nopts > 0) fputs("\033[0;31m", out);
            else fputs("\033[0;2m", out);
            fputc((PASSWORD)[strlen(buf) % strlen(PASSWORD)], out);
            fputs("\033[0m", out);
            backtrack = 1;
        } else {
            backtrack = draw_line(out, picked, buf, b);
        }
        fflush(out);
        int key;
      skip_redraw:
        key = bgetkey(in, NULL, NULL, -1);
        switch (key) {
            case -1: case -2: case -3: goto skip_redraw;
            case '\r':
                goto finished;
            case KEY_CTRL_C: case KEY_ESC:
                free(buf);
                buf = NULL;
                picked = NULL;
                goto finished;
            case KEY_CTRL_A:
                b = 0;
                break;
            case KEY_CTRL_E:
                b = len;
                break;
            case KEY_CTRL_U: {
                int to_clear = b;
                if (to_clear) {
                    memmove(buf, buf+b, (size_t)(len-b));
                    buf[len -= b] = 0;
                    b = 0;
                }
                break;
            }
            case KEY_CTRL_K:
                if (b < len)
                    buf[len = b] = 0;
                break;
            case KEY_CTRL_N:
                if (picked) {
                    for (int i = 1; i < nopts; i++) {
                        int j = (start + i) % nopts;
                        if (matches(opts[j], buf)) {
                            start = j;
                            break;
                        }
                    }
                }
                break;
            case KEY_CTRL_P:
                if (picked) {
                    for (int i = nopts-1; i > 0; i--) {
                        int j = (start + i) % nopts;
                        if (matches(opts[j], buf)) {
                            start = j;
                            break;
                        }
                    }
                }
                break;
            case KEY_BACKSPACE: case KEY_BACKSPACE2:
                if (b > 0) {
                    --b;
                    memmove(buf+b, buf+b+1, (size_t)(len-b));
                    buf[--len] = 0;
                }
                break;
            case KEY_DELETE: case KEY_CTRL_D:
                if (b < len) {
                    memmove(buf+b, buf+b+1, (size_t)(len-b));
                    buf[--len] = 0;
                }
                break;
            case KEY_ARROW_LEFT: case KEY_CTRL_B:
                if (b > 0) --b;
                break;
            case KEY_ARROW_RIGHT: case KEY_CTRL_F:
                if (b < len) ++b;
                break;
            case KEY_CTRL_Q: case KEY_CTRL_V: {
                int nextkey;
                while ((nextkey = bgetkey(in, NULL, NULL, -1)) < 0)
                    ;
                if (len + 1 >= (int)cap)
                    buf = memcheck(realloc(buf, (cap += 100)));
                if (b < len)
                    memmove(buf+b+1, buf+b, (size_t)(len-b+1));
                buf[b++] = (char)nextkey;
                buf[++len] = 0;
                break;
            }
            case KEY_CTRL_T: {
                if (len < 2 || b == 0) break;
                if (b < len)
                    b++;
                char tmp = buf[b-1];
                buf[b-1] = buf[b-2];
                buf[b-2] = tmp;
                break;
            }
            default:
                if ((' ' <= key && key <= '~') || key == '\t') {
                    if (len + 1 >= (int)cap)
                        buf = memcheck(realloc(buf, (cap += 100)));
                    if (b < len)
                        memmove(buf+b+1, buf+b, (size_t)(len-b+1));
                    buf[b++] = (char)key;
                    buf[++len] = 0;
                }
                break;
        }
    }
  finished:
    if (picked) picked = memcheck(strdup(picked));
    else picked = buf;
    if (backtrack || prompt[0])
        fprintf(out, "\033[%dD", backtrack + (int)strlen(prompt));
    fputs("\033[0m\033[K", out);
    if (picked != buf && buf) free(buf);
    return picked;
}

static int cmp_len(const void *v1, const void *v2)
{
    char *s1 = *(char**)v1, *s2 = *(char**)v2;
    size_t len1 = strlen(s1), len2 = strlen(s2);
    if (len1 != len2) return (int)(len1 - len2);
    return strcmp(s1, s2);
}

int main(int argc, char *argv[])
{
    int yes = 0, no = 0;
    char *prompt = NULL, *query = NULL;
    char **opts = NULL;
    size_t linescap = 0, linecap = 0;
    int nopts = 0;
    char *line = NULL;
    struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
    if (poll(&pfd, 1, 50) > 0) {
        while ((getline(&line, &linecap, stdin)) >= 0) {
            if ((size_t)nopts >= linescap)
                opts = memcheck(realloc(opts, (linescap += 100)*sizeof(char*)));
            if (!line[0]) continue;
            if (line[strlen(line)-1] == '\n')
                line[strlen(line)-1] = '\0';
            opts[nopts++] = memcheck(strdup(line));
        }
    }
    int a = 1;
    while (a < argc) {
        if (argv[a][0] == '-' && argv[a][1] != '-') {
            for (char *p = &argv[a][1]; *p; p++) {
                switch (*p) {
                    case 'P': password = 1; break;
                    case 'q': quickpick = 1; break;
                    case 'h': goto help_flag;
                    case 'v': goto version_flag;
                    case 'y': yes = 1; quickpick = 1; break;
                    case 'n': no = 1; quickpick = 1; break;
                }
            }
        } else if (strcmp(argv[a], "-p") == 0) {
            prompt = argv[++a];
        } else if (strcmp(argv[a], "-q") == 0) {
            query = argv[++a];
        } else if (strncmp(argv[a], "--prompt=", strlen("--prompt=")) == 0) {
            prompt = &argv[a][strlen("--prompt=")];
        } else if (strncmp(argv[a], "--query=", strlen("--query=")) == 0) {
            query = &argv[a][strlen("--query=")];
        } else if (strcmp(argv[a], "--password") == 0) {
            password = 1;
        } else if (strcmp(argv[a], "--quickpick") == 0) {
            quickpick = 1;
        } else if (strcmp(argv[a], "--yes") == 0) {
            yes = 1;
            quickpick = 1;
        } else if (strcmp(argv[a], "--no") == 0) {
            no = 1;
            quickpick = 1;
        } else if (strcmp(argv[a], "--help") == 0) {
          help_flag:
            printf("ask - A simple command line input tool.\n"
                   "Usage: ask [-Q|--quickpick] [-P|--password] [-v|--version] [-h|--help] "
                   "[-y|--yes] [-n|--no] [-q |--query=query] [[-p |--prompt=]prompt [options...]]\n");
            return 0;
        } else if (strcmp(argv[a], "--version") == 0) {
          version_flag:
            printf("ask %s\n", ASK_VERSION);
            return 0;
        } else break;
        ++a;
    }
    if (!prompt && a < argc) prompt = argv[a++];

    while (a < argc) {
        if ((size_t)nopts >= linescap)
            opts = memcheck(realloc(opts, (linescap += 100)*sizeof(char*)));
        opts[nopts++] = argv[a++];
    }

    if (yes || no) {
        if ((size_t)nopts + 4 >= linescap)
            opts = memcheck(realloc(opts, (linescap += 4)*sizeof(char*)));
        opts[nopts++] = "y";
        opts[nopts++] = "n";
        opts[nopts++] = "Y";
        opts[nopts++] = "N";
    }

    if (yes) {
        char *p2 = memcheck(calloc((prompt ? strlen(prompt) : 0)+5+1, sizeof(char)));
        if (prompt) strcpy(p2, prompt);
        strcat(p2, "[Y/n]");
        prompt = p2;
    } else if (no) {
        char *p2 = memcheck(calloc((prompt ? strlen(prompt) : 0)+5+1, sizeof(char)));
        if (prompt) strcpy(p2, prompt);
        strcat(p2, "[y/N]");
        prompt = p2;
    }

    if (!prompt) prompt = "> ";

    FILE *tty_in = fopen("/dev/tty", "r");
    FILE *tty_out = fopen("/dev/tty", "w");
    struct termios orig_termios, bb_termios;
    tcgetattr(fileno(tty_out), &orig_termios);
    cfmakeraw(&bb_termios);
    if (tcsetattr(fileno(tty_out), TCSAFLUSH, &bb_termios) == -1)
        return 1;

    // Prefer shorter matches, but otherwise use alphabetic order
    qsort(opts, (size_t)nopts, sizeof(char*), cmp_len);
    char *output = get_input(tty_in, tty_out, prompt, query, nopts, opts);

    fflush(tty_out);
    tcsetattr(fileno(tty_out), TCSAFLUSH, &orig_termios);
    fclose(tty_out);
    tty_out = NULL;
    fclose(tty_in);
    tty_in = NULL;

    // This doesn't free memory, but it doesn't need to because
    // the program is exiting
    if (!output) return 1;

    fputs(output, stdout);
    if (yes)
        return strcmp(output, "n") == 0 && strcmp(output, "N") == 0;
    if (no)
        return strcmp(output, "y") != 0 && strcmp(output, "Y") != 0;

    return 0;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1