Skip to content

Commit 46a6047

Browse files
committed
feat(cli): colorize help page
Inspired by sharkdp's tooling.
1 parent dd97fe2 commit 46a6047

File tree

86 files changed

+747
-443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+747
-443
lines changed

cli/cliui/cliui.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ var (
5151
Blue = color.Color("#5000ff")
5252
)
5353

54+
// Color returns a color for the given string.
55+
func Color(s string) termenv.Color {
56+
return color.Color(s)
57+
}
58+
5459
func isTerm() bool {
5560
return color != termenv.Ascii
5661
}

cli/help.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"golang.org/x/crypto/ssh/terminal"
1818
"golang.org/x/xerrors"
1919

20+
"github.com/coder/coder/v2/buildinfo"
2021
"github.com/coder/coder/v2/cli/clibase"
2122
"github.com/coder/coder/v2/cli/cliui"
23+
"github.com/coder/pretty"
2224
)
2325

2426
//go:embed help.tpl
@@ -47,12 +49,28 @@ func wrapTTY(s string) string {
4749
var usageTemplate = template.Must(
4850
template.New("usage").Funcs(
4951
template.FuncMap{
52+
"version": func() string {
53+
return buildinfo.Version()
54+
},
5055
"wrapTTY": func(s string) string {
5156
return wrapTTY(s)
5257
},
5358
"trimNewline": func(s string) string {
5459
return strings.TrimSuffix(s, "\n")
5560
},
61+
"keyword": func(s string) string {
62+
return pretty.Sprint(
63+
pretty.FgColor(cliui.Color("#0173ff")),
64+
s,
65+
)
66+
},
67+
"prettyHeader": func(s string) string {
68+
return pretty.Sprint(
69+
pretty.FgColor(
70+
cliui.Color("#ffb500"),
71+
), strings.ToUpper(s), ":",
72+
)
73+
},
5674
"typeHelper": func(opt *clibase.Option) string {
5775
switch v := opt.Value.(type) {
5876
case *clibase.Enum:
@@ -71,13 +89,15 @@ var usageTemplate = template.Must(
7189

7290
body = wordwrap.WrapString(body, uint(twidth-len(spacing)))
7391

92+
sc := bufio.NewScanner(strings.NewReader(body))
93+
7494
var sb strings.Builder
75-
for _, line := range strings.Split(body, "\n") {
95+
for sc.Scan() {
7696
// Remove existing indent, if any.
77-
line = strings.TrimSpace(line)
97+
// line = strings.TrimSpace(line)
7898
// Use spaces so we can easily calculate wrapping.
7999
_, _ = sb.WriteString(spacing)
80-
_, _ = sb.WriteString(line)
100+
_, _ = sb.Write(sc.Bytes())
81101
_, _ = sb.WriteString("\n")
82102
}
83103
return sb.String()
@@ -126,9 +146,7 @@ var usageTemplate = template.Must(
126146
"flagName": func(opt clibase.Option) string {
127147
return opt.Flag
128148
},
129-
"prettyHeader": func(s string) string {
130-
return cliui.Bold(s)
131-
},
149+
132150
"isEnterprise": func(opt clibase.Option) bool {
133151
return opt.Annotations.IsSet("enterprise")
134152
},
@@ -160,12 +178,6 @@ var usageTemplate = template.Must(
160178
}
161179
return sb.String()
162180
},
163-
"formatLong": func(long string) string {
164-
// We intentionally don't wrap here because it would misformat
165-
// examples, where the new line would start without the prior
166-
// line's indentation.
167-
return strings.TrimSpace(long)
168-
},
169181
"formatGroupDescription": func(s string) string {
170182
s = strings.ReplaceAll(s, "\n", "")
171183
s = s + "\n"

cli/help.tpl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
{{- /* Heavily inspired by the Go toolchain formatting. */ -}}
2-
Usage: {{.FullUsage}}
1+
{{- /* Heavily inspired by the Go toolchain and fd */ -}}
2+
coder {{version}}
3+
4+
{{prettyHeader "Usage"}}
5+
{{indent .FullUsage 2}}
36

47

58
{{ with .Short }}
6-
{{- wrapTTY . }}
9+
{{- indent . 2 | wrapTTY }}
710
{{"\n"}}
811
{{- end}}
912

1013
{{ with .Aliases }}
11-
{{ "\n" }}
12-
{{ "Aliases:"}} {{ joinStrings .}}
13-
{{ "\n" }}
14+
{{" Aliases: "}} {{- joinStrings .}}
1415
{{- end }}
1516

1617
{{- with .Long}}
17-
{{- formatLong . }}
18+
{{"\n"}}
19+
{{- indent . 2}}
1820
{{ "\n" }}
1921
{{- end }}
2022
{{ with visibleChildren . }}
@@ -34,11 +36,11 @@ Usage: {{.FullUsage}}
3436
{{- else }}
3537
{{- end }}
3638
{{- range $index, $option := $group.Options }}
37-
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n -" $option.FlagShorthand ", " -}}
39+
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n "}} {{ keyword "-"}}{{keyword $option.FlagShorthand }}{{", "}}
3840
{{- else }}{{- print "\n " -}}
3941
{{- end }}
40-
{{- with flagName $option }}--{{ . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
41-
{{- with envName $option }}, ${{ . }}{{ end }}
42+
{{- with flagName $option }}{{keyword "--"}}{{ keyword . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
43+
{{- with envName $option }}, {{ print "$" . | keyword }}{{ end }}
4244
{{- with $option.Default }} (default: {{ . }}){{ end }}
4345
{{- with $option.Description }}
4446
{{- $desc := $option.Description }}
@@ -47,7 +49,7 @@ Usage: {{.FullUsage}}
4749
{{- end -}}
4850
{{- end }}
4951
{{- end }}
50-
---
52+
———
5153
{{- if .Parent }}
5254
Run `coder --help` for a list of global options.
5355
{{- else }}

cli/testdata/coder_--help.golden

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
Usage: coder [global-flags] <subcommand>
2-
3-
Coder v0.0.0-devel — A tool for provisioning self-hosted development environments with Terraform.
4-
- Start a Coder server:
5-
6-
$ coder server
7-
8-
- Get started by creating a template from an example:
9-
10-
$ coder templates init
11-
12-
Subcommands
1+
coder v0.0.0-devel
2+
3+
USAGE:
4+
coder [global-flags] <subcommand>
5+
6+
Coder v0.0.0-devel — A tool for provisioning self-hosted development
7+
environments with Terraform.
8+
- Start a Coder server:
9+
10+
$ coder server
11+
12+
- Get started by creating a template from an example:
13+
14+
$ coder templates init
15+
16+
SUBCOMMANDS:
1317
config-ssh Add an SSH Host entry for your workspaces "ssh
1418
coder.workspace"
1519
create Create a workspace
@@ -45,7 +49,7 @@ Coder v0.0.0-devel — A tool for provisioning self-hosted development environme
4549
users Manage users
4650
version Show coder version
4751

48-
Global Options
52+
GLOBAL OPTIONS:
4953
Global options are applied to all commands. They can be set using environment
5054
variables or flags.
5155

cli/testdata/coder_agent_--help.golden

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
Usage: coder agent [flags]
1+
coder v0.0.0-devel
22

3-
Starts the Coder workspace agent.
3+
USAGE:
4+
coder agent [flags]
45

5-
Options
6+
Starts the Coder workspace agent.
7+
8+
OPTIONS:
69
--log-human string, $CODER_AGENT_LOGGING_HUMAN (default: /dev/stderr)
710
Output human-readable logs to a given file.
811

cli/testdata/coder_config-ssh_--help.golden

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
Usage: coder config-ssh [flags]
1+
coder v0.0.0-devel
22

3-
Add an SSH Host entry for your workspaces "ssh coder.workspace"
3+
USAGE:
4+
coder config-ssh [flags]
45

5-
- You can use -o (or --ssh-option) so set SSH options to be used for all your
6-
workspaces:
6+
Add an SSH Host entry for your workspaces "ssh coder.workspace"
77

8-
$ coder config-ssh -o ForwardAgent=yes
8+
- You can use -o (or --ssh-option) so set SSH options to be used for all
9+
your
10+
workspaces:
11+
12+
$ coder config-ssh -o ForwardAgent=yes
13+
14+
- You can use --dry-run (or -n) to see the changes that would be made:
15+
16+
$ coder config-ssh --dry-run
917

10-
- You can use --dry-run (or -n) to see the changes that would be made:
11-
12-
$ coder config-ssh --dry-run
13-
14-
Options
18+
OPTIONS:
1519
--coder-binary-path string, $CODER_SSH_CONFIG_BINARY_PATH
1620
Optionally specify the absolute path to the coder binary used in
1721
ProxyCommand. By default, the binary invoking this command ('config

cli/testdata/coder_create_--help.golden

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
Usage: coder create [flags] [name]
1+
coder v0.0.0-devel
22

3-
Create a workspace
3+
USAGE:
4+
coder create [flags] [name]
45

5-
- Create a workspace for another user (if you have permission):
6+
Create a workspace
67

7-
$ coder create <username>/<workspace_name>
8+
- Create a workspace for another user (if you have permission):
9+
10+
$ coder create <username>/<workspace_name>
811

9-
Options
12+
OPTIONS:
1013
--parameter string-array, $CODER_RICH_PARAMETER
1114
Rich parameter value in the format "name=value".
1215

cli/testdata/coder_delete_--help.golden

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
Usage: coder delete [flags] <workspace>
1+
coder v0.0.0-devel
22

3-
Delete a workspace
3+
USAGE:
4+
coder delete [flags] <workspace>
45

5-
Aliases: rm
6+
Delete a workspace
67

7-
Options
8+
Aliases: rm
9+
10+
OPTIONS:
811
--orphan bool
912
Delete a workspace without deleting its resources. This can delete a
1013
workspace in a broken state, but may also lead to unaccounted cloud

cli/testdata/coder_dotfiles_--help.golden

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
Usage: coder dotfiles [flags] <git_repo_url>
1+
coder v0.0.0-devel
22

3-
Personalize your workspace by applying a canonical dotfiles repository
3+
USAGE:
4+
coder dotfiles [flags] <git_repo_url>
45

5-
- Check out and install a dotfiles repository without prompts:
6+
Personalize your workspace by applying a canonical dotfiles repository
67

7-
$ coder dotfiles --yes git@github.com:example/dotfiles.git
8+
- Check out and install a dotfiles repository without prompts:
9+
10+
$ coder dotfiles --yes git@github.com:example/dotfiles.git
811

9-
Options
12+
OPTIONS:
1013
-b, --branch string
1114
Specifies which branch to clone. If empty, will default to cloning the
1215
default branch or using the existing branch in the cloned repo on

cli/testdata/coder_list_--help.golden

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
Usage: coder list [flags]
1+
coder v0.0.0-devel
22

3-
List workspaces
3+
USAGE:
4+
coder list [flags]
45

5-
Aliases: ls
6+
List workspaces
67

7-
Options
8+
Aliases: ls
9+
10+
OPTIONS:
811
-a, --all bool
912
Specifies whether all workspaces will be listed or not.
1013

cli/testdata/coder_login_--help.golden

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
Usage: coder login [flags] <url>
1+
coder v0.0.0-devel
22

3-
Authenticate with Coder deployment
3+
USAGE:
4+
coder login [flags] <url>
45

5-
Options
6+
Authenticate with Coder deployment
7+
8+
OPTIONS:
69
--first-user-email string, $CODER_FIRST_USER_EMAIL
710
Specifies an email address to use if creating the first user for the
811
deployment.

cli/testdata/coder_logout_--help.golden

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
Usage: coder logout [flags]
1+
coder v0.0.0-devel
22

3-
Unauthenticate your local session
3+
USAGE:
4+
coder logout [flags]
45

5-
Options
6+
Unauthenticate your local session
7+
8+
OPTIONS:
69
-y, --yes bool
710
Bypass prompts.
811

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
Usage: coder netcheck
1+
coder v0.0.0-devel
22

3-
Print network debug information for DERP and STUN
3+
USAGE:
4+
coder netcheck
5+
6+
Print network debug information for DERP and STUN
47

58
---
69
Run `coder --help` for a list of global options.

cli/testdata/coder_ping_--help.golden

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
Usage: coder ping [flags] <workspace>
1+
coder v0.0.0-devel
22

3-
Ping a workspace
3+
USAGE:
4+
coder ping [flags] <workspace>
45

5-
Options
6+
Ping a workspace
7+
8+
OPTIONS:
69
-n, --num int (default: 10)
710
Specifies the number of pings to perform.
811

0 commit comments

Comments
 (0)