Skip to content

Commit

Permalink
feat: no empty command completion if no_empty_cmd_completion is on
Browse files Browse the repository at this point in the history
On some systems, generating the entire list of available commands takes
a long time. `no_empty_cmd_completion` is semantically close enough to
piggyback on it here.

Closes #926
  • Loading branch information
scop committed Jun 1, 2023
1 parent 9f7931a commit faab292
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 26 deletions.
9 changes: 9 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,15 @@ _complete_as_root()
[[ $EUID -eq 0 || ${root_command-} ]]
}

# Complete on available commands, subject to `no_empty_cmd_completion`.
#
# @since 2.12
_comp_compgen_commands()
{
[[ ! ${cur-} ]] && shopt -q no_empty_cmd_completion && return
_comp_compgen -- -c
}

# @since 2.12
_comp_longopt()
{
Expand Down
2 changes: 1 addition & 1 deletion completions/_svn
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _comp_cmd_svn()
;;
--editor-cmd | --diff-cmd | --diff3-cmd)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/cpio
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _comp_cmd_cpio()
;;
--rsh-command)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/dpkg
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ _comp_cmd_dpkg()
return
;;
--status-logger)
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--verify-format)
Expand Down
4 changes: 2 additions & 2 deletions completions/fio
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ _comp_cmd_fio()
;;
--trigger | --trigger-remote)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--aux-path)
Expand All @@ -88,7 +88,7 @@ _comp_cmd_fio()
return
;;
--exec_postrun | --exec_prerun)
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--uid)
Expand Down
2 changes: 1 addition & 1 deletion completions/firefox
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ _comp_cmd_firefox()
return
;;
--debugger | -d)
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/man
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ _comp_cmd_man()
;;
--pager | -${noargopts}P)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--preprocessor | -${noargopts}p)
Expand Down
2 changes: 1 addition & 1 deletion completions/mr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _comp_cmd_mr()
return
;;
run)
_comp_compgen -- -c
_comp_compgen_commands
return
;;
*)
Expand Down
2 changes: 1 addition & 1 deletion completions/mussh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _comp_cmd_mussh()
;;
-c)
compopt -o filenames
_comp_compgen -a -- -c
_comp_compgen -a commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/mypy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _comp_cmd_mypy()
return
;;
--python-executable)
_comp_compgen -c "${cur:-py}" -- -c
_comp_compgen -c "${cur:-py}" commands
return
;;
--*-dir | --*-report)
Expand Down
2 changes: 1 addition & 1 deletion completions/perlcritic
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _comp_cmd_perlcritic()
;;
--pager)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/protoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _comp_cmd_protoc()
--plugin)
if [[ $cur != *=* ]]; then
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
fi
return
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/querybts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _comp_cmd_querybts()
;;
--mbox-reader-cmd)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/reportbug
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _comp_cmd_reportbug()
;;
--editor | --mua | --mbox-reader-cmd | -${noargopts}e)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--from-buildd)
Expand Down
2 changes: 1 addition & 1 deletion completions/rpm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ _comp_cmd_rpm()
;;
--pipe)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--rcfile)
Expand Down
2 changes: 1 addition & 1 deletion completions/sqlite3
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _comp_cmd_sqlite3()
;;
-cmd)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
8 changes: 4 additions & 4 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ _comp_cmd_ssh__suboption()
_known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
;;
proxycommand | remotecommand | localcommand)
_comp_compgen -- -c
_comp_compgen_commands
;;
pubkeyacceptedalgorithms | pubkeyacceptedkeytypes)
_comp_compgen_split -- "$(_comp_cmd_ssh__query "$1" key)"
Expand Down Expand Up @@ -371,7 +371,7 @@ _comp_cmd_ssh()
_count_args "=" "-*[BbcDeLpRWEFSIiJlmOoQw]"
if ((args > 1)); then
compopt -o filenames
_comp_compgen -a -- -c
_comp_compgen -a commands
else
_known_hosts_real ${ipvx-} -a ${configfile:+-F "$configfile"} \
-- "$cur"
Expand Down Expand Up @@ -425,7 +425,7 @@ _comp_cmd_sftp()
;;
-*S)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
-*4*)
Expand Down Expand Up @@ -575,7 +575,7 @@ _comp_cmd_scp()
;;
-*S)
compopt +o nospace -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
-*4*)
Expand Down
2 changes: 1 addition & 1 deletion completions/ssh-keygen
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ _comp_cmd_ssh_keygen()
case $cur in
force-command=*)
compopt -o filenames
_comp_compgen -c "${cur#*=}" -- -c
_comp_compgen -c "${cur#*=}" commands
;;
checkpoint=* | challenge=* | write-attestation=*)
_comp_compgen -c "${cur#*=}" filedir
Expand Down
2 changes: 1 addition & 1 deletion completions/strace
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ _comp_cmd_strace()
if [[ $cur == -* ]]; then
_comp_compgen_help -- -h
else
_comp_compgen -- -c
_comp_compgen_commands
fi
fi
} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/tar
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ _comp_cmd_tar__gnu()
--rmt-command | --rsh-command | --use-compress-program | \
-${noargopts}[FI])
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
break
;;
--atime-preserve)
Expand Down
2 changes: 1 addition & 1 deletion completions/tcpdump
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _comp_cmd_tcpdump()
;;
-${noargopts}z)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
--relinquish-privileges | -${noargopts}Z)
Expand Down
2 changes: 1 addition & 1 deletion completions/valgrind
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ _comp_cmd_valgrind()
;;
\<command\>)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
\<+([0-9])..+([0-9])\>)
Expand Down
2 changes: 1 addition & 1 deletion completions/vpnc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ _vpnc()
;;
--password-helper)
compopt -o filenames
_comp_compgen -- -c
_comp_compgen_commands
return
;;
esac
Expand Down
10 changes: 10 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ to have completion for their contents, so the default is unset.

Available since version 2.12.
Deprecated alias: `COMP_TAR_INTERNAL_PATHS`

## Shell options

### `no_empty_cmd_completion`

If on, completions producing command names do not do anything if the command to
be completed is empty. This can be useful on systems where generating the
entire list of commands takes a long time.

Available since version 2.12.
1 change: 1 addition & 0 deletions test/t/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ EXTRA_DIST = \
test_unit_abspath.py \
test_unit_command_offset.py \
test_unit_compgen.py \
test_unit_compgen_commands.py \
test_unit_count_args.py \
test_unit_delimited.py \
test_unit_deprecate_func.py \
Expand Down
34 changes: 34 additions & 0 deletions test/t/unit/test_unit_compgen_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from conftest import assert_bash_exec, bash_env_saved


@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=")
class TestUtilCompgenCommands:
@pytest.fixture(scope="class")
def functions(self, request, bash):
assert_bash_exec(
bash,
r"_comp_compgen_commands__test() {"
r" local COMPREPLY=() cur=${1-};"
r" _comp_compgen_commands;"
r' printf "%s\n" "${COMPREPLY[@]}";'
r"}",
)

def test_basic(self, bash, functions):
output = assert_bash_exec(
bash, "_comp_compgen_commands__test sh", want_output=True
)
assert output.strip()

@pytest.mark.parametrize(
"shopt_no_empty,result_empty", ((True, True), (False, False))
)
def test_empty(self, bash, functions, shopt_no_empty, result_empty):
with bash_env_saved(bash) as bash_env:
bash_env.shopt("no_empty_cmd_completion", shopt_no_empty)
output = assert_bash_exec(
bash, "_comp_compgen_commands__test", want_output=True
)
assert (output.strip() == "") == result_empty

0 comments on commit faab292

Please sign in to comment.