Skip to content

Commit 9a3070b

Browse files
authored
feat(web): improve terminal scrollback navigation and snapshots (#17091)
1 parent 12069ee commit 9a3070b

8 files changed

Lines changed: 429 additions & 59 deletions

File tree

‎apps/web/src/components/ThreadTerminalDrawer.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,29 @@ describe("terminal selection menus", () => {
2424
"add-to-chat",
2525
"copy",
2626
"paste",
27+
"select-all",
28+
"scroll-to-bottom",
2729
]);
2830

2931
expect(terminalSelectionMenuItems({ canAddToChat: false }).map(({ id }) => id)).toEqual([
3032
"copy",
3133
]);
3234
expect(
3335
terminalContextMenuItems({ hasSelection: true, canAddToChat: false }).map(({ id }) => id),
34-
).toEqual(["copy", "paste"]);
36+
).toEqual(["copy", "paste", "select-all", "scroll-to-bottom"]);
37+
});
38+
39+
it("keeps local viewing actions available to a read-only observer", () => {
40+
const items = terminalContextMenuItems({ hasSelection: false, readOnly: true });
41+
expect(items.filter((item) => item.disabled).map((item) => item.id)).toEqual([
42+
"add-to-chat",
43+
"copy",
44+
"paste",
45+
]);
46+
expect(items.filter((item) => !item.disabled).map((item) => item.id)).toEqual([
47+
"select-all",
48+
"scroll-to-bottom",
49+
]);
3550
});
3651
});
3752

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,12 @@ export function terminalSelectionLineRange(position: {
265265
};
266266
}
267267

268-
export type TerminalContextMenuAction = "add-to-chat" | "copy" | "paste";
268+
export type TerminalContextMenuAction =
269+
| "add-to-chat"
270+
| "copy"
271+
| "paste"
272+
| "select-all"
273+
| "scroll-to-bottom";
269274

270275
/** Post-selection popup: available selection actions, always enabled. */
271276
export function terminalSelectionMenuItems(options?: {
@@ -280,8 +285,8 @@ export function terminalSelectionMenuItems(options?: {
280285
}
281286

282287
/**
283-
* Right-click menu for the terminal canvas: the selection actions (disabled
284-
* until a selection exists) plus Paste. Paste is always offered: the browser
288+
* Right-click menu for selection, clipboard, and local scrollback actions.
289+
* Paste is always offered: the browser
285290
* (and Electron's default editing menu) can only paste into an editable
286291
* element, so a canvas terminal never gets a usable entry from them.
287292
*/
@@ -297,6 +302,8 @@ export function terminalContextMenuItems(options: {
297302
disabled: !hasSelection,
298303
})),
299304
{ id: "paste", label: "Paste", ...(options.readOnly ? { disabled: true } : {}) },
305+
{ id: "select-all", label: "Select all" },
306+
{ id: "scroll-to-bottom", label: "Jump to latest" },
300307
];
301308
}
302309

@@ -750,6 +757,14 @@ export function TerminalViewport({
750757
case "paste":
751758
await pasteFromClipboard(requestId);
752759
return;
760+
case "select-all":
761+
terminalRef.current?.selectAll();
762+
focusIfCurrent(requestId);
763+
return;
764+
case "scroll-to-bottom":
765+
terminalRef.current?.scrollToBottom();
766+
focusIfCurrent(requestId);
767+
return;
753768
}
754769
};
755770

‎apps/web/src/terminal/ghostty/core.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ describe("GhosttyTerminalCore snapshots", () => {
154154
});
155155
});
156156

157+
it("resolves foreground and background independently without retaining prior cell colors", async () => {
158+
const core = await createCore();
159+
core.write(
160+
"\x1b[38;2;123;45;67mF\x1b[39;48;2;9;8;7mB\x1b[0mD\x1b[38;2;12;34;56;48;2;78;90;123mC\x1b[0mE",
161+
);
162+
expect(
163+
core
164+
.snapshot()
165+
.rowData[0]!.cells.slice(0, 6)
166+
.map(({ text, foreground, background }) => ({ text, foreground, background })),
167+
).toEqual([
168+
{ text: "F", foreground: { r: 123, g: 45, b: 67 }, background: { r: 0, g: 0, b: 0 } },
169+
{ text: "B", foreground: { r: 255, g: 255, b: 255 }, background: { r: 9, g: 8, b: 7 } },
170+
{ text: "D", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
171+
{ text: "C", foreground: { r: 12, g: 34, b: 56 }, background: { r: 78, g: 90, b: 123 } },
172+
{ text: "E", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
173+
{ text: "", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
174+
]);
175+
});
176+
157177
it("reuses a grown grapheme buffer and releases it on disposal", async () => {
158178
const core = await createCore();
159179
const runtime = await loadGhosttyRuntime();

‎apps/web/src/terminal/ghostty/core.ts‎

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ const CELL_DATA = {
4747
selected: 7,
4848
} as const;
4949

50+
// wasm32 arrays for the official row_cells_get_multi API. Optional colors
51+
// come last because an unset color stops the query at that field.
52+
const CELL_QUERY = {
53+
keys: 0,
54+
values: 20,
55+
graphemesLength: 40,
56+
selected: 44,
57+
background: 48,
58+
foreground: 52,
59+
written: 56,
60+
size: 60,
61+
} as const;
62+
5063
const RAW_CELL_DATA = {
5164
wide: 3,
5265
} as const;
@@ -210,6 +223,7 @@ export class GhosttyTerminalCore {
210223
private graphemes = 0;
211224
private graphemeCapacity = 0;
212225
private style = 0;
226+
private cellQuery = 0;
213227
private scrollbar = 0;
214228
private rows: GhosttyRow[] = [];
215229
private disposed = false;
@@ -310,6 +324,19 @@ export class GhosttyTerminalCore {
310324
const styleSize = this.runtime.layout("GhosttyStyle").size;
311325
this.style = this.runtime.alloc(styleSize);
312326
this.runtime.setField(this.style, "GhosttyStyle", "size", styleSize);
327+
this.cellQuery = this.runtime.alloc(CELL_QUERY.size);
328+
const queryView = this.runtime.memoryDataView();
329+
const cellFields = [
330+
[CELL_DATA.style, this.style],
331+
[CELL_DATA.graphemesLength, this.cellQuery + CELL_QUERY.graphemesLength],
332+
[CELL_DATA.selected, this.cellQuery + CELL_QUERY.selected],
333+
[CELL_DATA.background, this.cellQuery + CELL_QUERY.background],
334+
[CELL_DATA.foreground, this.cellQuery + CELL_QUERY.foreground],
335+
] as const;
336+
for (const [index, [key, pointer]] of cellFields.entries()) {
337+
queryView.setUint32(this.cellQuery + CELL_QUERY.keys + index * 4, key, true);
338+
queryView.setUint32(this.cellQuery + CELL_QUERY.values + index * 4, pointer, true);
339+
}
313340
this.scrollbar = this.runtime.alloc(this.runtime.layout("GhosttyTerminalScrollbar").size);
314341
this.setTheme(theme);
315342
this.resize(cols, rows, cellWidth, cellHeight);
@@ -658,17 +685,26 @@ export class GhosttyTerminalCore {
658685
}
659686
}
660687

661-
selectAll(): void {
688+
selectAll(): GhosttySelectionRange["screen"] | null {
662689
this.ensureActive();
663690
const layout = this.runtime.layout("GhosttySelection");
664691
const selection = this.runtime.alloc(layout.size);
665-
this.runtime.setField(selection, "GhosttySelection", "size", layout.size);
666-
if (
667-
this.runtime.call("ghostty_terminal_select_all", this.terminal, selection) === GHOSTTY_SUCCESS
668-
) {
692+
try {
693+
this.runtime.setField(selection, "GhosttySelection", "size", layout.size);
694+
if (
695+
this.runtime.call("ghostty_terminal_select_all", this.terminal, selection) !==
696+
GHOSTTY_SUCCESS
697+
) {
698+
return null;
699+
}
700+
const start = this.pointFromGridRef(selection + layout.fields.start!.offset, 2);
701+
const end = this.pointFromGridRef(selection + layout.fields.end!.offset, 2);
702+
if (!start || !end) return null;
669703
this.runtime.call("ghostty_terminal_set", this.terminal, 21, selection);
704+
return { start, end };
705+
} finally {
706+
this.runtime.free(selection, layout.size);
670707
}
671-
this.runtime.free(selection, layout.size);
672708
}
673709

674710
selectWord(col: number, row: number): GhosttySelectionRange | null {
@@ -877,6 +913,7 @@ export class GhosttyTerminalCore {
877913
this.runtime.call("ghostty_terminal_free", this.terminal);
878914
}
879915
if (this.style) this.runtime.free(this.style, this.runtime.layout("GhosttyStyle").size);
916+
if (this.cellQuery) this.runtime.free(this.cellQuery, CELL_QUERY.size);
880917
if (this.scrollbar) {
881918
this.runtime.free(this.scrollbar, this.runtime.layout("GhosttyTerminalScrollbar").size);
882919
}
@@ -954,17 +991,29 @@ export class GhosttyTerminalCore {
954991
cells.length < cols &&
955992
this.runtime.call("ghostty_render_state_row_cells_next", cellsIterator) !== 0
956993
) {
957-
let foreground = this.getCellColor(cellsIterator, CELL_DATA.foreground, defaultForeground);
958-
let background = this.getCellColor(cellsIterator, CELL_DATA.background, defaultBackground);
959994
this.runtime.bytes(this.style, styleSize).fill(0);
960995
this.runtime.setField(this.style, "GhosttyStyle", "size", styleSize);
961-
this.runtime.call(
962-
"ghostty_render_state_row_cells_get",
996+
const result = this.runtime.call(
997+
"ghostty_render_state_row_cells_get_multi",
963998
cellsIterator,
964-
CELL_DATA.style,
965-
this.style,
999+
5,
1000+
this.cellQuery + CELL_QUERY.keys,
1001+
this.cellQuery + CELL_QUERY.values,
1002+
this.cellQuery + CELL_QUERY.written,
9661003
);
967-
const graphemeLength = this.getCellU32(cellsIterator, CELL_DATA.graphemesLength);
1004+
const queryView = this.runtime.memoryDataView();
1005+
const written = queryView.getUint32(this.cellQuery + CELL_QUERY.written, true);
1006+
if (written < 3) this.assertSuccess("ghostty_render_state_row_cells_get_multi", result);
1007+
const graphemeLength = queryView.getUint32(this.cellQuery + CELL_QUERY.graphemesLength, true);
1008+
const selected = queryView.getUint8(this.cellQuery + CELL_QUERY.selected) !== 0;
1009+
let background =
1010+
written >= 4 ? this.readColor(this.cellQuery + CELL_QUERY.background) : defaultBackground;
1011+
let foreground =
1012+
written === 5
1013+
? this.readColor(this.cellQuery + CELL_QUERY.foreground)
1014+
: written === 3
1015+
? this.getCellColor(cellsIterator, CELL_DATA.foreground, defaultForeground)
1016+
: defaultForeground;
9681017
let text = "";
9691018
if (graphemeLength > 0) {
9701019
const bufferSize = graphemeLength * 4;
@@ -1008,26 +1057,25 @@ export class GhosttyTerminalCore {
10081057
);
10091058
wide = this.runtime.view(this.scratch + 8, 4).getUint32(0, true);
10101059
}
1011-
const selected = this.getCellBool(cellsIterator, CELL_DATA.selected);
10121060
// Read the style after allocation and ABI calls, which can grow WASM memory.
1013-
const styleView = this.runtime.view(this.style, styleSize);
1014-
if (styleView.getUint8(styleFields.inverse!.offset) !== 0) {
1061+
const styleView = this.runtime.memoryDataView();
1062+
if (styleView.getUint8(this.style + styleFields.inverse!.offset) !== 0) {
10151063
[foreground, background] = [background, foreground];
10161064
}
1017-
if (styleView.getUint8(styleFields.faint!.offset) !== 0) {
1065+
if (styleView.getUint8(this.style + styleFields.faint!.offset) !== 0) {
10181066
foreground = blend(foreground, background);
10191067
}
10201068
cells.push({
10211069
text,
10221070
wide,
10231071
foreground,
10241072
background,
1025-
bold: styleView.getUint8(styleFields.bold!.offset) !== 0,
1026-
italic: styleView.getUint8(styleFields.italic!.offset) !== 0,
1027-
invisible: styleView.getUint8(styleFields.invisible!.offset) !== 0,
1028-
strikethrough: styleView.getUint8(styleFields.strikethrough!.offset) !== 0,
1029-
overline: styleView.getUint8(styleFields.overline!.offset) !== 0,
1030-
underline: styleView.getInt32(styleFields.underline!.offset, true) !== 0,
1073+
bold: styleView.getUint8(this.style + styleFields.bold!.offset) !== 0,
1074+
italic: styleView.getUint8(this.style + styleFields.italic!.offset) !== 0,
1075+
invisible: styleView.getUint8(this.style + styleFields.invisible!.offset) !== 0,
1076+
strikethrough: styleView.getUint8(this.style + styleFields.strikethrough!.offset) !== 0,
1077+
overline: styleView.getUint8(this.style + styleFields.overline!.offset) !== 0,
1078+
underline: styleView.getInt32(this.style + styleFields.underline!.offset, true) !== 0,
10311079
selected,
10321080
});
10331081
}
@@ -1177,25 +1225,6 @@ export class GhosttyTerminalCore {
11771225
);
11781226
}
11791227

1180-
private getCellU32(iterator: number, data: number): number {
1181-
this.runtime.bytes(this.scratch, 4).fill(0);
1182-
const result = this.runtime.call(
1183-
"ghostty_render_state_row_cells_get",
1184-
iterator,
1185-
data,
1186-
this.scratch,
1187-
);
1188-
return result === GHOSTTY_SUCCESS ? this.runtime.view(this.scratch, 4).getUint32(0, true) : 0;
1189-
}
1190-
1191-
private getCellBool(iterator: number, data: number): boolean {
1192-
this.runtime.bytes(this.scratch, 1)[0] = 0;
1193-
return (
1194-
this.runtime.call("ghostty_render_state_row_cells_get", iterator, data, this.scratch) ===
1195-
GHOSTTY_SUCCESS && this.runtime.bytes(this.scratch, 1)[0] !== 0
1196-
);
1197-
}
1198-
11991228
private getCellColor(iterator: number, data: number, fallback: GhosttyColor): GhosttyColor {
12001229
this.runtime.bytes(this.scratch, 3).fill(0);
12011230
const result = this.runtime.call(
@@ -1208,8 +1237,12 @@ export class GhosttyTerminalCore {
12081237
}
12091238

12101239
private readColor(pointer: number): GhosttyColor {
1211-
const bytes = this.runtime.bytes(pointer, 3);
1212-
return { r: bytes[0] ?? 0, g: bytes[1] ?? 0, b: bytes[2] ?? 0 };
1240+
const view = this.runtime.memoryDataView();
1241+
return {
1242+
r: view.getUint8(pointer),
1243+
g: view.getUint8(pointer + 1),
1244+
b: view.getUint8(pointer + 2),
1245+
};
12131246
}
12141247

12151248
private emptyCell(foreground: GhosttyColor, background: GhosttyColor): GhosttyCell {

‎apps/web/src/terminal/ghostty/runtime.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class GhosttyRuntime {
106106
}
107107

108108
readPointer(slot: number): number {
109-
return this.currentMemoryView().getUint32(slot, true);
109+
return this.memoryDataView().getUint32(slot, true);
110110
}
111111

112112
attachPtyWriter(terminal: number, writer: (data: string) => void): number {
@@ -135,7 +135,7 @@ export class GhosttyRuntime {
135135
}
136136

137137
/** Reuse scalar reads across cells, refreshing after any terminal grows shared WASM memory. */
138-
private currentMemoryView(): DataView {
138+
memoryDataView(): DataView {
139139
if (this.memoryView.buffer !== this.memory.buffer) {
140140
this.memoryView = new DataView(this.memory.buffer);
141141
}
@@ -145,7 +145,7 @@ export class GhosttyRuntime {
145145
setField(pointer: number, structName: string, fieldName: string, value: number): void {
146146
const field = this.layout(structName).fields[fieldName];
147147
if (!field) throw new Error(`libghostty-vt field is unavailable: ${structName}.${fieldName}`);
148-
const view = this.currentMemoryView();
148+
const view = this.memoryDataView();
149149
const offset = pointer + field.offset;
150150
switch (field.type) {
151151
case "bool":
@@ -173,7 +173,7 @@ export class GhosttyRuntime {
173173
readField(pointer: number, structName: string, fieldName: string): number {
174174
const field = this.layout(structName).fields[fieldName];
175175
if (!field) throw new Error(`libghostty-vt field is unavailable: ${structName}.${fieldName}`);
176-
const view = this.currentMemoryView();
176+
const view = this.memoryDataView();
177177
const offset = pointer + field.offset;
178178
switch (field.type) {
179179
case "bool":

0 commit comments

Comments
 (0)