fix(coding-agent): reduce automatic theme detection delay

This commit is contained in:
Armin Ronacher 2026-08-07 00:21:32 +02:00
parent 4688e4f92f
commit 7cb2a51c05
3 changed files with 53 additions and 3 deletions

View file

@ -9,6 +9,7 @@
### Changed
- Softened the bash tool's `PI_*` environment guideline in an attempt to reduce unnecessary inspection commands ([#7128](https://github.com/earendil-works/pi/issues/7128)).
- Reduced worst-case automatic terminal theme detection delay from 200 ms to 100 ms by probing color-scheme and background support concurrently.
### Fixed

View file

@ -793,13 +793,21 @@ export async function detectTerminalThemeForAuto({
timeoutMs,
env,
}: TerminalAutoThemeDetectionOptions): Promise<TerminalTheme> {
let colorSchemePromise: Promise<TerminalTheme | undefined> | undefined;
try {
const colorScheme = await ui.queryTerminalColorScheme?.({ timeoutMs });
colorSchemePromise = ui.queryTerminalColorScheme?.({ timeoutMs });
} catch {
// Fall back to OSC 11 / COLORFGBG detection when starting the color-scheme query fails.
}
const backgroundThemePromise = detectTerminalBackgroundTheme({ ui, timeoutMs, env });
try {
const colorScheme = await colorSchemePromise;
if (colorScheme) return colorScheme;
} catch {
// Fall back to OSC 11 / COLORFGBG detection when color-scheme DSR is unsupported.
// Fall back to the concurrently queried OSC 11 / COLORFGBG detection.
}
return (await detectTerminalBackgroundTheme({ ui, timeoutMs, env })).theme;
return (await backgroundThemePromise).theme;
}
export function getDefaultTheme(): string {

View file

@ -3,6 +3,7 @@ import { afterEach, describe, expect, it } from "vitest";
import {
detectTerminalBackgroundFromEnv,
detectTerminalBackgroundTheme,
detectTerminalThemeForAuto,
getThemeByName,
getThemeForRgbColor,
parseAutoThemeSetting,
@ -99,6 +100,46 @@ describe("detectTerminalBackgroundTheme", () => {
});
});
describe("detectTerminalThemeForAuto", () => {
it("starts both queries and returns the preferred color-scheme result without waiting", async () => {
let resolveColorScheme!: (theme: "dark" | "light" | undefined) => void;
let backgroundQueryStarted = false;
const detection = detectTerminalThemeForAuto({
timeoutMs: 100,
ui: {
queryTerminalColorScheme: () =>
new Promise((resolve) => {
resolveColorScheme = resolve;
}),
queryTerminalBackgroundColor: () => {
backgroundQueryStarted = true;
return new Promise<RgbColor | undefined>(() => {});
},
},
});
expect(backgroundQueryStarted).toBe(true);
resolveColorScheme("dark");
await expect(detection).resolves.toBe("dark");
});
it("uses the background result when the color-scheme query fails", async () => {
await expect(
detectTerminalThemeForAuto({
timeoutMs: 100,
ui: {
async queryTerminalColorScheme(): Promise<undefined> {
throw new Error("color-scheme query failed");
},
async queryTerminalBackgroundColor(): Promise<RgbColor> {
return { r: 250, g: 250, b: 250 };
},
},
}),
).resolves.toBe("light");
});
});
describe("theme color mode", () => {
it("uses terminal capabilities", () => {
setCapabilities({ images: null, trueColor: false, hyperlinks: false });