diff options
-rw-r--r-- | completions/tofi | 1 | ||||
-rw-r--r-- | doc/config | 6 | ||||
-rw-r--r-- | doc/tofi.5.md | 9 | ||||
-rw-r--r-- | doc/tofi.5.scd | 8 | ||||
-rw-r--r-- | src/config.c | 5 | ||||
-rw-r--r-- | src/entry.h | 8 | ||||
-rw-r--r-- | src/input.c | 4 | ||||
-rw-r--r-- | src/main.c | 26 | ||||
-rw-r--r-- | src/tofi.h | 1 |
9 files changed, 54 insertions, 14 deletions
diff --git a/completions/tofi b/completions/tofi index e588694..38ef388 100644 --- a/completions/tofi +++ b/completions/tofi @@ -80,6 +80,7 @@ _tofi() --fuzzy-match --require-match --auto-accept-single + --print-index --hide-input --hidden-character --drun-launch @@ -277,6 +277,12 @@ # This option only has an effect when hide-input is set to true. hidden-character = "*" + # Instead of printing the selected entry, print the 1-based index of + # the selection. This option has no effect in run or drun mode. If + # require-match is set to false, non-matching input will still result + # in the input being printed. + print-index = false + # 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 dfef45a..3656143 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -117,6 +117,15 @@ options. > > Default: \* +**print-index**=*true\|false* + +> Instead of printing the selected entry, print the 1-based index of the +> selection. This option has no effect in run or drun mode. If +> **require-match** is set to false, non-matching input will still +> result in the input being printed. +> +> Default: false + **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 013dc57..de564c5 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -109,6 +109,14 @@ options. Default: \* +*print-index*=_true|false_ + Instead of printing the selected entry, print the 1-based index of the + selection. This option has no effect in run or drun mode. If + *require-match* is set to false, non-matching input will still result in + the input being printed. + + Default: false + *drun-launch*=_true|false_ If true, directly launch applications on selection when in drun mode. Otherwise, just print the Exec line of the .desktop file to stdout. diff --git a/src/config.c b/src/config.c index 556199d..c0ede7a 100644 --- a/src/config.c +++ b/src/config.c @@ -708,6 +708,11 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const if (!err) { tofi->auto_accept_single = val; } + } else if (strcasecmp(option, "print-index") == 0) { + bool val = parse_bool(filename, lineno, value, &err); + if (!err) { + tofi->print_index = val; + } } else if (strcasecmp(option, "hide-input") == 0) { bool val = parse_bool(filename, lineno, value, &err); if (!err) { diff --git a/src/entry.h b/src/entry.h index 35d2302..e314a8a 100644 --- a/src/entry.h +++ b/src/entry.h @@ -18,6 +18,12 @@ #define MAX_FONT_FEATURES_LENGTH 128 #define MAX_FONT_VARIATIONS_LENGTH 128 +enum tofi_mode { + TOFI_MODE_PLAIN, + TOFI_MODE_RUN, + TOFI_MODE_DRUN +}; + enum cursor_style { CURSOR_STYLE_BAR, CURSOR_STYLE_BLOCK, @@ -90,7 +96,7 @@ struct entry { uint32_t clip_height; /* Options */ - bool drun; + enum tofi_mode mode; bool horizontal; bool hide_input; char hidden_character_utf8[6]; diff --git a/src/input.c b/src/input.c index 429b1d1..fe777f6 100644 --- a/src/input.c +++ b/src/input.c @@ -127,7 +127,7 @@ void add_character(struct tofi *tofi, xkb_keycode_t keycode) N_ELEM(buf)); entry->input_utf8_length += len; - if (entry->drun) { + if (entry->mode == TOFI_MODE_DRUN) { struct string_ref_vec results = desktop_vec_filter(&entry->apps, entry->input_utf8, tofi->matching_algorithm); string_ref_vec_destroy(&entry->results); entry->results = results; @@ -165,7 +165,7 @@ void input_refresh_results(struct tofi *tofi) entry->input_utf8[bytes_written] = '\0'; entry->input_utf8_length = bytes_written; string_ref_vec_destroy(&entry->results); - if (entry->drun) { + if (entry->mode == TOFI_MODE_DRUN) { entry->results = desktop_vec_filter(&entry->apps, entry->input_utf8, tofi->matching_algorithm); } else { entry->results = string_ref_vec_filter(&entry->commands, entry->input_utf8, tofi->matching_algorithm); @@ -914,6 +914,7 @@ const struct option long_options[] = { {"matching-algorithm", required_argument, NULL, 0}, {"require-match", required_argument, NULL, 0}, {"auto-accept-single", required_argument, NULL, 0}, + {"print-index", required_argument, NULL, 0}, {"hide-input", required_argument, NULL, 0}, {"hidden-character", required_argument, NULL, 0}, {"drun-launch", required_argument, NULL, 0}, @@ -1006,7 +1007,7 @@ static bool do_submit(struct tofi *tofi) if (tofi->window.entry.results.count == 0) { /* Always require a match in drun mode. */ - if (tofi->require_match || entry->drun) { + if (tofi->require_match || entry->mode == TOFI_MODE_DRUN) { return false; } else { printf("%s\n", entry->input_utf8); @@ -1014,7 +1015,7 @@ static bool do_submit(struct tofi *tofi) } } - if (entry->drun) { + if (entry->mode == TOFI_MODE_DRUN) { /* * At this point, the list of apps is history sorted rather * than alphabetically sorted, so we can't use @@ -1038,14 +1039,18 @@ static bool do_submit(struct tofi *tofi) drun_print(path, tofi->default_terminal); } } else { - printf("%s\n", res); + if (entry->mode == TOFI_MODE_PLAIN && tofi->print_index) { + printf("%u\n", selection + 1); + } else { + printf("%s\n", res); + } } if (tofi->use_history) { history_add( &entry->history, entry->results.buf[selection].string); if (tofi->history_file[0] == 0) { - history_save_default_file(&entry->history, entry->drun); + history_save_default_file(&entry->history, entry->mode == TOFI_MODE_DRUN); } else { history_save(&entry->history, tofi->history_file); } @@ -1444,11 +1449,12 @@ int main(int argc, char *argv[]) if (strstr(argv[0], "-run")) { log_debug("Generating command list.\n"); log_indent(); + tofi.window.entry.mode = TOFI_MODE_RUN; tofi.window.entry.command_buffer = compgen_cached(); struct string_ref_vec commands = string_ref_vec_from_buffer(tofi.window.entry.command_buffer); if (tofi.use_history) { if (tofi.history_file[0] == 0) { - tofi.window.entry.history = history_load_default_file(tofi.window.entry.drun); + tofi.window.entry.history = history_load_default_file(false); } else { tofi.window.entry.history = history_load(tofi.history_file); } @@ -1462,17 +1468,15 @@ int main(int argc, char *argv[]) } else if (strstr(argv[0], "-drun")) { log_debug("Generating desktop app list.\n"); log_indent(); - tofi.window.entry.drun = true; + tofi.window.entry.mode = TOFI_MODE_DRUN; struct desktop_vec apps = drun_generate_cached(); if (tofi.use_history) { if (tofi.history_file[0] == 0) { - tofi.window.entry.history = history_load_default_file(tofi.window.entry.drun); + tofi.window.entry.history = history_load_default_file(true); } else { tofi.window.entry.history = history_load(tofi.history_file); } - if (tofi.window.entry.drun) { - drun_history_sort(&apps, &tofi.window.entry.history); - } + drun_history_sort(&apps, &tofi.window.entry.history); } struct string_ref_vec commands = string_ref_vec_create(); for (size_t i = 0; i < apps.count; i++) { @@ -1891,7 +1895,7 @@ int main(int argc, char *argv[]) xkb_keymap_unref(tofi.xkb_keymap); xkb_context_unref(tofi.xkb_context); wl_registry_destroy(tofi.wl_registry); - if (tofi.window.entry.drun) { + if (tofi.window.entry.mode == TOFI_MODE_DRUN) { desktop_vec_destroy(&tofi.window.entry.apps); } if (tofi.window.entry.command_buffer != NULL) { @@ -101,6 +101,7 @@ struct tofi { bool drun_print_exec; bool require_match; bool auto_accept_single; + bool print_index; bool multiple_instance; char target_output_name[MAX_OUTPUT_NAME_LEN]; char default_terminal[MAX_TERMINAL_NAME_LEN]; |