Introduction to cli-progress
The cli-progress library is an essential tool for Node.js developers who want to display progress bars in the command-line interface (CLI). This lightweight and flexible library provides a range of features and customizations to create attractive and informative progress bars.
Installing cli-progress
npm install cli-progress
Basic Usage
Here’s a quick start to using cli-progress:
const cliProgress = require('cli-progress');
// Create a new progress bar instance
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
// Initialize the bar
bar1.start(200, 0);
// Update value
bar1.update(100);
// Stop the bar
bar1.stop();
Multiple Progress Bars
cli-progress also supports multiple progress bars.
const multiBar = new cliProgress.MultiBar({}, cliProgress.Presets.shades_grey);
const b1 = multiBar.create(100, 0);
const b2 = multiBar.create(200, 0);
b1.increment();
b2.update(20);
multiBar.stop();
Customizing Progress Bars
You can customize the appearance of your progress bar using different options and presets.
const customBar = new cliProgress.SingleBar({
format: 'Progress |{bar}| {percentage}% || {value}/{total} Chunks',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
customBar.start(200, 0);
customBar.update(100);
customBar.stop();
Progress Bar with ETA and Speed
const etaBar = new cliProgress.SingleBar({
etaBuffer: 100
}, cliProgress.Presets.shades_classic);
etaBar.start(200, 0);
for (let i = 0; i < 200; i++) {
etaBar.increment();
}
etaBar.stop();
Advanced Example Application
Let's build a sample application using multiple features of cli-progress.
const cliProgress = require('cli-progress');
const multiBar = new cliProgress.MultiBar({
clearOnComplete: false,
hideCursor: true
}, cliProgress.Presets.shades_grey);
const totalSteps = 100;
const bars = [
multiBar.create(totalSteps, 0, { task: "Task 1" }),
multiBar.create(totalSteps, 0, { task: "Task 2" }),
multiBar.create(totalSteps, 0, { task: "Task 3" })
];
const totalTasks = bars.length;
let completedTasks = 0;
bars.forEach((bar, index) => {
let value = 0;
const interval = setInterval(() => {
value++;
bar.increment();
if (value >= totalSteps) {
clearInterval(interval);
completedTasks++;
if (completedTasks === totalTasks) {
multiBar.stop();
}
}
}, 100);
});
This script demonstrates the creation of multiple progress bars, how to update them, and stop the progress bar when all tasks are completed.
With cli-progress, you can easily integrate stunning progress bars into your Node.js CLI applications, giving users a clear indication of ongoing processes.
Hash: 7e6e371fa415451645d785e7955586d3d881afded6cd7bf758ed146fc25acef2