xonecas.io
64875
Mc

mini-coder / commit a77af2e

fix: disable mc-edit ansi output when piped

xonecas2026-03-15 a77af2e1903185a5161d64464bcdde0990bbb899 parent a7f4fdf

3 files changed

src/cli/structured-output.ts+10−2
@@ -8,6 +8,7 @@ import type {
88 export interface StructuredOutputWriter {
99 stdout: (text: string) => void;
1010 stderr: (text: string) => void;
11+ supportsColor?: boolean;
1112 }
1213
1314 type FileEditCliSuccess = { ok: true } & ApplyExactTextEditResult;
@@ -51,9 +52,10 @@ export function renderUnifiedDiff(
5152 filePath: string,
5253 before: string,
5354 after: string,
55+ colorize = true,
5456 ): string {
5557 if (before === after) {
56- return c.dim("(no changes)");
58+ return colorize ? c.dim("(no changes)") : "(no changes)";
5759 }
5860
5961 const patchText = createTwoFilesPatch(
@@ -71,6 +73,7 @@ export function renderUnifiedDiff(
7173
7274 return lines
7375 .map((line) => {
76+ if (!colorize) return line;
7477 if (line.startsWith("+++ ") || line.startsWith("+")) {
7578 return c.green(line);
7679 }
@@ -110,7 +113,12 @@ export function writeFileEditResult(
110113 ): void {
111114 if (result.ok) {
112115 const sections = [
113- renderUnifiedDiff(result.path, result.before, result.after),
116+ renderUnifiedDiff(
117+ result.path,
118+ result.before,
119+ result.after,
120+ io.supportsColor ?? process.stdout.isTTY === true,
121+ ),
114122 renderMetadataBlock(result),
115123 ];
116124 io.stdout(`${sections.join("\n\n")}\n`);
src/internal/file-edit/cli.test.ts+2−0
@@ -28,6 +28,7 @@ function createIo() {
2828 stderr: (text: string) => {
2929 stderr += text;
3030 },
31+ supportsColor: false,
3132 },
3233 getStdout: () => stdout,
3334 getStderr: () => stderr,
@@ -61,6 +62,7 @@ describe("runFileEditCli", () => {
6162
6263 expect(exitCode).toBe(0);
6364 expect(capture.getStderr()).toBe("");
65+ expect(capture.getStdout()).not.toContain("\u001b[");
6466 expect(stripAnsi(capture.getStdout())).toBe(
6567 [
6668 "--- f.txt",
src/internal/file-edit/cli.ts+1−0
@@ -123,6 +123,7 @@ export async function runFileEditCli(
123123 io: StructuredOutputWriter = {
124124 stdout: (text) => process.stdout.write(text),
125125 stderr: (text) => process.stderr.write(text),
126+ supportsColor: process.stdout.isTTY === true,
126127 },
127128 ): Promise<number> {
128129 let parsed: FileEditCliArgs | null = null;