11package cmd
22
33import (
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
1018func 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