Skip to content

Commit cfab8ef

Browse files
committed
Add --auto-accept-single option.
1 parent 0fe8548 commit cfab8ef

File tree

8 files changed

+39
-1
lines changed

8 files changed

+39
-1
lines changed

completions/tofi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ _tofi()
7878
--history-file
7979
--fuzzy-match
8080
--require-match
81+
--auto-accept-single
8182
--hide-input
8283
--hidden-character
8384
--drun-launch

doc/config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@
257257
# In drun mode, this is always true.
258258
require-match = true
259259

260+
# If true, automatically accept a result if it is the only one
261+
# remaining. If there's only one result on startup, window creation is
262+
# skipped altogether.
263+
auto-accept-single = false
264+
260265
# If true, typed input will be hidden, and what is displayed (if
261266
# anything) is determined by the hidden-character option.
262267
hide-input = false

doc/tofi.5.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ options.
8989
>
9090
> Default: true
9191
92+
**auto-accept-single**=*true\|false*
93+
94+
> If true, automatically accept a result if it is the only one
95+
> remaining. If there's only one result on startup, window creation is
96+
> skipped altogether.
97+
>
98+
> Default: false
99+
92100
**hide-input**=*true\|false*
93101

94102
> If true, typed input will be hidden, and what is displayed (if

doc/tofi.5.scd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ options.
7676

7777
Default: true
7878

79+
*auto-accept-single*=_true|false_
80+
If true, automatically accept a result if it is the only one remaining.
81+
If there's only one result on startup, window creation is skipped
82+
altogether.
83+
84+
Default: false
85+
7986
*hide-input*=_true|false_
8087
If true, typed input will be hidden, and what is displayed (if
8188
anything) is determined by the *hidden-character* option.

src/config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const
693693
if (!err) {
694694
tofi->require_match = val;
695695
}
696+
} else if (strcasecmp(option, "auto-accept-single") == 0) {
697+
bool val = parse_bool(filename, lineno, value, &err);
698+
if (!err) {
699+
tofi->auto_accept_single = val;
700+
}
696701
} else if (strcasecmp(option, "hide-input") == 0) {
697702
bool val = parse_bool(filename, lineno, value, &err);
698703
if (!err) {

src/input.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ void input_handle_keypress(struct tofi *tofi, xkb_keycode_t keycode)
110110
return;
111111
}
112112

113+
if (tofi->auto_accept_single && tofi->window.entry.results.count == 1) {
114+
tofi->submit = true;
115+
}
116+
113117
tofi->window.surface.redraw = true;
114118
}
115119

src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ const struct option long_options[] = {
913913
{"history-file", required_argument, NULL, 0},
914914
{"fuzzy-match", required_argument, NULL, 0},
915915
{"require-match", required_argument, NULL, 0},
916+
{"auto-accept-single", required_argument, NULL, 0},
916917
{"hide-input", required_argument, NULL, 0},
917918
{"hidden-character", required_argument, NULL, 0},
918919
{"drun-launch", required_argument, NULL, 0},
@@ -1198,7 +1199,7 @@ int main(int argc, char *argv[])
11981199
}
11991200

12001201
parse_args(&tofi, argc, argv);
1201-
log_debug("Config done\n");
1202+
log_debug("Config done.\n");
12021203

12031204
if (!tofi.multiple_instance && lock_check()) {
12041205
log_error("Another instance of tofi is already running.\n");
@@ -1498,6 +1499,12 @@ int main(int argc, char *argv[])
14981499
}
14991500
tofi.window.entry.results = string_ref_vec_copy(&tofi.window.entry.commands);
15001501

1502+
if (tofi.auto_accept_single && tofi.window.entry.results.count == 1) {
1503+
log_debug("Only one result, exiting.\n");
1504+
do_submit(&tofi);
1505+
return EXIT_SUCCESS;
1506+
}
1507+
15011508
/*
15021509
* Next, we create the Wayland surface, which takes on the
15031510
* layer shell role.

src/tofi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct tofi {
9999
bool drun_print_exec;
100100
bool fuzzy_match;
101101
bool require_match;
102+
bool auto_accept_single;
102103
bool multiple_instance;
103104
char target_output_name[MAX_OUTPUT_NAME_LEN];
104105
char default_terminal[MAX_TERMINAL_NAME_LEN];

0 commit comments

Comments
 (0)