From 24d7bef0248b653d54430361240e70f212d51ae2 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 15 Nov 2024 15:58:47 -0500 Subject: [PATCH] refactor: remove error return from HasDarkBackground 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. --- compat/color.go | 2 +- query.go | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/compat/color.go b/compat/color.go index c8c99d13..8551da48 100644 --- a/compat/color.go +++ b/compat/color.go @@ -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 }() diff --git a/query.go b/query.go index bff51cdc..4d678fb7 100644 --- a/query.go +++ b/query.go @@ -1,7 +1,6 @@ package lipgloss import ( - "errors" "fmt" "image/color" "os" @@ -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") // @@ -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) }