First try at better zsh completions:
A very basic POC. Need to refactor to generate completion structure before passing to the template to avoid repeated computations. What works: * Real zsh completion (not built on bash) * Basic flags (with long flag and optional shorthand) * Basic filename completion indication (not with file extensions though) What's missing: * File extensions to filename completions * Positional args * Do we require handling only short flags?
This commit is contained in:
committed by
Steve Francia
parent
67fc4837d2
commit
7e2436b79d
56
zsh_template.tmpl
Normal file
56
zsh_template.tmpl
Normal file
@ -0,0 +1,56 @@
|
||||
{{define "complexFlag"}}{{ /* for pflag.Flag with short and long options */ -}}
|
||||
"(-{{.Shorthand}} --{{.Name}})"\{-{{.Shorthand}}, --{{.Name}}\}[{{.Usage}}]
|
||||
{{- end}}
|
||||
|
||||
{{define "simpleFlag"}}{{ /* for pflag.Flag with either short or long options */ -}}
|
||||
"{{with .Name}}-{{.}}{{else}}--{{.Shorthand}}{{end}}[{{.Usage}}]"
|
||||
{{- end}}
|
||||
|
||||
{{define "argumentsC"}}
|
||||
{{- /* should accept Command (that contains subcommands) as parameter */ -}}
|
||||
function {{constructPath .}} {
|
||||
local line
|
||||
|
||||
_arguments -C \
|
||||
{{range extractFlags . -}}
|
||||
{{if simpleFlag .}}{{template "simpleFlag" .}}{{else}}{{template "complexFlag" .}}{{end}} \
|
||||
{{end -}}
|
||||
"1: :({{subCmdList .}})" \
|
||||
"*:arg:->args"
|
||||
|
||||
case $line[1] in
|
||||
{{range .Commands -}}
|
||||
{{.Use}})
|
||||
{{constructPath .}}
|
||||
;;
|
||||
{{end -}}
|
||||
esac
|
||||
}
|
||||
{{range .Commands -}}
|
||||
|
||||
{{template "selectCmdTemplate" .}}
|
||||
{{end -}}
|
||||
{{end}}
|
||||
|
||||
{{define "arguments"}}
|
||||
function {{constructPath .}} {
|
||||
{{- /* should accept Command without subcommands as parameter */ -}}
|
||||
{{with extractFlags . -}}
|
||||
_arguments \
|
||||
{{range .}}
|
||||
{{if simpleFlag .}}{{template "simpleFlag" .}}{{else}}{{template "complexFlag" .}}{{end}} \
|
||||
{{end -}}
|
||||
{{ /* leave this line empty because of the last backslash */ }}
|
||||
{{end -}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{define "selectCmdTemplate" -}}
|
||||
{{with .Commands}}{{template "argumentsC" .}}{{else}}{{template "arguments" .}}{{end}}
|
||||
{{end -}}
|
||||
|
||||
{{define "Main"}}
|
||||
#compdef _{{.Use}} {{.Use}}
|
||||
|
||||
{{template "selectCmdTemplate" .}}
|
||||
{{end}}
|
Reference in New Issue
Block a user