Change way to calculate offset
This commit is contained in:
parent
b539ee7dc6
commit
d89bc4a6e2
22
help.go
22
help.go
@ -296,11 +296,14 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
|
|||||||
|
|
||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"join": strings.Join,
|
"join": strings.Join,
|
||||||
|
"subtract": subtract,
|
||||||
|
"add": add,
|
||||||
"indent": indent,
|
"indent": indent,
|
||||||
"nindent": nindent,
|
"nindent": nindent,
|
||||||
"trim": strings.TrimSpace,
|
"trim": strings.TrimSpace,
|
||||||
"wrap": func(input string, offset int) string { return wrap(input, offset, maxLineLength) },
|
"wrap": func(input string, offset int) string { return wrap(input, offset, maxLineLength) },
|
||||||
"offset": offset,
|
"offset": offset,
|
||||||
|
"offsetCommands": offsetCommands,
|
||||||
}
|
}
|
||||||
|
|
||||||
if customFuncs["wrapAt"] != nil {
|
if customFuncs["wrapAt"] != nil {
|
||||||
@ -416,6 +419,14 @@ func checkCommandCompletions(c *Context, name string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func subtract(a, b int) int {
|
||||||
|
return a - b
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(a, b int) int {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
func indent(spaces int, v string) string {
|
func indent(spaces int, v string) string {
|
||||||
pad := strings.Repeat(" ", spaces)
|
pad := strings.Repeat(" ", spaces)
|
||||||
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
||||||
@ -476,3 +487,14 @@ func wrapLine(input string, offset int, wrapAt int, padding string) string {
|
|||||||
func offset(input string, fixed int) int {
|
func offset(input string, fixed int) int {
|
||||||
return len(input) + fixed
|
return len(input) + fixed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func offsetCommands(cmds []*Command, fixed int) int {
|
||||||
|
var max int = 0
|
||||||
|
for _, cmd := range cmds {
|
||||||
|
s := strings.Join(cmd.Names(), ", ")
|
||||||
|
if len(s) > max {
|
||||||
|
max = len(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max + fixed
|
||||||
|
}
|
||||||
|
@ -21,8 +21,8 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
|
|||||||
|
|
||||||
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
{{.Name}}:{{range .VisibleCommands}}
|
{{.Name}}:{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{ $cv := offsetCommands .VisibleCommands 5}}{{range .VisibleCommands}}
|
||||||
{{$s := join .Names ", "}}{{$v := offset $s 5}}{{$s}}{{"\t"}}{{wrap .Usage $v}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlagCategories}}
|
{{$s := join .Names ", "}}{{$s}}{{ $sp := subtract $cv (add (len $s) 3) }}{{ indent $sp ""}}{{wrap .Usage $cv}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlagCategories}}
|
||||||
|
|
||||||
GLOBAL OPTIONS:{{range .VisibleFlagCategories}}
|
GLOBAL OPTIONS:{{range .VisibleFlagCategories}}
|
||||||
{{if .Name}}{{.Name}}
|
{{if .Name}}{{.Name}}
|
||||||
@ -76,8 +76,8 @@ DESCRIPTION:
|
|||||||
|
|
||||||
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
{{.Name}}:{{range .VisibleCommands}}
|
{{.Name}}:{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{ $cv := offsetCommands .VisibleCommands 5}}{{range .VisibleCommands}}
|
||||||
{{$s := join .Names ", "}}{{$v := offset $s 5}}{{$s}}{{"\t"}}{{wrap .Usage $v}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
{{$s := join .Names ", "}}{{$s}}{{ $sp := subtract $cv (add (len $s) 3) }}{{ indent $sp ""}}{{wrap .Usage $cv}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
{{range .VisibleFlags}}{{.}}
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
Loading…
Reference in New Issue
Block a user