Fix spacing in bash implementation
This commit is contained in:
parent
2c26eda5e2
commit
71cddc5b20
|
@ -14,40 +14,43 @@ func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add --option|--other-option...) to return nothing for options that require a value
|
||||||
var bashTpl = template.Must(template.New("bash").Funcs(tplFuncs).Parse(`
|
var bashTpl = template.Must(template.New("bash").Funcs(tplFuncs).Parse(`
|
||||||
{{- $rootCmd := .RootCmd -}}
|
{{- $rootCmd := .RootCmd -}}
|
||||||
{{- $progName := $rootCmd.Name -}}
|
{{- $progName := $rootCmd.Name -}}
|
||||||
{{- $varName := under $rootCmd.Name -}}
|
{{- $varName := under $rootCmd.Name -}}
|
||||||
|
|
||||||
{{- define "cmd" }}
|
{{- define "cmd" }}
|
||||||
{{ $currIdx := .Index }}
|
{{ $currIdx := .Index -}}
|
||||||
{{ $nextIdx := inc .Index }}
|
{{- $nextIdx := inc .Index -}}
|
||||||
"{{ .Cmd.Name }}")
|
{{- $indentSize := mult $currIdx 2 -}}
|
||||||
case ${COMP_WORDS[{{ $currIdx }}]} in
|
{{ indent $indentSize }}"{{ .Cmd.Name }}")
|
||||||
{{ range .Cmd.Subcommands -}}
|
{{ indent $indentSize }} case ${COMP_WORDS[{{ $currIdx }}]} in
|
||||||
{{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}}
|
{{ indent $indentSize }} {{ range .Cmd.Subcommands -}}
|
||||||
{{ end }}
|
{{ indent $indentSize }} {{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}}
|
||||||
*)
|
{{ indent $indentSize }} {{ end }}
|
||||||
COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr))
|
{{ indent $indentSize }} *)
|
||||||
esac
|
{{ indent $indentSize }} COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr))
|
||||||
;;
|
{{ indent $indentSize }} ;;
|
||||||
{{ end -}}
|
{{ indent $indentSize }} esac
|
||||||
|
{{ indent $indentSize }};; # {{ .Cmd.Name }}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
_{{$varName}}_completions()
|
_{{$varName}}_completions()
|
||||||
{
|
{
|
||||||
local curr prev
|
COMPREPLY=()
|
||||||
COMPREPLY=()
|
|
||||||
curr=${COMP_WORDS[COMP_CWORD]}
|
|
||||||
prev=${COMP_WORDS[COMP_CWORD-1]}
|
|
||||||
|
|
||||||
case ${COMP_WORDS[1]} in
|
case ${COMP_WORDS[1]} in
|
||||||
{{ range .RootCmd.Subcommands -}}
|
{{- range .RootCmd.Subcommands -}}
|
||||||
{{ template "cmd" (map "Cmd" . "Index" 2) -}}
|
{{ template "cmd" (map "Cmd" . "Index" 2) -}}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
*)
|
||||||
|
COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr))
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
*)
|
# Global Options
|
||||||
COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr))
|
+=($(compgen -W "{{ join .GlobalOpts.Names " " }}" -- $curr))
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
complete -F _{{$varName}}_completions {{$progName}}
|
complete -F _{{$varName}}_completions {{$progName}}
|
||||||
|
|
34
tpl_funcs.go
34
tpl_funcs.go
|
@ -12,7 +12,13 @@ var tplFuncs = template.FuncMap{
|
||||||
"join": tplJoin,
|
"join": tplJoin,
|
||||||
"under": tplUnder,
|
"under": tplUnder,
|
||||||
"varPrefix": tplVarPrefix,
|
"varPrefix": tplVarPrefix,
|
||||||
|
"repeat": tplRepeat,
|
||||||
|
"indent": tplIndent,
|
||||||
|
"add": tplAdd,
|
||||||
"inc": tplInc,
|
"inc": tplInc,
|
||||||
|
"sub": tplSub,
|
||||||
|
"dec": tplDec,
|
||||||
|
"mult": tplMult,
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplMap(vals ...any) (map[string]any, error) {
|
func tplMap(vals ...any) (map[string]any, error) {
|
||||||
|
@ -47,6 +53,30 @@ func tplVarPrefix(s string) string {
|
||||||
return tplUnder(s) + "_"
|
return tplUnder(s) + "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplInc(i int) int {
|
func tplRepeat(s string, n int) string {
|
||||||
return i + 1
|
return strings.Repeat(s, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplIndent(n int) string {
|
||||||
|
return tplRepeat(" ", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplAdd(a, b int) int {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplInc(i int) int {
|
||||||
|
return tplAdd(i, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplSub(a, b int) int {
|
||||||
|
return a - b
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplDec(i int) int {
|
||||||
|
return tplSub(i, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplMult(a, b int) int {
|
||||||
|
return a * b
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue