Skip to content

Commit 65bca58

Browse files
committed
improve dark mode detection
1 parent 6f29ef6 commit 65bca58

1 file changed

Lines changed: 51 additions & 37 deletions

File tree

lh/colorized.go

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ type Palette struct {
6464

6565
// darkPalette defines colors optimized for dark terminal backgrounds.
6666
var darkPalette = Palette{
67-
Header: "\033[1;31m",
68-
Goroutine: "\033[1;36m",
69-
Func: "\033[97m",
70-
Path: "\033[38;5;245m",
71-
FileLine: "\033[38;5;111m",
67+
Header: "\033[1;38;5;203m", // Brighter red
68+
Goroutine: "\033[1;38;5;51m", // Bright cyan
69+
Func: "\033[1;97m", // Bright white
70+
Path: "\033[38;5;110m", // Brighter gray-blue
71+
FileLine: "\033[38;5;117m", // Bright blue
7272
Reset: "\033[0m",
7373
Title: "\033[38;5;245m",
7474
Pos: "\033[38;5;117m",
@@ -80,14 +80,14 @@ var darkPalette = Palette{
8080
Error: "\033[31m",
8181
Fatal: "\033[1;31m",
8282

83-
// Field type colors
84-
Key: "\033[38;5;117m",
85-
Number: "\033[38;5;141m",
86-
String: "\033[38;5;223m",
87-
Bool: "\033[38;5;85m",
88-
Time: "\033[38;5;110m",
89-
Nil: "\033[38;5;243m",
90-
Default: "\033[38;5;250m",
83+
// Field type colors - made brighter for dark backgrounds
84+
Key: "\033[38;5;117m", // Brighter blue
85+
Number: "\033[38;5;141m", // Brighter purple
86+
String: "\033[38;5;223m", // Brighter yellow/orange
87+
Bool: "\033[38;5;85m", // Brighter green
88+
Time: "\033[38;5;110m", // Brighter cyan-blue
89+
Nil: "\033[38;5;243m", // Slightly brighter gray
90+
Default: "\033[38;5;250m", // Brighter gray
9191

9292
// JSON and Inspect colors
9393
JSONKey: "\033[38;5;117m",
@@ -320,6 +320,24 @@ func WithColorIntensity(intensity ColorIntensity) ColorOption {
320320
}
321321
}
322322

323+
// WithColorTheme configures the ColorizedHandler to use a specific color theme based on the provided theme name.
324+
func WithColorTheme(theme string) ColorOption {
325+
return func(c *ColorizedHandler) {
326+
switch strings.ToLower(theme) {
327+
case "light":
328+
c.palette = lightPalette
329+
case "dark":
330+
c.palette = darkPalette
331+
case "bright":
332+
c.palette = brightPalette
333+
case "pastel":
334+
c.palette = pastelPalette
335+
case "vibrant":
336+
c.palette = vibrantPalette
337+
}
338+
}
339+
}
340+
323341
// NewColorizedHandler creates a new ColorizedHandler writing to the specified writer.
324342
func NewColorizedHandler(w io.Writer, opts ...ColorOption) *ColorizedHandler {
325343
c := &ColorizedHandler{
@@ -967,39 +985,35 @@ func (h *ColorizedHandler) detectPalette() Palette {
967985
}
968986
}
969987

970-
var basePalette Palette
971-
972-
if bg, ok := os.LookupEnv("TERM_BACKGROUND"); ok {
973-
if bg == "light" {
974-
basePalette = lightPalette
975-
} else {
976-
basePalette = darkPalette
977-
}
978-
} else if fgBg, ok := os.LookupEnv("COLORFGBG"); ok {
988+
// First, try to detect background color
989+
isDarkBackground := true // Default to dark
990+
991+
// Check for common dark/light environment variables
992+
if style, ok := os.LookupEnv("AppleInterfaceStyle"); ok && strings.EqualFold(style, "dark") {
993+
isDarkBackground = true
994+
} else if style, ok := os.LookupEnv("APPEARANCE"); ok && strings.EqualFold(style, "light") {
995+
isDarkBackground = false
996+
} else if bg := os.Getenv("TERM_BACKGROUND"); bg != "" {
997+
isDarkBackground = strings.ToLower(bg) != "light"
998+
} else if fgBg := os.Getenv("COLORFGBG"); fgBg != "" {
999+
// COLORFGBG format: "foreground;background" or "foreground;background;unused"
9791000
parts := strings.Split(fgBg, ";")
9801001
if len(parts) >= 2 {
9811002
bg := parts[len(parts)-1]
982-
9831003
bgInt, err := strconv.Atoi(bg)
9841004
if err == nil {
985-
if bgInt >= 0 && bgInt <= 7 || bgInt == 15 {
986-
basePalette = lightPalette
987-
} else {
988-
basePalette = darkPalette
989-
}
990-
} else {
991-
basePalette = darkPalette
1005+
// According to XTerm documentation:
1006+
// 0-7: dark colors, 15: white, 8-14: bright colors
1007+
// Typically, 0=black (dark), 7=light gray (light), 15=white (light)
1008+
isDarkBackground = (bgInt >= 0 && bgInt <= 6) || (bgInt >= 8 && bgInt <= 14)
9921009
}
993-
} else {
994-
basePalette = darkPalette
9951010
}
996-
} else if style, ok := os.LookupEnv("AppleInterfaceStyle"); ok && strings.EqualFold(style, "dark") {
997-
basePalette = darkPalette
998-
} else {
999-
basePalette = darkPalette
10001011
}
10011012

1002-
return h.applyIntensity(basePalette)
1013+
if isDarkBackground {
1014+
return h.applyIntensity(darkPalette)
1015+
}
1016+
return h.applyIntensity(lightPalette)
10031017
}
10041018

10051019
// applyIntensity applies the intensity setting to a base palette

0 commit comments

Comments
 (0)