diff --git a/completions_bash.go b/completions_bash.go index b402e91..4cc2d75 100644 --- a/completions_bash.go +++ b/completions_bash.go @@ -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(` {{- $rootCmd := .RootCmd -}} {{- $progName := $rootCmd.Name -}} {{- $varName := under $rootCmd.Name -}} {{- define "cmd" }} - {{ $currIdx := .Index }} - {{ $nextIdx := inc .Index }} - "{{ .Cmd.Name }}") - case ${COMP_WORDS[{{ $currIdx }}]} in - {{ range .Cmd.Subcommands -}} - {{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}} - {{ end }} - *) - COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr)) - esac - ;; -{{ end -}} +{{ $currIdx := .Index -}} +{{- $nextIdx := inc .Index -}} +{{- $indentSize := mult $currIdx 2 -}} +{{ indent $indentSize }}"{{ .Cmd.Name }}") +{{ indent $indentSize }} case ${COMP_WORDS[{{ $currIdx }}]} in +{{ indent $indentSize }} {{ range .Cmd.Subcommands -}} +{{ indent $indentSize }} {{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}} +{{ indent $indentSize }} {{ end }} +{{ indent $indentSize }} *) +{{ indent $indentSize }} COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr)) +{{ indent $indentSize }} ;; +{{ indent $indentSize }} esac +{{ indent $indentSize }};; # {{ .Cmd.Name }} +{{- end -}} _{{$varName}}_completions() { - local curr prev - COMPREPLY=() - curr=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + COMPREPLY=() - case ${COMP_WORDS[1]} in - {{ range .RootCmd.Subcommands -}} - {{ template "cmd" (map "Cmd" . "Index" 2) -}} - {{ end }} + case ${COMP_WORDS[1]} in + {{- range .RootCmd.Subcommands -}} + {{ template "cmd" (map "Cmd" . "Index" 2) -}} + {{ end }} + *) + COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr)) + ;; + esac - *) - COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr)) - esac + # Global Options + +=($(compgen -W "{{ join .GlobalOpts.Names " " }}" -- $curr)) } complete -F _{{$varName}}_completions {{$progName}} diff --git a/tpl_funcs.go b/tpl_funcs.go index a41e10b..834f3a4 100644 --- a/tpl_funcs.go +++ b/tpl_funcs.go @@ -12,7 +12,13 @@ var tplFuncs = template.FuncMap{ "join": tplJoin, "under": tplUnder, "varPrefix": tplVarPrefix, + "repeat": tplRepeat, + "indent": tplIndent, + "add": tplAdd, "inc": tplInc, + "sub": tplSub, + "dec": tplDec, + "mult": tplMult, } func tplMap(vals ...any) (map[string]any, error) { @@ -47,6 +53,30 @@ func tplVarPrefix(s string) string { return tplUnder(s) + "_" } -func tplInc(i int) int { - return i + 1 +func tplRepeat(s string, n int) string { + 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 }