Просмотр исходного кода

fix: preserve TUI scrollback boundaries during redraws (#243)

DanielWalnut 2 месяцев назад
Родитель
Сommit
a2f0af6db7

+ 14 - 3
packages/tui/src/tui/engine/tui-main-screen.ts

@@ -304,6 +304,19 @@ export class TuiMainScreen extends TuiBase implements TUI {
 		// Helper to redraw either the complete logical document or only the visible viewport.
 		// Viewport-only redraws preserve the terminal's native scrollback.
 		const fullRender = (clear: boolean, viewportOnly = false): void => {
+			// Native scrollback cannot move backwards with a shrinking document. Keep the
+			// previous viewport origin so rows already scrolled out are not painted twice,
+			// and growth still writes every row before it scrolls out. Resize previews are
+			// temporary: they show the new tail until the pending full history replay.
+			if (viewportOnly && !this.historyReplayPending && newLines.length <= prevViewportTop) {
+				// Nothing remains addressable on screen; rebuild with one consistent origin.
+				viewportOnly = false;
+			}
+			const start = viewportOnly
+				? this.historyReplayPending
+					? Math.max(0, newLines.length - height)
+					: prevViewportTop
+				: 0;
 			this.fullRedrawCount += 1;
 			const output = new BoundedTerminalWriter((data) => this.terminal.write(data));
 			output.append("\x1b[?2026h"); // Begin synchronized output
@@ -311,7 +324,6 @@ export class TuiMainScreen extends TuiBase implements TUI {
 				output.append(this.deleteKittyImages(this.previousKittyImageIds));
 				output.append(viewportOnly ? "\x1b[2J\x1b[H" : "\x1b[2J\x1b[H\x1b[3J");
 			}
-			const start = viewportOnly ? Math.max(0, newLines.length - height) : 0;
 			for (let i = start; i < newLines.length; i++) {
 				if (i > start) output.append("\r\n");
 				const line = newLines[i]!;
@@ -339,8 +351,7 @@ export class TuiMainScreen extends TuiBase implements TUI {
 			} else {
 				this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
 			}
-			const bufferLength = Math.max(height, newLines.length);
-			this.previousViewportTop = Math.max(0, bufferLength - height);
+			this.previousViewportTop = Math.max(start, newLines.length - height);
 			this.positionHardwareCursor(cursorPos, newLines.length);
 			this.previousLines = newLines;
 			this.previousKittyImageIds = this.collectKittyImageIds(newLines);

+ 113 - 1
packages/tui/test/unit/tui-engine-local-deltas.test.ts

@@ -83,10 +83,122 @@ describe('MCode Pi Engine local deltas', () => {
     expect(writes).toContain('\x1b[2J\x1b[H');
     expect(writes).not.toContain('\x1b[3J');
     expect(terminal.getScrollBuffer()).toContain('activity-1');
-    expect(terminal.getViewport()).toEqual(['header', 'status', 'composer', '', '']);
+    expect(terminal.getViewport()).toEqual(['composer', '', '', '', '']);
+    expect(terminal.getScrollBuffer().filter((line) => line === 'header')).toHaveLength(1);
     expect(tui.fullRedraws).toBe(2);
   });
 
+  it('does not replay scrolled answer rows after a narrow terminal frame shrinks', async () => {
+    const terminal = new RecordingVirtualTerminal(67, 44);
+    const tui = new TuiMainScreen(terminal);
+    const component = new MutableLines();
+    const answer = Array.from({ length: 80 }, (_, index) => `Answer line ${index}`);
+    component.lines = [...answer, 'activity', `composer${CURSOR_MARKER}`, 'status'];
+    tui.addChild(component);
+    tui.renderNow();
+    await terminal.flush();
+
+    // Settle the activity row, then repaint a historical line (for example Markdown styling).
+    component.lines = [...answer, `composer${CURSOR_MARKER}`, 'status'];
+    tui.renderNow();
+    await terminal.flush();
+    terminal.takeWrites();
+    component.lines[0] = '\x1b[1mAnswer line 0\x1b[0m';
+    tui.renderNow();
+    await terminal.flush();
+
+    expect(terminal.takeWrites()).not.toContain('\x1b[3J');
+    expect(terminal.getScrollBuffer()).toEqual([...answer, 'composer', 'status', '']);
+    expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 41 });
+
+    // A later burst must continue from the same logical/physical boundary.
+    component.lines.splice(80, 0, ...Array.from({ length: 50 }, (_, index) => `More ${index}`));
+    tui.renderNow();
+    await terminal.flush();
+    expect(terminal.getScrollBuffer()).toEqual([
+      ...answer,
+      ...Array.from({ length: 50 }, (_, index) => `More ${index}`),
+      'composer',
+      'status',
+    ]);
+  });
+
+  it('keeps every appended row when a historical row changes in the same frame', async () => {
+    const terminal = new RecordingVirtualTerminal(67, 44);
+    const tui = new TuiMainScreen(terminal);
+    const component = new MutableLines();
+    const answer = Array.from({ length: 80 }, (_, index) => `Answer line ${index}`);
+    component.lines = [...answer, 'composer'];
+    tui.addChild(component);
+    tui.renderNow();
+    await terminal.flush();
+    terminal.takeWrites();
+
+    const more = Array.from({ length: 50 }, (_, index) => `More ${index}`);
+    component.lines = [...answer, ...more, 'composer'];
+    component.lines[0] = '\x1b[1mAnswer line 0\x1b[0m';
+    tui.renderNow();
+    await terminal.flush();
+
+    expect(terminal.takeWrites()).not.toContain('\x1b[3J');
+    expect(terminal.getScrollBuffer()).toEqual([...answer, ...more, 'composer']);
+  });
+
+  it('rebuilds the document when shrinking leaves no rows in the previous viewport', async () => {
+    const terminal = new RecordingVirtualTerminal(67, 44);
+    const tui = new TuiMainScreen(terminal);
+    const component = new MutableLines();
+    component.lines = Array.from({ length: 100 }, (_, index) => `Old line ${index}`);
+    tui.addChild(component);
+    tui.renderNow();
+    await terminal.flush();
+
+    component.lines = ['answer', `composer${CURSOR_MARKER}`];
+    tui.renderNow();
+    await terminal.flush();
+
+    expect(terminal.getScrollBuffer().filter(Boolean)).toEqual(['answer', 'composer']);
+    expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 1 });
+  });
+
+  it('still previews the resized tail and restores ordered scrollback after resize settles', async () => {
+    const terminal = new RecordingVirtualTerminal(67, 44);
+    const tui = new TuiMainScreen(terminal);
+    const component = new MutableLines();
+    component.lines = [
+      ...Array.from({ length: 80 }, (_, index) => `Answer line ${index}`),
+      `composer${CURSOR_MARKER}`,
+    ];
+    tui.addChild(component);
+    try {
+      tui.start();
+      tui.renderNow();
+      await terminal.flush();
+      terminal.takeWrites();
+
+      terminal.resize(60, 30);
+      tui.renderNow();
+      await terminal.flush();
+      expect(terminal.takeWrites()).not.toContain('\x1b[3J');
+      expect(terminal.getViewport()).toEqual([
+        ...Array.from({ length: 29 }, (_, index) => `Answer line ${index + 51}`),
+        'composer',
+      ]);
+
+      await new Promise<void>((resolve) => setTimeout(resolve, 200));
+      tui.renderNow();
+      await terminal.flush();
+      expect(terminal.takeWrites()).toContain('\x1b[3J');
+      expect(terminal.getScrollBuffer()).toEqual([
+        ...Array.from({ length: 80 }, (_, index) => `Answer line ${index}`),
+        'composer',
+      ]);
+      expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 29 });
+    } finally {
+      tui.stop();
+    }
+  });
+
   it('renders an urgent product interaction without resetting Main diff state', async () => {
     const terminal = new RecordingVirtualTerminal(40, 5);
     const tui = new TuiMainScreen(terminal);