|
| 1 | +import { type Component, Container, Input, Text } from "@earendil-works/pi-tui"; |
| 2 | +import { displayLine, filterHistory, searchCandidates } from "../ui/history-search-core.js"; |
| 3 | +import { ansi } from "./theme.js"; |
| 4 | + |
| 5 | +const MAX_SHOWN = 8; |
| 6 | + |
| 7 | +/** |
| 8 | + * Ctrl-R reverse history search overlay. Typing filters past prompts |
| 9 | + * (newest first, deduplicated); ↑↓ or repeated Ctrl-R move the |
| 10 | + * selection; Enter hands the pick back; Esc cancels. |
| 11 | + */ |
| 12 | +export class HistorySearchOverlay extends Container { |
| 13 | + private readonly candidates: readonly string[]; |
| 14 | + private readonly input: SearchInput; |
| 15 | + private cursor = 0; |
| 16 | + private readonly onPick: (text: string) => void; |
| 17 | + private readonly onCancel: () => void; |
| 18 | + |
| 19 | + constructor(history: readonly string[], onPick: (text: string) => void, onCancel: () => void) { |
| 20 | + super(); |
| 21 | + this.onPick = onPick; |
| 22 | + this.onCancel = onCancel; |
| 23 | + this.candidates = searchCandidates(history); |
| 24 | + |
| 25 | + this.input = new SearchInput(); |
| 26 | + this.input.onChanged = () => { |
| 27 | + this.cursor = 0; |
| 28 | + this.rebuild(); |
| 29 | + }; |
| 30 | + this.input.onNavigate = (delta) => { |
| 31 | + const total = this.matches().length; |
| 32 | + if (total > 0) this.cursor = (this.cursor + delta + total) % total; |
| 33 | + this.rebuild(); |
| 34 | + }; |
| 35 | + this.input.onSubmit = () => { |
| 36 | + const picked = this.matches()[this.cursor]; |
| 37 | + if (picked) this.onPick(picked); |
| 38 | + else this.onCancel(); |
| 39 | + }; |
| 40 | + this.input.onEscape = () => this.onCancel(); |
| 41 | + this.rebuild(); |
| 42 | + } |
| 43 | + |
| 44 | + getFocusTarget(): Component { |
| 45 | + return this.input; |
| 46 | + } |
| 47 | + |
| 48 | + private matches(): string[] { |
| 49 | + return filterHistory(this.candidates, this.input.getValue()); |
| 50 | + } |
| 51 | + |
| 52 | + private rebuild(): void { |
| 53 | + this.clear(); |
| 54 | + this.addChild(new Text(ansi.bold(ansi.cyan("(reverse-i-search)")), 1, 0)); |
| 55 | + this.addChild(this.input); |
| 56 | + const matches = this.matches(); |
| 57 | + this.cursor = Math.min(this.cursor, Math.max(0, matches.length - 1)); |
| 58 | + if (matches.length === 0) { |
| 59 | + this.addChild(new Text(ansi.dim(" no matching prompts"), 1, 0)); |
| 60 | + } else { |
| 61 | + const start = Math.max(0, Math.min(this.cursor - 2, matches.length - MAX_SHOWN)); |
| 62 | + for (let i = start; i < Math.min(start + MAX_SHOWN, matches.length); i++) { |
| 63 | + const selected = i === this.cursor; |
| 64 | + const line = displayLine(matches[i]); |
| 65 | + this.addChild(new Text(selected ? `${ansi.cyan("▸ ")}${ansi.bold(line)}` : ansi.dim(` ${line}`), 1, 0)); |
| 66 | + } |
| 67 | + } |
| 68 | + this.addChild(new Text(ansi.dim("Enter to use · ↑↓/Ctrl-R to move · Esc to cancel"), 1, 1)); |
| 69 | + this.invalidate(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Input that reports edits (for live filtering) and intercepts the |
| 75 | + * navigation keys before the base class can treat them as cursor moves. |
| 76 | + */ |
| 77 | +class SearchInput extends Input { |
| 78 | + onChanged?: () => void; |
| 79 | + onNavigate?: (delta: 1 | -1) => void; |
| 80 | + |
| 81 | + override handleInput(data: string): void { |
| 82 | + if (data === "\x1b[A") { |
| 83 | + this.onNavigate?.(-1); |
| 84 | + return; |
| 85 | + } |
| 86 | + if (data === "\x1b[B" || data === "\x12") { |
| 87 | + this.onNavigate?.(1); |
| 88 | + return; |
| 89 | + } |
| 90 | + const before = this.getValue(); |
| 91 | + super.handleInput(data); |
| 92 | + if (this.getValue() !== before) this.onChanged?.(); |
| 93 | + } |
| 94 | +} |
0 commit comments