- Added fuzzy token matching in agent-dashboard, state-manager, and tree-selector, replacing lowercased checks. - Added search-query state and fuzzy-filter helpers to hook, oauth, and user-message selectors for query filtering. - Updated filtered selectors to render match results, status lines, no-match text, and move selection within results. - Added `overflowSearch` and filter state to `SelectList`, switching overflowing list matching to fuzzy checks. - Configured `SelectList` input flow and fixed cancel so Escape/Ctrl+C closes lists when no matches exist. - Updated changelogs and added tests for fuzzy-filter behavior in hook, oauth, user-message, and list selectors.
341 lines
11 KiB
TypeScript
341 lines
11 KiB
TypeScript
/**
|
|
* Generic selector component for hooks.
|
|
* Displays a list of string options with keyboard navigation.
|
|
*/
|
|
import {
|
|
Container,
|
|
extractPrintableText,
|
|
fuzzyFilter,
|
|
Markdown,
|
|
matchesKey,
|
|
padding,
|
|
renderInlineMarkdown,
|
|
replaceTabs,
|
|
Spacer,
|
|
Text,
|
|
type TUI,
|
|
truncateToWidth,
|
|
visibleWidth,
|
|
} from "@oh-my-pi/pi-tui";
|
|
import { getMarkdownTheme, type ThemeColor, theme } from "../../modes/theme/theme";
|
|
import {
|
|
matchesAppExternalEditor,
|
|
matchesSelectCancel,
|
|
matchesSelectDown,
|
|
matchesSelectUp,
|
|
} from "../../modes/utils/keybinding-matchers";
|
|
import { CountdownTimer } from "./countdown-timer";
|
|
import { DynamicBorder } from "./dynamic-border";
|
|
import { renderSegmentTrack } from "./segment-track";
|
|
|
|
/** One segment of a {@link HookSelectorSlider} — a label, its accent color, and
|
|
* an optional detail line (e.g. the resolved model name) shown beneath the
|
|
* track while the segment is active. */
|
|
export interface HookSelectorSliderSegment {
|
|
label: string;
|
|
/** Theme color for the segment label; defaults to `accent`. */
|
|
color?: ThemeColor;
|
|
/** Secondary line rendered under the track when this segment is selected. */
|
|
detail?: string;
|
|
}
|
|
|
|
/**
|
|
* A horizontal left/right selector rendered above the option list. Unlike the
|
|
* up/down option cursor, the slider is moved with the left/right arrows from
|
|
* any list position, letting the caller capture an orthogonal choice (e.g. the
|
|
* model tier to continue execution with) alongside the selected option.
|
|
*/
|
|
export interface HookSelectorSlider {
|
|
/** Dim caption rendered before the slider track (e.g. "continue with"). */
|
|
caption?: string;
|
|
segments: HookSelectorSliderSegment[];
|
|
/** Initially highlighted segment index. */
|
|
index: number;
|
|
/** Invoked with the new index whenever the slider moves. */
|
|
onChange?: (index: number) => void;
|
|
}
|
|
|
|
export interface HookSelectorOptions {
|
|
tui?: TUI;
|
|
timeout?: number;
|
|
onTimeout?: () => void;
|
|
initialIndex?: number;
|
|
outline?: boolean;
|
|
maxVisible?: number;
|
|
onLeft?: () => void;
|
|
onRight?: () => void;
|
|
onExternalEditor?: () => void;
|
|
helpText?: string;
|
|
slider?: HookSelectorSlider;
|
|
}
|
|
|
|
class OutlinedList extends Container {
|
|
#lines: string[] = [];
|
|
|
|
setLines(lines: string[]): void {
|
|
this.#lines = lines;
|
|
this.invalidate();
|
|
}
|
|
|
|
render(width: number): string[] {
|
|
const borderColor = (text: string) => theme.fg("border", text);
|
|
const horizontal = borderColor(theme.boxSharp.horizontal.repeat(Math.max(1, width)));
|
|
const innerWidth = Math.max(1, width - 2);
|
|
const content = this.#lines.map(line => {
|
|
const normalized = replaceTabs(line);
|
|
const fitted = truncateToWidth(normalized, innerWidth);
|
|
const pad = Math.max(0, innerWidth - visibleWidth(fitted));
|
|
return `${borderColor(theme.boxSharp.vertical)}${fitted}${padding(pad)}${borderColor(theme.boxSharp.vertical)}`;
|
|
});
|
|
return [horizontal, ...content, horizontal];
|
|
}
|
|
}
|
|
|
|
export class HookSelectorComponent extends Container {
|
|
#options: string[];
|
|
#filteredOptions: string[];
|
|
#searchQuery = "";
|
|
#selectedIndex: number;
|
|
#maxVisible: number;
|
|
#listContainer: Container | undefined;
|
|
#outlinedList: OutlinedList | undefined;
|
|
#onSelectCallback: (option: string) => void;
|
|
#onCancelCallback: () => void;
|
|
#titleComponent: Markdown;
|
|
#baseTitle: string;
|
|
#countdown: CountdownTimer | undefined;
|
|
#onLeftCallback: (() => void) | undefined;
|
|
#onRightCallback: (() => void) | undefined;
|
|
#onExternalEditorCallback: (() => void) | undefined;
|
|
#slider: HookSelectorSlider | undefined;
|
|
#sliderIndex: number = 0;
|
|
#sliderComponent: Text | undefined;
|
|
constructor(
|
|
title: string,
|
|
options: string[],
|
|
onSelect: (option: string) => void,
|
|
onCancel: () => void,
|
|
opts?: HookSelectorOptions,
|
|
) {
|
|
super();
|
|
|
|
this.#options = options;
|
|
this.#filteredOptions = options;
|
|
this.#selectedIndex = Math.min(opts?.initialIndex ?? 0, this.#filteredOptions.length - 1);
|
|
this.#maxVisible = Math.max(3, opts?.maxVisible ?? 12);
|
|
this.#onSelectCallback = onSelect;
|
|
this.#onCancelCallback = onCancel;
|
|
this.#baseTitle = title;
|
|
this.#onLeftCallback = opts?.onLeft;
|
|
this.#onRightCallback = opts?.onRight;
|
|
this.#onExternalEditorCallback = opts?.onExternalEditor;
|
|
if (opts?.slider && opts.slider.segments.length > 0) {
|
|
this.#slider = opts.slider;
|
|
this.#sliderIndex = Math.max(0, Math.min(opts.slider.index, opts.slider.segments.length - 1));
|
|
}
|
|
|
|
this.addChild(new DynamicBorder());
|
|
this.addChild(new Spacer(1));
|
|
|
|
this.#titleComponent = new Markdown(title, 1, 0, getMarkdownTheme(), { color: t => theme.fg("accent", t) });
|
|
this.addChild(this.#titleComponent);
|
|
this.addChild(new Spacer(1));
|
|
|
|
if (this.#slider) {
|
|
this.#sliderComponent = new Text(this.#renderSliderLine(), 1, 0);
|
|
this.addChild(this.#sliderComponent);
|
|
this.addChild(new Spacer(1));
|
|
}
|
|
|
|
if (opts?.timeout && opts.timeout > 0 && opts.tui) {
|
|
this.#countdown = new CountdownTimer(
|
|
opts.timeout,
|
|
opts.tui,
|
|
s => this.#titleComponent.setText(`${this.#baseTitle} (${s}s)`),
|
|
() => {
|
|
opts?.onTimeout?.();
|
|
const selected = this.#filteredOptions[this.#selectedIndex];
|
|
if (selected) {
|
|
this.#onSelectCallback(selected);
|
|
} else {
|
|
this.#onCancelCallback();
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
if (opts?.outline) {
|
|
this.#outlinedList = new OutlinedList();
|
|
this.addChild(this.#outlinedList);
|
|
} else {
|
|
this.#listContainer = new Container();
|
|
this.addChild(this.#listContainer);
|
|
}
|
|
this.addChild(new Spacer(1));
|
|
const controlsHint = opts?.helpText ?? "up/down navigate enter select esc cancel";
|
|
this.addChild(new Text(theme.fg("dim", controlsHint), 1, 0));
|
|
this.addChild(new Spacer(1));
|
|
this.addChild(new DynamicBorder());
|
|
|
|
this.#updateList();
|
|
}
|
|
|
|
#updateList(): void {
|
|
const lines: string[] = [];
|
|
const total = this.#filteredOptions.length;
|
|
const startIndex = Math.max(
|
|
0,
|
|
Math.min(this.#selectedIndex - Math.floor(this.#maxVisible / 2), total - this.#maxVisible),
|
|
);
|
|
const endIndex = Math.min(startIndex + this.#maxVisible, total);
|
|
|
|
const mdTheme = getMarkdownTheme();
|
|
for (let i = startIndex; i < endIndex; i++) {
|
|
const option = this.#filteredOptions[i];
|
|
if (option === undefined) continue;
|
|
const isSelected = i === this.#selectedIndex;
|
|
const label = isSelected
|
|
? renderInlineMarkdown(option, mdTheme, t => theme.fg("accent", t))
|
|
: renderInlineMarkdown(option, mdTheme, t => theme.fg("text", t));
|
|
const prefix = isSelected ? theme.fg("accent", `${theme.nav.cursor} `) : " ";
|
|
lines.push(prefix + label);
|
|
}
|
|
|
|
if (total === 0) {
|
|
lines.push(theme.fg("dim", " No matching options"));
|
|
}
|
|
|
|
if (startIndex > 0 || endIndex < total || this.#shouldRenderSearchStatus()) {
|
|
lines.push(this.#renderStatusLine(total));
|
|
}
|
|
if (this.#outlinedList) {
|
|
this.#outlinedList.setLines(lines);
|
|
return;
|
|
}
|
|
this.#listContainer?.clear();
|
|
for (const line of lines) {
|
|
this.#listContainer?.addChild(new Text(line, 1, 0));
|
|
}
|
|
}
|
|
|
|
/** Render the slider block in the style of the status line: each option is a
|
|
* distinctly colored segment, the active one filled as a powerline chip
|
|
* (its accent as the background, a luminance-matched label, flanked by
|
|
* triangle caps) and the rest shown as plain colored labels joined by a thin
|
|
* separator. Edge arrows brighten while there is room to move. When the
|
|
* active segment carries a `detail` (e.g. the resolved model name) a muted
|
|
* second line is appended. Returns one or two `\n`-joined lines. */
|
|
#renderSliderLine(): string {
|
|
const slider = this.#slider;
|
|
if (!slider) return "";
|
|
const segments = slider.segments;
|
|
const active = this.#sliderIndex;
|
|
const track = renderSegmentTrack(segments, active);
|
|
|
|
const leftArrow = theme.fg(active > 0 ? "accent" : "dim", "◂");
|
|
const rightArrow = theme.fg(active < segments.length - 1 ? "accent" : "dim", "▸");
|
|
const caption = slider.caption ? `${theme.fg("dim", slider.caption)} ` : "";
|
|
const trackLine = `${caption}${leftArrow} ${track} ${rightArrow}`;
|
|
const detail = segments[active]?.detail;
|
|
if (!detail) return trackLine;
|
|
return `${trackLine}\n ${theme.fg("dim", "↳")} ${theme.fg("muted", detail)}`;
|
|
}
|
|
|
|
/** Move the slider by `delta`, clamped to the segment range, refresh the
|
|
* rendered track, and notify the caller only when the index actually moves. */
|
|
#moveSlider(delta: number): void {
|
|
const slider = this.#slider;
|
|
if (!slider) return;
|
|
const next = Math.max(0, Math.min(slider.segments.length - 1, this.#sliderIndex + delta));
|
|
if (next === this.#sliderIndex) return;
|
|
this.#sliderIndex = next;
|
|
this.#sliderComponent?.setText(this.#renderSliderLine());
|
|
slider.onChange?.(next);
|
|
}
|
|
|
|
#isSearchEnabled(): boolean {
|
|
return this.#options.length > this.#maxVisible;
|
|
}
|
|
|
|
#shouldRenderSearchStatus(): boolean {
|
|
return this.#isSearchEnabled() || this.#searchQuery.length > 0;
|
|
}
|
|
|
|
#renderStatusLine(total: number): string {
|
|
const selectedCount = total === 0 ? 0 : this.#selectedIndex + 1;
|
|
const count =
|
|
this.#searchQuery.trim() && total !== this.#options.length
|
|
? `${selectedCount}/${total} of ${this.#options.length}`
|
|
: `${selectedCount}/${total}`;
|
|
const suffix = this.#searchQuery.trim() ? ` Search: ${this.#searchQuery}` : " Type to search";
|
|
return theme.fg("dim", ` (${count})${suffix}`);
|
|
}
|
|
|
|
#setSearchQuery(query: string): void {
|
|
this.#searchQuery = query;
|
|
this.#filteredOptions = query.trim() ? fuzzyFilter(this.#options, query, option => option) : this.#options;
|
|
this.#selectedIndex = 0;
|
|
this.#updateList();
|
|
}
|
|
|
|
#handleSearchInput(keyData: string): boolean {
|
|
if (!this.#isSearchEnabled()) return false;
|
|
|
|
if (matchesKey(keyData, "backspace")) {
|
|
if (this.#searchQuery.length === 0) return false;
|
|
const chars = [...this.#searchQuery];
|
|
chars.pop();
|
|
this.#setSearchQuery(chars.join(""));
|
|
return true;
|
|
}
|
|
|
|
const printableText = extractPrintableText(keyData);
|
|
if (printableText === undefined) return false;
|
|
if (this.#searchQuery.length === 0 && printableText.trim().length === 0) return false;
|
|
|
|
this.#setSearchQuery(this.#searchQuery + printableText);
|
|
return true;
|
|
}
|
|
|
|
handleInput(keyData: string): void {
|
|
// Reset countdown on any interaction
|
|
this.#countdown?.reset();
|
|
|
|
if (matchesSelectCancel(keyData)) {
|
|
this.#onCancelCallback();
|
|
return;
|
|
}
|
|
|
|
if (this.#handleSearchInput(keyData)) {
|
|
return;
|
|
}
|
|
|
|
if (matchesSelectUp(keyData) || (!this.#isSearchEnabled() && keyData === "k")) {
|
|
if (this.#filteredOptions.length > 0) {
|
|
this.#selectedIndex = Math.max(0, this.#selectedIndex - 1);
|
|
this.#updateList();
|
|
}
|
|
} else if (matchesSelectDown(keyData) || (!this.#isSearchEnabled() && keyData === "j")) {
|
|
if (this.#filteredOptions.length > 0) {
|
|
this.#selectedIndex = Math.min(this.#filteredOptions.length - 1, this.#selectedIndex + 1);
|
|
this.#updateList();
|
|
}
|
|
} else if (matchesKey(keyData, "enter") || matchesKey(keyData, "return") || keyData === "\n") {
|
|
const selected = this.#filteredOptions[this.#selectedIndex];
|
|
if (selected) this.#onSelectCallback(selected);
|
|
} else if (matchesKey(keyData, "left") || (this.#slider && !this.#isSearchEnabled() && keyData === "h")) {
|
|
if (this.#slider) this.#moveSlider(-1);
|
|
else this.#onLeftCallback?.();
|
|
} else if (matchesKey(keyData, "right") || (this.#slider && !this.#isSearchEnabled() && keyData === "l")) {
|
|
if (this.#slider) this.#moveSlider(1);
|
|
else this.#onRightCallback?.();
|
|
} else if (this.#onExternalEditorCallback && matchesAppExternalEditor(keyData)) {
|
|
this.#onExternalEditorCallback();
|
|
}
|
|
}
|
|
|
|
dispose(): void {
|
|
this.#countdown?.dispose();
|
|
}
|
|
}
|