Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Mark first render
if (this.state.first) {
this.state.first = false;
this.onFirst();
}
let content = this.toString(this.renderer());
// Always end with a new line
if (!content.endsWith('\n')) {
content += '\n';
}
// Content cannot be higher than the terminal
const lines = content.split('\n');
const maxHeight = screen.size().rows - 1; // Buffer for input line
if (lines.length >= maxHeight) {
content = lines.slice(-maxHeight).join('\n');
}
// Write output
this.console.out(content);
// Mark output as complete if the final render
if (this.isFinal()) {
this.markComplete();
// Otherwise calculate the height of the output
} else {
this.previousHeight = lines.length;
}
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) {
bar = style.green(bar);
} else if (percent >= 45) {
bar = style.yellow(bar);
} else {
bar = style.red(bar);
}
}
size(): { columns: number; rows: number } {
return screen.size();
}