xonecas.io
6615
Yc

yoctoselect / commit 091eae4

fix: arrow keys scroll viewport past maxVisible items

Extract viewport logic (moveUp, moveDown, adjustScroll, clampHighlight)
into src/viewport.ts with full test coverage.

- highlightIndex now tracks position in full filtered list, not just
  visible window
- scrollOffset shifts the visible slice to keep highlight in view
- Arrow up/down navigate all items with wrapping
- Home/End jump to first/last of entire filtered list
- Typing and backspace reset scroll to top

Fixes yoctoselect-scroll-bug.md

xonecas2026-03-22 091eae4406ecd94f49371ecc415da40f44089ef3 parent ded4f9e

3 files changed

src/select.ts+18−19
@@ -2,6 +2,7 @@ import * as c from "yoctocolors";
22 import { clearDown, cursorUp, hideCursor, showCursor, write } from "./ansi.ts";
33 import { type NormalizedItem, normalizeItems, prefixFilter } from "./filter.ts";
44 import type { SelectOptions } from "./types.ts";
5+import { adjustScroll, moveDown, moveUp } from "./viewport.ts";
56
67 const DEFAULT_MAX_VISIBLE = 20;
78 const DEFAULT_PLACEHOLDER = "type to filter...";
@@ -13,6 +14,7 @@ export async function select(opts: SelectOptions): Promise<string | null> {
1314
1415 let query = "";
1516 let highlightIndex = 0;
17+ let scrollOffset = 0;
1618 let renderedLines = 0;
1719
1820 function getFiltered(): NormalizedItem[] {
@@ -26,8 +28,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
2628 }
2729
2830 const filtered = getFiltered();
29- const visible = filtered.slice(0, maxVisible);
30- const overflow = filtered.length - visible.length;
31+ const visible = filtered.slice(scrollOffset, scrollOffset + maxVisible);
32+ const overflow = filtered.length - scrollOffset - visible.length;
3133
3234 // Search line
3335 const searchText = query || c.dim(placeholder);
@@ -43,7 +45,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
4345 for (let i = 0; i < visible.length; i++) {
4446 const item = visible[i];
4547 if (!item) continue;
46- if (i === highlightIndex) {
48+ const globalIndex = scrollOffset + i;
49+ if (globalIndex === highlightIndex) {
4750 write(` ${c.cyan("›")} ${c.cyan(item.label)}\n`);
4851 } else {
4952 write(` ${item.label}\n`);
@@ -64,9 +67,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
6467 }
6568 }
6669
67- function clampHighlight(filtered: NormalizedItem[]): void {
68- const max = Math.min(filtered.length, maxVisible) - 1;
69- if (highlightIndex > max) highlightIndex = Math.max(0, max);
70+ function updateScroll(): void {
71+ scrollOffset = adjustScroll(highlightIndex, scrollOffset, maxVisible);
7072 }
7173
7274 return new Promise<string | null>((resolve) => {
@@ -99,8 +101,7 @@ export async function select(opts: SelectOptions): Promise<string | null> {
99101 // Enter
100102 if (key === "\r" || key === "\n") {
101103 const filtered = getFiltered();
102- const visible = filtered.slice(0, maxVisible);
103- const selected = visible[highlightIndex];
104+ const selected = filtered[highlightIndex];
104105 cleanup(selected?.value ?? null);
105106 return;
106107 }
@@ -110,6 +111,7 @@ export async function select(opts: SelectOptions): Promise<string | null> {
110111 if (query.length > 0) {
111112 query = query.slice(0, -1);
112113 highlightIndex = 0;
114+ scrollOffset = 0;
113115 }
114116 render();
115117 return;
@@ -118,10 +120,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
118120 // Arrow up
119121 if (key === "\x1b[A") {
120122 const filtered = getFiltered();
121- const max = Math.min(filtered.length, maxVisible);
122- if (max > 0) {
123- highlightIndex = highlightIndex <= 0 ? max - 1 : highlightIndex - 1;
124- }
123+ highlightIndex = moveUp(highlightIndex, filtered.length);
124+ updateScroll();
125125 render();
126126 return;
127127 }
@@ -129,10 +129,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
129129 // Arrow down
130130 if (key === "\x1b[B") {
131131 const filtered = getFiltered();
132- const max = Math.min(filtered.length, maxVisible);
133- if (max > 0) {
134- highlightIndex = highlightIndex >= max - 1 ? 0 : highlightIndex + 1;
135- }
132+ highlightIndex = moveDown(highlightIndex, filtered.length);
133+ updateScroll();
136134 render();
137135 return;
138136 }
@@ -140,6 +138,7 @@ export async function select(opts: SelectOptions): Promise<string | null> {
140138 // Home
141139 if (key === "\x1b[H" || key === "\x1b[1~") {
142140 highlightIndex = 0;
141+ scrollOffset = 0;
143142 render();
144143 return;
145144 }
@@ -147,8 +146,8 @@ export async function select(opts: SelectOptions): Promise<string | null> {
147146 // End
148147 if (key === "\x1b[F" || key === "\x1b[4~") {
149148 const filtered = getFiltered();
150- const max = Math.min(filtered.length, maxVisible);
151- highlightIndex = Math.max(0, max - 1);
149+ highlightIndex = Math.max(0, filtered.length - 1);
150+ updateScroll();
152151 render();
153152 return;
154153 }
@@ -157,7 +156,7 @@ export async function select(opts: SelectOptions): Promise<string | null> {
157156 if (key.length === 1 && key >= " ") {
158157 query += key;
159158 highlightIndex = 0;
160- clampHighlight(getFiltered());
159+ scrollOffset = 0;
161160 render();
162161 return;
163162 }
src/viewport.test.ts+78−0
@@ -0,0 +1,78 @@
1+import { describe, expect, test } from "bun:test";
2+import { adjustScroll, clampHighlight, moveDown, moveUp } from "./viewport.ts";
3+
4+describe("moveDown", () => {
5+ test("advances by one", () => {
6+ expect(moveDown(0, 5)).toBe(1);
7+ });
8+
9+ test("wraps from last to first", () => {
10+ expect(moveDown(4, 5)).toBe(0);
11+ });
12+
13+ test("returns 0 when total is 0", () => {
14+ expect(moveDown(0, 0)).toBe(0);
15+ });
16+});
17+
18+describe("moveUp", () => {
19+ test("goes back by one", () => {
20+ expect(moveUp(2, 5)).toBe(1);
21+ });
22+
23+ test("wraps from first to last", () => {
24+ expect(moveUp(0, 5)).toBe(4);
25+ });
26+
27+ test("returns 0 when total is 0", () => {
28+ expect(moveUp(0, 0)).toBe(0);
29+ });
30+});
31+
32+describe("clampHighlight", () => {
33+ test("returns same index when in range", () => {
34+ expect(clampHighlight(3, 10)).toBe(3);
35+ });
36+
37+ test("clamps to last item when past end", () => {
38+ expect(clampHighlight(15, 10)).toBe(9);
39+ });
40+
41+ test("returns 0 when total is 0", () => {
42+ expect(clampHighlight(5, 0)).toBe(0);
43+ });
44+});
45+
46+describe("adjustScroll", () => {
47+ test("no scroll needed when highlight is within viewport", () => {
48+ expect(adjustScroll(3, 0, 5)).toBe(0);
49+ });
50+
51+ test("scrolls down when highlight passes bottom of viewport", () => {
52+ // highlight=5, scrollOffset=0, maxVisible=5 → need scrollOffset=1
53+ expect(adjustScroll(5, 0, 5)).toBe(1);
54+ });
55+
56+ test("scrolls up when highlight is above viewport", () => {
57+ // highlight=2, scrollOffset=5, maxVisible=5 → need scrollOffset=2
58+ expect(adjustScroll(2, 5, 5)).toBe(2);
59+ });
60+
61+ test("keeps scrollOffset when highlight is at top edge of viewport", () => {
62+ expect(adjustScroll(3, 3, 5)).toBe(3);
63+ });
64+
65+ test("keeps scrollOffset when highlight is at bottom edge of viewport", () => {
66+ // highlight=7, scrollOffset=3, maxVisible=5 → visible 3..7, highlight=7 is last visible
67+ expect(adjustScroll(7, 3, 5)).toBe(3);
68+ });
69+
70+ test("wrapping to 0 resets scroll to 0", () => {
71+ expect(adjustScroll(0, 10, 5)).toBe(0);
72+ });
73+
74+ test("wrapping to last item scrolls to show it", () => {
75+ // 25 items, maxVisible=5, highlight wraps to 24
76+ expect(adjustScroll(24, 0, 5)).toBe(20);
77+ });
78+});
src/viewport.ts+24−0
@@ -0,0 +1,24 @@
1+export function moveDown(highlight: number, total: number): number {
2+ if (total === 0) return 0;
3+ return highlight >= total - 1 ? 0 : highlight + 1;
4+}
5+
6+export function moveUp(highlight: number, total: number): number {
7+ if (total === 0) return 0;
8+ return highlight <= 0 ? total - 1 : highlight - 1;
9+}
10+
11+export function clampHighlight(highlight: number, total: number): number {
12+ if (total === 0) return 0;
13+ return Math.min(highlight, total - 1);
14+}
15+
16+export function adjustScroll(
17+ highlight: number,
18+ scrollOffset: number,
19+ maxVisible: number,
20+): number {
21+ if (highlight < scrollOffset) return highlight;
22+ if (highlight >= scrollOffset + maxVisible) return highlight - maxVisible + 1;
23+ return scrollOffset;
24+}