Skip to content

Commit

Permalink
Merge pull request #669 from twpayne/upgrade-deps
Browse files Browse the repository at this point in the history
Upgrade dependencies
  • Loading branch information
twpayne committed Apr 14, 2020
2 parents 987450b + 1489cac commit c1c19fb
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 541 deletions.
2 changes: 1 addition & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Config) runCompletion(cmd *cobra.Command, args []string) error {
case "zsh":
return rootCmd.GenZshCompletion(c.Stdout)
case "fish":
return rootCmd.GenFishCompletion(c.Stdout)
return rootCmd.GenFishCompletion(c.Stdout, true)
default:
return errors.New("unsupported shell")
}
Expand Down
79 changes: 73 additions & 6 deletions completions/chezmoi-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,71 @@ __chezmoi_contains_word()
return 1
}

__chezmoi_handle_go_custom_completion()
{
__chezmoi_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"

local out requestComp lastParam lastChar comp directive args

# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly chezmoi allows to handle aliases
args=("${words[@]:1}")
requestComp="${words[0]} __completeNoDesc ${args[*]}"

lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1}
__chezmoi_debug "${FUNCNAME[0]}: lastParam ${lastParam}, lastChar ${lastChar}"

if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__chezmoi_debug "${FUNCNAME[0]}: Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi

__chezmoi_debug "${FUNCNAME[0]}: calling ${requestComp}"
# Use eval to handle any environment variables and such
out=$(eval "${requestComp}" 2>/dev/null)

# Extract the directive integer at the very end of the output following a colon (:)
directive=${out##*:}
# Remove the directive
out=${out%:*}
if [ "${directive}" = "${out}" ]; then
# There is not directive specified
directive=0
fi
__chezmoi_debug "${FUNCNAME[0]}: the completion directive is: ${directive}"
__chezmoi_debug "${FUNCNAME[0]}: the completions are: ${out[*]}"

if [ $((directive & 1)) -ne 0 ]; then
# Error code. No completion.
__chezmoi_debug "${FUNCNAME[0]}: received error from custom completion go code"
return
else
if [ $((directive & 2)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__chezmoi_debug "${FUNCNAME[0]}: activating no space"
compopt -o nospace
fi
fi
if [ $((directive & 4)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__chezmoi_debug "${FUNCNAME[0]}: activating no file completion"
compopt +o default
fi
fi

while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${out[*]}" -- "$cur")
fi
}

__chezmoi_handle_reply()
{
__chezmoi_debug "${FUNCNAME[0]}"
local comp
case $cur in
-*)
if [[ $(type -t compopt) = "builtin" ]]; then
Expand All @@ -50,8 +112,8 @@ __chezmoi_handle_reply()
else
allflags=("${flags[*]} ${two_word_flags[*]}")
fi
while IFS='' read -r c; do
COMPREPLY+=("$c")
while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${allflags[*]}" -- "$cur")
if [[ $(type -t compopt) = "builtin" ]]; then
[[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace
Expand Down Expand Up @@ -98,17 +160,21 @@ __chezmoi_handle_reply()
completions=("${commands[@]}")
if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
completions=("${must_have_one_noun[@]}")
elif [[ -n "${has_completion_function}" ]]; then
# if a go completion function is provided, defer to that function
completions=()
__chezmoi_handle_go_custom_completion
fi
if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
completions+=("${must_have_one_flag[@]}")
fi
while IFS='' read -r c; do
COMPREPLY+=("$c")
while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${completions[*]}" -- "$cur")

if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
while IFS='' read -r c; do
COMPREPLY+=("$c")
while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${noun_aliases[*]}" -- "$cur")
fi

Expand Down Expand Up @@ -2039,6 +2105,7 @@ __start_chezmoi()
local commands=("chezmoi")
local must_have_one_flag=()
local must_have_one_noun=()
local has_completion_function
local last_command
local nouns=()

Expand Down
Loading

0 comments on commit c1c19fb

Please sign in to comment.