Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5e308df
chore: add command for showing colors
DanielleMaywood Sep 13, 2024
86175bf
fix: use ANSI color codes instead of RGB
DanielleMaywood Sep 13, 2024
098d35d
feat: add '--no-color' flag
DanielleMaywood Sep 13, 2024
3d13060
fix: revert colors
DanielleMaywood Sep 13, 2024
5b8fa3b
chore: change colors
DanielleMaywood Sep 13, 2024
4ce84a4
fix: update golden files
DanielleMaywood Sep 13, 2024
a75dbb2
fix: replace blue with brightBlue
DanielleMaywood Sep 13, 2024
390a7ca
fix: drop '> ' for unfocused prompts
DanielleMaywood Sep 13, 2024
7e6db79
fix: run 'make fmt'
DanielleMaywood Sep 13, 2024
ce913a5
chore: allow disabling color with env flags
DanielleMaywood Sep 13, 2024
22a2d0b
fix: apply fixes from feedback
DanielleMaywood Sep 13, 2024
5e42118
fix: run 'make gen'
DanielleMaywood Sep 13, 2024
40eb24e
fix: refactor janky code
DanielleMaywood Sep 13, 2024
ccf174a
fix: re-add public function
DanielleMaywood Sep 13, 2024
1fbef1d
fix: re-add init for non-color tests
DanielleMaywood Sep 13, 2024
44c3726
fix: move styles to 'init' that can be
DanielleMaywood Sep 13, 2024
d16f625
fix: stop overwriting entire DefaultStyles
DanielleMaywood Sep 13, 2024
1babe96
fix: make code and field obey --no-color
DanielleMaywood Sep 13, 2024
ff3c392
fix: rip out '--no-color' due to race condition
DanielleMaywood Sep 13, 2024
6d93c56
fix: apply nit
DanielleMaywood Sep 13, 2024
506aa28
fix: simplify code && hide command
DanielleMaywood Sep 16, 2024
997dc43
fix: newline shouldn't be themed
DanielleMaywood Sep 16, 2024
24a838d
fix: appease the linter
DanielleMaywood Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions cli/cliui/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ var (
)

var (
Green = Color("#04B575")
Red = Color("#ED567A")
Fuchsia = Color("#EE6FF8")
Yellow = Color("#ECFD65")
Blue = Color("#5000ff")
// ANSI color codes
red = Color("1")
green = Color("2")
yellow = Color("3")
magenta = Color("5")
white = Color("7")
brightBlue = Color("12")
brightMagenta = Color("13")
Comment on lines +41 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indicate these are ANSI color codes

)

// Color returns a color for the given string.
func Color(s string) termenv.Color {
colorOnce.Do(func() {
color = termenv.NewOutput(os.Stdout).ColorProfile()
color = termenv.NewOutput(os.Stdout).EnvColorProfile()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution for disabling colors 👍🏻


if flag.Lookup("test.v") != nil {
// Use a consistent colorless profile in tests so that results
// are deterministic.
Expand Down Expand Up @@ -123,42 +127,45 @@ func init() {
DefaultStyles = Styles{
Code: pretty.Style{
ifTerm(pretty.XPad(1, 1)),
pretty.FgColor(Red),
pretty.BgColor(color.Color("#2c2c2c")),
pretty.FgColor(Color("#ED567A")),
pretty.BgColor(Color("#2C2C2C")),
},
DateTimeStamp: pretty.Style{
pretty.FgColor(color.Color("#7571F9")),
pretty.FgColor(brightBlue),
},
Error: pretty.Style{
pretty.FgColor(Red),
pretty.FgColor(red),
},
Field: pretty.Style{
pretty.XPad(1, 1),
pretty.FgColor(color.Color("#FFFFFF")),
pretty.BgColor(color.Color("#2b2a2a")),
pretty.FgColor(Color("#FFFFFF")),
pretty.BgColor(Color("#2B2A2A")),
},
Fuchsia: pretty.Style{
pretty.FgColor(brightMagenta),
},
FocusedPrompt: pretty.Style{
pretty.FgColor(white),
pretty.Wrap("> ", ""),
pretty.FgColor(brightBlue),
},
Keyword: pretty.Style{
pretty.FgColor(Green),
pretty.FgColor(green),
},
Placeholder: pretty.Style{
pretty.FgColor(color.Color("#4d46b3")),
pretty.FgColor(magenta),
},
Prompt: pretty.Style{
pretty.FgColor(color.Color("#5C5C5C")),
pretty.Wrap("> ", ""),
pretty.FgColor(white),
pretty.Wrap(" ", ""),
},
Warn: pretty.Style{
pretty.FgColor(Yellow),
pretty.FgColor(yellow),
},
Wrap: pretty.Style{
pretty.LineWrap(80),
},
}

DefaultStyles.FocusedPrompt = append(
DefaultStyles.Prompt,
pretty.FgColor(Blue),
)
}

// ValidateNotEmpty is a helper function to disallow empty inputs!
Expand Down
10 changes: 5 additions & 5 deletions cli/cliui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (m selectModel) View() string {
if m.cursor == start+i {
style = pretty.Style{
pretty.Wrap("> ", ""),
pretty.FgColor(Green),
DefaultStyles.Keyword,
}
}

Expand Down Expand Up @@ -481,13 +481,13 @@ func (m multiSelectModel) View() string {
o := option.option

if m.cursor == i {
cursor = pretty.Sprint(pretty.FgColor(Green), "> ")
chosen = pretty.Sprint(pretty.FgColor(Green), "[ ]")
o = pretty.Sprint(pretty.FgColor(Green), o)
cursor = pretty.Sprint(DefaultStyles.Keyword, "> ")
chosen = pretty.Sprint(DefaultStyles.Keyword, "[ ]")
o = pretty.Sprint(DefaultStyles.Keyword, o)
}

if option.chosen {
chosen = pretty.Sprint(pretty.FgColor(Green), "[x]")
chosen = pretty.Sprint(DefaultStyles.Keyword, "[x]")
}

_, _ = s.WriteString(fmt.Sprintf(
Expand Down
38 changes: 38 additions & 0 deletions cmd/cliui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
"github.com/coder/serpent"
)

Expand All @@ -37,6 +38,43 @@ func main() {
},
}

root.Children = append(root.Children, &serpent.Command{
Use: "colors",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be hidden

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Handler: func(inv *serpent.Invocation) error {
msg := pretty.Sprint(cliui.DefaultStyles.Code, "This is a code message")
_, _ = fmt.Fprintln(inv.Stdout, msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this with pretty.Fprintf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I had to separate the newline from the Fprintf call though as otherwise the newline would get themed.


msg = pretty.Sprint(cliui.DefaultStyles.DateTimeStamp, "This is a datetimestamp message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Error, "This is an error message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Field, "This is a field message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Keyword, "This is a keyword message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Placeholder, "This is a placeholder message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Prompt, "This is a prompt message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.FocusedPrompt, "This is a focused prompt message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Fuchsia, "This is a fuchsia message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

msg = pretty.Sprint(cliui.DefaultStyles.Warn, "This is a warning message")
_, _ = fmt.Fprintln(inv.Stdout, msg)

return nil
},
})

root.Children = append(root.Children, &serpent.Command{
Use: "prompt",
Handler: func(inv *serpent.Invocation) error {
Expand Down
Loading