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
+12 -3
View File
@@ -2,6 +2,7 @@ package ui
import (
"image/color"
"strings"
"testing"
"github.com/gdamore/tcell/v2"
@@ -31,6 +32,11 @@ func TestParseTerminalAnswers(t *testing.T) {
}
func TestUseTerminalBackground(t *testing.T) {
// tcell's terminal database also reads these from the real environment
// (and keeps what it added to an entry because of them, so the test uses
// a terminal nothing else looks up).
t.Setenv("COLORTERM", "")
t.Setenv("TCELL_TRUECOLOR", "")
defer func(old tcell.Color) { tview.Styles.PrimitiveBackgroundColor = old }(tview.Styles.PrimitiveBackgroundColor)
env := func(m map[string]string) func(string) string { return func(k string) string { return m[k] } }
dark := terminalInfo{bg: color.RGBA{30, 30, 46, 255}, hasBG: true}
@@ -44,13 +50,16 @@ func TestUseTerminalBackground(t *testing.T) {
want bool
}{
{"dark background, true color", dark, truecolor, true},
{"no true color", dark, env(map[string]string{"TERM": "xterm-256color"}), false},
{"no true color", dark, env(map[string]string{"TERM": "vt100"}), false},
{"ghostty without COLORTERM", dark, env(map[string]string{"TERM": "xterm-ghostty"}), true},
{"true color disabled for tcell", dark, env(map[string]string{"TERM": "xterm-ghostty", "TCELL_TRUECOLOR": "disable"}), false},
{"light background keeps the default", light, truecolor, false},
{"no answer", terminalInfo{}, truecolor, false},
} {
tview.Styles.PrimitiveBackgroundColor = tcell.ColorBlack
if got := useTerminalBackground(tc.info, tc.env); got != tc.want {
t.Errorf("%s: %v", tc.name, got)
note := useTerminalBackground(tc.info, tc.env)
if got := strings.Contains(note, "own color"); got != tc.want {
t.Errorf("%s: %s", tc.name, note)
}
want := tcell.ColorBlack
if tc.want {