diff --git a/query.go b/query.go index a39dded5..2b87e198 100644 --- a/query.go +++ b/query.go @@ -5,17 +5,12 @@ import ( "fmt" "image/color" "os" + "runtime" "github.com/charmbracelet/x/term" ) -// BackgroundColor queries the terminal's background color. Typically, you'll -// want to query against stdin and either stdout or stderr, depending on what -// you're writing to. -// -// This function is intended for standalone Lip Gloss use only. If you're using -// Bubble Tea, listen for tea.BackgroundColorMsg in your update function. -func BackgroundColor(in *os.File, out *os.File) (color.Color, error) { +func backgroundColor(in *os.File, out *os.File) (color.Color, error) { state, err := term.MakeRaw(in.Fd()) if err != nil { return nil, fmt.Errorf("error setting raw state to detect background color: %w", err) @@ -31,6 +26,26 @@ func BackgroundColor(in *os.File, out *os.File) (color.Color, error) { return bg, nil } +// BackgroundColor queries the terminal's background color. Typically, you'll +// want to query against stdin and either stdout or stderr, depending on what +// you're writing to. +// +// This function is intended for standalone Lip Gloss use only. If you're using +// Bubble Tea, listen for tea.BackgroundColorMsg in your update function. +func BackgroundColor(in *os.File, out *os.File) (bg color.Color, err error) { + if runtime.GOOS == "windows" { + return backgroundColor(in, out) + } else { + // NOTE: On Unix, one of the given files must be a tty. + for _, f := range []*os.File{in, out} { + if bg, err = backgroundColor(f, f); err == nil { + return bg, nil + } + } + } + return +} + // HasDarkBackground detects whether the terminal has a light or dark // background. //