Skip to content

Commit

Permalink
Fix: progress bar can't have negative "in progress" value (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Aug 6, 2023
1 parent 38bf781 commit 6056738
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/console/singleBarFormatted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ export default class SingleBarFormatted {

private static getBar(options: Options, params: Params, payload: ProgressBarPayload): string {
const barSize = options.barsize ?? 0;
const completeSize = Math.floor(Math.max(params.progress, 0) * barSize);
if (barSize <= 0) {
return '';
}

const clamp = (
val: number | undefined,
min: number,
max: number,
): number => Math.min(Math.max(val ?? 0, min), max);
const completeSize = Math.floor(clamp(params.progress, 0.0, 1.0) * barSize);
const inProgressSize = params.total > 0
? Math.ceil((Math.max(payload.inProgress ?? 0, 0) / params.total) * barSize)
? Math.ceil((clamp(payload.inProgress, 0, params.total) / params.total) * barSize)
: 0;
const incompleteSize = barSize - inProgressSize - completeSize;

return (SingleBarFormatted.BAR_COMPLETE_CHAR || '').repeat(completeSize)
+ (SingleBarFormatted.BAR_IN_PROGRESS_CHAR || '').repeat(inProgressSize)
+ (SingleBarFormatted.BAR_INCOMPLETE_CHAR || '').repeat(incompleteSize);
return (SingleBarFormatted.BAR_COMPLETE_CHAR || '').repeat(Math.max(completeSize, 0))
+ (SingleBarFormatted.BAR_IN_PROGRESS_CHAR || '').repeat(Math.max(inProgressSize, 0))
+ (SingleBarFormatted.BAR_INCOMPLETE_CHAR || '').repeat(Math.max(incompleteSize, 0));
}

private getEtaFormatted(etaSeconds: number): string {
Expand Down

0 comments on commit 6056738

Please sign in to comment.