xonecas.io
64875
Mc

mini-coder / commit 670c3c8

refactor(cli): structure live shell output

xonecas2026-03-14 670c3c87a018322ea3c91354e07c9542a7569d86 parent cdaeafb

4 files changed

src/cli/cli-helpers.ts+1−2
@@ -1,4 +1,3 @@
1-import * as c from "yoctocolors";
21 import type { AgentReporter } from "../agent/reporter.ts";
32
43 export async function getGitBranch(cwd: string): Promise<string | null> {
@@ -34,7 +33,7 @@ export async function runShellPassthrough(
3433 ]);
3534 await proc.exited;
3635 const out = [stdout, stderr].filter(Boolean).join("\n").trim();
37- if (out) reporter.writeText(c.dim(out));
36+ if (out) reporter.streamChunk(`${out}\n`);
3837 return out;
3938 } finally {
4039 reporter.restoreTerminal();
src/cli/live-output.ts+54−0
@@ -0,0 +1,54 @@
1+import * as c from "yoctocolors";
2+import { terminal } from "./terminal-io.ts";
3+
4+const LIVE_OUTPUT_PREFIX = ` ${c.dim("│")} `;
5+
6+function write(text: string): void {
7+ terminal.stdoutWrite(text);
8+}
9+
10+function writeln(text = ""): void {
11+ terminal.stdoutWrite(`${text}\n`);
12+}
13+
14+export class LiveOutputBlock {
15+ private pending = "";
16+ private lineOpen = false;
17+
18+ append(chunk: string): void {
19+ if (!chunk) return;
20+ this.pending += chunk.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
21+ this.flushCompleteLines();
22+ }
23+
24+ finish(): void {
25+ if (this.pending.length > 0) {
26+ this.openLine();
27+ write(this.pending);
28+ this.pending = "";
29+ }
30+ if (this.lineOpen) {
31+ writeln();
32+ this.lineOpen = false;
33+ }
34+ }
35+
36+ private flushCompleteLines(): void {
37+ let boundary = this.pending.indexOf("\n");
38+ while (boundary !== -1) {
39+ const line = this.pending.slice(0, boundary);
40+ this.openLine();
41+ write(line);
42+ writeln();
43+ this.lineOpen = false;
44+ this.pending = this.pending.slice(boundary + 1);
45+ boundary = this.pending.indexOf("\n");
46+ }
47+ }
48+
49+ private openLine(): void {
50+ if (this.lineOpen) return;
51+ write(LIVE_OUTPUT_PREFIX);
52+ this.lineOpen = true;
53+ }
54+}
src/cli/output.test.ts+24−1
@@ -1,5 +1,5 @@
11 import { afterEach, describe, expect, test } from "bun:test";
2-import { renderUserMessage } from "./output.ts";
2+import { CliReporter, renderUserMessage } from "./output.ts";
33 import { terminal } from "./terminal-io.ts";
44
55 let stdout = "";
@@ -38,3 +38,26 @@ describe("renderUserMessage", () => {
3838 expect(stripAnsi(stdout)).toBe("› plan\n step 1\n \n step 2\n");
3939 });
4040 });
41+
42+describe("CliReporter live output", () => {
43+ test("groups streamed chunks into an indented output block", () => {
44+ captureStdout();
45+ const reporter = new CliReporter();
46+
47+ reporter.streamChunk("hello");
48+ reporter.streamChunk("\nworld\n");
49+ reporter.info("done");
50+
51+ expect(stripAnsi(stdout)).toBe(" │ hello\n │ world\n· done\n");
52+ });
53+
54+ test("flushes an unterminated streamed line before the next log line", () => {
55+ captureStdout();
56+ const reporter = new CliReporter();
57+
58+ reporter.streamChunk("partial line");
59+ reporter.warn("next");
60+
61+ expect(stripAnsi(stdout)).toBe(" │ partial line\n! next\n");
62+ });
63+});
src/cli/output.ts+22−9
@@ -12,6 +12,7 @@ import { loadAgents } from "./agents.ts";
1212 import { loadCustomCommands } from "./custom-commands.ts";
1313 import { logError } from "./error-log.ts";
1414 import { parseAppError } from "./error-parse.ts";
15+import { LiveOutputBlock } from "./live-output.ts";
1516 import { loadSkillsIndex } from "./skills.ts";
1617 import { Spinner } from "./spinner.ts";
1718 import { renderStatusBar } from "./status-bar.ts";
@@ -179,14 +180,24 @@ export function renderBanner(model: string, cwd: string): void {
179180
180181 export class CliReporter implements AgentReporter {
181182 private spinner = new Spinner();
183+ private liveOutput = new LiveOutputBlock();
182184
183- info(msg: string): void {
185+ private haltSpinner(): void {
186+ this.spinner.stop();
187+ }
188+
189+ private haltSpinnerAndFlushLiveOutput(): void {
184190 this.spinner.stop();
191+ this.liveOutput.finish();
192+ }
193+
194+ info(msg: string): void {
195+ this.haltSpinnerAndFlushLiveOutput();
185196 writeln(`${G.info} ${c.dim(msg)}`);
186197 }
187198
188199 error(msg: string | Error, hint?: string): void {
189- this.spinner.stop();
200+ this.haltSpinnerAndFlushLiveOutput();
190201 if (typeof msg === "string") {
191202 renderError(msg, hint);
192203 } else {
@@ -195,18 +206,18 @@ export class CliReporter implements AgentReporter {
195206 }
196207
197208 warn(msg: string): void {
198- this.spinner.stop();
209+ this.haltSpinnerAndFlushLiveOutput();
199210 writeln(`${G.warn} ${msg}`);
200211 }
201212
202213 writeText(text: string): void {
203- this.spinner.stop();
214+ this.haltSpinnerAndFlushLiveOutput();
204215 writeln(text);
205216 }
206217
207218 streamChunk(text: string): void {
208- this.spinner.stop();
209- write(text);
219+ this.haltSpinner();
220+ this.liveOutput.append(text);
210221 }
211222
212223 startSpinner(label?: string): void {
@@ -214,11 +225,11 @@ export class CliReporter implements AgentReporter {
214225 }
215226
216227 stopSpinner(): void {
217- this.spinner.stop();
228+ this.haltSpinner();
218229 }
219230
220231 renderSubState(label: string): void {
221- this.spinner.stop();
232+ this.haltSpinnerAndFlushLiveOutput();
222233 writeln(` ${G.info} ${c.dim(label)}`);
223234 this.spinner.start();
224235 }
@@ -227,15 +238,17 @@ export class CliReporter implements AgentReporter {
227238 events: AsyncIterable<TurnEvent>,
228239 opts?: { showReasoning?: boolean },
229240 ): Promise<TurnResult> {
241+ this.liveOutput.finish();
230242 return renderTurn(events, this.spinner, opts);
231243 }
232244
233245 renderStatusBar(data: StatusBarData): void {
246+ this.liveOutput.finish();
234247 renderStatusBar(data);
235248 }
236249
237250 renderHook(toolName: string, scriptPath: string, success: boolean): void {
238- this.spinner.stop();
251+ this.haltSpinnerAndFlushLiveOutput();
239252 renderHook(toolName, scriptPath, success);
240253 }