Skip to content

Commit 6428d05

Browse files
committed
refactor: use official urfave/cli completion scripts via embed
1 parent 9966ea5 commit 6428d05

3 files changed

Lines changed: 69 additions & 44 deletions

File tree

cmd/autocomplete/bash_autocomplete

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /bin/bash
2+
3+
: ${PROG:=$(basename ${BASH_SOURCE})}
4+
5+
# Macs have bash3 for which the bash-completion package doesn't include
6+
# _init_completion. This is a minimal version of that function.
7+
_cli_init_completion() {
8+
COMPREPLY=()
9+
_get_comp_words_by_ref "$@" cur prev words cword
10+
}
11+
12+
_cli_bash_autocomplete() {
13+
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
14+
local cur opts base words
15+
COMPREPLY=()
16+
cur="${COMP_WORDS[COMP_CWORD]}"
17+
if declare -F _init_completion >/dev/null 2>&1; then
18+
_init_completion -n "=:" || return
19+
else
20+
_cli_init_completion -n "=:" || return
21+
fi
22+
words=("${words[@]:0:$cword}")
23+
if [[ "$cur" == "-"* ]]; then
24+
requestComp="${words[*]} ${cur} --generate-bash-completion"
25+
else
26+
requestComp="${words[*]} --generate-bash-completion"
27+
fi
28+
opts=$(eval "${requestComp}" 2>/dev/null)
29+
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
30+
return 0
31+
fi
32+
}
33+
34+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
35+
unset PROG

cmd/autocomplete/zsh_autocomplete

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#compdef $PROG
2+
3+
_cli_zsh_autocomplete() {
4+
local -a opts
5+
local cur
6+
cur=${words[-1]}
7+
if [[ "$cur" == "-"* ]]; then
8+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
9+
else
10+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
11+
fi
12+
13+
if [[ "${opts[1]}" != "" ]]; then
14+
_describe 'values' opts
15+
else
16+
_files
17+
fi
18+
}
19+
20+
compdef _cli_zsh_autocomplete $PROG

cmd/completion.go

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package cmd
22

33
import (
4+
_ "embed"
45
"fmt"
6+
"strings"
57

68
"github.com/urfave/cli/v2"
79
)
810

11+
//go:embed autocomplete/bash_autocomplete
12+
var bashScriptTemplate string
13+
14+
//go:embed autocomplete/zsh_autocomplete
15+
var zshScriptTemplate string
16+
917
// CompletionCommand returns the completion command
1018
func CompletionCommand() *cli.Command {
1119
return &cli.Command{
@@ -16,60 +24,22 @@ func CompletionCommand() *cli.Command {
1624
Name: "bash",
1725
Usage: "Generate bash completion script",
1826
Action: func(c *cli.Context) error {
19-
fmt.Println(bashCompletionScript)
27+
// Replace $PROG placeholder with actual program name
28+
script := strings.ReplaceAll(bashScriptTemplate, "$PROG", "musing")
29+
fmt.Print(script)
2030
return nil
2131
},
2232
},
2333
{
2434
Name: "zsh",
2535
Usage: "Generate zsh completion script",
2636
Action: func(c *cli.Context) error {
27-
fmt.Println(zshCompletionScript)
37+
// Replace $PROG placeholder with actual program name
38+
script := strings.ReplaceAll(zshScriptTemplate, "$PROG", "musing")
39+
fmt.Print(script)
2840
return nil
2941
},
3042
},
3143
},
3244
}
3345
}
34-
35-
const bashCompletionScript = `#! /bin/bash
36-
37-
_musing_bash_autocomplete() {
38-
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
39-
local cur opts base
40-
COMPREPLY=()
41-
cur="${COMP_WORDS[COMP_CWORD]}"
42-
if [[ "$cur" == "-"* ]]; then
43-
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
44-
else
45-
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
46-
fi
47-
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
48-
return 0
49-
fi
50-
}
51-
52-
complete -o bashdefault -o default -o nospace -F _musing_bash_autocomplete musing
53-
`
54-
55-
const zshCompletionScript = `#compdef musing
56-
57-
_musing_zsh_autocomplete() {
58-
local -a opts
59-
local cur
60-
cur=${words[-1]}
61-
if [[ "$cur" == "-"* ]]; then
62-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
63-
else
64-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
65-
fi
66-
67-
if [[ "${opts[1]}" != "" ]]; then
68-
_describe 'values' opts
69-
else
70-
_files
71-
fi
72-
}
73-
74-
compdef _musing_zsh_autocomplete musing
75-
`

0 commit comments

Comments
 (0)