Skip to content

Instantly share code, notes, and snippets.

@erhhung
Last active August 11, 2018 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erhhung/3b8bdcbac3004907acf28de40a1cf21a to your computer and use it in GitHub Desktop.
Save erhhung/3b8bdcbac3004907acf28de40a1cf21a to your computer and use it in GitHub Desktop.
Throttled download progress bar example for "bubkoo/ascii-progress"
var ProgressBar = require('../index.js');
var bars = [];
var total = 1000;
var left = total;
var count = 0;
var simul = 5;
var timer = setInterval(function () {
if (left < 1) {
clearInterval(timer);
return;
}
while (count < total && bars.length < simul) {
var opts = {
schema: 'file_' + zeroPad(count++, 3) + '.jpg: [:bar]'
};
if (count < simul) {
opts.current = (simul - count) * 20;
}
bars.push(new ProgressBar(opts));
}
for (var i = 0; i < bars.length; i++) {
var bar = bars[i];
bar.tick(5);
if (!bar.completed) {
continue;
}
var time = (new Date - bar.start) / 1000;
time = 'time ' + zeroPad(time.toFixed(1), 4) + ' sec';
var mem = process.memoryUsage().heapUsed / 1024 / 1024;
mem = ', heap ' + zeroPad(mem.toFixed(2), 5) + ' MB';
bar.render(bar.schema.replace('[:bar]', time + mem));
bars.splice(i--, 1);
left--;
}
}, 50);
function zeroPad(value, width) {
var v = String(value || '');
var w = width | 0;
if (w < 1) {
return v;
}
return '0000000000000000'.substr(0, w - v.length) + v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment