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: refactor janky code
  • Loading branch information
DanielleMaywood committed Sep 13, 2024
commit 40eb24ee2b9888d3f6c28ce14491117f6b6eaf81
68 changes: 26 additions & 42 deletions cli/cliui/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package cliui

import (
"flag"
"fmt"
"os"
"slices"
"sync"
"time"

"github.com/muesli/termenv"
Expand Down Expand Up @@ -36,46 +33,9 @@ type Styles struct {
}

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

var (
// ANSI color codes
red = Color("1")
green = Color("2")
yellow = Color("3")
magenta = Color("5")
white = Color("7")
brightBlue = Color("12")
brightMagenta = Color("13")
)

// Color returns a color for the given string.
func Color(s string) termenv.Color {
colorOnce.Do(func() {
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
}

// Currently it appears there is no way to use the flag from
// serpent as it isn't possible to create a root middleware that
// runs for every command. For now we just check if `os.Args`
// has the flag.
if slices.Contains(os.Args, fmt.Sprintf("--%s", NoColorFlag)) ||
slices.Contains(os.Args, fmt.Sprintf("--%s=true", NoColorFlag)) ||
os.Getenv("CODER_NO_COLOR") != "" ||
os.Getenv("NO_COLOR") != "" {
color = termenv.Ascii
}
})
return color.Color(s)
}

func isTerm() bool {
return color != termenv.Ascii
}
Expand Down Expand Up @@ -135,7 +95,31 @@ func ifTerm(f pretty.Formatter) pretty.Formatter {
return f
}

func init() {
type InitOptions struct {
NoColor bool
}

func Init(opts InitOptions) {
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
}

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.
Expand Down
23 changes: 16 additions & 7 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,21 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
r.clientURL = new(url.URL)
}

// NOTE(Danielle): It appears there is no way to have a 'global' middleware in
// serpent so we manually handle the ENV/flag lookup and setup
// the option in the below OptionSet so it is documented.
// We use (and discard) this local variable to get the correct
// behavior from the CLI when the option is passed.
var noColorDiscarded bool
// 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",
Expand Down Expand Up @@ -474,7 +483,7 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
Default: "false",
Description: "Disable use of color in CLI output.",
Group: globalGroup,
Value: serpent.BoolOf(&noColorDiscarded),
Value: serpent.BoolOf(&noColor),
},
{
Flag: "version",
Expand Down
32 changes: 22 additions & 10 deletions cmd/cliui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ func main() {
},
}

var noColorDiscarded bool

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

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 {
Expand Down Expand Up @@ -398,6 +388,28 @@ 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
Loading