Skip to content

Commit

Permalink
refactor: remove error return from HasDarkBackground
Browse files Browse the repository at this point in the history
This defaults to true if an error is encountered or if the background
color is nil. This is a simplification that makes the function easier to
use in practice.

Users can still check for errors by calling BackgroundColor directly.
  • Loading branch information
aymanbagabas committed Nov 15, 2024
1 parent ec86e88 commit 24d7bef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compat/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
// HasDarkBackground is true if the terminal has a dark background.
HasDarkBackground = func() bool {
hdb, _ := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
hdb := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
return hdb
}()

Expand Down
17 changes: 7 additions & 10 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lipgloss

import (
"errors"
"fmt"
"image/color"
"os"
Expand Down Expand Up @@ -53,7 +52,7 @@ func BackgroundColor(in *os.File, out *os.File) (bg color.Color, err error) {
// Typically, you'll want to query against stdin and either stdout or stderr
// depending on what you're writing to.
//
// hasDarkBG, _ := HasDarkBackground(os.Stdin, os.Stdout)
// hasDarkBG := HasDarkBackground(os.Stdin, os.Stdout)
// lightDark := LightDark(hasDarkBG)
// myHotColor := lightDark("#ff0000", "#0000ff")
//
Expand All @@ -62,14 +61,12 @@ func BackgroundColor(in *os.File, out *os.File) (bg color.Color, err error) {
//
// case tea.BackgroundColorMsg:
// hasDarkBackground = msg.IsDark()
func HasDarkBackground(in *os.File, out *os.File) (bool, error) {
//
// By default, this function will return true if it encounters an error.
func HasDarkBackground(in *os.File, out *os.File) bool {
bg, err := BackgroundColor(in, out)
if err != nil {
return true, fmt.Errorf("could not detect background color: %w", err)
if err != nil || bg == nil {
return true
}
if bg == nil {
return true, errors.New("detected background color is nil")
}

return isDarkColor(bg), nil
return isDarkColor(bg)
}

0 comments on commit 24d7bef

Please sign in to comment.