UNPKG

@code-yeongyu/senpi

Version:

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

136 lines • 5.98 kB
import { Container, SelectList, Spacer, sanitizeTerminalLabel, Text, truncateToWidth, } from "@earendil-works/pi-tui"; import { DynamicBorder } from "../../../../modes/interactive/components/dynamic-border.js"; import { keyHint } from "../../../../modes/interactive/components/keybinding-hints.js"; import { getSelectListTheme } from "../../../../modes/interactive/theme/theme.js"; /** A focused menu that rebuilds on MCP events, never on a polling timer. */ export class McpManagerView { constructor(tui, theme, keybindings) { this.content = new Container(); this.disposed = false; this.tui = tui; this.theme = theme; this.keybindings = keybindings; this.status("MCP servers", "Loading..."); } menu(build, subscribe) { if (this.disposed) return Promise.resolve(undefined); return new Promise((resolve, reject) => { let generation = 0; let settled = false; let selected; let unsubscribe; const finish = (value, error) => { if (settled) return; settled = true; generation++; unsubscribe?.(); this.cancelMenu = undefined; this.rebuild = undefined; this.inputHandler = undefined; if (error !== undefined) reject(error); else resolve(value); }; const render = async () => { const current = ++generation; try { const menu = await build(); if (settled || current !== generation) return; const list = new SelectList(menu.items.map((item) => ({ ...item, label: sanitizeTerminalLabel(item.label || item.value) || "(empty)", description: item.description === undefined ? undefined : sanitizeTerminalLabel(item.description), })), Math.max(1, Math.min(12, this.tui.terminal.rows - 10)), getSelectListTheme()); const wanted = selected ?? menu.selected; const index = menu.items.findIndex((item) => item.value === wanted); if (index !== -1) list.setSelectedIndex(index); selected = list.getSelectedItem()?.value; list.onSelectionChange = (item) => { selected = item.value; }; list.onSelect = (item) => finish(item.value); list.onCancel = () => finish(undefined); const body = []; if (menu.details) body.push(new Text(this.theme.fg("muted", menu.details.split("\n").map(sanitizeTerminalLabel).join("\n")), 1, 0)); body.push(new Spacer(1)); body.push(menu.items.length > 0 ? list : new Text(sanitizeTerminalLabel(menu.empty ?? "Nothing to show."), 1, 0)); const footer = menu.items.length > 0 ? `${keyHint("tui.select.confirm", menu.confirmLabel)} | ${keyHint("tui.select.cancel", menu.cancelLabel)}` : keyHint("tui.select.cancel", menu.cancelLabel); this.setContent(menu.title, body, footer); this.inputHandler = (data) => { if (menu.items.length > 0) list.handleInput(data); else if (this.keybindings.matches(data, "tui.select.cancel")) finish(undefined); }; } catch (error) { if (!settled && current === generation) finish(undefined, error); } }; this.cancelMenu = () => finish(undefined); this.rebuild = () => void render(); this.inputHandler = (data) => { if (this.keybindings.matches(data, "tui.select.cancel")) finish(undefined); }; unsubscribe = subscribe?.(() => void render()); void render(); }); } status(title, message) { this.setContent(title, [ new Spacer(1), new Text(this.theme.fg("muted", message.split("\n").map(sanitizeTerminalLabel).join("\n")), 1, 0), ]); } get columns() { return this.tui.terminal.columns; } setContent(title, body, footer) { if (this.disposed) return; this.content = new Container(); this.content.addChild(new DynamicBorder((text) => this.theme.fg("accent", text))); this.content.addChild(new Text(this.theme.fg("accent", this.theme.bold(sanitizeTerminalLabel(title))), 1, 0)); for (const child of body) this.content.addChild(child); if (footer) { this.content.addChild(new Spacer(1)); this.content.addChild(new Text(footer, 1, 0)); } this.content.addChild(new DynamicBorder((text) => this.theme.fg("accent", text))); this.tui.requestRender(); } handleInput(data) { this.inputHandler?.(data); this.tui.requestRender(); } render(width) { if (this.width !== width || this.rows !== this.tui.terminal.rows) { this.width = width; this.rows = this.tui.terminal.rows; this.rebuild?.(); } return this.content.render(width).map((line) => truncateToWidth(line, width, "")); } invalidate() { this.content.invalidate(); this.rebuild?.(); } dispose() { this.disposed = true; this.cancelMenu?.(); } } //# sourceMappingURL=manager-view.js.map