Skip to content

Commit

Permalink
fix: query both stdin and stdout for background color on non-Windows …
Browse files Browse the repository at this point in the history
…platforms
  • Loading branch information
aymanbagabas committed Oct 29, 2024
1 parent 9718f22 commit a7c67ae
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
//
Expand Down

0 comments on commit a7c67ae

Please sign in to comment.