Skip to content

Commit 66b8f6f

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

File tree

86 files changed

+737
-434
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

+737
-434
lines changed

cli/cliui/cliui.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
Red = color.Color("#ED567A")
4949
Fuchsia = color.Color("#EE6FF8")
5050
Yellow = color.Color("#ECFD65")
51+
Gold = color.Color("#ffd700")
5152
Blue = color.Color("#5000ff")
5253
)
5354

cli/help.go

Lines changed: 19 additions & 4 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,21 @@ func wrapTTY(s string) string {
4749
var usageTemplate = template.Must(
4850
template.New("usage").Funcs(
4951
template.FuncMap{
52+
"nameVersion": func() string {
53+
txt := pretty.String("coder")
54+
pretty.FgColor(cliui.Green).Format(txt)
55+
txt.Append(" ", buildinfo.Version())
56+
return txt.String()
57+
},
5058
"wrapTTY": func(s string) string {
5159
return wrapTTY(s)
5260
},
5361
"trimNewline": func(s string) string {
5462
return strings.TrimSuffix(s, "\n")
5563
},
64+
"keyword": func(s string) string {
65+
return cliui.Keyword(s)
66+
},
5667
"typeHelper": func(opt *clibase.Option) string {
5768
switch v := opt.Value.(type) {
5869
case *clibase.Enum:
@@ -71,13 +82,15 @@ var usageTemplate = template.Must(
7182

7283
body = wordwrap.WrapString(body, uint(twidth-len(spacing)))
7384

85+
sc := bufio.NewScanner(strings.NewReader(body))
86+
7487
var sb strings.Builder
75-
for _, line := range strings.Split(body, "\n") {
88+
for sc.Scan() {
7689
// Remove existing indent, if any.
77-
line = strings.TrimSpace(line)
90+
// line = strings.TrimSpace(line)
7891
// Use spaces so we can easily calculate wrapping.
7992
_, _ = sb.WriteString(spacing)
80-
_, _ = sb.WriteString(line)
93+
_, _ = sb.Write(sc.Bytes())
8194
_, _ = sb.WriteString("\n")
8295
}
8396
return sb.String()
@@ -127,7 +140,9 @@ var usageTemplate = template.Must(
127140
return opt.Flag
128141
},
129142
"prettyHeader": func(s string) string {
130-
return cliui.Bold(s)
143+
return pretty.Sprint(
144+
pretty.FgColor(cliui.Gold), strings.ToUpper(s), ":",
145+
)
131146
},
132147
"isEnterprise": func(opt clibase.Option) bool {
133148
return opt.Annotations.IsSet("enterprise")

cli/help.tpl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
{{- /* Heavily inspired by the Go toolchain formatting. */ -}}
2-
Usage: {{.FullUsage}}
1+
{{- /* Heavily inspired by the Go toolchain and fd */ -}}
2+
{{nameVersion}}
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+
{{- $long := formatLong . }}
20+
{{- indent $long 2}}
1821
{{ "\n" }}
1922
{{- end }}
2023
{{ with visibleChildren . }}
@@ -34,11 +37,11 @@ Usage: {{.FullUsage}}
3437
{{- else }}
3538
{{- end }}
3639
{{- range $index, $option := $group.Options }}
37-
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n -" $option.FlagShorthand ", " -}}
40+
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n "}} {{ keyword "-"}}{{keyword $option.FlagShorthand }}{{", "}}
3841
{{- else }}{{- print "\n " -}}
3942
{{- end }}
40-
{{- with flagName $option }}--{{ . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
41-
{{- with envName $option }}, ${{ . }}{{ end }}
43+
{{- with flagName $option }}{{keyword "--"}}{{ keyword . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
44+
{{- with envName $option }}, {{ print "$" . | keyword }}{{ end }}
4245
{{- with $option.Default }} (default: {{ . }}){{ end }}
4346
{{- with $option.Description }}
4447
{{- $desc := $option.Description }}

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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
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 your
9+
workspaces:
10+
11+
$ coder config-ssh -o ForwardAgent=yes
12+
13+
- You can use --dry-run (or -n) to see the changes that would be made:
14+
15+
$ coder config-ssh --dry-run
916

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
17+
OPTIONS:
1518
--coder-binary-path string, $CODER_SSH_CONFIG_BINARY_PATH
1619
Optionally specify the absolute path to the coder binary used in
1720
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)