xonecas.io
64875
Mc

mini-coder / commit c8f1f94

refactor(cli): centralize terminal writes and fix spinner output consistency

- Add `beforeWriteCallback` to TerminalIO to safely clear spinner before any stream output.
- Adjust UI hierarchy in stream-render to cleanly separate logical blocks with consistent linebreaks.
- Update tests to use the new `doStdoutWrite` and `doStderrWrite` test mocks.

xonecas2026-03-15 c8f1f947622988e90eed405bba4dfdb5bf50f4dc parent 3484bf3

6 files changed

src/cli/live-reasoning.ts+4−0
@@ -19,6 +19,10 @@ export class LiveReasoningBlock {
1919 }
2020 }
2121
22+ isOpen(): boolean {
23+ return this.blockOpen;
24+ }
25+
2226 finish(): void {
2327 if (!this.blockOpen) return;
2428 if (this.lineOpen) writeln();
src/cli/spinner.ts+8−0
@@ -15,12 +15,20 @@ export class Spinner {
1515 terminal.stderrWrite("\x1B[?25l");
1616 this._tick();
1717 this.timer = setInterval(() => this._tick(), 80);
18+ terminal.setBeforeWriteCallback(() => {
19+ this.clear();
20+ });
21+ }
22+
23+ private clear(): void {
24+ terminal.stderrWrite("\r\x1B[2K");
1825 }
1926
2027 stop(): void {
2128 if (!this.timer) return;
2229 clearInterval(this.timer);
2330 this.timer = null;
31+ terminal.setBeforeWriteCallback(null);
2432 terminal.stderrWrite("\r\x1B[2K\x1B[?25h");
2533 }
2634
src/cli/stream-render-content.ts+5−1
@@ -20,11 +20,15 @@ export class StreamRenderContent {
2020 return this.inText;
2121 }
2222
23- appendTextDelta(delta: string | undefined): void {
23+ appendTextDelta(
24+ delta: string | undefined,
25+ renderedVisibleOutput: boolean,
26+ ): void {
2427 const text = delta ?? "";
2528 if (!text) return;
2629 if (!this.inText) {
2730 this.spinner.stop();
31+ if (renderedVisibleOutput) writeln();
2832 write(`${G.reply} `);
2933 this.inText = true;
3034 }
src/cli/stream-render.test.ts+2−2
@@ -15,7 +15,7 @@ afterEach(() => {
1515 });
1616
1717 function captureStdout(): void {
18- terminal.stdoutWrite = (text: string) => {
18+ terminal.doStdoutWrite = (text: string) => {
1919 stdout += text;
2020 };
2121 }
@@ -350,7 +350,7 @@ describe("renderTurn", () => {
350350 new Spinner(),
351351 );
352352
353- expect(simulateTerminal(stdout)).toBe("· reasoning\n step\n◆ answer\n");
353+ expect(simulateTerminal(stdout)).toBe("· reasoning\n step\n\n◆ answer\n");
354354 });
355355
356356 test("starts a new reasoning block after tool activity", async () => {
src/cli/stream-render.ts+3−1
@@ -48,7 +48,7 @@ export async function renderTurn(
4848 switch (event.type) {
4949 case "text-delta": {
5050 liveReasoning.finish();
51- content.appendTextDelta(event.delta);
51+ content.appendTextDelta(event.delta, renderedVisibleOutput);
5252 if (event.delta) renderedVisibleOutput = true;
5353 break;
5454 }
@@ -60,6 +60,7 @@ export async function renderTurn(
6060 reasoningComputed = false;
6161 if (showReasoning && delta) {
6262 spinner.stop();
63+ if (renderedVisibleOutput && !liveReasoning.isOpen()) writeln();
6364 liveReasoning.append(delta);
6465 renderedVisibleOutput = true;
6566 }
@@ -74,6 +75,7 @@ export async function renderTurn(
7475 liveReasoning.finish();
7576 content.flushOpenContent();
7677 spinner.stop();
78+ if (renderedVisibleOutput) writeln();
7779 renderToolCall(event.toolName, event.args);
7880 renderedVisibleOutput = true;
7981
src/cli/terminal-io.ts+26−0
@@ -6,11 +6,37 @@ class TerminalIO {
66 this.interruptHandler = handler;
77 }
88
9+ private beforeWriteCallback: (() => void) | null = null;
10+
11+ setBeforeWriteCallback(cb: (() => void) | null): void {
12+ this.beforeWriteCallback = cb;
13+ }
14+
15+ private skipCallback = false;
16+
917 stdoutWrite(text: string): void {
18+ if (text && this.beforeWriteCallback && !this.skipCallback) {
19+ this.skipCallback = true;
20+ this.beforeWriteCallback();
21+ this.skipCallback = false;
22+ }
23+ this.doStdoutWrite(text);
24+ }
25+
26+ doStdoutWrite(text: string): void {
1027 process.stdout.write(text);
1128 }
1229
1330 stderrWrite(text: string): void {
31+ if (text && this.beforeWriteCallback && !this.skipCallback) {
32+ this.skipCallback = true;
33+ this.beforeWriteCallback();
34+ this.skipCallback = false;
35+ }
36+ this.doStderrWrite(text);
37+ }
38+
39+ doStderrWrite(text: string): void {
1440 process.stderr.write(text);
1541 }