From a265492706a51244bc818c3e56fe204489eaf222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Sat, 13 Aug 2022 00:13:32 -0300 Subject: [PATCH] Add intial query command line option. It has an existing bug where the filter is only applied after the first user interaction. --- ef.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ef.c b/ef.c index 2a293fa..f7247f2 100644 --- a/ef.c +++ b/ef.c @@ -43,9 +43,10 @@ int main(int argc, char **argv) setlocale(LC_ALL, ""); char delim = '\n'; + char *query = NULL; int opt; - while ((opt = getopt(argc, argv, "01")) != -1) { + while ((opt = getopt(argc, argv, "01q:")) != -1) { switch (opt) { case '0': /* set delimiter to NUL char instead of newline */ @@ -54,6 +55,10 @@ int main(int argc, char **argv) case '1': /* ignored for compatibility with fzf */ break; + case 'q': + /* initial query */ + query = optarg; + break; default: exit(1); } @@ -184,6 +189,16 @@ int main(int argc, char **argv) char *name = NULL; /* search tokens */ struct str_array toks = { 0 }; + + if (query) { + /* the intial query can be edited, + * so it needs to be the same sort of storage */ + name = xmalloc(cap); + strncpy(name, query, cap); + add_entry(&toks, name); + + name = NULL; + } print_prompt(prompt, &toks, true); /* index inside set of matches */