From faf685ca266d9cd3e2bb42d2ef105a5577cfcaf1 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sun, 21 Aug 2022 20:58:10 +0100 Subject: Add --require-match option. --- completions/tofi | 1 + doc/config | 5 +++++ doc/tofi.5.md | 8 ++++++++ doc/tofi.5.scd | 7 +++++++ src/config.c | 2 ++ src/main.c | 22 ++++++++++++++++++---- src/tofi.h | 1 + 7 files changed, 42 insertions(+), 4 deletions(-) diff --git a/completions/tofi b/completions/tofi index 88988f9..f20cb33 100644 --- a/completions/tofi +++ b/completions/tofi @@ -42,6 +42,7 @@ _tofi() --hide-cursor --history --fuzzy-match + --require-match --drun-launch --hint-font --late-keyboard-init diff --git a/doc/config b/doc/config index f544966..4d337b5 100644 --- a/doc/config +++ b/doc/config @@ -127,6 +127,11 @@ # Use fuzzy matching for searches. fuzzy-match = false + # If true, require a match to allow a selection to be made. If false, + # making a selection with no matches will print input to stdout. + # In drun mode, this is always true. + require-match = true + # If true, directly launch applications on selection when in drun mode. # Otherwise, just print the command line to stdout. drun-launch = false diff --git a/doc/tofi.5.md b/doc/tofi.5.md index 6eed96d..ba88c1b 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -260,6 +260,14 @@ options. > > Default: false +**require-match**=*true\|false* + +> If true, require a match to allow a selection to be made. If false, +> making a selection with no matches will print input to stdout. In drun +> mode, this is always true. +> +> Default: true + **drun-launch**=*true\|false* > If true, directly launch applications on selection when in drun mode. diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd index cf605b3..31f8083 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -226,6 +226,13 @@ options. Default: false +*require-match*=_true|false_ + If true, require a match to allow a selection to be made. If false, + making a selection with no matches will print input to stdout. + In drun mode, this is always true. + + Default: true + *drun-launch*=_true|false_ If true, directly launch applications on selection when in drun mode. Otherwise, just print the path of the .desktop file to stdout. diff --git a/src/config.c b/src/config.c index 3cbb18b..5dff7f2 100644 --- a/src/config.c +++ b/src/config.c @@ -324,6 +324,8 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const tofi->use_history = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "fuzzy-match") == 0) { tofi->fuzzy_match = parse_bool(filename, lineno, value, &err); + } else if (strcasecmp(option, "require-match") == 0) { + tofi->require_match = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "drun-launch") == 0) { tofi->drun_launch = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "drun-print-exec") == 0) { diff --git a/src/main.c b/src/main.c index 9f244af..62a4e81 100644 --- a/src/main.c +++ b/src/main.c @@ -811,6 +811,7 @@ static void usage() " --horizontal List results horizontally.\n" " --history Sort results by number of usages.\n" " --fuzzy-match Use fuzzy matching for searching.\n" +" --require-match Require a match for selection.\n" " --drun-launch Launch apps directly in drun mode.\n" " --drun-print-exec Print a command line in drun mode.\n" " This is now always the case,\n" @@ -858,6 +859,7 @@ const struct option long_options[] = { {"hide-cursor", required_argument, NULL, 0}, {"history", required_argument, NULL, 0}, {"fuzzy-match", required_argument, NULL, 0}, + {"require-match", required_argument, NULL, 0}, {"drun-launch", required_argument, NULL, 0}, {"drun-print-exec", required_argument, NULL, 0}, {"hint-font", required_argument, NULL, 0}, @@ -933,11 +935,22 @@ static void parse_args(struct tofi *tofi, int argc, char *argv[]) } } -static void do_submit(struct tofi *tofi) +static bool do_submit(struct tofi *tofi) { struct entry *entry = &tofi->window.entry; uint32_t selection = entry->selection + entry->first_result; char *res = entry->results.buf[selection].string; + + if (tofi->window.entry.results.count == 0) { + /* Always require a match in drun mode. */ + if (tofi->require_match || entry->drun) { + return false; + } else { + printf("%ls\n", entry->input); + return true; + } + } + if (entry->drun) { /* * TODO: This is ugly. The list of apps needs to be sorted @@ -948,7 +961,7 @@ static void do_submit(struct tofi *tofi) struct desktop_entry *app = desktop_vec_find(&entry->apps, res); if (app == NULL) { log_error("Couldn't find application file! This shouldn't happen.\n"); - return; + return false; } else { res = app->path; } @@ -966,6 +979,7 @@ static void do_submit(struct tofi *tofi) entry->results.buf[selection].string); history_save(&entry->history, entry->drun); } + return true; } int main(int argc, char *argv[]) @@ -1007,6 +1021,7 @@ int main(int argc, char *argv[]) | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, .use_history = true, + .require_match = true, }; wl_list_init(&tofi.output_list); @@ -1432,8 +1447,7 @@ int main(int argc, char *argv[]) } if (tofi.submit) { tofi.submit = false; - if (tofi.window.entry.results.count > 0) { - do_submit(&tofi); + if (do_submit(&tofi)) { break; } } diff --git a/src/tofi.h b/src/tofi.h index eb3f0ce..419f86e 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -84,6 +84,7 @@ struct tofi { bool drun_launch; bool drun_print_exec; bool fuzzy_match; + bool require_match; char target_output_name[MAX_OUTPUT_NAME_LEN]; }; -- cgit v1.2.3