Skip to content

fix: use ANSI colors codes instead of RGB #14665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: rip out '--no-color' due to race condition
We still support `NO_COLOR` env variable through termenv's
`EnvColorProfile`. The reason for the race condition is that
`DefaultStyles` is a global that we shouldn't mutate after `init`
is called, but we have to mutate it after `init` has ran to have
serpent collect the cli flags and env vars for us.
  • Loading branch information
DanielleMaywood committed Sep 13, 2024
commit ff3c39294d72475b0f0ed025ecce43c745faef1f
142 changes: 67 additions & 75 deletions cli/cliui/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cliui
import (
"flag"
"os"
"sync"
"time"

"github.com/muesli/termenv"
Expand All @@ -11,8 +12,6 @@ import (
"github.com/coder/pretty"
)

const NoColorFlag = "no-color"

var Canceled = xerrors.New("canceled")

// DefaultStyles compose visual elements of the UI.
Expand All @@ -32,10 +31,32 @@ type Styles struct {
Wrap pretty.Style
}

var color termenv.Profile
var (
color termenv.Profile
colorOnce sync.Once
)

var (
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).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.
color = termenv.Ascii
}
})
return color.Color(s)
}

Expand Down Expand Up @@ -91,87 +112,58 @@ func Field(s string) string {
return pretty.Sprint(DefaultStyles.Field, s)
}

func ifTerm(f pretty.Formatter) pretty.Formatter {
func ifTerm(fmt pretty.Formatter) pretty.Formatter {
if !isTerm() {
return pretty.Nop
}
return f
}

type InitOptions struct {
NoColor bool
return fmt
}

func init() {
color = termenv.NewOutput(os.Stdout).ColorProfile()

if flag.Lookup("test.v") != nil {
// Use a consistent colorless profile in tests so that results
// are deterministic.
color = termenv.Ascii
}

DefaultStyles.Code = pretty.Style{
ifTerm(pretty.XPad(1, 1)),
}
DefaultStyles.Field = pretty.Style{
pretty.XPad(1, 1),
}
DefaultStyles.Wrap = pretty.Style{
pretty.LineWrap(80),
}
}

func Init(opts InitOptions) {
if opts.NoColor {
color = termenv.Ascii
}

red := color.Color("1")
green := color.Color("2")
yellow := color.Color("3")
magenta := color.Color("5")
white := color.Color("7")
brightBlue := color.Color("12")
brightMagenta := color.Color("13")

// We do not adapt the color based on whether the terminal is light or dark.
// Doing so would require a round-trip between the program and the terminal
// due to the OSC query and response.
DefaultStyles.Code = append(DefaultStyles.Code,
pretty.FgColor(color.Color("#ED567A")),
pretty.BgColor(color.Color("#2C2C2C")),
)
DefaultStyles.DateTimeStamp = pretty.Style{
pretty.FgColor(brightBlue),
}
DefaultStyles.Error = pretty.Style{
pretty.FgColor(red),
}
DefaultStyles.Field = append(DefaultStyles.Field,
pretty.FgColor(color.Color("#FFFFFF")),
pretty.BgColor(color.Color("#2B2A2A")),
)
DefaultStyles.Fuchsia = pretty.Style{
pretty.FgColor(brightMagenta),
}
DefaultStyles.FocusedPrompt = pretty.Style{
pretty.FgColor(white),
pretty.Wrap("> ", ""),
pretty.FgColor(brightBlue),
}
DefaultStyles.Keyword = pretty.Style{
pretty.FgColor(green),
}
DefaultStyles.Placeholder = pretty.Style{
pretty.FgColor(magenta),
}
DefaultStyles.Prompt = pretty.Style{
pretty.FgColor(white),
pretty.Wrap(" ", ""),
}
DefaultStyles.Warn = pretty.Style{
pretty.FgColor(yellow),
DefaultStyles = Styles{
Code: pretty.Style{
ifTerm(pretty.XPad(1, 1)),
pretty.FgColor(Color("#ED567A")),
pretty.BgColor(Color("#2C2C2C")),
},
DateTimeStamp: pretty.Style{
pretty.FgColor(brightBlue),
},
Error: pretty.Style{
pretty.FgColor(red),
},
Field: pretty.Style{
pretty.XPad(1, 1),
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),
},
Placeholder: pretty.Style{
pretty.FgColor(magenta),
},
Prompt: pretty.Style{
pretty.FgColor(white),
pretty.Wrap(" ", ""),
},
Warn: pretty.Style{
pretty.FgColor(yellow),
},
Wrap: pretty.Style{
pretty.LineWrap(80),
},
}
}

Expand Down
24 changes: 0 additions & 24 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,22 +331,6 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
r.clientURL = new(url.URL)
}

// Add a wrapper to every command to ensure we've loaded our CLI theme prior
// to running any handler.
var noColor bool
cmd.Walk(func(c *serpent.Command) {
middleware := func(next serpent.HandlerFunc) serpent.HandlerFunc {
cliui.Init(cliui.InitOptions{NoColor: noColor})
return next
}

if c.Middleware != nil {
c.Middleware = serpent.Chain(c.Middleware, middleware)
} else {
c.Middleware = middleware
}
})

globalGroup := &serpent.Group{
Name: "Global",
Description: `Global options are applied to all commands. They can be set using environment variables or flags.`,
Expand Down Expand Up @@ -477,14 +461,6 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
Value: serpent.StringOf(&r.globalConfig),
Group: globalGroup,
},
{
Flag: cliui.NoColorFlag,
Env: "CODER_NO_COLOR",
Default: "false",
Description: "Disable use of color in CLI output.",
Group: globalGroup,
Value: serpent.BoolOf(&noColor),
},
{
Flag: "version",
// This was requested by a customer to assist with their migration.
Expand Down
3 changes: 0 additions & 3 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ variables or flags.
requests. The command must output each header as `key=value` on its
own line.

--no-color bool, $CODER_NO_COLOR (default: false)
Disable use of color in CLI output.

--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
Suppress warnings about unlicensed features.

Expand Down
22 changes: 0 additions & 22 deletions cmd/cliui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,6 @@ func main() {
},
})

var noColor bool
root.Options = []serpent.Option{
{
Default: "false",
Flag: cliui.NoColorFlag,
Value: serpent.BoolOf(&noColor),
},
}

root.Walk(func(cmd *serpent.Command) {
middleware := func(next serpent.HandlerFunc) serpent.HandlerFunc {
cliui.Init(cliui.InitOptions{NoColor: noColor})
return next
}

if cmd.Middleware != nil {
cmd.Middleware = serpent.Chain(cmd.Middleware, middleware)
} else {
cmd.Middleware = middleware
}
})

err := root.Invoke(os.Args[1:]...).WithOS().Run()
if err != nil {
_, _ = fmt.Println(err.Error())
Expand Down
10 changes: 0 additions & 10 deletions docs/reference/cli/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions enterprise/cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ variables or flags.
requests. The command must output each header as `key=value` on its
own line.

--no-color bool, $CODER_NO_COLOR (default: false)
Disable use of color in CLI output.

--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
Suppress warnings about unlicensed features.

Expand Down
Loading