xonecas.io
64875
Mc

mini-coder / commit b3d248f

refactor(cli): route terminal rendering through terminal-io

xonecas2026-03-14 b3d248f6cd3aaa7bb23bc326555ca46134f5e175 parent 216fefc

5 files changed

src/cli/spinner.test.ts+5−5
@@ -1,5 +1,6 @@
11 import { describe, expect, test } from "bun:test";
22 import { SPINNER_FRAMES, Spinner } from "./spinner.ts";
3+import { terminal } from "./terminal-io.ts";
34
45 const BRAILLE_BASE = 0x2800;
56 const BRAILLE_DOT_7 = 1 << 6;
@@ -18,13 +19,12 @@ describe("SPINNER_FRAMES", () => {
1819
1920 describe("Spinner", () => {
2021 test("clears the spinner line and shows cursor without advancing to a new line", () => {
21- const originalWrite = process.stderr.write;
22+ const originalWrite = terminal.stderrWrite.bind(terminal);
2223 const originalIsTTY = process.stderr.isTTY;
2324 let stderr = "";
24- process.stderr.write = ((text: string) => {
25+ terminal.stderrWrite = (text: string) => {
2526 stderr += text;
26- return true;
27- }) as typeof process.stderr.write;
27+ };
2828 process.stderr.isTTY = true;
2929
3030 try {
@@ -35,7 +35,7 @@ describe("Spinner", () => {
3535 expect(stderr).toContain("\x1B[?25h");
3636 expect(stderr).not.toContain("\n");
3737 } finally {
38- process.stderr.write = originalWrite;
38+ terminal.stderrWrite = originalWrite;
3939 process.stderr.isTTY = originalIsTTY;
4040 }
4141 });
src/cli/spinner.ts+5−4
@@ -1,4 +1,5 @@
11 import * as c from "yoctocolors";
2+import { terminal } from "./terminal-io.ts";
23
34 export const SPINNER_FRAMES = ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"];
45
@@ -8,10 +9,10 @@ export class Spinner {
89 private label = "";
910
1011 start(label = ""): void {
11- if (!process.stderr.isTTY) return;
12+ if (!terminal.isStderrTTY) return;
1213 this.label = label;
1314 if (this.timer) return;
14- process.stderr.write("\x1B[?25l");
15+ terminal.stderrWrite("\x1B[?25l");
1516 this._tick();
1617 this.timer = setInterval(() => this._tick(), 80);
1718 }
@@ -20,7 +21,7 @@ export class Spinner {
2021 if (!this.timer) return;
2122 clearInterval(this.timer);
2223 this.timer = null;
23- process.stderr.write("\r\x1B[2K\x1B[?25h");
24+ terminal.stderrWrite("\r\x1B[2K\x1B[?25h");
2425 }
2526
2627 update(label: string): void {
@@ -30,6 +31,6 @@ export class Spinner {
3031 private _tick(): void {
3132 const f = SPINNER_FRAMES[this.frame++ % SPINNER_FRAMES.length] ?? "⣾";
3233 const label = this.label ? c.dim(` ${this.label}`) : "";
33- process.stderr.write(`\r${c.dim(f)}${label}`);
34+ terminal.stderrWrite(`\r${c.dim(f)}${label}`);
3435 }
3536 }
src/cli/status-bar.ts+3−2
@@ -1,4 +1,5 @@
11 import * as c from "yoctocolors";
2+import { terminal } from "./terminal-io.ts";
23
34 const ANSI_ESCAPE = "\u001b";
45
@@ -45,7 +46,7 @@ export function renderStatusBar(opts: {
4546 activeAgent?: string | null;
4647 showReasoning?: boolean;
4748 }): void {
48- const cols = (process.stdout as NodeJS.WriteStream).columns ?? 80;
49+ const cols = terminal.stdoutColumns || 80;
4950
5051 // Build segments from right priority (rightmost items drop first)
5152 const left: string[] = [c.cyan(opts.model)];
@@ -93,5 +94,5 @@ export function renderStatusBar(opts: {
9394
9495 const out = visible.length > cols ? truncateAnsi(full, cols - 1) : full;
9596
96- process.stdout.write(`${out}\n`);
97+ terminal.stdoutWrite(`${out}\n`);
9798 }
src/cli/stream-render-content.ts+3−2
@@ -9,6 +9,7 @@ import {
99 setStreamPreviewPrefix,
1010 streamPartialPreviewDelta,
1111 } from "./stream-preview.ts";
12+import { terminal } from "./terminal-io.ts";
1213
1314 export class StreamRenderContent {
1415 private inText = false;
@@ -83,7 +84,7 @@ export class StreamRenderContent {
8384 if (this.partialPreview.partialWritten > 0) {
8485 const clearSeq = buildClearPartialPreview(
8586 this.partialPreview,
86- process.stdout.columns ?? 0,
87+ terminal.stdoutColumns,
8788 );
8889 if (out !== null) write(`${clearSeq}${this.styledPrefix}${out}`);
8990 else write(clearSeq);
@@ -139,7 +140,7 @@ export class StreamRenderContent {
139140 if (firstLine && this.partialPreview.partialWritten > 0) {
140141 const clearSeq = buildClearPartialPreview(
141142 this.partialPreview,
142- process.stdout.columns ?? 0,
143+ terminal.stdoutColumns,
143144 );
144145 if (out !== null)
145146 batchOutput += `${clearSeq}${this.styledPrefix}${out}\n`;
src/cli/terminal-io.ts+12−0
@@ -18,6 +18,18 @@ class TerminalIO {
1818 return process.stdin.isTTY;
1919 }
2020
21+ get isStdoutTTY(): boolean {
22+ return process.stdout.isTTY;
23+ }
24+
25+ get isStderrTTY(): boolean {
26+ return process.stderr.isTTY;
27+ }
28+
29+ get stdoutColumns(): number {
30+ return process.stdout.columns ?? 0;
31+ }
32+
2133 setRawMode(enable: boolean): void {
2234 if (this.isTTY) {
2335 process.stdin.setRawMode(enable);