/** * Common UI helper patterns */ import type { ExtensionCommandContext } from "@mariozechner/pi-coding-agent"; import { UI } from "../constants.js"; import { notify } from "./notify.js"; export type ReloadMode = "prompt" | "defer"; export interface ReloadOutcome { reloaded: boolean; reloadRequired: boolean; } const NO_RELOAD_OUTCOME: ReloadOutcome = { reloaded: false, reloadRequired: false, }; /** * Confirm and trigger reload * Returns true if reload was triggered */ export async function confirmReload(ctx: ExtensionCommandContext, reason: string): Promise { if (!ctx.hasUI) { notify(ctx, `Reload pi to apply changes. (${reason})`); return false; } const confirmed = await ctx.ui.confirm("Reload Required", `${reason}\nReload pi now?`); if (confirmed) { await ctx.reload(); return true; } return false; } export async function handleReloadRequirement( ctx: ExtensionCommandContext, reason: string, mode: ReloadMode = "prompt", ): Promise { if (mode === "defer") { if (!ctx.hasUI) { notify(ctx, `Reload pi to apply changes. (${reason})`); } return { reloaded: false, reloadRequired: true }; } const reloaded = await confirmReload(ctx, reason); return reloaded ? { reloaded: true, reloadRequired: false } : { reloaded: false, reloadRequired: true }; } export function noReloadOutcome(): ReloadOutcome { return { ...NO_RELOAD_OUTCOME }; } /** * Confirm action with timeout */ export async function confirmAction( ctx: ExtensionCommandContext, title: string, message: string, timeoutMs: number = UI.confirmTimeout as number, ): Promise { if (!ctx.hasUI) { // In non-interactive mode, assume yes for automated workflows return true; } return ctx.ui.confirm(title, message, { timeout: timeoutMs }); } /** * Show progress notification that works in both modes */ export function showProgress(ctx: ExtensionCommandContext, action: string, target: string): void { const message = `${action} ${target}...`; notify(ctx, message, "info"); } /** * Format list output for display */ export function formatListOutput(ctx: ExtensionCommandContext, title: string, items: string[]): void { if (items.length === 0) { notify(ctx, `No ${title.toLowerCase()} found.`, "info"); return; } const output = items.join("\n"); const titledOutput = `${title}:\n${output}`; if (ctx.hasUI) { ctx.ui.notify(titledOutput, "info"); } else { console.log(`${title}:`); console.log(output); } }