A terminal picker can accept a wheel event, move its cursor and redraw successfully while the reader sees the same lines. The event handler did work. The viewport was still obeying an earlier instruction to stay put.
That is the small bug fixed in pi-coderef’s new source change. The extension lets a user select code in a terminal and insert a file-and-line reference into a pi prompt. Its wheel handler now clears pinnedFirstLine before moving the code cursor. [1] I followed that single state change through the renderer.
A drag borrows control of the viewport
When a mouse selection begins, the picker remembers the first rendered line. It keeps that top line fixed while the pointer moves. That is useful: otherwise a cursor-following renderer could move the text under the pointer during the selection itself. [2]
Releasing the mouse freezes the selected range and clears the drag anchor. It intentionally leaves the viewport pinned where the user released it. The source already lets keyboard navigation return to cursor-following behavior. [2]
The wheel needs the same handover. Before the fix, it changed the cursor but kept the old pin. The renderer chooses a pinned top line when present, so a fresh render could reproduce the same viewport. Calling redraw again would not remove that state.
Original replay diagram. Zero-based indices and viewport size are illustrative, not a terminal recording. Image by Shivam Dwivedi.
Replay the handover without a terminal
This small JavaScript program isolates the state rule. Save it as viewport.mjs and run node viewport.mjs with a current Node.js runtime. It uses the built-in assertion module and installs nothing.
I initialize the state as if a drag has just finished. The cursor is at zero-based line 23, the remembered top is zero, and the invented window displays 24 lines from a 300-line file. The centering calculation follows the source’s cursor offset and end clamp; the example leaves terminal chrome and selection machinery out. [2]
import assert from 'node:assert/strict'; function replay(clearPin) { const state = { cursor: 23, pinnedTop: 0 }; const total = 300, visible = 24; const top = () => state.pinnedTop ?? Math.max(0, Math.min(state.cursor - 10, total - visible)); const before = top(); for (const delta of [1, 1]) { if (clearPin) state.pinnedTop = null; state.cursor = Math.max(0, Math.min(total - 1, state.cursor + delta * 3)); } return { cursor: state.cursor, before, after: top() }; } const old = replay(false), fixed = replay(true); assert.equal(old.cursor, fixed.cursor); assert.equal(old.after, old.before); assert.ok(fixed.after > fixed.before); assert.deepEqual(fixed, { cursor: 29, before: 0, after: 19 }); console.log('old:', JSON.stringify(old)); console.log('fixed:', JSON.stringify(fixed)); console.log('same cursor, different viewport'); old: {"cursor":29,"before":0,"after":0} fixed: {"cursor":29,"before":0,"after":19} same cursor, different viewport Both versions end at cursor 29. Only the path that clears the pin lets the top become 19. A test that checks only the cursor would approve both implementations. A test of what is visible distinguishes them.
Notice the null check in the expression: a pin of zero is a real position. Treating zero as “no pin” would accidentally hide the initial-state problem. The absence of a pin is represented by null here.
Assert the visible consequence
The upstream regression test goes through the real picker rather than this stripped-down replay. It opens a synthetic file, performs a mouse press, drag and release, then sends wheel events. It reads the first visible line from the rendered output and checks that the view advances. [3] That is the assertion the bug needs.
I inspected that test; I did not run the extension, its dependency-backed suite or an interactive pi session. The local replay above checks the mechanism and its arithmetic, not terminal compatibility or event decoding.
For a similar widget, I would keep cursor position, selected range and viewport mode explicit. Then exercise transitions between input methods: mouse selection followed by wheel, keyboard movement after a drag, or a search jump after a pinned view. Each action needs a rule for who controls the viewport afterward.
The pin was useful state with an incomplete release rule. Removing pinning altogether would lose the stable drag behavior it was added to provide. The focused repair is to hand control back when the wheel requests movement, and verify the resulting visible lines.
References
examplecode and pi-coderef contributors, wheel-scroll repair.
pi-coderef contributors, picker, renderer and input handlers.
pi-coderef contributors, picker behavior tests, including wheel movement after a mouse selection.
Asset credits
Original explanatory figure and simplified replay, CC BY 4.0. Credit: Shivam Dwivedi. Upstream implementation and tests are credited above; no third-party screenshots reproduced.
AI disclosure
AI was used to draft the article.
