Files
Netcatty/components/terminal/terminalReplay.ts
T
20b12a861c fix(terminal): color the viewport in-frame on bulk keyword-highlight bypass (#3272)
* fix(#3271): automated AI fix

* fix: address Codex review on PR #3272

* fix: address Codex review on PR #3272

* fix: address Codex review on PR #3272

* fix: address Codex review on PR #3272

* fix: address Codex review on PR #3272

* fix: address Codex review on PR #3272

* fix(terminal): defer viewport coloring during history replay

* fix(terminal): skip extra bulk repaint while reading history

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-05 13:13:56 +08:00

65 lines
1.7 KiB
TypeScript

import type { Terminal as XTerm } from "@xterm/xterm";
export type TerminalReplayOptions = {
chunkBytes?: number;
};
const scheduleFrame = (): Promise<void> => new Promise((resolve) => {
if (typeof requestAnimationFrame === "function") {
requestAnimationFrame(() => resolve());
return;
}
setTimeout(resolve, 0);
});
// The highlighter snapshots this flag synchronously when term.write enters.
// Replay has no PTY pressure samples, so it must not look like live log output.
const replayWrites = new WeakSet<XTerm>();
export const isTerminalReplayWrite = (term: XTerm): boolean => replayWrites.has(term);
const writeChunk = (term: XTerm, data: string): Promise<void> => {
if (!data) return Promise.resolve();
return new Promise((resolve) => {
replayWrites.add(term);
try {
term.write(data, () => resolve());
} finally {
replayWrites.delete(term);
}
});
};
export async function writeTerminalPayloadChunked(
term: XTerm,
data: string,
options: TerminalReplayOptions = {},
): Promise<void> {
const chunkBytes = Math.max(1024, options.chunkBytes ?? 16 * 1024);
if (!data) return;
if (data.length <= chunkBytes) {
await writeChunk(term, data);
return;
}
let offset = 0;
while (offset < data.length) {
const end = Math.min(data.length, offset + chunkBytes);
await writeChunk(term, data.slice(offset, end));
offset = end;
if (offset < data.length) {
await scheduleFrame();
}
}
}
export async function writeTerminalReplaySequence(
term: XTerm,
segments: string[],
options: TerminalReplayOptions = {},
): Promise<void> {
for (const segment of segments) {
if (!segment) continue;
await writeTerminalPayloadChunked(term, segment, options);
}
}