refactor: polish CLI theme + progress helpers

This commit is contained in:
Peter Steinberger
2026-01-08 05:58:43 +01:00
parent e758cccd46
commit b8a186fbd3
6 changed files with 59 additions and 25 deletions

View File

@@ -23,6 +23,12 @@ export type ProgressReporter = {
done: () => void;
};
export type ProgressTotalsUpdate = {
completed: number;
total: number;
label?: string;
};
const noopReporter: ProgressReporter = {
setLabel: () => {},
setPercent: () => {},
@@ -133,3 +139,20 @@ export async function withProgress<T>(
progress.done();
}
}
export async function withProgressTotals<T>(
options: ProgressOptions,
work: (
update: (update: ProgressTotalsUpdate) => void,
progress: ProgressReporter,
) => Promise<T>,
): Promise<T> {
return await withProgress(options, async (progress) => {
const update = ({ completed, total, label }: ProgressTotalsUpdate) => {
if (label) progress.setLabel(label);
if (!Number.isFinite(total) || total <= 0) return;
progress.setPercent((completed / total) * 100);
};
return await work(update, progress);
});
}