xonecas.io
64875
Mc

mini-coder / commit 5e01dab

refactor(mc-edit): remove diff coloring

xonecas2026-03-15 5e01dab518924339b08e3c266549846a5034bcd8 parent 4f2e10f

5 files changed

docs/mini-coder.1.md+1−1
@@ -111,7 +111,7 @@ The agent has access to the following tools:
111111
112112 ## FILE EDIT HELPER
113113 **mc-edit**
114-: Helper command used from shell for one exact-text edit on an existing file. On success it prints a human-readable colored unified diff to stdout followed by a short metadata block (`ok`, `path`, `changed`). If the edit is a no-op, it prints `(no changes)` plus the same metadata. Errors are written to stderr.
114+: Helper command used from shell for one exact-text edit on an existing file. On success it prints a human-readable plain unified diff to stdout followed by a short metadata block (`ok`, `path`, `changed`). If the edit is a no-op, it prints `(no changes)` plus the same metadata. Errors are written to stderr.
115115
116116 ## ENVIRONMENT
117117 **OPENCODE_API_KEY**
src/cli/structured-output.test.ts+4−9
@@ -1,16 +1,11 @@
11 import { describe, expect, test } from "bun:test";
22 import { renderUnifiedDiff, writeJsonLine } from "./structured-output.ts";
33
4-function stripAnsi(text: string): string {
5- const esc = String.fromCharCode(0x1b);
6- return text.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
7-}
8-
94 describe("renderUnifiedDiff", () => {
10- test("renders a colored unified diff without legacy index headers", () => {
5+ test("renders a plain unified diff without legacy index headers", () => {
116 const output = renderUnifiedDiff("f.txt", "a\nb\nc\n", "a\nB\nc\n");
127
13- expect(stripAnsi(output)).toBe(
8+ expect(output).toBe(
149 [
1510 "--- f.txt",
1611 "+++ f.txt",
@@ -26,7 +21,7 @@ describe("renderUnifiedDiff", () => {
2621 test("renders valid hunk headers when deleting all file contents", () => {
2722 const output = renderUnifiedDiff("f.txt", "x\n", "");
2823
29- expect(stripAnsi(output)).toBe(
24+ expect(output).toBe(
3025 ["--- f.txt", "+++ f.txt", "@@ -1,1 +0,0 @@", "-x"].join("\n"),
3126 );
3227 });
@@ -34,7 +29,7 @@ describe("renderUnifiedDiff", () => {
3429 test("renders valid hunk headers when inserting at the start of a file", () => {
3530 const output = renderUnifiedDiff("f.txt", "", "x\n");
3631
37- expect(stripAnsi(output)).toBe(
32+ expect(output).toBe(
3833 ["--- f.txt", "+++ f.txt", "@@ -0,0 +1,1 @@", "+x"].join("\n"),
3934 );
4035 });
src/cli/structured-output.ts+3−30
@@ -1,5 +1,4 @@
11 import { createTwoFilesPatch } from "diff";
2-import * as c from "yoctocolors";
32 import type {
43 ApplyExactTextEditResult,
54 FileEditErrorCode,
@@ -8,7 +7,6 @@ import type {
87 export interface StructuredOutputWriter {
98 stdout: (text: string) => void;
109 stderr: (text: string) => void;
11- supportsColor?: boolean;
1210 }
1311
1412 type FileEditCliSuccess = { ok: true } & ApplyExactTextEditResult;
@@ -52,10 +50,9 @@ export function renderUnifiedDiff(
5250 filePath: string,
5351 before: string,
5452 after: string,
55- colorize = true,
5653 ): string {
5754 if (before === after) {
58- return colorize ? c.dim("(no changes)") : "(no changes)";
55+ return "(no changes)";
5956 }
6057
6158 const patchText = createTwoFilesPatch(
@@ -69,26 +66,7 @@ export function renderUnifiedDiff(
6966 context: 3,
7067 },
7168 );
72- const lines = normalizePatchLines(patchText);
73-
74- return lines
75- .map((line) => {
76- if (!colorize) return line;
77- if (line.startsWith("+++ ") || line.startsWith("+")) {
78- return c.green(line);
79- }
80- if (line.startsWith("--- ") || line.startsWith("-")) {
81- return c.red(line);
82- }
83- if (line.startsWith("@@ ")) {
84- return c.cyan(line);
85- }
86- if (line.startsWith("\\")) {
87- return c.dim(line);
88- }
89- return line;
90- })
91- .join("\n");
69+ return normalizePatchLines(patchText).join("\n");
9270 }
9371
9472 function renderMetadataBlock(
@@ -113,12 +91,7 @@ export function writeFileEditResult(
11391 ): void {
11492 if (result.ok) {
11593 const sections = [
116- renderUnifiedDiff(
117- result.path,
118- result.before,
119- result.after,
120- io.supportsColor ?? process.stdout.isTTY === true,
121- ),
94+ renderUnifiedDiff(result.path, result.before, result.after),
12295 renderMetadataBlock(result),
12396 ];
12497 io.stdout(`${sections.join("\n\n")}\n`);
src/internal/file-edit/cli.test.ts+3−10
@@ -28,18 +28,12 @@ function createIo() {
2828 stderr: (text: string) => {
2929 stderr += text;
3030 },
31- supportsColor: false,
3231 },
3332 getStdout: () => stdout,
3433 getStderr: () => stderr,
3534 };
3635 }
3736
38-function stripAnsi(text: string): string {
39- const esc = String.fromCharCode(0x1b);
40- return text.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
41-}
42-
4337 describe("runFileEditCli", () => {
4438 test("renders a unified diff and metadata for a changed file", async () => {
4539 await writeFile(join(cwd, "f.txt"), "before\nOLD\nafter\n");
@@ -62,8 +56,7 @@ describe("runFileEditCli", () => {
6256
6357 expect(exitCode).toBe(0);
6458 expect(capture.getStderr()).toBe("");
65- expect(capture.getStdout()).not.toContain("\u001b[");
66- expect(stripAnsi(capture.getStdout())).toBe(
59+ expect(capture.getStdout()).toBe(
6760 [
6861 "--- f.txt",
6962 "+++ f.txt",
@@ -95,7 +88,7 @@ describe("runFileEditCli", () => {
9588
9689 expect(exitCode).toBe(0);
9790 expect(capture.getStderr()).toBe("");
98- expect(stripAnsi(capture.getStdout())).toBe(
91+ expect(capture.getStdout()).toBe(
9992 [
10093 "(no changes)",
10194 "",
@@ -117,7 +110,7 @@ describe("runFileEditCli", () => {
117110
118111 expect(exitCode).toBe(1);
119112 expect(capture.getStdout()).toBe("");
120- expect(stripAnsi(capture.getStderr())).toBe(
113+ expect(capture.getStderr()).toBe(
121114 [
122115 "ok: false",
123116 "code: invalid_args",
src/internal/file-edit/cli.ts+1−2
@@ -17,7 +17,7 @@ const HELP = `Usage: mc-edit <path> (--old <text> | --old-file <path>) [--new <t
1717 Apply one safe exact-text edit to an existing file.
1818 - The expected old text must match exactly once.
1919 - Omit --new / --new-file to delete the matched text.
20-- Success output is human-oriented: unified diff first, metadata second.`;
20+- Success output is human-oriented: plain unified diff first, metadata second.`;
2121
2222 async function readArgText(
2323 flag: "--old-file" | "--new-file",
@@ -123,7 +123,6 @@ 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,
127126 },
128127 ): Promise<number> {
129128 let parsed: FileEditCliArgs | null = null;