fix(cli): silence raw TUI progress

This commit is contained in:
Radek Sienkiewicz
2026-04-30 11:46:16 +02:00
parent 0f8b5167be
commit cce59a0333
2 changed files with 45 additions and 1 deletions

View File

@@ -1,6 +1,23 @@
import { describe, expect, it, vi } from "vitest";
import { createCliProgress, shouldUseInteractiveProgressSpinner } from "./progress.js";
function withStdinIsRaw<T>(isRaw: boolean, run: () => T): T {
const original = Object.getOwnPropertyDescriptor(process.stdin, "isRaw");
Object.defineProperty(process.stdin, "isRaw", {
configurable: true,
value: isRaw,
});
try {
return run();
} finally {
if (original) {
Object.defineProperty(process.stdin, "isRaw", original);
} else {
Reflect.deleteProperty(process.stdin, "isRaw");
}
}
}
describe("cli progress", () => {
it("logs progress when non-tty and fallback=log", () => {
const writes: string[] = [];
@@ -61,4 +78,27 @@ describe("cli progress", () => {
}),
).toBe(true);
});
it("does not write terminal controls when raw TUI input suppresses the default spinner", () => {
const writes: string[] = [];
const stream = {
isTTY: true,
write: vi.fn((chunk: string) => {
writes.push(chunk);
}),
} as unknown as NodeJS.WriteStream;
withStdinIsRaw(true, () => {
const progress = createCliProgress({
label: "Scanning",
total: 2,
stream,
});
progress.setLabel("Still scanning");
progress.tick();
progress.done();
});
expect(writes).toEqual([]);
});
});

View File

@@ -66,12 +66,16 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
const delayMs = typeof options.delayMs === "number" ? options.delayMs : DEFAULT_DELAY_MS;
const canOsc = isTty && supportsOscProgress(process.env, isTty);
const stdinIsRaw = process.stdin.isRaw === true;
const allowSpinner = shouldUseInteractiveProgressSpinner({
fallback: options.fallback,
streamIsTty: isTty,
stdinIsRaw: process.stdin.isRaw,
stdinIsRaw,
});
const allowLine = isTty && options.fallback === "line";
if (isTty && stdinIsRaw && (options.fallback === undefined || options.fallback === "spinner")) {
return noopReporter;
}
let started = false;
let label = options.label;