Skip to content

feat(_comp_looks_like_path): new utility, use it #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ _comp_expand_glob()
return 0
}

# Check if the argument looks like a path.
# @param $1 thing to check
# @return True (0) if it does, False (> 0) otherwise
_comp_looks_like_path()
{
[[ ${1-} == @(*/|[.~])* ]]
}

# Reassemble command line words, excluding specified characters from the
# list of word completion separators (COMP_WORDBREAKS).
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-get
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _apt_get()
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$cur"))
;;
install | reinstall)
if [[ $cur == */* ]]; then
if _comp_looks_like_path "$cur"; then
_filedir deb
return
elif [[ $cur == *=* ]]; then
Expand All @@ -58,7 +58,7 @@ _apt_get()
;;&
build-dep)
_filedir -d
[[ $cur != */* ]] || return
_comp_looks_like_path "$cur" && return
;;&
*)
COMPREPLY+=($(_comp_xfunc apt-cache packages))
Expand Down
2 changes: 1 addition & 1 deletion completions/gdb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _gdb()
if ((cword == 1)); then
local IFS
compopt -o filenames
if [[ $cur == */* ]]; then
if _comp_looks_like_path "$cur"; then
# compgen -c works as expected if $cur contains any slashes.
IFS=$'\n'
COMPREPLY=($(PATH="$PATH:." compgen -d -c -- "$cur"))
Expand Down
2 changes: 1 addition & 1 deletion completions/kldload
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _kldload()
local cur prev words cword
_init_completion || return

if [[ "$cur" == */* ]]; then
if _comp_looks_like_path "$cur"; then
_filedir ko
return
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/man
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _man()
fi

# file based completion if parameter looks like a path
if [[ $cur == @(*/|[.~])* ]]; then
if _comp_looks_like_path "$cur"; then
_filedir "$manext"
return
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/pydoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _pydoc()

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

if [[ $cur != @(.|*/)* ]]; then
if ! _comp_looks_like_path "$cur"; then
local python=python
[[ ${1##*/} == *3* ]] && python=python3
_comp_xfunc python modules $python
Expand Down
2 changes: 1 addition & 1 deletion completions/pylint
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ _pylint()
return
fi

[[ $cur == @(.|*/)* ]] || _comp_xfunc python modules $python
_comp_looks_like_path "$cur" || _comp_xfunc python modules $python
_filedir py
} &&
complete -F _pylint pylint pylint-2 pylint-3
Expand Down
2 changes: 1 addition & 1 deletion completions/removepkg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _removepkg()
return
fi

if [[ $cur == */* ]]; then
if _comp_looks_like_path "$cur"; then
_filedir
return
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/rpm
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ _rpm()
;;
--whatenhances | --whatprovides | --whatrecommends | --whatrequires | \
--whatsuggests | --whatsupplements)
if [[ $cur == */* ]]; then
if _comp_looks_like_path "$cur"; then
_filedir
else
# complete on capabilities
Expand Down
9 changes: 4 additions & 5 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,11 @@ _scp()
COMPREPLY=("${COMPREPLY[@]/%/ }")
return
;;
*/* | [.~]*)
# not a known host, pass through
;;
*)
_known_hosts_real ${ipvx-} -c -a \
${configfile:+-F "$configfile"} -- "$cur"
if ! _comp_looks_like_path "$cur"; then
_known_hosts_real ${ipvx-} -c -a \
${configfile:+-F "$configfile"} -- "$cur"
fi
;;
esac
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/vpnc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _vpnc()

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1" --long-help)' -- "$cur"))
elif [[ $cur == */* ]]; then
elif _comp_looks_like_path "$cur"; then
# explicit filename
_filedir conf
else
Expand Down
31 changes: 31 additions & 0 deletions test/t/unit/test_unit_looks_like_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from conftest import TestUnitBase, assert_bash_exec


@pytest.mark.bashcomp(cmd=None)
class TestUnitQuote(TestUnitBase):
@pytest.mark.parametrize(
"thing_looks_like",
(
("", False),
("foo", False),
("/foo", True),
("foo/", True),
("foo/bar", True),
(".", True),
("../", True),
("~", True),
("~foo", True),
),
)
def test_1(self, bash, thing_looks_like):
thing, looks_like = thing_looks_like
output = assert_bash_exec(
bash,
f"_comp_looks_like_path '{thing}'; printf %s $?",
want_output=True,
want_newline=False,
)
is_zero = output.strip() == "0"
assert (looks_like and is_zero) or (not looks_like and not is_zero)