-
Notifications
You must be signed in to change notification settings - Fork 897
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
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 86175bf
fix: use ANSI color codes instead of RGB
DanielleMaywood 098d35d
feat: add '--no-color' flag
DanielleMaywood 3d13060
fix: revert colors
DanielleMaywood 5b8fa3b
chore: change colors
DanielleMaywood 4ce84a4
fix: update golden files
DanielleMaywood a75dbb2
fix: replace blue with brightBlue
DanielleMaywood 390a7ca
fix: drop '> ' for unfocused prompts
DanielleMaywood 7e6db79
fix: run 'make fmt'
DanielleMaywood ce913a5
chore: allow disabling color with env flags
DanielleMaywood 22a2d0b
fix: apply fixes from feedback
DanielleMaywood 5e42118
fix: run 'make gen'
DanielleMaywood 40eb24e
fix: refactor janky code
DanielleMaywood ccf174a
fix: re-add public function
DanielleMaywood 1fbef1d
fix: re-add init for non-color tests
DanielleMaywood 44c3726
fix: move styles to 'init' that can be
DanielleMaywood d16f625
fix: stop overwriting entire DefaultStyles
DanielleMaywood 1babe96
fix: make code and field obey --no-color
DanielleMaywood ff3c392
fix: rip out '--no-color' due to race condition
DanielleMaywood 6d93c56
fix: apply nit
DanielleMaywood 506aa28
fix: simplify code && hide command
DanielleMaywood 997dc43
fix: newline shouldn't be themed
DanielleMaywood 24a838d
fix: appease the linter
DanielleMaywood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
commit ff3c39294d72475b0f0ed025ecce43c745faef1f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package cliui | |
import ( | ||
"flag" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/muesli/termenv" | ||
|
@@ -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. | ||
|
@@ -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") | ||
) | ||
|
||
// Color returns a color for the given string. | ||
func Color(s string) termenv.Color { | ||
colorOnce.Do(func() { | ||
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. | ||
color = termenv.Ascii | ||
} | ||
}) | ||
return color.Color(s) | ||
} | ||
|
||
|
@@ -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), | ||
}, | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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