-
Notifications
You must be signed in to change notification settings - Fork 979
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 20 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 |
---|---|---|
|
@@ -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") | ||
) | ||
|
||
// 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() | ||
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. Good solution for disabling colors 👍🏻 |
||
|
||
if flag.Lookup("test.v") != nil { | ||
// Use a consistent colorless profile in tests so that results | ||
// are deterministic. | ||
|
@@ -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! | ||
|
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,43 @@ func main() { | |
}, | ||
} | ||
|
||
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{ | ||
Use: "prompt", | ||
Handler: func(inv *serpent.Invocation) error { | ||
|
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