Skip to content

Commit 13868b8

Browse files
authored
Merge pull request #847 from scop/feat/filedir-C
feat(_filedir): add `-f` to manually suffix / to directory names
2 parents 8d54b26 + 99c599b commit 13868b8

File tree

7 files changed

+62
-14
lines changed

7 files changed

+62
-14
lines changed

bash_completion

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,9 +1273,15 @@ _comp_quote_compgen()
12731273

12741274
# This function performs file and directory completion. It's better than
12751275
# simply using 'compgen -f', because it honours spaces in filenames.
1276-
# @param $1 If `-d', complete only on directories. Otherwise filter/pick only
1277-
# completions with `.$1' and the uppercase version of it as file
1278-
# extension.
1276+
# @param $1 Complete filenames matching `.$1' and the uppercase version of it.
1277+
# Ignored with `-d`.
1278+
# OPTIONS
1279+
# -d Complete only on directories
1280+
# -f Perform `compopt -o filenames` modifications manually. This
1281+
# suffixes a slash to a directory name. This can be combined with
1282+
# the `-C dir` option to `_comp_compgen`, where the generated
1283+
# filenames do not exist in the current working directory and Bash
1284+
# fails to properly detect the filenames.
12791285
# @return 0 if at least one completion is generated, or 1 otherwise.
12801286
#
12811287
# @since 2.12
@@ -1284,9 +1290,22 @@ _comp_compgen_filedir()
12841290
_comp_compgen_tilde && return
12851291

12861292
local -a toks
1293+
local _dir="" _filenames=""
1294+
local OPTIND=1 OPTARG="" OPTERR=0 _opt
1295+
while getopts ":df" _opt "$@"; do
1296+
case $_opt in
1297+
d) _dir=set ;;
1298+
f) _filenames=set ;;
1299+
*)
1300+
printf "bash_completion: %s: usage error\n" "$FUNCNAME" >&2
1301+
return 2
1302+
;;
1303+
esac
1304+
done
1305+
shift "$((OPTIND - 1))"
12871306
local _arg=${1-}
12881307

1289-
if [[ $_arg == -d ]]; then
1308+
if [[ $_dir ]]; then
12901309
_comp_compgen -v toks -- -d
12911310
else
12921311
local REPLY
@@ -1330,8 +1349,17 @@ _comp_compgen_filedir()
13301349
fi
13311350

13321351
if ((${#toks[@]} != 0)); then
1333-
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
1334-
# unit test
1352+
# compopt 2>/dev/null for direct invocation, e.g. in
1353+
# _comp_compgen_filedir unit test
1354+
if [[ $_filenames ]]; then
1355+
local i
1356+
for i in "${!toks[@]}"; do
1357+
if [[ -d ${toks[i]} ]]; then
1358+
toks[i]+=/
1359+
compopt -o nospace 2>/dev/null
1360+
fi
1361+
done
1362+
fi
13351363
compopt -o filenames 2>/dev/null
13361364
fi
13371365

completions/_mount.linux

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/" -- -f
35+
_comp_compgen -C "/dev/disk/by-label/" filedir -f
3636
return
3737
;;
3838
-U)
39-
_comp_compgen -C "/dev/disk/by-uuid/" -- -f
39+
_comp_compgen -C "/dev/disk/by-uuid/" filedir -f
4040
return
4141
;;
4242
-O | --test-opts)

completions/_slackpkg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ _comp_cmd_slackpkg()
6565
;;
6666
install-template | remove-template)
6767
if [[ -e $confdir/templates ]]; then
68-
_comp_compgen -C "$confdir/templates" -- -f -X \
69-
"!?*.template" && COMPREPLY=("${COMPREPLY[@]%.template}")
68+
_comp_compgen -C "$confdir/templates" filedir -f template &&
69+
COMPREPLY=("${COMPREPLY[@]%.template}")
7070
fi
7171
return
7272
;;
7373
remove)
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 -- -f
77+
_comp_compgen -aC /var/log/packages filedir -f
7878
return
7979
;;
8080
install | reinstall | upgrade | blacklist | download)

completions/removepkg

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" -- -f
18+
_comp_compgen -C "$root/var/log/packages" filedir -f
1919
} &&
2020
complete -F _comp_cmd_removepkg removepkg
2121

completions/sbopkg

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" -- -f -X "!*.sqf"
67+
_comp_compgen -aC "$QUEUEDIR" filedir -f sqf
6868
fi
6969
} &&
7070
complete -F _comp_cmd_sbopkg sbopkg

completions/slapt-get

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 -- -f
77+
_comp_compgen -C /var/log/packages filedir -f
7878
return
7979
;;
8080
set) # --install-set

test/t/unit/test_unit_compgen_filedir.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ def functions(self, request, bash):
2424
"_g() { local cur;_comp_get_words cur; unset -v COMPREPLY; _comp_compgen_filedir e1; }; "
2525
"complete -F _g g",
2626
)
27+
assert_bash_exec(
28+
bash,
29+
"_fc() { local cur=$(_get_cword); unset -v COMPREPLY; _comp_compgen -C _filedir filedir; }; "
30+
"complete -F _fc fc; "
31+
"complete -F _fc -o filenames fc2",
32+
)
2733
assert_bash_exec(
2834
bash,
2935
"_fd() { local cur;_comp_get_words cur; unset -v COMPREPLY; _comp_compgen_filedir -d; };"
3036
"complete -F _fd fd",
3137
)
38+
assert_bash_exec(
39+
bash,
40+
"_fcd() { local cur=$(_get_cword); unset -v COMPREPLY; _comp_compgen -C _filedir filedir -df; };"
41+
"complete -F _fcd fcd",
42+
)
3243

3344
@pytest.fixture(scope="class")
3445
def non_windows_testdir(self, request, bash):
@@ -68,6 +79,11 @@ def test_2(self, bash, functions, funcname):
6879
completion = assert_complete(bash, "%s ab/" % funcname, cwd="_filedir")
6980
assert completion == "e"
7081

82+
@pytest.mark.parametrize("funcname", "fc fc2".split())
83+
def test_2C(self, bash, functions, funcname):
84+
completion = assert_complete(bash, "%s _filedir ab/" % funcname)
85+
assert completion == "e"
86+
7187
@pytest.mark.parametrize("funcname", "f f2".split())
7288
def test_3(self, bash, functions, funcname):
7389
completion = assert_complete(
@@ -156,6 +172,10 @@ def test_14(self, bash, functions, funcname):
156172
def test_15(self, functions, completion):
157173
assert completion == "b/"
158174

175+
@pytest.mark.complete(r"fcd a\ ")
176+
def test_15d(self, functions, completion):
177+
assert completion == "b/"
178+
159179
@pytest.mark.complete("g ", cwd="_filedir/ext")
160180
def test_16(self, functions, completion):
161181
assert completion == sorted("ee.e1 foo/ gg.e1 ii.E1".split())

0 commit comments

Comments
 (0)