Small improvements to bash completions

This commit is contained in:
Paul Ouellette
2019-06-11 17:22:27 -04:00
parent ab7717898c
commit bcce0fcdcf
2 changed files with 35 additions and 55 deletions

View File

@@ -13,8 +13,7 @@ _foo() {
cmd="foo" cmd="foo"
opts="-h --help" opts="-h --help"
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }
@@ -41,8 +40,7 @@ _foo() {
;; ;;
esac esac
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }
@@ -70,8 +68,7 @@ _foo() {
;; ;;
esac esac
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }
@@ -92,8 +89,7 @@ _foo() {
cmd="foo" cmd="foo"
opts="" opts=""
for arg in ${COMP_WORDS[@]:1} for arg in ${COMP_WORDS[@]:1}; do
do
case "$arg" in case "$arg" in
install) install)
cmd="install" cmd="install"
@@ -106,11 +102,9 @@ _foo() {
foo) foo)
COMPREPLY=($(compgen -W "install" -- "$cur")) COMPREPLY=($(compgen -W "install" -- "$cur"))
;; ;;
esac esac
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }
@@ -132,8 +126,7 @@ _foo() {
cmd="foo" cmd="foo"
opts="" opts=""
for arg in ${COMP_WORDS[@]:1} for arg in ${COMP_WORDS[@]:1}; do
do
case "$arg" in case "$arg" in
install) install)
cmd="install" cmd="install"
@@ -151,8 +144,7 @@ _foo() {
;; ;;
esac esac
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }
@@ -174,12 +166,8 @@ _foo() {
cmd="foo" cmd="foo"
opts="" opts=""
for arg in ${COMP_WORDS[@]:1} for arg in ${COMP_WORDS[@]:1}; do
do
case "$arg" in case "$arg" in
help)
break
;;
install) install)
cmd="install" cmd="install"
break break
@@ -191,11 +179,9 @@ _foo() {
foo) foo)
COMPREPLY=($(compgen -W "help install" -- "$cur")) COMPREPLY=($(compgen -W "help install" -- "$cur"))
;; ;;
esac esac
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi fi
} }

View File

@@ -1144,7 +1144,7 @@ function Parser:_bash_option_args(buf, indent)
compreply = ('COMPREPLY=($(compgen -W "%s" -- "$cur"))') compreply = ('COMPREPLY=($(compgen -W "%s" -- "$cur"))')
:format(table.concat(option._choices, " ")) :format(table.concat(option._choices, " "))
else else
compreply = ('COMPREPLY=($(compgen -f "$cur"))') compreply = 'COMPREPLY=($(compgen -f "$cur"))'
end end
table.insert(opts, (" "):rep(indent + 4) .. pattern) table.insert(opts, (" "):rep(indent + 4) .. pattern)
table.insert(opts, (" "):rep(indent + 8) .. compreply) table.insert(opts, (" "):rep(indent + 8) .. compreply)
@@ -1163,23 +1163,21 @@ end
function Parser:_bash_get_cmd(buf) function Parser:_bash_get_cmd(buf)
local cmds = {} local cmds = {}
for idx, command in ipairs(self._commands) do for idx, command in ipairs(self._commands) do
table.insert(cmds, (" "):rep(12) .. ("%s)"):format(table.concat(command._aliases, "|")))
if idx ~= self._help_command_idx then if idx ~= self._help_command_idx then
table.insert(cmds, (" "):rep(12) .. ("%s)"):format(table.concat(command._aliases, "|")))
table.insert(cmds, (" "):rep(16) .. ('cmd="%s"'):format(command._aliases[1])) table.insert(cmds, (" "):rep(16) .. ('cmd="%s"'):format(command._aliases[1]))
end
table.insert(cmds, (" "):rep(16) .. "break") table.insert(cmds, (" "):rep(16) .. "break")
table.insert(cmds, (" "):rep(16) .. ";;") table.insert(cmds, (" "):rep(16) .. ";;")
end end
end
local get_cmd = ([[ if #cmds > 0 then
for arg in ${COMP_WORDS[@]:1} table.insert(buf, (" "):rep(4) .. "for arg in ${COMP_WORDS[@]:1}; do")
do table.insert(buf, (" "):rep(8) .. 'case "$arg" in')
case "$arg" in table.insert(buf, table.concat(cmds, "\n"))
%s table.insert(buf, (" "):rep(8) .. "esac")
esac table.insert(buf, (" "):rep(4) .. "done\n")
done end
]]):format(table.concat(cmds, "\n"))
table.insert(buf, get_cmd)
end end
function Parser:_bash_cmd_completions(buf) function Parser:_bash_cmd_completions(buf)
@@ -1193,15 +1191,14 @@ function Parser:_bash_cmd_completions(buf)
end end
end end
local cmd_completions = ([[ table.insert(buf, (" "):rep(4) .. 'case "$cmd" in')
case "$cmd" in table.insert(buf, (" "):rep(8) .. self._name .. ")")
%s) table.insert(buf, (" "):rep(12) .. ('COMPREPLY=($(compgen -W "%s" -- "$cur"))'):format(get_commands(self)))
COMPREPLY=($(compgen -W "%s" -- "$cur")) table.insert(buf, (" "):rep(12) .. ";;")
;; if #subcmds > 0 then
%s table.insert(buf, table.concat(subcmds, "\n"))
esac end
]]):format(self._name, get_commands(self), table.concat(subcmds, "\n")) table.insert(buf, (" "):rep(4) .. "esac\n")
table.insert(buf, cmd_completions)
end end
function Parser:get_bash_complete() function Parser:get_bash_complete()
@@ -1215,21 +1212,18 @@ _%s() {
]]):format(self._name, self._name, get_options(self))} ]]):format(self._name, self._name, get_options(self))}
self:_bash_option_args(buf, 4) self:_bash_option_args(buf, 4)
if #self._commands > 0 then
self:_bash_get_cmd(buf) self:_bash_get_cmd(buf)
if #self._commands > 0 then
self:_bash_cmd_completions(buf) self:_bash_cmd_completions(buf)
end end
table.insert(buf, [=[ table.insert(buf, ([=[
if [[ "$cur" = -* ]] if [[ "$cur" = -* ]]; then
then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi]=]) fi
table.insert(buf, "}\n") }
local complete = ("complete -F _%s -o bashdefault -o default %s")
:format(self._name, self._name) complete -F _%s -o bashdefault -o default %s]=]):format(self._name, self._name))
table.insert(buf, complete)
return table.concat(buf, "\n") return table.concat(buf, "\n")
end end