Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Mark as final for convenience
if (current >= total) {
this.markFinal();
}
// Compile our template
const progress = Math.min(Math.max(current / total, 0), 1);
const percent = Math.floor(progress * 100);
const elapsed = Date.now() - this.startTime;
const estimated = percent === 100 ? 0 : elapsed * (total / current - 1);
const rate = current / (elapsed / 1000);
const partialTemplate = template
.replace('{progress}', `${current}/${total}`)
.replace('{current}', String(current))
.replace('{elapsed}', formatMs(elapsed))
.replace('{estimated}', formatMs(estimated))
.replace('{percent}', `${percent.toFixed(0)}%`)
.replace('{rate}', String(rate.toFixed(2)))
.replace('{total}', String(total));
// Render the progress bar
const currentWidth = partialTemplate.replace('{bar}', '').length;
const remainingWidth = screen.size().columns - currentWidth;
const completed = Math.round(remainingWidth * progress);
const [complete, incomplete] = STYLES[styleName];
let bar = [
complete.repeat(Math.max(0, completed)),
(transparent ? ' ' : incomplete).repeat(Math.max(0, remainingWidth - completed)),
].join('');
if (color) {
if (percent >= 90) {
// Mark as final for convenience
if (current >= total) {
this.markFinal();
}
// Compile our template
const progress = Math.min(Math.max(current / total, 0), 1);
const percent = Math.floor(progress * 100);
const elapsed = Date.now() - this.startTime;
const estimated = percent === 100 ? 0 : elapsed * (total / current - 1);
const rate = current / (elapsed / 1000);
const partialTemplate = template
.replace('{progress}', `${current}/${total}`)
.replace('{current}', String(current))
.replace('{elapsed}', formatMs(elapsed))
.replace('{estimated}', formatMs(estimated))
.replace('{percent}', `${percent.toFixed(0)}%`)
.replace('{rate}', String(rate.toFixed(2)))
.replace('{total}', String(total));
// Render the progress bar
const currentWidth = partialTemplate.replace('{bar}', '').length;
const remainingWidth = screen.size().columns - currentWidth;
const completed = Math.round(remainingWidth * progress);
const [complete, incomplete] = STYLES[styleName];
let bar = [
complete.repeat(Math.max(0, completed)),
(transparent ? ' ' : incomplete).repeat(Math.max(0, remainingWidth - completed)),
].join('');
if (color) {
getElapsedTime(start: number, stop: number = 0, highlight: boolean = true): string {
const time = (stop || Date.now()) - start;
const isSlow = time > SLOW_THRESHOLD;
const elapsed = formatMs(time);
return isSlow && highlight ? this.style(elapsed, 'failure') : elapsed;
}
getElapsedTime(start: number, stop: number = 0, highlight: boolean = true): string {
const time = (stop || Date.now()) - start;
const isSlow = time > SLOW_THRESHOLD;
const elapsed = formatMs(time);
return isSlow && highlight ? this.style(elapsed, 'failure') : elapsed;
}