Skip to content

Commit dab06c0

Browse files
zero-two-rafaeltabjuliusmarmingeclaude
authored
fix(terminal): use a byte budget for Ghostty scrollback (pingdotgg#8766)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 52e8788 commit dab06c0

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-terminal/android/src/main/cpp/t3_terminal_jni.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace {
1212

1313
constexpr uint32_t kSnapshotMagic = 0x54563354; // "T3VT" in little endian.
1414
constexpr uint16_t kSnapshotVersion = 1;
15-
constexpr size_t kMaxScrollbackRows = 10000;
15+
// The pinned ABI measures max_scrollback in bytes despite its header saying lines.
16+
// Match Ghostty's default scrollback-limit.
17+
constexpr size_t kMaxScrollbackBytes = 10'000'000;
1618

1719
enum CellFlag : uint16_t {
1820
kBold = 1 << 0,
@@ -205,7 +207,7 @@ Java_expo_modules_t3terminal_GhosttyBridge_nativeCreate(
205207
GhosttyTerminalOptions options = {
206208
.cols = static_cast<uint16_t>(std::clamp(cols, 1, 65535)),
207209
.rows = static_cast<uint16_t>(std::clamp(rows, 1, 65535)),
208-
.max_scrollback = kMaxScrollbackRows,
210+
.max_scrollback = kMaxScrollbackBytes,
209211
};
210212
if (ghostty_terminal_new(nullptr, &session->terminal, options) != GHOSTTY_SUCCESS ||
211213
ghostty_render_state_new(nullptr, &session->render_state) != GHOSTTY_SUCCESS ||

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,28 @@ describe("GhosttyTerminalCore snapshots", () => {
407407
expect(core.snapshot()).toEqual(reference.snapshot());
408408
});
409409
});
410+
411+
describe("GhosttyTerminalCore scrollback", () => {
412+
it("retains thousands of rows of scrollback", async () => {
413+
const core = await GhosttyTerminalCore.create(
414+
80,
415+
10,
416+
8,
417+
16,
418+
{
419+
foreground: { r: 255, g: 255, b: 255 },
420+
background: { r: 0, g: 0, b: 0 },
421+
cursor: { r: 255, g: 255, b: 255 },
422+
},
423+
() => {},
424+
);
425+
try {
426+
core.write(Array.from({ length: 1_200 }, (_, index) => `${index}\r\n`).join(""));
427+
const scrollbar = core.scrollbarState();
428+
// max_scrollback is a byte budget in the pinned ABI, not a row count.
429+
expect(scrollbar!.total - scrollbar!.len).toBeGreaterThan(1_000);
430+
} finally {
431+
core.dispose();
432+
}
433+
});
434+
});

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import { GhosttyRuntime, loadGhosttyRuntime } from "./runtime";
99

1010
const GHOSTTY_SUCCESS = 0;
1111
const GHOSTTY_OUT_OF_SPACE = -3;
12-
const MAX_SCROLLBACK_ROWS = 10_000;
12+
// The pinned ABI measures max_scrollback in bytes despite its header saying lines.
13+
// Match Ghostty's default scrollback-limit.
14+
const MAX_SCROLLBACK_BYTES = 10_000_000;
1315
// wasm32 GhosttyTerminalSelectionFormatOptions layout at the pinned libghostty-vt revision.
1416
const SELECTION_FORMAT_OPTIONS_SIZE = 16;
1517
const PALETTE_BYTES = 256 * 3;
@@ -261,7 +263,12 @@ export class GhosttyTerminalCore {
261263
const options = this.runtime.alloc(optionsSize);
262264
this.runtime.setField(options, "GhosttyTerminalOptions", "cols", cols);
263265
this.runtime.setField(options, "GhosttyTerminalOptions", "rows", rows);
264-
this.runtime.setField(options, "GhosttyTerminalOptions", "max_scrollback", MAX_SCROLLBACK_ROWS);
266+
this.runtime.setField(
267+
options,
268+
"GhosttyTerminalOptions",
269+
"max_scrollback",
270+
MAX_SCROLLBACK_BYTES,
271+
);
265272
this.terminalSlot = this.runtime.allocOpaque();
266273
const terminalResult = this.runtime.call("ghostty_terminal_new", 0, this.terminalSlot, options);
267274
this.runtime.free(options, optionsSize);

0 commit comments

Comments
 (0)