feat: add memory indexing progress options

This commit is contained in:
Peter Steinberger
2026-01-18 08:29:50 +00:00
parent 889db137b8
commit f76ab69612
8 changed files with 308 additions and 24 deletions

View File

@@ -17,7 +17,7 @@ type ProgressOptions = {
enabled?: boolean;
delayMs?: number;
stream?: NodeJS.WriteStream;
fallback?: "spinner" | "line" | "none";
fallback?: "spinner" | "line" | "log" | "none";
};
export type ProgressReporter = {
@@ -45,12 +45,14 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
if (activeProgress > 0) return noopReporter;
const stream = options.stream ?? process.stderr;
if (!stream.isTTY) return noopReporter;
const isTty = stream.isTTY;
const allowLog = !isTty && options.fallback === "log";
if (!isTty && !allowLog) return noopReporter;
const delayMs = typeof options.delayMs === "number" ? options.delayMs : DEFAULT_DELAY_MS;
const canOsc = supportsOscProgress(process.env, stream.isTTY);
const allowSpinner = options.fallback === undefined || options.fallback === "spinner";
const allowLine = options.fallback === "line";
const canOsc = isTty && supportsOscProgress(process.env, isTty);
const allowSpinner = isTty && (options.fallback === undefined || options.fallback === "spinner");
const allowLine = isTty && options.fallback === "line";
let started = false;
let label = options.label;
@@ -61,7 +63,9 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
options.indeterminate ?? (options.total === undefined || options.total === null);
activeProgress += 1;
registerActiveProgressLine(stream);
if (isTty) {
registerActiveProgressLine(stream);
}
const controller = canOsc
? createOscProgressController({
@@ -80,6 +84,23 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
stream.write(`${theme.accent(label)}${suffix}`);
}
: null;
const renderLog = allowLog
? (() => {
let lastLine = "";
let lastAt = 0;
const throttleMs = 250;
return () => {
if (!started) return;
const suffix = indeterminate ? "" : ` ${percent}%`;
const nextLine = `${label}${suffix}`;
const now = Date.now();
if (nextLine === lastLine && now - lastAt < throttleMs) return;
lastLine = nextLine;
lastAt = now;
stream.write(`${nextLine}\n`);
};
})()
: null;
let timer: NodeJS.Timeout | null = null;
const applyState = () => {
@@ -94,6 +115,9 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
if (renderLine) {
renderLine();
}
if (renderLog) {
renderLog();
}
};
const start = () => {
@@ -141,7 +165,9 @@ export function createCliProgress(options: ProgressOptions): ProgressReporter {
if (controller) controller.clear();
if (spin) spin.stop();
clearActiveProgressLine();
unregisterActiveProgressLine(stream);
if (isTty) {
unregisterActiveProgressLine(stream);
}
activeProgress = Math.max(0, activeProgress - 1);
};