Skip to content

Commit 883d2ef

Browse files
committed
fix: lipgloss.HasDarkBackground hangs in CI environments
1 parent 3d0fe5d commit 883d2ef

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/charmbracelet/colorprofile v0.4.3
1313
github.com/charmbracelet/x/ansi v0.11.6
1414
github.com/charmbracelet/x/exp/teatest/v2 v2.0.0-20260330094520-2dce04b6f8a4
15+
github.com/charmbracelet/x/term v0.2.2
1516
github.com/muesli/reflow v0.3.0
1617
go.uber.org/mock v0.6.0
1718
golang.org/x/term v0.41.0
@@ -29,7 +30,6 @@ require (
2930
github.com/charmbracelet/x/exp/ordered v0.1.0 // indirect
3031
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
3132
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
32-
github.com/charmbracelet/x/term v0.2.2 // indirect
3333
github.com/charmbracelet/x/termios v0.1.1 // indirect
3434
github.com/charmbracelet/x/windows v0.2.2 // indirect
3535
github.com/clipperhouse/displaywidth v0.11.0 // indirect

themes/themes.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"charm.land/bubbles/v2/spinner"
88
"charm.land/lipgloss/v2"
9+
"github.com/charmbracelet/x/term"
910
)
1011

1112
const (
@@ -22,10 +23,21 @@ func NewTheme(name string, cp ColorPalette) Theme {
2223
Name: name,
2324
SpinnerType: spinner.Points,
2425
Colors: &colors,
25-
isDark: lipgloss.HasDarkBackground(os.Stdin, os.Stdout),
26+
isDark: detectDarkBackground(),
2627
}
2728
}
2829

30+
// detectDarkBackground checks if the terminal has a dark background.
31+
// It only queries the terminal when both stdin and stdout are real TTYs,
32+
// avoiding hangs in CI environments and headless contexts where the
33+
// terminal won't respond to OSC escape sequences.
34+
func detectDarkBackground() bool {
35+
if !term.IsTerminal(os.Stdin.Fd()) || !term.IsTerminal(os.Stdout.Fd()) {
36+
return true // default to dark when not a real terminal
37+
}
38+
return lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
39+
}
40+
2941
type ThemeFunc func() Theme
3042

3143
func AllThemes() map[string]ThemeFunc {

0 commit comments

Comments
 (0)