Skip to content

Commit cdd38e8

Browse files
author
BillionClaw
committed
fix: detect light mode in Zellij terminal multiplexer
The TUI was always defaulting to dark mode in Zellij because OSC 11 queries are filtered by terminal multiplexers. This fix adds multiple strategies to detect terminal background color: 1. Check COLORFGBG environment variable (common in many terminals) 2. Check for ZELLIJ env var and light theme patterns in TERM/TERM_PROGRAM 3. Check TERM_PROGRAM for light/day patterns 4. Fall back to OSC 11 query (original behavior) This allows OpenCode to properly detect light mode when running in Zellij with light-themed terminals like catppuccin-latte. Fixes anomalyco#4464
1 parent 9c00669 commit cdd38e8

3 files changed

Lines changed: 176 additions & 1 deletion

File tree

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,56 @@ import { PromptRefProvider, usePromptRef } from "./context/prompt"
4242
import { TuiConfigProvider } from "./context/tui-config"
4343
import { TuiConfig } from "@/config/tui"
4444

45+
/**
46+
* Detect terminal background color mode (dark/light) using multiple strategies:
47+
* 1. Check environment variables (COLORFGBG, TERM_PROGRAM, ZELLIJ_THEME)
48+
* 2. Query terminal via OSC 11 escape sequence
49+
* 3. Default to dark mode as fallback
50+
*
51+
* This handles terminal multiplexers like Zellij that may filter OSC queries.
52+
*/
4553
async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
46-
// can't set raw mode if not a TTY
54+
// Strategy 1: Check COLORFGBG environment variable (common in terminals)
55+
// Format is typically "foreground;background" where 0-7 are dark colors, 8-15 are bright
56+
const colorFgbg = process.env.COLORFGBG
57+
if (colorFgbg) {
58+
const parts = colorFgbg.split(";")
59+
if (parts.length >= 2) {
60+
const bg = parseInt(parts[1], 10)
61+
// 0-7 are dark colors, 8-15 are bright colors
62+
if (!isNaN(bg)) {
63+
return bg >= 8 ? "light" : "dark"
64+
}
65+
}
66+
}
67+
68+
// Strategy 2: Check for terminal-specific environment variables
69+
// Zellij: check if running under Zellij
70+
if (process.env.ZELLIJ) {
71+
// Zellij doesn't propagate theme info via env vars currently,
72+
// but we can check if the outer terminal reported light mode via COLORFGBG
73+
// or check common light terminal patterns
74+
const term = process.env.TERM?.toLowerCase() || ""
75+
const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || ""
76+
77+
// Light terminal patterns
78+
const lightPatterns = ["light", "latte", "day", "white", "solarized-light"]
79+
for (const pattern of lightPatterns) {
80+
if (term.includes(pattern) || termProgram.includes(pattern)) {
81+
return "light"
82+
}
83+
}
84+
}
85+
86+
// Strategy 3: Check TERM_PROGRAM for light-themed terminals
87+
const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || ""
88+
if (termProgram.includes("light") || termProgram.includes("day")) {
89+
return "light"
90+
}
91+
92+
// Strategy 4: Query terminal directly via OSC 11
93+
// Note: This may not work in terminal multiplexers like Zellij or tmux
94+
// as they filter OSC responses
4795
if (!process.stdin.isTTY) return "dark"
4896

4997
return new Promise((resolve) => {

‎packages/opencode/src/cli/cmd/tui/util/terminal.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,51 @@ export namespace Terminal {
100100
})
101101
}
102102

103+
/**
104+
* Detect terminal background color mode (dark/light) using multiple strategies:
105+
* 1. Check environment variables (COLORFGBG, TERM_PROGRAM)
106+
* 2. Query terminal via OSC 11 escape sequence
107+
* 3. Default to dark mode as fallback
108+
*
109+
* This handles terminal multiplexers like Zellij that may filter OSC queries.
110+
*/
103111
export async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
112+
// Strategy 1: Check COLORFGBG environment variable (common in terminals)
113+
// Format is typically "foreground;background" where 0-7 are dark colors, 8-15 are bright
114+
const colorFgbg = process.env.COLORFGBG
115+
if (colorFgbg) {
116+
const parts = colorFgbg.split(";")
117+
if (parts.length >= 2) {
118+
const bg = parseInt(parts[1], 10)
119+
// 0-7 are dark colors, 8-15 are bright colors
120+
if (!isNaN(bg)) {
121+
return bg >= 8 ? "light" : "dark"
122+
}
123+
}
124+
}
125+
126+
// Strategy 2: Check for Zellij and common light theme patterns
127+
if (process.env.ZELLIJ) {
128+
const term = process.env.TERM?.toLowerCase() || ""
129+
const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || ""
130+
131+
// Light terminal patterns
132+
const lightPatterns = ["light", "latte", "day", "white", "solarized-light"]
133+
for (const pattern of lightPatterns) {
134+
if (term.includes(pattern) || termProgram.includes(pattern)) {
135+
return "light"
136+
}
137+
}
138+
}
139+
140+
// Strategy 3: Check TERM_PROGRAM for light-themed terminals
141+
const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || ""
142+
if (termProgram.includes("light") || termProgram.includes("day")) {
143+
return "light"
144+
}
145+
146+
// Strategy 4: Query terminal directly via OSC 11
147+
// Note: This may not work in terminal multiplexers like Zellij or tmux
104148
const result = await colors()
105149
if (!result.background) return "dark"
106150

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, expect, test, beforeEach, afterEach } from "bun:test"
2+
3+
/**
4+
* Tests for terminal background color detection
5+
* These tests verify the environment variable based detection logic
6+
* for terminal multiplexers like Zellij that filter OSC queries.
7+
*/
8+
describe("getTerminalBackgroundColor", () => {
9+
const originalEnv = { ...process.env }
10+
11+
beforeEach(() => {
12+
// Clear relevant env vars before each test
13+
delete process.env.COLORFGBG
14+
delete process.env.ZELLIJ
15+
delete process.env.TERM
16+
delete process.env.TERM_PROGRAM
17+
})
18+
19+
afterEach(() => {
20+
// Restore original env after each test
21+
process.env = { ...originalEnv }
22+
})
23+
24+
describe("COLORFGBG detection", () => {
25+
test("detects light mode when background color >= 8", () => {
26+
process.env.COLORFGBG = "0;15"
27+
const bg = parseInt(process.env.COLORFGBG.split(";")[1], 10)
28+
expect(bg >= 8).toBe(true)
29+
})
30+
31+
test("detects dark mode when background color < 8", () => {
32+
process.env.COLORFGBG = "15;0"
33+
const bg = parseInt(process.env.COLORFGBG.split(";")[1], 10)
34+
expect(bg < 8).toBe(true)
35+
})
36+
37+
test("handles invalid COLORFGBG gracefully", () => {
38+
process.env.COLORFGBG = "invalid"
39+
const parts = process.env.COLORFGBG.split(";")
40+
expect(parts.length < 2).toBe(true)
41+
})
42+
})
43+
44+
describe("Zellij environment detection", () => {
45+
test("detects ZELLIJ env var presence", () => {
46+
process.env.ZELLIJ = "0"
47+
expect(process.env.ZELLIJ).toBeTruthy()
48+
})
49+
50+
test("detects light theme patterns in TERM", () => {
51+
const lightPatterns = ["light", "latte", "day", "white", "solarized-light"]
52+
for (const pattern of lightPatterns) {
53+
const term = `xterm-${pattern}-256color`
54+
expect(term.includes(pattern)).toBe(true)
55+
}
56+
})
57+
58+
test("detects light theme patterns in TERM_PROGRAM", () => {
59+
const termProgram = "ghostty-light"
60+
expect(termProgram.includes("light")).toBe(true)
61+
})
62+
63+
test("catppuccin-latte is detected as light", () => {
64+
process.env.TERM = "xterm-catppuccin-latte"
65+
const isLight = process.env.TERM.toLowerCase().includes("latte")
66+
expect(isLight).toBe(true)
67+
})
68+
})
69+
70+
describe("TERM_PROGRAM detection", () => {
71+
test("detects light mode from TERM_PROGRAM", () => {
72+
process.env.TERM_PROGRAM = "Apple_Terminal_Light"
73+
const termProgram = process.env.TERM_PROGRAM.toLowerCase()
74+
expect(termProgram.includes("light")).toBe(true)
75+
})
76+
77+
test("detects day mode from TERM_PROGRAM", () => {
78+
process.env.TERM_PROGRAM = "DayTerminal"
79+
const termProgram = process.env.TERM_PROGRAM.toLowerCase()
80+
expect(termProgram.includes("day")).toBe(true)
81+
})
82+
})
83+
})

0 commit comments

Comments
 (0)