fix(terminal): replay snapshot history at the PTY size it was drawn for
Opening the terminal panel sometimes showed stray fragments on the prompt row: zsh's end-of-line mark and pieces of the prompt path. The shell had laid its output out for one PTY width, but the client replayed that history into an emulator of another width (an early size estimate, a remount, or a renderer rebuild after fonts loaded). ghostty-web's reflow then left fragments the shell's SIGWINCH redraw never clears. The server now reports the PTY cols/rows in every snapshot, the transport carries them through projections and accepted resizes, and the viewport replays a sized snapshot chunk at that size before returning to the fitted size. The container-based size estimate only seeds newly spawned shells and is no longer sent to a running PTY. Tests cover the sized replay, the store chunk size, the transport projection, and the server snapshot; verified in a production build by reloading with the panel open and switching tabs at a changed width.
This commit is contained in:
@@ -97,7 +97,14 @@ type Props = {
|
||||
sessionKey: string;
|
||||
chunks: TerminalChunk[];
|
||||
onInput: (data: string) => void;
|
||||
/** Fitted size: the emulator has this size, so the PTY should follow. */
|
||||
onResize: (cols: number, rows: number) => void;
|
||||
/**
|
||||
* Size estimated from the container before Ghostty has measured anything.
|
||||
* Good enough to spawn a shell early, not authoritative: an existing PTY
|
||||
* must not be resized to it. Falls back to `onResize` when omitted.
|
||||
*/
|
||||
onProvisionalSize?: (cols: number, rows: number) => void;
|
||||
theme: TerminalTheme;
|
||||
monoFont: MonoFontOption;
|
||||
fontFamily: string;
|
||||
@@ -109,7 +116,7 @@ type Props = {
|
||||
};
|
||||
|
||||
const TerminalViewport = React.forwardRef<TerminalController, Props>(({
|
||||
sessionKey, chunks, onInput, onResize, theme, monoFont, fontFamily, fontSize, className,
|
||||
sessionKey, chunks, onInput, onResize, onProvisionalSize, theme, monoFont, fontFamily, fontSize, className,
|
||||
enableTouchScroll = false, autoFocus = true, isVisible = true,
|
||||
}, ref) => {
|
||||
const containerRef = React.useRef<HTMLDivElement>(null);
|
||||
@@ -117,6 +124,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
|
||||
const fitRef = React.useRef<FitAddon | null>(null);
|
||||
const inputRef = React.useRef(onInput);
|
||||
const resizeRef = React.useRef(onResize);
|
||||
const provisionalSizeCallbackRef = React.useRef(onProvisionalSize);
|
||||
const lastSizeRef = React.useRef<TerminalSize | null>(null);
|
||||
const provisionalSizeRef = React.useRef<TerminalSize | null>(null);
|
||||
const lastChunkRef = React.useRef<number | null>(null);
|
||||
@@ -133,6 +141,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
|
||||
const [rendererGeneration, setRendererGeneration] = React.useState(0);
|
||||
inputRef.current = onInput;
|
||||
resizeRef.current = onResize;
|
||||
provisionalSizeCallbackRef.current = onProvisionalSize;
|
||||
visibleRef.current = isVisible;
|
||||
safeResetRef.current = getGhosttySafeResetSequence(theme.background);
|
||||
|
||||
@@ -141,7 +150,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
|
||||
if (!container) return;
|
||||
const size = getProvisionalTerminalSize(container, fontFamily, fontSize);
|
||||
provisionalSizeRef.current = size;
|
||||
if (size) resizeRef.current(size.cols, size.rows);
|
||||
if (size) (provisionalSizeCallbackRef.current ?? resizeRef.current)(size.cols, size.rows);
|
||||
}, [fontFamily, fontSize]);
|
||||
|
||||
const fit = React.useCallback(() => {
|
||||
@@ -327,18 +336,51 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
|
||||
terminal.options.cursorBlink = isVisible && document.hasFocus() && container.contains(document.activeElement);
|
||||
}, [isVisible, ready]);
|
||||
|
||||
/**
|
||||
* Snapshot history was laid out by the shell for the PTY size recorded on the
|
||||
* chunk. Writing it into an emulator of another width wraps or joins lines the
|
||||
* shell never wrapped, and the shell's later SIGWINCH redraw only repaints
|
||||
* from its own cursor row down, so the stray fragments stay on screen. Replay
|
||||
* such a chunk at its own size and let the emulator reflow back to the fitted
|
||||
* size; a subsequent PTY resize (when the sizes differ) makes the shell redraw
|
||||
* on top of a consistent screen.
|
||||
*
|
||||
* Only valid while nothing is queued: the write must not overtake bytes that
|
||||
* are still waiting for the emulator.
|
||||
*/
|
||||
const writeReplayAtDrawnSize = React.useCallback((terminal: GhosttyTerminal, chunk: TerminalChunk): boolean => {
|
||||
if (!chunk.size || writingRef.current || writeQueueRef.current) return false;
|
||||
const rewritten = rewriteGhosttyDefaultBackgroundResets(
|
||||
chunk.replayData ?? chunk.data,
|
||||
outputRewriteCarryRef.current,
|
||||
safeResetRef.current,
|
||||
);
|
||||
outputRewriteCarryRef.current = rewritten.carry;
|
||||
if (!rewritten.data) return true;
|
||||
const fitted = { cols: terminal.cols, rows: terminal.rows };
|
||||
const resizeForReplay = chunk.size.cols !== fitted.cols || chunk.size.rows !== fitted.rows;
|
||||
if (resizeForReplay) terminal.resize(chunk.size.cols, chunk.size.rows);
|
||||
try {
|
||||
terminal.write(rewritten.data);
|
||||
} finally {
|
||||
if (resizeForReplay) terminal.resize(fitted.cols, fitted.rows);
|
||||
}
|
||||
return true;
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
const terminal = terminalRef.current;
|
||||
if (!terminal) return;
|
||||
const { reset, replay, pending } = selectTerminalChunkReplay(chunks, lastChunkRef.current);
|
||||
if (reset) recreateRenderer();
|
||||
if (pending.length === 0) return;
|
||||
writeQueueRef.current += pending
|
||||
const queued = replay && writeReplayAtDrawnSize(terminal, pending[0]) ? pending.slice(1) : pending;
|
||||
writeQueueRef.current += queued
|
||||
.map((chunk) => replay ? (chunk.replayData ?? chunk.data) : chunk.data)
|
||||
.join('');
|
||||
lastChunkRef.current = chunks.at(-1)?.id ?? null;
|
||||
flush();
|
||||
}, [chunks, flush, ready, recreateRenderer]);
|
||||
}, [chunks, flush, ready, recreateRenderer, writeReplayAtDrawnSize]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!autoFocus || !isVisible) return;
|
||||
|
||||
Reference in New Issue
Block a user