Skip to content

Instantly share code, notes, and snippets.

@kasterra
Created August 9, 2022 09:21
Show Gist options
  • Save kasterra/e002c1ccf57d5368205ce731febb19c1 to your computer and use it in GitHub Desktop.
Save kasterra/e002c1ccf57d5368205ce731febb19c1 to your computer and use it in GitHub Desktop.
MultiBar test from cli-progress npm module
import { MultiBar } from "cli-progress";
const files = {
"eta.js ": 187,
"generic-bar.js": 589,
"multi-bar.js ": 5342,
"options.js ": 42,
"single-bar.js ": 2123,
"terminal.js ": 4123,
};
const bars = [];
let a;
// create new container
const multibar = new MultiBar({
format: ' {bar} | "{file}" | {value}/{total}',
hideCursor: true,
barCompleteChar: "\u2588",
barIncompleteChar: "\u2591",
// only the bars will be cleared, not the logged content
clearOnComplete: true,
stopOnComplete: true,
// important! redraw everything to avoid "empty" completed bars
forceRedraw: true,
});
console.log("Downloading files..\n");
// add bars
for (const filename in files) {
const size = files[filename];
a = multibar.create(size, 0, { file: filename });
bars.push(a);
}
console.log(multibar);
const timer = setInterval(function () {
// increment
for (let i = 0; i < bars.length; i++) {
const bar = bars[i];
// download complete ?
if (bar.value < bar.total) {
bar.increment();
}
}
// progress bar running ?
// check "isActive" property in case you've enabled "stopOnComplete" !
if (multibar.isActive === false) {
clearInterval(timer);
multibar.stop();
console.log("Download complete!");
}
}, 3);
let numMessages = 0;
const loggingTimer = setInterval(function () {
// don't forget to add a linebreak !!!
multibar.log(
`[${new Date().toString()}] I'm logging message #${numMessages++}\n`
);
}, 1500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment