-
Notifications
You must be signed in to change notification settings - Fork 234
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
(v2) adaptive colors + writers #397
Conversation
This introduces a helper type `LightDark` that takes a boolean to determine which `Color(light, dark)` to choose from. The `adaptive` package is a helper package that uses the `lipgloss.LightDark` along with querying the terminal when the module is imported to choose the appropriate light-dark color. Example: ```go var ( light = "#0000ff" dark = "#ff0000" ) colorToUse := adaptive.Color(light, dark) // the terminal is queried before choosing the color fmt.Println(colorToUse) ```
Co-authored-by: Ayman Bagabas <[email protected]>
Co-authored-by: Ayman Bagabas <[email protected]>
Co-authored with @aymanbagabas
762e3ca
to
6fa7bb6
Compare
// Fprint pritnts to the given writer, automatically downsampling colors when | ||
// necessary. | ||
// | ||
// Example: | ||
// | ||
// str := NewStyle(). | ||
// Foreground(lipgloss.Color("#6a00ff")). | ||
// Render("guzzle") | ||
// | ||
// Fprint(os.Stderr, "I %s horchata pretty much all the time.\n", str) | ||
func Fprint(w io.Writer, v ...interface{}) (int, error) { | ||
return fmt.Fprint(colorprofile.NewWriter(w, os.Environ()), v...) //nolint:wrapcheck | ||
} | ||
|
||
// Fprint pritnts to the given writer, automatically downsampling colors when | ||
// necessary, and ending with a trailing newline. | ||
// | ||
// Example: | ||
// | ||
// str := NewStyle(). | ||
// Foreground(lipgloss.Color("#6a00ff")). | ||
// Render("Sandwich time!") | ||
// | ||
// Fprintln(os.Stderr, str) | ||
func Fprintln(w io.Writer, v ...interface{}) (int, error) { | ||
return fmt.Fprintln(colorprofile.NewWriter(w, os.Environ()), v...) //nolint:wrapcheck | ||
} | ||
|
||
// Fprintf prints text to a writer, against the given format, automatically | ||
// downsampling colors when necessary. | ||
// | ||
// Example: | ||
// | ||
// str := NewStyle(). | ||
// Foreground(lipgloss.Color("#6a00ff")). | ||
// Render("artichokes") | ||
// | ||
// Fprintf(os.Stderr, "I really love %s!\n", food) | ||
func Fprintf(w io.Writer, format string, v ...interface{}) (int, error) { | ||
return fmt.Fprintf(colorprofile.NewWriter(w, os.Environ()), format, v...) //nolint:wrapcheck | ||
} |
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.
I would leave this out. Every call allocates a new writer which defeats the purpose of fmt.Fprint
. Instead, people should create a writer for os.Stderr
and use fmt.Fprint
w := colorprofile.NewWriter(os.Stderr, os.Environ())
fmt.Fprintf(w, "I really love %s!", lipgloss.NewStyle().Render("artichokes"))
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.
I agree, but ideally we reduce the cognitive complexity a bit. Let's look at it again once this beast of a branch is merged into v2-exp
.
This PR contains solutions for adaptive colors, background color detection, and colorprofile-based writers. It also updates all tests and examples for
v2
.The big changes in here are:
Available writer utilties are:
Writer
(writes to stdout)Print(v ...any)
Println(...any)
Printf(string, ...any)
Fprint(io.Writer, ...any)
Fprintf(io.Writer, string, ...any)
Supersedes #392.
Supersedes #389.