TypeScript
/**
* 터미널 시각화 (PRD v2 §W2, --view term). 외부 의존성 0 — picocolors + 박스/막대 문자.
* 같은 데이터 모델(ComboMetrics, FlowGraph)을 ANSI 로 렌더(리포트와 1:1).
*/
import pc from "picocolors";
import { tr } from "../i18n";
import type { ComboMetrics } from "../metrics";
import { bestByPerspectiveDetailed, sortCombos } from "../metrics";
import type { FlowGraph, FlowNode, Lang } from "../models";
const BAR = "█";
const BAR_W = 24;
function bar(value: number, max: number, color: (s: string) => string): string {
if (max <= 0) return "";
const n = Math.max(0, Math.min(BAR_W, Math.round((value / max) * BAR_W)));
return color(BAR.repeat(n)) + pc.dim("·".repeat(BAR_W - n));
}
function fmtTokens(n: number): string {
return n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n);
}
/** 비교 막대 차트 (정확도↑ / 토큰↓ / 비용↓) + 흐름 트리. 문자열 반환(테스트 가능). */
export function renderTerminal(metrics: ComboMetrics[], graphs: FlowGraph[], lang: Lang): string {
const out: string[] = [];
const sorted = sortCombos(metrics);
const labelW = Math.max(8, ...sorted.map((m) => m.comboId.length));
const chart = (
title: string,
pick: (m: ComboMetrics) => number,
color: (s: string) => string,
fmt: (n: number) => string,
) => {
const vals = sorted.map(pick).filter((v) => Number.isFinite(v));
const max = vals.length ? Math.max(...vals) : 0;
out.push(pc.bold(`\n${title}`));
for (const m of sorted) {
const v = pick(m);
const shown = Number.isFinite(v) ? fmt(v) : pc.dim("null");
out.push(
` ${m.comboId.padEnd(labelW)} ${bar(Number.isFinite(v) ? v : 0, max, color)} ${shown}`,
);
}
};
// Olympus 알파인 모노크롬 — 실버(white)·글레이셔(cyan)·스틸(gray)·딥(blue). 무지개 금지.
chart(
tr(lang, "col_acc"),
(m) => m.accuracy,
pc.white,
(n) => `${n.toFixed(1)}%`,
);
chart(tr(lang, "col_tokens"), (m) => m.totalTokens, pc.cyan, fmtTokens);
chart(
tr(lang, "col_cost"),
(m) => m.costUsd ?? Number.NaN,
pc.gray,
(n) => `$${n.toFixed(4)}`,
);
chart(
tr(lang, "col_subagents"),
(m) => m.subagentCount,
pc.blue,
(n) => String(n),
);
// 관점별 최적
out.push(pc.bold(`\n${tr(lang, "sec_best")}`));
for (const b of bestByPerspectiveDetailed(metrics, lang)) {
out.push(` ${pc.green("▸")} ${b.label}: ${pc.bold(b.comboId)}`);
}
// 흐름 트리(대표 그래프들)
if (graphs.length) {
out.push(pc.bold(`\n${tr(lang, "sec_flow")}`));
for (const g of graphs.slice(0, 12)) out.push(renderFlowTree(g));
}
return out.join("\n");
}
/** 흐름 그래프를 들여쓰기 트리(박스 문자)로. */
export function renderFlowTree(graph: FlowGraph): string {
const byParent = new Map<number | null, FlowNode[]>();
const idxOf = new Map<string, number>();
graph.nodes.forEach((n, i) => idxOf.set(n.id, i));
// edges 로 parent→child (없으면 depth 로 추정)
const parentOf = new Map<string, string>();
for (const e of graph.edges) parentOf.set(e.dst, e.src);
for (const n of graph.nodes) {
const p = parentOf.get(n.id);
const pIdx = p ? (idxOf.get(p) ?? null) : null;
const arr = byParent.get(pIdx ?? null) ?? [];
arr.push(n);
byParent.set(pIdx ?? null, arr);
}
const lines: string[] = [
` ${pc.dim(graph.comboId)} ${pc.dim("·")} ${pc.dim(graph.taskId)}#${graph.runIdx}`,
];
const walk = (parentIdx: number | null, prefix: string) => {
const children = byParent.get(parentIdx) ?? [];
children.forEach((n, i) => {
const last = i === children.length - 1;
const branch = last ? "└─ " : "├─ ";
const tag = n.isMain ? pc.white("main") : pc.cyan(`sub·${n.harness}`);
const mark = n.status === "fail" ? pc.red(" ✗") : n.highlight ? pc.white(" ★") : "";
const stat = pc.dim(`${fmtTokens(n.tokens)} tok · ${n.toolCalls} tools`);
lines.push(` ${prefix}${branch}${tag}${mark} ${stat}`);
walk(idxOf.get(n.id) ?? null, prefix + (last ? " " : "│ "));
});
};
walk(null, "");
return lines.join("\n");
}