The best way to control Claude Code, Codex and more from any computer or phone
fix(web): repaint terminal when programs change default colors (#37193)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 15, 2026 at 13:45 UTC
be8c4c6d9fdb2f14066447b158f9706f5a76e483
4 files changed
+141
-2
apps/web/src/terminal/ghostty/core.test.ts
+36
-1
@@ -248,7 +248,7 @@ describe("GhosttyTerminalCore snapshots", () => {
248
]);
249
});
250
251
- it("paints ANSI colors from the theme palette and repaints them on theme change", async () => {
251
+ it("paints ANSI colors from the theme palette and repaints them when the theme or a program changes colors", async () => {
252
const core = await createCore();
253
const theme = {
254
foreground: { r: 255, g: 255, b: 255 },
@@ -266,6 +266,41 @@ describe("GhosttyTerminalCore snapshots", () => {
266
cells = core.snapshot().rowData[0]!.cells;
267
expect(cells[0]!.foreground).toEqual({ r: 1, g: 9, b: 2 });
268
expect(cells[1]!.foreground).toEqual({ r: 255, g: 0, b: 0 });
269
+
270
+ // Inverse, default and explicit-background cells, cached before a program sets OSC 10/11.
271
+ core.resetAndWrite("\x1b[7mI\x1b[0mD\x1b[48;2;9;8;7mB\x1b[0m\r\n");
272
+ core.snapshot();
273
+ const colors = () => {
274
+ const { background, rowData } = core.snapshot();
275
+ return {
276
+ background,
277
+ cells: rowData[0]!.cells
278
+ .slice(0, 3)
279
+ .map(({ foreground, background }) => ({ foreground, background })),
280
+ };
281
+ };
282
+ const programForeground = { r: 17, g: 34, b: 51 };
283
+ const programBackground = { r: 68, g: 85, b: 102 };
284
+ const explicitBackground = { r: 9, g: 8, b: 7 };
285
+ core.write("\x1b]10;rgb:11/22/33\x07\x1b]11;rgb:44/55/66\x07");
286
+ expect(colors()).toEqual({
287
+ background: programBackground,
288
+ cells: [
289
+ { foreground: programBackground, background: programForeground },
290
+ { foreground: programForeground, background: programBackground },
291
+ { foreground: programForeground, background: explicitBackground },
292
+ ],
293
+ });
294
+
295
+ core.write("\x1b]110\x07\x1b]111\x07");
296
+ expect(colors()).toEqual({
297
+ background: theme.background,
298
+ cells: [
299
+ { foreground: theme.background, background: theme.foreground },
300
+ { foreground: theme.foreground, background: theme.background },
301
+ { foreground: theme.foreground, background: explicitBackground },
302
+ ],
303
+ });
304
});
305
306
it("preserves Unicode and split ANSI parser state across batched renderer reads", async () => {
apps/web/src/terminal/ghostty/core.ts
+8
-1
@@ -248,6 +248,8 @@ export class GhosttyTerminalCore {
248
private cellQuery = 0;
249
private scrollbar = 0;
250
private rows: GhosttyRow[] = [];
251
+ // Defaults the cached rows resolved; Ghostty marks no row dirty when OSC 10/11 change them.
252
+ private rowColors: Pick<GhosttySnapshot, "foreground" | "background"> | null = null;
253
private disposed = false;
254
private keyboardLayoutMap: GhosttyKeyboardLayoutMap | undefined;
255
@@ -865,9 +867,14 @@ export class GhosttyTerminalCore {
867
);
868
const cols = this.getU16(RENDER_DATA.cols);
869
const rowCount = this.getU16(RENDER_DATA.rows);
868
- const dirty = this.getU32(RENDER_DATA.dirty);
870
const foreground = this.getColor(RENDER_DATA.foreground, { r: 229, g: 231, b: 235 });
871
const background = this.getColor(RENDER_DATA.background, { r: 0, g: 0, b: 0 });
872
+ const colorsChanged =
873
+ this.rowColors !== null &&
874
+ (!sameColor(this.rowColors.foreground, foreground) ||
875
+ !sameColor(this.rowColors.background, background));
876
+ this.rowColors = { foreground, background };
877
+ const dirty = colorsChanged ? 2 : this.getU32(RENDER_DATA.dirty);
878
const cursorHasValue = this.getBool(RENDER_DATA.cursorHasValue);
879
const cursor = cursorHasValue ? this.getColor(RENDER_DATA.cursor, foreground) : foreground;
880
const cursorInViewport = this.getBool(RENDER_DATA.cursorInViewport);
apps/web/src/terminal/ghostty/surface.ts
+6
@@ -5,6 +5,7 @@ import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../term
5
import {
6
GHOSTTY_CELL_WIDE,
7
GhosttyTerminalCore,
8
+ ghosttyColorsEqual,
9
type GhosttyCell,
10
type GhosttyScrollbar,
11
type GhosttySnapshot,
@@ -2043,7 +2044,12 @@ export class GhosttyTerminalSurface {
2044
this.canvasConfigured = true;
2045
this.canvasDevicePixelRatio = canvasConfiguration.ratio;
2046
}
2047
+ const previousBackground = this.snapshot?.background;
2048
this.snapshot = this.core.snapshot();
2049
+ // Only a full render repaints the padding around the rows in the new default background.
2050
+ if (previousBackground && !ghosttyColorsEqual(previousBackground, this.snapshot.background)) {
2051
+ this.forceFullRender = true;
2052
+ }
2053
if (!this.blinkEnabled()) this.cursorOn = true;
2054
// Move the origin only on full repaint; dirty-row updates cannot mix origins.
2055
const scrollState = this.readScrollbarState();
scripts/scenarios/terminal-program-background-repaints-screen.e2e.ts
new
+91
@@ -0,0 +1,91 @@
1
+/// <reference lib="dom" />
2
+// The DOM lib types the function this scenario runs in the page.
3
+import { expect } from "e2e";
4
+import * as Effect from "effect/Effect";
5
+import * as FileSystem from "effect/FileSystem";
6
+
7
+import { runPromise, test, WEB } from "./t3.ts";
8
+
9
+// A raw-mode process prints rows, then on `b` sets the default background with OSC 11.
10
+const PROGRAM = String.raw`import { writeFileSync } from "node:fs";
11
+
12
+const state = new URL("state", import.meta.url);
13
+process.stdin.setRawMode(true);
14
+process.stdout.write("\x1b[2J\x1b[HROW ONE\r\nROW TWO\r\nROW THREE\r\nROW FOUR\r\n");
15
+writeFileSync(state, "ready");
16
+process.stdin.on("data", (chunk) => {
17
+ const key = chunk.toString();
18
+ if (key === "b") {
19
+ process.stdout.write("\x1b]11;rgb:44/55/66\x07", () => writeFileSync(state, "changed"));
20
+ }
21
+ if (key === "x") {
22
+ process.stdin.setRawMode(false);
23
+ process.exit(0);
24
+ }
25
+});
26
+`;
27
+
28
+test(
29
+ "A program setting the terminal's default background repaints the whole screen",
30
+ WEB,
31
+ async ({ t3, app, browser, screen }) => {
32
+ await browser.emulateMedia({ colorScheme: "dark" });
33
+ const project = await t3.project("colors", { "program.mjs": PROGRAM });
34
+ const thread = await t3.thread({ project, title: "Colors", messages: ["hello"] });
35
+ const read = (file: string) =>
36
+ runPromise(
37
+ Effect.gen(function* () {
38
+ const fs = yield* FileSystem.FileSystem;
39
+ return yield* fs.readFileString(`${project}/${file}`);
40
+ }).pipe(Effect.orElseSucceed(() => "")),
41
+ );
42
+ await t3.open(thread);
43
+ await screen.getByRole("button", "Toggle terminal drawer").tap();
44
+ const terminal = screen.getByLabel("Terminal input");
45
+
46
+ // Input sent before the shell runs is dropped, so retype until it lands.
47
+ await expect
48
+ .poll(
49
+ async () => {
50
+ await terminal.focus();
51
+ await browser.keyboard.type(`echo ready > '${project}/shell-ready'`);
52
+ await browser.keyboard.press("Enter");
53
+ return read("shell-ready");
54
+ },
55
+ { timeout: 30_000 },
56
+ )
57
+ .toBe("ready\n");
58
+
59
+ await terminal.focus();
60
+ await browser.keyboard.type(`'${process.execPath}' '${project}/program.mjs'`);
61
+ await browser.keyboard.press("Enter");
62
+ await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("ready");
63
+ await app.screenshot("before-background");
64
+
65
+ await terminal.focus();
66
+ await browser.keyboard.type("b");
67
+ await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("changed");
68
+ await app.screenshot("after-background");
69
+
70
+ // Sample the padding above the rows, a cell beside the printed rows, and the canvas bottom.
71
+ const pixels = await browser.evaluate(() => {
72
+ const canvas = document
73
+ .querySelector('[aria-label="Terminal input"]')!
74
+ .parentElement!.querySelector("canvas")!;
75
+ const context = canvas.getContext("2d")!;
76
+ const at = (x: number, y: number) => [...context.getImageData(x, y, 1, 1).data];
77
+ return {
78
+ padding: at(canvas.width - 2, 1),
79
+ row: at(canvas.width - 20, Math.floor(canvas.height / 2)),
80
+ bottom: at(canvas.width - 2, canvas.height - 1),
81
+ };
82
+ });
83
+ await browser.keyboard.type("x");
84
+ const programBackground = [68, 85, 102, 255];
85
+ expect(pixels).toEqual({
86
+ padding: programBackground,
87
+ row: programBackground,
88
+ bottom: programBackground,
89
+ });
90
+ },
91
+);