xonecas.io
64875
Mc

mini-coder / commit 0da6bfa

refactor(cli): simplify status bar layout and fitting

xonecas2026-03-14 0da6bfa27c33fb01c3350d6e2887fd366fa4ccc1 parent d2e064f

2 files changed

src/cli/status-bar.test.ts+130−0
@@ -0,0 +1,130 @@
1+import { afterEach, describe, expect, test } from "bun:test";
2+import { renderStatusBar } from "./status-bar.ts";
3+import { terminal } from "./terminal-io.ts";
4+
5+let stdout = "";
6+const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
7+
8+function stripAnsi(s: string): string {
9+ const esc = String.fromCharCode(0x1b);
10+ return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
11+}
12+
13+async function withTerminalColumns(
14+ cols: number,
15+ fn: () => Promise<void> | void,
16+): Promise<void> {
17+ const out = process.stdout as NodeJS.WriteStream & { columns?: number };
18+ const previous = Object.getOwnPropertyDescriptor(out, "columns");
19+ Object.defineProperty(out, "columns", {
20+ value: cols,
21+ configurable: true,
22+ writable: true,
23+ });
24+ try {
25+ await fn();
26+ } finally {
27+ if (previous) {
28+ Object.defineProperty(out, "columns", previous);
29+ } else {
30+ Object.defineProperty(out, "columns", {
31+ value: undefined,
32+ configurable: true,
33+ writable: true,
34+ });
35+ }
36+ }
37+}
38+
39+afterEach(() => {
40+ stdout = "";
41+ terminal.stdoutWrite = originalStdoutWrite;
42+});
43+
44+describe("renderStatusBar", () => {
45+ test("renders concise segments in stable order", async () => {
46+ terminal.stdoutWrite = (text: string) => {
47+ stdout += text;
48+ };
49+
50+ await withTerminalColumns(200, () => {
51+ renderStatusBar({
52+ model: "gpt-5.3-codex",
53+ provider: "zen",
54+ cwd: "~/src/mini-coder",
55+ gitBranch: "main",
56+ sessionId: "1234567890",
57+ inputTokens: 1200,
58+ outputTokens: 3400,
59+ contextTokens: 8000,
60+ contextWindow: 128000,
61+ thinkingEffort: "medium",
62+ activeAgent: "general",
63+ showReasoning: true,
64+ });
65+ });
66+
67+ const line = stripAnsi(stdout).trim();
68+ expect(line).toContain("gpt-5.3-codex");
69+ expect(line).toContain("~/src/mini-coder");
70+ expect(line).toContain("@general");
71+ expect(line).toContain("tok 1.2k/3.4k");
72+ expect(line).toContain("ctx 8.0k/128.0k");
73+ });
74+
75+ test("drops low-priority segments on narrow terminals", async () => {
76+ terminal.stdoutWrite = (text: string) => {
77+ stdout += text;
78+ };
79+
80+ await withTerminalColumns(42, () => {
81+ renderStatusBar({
82+ model: "gpt-5.3-codex",
83+ provider: "openai",
84+ cwd: "~/src/mini-coder",
85+ gitBranch: "feature/super-long-branch-name",
86+ sessionId: "abcdef123456",
87+ inputTokens: 12345,
88+ outputTokens: 9876,
89+ contextTokens: 72000,
90+ contextWindow: 128000,
91+ thinkingEffort: "xhigh",
92+ activeAgent: "writer",
93+ showReasoning: true,
94+ });
95+ });
96+
97+ const line = stripAnsi(stdout).trim();
98+ expect(line).toContain("gpt-5.3-codex");
99+ expect(line).toContain("~/src/");
100+ expect(line).not.toContain("#abcdef12");
101+ expect(line).not.toContain("ctx 72.0k");
102+ });
103+
104+ test("renders context usage without window percentage", async () => {
105+ terminal.stdoutWrite = (text: string) => {
106+ stdout += text;
107+ };
108+
109+ await withTerminalColumns(120, () => {
110+ renderStatusBar({
111+ model: "llama3.2",
112+ provider: "ollama",
113+ cwd: "~/src/mini-coder",
114+ gitBranch: null,
115+ sessionId: "001122334455",
116+ inputTokens: 0,
117+ outputTokens: 0,
118+ contextTokens: 2048,
119+ contextWindow: null,
120+ thinkingEffort: null,
121+ activeAgent: null,
122+ showReasoning: false,
123+ });
124+ });
125+
126+ const line = stripAnsi(stdout).trim();
127+ expect(line).toContain("ctx 2.0k");
128+ expect(line).not.toContain("ctx 2.0k/");
129+ });
130+});
src/cli/status-bar.ts+73−52
@@ -2,6 +2,7 @@ import * as c from "yoctocolors";
22 import { terminal } from "./terminal-io.ts";
33
44 const ANSI_ESCAPE = "\u001b";
5+const STATUS_SEP = c.dim(" · ");
56
67 function stripAnsi(s: string): string {
78 if (!s.includes(ANSI_ESCAPE)) return s;
@@ -11,20 +12,10 @@ function stripAnsi(s: string): string {
1112 .join("");
1213 }
1314
14-function truncateAnsi(s: string, maxLen: number): string {
15- const plain = stripAnsi(s);
16- if (plain.length <= maxLen) return s;
17- let visible = 0;
18- let i = 0;
19- while (i < s.length && visible < maxLen - 1) {
20- if (s[i] === "\x1B") {
21- while (i < s.length && s[i] !== "m") i++;
22- } else {
23- visible++;
24- }
25- i++;
26- }
27- return s.slice(0, i) + c.dim("…");
15+function truncatePlainText(value: string, maxLen: number): string {
16+ if (value.length <= maxLen) return value;
17+ if (maxLen <= 1) return "…";
18+ return `${value.slice(0, maxLen - 1)}…`;
2819 }
2920
3021 function fmtTokens(n: number): string {
@@ -32,6 +23,55 @@ function fmtTokens(n: number): string {
3223 return String(n);
3324 }
3425
26+function buildContextSegment(opts: {
27+ contextTokens: number;
28+ contextWindow: number | null;
29+}): string | null {
30+ if (opts.contextTokens <= 0) return null;
31+ if (opts.contextWindow === null) {
32+ return c.dim(`ctx ${fmtTokens(opts.contextTokens)}`);
33+ }
34+
35+ const pct = Math.round((opts.contextTokens / opts.contextWindow) * 100);
36+ const pctStr = `${pct}%`;
37+ const pctColored =
38+ pct >= 90 ? c.red(pctStr) : pct >= 75 ? c.yellow(pctStr) : c.dim(pctStr);
39+ return (
40+ c.dim(
41+ `ctx ${fmtTokens(opts.contextTokens)}/${fmtTokens(opts.contextWindow)} `,
42+ ) + pctColored
43+ );
44+}
45+
46+function renderStatusLine(segments: string[]): string {
47+ return segments.join(STATUS_SEP);
48+}
49+
50+function fitStatusSegments(
51+ required: string[],
52+ optional: string[],
53+ cols: number,
54+): string {
55+ const fittedOptional = [...optional];
56+ let line = renderStatusLine([...required, ...fittedOptional]);
57+
58+ while (stripAnsi(line).length > cols && fittedOptional.length > 0) {
59+ fittedOptional.pop();
60+ line = renderStatusLine([...required, ...fittedOptional]);
61+ }
62+
63+ if (stripAnsi(line).length <= cols) return line;
64+
65+ const plainRequired = required.map((segment) => stripAnsi(segment));
66+ const sepLen = stripAnsi(STATUS_SEP).length;
67+ const fixedPrefix = plainRequired[0] ?? "";
68+ if (plainRequired.length <= 1) return truncatePlainText(fixedPrefix, cols);
69+
70+ const maxTailLen = Math.max(8, cols - fixedPrefix.length - sepLen);
71+ const truncatedTail = truncatePlainText(plainRequired[1] ?? "", maxTailLen);
72+ return `${required[0]}${STATUS_SEP}${c.dim(truncatedTail)}`;
73+}
74+
3575 export function renderStatusBar(opts: {
3676 model: string;
3777 provider: string;
@@ -46,53 +86,34 @@ export function renderStatusBar(opts: {
4686 activeAgent?: string | null;
4787 showReasoning?: boolean;
4888 }): void {
49- const cols = terminal.stdoutColumns || 80;
89+ const cols = Math.max(20, terminal.stdoutColumns || 80);
90+ const required = [c.cyan(opts.model), c.dim(opts.cwd)];
91+ const optional: string[] = [];
5092
51- // Build segments from right priority (rightmost items drop first)
52- const left: string[] = [c.cyan(opts.model)];
53- if (opts.thinkingEffort) left.push(c.dim(`✦ ${opts.thinkingEffort}`));
54- if (opts.showReasoning) left.push(c.dim("🤔"));
55- if (opts.provider && opts.provider !== "zen") left.push(c.dim(opts.provider));
56- left.push(c.dim(opts.sessionId.slice(0, 8)));
57- if (opts.activeAgent) left.push(c.green(`@${opts.activeAgent}`));
93+ if (opts.activeAgent) optional.push(c.green(`@${opts.activeAgent}`));
94+ if (opts.thinkingEffort) optional.push(c.dim(`✦ ${opts.thinkingEffort}`));
95+ if (opts.showReasoning) optional.push(c.dim("reasoning"));
96+ if (opts.provider && opts.provider !== "zen") {
97+ optional.push(c.dim(opts.provider));
98+ }
99+ if (opts.gitBranch) optional.push(c.dim(`⎇ ${opts.gitBranch}`));
58100
59- const right: string[] = [];
60101 if (opts.inputTokens > 0 || opts.outputTokens > 0) {
61- right.push(
102+ optional.push(
62103 c.dim(
63- `↑ ${fmtTokens(opts.inputTokens)} ↓ ${fmtTokens(opts.outputTokens)}`,
104+ `tok ${fmtTokens(opts.inputTokens)}/${fmtTokens(opts.outputTokens)}`,
64105 ),
65106 );
66107 }
67- if (opts.contextTokens > 0) {
68- const ctxRaw = fmtTokens(opts.contextTokens);
69- if (opts.contextWindow !== null) {
70- const pct = Math.round((opts.contextTokens / opts.contextWindow) * 100);
71- const ctxMax = fmtTokens(opts.contextWindow);
72- const pctStr = `${pct}%`;
73- const colored =
74- pct >= 90
75- ? c.red(pctStr)
76- : pct >= 75
77- ? c.yellow(pctStr)
78- : c.dim(pctStr);
79- right.push(c.dim(`ctx ${ctxRaw}/${ctxMax} `) + colored);
80- } else {
81- right.push(c.dim(`ctx ${ctxRaw}`));
82- }
83- }
84- if (opts.gitBranch) right.push(c.dim(`⎇ ${opts.gitBranch}`));
85-
86- const cwdDisplay = opts.cwd;
87-
88- // Assemble: left · cwd · right (respects terminal width)
89- const middle = c.dim(cwdDisplay);
90- const sep = c.dim(" ");
91- const full = [...left, middle, ...right.reverse()].join(sep);
92108
93- const visible = stripAnsi(full);
109+ const contextSegment = buildContextSegment({
110+ contextTokens: opts.contextTokens,
111+ contextWindow: opts.contextWindow,
112+ });
113+ if (contextSegment) optional.push(contextSegment);
94114
95- const out = visible.length > cols ? truncateAnsi(full, cols - 1) : full;
115+ optional.push(c.dim(`#${opts.sessionId.slice(0, 8)}`));
96116
117+ const out = fitStatusSegments(required, optional, cols);
97118 terminal.stdoutWrite(`${out}\n`);
98119 }