@@ -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 }