diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 38014d8d11..e92d4e9b97 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -476,7 +476,7 @@ function reportPostRenderTerminationEvent( error: Error, ): void { process.stderr.write( - ` [hyperframes] Late ${label} (the command already delivered its result): ${error.message}\n`, + ` [hyperframes] Late ${label} (the command already delivered its result): ${terminalText(error.message)}\n`, ); emitCliErrorEvent(kind, error); } diff --git a/packages/cli/src/commands/capture.ts b/packages/cli/src/commands/capture.ts index b9361212a1..912a90f5fe 100644 --- a/packages/cli/src/commands/capture.ts +++ b/packages/cli/src/commands/capture.ts @@ -135,7 +135,7 @@ export default defineCommand({ try { captureUpstreamProxy(); } catch (error) { - console.error(normalizeErrorMessage(error)); + console.error(terminalLines(normalizeErrorMessage(error))); failCommand(); } @@ -162,7 +162,7 @@ export default defineCommand({ try { url = resolveLocalCaptureTarget(localFile, localRoot).fileUrl; } catch (err) { - console.error(normalizeErrorMessage(err)); + console.error(terminalLines(normalizeErrorMessage(err))); failCommand(); } } else { diff --git a/packages/cli/src/commands/capture/video.ts b/packages/cli/src/commands/capture/video.ts index a360707714..e8ae88300d 100644 --- a/packages/cli/src/commands/capture/video.ts +++ b/packages/cli/src/commands/capture/video.ts @@ -263,7 +263,7 @@ export async function runVideoMode(args: VideoModeArgs): Promise { try { ensureCaptureDirSync(projectDir, outDir); } catch (e) { - console.error(`${c.error("✗")} ${(e as Error).message}`); + console.error(`${c.error("✗")} ${terminalText((e as Error).message)}`); setCommandExitCode(1); return; } diff --git a/packages/cli/src/commands/check.ts b/packages/cli/src/commands/check.ts index 50b469f42c..f26785d076 100644 --- a/packages/cli/src/commands/check.ts +++ b/packages/cli/src/commands/check.ts @@ -416,7 +416,7 @@ function reportCheckFailure( const code = usage ? error.code : "runtime_error"; console.log(JSON.stringify(addMeta(jsonFailure(code, message)), null, 2)); } else { - console.error(`${c.error("✗")} Check failed: ${message}`); + console.error(`${c.error("✗")} Check failed: ${terminalText(message)}`); } if (usage) failUsage(); setCommandExitCode(1); diff --git a/packages/cli/src/commands/diff.test.ts b/packages/cli/src/commands/diff.test.ts new file mode 100644 index 0000000000..810886d21b --- /dev/null +++ b/packages/cli/src/commands/diff.test.ts @@ -0,0 +1,40 @@ +import { runCommand } from "citty"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { CliRuntimeError } from "../utils/commandResult.js"; + +vi.mock("../measure/diff.js", async (importOriginal) => ({ + ...(await importOriginal()), + diffVideos: vi.fn(), +})); + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe("diff command terminal output", () => { + it("prints a page error with control characters as visible escapes", async () => { + const { diffVideos } = await import("../measure/diff.js"); + vi.mocked(diffVideos).mockRejectedValueOnce( + new Error("page threw \u001b[2J\u009b\nsecond line \u001b]0;t\u0007"), + ); + const printed: string[] = []; + const keep = (...args: unknown[]) => { + printed.push(args.map(String).join(" ")); + }; + vi.spyOn(console, "log").mockImplementation(keep); + vi.spyOn(console, "error").mockImplementation(keep); + vi.spyOn(process.stderr, "write").mockImplementation(() => true); + vi.spyOn(process, "exit").mockImplementation(() => undefined as never); + const { default: diff } = await import("./diff.js"); + + await runCommand(diff, { rawArgs: ["original.mp4", "yours.mp4"] }).catch((error: unknown) => { + if (!(error instanceof CliRuntimeError)) throw error; + }); + + const text = printed.join("\n"); + expect(text).toContain("page threw \\x1b[2J\\x9b\nsecond line \\x1b]0;t\\x07"); + for (const control of ["\u001b[2J", "\u009b", "\u001b]", "\u0007"]) { + expect(text).not.toContain(control); + } + }); +}); diff --git a/packages/cli/src/commands/diff.ts b/packages/cli/src/commands/diff.ts index fdc8efd86c..b3824e9b41 100644 --- a/packages/cli/src/commands/diff.ts +++ b/packages/cli/src/commands/diff.ts @@ -1,3 +1,4 @@ +import { terminalLines } from "@hyperframes/core"; import { basename, extname, join, resolve } from "node:path"; import { defineCommand } from "../utils/defineCommand.js"; import type { Example } from "./_examples.js"; @@ -294,7 +295,10 @@ export default defineCommand({ const message = normalizeErrorMessage(err); if (json) console.log(formatJson(withMeta({ ok: false, error: message, ...(fix ? { fix } : {}) }))); - else console.error(`${c.error("✗")} ${message}${fix ? `\n Fix: ${fix}` : ""}`); + else + console.error( + `${c.error("✗")} ${terminalLines(message)}${fix ? `\n Fix: ${terminalLines(fix)}` : ""}`, + ); failCommand(1, err); } finally { scheduleCliExit(exitCode); diff --git a/packages/cli/src/commands/layout.ts b/packages/cli/src/commands/layout.ts index a67a7ee260..869acb0c53 100644 --- a/packages/cli/src/commands/layout.ts +++ b/packages/cli/src/commands/layout.ts @@ -418,7 +418,7 @@ function resolveMotionSpec(specPath: string, json: boolean): MotionSpec { ), ); } else { - console.error(`${c.error("✗")} ${message}`); + console.error(`${c.error("✗")} ${terminalLines(message)}`); } failCommand(); } diff --git a/packages/cli/src/commands/media-treatment.ts b/packages/cli/src/commands/media-treatment.ts index fbdeca6204..9b7dcd621f 100644 --- a/packages/cli/src/commands/media-treatment.ts +++ b/packages/cli/src/commands/media-treatment.ts @@ -8,6 +8,7 @@ import { isPathInside, normalizeHfColorGrading, serializeHfColorGrading, + terminalLines, } from "@hyperframes/core"; import { COLOR_GRADING_SOURCE_TAGS, @@ -805,7 +806,7 @@ function printFailure(error: unknown, json: boolean): void { console.log( JSON.stringify(withMeta(jsonFailure(usage ? error.code : "runtime_error", message))), ); - } else console.error(`${c.error("✗")} ${message}`); + } else console.error(`${c.error("✗")} ${terminalLines(message)}`); if (usage) failUsage(); failCommand(1, error); } diff --git a/packages/cli/src/commands/preview.ts b/packages/cli/src/commands/preview.ts index cab34665bb..1d6848a149 100644 --- a/packages/cli/src/commands/preview.ts +++ b/packages/cli/src/commands/preview.ts @@ -1,3 +1,4 @@ +import { terminalText } from "@hyperframes/core"; import { setCommandExitCode, requestCliExit } from "../utils/commandResult.js"; // fallow-ignore-file code-duplication import { defineCommand } from "../utils/defineCommand.js"; @@ -1223,7 +1224,7 @@ async function printCurrentContext( if (selection.ok) { console.log(` ${c.dim("Selection")} ${selection.value.label}`); } else { - console.log(` ${c.dim("Selection")} ${selection.error.message}`); + console.log(` ${c.dim("Selection")} ${terminalText(selection.error.message)}`); } } if (contextIncludes(fields, "lint")) { @@ -1232,7 +1233,7 @@ async function printCurrentContext( ` ${c.dim("Lint")} ${lint.summary.errors} error(s), ${lint.summary.warnings} warning(s)`, ); } else { - console.log(` ${c.dim("Lint")} ${lint.error.message}`); + console.log(` ${c.dim("Lint")} ${terminalText(lint.error.message)}`); } } console.log(); diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index 4135168093..b67863617c 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -1667,7 +1667,9 @@ export async function selfClosedMediaWarning(source: string): Promise` shares: one JSON object on stdout (summaries, never per-frame arrays; // pictures as file paths), progress on stderr, and an error that says what to run instead. import { c } from "../ui/colors.js"; @@ -30,7 +31,8 @@ export async function runMeasure(work: () => object | Promise): Promise< }), ), ); - if (process.stdout.isTTY) console.error(`${c.error("✗")} ${normalizeErrorMessage(err)}`); + if (process.stdout.isTTY) + console.error(`${c.error("✗")} ${terminalLines(normalizeErrorMessage(err))}`); failCommand(1, err); } } diff --git a/packages/cli/src/ui/format.test.ts b/packages/cli/src/ui/format.test.ts index 938d3a4a92..303f1efe5c 100644 --- a/packages/cli/src/ui/format.test.ts +++ b/packages/cli/src/ui/format.test.ts @@ -1,6 +1,7 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { + errorBox, formatBytes, formatDuration, formatRenderPipelineDetail, @@ -197,3 +198,29 @@ describe("formatBytes", () => { expect(formatBytes(1_048_576)).toBe("1.0 MB"); }); }); + +describe("errorBox", () => { + it("prints control characters in its title, hint and suggestion as visible escapes", () => { + const said: string[] = []; + const spy = vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => { + said.push(args.map(String).join(" ")); + }); + try { + errorBox( + "Load \u001b]0;t\u0007", + "page threw \u001b[2J\u009b\nsecond line", + "run \u001b[9;9H", + ); + } finally { + spy.mockRestore(); + } + const text = said.join("\n"); + expect(text).toContain("Load \\x1b]0;t\\x07"); + expect(text).toContain("page threw \\x1b[2J\\x9b"); + expect(text).toContain(" second line"); + expect(text).toContain("run \\x1b[9;9H"); + for (const control of ["\u001b]", "\u0007", "\u009b", "\u001b[2J", "\u001b[9;9H"]) { + expect(text).not.toContain(control); + } + }); +}); diff --git a/packages/cli/src/ui/format.ts b/packages/cli/src/ui/format.ts index 6133be9eab..cc9a3bdcac 100644 --- a/packages/cli/src/ui/format.ts +++ b/packages/cli/src/ui/format.ts @@ -1,4 +1,5 @@ import type { BrowserGpuMode } from "../browser/gpuPolicy.js"; +import { terminalLines, terminalText } from "@hyperframes/core"; import { c } from "./colors.js"; import { recordFailure } from "../utils/jsonError.js"; @@ -132,17 +133,17 @@ export function label(name: string, value: string): string { export function errorBox(title: string, hint?: string, suggestion?: string): void { recordFailure(title, hint ?? suggestion); - console.error(`\n${c.error("\u2717")} ${c.bold(title)}`); + console.error(`\n${c.error("\u2717")} ${c.bold(terminalText(title))}`); if (hint) { // Indent EVERY hint line, not just the first \u2014 a multi-line hint (e.g. the // NO_TOKEN numbered setup list) otherwise had line 1 indented and the rest // flush-left, mangling the list. Single-line hints are unchanged. - const indented = hint + const indented = terminalLines(hint) .split("\n") .map((line) => ` ${line}`) .join("\n"); console.error(`\n${c.dim(indented)}`); } - if (suggestion) console.error(` ${c.accent(suggestion)}`); + if (suggestion) console.error(` ${c.accent(terminalLines(suggestion))}`); console.error(); } diff --git a/packages/cli/src/utils/checkPipeline.ts b/packages/cli/src/utils/checkPipeline.ts index 71edf74cc0..4ff16e1225 100644 --- a/packages/cli/src/utils/checkPipeline.ts +++ b/packages/cli/src/utils/checkPipeline.ts @@ -1,3 +1,4 @@ +import { terminalLines } from "@hyperframes/core"; import { DEFAULT_CONFIG } from "@hyperframes/engine"; import { BrowserCleanupError, onStopSignal } from "../browser/launch.js"; import type { CheckProgress } from "./checkProgress.js"; @@ -1904,7 +1905,7 @@ async function withFindingCrops( } // Still non-gating, but observable: rollouts need the crop-failure rate // (a second Chrome launch failing/timing out) without failing the run. - console.error(" finding crops skipped: " + normalizeErrorMessage(error)); + console.error(" finding crops skipped: " + terminalLines(normalizeErrorMessage(error))); trackCommandFailure("check-finding-crops", error); return report; } @@ -1936,7 +1937,7 @@ async function withOptionalFindingFrames( const finding = runtimeFailure(error); return { ...report, ok: false, runtime: section([...report.runtime.findings, finding]) }; } - console.error(" finding frames skipped: " + normalizeErrorMessage(error)); + console.error(" finding frames skipped: " + terminalLines(normalizeErrorMessage(error))); trackCommandFailure("check-finding-frames", error); return report; }