-
Notifications
You must be signed in to change notification settings - Fork 943
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
Changes from 12 commits
5e308df
86175bf
098d35d
3d13060
5b8fa3b
4ce84a4
a75dbb2
390a7ca
7e6db79
ce913a5
22a2d0b
5e42118
40eb24e
ccf174a
1fbef1d
44c3726
d16f625
1babe96
ff3c392
6d93c56
506aa28
997dc43
24a838d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package cliui | |
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"slices" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -12,6 +14,8 @@ import ( | |
"github.com/coder/pretty" | ||
) | ||
|
||
const NoColorFlag = "no-color" | ||
|
||
var Canceled = xerrors.New("canceled") | ||
|
||
// DefaultStyles compose visual elements of the UI. | ||
|
@@ -37,22 +41,37 @@ 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") | ||
) | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried setting the option on the root command? It should then evaluate on every command. |
||
// 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)) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do!
Do my eyes deceive me or are these both the same? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A case of copy-pasta 😂, sorry. Meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got you, just pushed a change with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is super janky. Relying on os.Args makes it difficult to test. Also, someone could modify the option at the CLI layer and easily forget to update this. Have you considered checking the CLI option higher up in the stack? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I absolutely agree with you, however serpent doesn't (from what I can tell) have any way to support this as it lacks the ability to set a middleware that runs before any command. To demonstrate what I mean, the code in this gist has a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scratch that: I think using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed a change that should be less janky (using the method describe in the prior message), let me know what you think! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do the Walk approach we have the flag duplicated on every help, which is obnoxious. Can't you use an Option on the root command? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I think I explained myself poorly, the Walk was to inject middleware onto every command. The Option was on the root command. This change was backed out anyways as mutating the |
||
slices.Contains(os.Args, fmt.Sprintf("--%s=true", NoColorFlag)) || | ||
os.Getenv("CODER_NO_COLOR") != "" || | ||
os.Getenv("NO_COLOR") != "" { | ||
color = termenv.Ascii | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? I see that the serpent options modify the global var in this package, so checking That said, I really wish we could do it differently as modifying globals is a bit icky 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately not as |
||
}) | ||
return color.Color(s) | ||
} | ||
|
@@ -109,11 +128,11 @@ func Field(s string) string { | |
return pretty.Sprint(DefaultStyles.Field, s) | ||
} | ||
|
||
func ifTerm(fmt pretty.Formatter) pretty.Formatter { | ||
func ifTerm(f pretty.Formatter) pretty.Formatter { | ||
if !isTerm() { | ||
return pretty.Nop | ||
} | ||
return fmt | ||
return f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more F438 a>. 👍 |
||
} | ||
|
||
func init() { | ||
|
@@ -123,42 +142,45 @@ func init() { | |
DefaultStyles = Styles{ | ||
Code: pretty.Style{ | ||
ifTerm(pretty.XPad(1, 1)), | ||
pretty.FgColor(Red), | ||
pretty.BgColor(color.Color("#2c2c2c")), | ||
pretty.FgColor(color.Color("#ED567A")), | ||
pretty.BgColor(color.Color("#2C2C2C")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Were there no acceptable ANSI colors to replace these? Why did we go from red to ed567a? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we set both the foreground and background colour, we have complete control over the contrast so this looks the same regardless of the terminal theme. If we want to use an ANSI code instead we can but there isn't a need to here.
|
||
}, | ||
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.BgColor(color.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! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,6 +331,13 @@ 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 | ||
|
||
globalGroup := &serpent.Group{ | ||
Name: "Global", | ||
Description: `Global options are applied to all commands. They can be set using environment variables or flags.`, | ||
|
@@ -461,6 +468,14 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err | |
Value: serpent.StringOf(&r.globalConfig), | ||
Group: globalGroup, | ||
}, | ||
{ | ||
Flag: cliui.NoColorFlag, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wonder if it would make sense to also allow setting it as an env ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that should be a good idea. There is a 'standard' around using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: I think we should add the env as well here. |
||
Env: "CODER_NO_COLOR", | ||
Default: "false", | ||
Description: "Disable use of color in CLI output.", | ||
Group: globalGroup, | ||
Value: serpent.BoolOf(&noColorDiscarded), | ||
}, | ||
{ | ||
Flag: "version", | ||
// This was requested by a customer to assist with their migration. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -37,6 +38,53 @@ 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be hidden There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify this with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
Handler: func(inv *serpent.Invocation) error { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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