Match the terminal's colours: auto from its background, and a terminal theme

auto now reads the background through OpenTUI's palette detector (OSC 11) and
falls back to dark. The new terminal theme builds the palette from the detected
background, foreground and ANSI colours, and shows in settings.
This commit is contained in:
Callum Alpass committed 2026-11-03 03:18:11 +11:00
1 parent c1a7376aa8
commit f18fc9c68d
7 files changed
+143 -12

No files matched your search

+8 -3
View File
@@ -162,9 +162,14 @@ async function runUi(attachNeedle?: string) {
setInterval(() => {
if (Date.now() - lastInput > 5_000) Bun.gc(true);
}, 30_000).unref();
const { applyTheme } = await import("./ui/theme.ts");
// Terminals that don't say what their background is get the dark palette.
applyTheme(config.theme === "auto" ? ((await renderer.waitForThemeMode(300)) ?? "dark") : config.theme);
const { applyTheme, terminalPalette, themeModeOf } = await import("./ui/theme.ts");
if (config.theme === "dark" || config.theme === "light") applyTheme(config.theme);
else {
// The terminal's colours through OSC 4, 10 and 11; terminals that don't answer get the dark palette.
const detected = await renderer.getPalette({ timeout: 300, size: 16 }).catch(() => null);
const mode = themeModeOf(detected) ?? renderer.themeMode ?? "dark";
applyTheme(config.theme === "terminal" ? (terminalPalette(detected) ?? mode) : mode);
}
createRoot(renderer).render(createElement(App, { session, config, layout, ...(attachThreadId ? { attachThreadId } : {}) }));
}
+3 -3
View File
@@ -18,8 +18,8 @@ export interface T3tuiConfig {
readonly notificationMode: "off" | "notifications" | "sound" | "notifications-and-sound";
/** As the web's in-app toasts: a line at the bottom for threads you aren't looking at. */
readonly inAppNotificationsEnabled: boolean;
/** auto follows the terminal's background colour. */
readonly theme: "auto" | "dark" | "light";
/** auto picks dark or light from the terminal's background; terminal builds the palette from its colours. */
readonly theme: "auto" | "dark" | "light" | "terminal";
}
export const DEFAULT_CONFIG: T3tuiConfig = { followUpBehavior: "steer", sendShortcut: "enter", timestampFormat: "locale", diffIgnoreWhitespace: true, notificationMode: "sound", inAppNotificationsEnabled: true, theme: "auto" };
@@ -32,7 +32,7 @@ export const CONFIG_CHOICES: { readonly [K in keyof T3tuiConfig]: readonly T3tui
diffIgnoreWhitespace: [true, false],
notificationMode: ["off", "sound", "notifications", "notifications-and-sound"],
inAppNotificationsEnabled: [true, false],
theme: ["auto", "dark", "light"],
theme: ["auto", "dark", "light", "terminal"],
};
export const configPath = () => join(configDir(), "config.json");
+1 -1
View File
@@ -21,7 +21,7 @@ const LABELS: { readonly [K in keyof T3tuiConfig]: { readonly label: string; rea
values: { off: "off", sound: "bell", notifications: "desktop notifications", "notifications-and-sound": "bell and desktop notifications" },
},
inAppNotificationsEnabled: { label: "In-app alerts", help: "a line at the bottom for other threads", values: { true: "on", false: "off" } },
theme: { label: "Colours", help: "from the next start", values: { auto: "match the terminal", dark: "dark", light: "light" } },
theme: { label: "Colours", help: "from the next start", values: { auto: "dark or light, as the terminal is", dark: "dark", light: "light", terminal: "the terminal's own colours" } },
};
/** t3tui's own settings, in display order, each cycled with enter. */
+75 -3
View File
@@ -1,4 +1,4 @@
import { type NativeResourceOwner, SyntaxStyle, type ThemeMode } from "@opentui/core";
import { type NativeResourceOwner, SyntaxStyle, type TerminalColors, type ThemeMode } from "@opentui/core";
import { useRenderer } from "@opentui/react";
import type { StatusTone } from "../model/threads.ts";
@@ -79,11 +79,83 @@ const LIGHT: Palette = {
/** The palette in use. Read at render time, so applyTheme before the first frame sets it everywhere. */
export const colors: Palette = { ...DARK };
export function applyTheme(mode: ThemeMode): void {
Object.assign(colors, mode === "light" ? LIGHT : DARK);
/** Sets the palette: the dark or light one, or one built with terminalPalette. */
export function applyTheme(theme: ThemeMode | Palette): void {
Object.assign(colors, theme === "light" ? LIGHT : theme === "dark" ? DARK : theme);
markdownStyles = new WeakMap();
}
/** Dark or light, by the background's luminance; null if the terminal didn't say. */
export function themeModeOf(terminal: TerminalColors | null): ThemeMode | null {
const background = rgb(terminal?.defaultBackground ?? null);
if (!background) return null;
const [r, g, b] = background;
return 0.2126 * r + 0.7152 * g + 0.0722 * b > 127 ? "light" : "dark";
}
/**
* The terminal's own colours as a palette: panels and raised blocks a few small steps off
* its background, text from its foreground, status hues from its ANSI colours. Null without
* a background and foreground; colours it didn't report come from the dark or light palette.
*/
export function terminalPalette(terminal: TerminalColors | null): Palette | null {
const mode = themeModeOf(terminal);
const bg = terminal?.defaultBackground;
const text = terminal?.defaultForeground;
if (!mode || !bg || !text || !rgb(text)) return null;
const base = mode === "light" ? LIGHT : DARK;
const ansi = (index: number, fallback: string) => (rgb(terminal.palette[index] ?? null) ? terminal.palette[index]! : fallback);
// Lighter steps on a dark background, darker on a light one.
const step = (amount: number) => mix(bg, mode === "dark" ? "#ffffff" : "#000000", amount);
const accent = ansi(12, ansi(4, base.accent));
const good = ansi(2, base.good);
const bad = ansi(1, base.bad);
const cyan = ansi(6, base.info);
return {
bg,
panel: step(0.04),
element: step(0.08),
elementHover: step(0.12),
border: mix(bg, text, 0.22),
borderFocused: accent,
text,
muted: mix(text, bg, 0.4),
faint: mix(text, bg, 0.6),
accent,
user: cyan,
userBg: step(0.08),
good,
bad,
warn: ansi(3, base.warn),
selectedBg: mix(bg, accent, 0.22),
hoverBg: step(0.06),
buttonBg: step(0.08),
info: cyan,
input: ansi(5, base.input),
plan: ansi(11, ansi(3, base.plan)),
diffAddedBg: mix(bg, good, 0.14),
diffRemovedBg: mix(bg, bad, 0.14),
diffGutterBg: step(0.03),
diffAddedGutterBg: mix(bg, good, 0.26),
diffRemovedGutterBg: mix(bg, bad, 0.26),
diffAddedSign: good,
diffRemovedSign: bad,
};
}
/** A `#rrggbb` colour's channels, or null for anything else. */
function rgb(hex: string | null): [number, number, number] | null {
const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex ?? "");
return match ? [Number.parseInt(match[1]!, 16), Number.parseInt(match[2]!, 16), Number.parseInt(match[3]!, 16)] : null;
}
/** `amount` of the way from one colour to another. */
function mix(from: string, to: string, amount: number): string {
const a = rgb(from)!;
const b = rgb(to)!;
return `#${a.map((channel, index) => Math.round(channel + (b[index]! - channel) * amount).toString(16).padStart(2, "0")).join("")}`;
}
const STATUS_COLORS: Record<StatusTone, keyof Palette> = { info: "info", muted: "muted", warn: "warn", input: "input", bad: "bad", good: "good" };
export const statusColor = (tone: StatusTone): string => colors[STATUS_COLORS[tone]];