Terminal background: decide 24-bit color like tcell, and log why

The check only looked at COLORTERM, but tcell also draws 24-bit color for
terminals whose description has it (xterm-ghostty, xterm-kitty), so in
Ghostty on macOS without COLORTERM the background was never used. Use
tcell's rules, and always log which background is used and why.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019reK4DUGYqDUjniWYJvwUW
This commit is contained in:
2026-10-21 11:53:47 +02:00
co-authored by Claude Opus 5.5
parent 0270462fe4
commit d2e868293f
5 changed files with 48 additions and 18 deletions
+31 -9
View File
@@ -2,6 +2,7 @@ package ui
import (
"bytes"
"fmt"
"image/color"
"strconv"
"strings"
@@ -9,6 +10,7 @@ import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/terminfo"
"github.com/rivo/tview"
)
@@ -67,22 +69,42 @@ func parseTerminalAnswers(got []byte) (info terminalInfo) {
// on which grey text almost disappears). Only for dark backgrounds: the text
// colors are made for those. Needs true color, else the exact color cannot be
// drawn. Must run before any widget is created, they copy the style. It
// reports whether it changed anything.
func useTerminalBackground(info terminalInfo, env func(string) string) bool {
if !info.hasBG || !hasTrueColor(env) || luminance(info.bg) > 0.4 {
return false
// returns a line for the log that says what was decided and why.
func useTerminalBackground(info terminalInfo, env func(string) string) string {
if !info.hasBG {
return "Background: palette color 0 (the terminal did not report its background color)"
}
hex := fmt.Sprintf("#%02x%02x%02x", info.bg.R, info.bg.G, info.bg.B)
switch {
case !hasTrueColor(env):
return "Background: palette color 0 (the terminal's " + hex + " needs 24-bit color, which " + env("TERM") + " does not have; COLORTERM=truecolor enables it)"
case luminance(info.bg) > 0.4:
return "Background: palette color 0 (the terminal's " + hex + " is light; the text colors are made for dark backgrounds)"
}
tview.Styles.PrimitiveBackgroundColor = tcell.NewRGBColor(int32(info.bg.R), int32(info.bg.G), int32(info.bg.B))
return true
return "Background: the terminal's own color " + hex
}
// hasTrueColor reports whether the terminal says it draws 24-bit colors.
// hasTrueColor reports whether tcell will draw 24-bit colors, by the same
// rules: TCELL_TRUECOLOR, COLORTERM, or a terminal description with direct
// color (e.g. xterm-ghostty and xterm-kitty, also without COLORTERM).
func hasTrueColor(env func(string) string) bool {
switch strings.ToLower(env("COLORTERM")) {
case "truecolor", "24bit":
switch env("TCELL_TRUECOLOR") {
case "":
case "disable":
return false
default:
return true
}
return strings.HasSuffix(env("TERM"), "-direct")
switch strings.ToLower(env("COLORTERM")) {
case "truecolor", "24bit", "24-bit":
return true
}
if strings.HasSuffix(env("TERM"), "-direct") {
return true
}
ti, err := terminfo.LookupTerminfo(env("TERM"))
return err == nil && (ti.TrueColor || ti.SetFgBgRGB != "")
}
// luminance is the relative brightness of c, from 0 (black) to 1 (white).