UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

99 lines • 4.53 kB
/** * Generic selector component for extensions. * Displays a list of string options with keyboard navigation. */ import { Container, getKeybindings, Spacer, Text } from "@earendil-works/pi-tui"; import { theme } from "../theme/theme.js"; import { CountdownTimer } from "./countdown-timer.js"; import { DynamicBorder } from "./dynamic-border.js"; import { keyHint, rawKeyHint } from "./keybinding-hints.js"; export class ExtensionSelectorComponent extends Container { static { this.DEFAULT_MAX_VISIBLE_OPTIONS = 10; } constructor(title, options, onSelect, onCancel, opts) { super(); this.selectedIndex = 0; this.answerSettled = false; this.options = options; this.onSelectCallback = onSelect; this.onCancelCallback = onCancel; this.onToggleToolsExpanded = opts?.onToggleToolsExpanded; this.baseTitle = title; // Window long lists (issue #795): rendering every option overflows the // viewport on large registries and the moved highlight is never painted, // making navigation look dead. Same sizing rule as TreeList. this.maxVisibleOptions = opts?.tui ? Math.max(5, Math.floor(opts.tui.terminal.rows / 2)) : ExtensionSelectorComponent.DEFAULT_MAX_VISIBLE_OPTIONS; this.addChild(new DynamicBorder()); this.addChild(new Spacer(1)); this.titleText = new Text(theme.fg("accent", theme.bold(title)), 1, 0); this.addChild(this.titleText); if (opts?.description) { this.addChild(new Spacer(1)); this.addChild(new Text(theme.fg("text", opts.description), 1, 0)); } this.addChild(new Spacer(1)); if (opts?.timeout && opts.timeout > 0 && opts.tui) { this.countdown = new CountdownTimer(opts.timeout, opts.tui, (s) => this.titleText.setText(theme.fg("accent", theme.bold(`${this.baseTitle} (${s}s)`))), () => this.onCancelCallback()); } this.listContainer = new Container(); this.addChild(this.listContainer); this.addChild(new Spacer(1)); this.addChild(new Text(rawKeyHint("↑↓", "navigate") + " " + keyHint("tui.select.confirm", "select") + " " + keyHint("tui.select.cancel", "cancel"), 1, 0)); this.addChild(new Spacer(1)); this.addChild(new DynamicBorder()); this.updateList(); } updateList() { this.listContainer.clear(); const count = this.options.length; const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(this.maxVisibleOptions / 2), count - this.maxVisibleOptions)); const endIndex = Math.min(startIndex + this.maxVisibleOptions, count); if (startIndex > 0) { this.listContainer.addChild(new Text(theme.fg("muted", ` … ${startIndex} more above`), 1, 0)); } for (let i = startIndex; i < endIndex; i++) { const isSelected = i === this.selectedIndex; const text = isSelected ? theme.fg("accent", "→ ") + theme.fg("accent", this.options[i]) : ` ${theme.fg("text", this.options[i])}`; this.listContainer.addChild(new Text(text, 1, 0)); } if (endIndex < count) { this.listContainer.addChild(new Text(theme.fg("muted", ` … ${count - endIndex} more below`), 1, 0)); } } handleInput(keyData) { if (this.answerSettled) return; const kb = getKeybindings(); if (kb.matches(keyData, "app.tools.expand")) { this.onToggleToolsExpanded?.(); } else if (kb.matches(keyData, "tui.select.up") || keyData === "k") { this.selectedIndex = Math.max(0, this.selectedIndex - 1); this.updateList(); } else if (kb.matches(keyData, "tui.select.down") || keyData === "j") { this.selectedIndex = Math.min(this.options.length - 1, this.selectedIndex + 1); this.updateList(); } else if (kb.matches(keyData, "tui.select.confirm") || keyData === "\n") { const selected = this.options[this.selectedIndex]; if (selected) this.onSelectCallback(selected); } else if (kb.matches(keyData, "tui.select.cancel")) { this.onCancelCallback(); } } dispose() { this.answerSettled = true; this.countdown?.dispose(); } } //# sourceMappingURL=extension-selector.js.map