Skip to content

Commit 36ceb27

Browse files
authored
Merge pull request scop#775 from scop/feat/looks-like-path
feat(_comp_looks_like_path): new utility, use it
2 parents 66ec66e + f2b0511 commit 36ceb27

File tree

12 files changed

+53
-15
lines changed

12 files changed

+53
-15
lines changed

bash_completion

+8
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ _comp_expand_glob()
390390
return 0
391391
}
392392

393+
# Check if the argument looks like a path.
394+
# @param $1 thing to check
395+
# @return True (0) if it does, False (> 0) otherwise
396+
_comp_looks_like_path()
397+
{
398+
[[ ${1-} == @(*/|[.~])* ]]
399+
}
400+
393401
# Reassemble command line words, excluding specified characters from the
394402
# list of word completion separators (COMP_WORDBREAKS).
395403
# @param $1 chars Characters out of $COMP_WORDBREAKS which should

completions/apt-get

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ _apt_get()
3939
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$cur"))
4040
;;
4141
install | reinstall)
42-
if [[ $cur == */* ]]; then
42+
if _comp_looks_like_path "$cur"; then
4343
_filedir deb
4444
return
4545
elif [[ $cur == *=* ]]; then
@@ -58,7 +58,7 @@ _apt_get()
5858
;;&
5959
build-dep)
6060
_filedir -d
61-
[[ $cur != */* ]] || return
61+
_comp_looks_like_path "$cur" && return
6262
;;&
6363
*)
6464
COMPREPLY+=($(_comp_xfunc apt-cache packages))

completions/gdb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _gdb()
1717
if ((cword == 1)); then
1818
local IFS
1919
compopt -o filenames
20-
if [[ $cur == */* ]]; then
20+
if _comp_looks_like_path "$cur"; then
2121
# compgen -c works as expected if $cur contains any slashes.
2222
IFS=$'\n'
2323
COMPREPLY=($(PATH="$PATH:." compgen -d -c -- "$cur"))

completions/kldload

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _kldload()
77
local cur prev words cword
88
_init_completion || return
99

10-
if [[ "$cur" == */* ]]; then
10+
if _comp_looks_like_path "$cur"; then
1111
_filedir ko
1212
return
1313
fi

completions/man

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ _man()
5252
fi
5353

5454
# file based completion if parameter looks like a path
55-
if [[ $cur == @(*/|[.~])* ]]; then
55+
if _comp_looks_like_path "$cur"; then
5656
_filedir "$manext"
5757
return
5858
fi

completions/pydoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _pydoc()
2424

2525
COMPREPLY=($(compgen -W 'keywords topics modules' -- "$cur"))
2626

27-
if [[ $cur != @(.|*/)* ]]; then
27+
if ! _comp_looks_like_path "$cur"; then
2828
local python=python
2929
[[ ${1##*/} == *3* ]] && python=python3
3030
_comp_xfunc python modules $python

completions/pylint

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ _pylint()
107107
return
108108
fi
109109

110-
[[ $cur == @(.|*/)* ]] || _comp_xfunc python modules $python
110+
_comp_looks_like_path "$cur" || _comp_xfunc python modules $python
111111
_filedir py
112112
} &&
113113
complete -F _pylint pylint pylint-2 pylint-3

completions/removepkg

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _removepkg()
99
return
1010
fi
1111

12-
if [[ $cur == */* ]]; then
12+
if _comp_looks_like_path "$cur"; then
1313
_filedir
1414
return
1515
fi

completions/rpm

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ _rpm()
9292
;;
9393
--whatenhances | --whatprovides | --whatrecommends | --whatrequires | \
9494
--whatsuggests | --whatsupplements)
95-
if [[ $cur == */* ]]; then
95+
if _comp_looks_like_path "$cur"; then
9696
_filedir
9797
else
9898
# complete on capabilities

completions/ssh

+4-5
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,11 @@ _scp()
580580
COMPREPLY=("${COMPREPLY[@]/%/ }")
581581
return
582582
;;
583-
*/* | [.~]*)
584-
# not a known host, pass through
585-
;;
586583
*)
587-
_known_hosts_real ${ipvx-} -c -a \
588-
${configfile:+-F "$configfile"} -- "$cur"
584+
if ! _comp_looks_like_path "$cur"; then
585+
_known_hosts_real ${ipvx-} -c -a \
586+
${configfile:+-F "$configfile"} -- "$cur"
587+
fi
589588
;;
590589
esac
591590
fi

completions/vpnc

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ _vpnc()
6161

6262
if [[ $cur == -* ]]; then
6363
COMPREPLY=($(compgen -W '$(_parse_help "$1" --long-help)' -- "$cur"))
64-
elif [[ $cur == */* ]]; then
64+
elif _comp_looks_like_path "$cur"; then
6565
# explicit filename
6666
_filedir conf
6767
else
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from conftest import TestUnitBase, assert_bash_exec
4+
5+
6+
@pytest.mark.bashcomp(cmd=None)
7+
class TestUnitQuote(TestUnitBase):
8+
@pytest.mark.parametrize(
9+
"thing_looks_like",
10+
(
11+
("", False),
12+
("foo", False),
13+
("/foo", True),
14+
("foo/", True),
15+
("foo/bar", True),
16+
(".", True),
17+
("../", True),
18+
("~", True),
19+
("~foo", True),
20+
),
21+
)
22+
def test_1(self, bash, thing_looks_like):
23+
thing, looks_like = thing_looks_like
24+
output = assert_bash_exec(
25+
bash,
26+
f"_comp_looks_like_path '{thing}'; printf %s $?",
27+
want_output=True,
28+
want_newline=False,
29+
)
30+
is_zero = output.strip() == "0"
31+
assert (looks_like and is_zero) or (not looks_like and not is_zero)

0 commit comments

Comments
 (0)