Skip to content

feat: add preserveScrollOnWrite option to lock viewport on new output#5

Merged
diegosouzapw merged 1 commit into
mainfrom
feat/port-pr-150-preserve-scroll-opt
May 23, 2026
Merged

feat: add preserveScrollOnWrite option to lock viewport on new output#5
diegosouzapw merged 1 commit into
mainfrom
feat/port-pr-150-preserve-scroll-opt

Conversation

@diegosouzapw

Copy link
Copy Markdown
Owner

Summary

Adapts upstream PR #150 into an opt-in option:

  • New preserveScrollOnWrite?: boolean on ITerminalOptions (default false).
  • Default behaviour is unchanged — legacy auto-scroll-to-bottom is preserved.
  • When true, the viewport stays locked on its current scrollback content as new output arrives (kitty/alacritty-style). Viewport shifts by the scrollback delta and clamps to the new scrollback length.
  • Also resolves upstream issue #127, which requested exactly this configurability.

Difference from upstream PR

Upstream made the new behaviour unconditional — a breaking change for any embedder relying on xterm.js auto-scroll semantics. This port gates it behind an opt-in flag so downstream consumers can adopt at their own pace.

Attribution

Thanks to @sauyon for the original implementation.

Test plan

  • bun run fmt && bun run lint && bun run typecheck
  • bun test — 333 tests pass (2 new regression tests for both option values), 0 fail
  • bun run build:lib
  • bun run build:wasm not run locally (Zig not installed); no WASM path touched

Risk

Low — additive, opt-in. Default false preserves current behaviour. Two regression tests cover both legacy auto-scroll AND the new locked-viewport path.

@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

When the user has scrolled into the scrollback and new output arrives,
the legacy xterm.js-style behaviour auto-scrolls back to the bottom
(losing the user's reading position). Modern terminals (kitty, alacritty)
instead lock the viewport on the same content so the user keeps reading
where they were.

This commit ports the upstream fix as a new opt-in option:

- Add `preserveScrollOnWrite?: boolean` to `ITerminalOptions`
  (default: `false`, preserves the current xterm.js-compat behaviour).
- When `true`, save `viewportY` and `getScrollbackLength()` before the
  WASM write, compute the scrollback delta after, and shift `viewportY`
  by that delta — clamped to the new scrollback length in case old lines
  were dropped by the scrollback limit. Re-fires `scrollEmitter` and
  briefly shows the scrollbar when the viewport actually shifts.
- Add two regression tests covering both behaviours.

This is an adaptation of upstream PR coder#150 (which made the new behaviour
unconditional). Resolves coder#127 (request to make
auto-scroll configurable).

Co-authored-by: Sauyon Lee <git@sjle.co>
Inspired-by: coder#150
@diegosouzapw diegosouzapw force-pushed the feat/port-pr-150-preserve-scroll-opt branch from ce1e9e9 to 6a6824a Compare May 23, 2026 15:57
@diegosouzapw diegosouzapw merged commit 84a64fb into main May 23, 2026
1 check passed
@diegosouzapw diegosouzapw deleted the feat/port-pr-150-preserve-scroll-opt branch May 23, 2026 15:57
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant