refactor: polish CLI theme + progress helpers
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user