Skip to content

Commit 3104050

Browse files
committed
feat(_comp_compgen_filedir): automatically add "-f"
1 parent f817cca commit 3104050

File tree

9 files changed

+33
-18
lines changed

9 files changed

+33
-18
lines changed

bash_completion

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ _comp_compgen__call_generator()
791791
local _comp_compgen__append=$_append
792792
local _comp_compgen__var=$_var
793793
local _comp_compgen__cur=$_cur cur=$_cur
794+
local _comp_compgen__dir=$_dir
795+
local _comp_compgen__prefix=$_prefix
794796
if [[ $_prefix ]]; then
795797
local -a tmp=()
796798
local _comp_compgen__var=tmp
@@ -1287,10 +1289,7 @@ _comp_quote_compgen()
12871289
# OPTIONS
12881290
# -d Complete only on directories
12891291
# -f Perform `compopt -o filenames` modifications manually. This
1290-
# suffixes a slash to a directory name. This can be combined with
1291-
# the `-C dir` option to `_comp_compgen`, where the generated
1292-
# filenames do not exist in the current working directory and Bash
1293-
# fails to properly detect the filenames.
1292+
# suffixes a slash to a directory name.
12941293
# @return 0 if at least one completion is generated, or 1 otherwise.
12951294
#
12961295
# @since 2.12
@@ -1300,6 +1299,19 @@ _comp_compgen_filedir()
13001299

13011300
local -a toks
13021301
local _dir="" _filenames=""
1302+
if [[ ${_comp_compgen__dir-} || ${_comp_compgen__prefix-} ]]; then
1303+
# When "-C <dir>" or "-P <prefix>" is specified, the working directory
1304+
# does not contain the generated candidates as filenames. In the case
1305+
# of "-C <dir>", the generated filenames are located in the directory
1306+
# "<dir>" but not in the working directory. In the case of "-P
1307+
# <prefix>", candidates have the form "<prefix><filename>". In those
1308+
# cases, Bash fails to recognize the generated candidates to be
1309+
# filenames, and Bash does not adjust these as filenames. We
1310+
# automatically detect such cases and modify the generated filenames as
1311+
# if the option "-f" is passed.
1312+
_filenames=set
1313+
fi
1314+
13031315
local OPTIND=1 OPTARG="" OPTERR=0 _opt
13041316
while getopts ":df" _opt "$@"; do
13051317
case $_opt in

completions-core/curl.bash

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ _comp_cmd_curl()
5555
if [[ $cur == \@* ]]; then
5656
_comp_compgen -P @ filedir
5757
_comp_compgen -aP @ -- -W '-'
58-
if [[ ${#COMPREPLY[@]} -eq 1 && -d ${COMPREPLY[0]#@} ]]; then
59-
COMPREPLY[0]+=/
60-
compopt -o nospace
61-
fi
6258
fi
6359
return
6460
;;

completions-core/removepkg.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _comp_cmd_removepkg()
1515
fi
1616

1717
local root=${ROOT:-/}
18-
_comp_compgen -C "$root/var/log/packages" filedir -f
18+
_comp_compgen -C "$root/var/log/packages" filedir
1919
} &&
2020
complete -F _comp_cmd_removepkg removepkg
2121

completions-core/sbopkg.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ _comp_cmd_sbopkg()
6464
_comp_compgen_split -l -- "$(command sed -ne "s/^SLACKBUILD NAME: //p" \
6565
"$file")"
6666
if [[ -d ${QUEUEDIR-} ]]; then
67-
_comp_compgen -aC "$QUEUEDIR" filedir -f sqf
67+
_comp_compgen -aC "$QUEUEDIR" filedir sqf
6868
fi
6969
} &&
7070
complete -F _comp_cmd_sbopkg sbopkg

completions-core/slapt-get.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ _comp_cmd_slapt_get()
7474
return
7575
;;
7676
ins) # --remove|--filelist
77-
_comp_compgen -C /var/log/packages filedir -f
77+
_comp_compgen -C /var/log/packages filedir
7878
return
7979
;;
8080
set) # --install-set

completions-fallback/mount.linux.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ _comp_cmd_mount()
3232
return
3333
;;
3434
-L)
35-
_comp_compgen -C "/dev/disk/by-label/" filedir -f
35+
_comp_compgen -C "/dev/disk/by-label/" filedir
3636
return
3737
;;
3838
-U)
39-
_comp_compgen -C "/dev/disk/by-uuid/" filedir -f
39+
_comp_compgen -C "/dev/disk/by-uuid/" filedir
4040
return
4141
;;
4242
-O | --test-opts)

completions-fallback/slackpkg.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ _comp_cmd_slackpkg()
6565
;;
6666
install-template | remove-template)
6767
if [[ -e $confdir/templates ]]; then
68-
_comp_compgen -C "$confdir/templates" filedir -f template &&
68+
_comp_compgen -C "$confdir/templates" filedir template &&
6969
COMPREPLY=("${COMPREPLY[@]%.template}")
7070
fi
7171
return
@@ -74,7 +74,7 @@ _comp_cmd_slackpkg()
7474
_comp_compgen_filedir
7575
_comp_compgen -a -- -W 'a ap d e f k kde kdei l n t tcl x xap xfce
7676
y'
77-
_comp_compgen -aC /var/log/packages filedir -f
77+
_comp_compgen -aC /var/log/packages filedir
7878
return
7979
;;
8080
install | reinstall | upgrade | blacklist | download)

test/t/unit/test_unit_compgen.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ def test_6_option_C_1(self, bash, functions):
127127
want_output=True,
128128
)
129129
set1 = set(re.findall(r"<[^<>]*>", output.strip()))
130-
assert set1 == {"<a b>", "<a$b>", "<a&b>", "<a'b>", "<ab>", "<aé>"}
130+
assert set1 == {
131+
"<a b/>",
132+
"<a$b/>",
133+
"<a&b/>",
134+
"<a'b/>",
135+
"<ab/>",
136+
"<aé/>",
137+
}
131138

132139
def test_6_option_C_2(self, bash, functions):
133140
output = assert_bash_exec(
@@ -146,7 +153,7 @@ def test_6_option_C_3(self, bash, functions, funcname):
146153
def test_6_option_C_4(self, functions, completion):
147154
# Note: we are not in the original directory that "b" exists, so Bash
148155
# will not suffix a slash to the directory name.
149-
assert completion == "b"
156+
assert completion == "b/"
150157

151158
@pytest.mark.complete(r"fb nonexistent")
152159
def test_6_option_C_5(self, bash, functions, completion):

test/t/unit/test_unit_compgen_filedir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def functions(self, request, bash):
3434
)
3535
assert_bash_exec(
3636
bash,
37-
"_fcd() { local cur=$(_get_cword); unset -v COMPREPLY; _comp_compgen -C _filedir filedir -df; };"
37+
"_fcd() { local cur=$(_get_cword); unset -v COMPREPLY; _comp_compgen -C _filedir filedir -d; };"
3838
"complete -F _fcd fcd",
3939
)
4040

0 commit comments

Comments
 (0)