From 6060daaee163918f97af4db09bba9a7f9272d6c1 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sun, 14 May 2023 19:01:28 +0100 Subject: Add --print-index option. --- src/config.c | 5 +++++ src/entry.h | 8 +++++++- src/input.c | 4 ++-- src/main.c | 26 +++++++++++++++----------- src/tofi.h | 1 + 5 files changed, 30 insertions(+), 14 deletions(-) (limited to 'src') 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); diff --git a/src/main.c b/src/main.c index 76df63d..f7dce5d 100644 --- a/src/main.c +++ b/src/main.c @@ -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) { diff --git a/src/tofi.h b/src/tofi.h index 048f9c9..1345fd5 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -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]; -- cgit v1.2.3