|
| 1 | +/** |
| 2 | + * Timed Confirm — a reusable confirmation dialog with a countdown timer. |
| 3 | + * |
| 4 | + * Shows a bordered confirmation prompt that auto-confirms after a |
| 5 | + * configurable number of seconds. The user can press Enter to confirm |
| 6 | + * immediately or Escape to cancel. |
| 7 | + */ |
| 8 | + |
| 9 | +import { DynamicBorder } from "@mariozechner/pi-coding-agent"; |
| 10 | +import type { ExtensionCommandContext } from "@mariozechner/pi-coding-agent"; |
| 11 | +import { Container, Key, Text, matchesKey } from "@mariozechner/pi-tui"; |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Options |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +/** Configuration for the timed confirmation dialog. */ |
| 18 | +export interface TimedConfirmOptions { |
| 19 | + /** Dialog title. */ |
| 20 | + title: string; |
| 21 | + /** Descriptive message shown below the title. */ |
| 22 | + message: string; |
| 23 | + /** Countdown duration in seconds. Defaults to 5. */ |
| 24 | + seconds?: number; |
| 25 | + /** Value returned when the timer expires. Defaults to `true`. */ |
| 26 | + defaultValue?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | +// Public API |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | + |
| 33 | +/** Default countdown duration in seconds. */ |
| 34 | +const DEFAULT_SECONDS = 5; |
| 35 | + |
| 36 | +/** |
| 37 | + * Show a confirmation dialog with a countdown timer. |
| 38 | + * |
| 39 | + * Auto-resolves with `defaultValue` (default `true`) when the timer expires. |
| 40 | + * The user can press Enter to confirm or Escape to cancel at any time. |
| 41 | + * |
| 42 | + * @example |
| 43 | + * ```ts |
| 44 | + * const ok = await timedConfirm(ctx, { |
| 45 | + * title: "Merge PR", |
| 46 | + * message: `Merge PR #${pr.number} into main?`, |
| 47 | + * seconds: 5, |
| 48 | + * }); |
| 49 | + * ``` |
| 50 | + */ |
| 51 | +export async function timedConfirm( |
| 52 | + ctx: ExtensionCommandContext, |
| 53 | + options: TimedConfirmOptions, |
| 54 | +): Promise<boolean> { |
| 55 | + const { title, message, seconds = DEFAULT_SECONDS, defaultValue = true } = options; |
| 56 | + |
| 57 | + return ctx.ui.custom<boolean>((tui, theme, _kb, done) => { |
| 58 | + let remaining = seconds; |
| 59 | + let resolved = false; |
| 60 | + |
| 61 | + const finish = (value: boolean) => { |
| 62 | + if (resolved) return; |
| 63 | + resolved = true; |
| 64 | + clearInterval(timer); |
| 65 | + done(value); |
| 66 | + }; |
| 67 | + |
| 68 | + const timer = setInterval(() => { |
| 69 | + remaining--; |
| 70 | + if (remaining <= 0) { |
| 71 | + finish(defaultValue); |
| 72 | + } else { |
| 73 | + tui.requestRender(); |
| 74 | + } |
| 75 | + }, 1000); |
| 76 | + |
| 77 | + const container = new Container(); |
| 78 | + const borderTop = new DynamicBorder((s: string) => theme.fg("accent", s)); |
| 79 | + const titleText = new Text("", 1, 0); |
| 80 | + const messageText = new Text("", 1, 0); |
| 81 | + const helpText = new Text("", 1, 0); |
| 82 | + const borderBottom = new DynamicBorder((s: string) => theme.fg("accent", s)); |
| 83 | + |
| 84 | + container.addChild(borderTop); |
| 85 | + container.addChild(titleText); |
| 86 | + container.addChild(messageText); |
| 87 | + container.addChild(helpText); |
| 88 | + container.addChild(borderBottom); |
| 89 | + |
| 90 | + const defaultLabel = defaultValue ? "confirm" : "cancel"; |
| 91 | + |
| 92 | + const updateTexts = () => { |
| 93 | + titleText.setText(theme.fg("accent", theme.bold(title))); |
| 94 | + messageText.setText(message); |
| 95 | + helpText.setText( |
| 96 | + theme.fg("dim", `Auto-${defaultLabel} in ${remaining}s - enter confirm - esc cancel`), |
| 97 | + ); |
| 98 | + }; |
| 99 | + updateTexts(); |
| 100 | + |
| 101 | + return { |
| 102 | + render: (w: number) => { |
| 103 | + updateTexts(); |
| 104 | + return container.render(w); |
| 105 | + }, |
| 106 | + invalidate: () => container.invalidate(), |
| 107 | + handleInput: (data: string) => { |
| 108 | + if (matchesKey(data, Key.enter)) { |
| 109 | + finish(true); |
| 110 | + } else if (matchesKey(data, Key.escape)) { |
| 111 | + finish(false); |
| 112 | + } |
| 113 | + }, |
| 114 | + }; |
| 115 | + }); |
| 116 | +} |
0 commit comments