zsh-completion template refactoring:
- removed redundant function - improved other functions :) - better names for other functions
This commit is contained in:
parent
2662787697
commit
e8018e8612
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -12,11 +11,9 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
funcMap = template.FuncMap{
|
funcMap = template.FuncMap{
|
||||||
"constructPath": constructPath,
|
"genZshFuncName": generateZshCompletionFuncName,
|
||||||
"subCmdList": subCmdList,
|
"extractFlags": extractFlags,
|
||||||
"extractFlags": extractFlags,
|
"simpleFlag": simpleFlag,
|
||||||
"cmdName": cmdName,
|
|
||||||
"simpleFlag": simpleFlag,
|
|
||||||
}
|
}
|
||||||
zshCompletionText = `
|
zshCompletionText = `
|
||||||
{{/* for pflag.Flag (specifically annotations) */}}
|
{{/* for pflag.Flag (specifically annotations) */}}
|
||||||
@ -36,7 +33,8 @@ var (
|
|||||||
|
|
||||||
{{/* should accept Command (that contains subcommands) as parameter */}}
|
{{/* should accept Command (that contains subcommands) as parameter */}}
|
||||||
{{define "argumentsC" -}}
|
{{define "argumentsC" -}}
|
||||||
function {{constructPath .}} {
|
{{ $cmdPath := genZshFuncName .}}
|
||||||
|
function {{$cmdPath}} {
|
||||||
local -a commands
|
local -a commands
|
||||||
|
|
||||||
_arguments -C \{{- range extractFlags .}}
|
_arguments -C \{{- range extractFlags .}}
|
||||||
@ -47,15 +45,15 @@ function {{constructPath .}} {
|
|||||||
case $state in
|
case $state in
|
||||||
cmnds)
|
cmnds)
|
||||||
commands=({{range .Commands}}{{if not .Hidden}}
|
commands=({{range .Commands}}{{if not .Hidden}}
|
||||||
"{{cmdName .}}:{{.Short}}"{{end}}{{end}}
|
"{{.Name}}:{{.Short}}"{{end}}{{end}}
|
||||||
)
|
)
|
||||||
_describe "command" commands
|
_describe "command" commands
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
case "$words[1]" in {{- range .Commands}}{{if not .Hidden}}
|
case "$words[1]" in {{- range .Commands}}{{if not .Hidden}}
|
||||||
{{cmdName .}})
|
{{.Name}})
|
||||||
{{constructPath .}}
|
{{$cmdPath}}_{{.Name}}
|
||||||
;;{{end}}{{end}}
|
;;{{end}}{{end}}
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
@ -66,7 +64,7 @@ function {{constructPath .}} {
|
|||||||
|
|
||||||
{{/* should accept Command without subcommands as parameter */}}
|
{{/* should accept Command without subcommands as parameter */}}
|
||||||
{{define "arguments" -}}
|
{{define "arguments" -}}
|
||||||
function {{constructPath .}} {
|
function {{genZshFuncName .}} {
|
||||||
{{" _arguments"}}{{range extractFlags .}} \
|
{{" _arguments"}}{{range extractFlags .}} \
|
||||||
{{if simpleFlag .}}{{template "simpleFlag" .}}{{else}}{{template "complexFlag" .}}{{end -}}
|
{{if simpleFlag .}}{{template "simpleFlag" .}}{{else}}{{template "complexFlag" .}}{{end -}}
|
||||||
{{end}}
|
{{end}}
|
||||||
@ -82,7 +80,7 @@ function {{constructPath .}} {
|
|||||||
|
|
||||||
{{/* template entry point */}}
|
{{/* template entry point */}}
|
||||||
{{define "Main" -}}
|
{{define "Main" -}}
|
||||||
#compdef _{{cmdName .}} {{cmdName .}}
|
#compdef _{{.Name}} {{.Name}}
|
||||||
|
|
||||||
{{template "selectCmdTemplate" .}}
|
{{template "selectCmdTemplate" .}}
|
||||||
{{end}}
|
{{end}}
|
||||||
@ -109,39 +107,11 @@ func (c *Command) GenZshCompletion(w io.Writer) error {
|
|||||||
return tmpl.Execute(w, c)
|
return tmpl.Execute(w, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func constructPath(c *Command) string {
|
func generateZshCompletionFuncName(c *Command) string {
|
||||||
var path []string
|
if c.HasParent() {
|
||||||
tmpCmd := c
|
return generateZshCompletionFuncName(c.Parent()) + "_" + c.Name()
|
||||||
path = append(path, tmpCmd.Name())
|
|
||||||
|
|
||||||
for {
|
|
||||||
if !tmpCmd.HasParent() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
tmpCmd = tmpCmd.Parent()
|
|
||||||
path = append(path, tmpCmd.Name())
|
|
||||||
}
|
}
|
||||||
|
return "_" + c.Name()
|
||||||
// reverse path
|
|
||||||
for left, right := 0, len(path)-1; left < right; left, right = left+1, right-1 {
|
|
||||||
path[left], path[right] = path[right], path[left]
|
|
||||||
}
|
|
||||||
|
|
||||||
return "_" + strings.Join(path, "_")
|
|
||||||
}
|
|
||||||
|
|
||||||
// subCmdList returns a space separated list of subcommands names
|
|
||||||
func subCmdList(c *Command) string {
|
|
||||||
var subCmds []string
|
|
||||||
|
|
||||||
for _, cmd := range c.Commands() {
|
|
||||||
if cmd.Hidden {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
subCmds = append(subCmds, cmd.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(subCmds, " ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractFlags(c *Command) []*pflag.Flag {
|
func extractFlags(c *Command) []*pflag.Flag {
|
||||||
@ -159,11 +129,6 @@ func extractFlags(c *Command) []*pflag.Flag {
|
|||||||
return flags
|
return flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmdName returns the command's innvocation
|
|
||||||
func cmdName(c *Command) string {
|
|
||||||
return c.Name()
|
|
||||||
}
|
|
||||||
|
|
||||||
func simpleFlag(p *pflag.Flag) bool {
|
func simpleFlag(p *pflag.Flag) bool {
|
||||||
return p.Name == "" || p.Shorthand == ""
|
return p.Name == "" || p.Shorthand == ""
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user