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

feat: simplify adaptive package and expose QueryBackgroundColor #393

Open
wants to merge 1 commit into
base: v2-exp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 28 additions & 37 deletions adaptive/adaptive.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
package adaptive

import (
"image/color"
"os"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/term"
)

// Stdin, Stdout, and HasDarkBackground are the standard input, output, and
// default background color value to use. They can be overridden by the
// importing program.
var (
Stdin = os.Stdin
Stdout = os.Stdout
HasDarkBackground = true
)

// colorFn is the light-dark Lip Gloss color function to use to determine the
// appropriate color based on the terminal's background color.
// When a program imports this package, it will query the terminal's background
// color and use it to determine whether to use the light or dark color.
var colorFn lipgloss.Adapt

func init() {
Query()
}

// Query queries the terminal's background color and updates the color function
// accordingly.
func Query() {
colorFn = lipgloss.Adapt(func() bool {
state, err := term.MakeRaw(Stdin.Fd())
if err != nil {
return HasDarkBackground
}

defer term.Restore(Stdin.Fd(), state) //nolint:errcheck

bg, err := queryBackgroundColor(Stdin, Stdout)
if err == nil {
return lipgloss.IsDarkColor(bg)
}

return HasDarkBackground
}())
// HasDarkBackground is true if the terminal has a dark background. It defaults
// to true if the background color cannot be determined.
var HasDarkBackground = func() bool {
bg, err := QueryBackgroundColor(os.Stdin, os.Stdout)
if err != nil {
return true
}

return lipgloss.IsDarkColor(bg)
}()

// QueryBackgroundColor queries the terminal for the background color. If the
// terminal does not support querying the background color, nil is returned.
//
// Example usage:
//
// bg, _ := adaptive.QueryBackgroundColor(os.Stdin, os.Stdout)
// fmt.Printf("Background color: %v, isDark: %v\n", bg, lipgloss.IsDarkColor(bg))
func QueryBackgroundColor(stdin term.File, stdout term.File) (color.Color, error) {
state, err := term.MakeRaw(stdin.Fd())
if err != nil {
return nil, err

Check failure on line 32 in adaptive/adaptive.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func github.com/charmbracelet/x/term.MakeRaw(fd uintptr) (*github.com/charmbracelet/x/term.State, error) (wrapcheck)

Check failure on line 32 in adaptive/adaptive.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func github.com/charmbracelet/x/term.MakeRaw(fd uintptr) (*github.com/charmbracelet/x/term.State, error) (wrapcheck)
}

defer term.Restore(stdin.Fd(), state) //nolint:errcheck

return queryBackgroundColor(stdin, stdout)
}
4 changes: 3 additions & 1 deletion adaptive/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package adaptive

import (
"image/color"

"github.com/charmbracelet/lipgloss"
)

// Color returns the color that should be used based on the terminal's
// background color.
func Color(light, dark any) color.Color {
return colorFn.Color(light, dark)
return lipgloss.Adapt(HasDarkBackground).Color(light, dark)
}
Loading