Skip to content
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

Merged
merged 33 commits into from
Oct 18, 2024
Merged

(v2) adaptive colors + writers #397

merged 33 commits into from
Oct 18, 2024

Conversation

meowgorithm
Copy link
Member

@meowgorithm meowgorithm commented Oct 16, 2024

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:

// Background color detection.
hasDarkBG, err := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
if err != nil {
    log.Fatal("Uh oh:", err)
}

// Adaptive color helper.
lightDark := lipgloss.LightDark(hasDarkBG)
c := lightDark.Color(lightDark(0x212121, 0xf1f1f1))

// Color downsampling happens via a writer.
str := lipgloss.NewStyle().Foreground(c).Render("Hello!")
lipgloss.Println(str)

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.

aymanbagabas and others added 22 commits August 30, 2024 17:12
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]>
@meowgorithm meowgorithm force-pushed the v2-adaptive-standalone branch from 762e3ca to 6fa7bb6 Compare October 17, 2024 17:10
@meowgorithm meowgorithm added the v2 label Oct 17, 2024
@meowgorithm meowgorithm marked this pull request as ready for review October 17, 2024 17:49
@meowgorithm meowgorithm marked this pull request as ready for review October 17, 2024 20:17
examples/layout/main.go Outdated Show resolved Hide resolved
go.mod Show resolved Hide resolved
Comment on lines +56 to +96
// 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
}
Copy link
Member

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"))

Copy link
Member Author

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.

@meowgorithm meowgorithm merged commit 125420d into v2-exp Oct 18, 2024
10 checks passed
@meowgorithm meowgorithm deleted the v2-adaptive-standalone branch October 18, 2024 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants