feat(cli): colorize help page (#9589)

This commit is contained in:
Ammar Bandukwala
2023-09-08 18:21:33 +00:00
committed by GitHub
parent 11404af9ca
commit e361f1107b
86 changed files with 1224 additions and 920 deletions
+24 -12
View File
@@ -17,8 +17,10 @@ import (
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/pretty"
)
//go:embed help.tpl
@@ -47,12 +49,28 @@ func wrapTTY(s string) string {
var usageTemplate = template.Must(
template.New("usage").Funcs(
template.FuncMap{
"version": func() string {
return buildinfo.Version()
},
"wrapTTY": func(s string) string {
return wrapTTY(s)
},
"trimNewline": func(s string) string {
return strings.TrimSuffix(s, "\n")
},
"keyword": func(s string) string {
return pretty.Sprint(
pretty.FgColor(cliui.Color("#0173ff")),
s,
)
},
"prettyHeader": func(s string) string {
return pretty.Sprint(
pretty.FgColor(
cliui.Color("#ffb500"),
), strings.ToUpper(s), ":",
)
},
"typeHelper": func(opt *clibase.Option) string {
switch v := opt.Value.(type) {
case *clibase.Enum:
@@ -71,13 +89,15 @@ var usageTemplate = template.Must(
body = wordwrap.WrapString(body, uint(twidth-len(spacing)))
sc := bufio.NewScanner(strings.NewReader(body))
var sb strings.Builder
for _, line := range strings.Split(body, "\n") {
for sc.Scan() {
// Remove existing indent, if any.
line = strings.TrimSpace(line)
// line = strings.TrimSpace(line)
// Use spaces so we can easily calculate wrapping.
_, _ = sb.WriteString(spacing)
_, _ = sb.WriteString(line)
_, _ = sb.Write(sc.Bytes())
_, _ = sb.WriteString("\n")
}
return sb.String()
@@ -126,9 +146,7 @@ var usageTemplate = template.Must(
"flagName": func(opt clibase.Option) string {
return opt.Flag
},
"prettyHeader": func(s string) string {
return cliui.Bold(s)
},
"isEnterprise": func(opt clibase.Option) bool {
return opt.Annotations.IsSet("enterprise")
},
@@ -160,12 +178,6 @@ var usageTemplate = template.Must(
}
return sb.String()
},
"formatLong": func(long string) string {
// We intentionally don't wrap here because it would misformat
// examples, where the new line would start without the prior
// line's indentation.
return strings.TrimSpace(long)
},
"formatGroupDescription": func(s string) string {
s = strings.ReplaceAll(s, "\n", "")
s = s + "\n"