#!/usr/bin/env node // 渲染冒烟:用 pi 自己的扩展加载器加载本扩展,绕过 TUI 直接调用 render(), // 把图表/表格/洞察三个视图按指定宽度打印成纯文本(去 ANSI),并报告超宽行数。 // // 用法: // node scripts/usage-render-smoke.mjs # 默认 110 列,三视图 // node scripts/usage-render-smoke.mjs --width 80 --views table,insights // PI_DIST=/path/to/@earendil-works/pi-coding-agent node scripts/usage-render-smoke.mjs // // 只读会话数据,不写任何文件(采集缓存由扩展自己维护)。 import { existsSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const argv = process.argv.slice(2); const arg = (name, fallback) => { const i = argv.indexOf(`--${name}`); return i >= 0 && argv[i + 1] ? argv[i + 1] : fallback; }; // pi 安装位置:优先 --pi-dist / PI_DIST,其次常见全局路径 const PI_DIST_CANDIDATES = [ arg("pi-dist", process.env.PI_DIST), join(homedir(), ".local/lib/node_modules/@agegr/pi-web/node_modules/@earendil-works/pi-coding-agent"), join(homedir(), ".local/lib/node_modules/@earendil-works/pi-coding-agent"), "/usr/lib/node_modules/@earendil-works/pi-coding-agent", "/usr/local/lib/node_modules/@earendil-works/pi-coding-agent", ].filter(Boolean); const PI_DIST = PI_DIST_CANDIDATES.find((p) => existsSync(join(p, "dist/core/extensions/loader.js"))); if (!PI_DIST) { console.error("找不到 pi 安装目录,请用 PI_DIST=<...>/pi-coding-agent 指定"); process.exit(2); } const extDir = arg("dir", repoRoot).replace(/^~(?=\/|$)/, homedir()); const width = Number(arg("width", "110")); const wantViews = arg("views", "graph,table,insights").split(","); const { loadExtensions } = await import(`${PI_DIST}/dist/core/extensions/loader.js`); const res = await loadExtensions([extDir], process.cwd()); if (!res.extensions?.length) { console.error("扩展加载失败:", JSON.stringify(res.errors ?? [], null, 2).slice(0, 2000)); process.exit(1); } const cmd = res.extensions[0].commands.get("usage"); if (!cmd) { console.error("未注册 usage 命令"); process.exit(1); } const strip = (s) => s.replace(/\x1b\[[0-9;]*m/g, ""); // 主题桩:fg(color, text) / bold(text) 等一律返回原文本,便于纯文本比对。 const theme = new Proxy({}, { get: () => (a, b) => (typeof a === "string" && typeof b === "string" ? b : a) }); let component = null; const tui = { requestRender() {}, terminal: { columns: width, rows: 60 } }; const ctx = { hasUI: true, ui: { custom: async (factory) => { let value; const done = (v) => (value = v); const c = await factory({ requestRender() {}, terminal: tui.terminal }, theme, {}, done); if (c?.render && c?.handleInput) component = c; await new Promise((resolveWait) => { const timer = setInterval(() => { if (value !== undefined) { clearInterval(timer); resolveWait(); } }, 50); setTimeout(() => { clearInterval(timer); resolveWait(); }, 300_000); }); return value; }, }, }; await cmd.handler("", ctx); if (!component) { console.error("未捕获渲染组件"); process.exit(1); } // pi 的 ui.custom 返回包装对象,只暴露 render/handleInput,读不到 viewMode, // 所以用本地游标跟踪当前视图(切换顺序 = 扩展内的 VIEW_CYCLE:graph → table → insights)。 const VIEW_CYCLE = ["graph", "table", "insights"]; let exitCode = 0; let current = 0; // 当前视图游标(初始 graph) for (const view of wantViews) { const target = VIEW_CYCLE.indexOf(view); if (target < 0) { console.error(`未知视图: ${view}(可选 ${VIEW_CYCLE.join("/")})`); current = 0; continue; } const presses = (target - current + VIEW_CYCLE.length) % VIEW_CYCLE.length; for (let i = 0; i < presses; i++) component.handleInput("v"); const lines = component.render(width).map(strip); const over = lines.filter((l) => l.length > width); console.log(`\n===== view=${view} width=${width} lines=${lines.length} 超宽=${over.length} =====`); console.log(lines.join("\n")); current = target; if (over.length > 0) { exitCode = 1; for (const l of over) console.log("OVER:", l); } } process.exit(exitCode);