| 2 | const _colors = require('ansi-colors'); |
| 3 | |
| 4 | function Example5(){ |
| 5 | console.log(''); |
| 6 | // create new progress bar |
| 7 | const b1 = new _progress.Bar({ |
| 8 | format: 'CLI Progress |' + _colors.cyan('{bar}') + '| {percentage}% || {value}/{total} Chunks || Speed: {speed}', |
| 9 | barCompleteChar: '\u2588', |
| 10 | barIncompleteChar: '\u2591', |
| 11 | hideCursor: true |
| 12 | }); |
| 13 | |
| 14 | // initialize the bar - defining payload token "speed" with the default value "N/A" |
| 15 | b1.start(200, 0, { |
| 16 | speed: "N/A" |
| 17 | }); |
| 18 | |
| 19 | // the bar value - will be linear incremented |
| 20 | let value = 0; |
| 21 | |
| 22 | const speedData = []; |
| 23 | |
| 24 | // 20ms update rate |
| 25 | const timer = setInterval(function(){ |
| 26 | // increment value |
| 27 | value++; |
| 28 | |
| 29 | // example speed data |
| 30 | speedData.push(Math.random()*2+5); |
| 31 | const currentSpeedData = speedData.splice(-10); |
| 32 | |
| 33 | // update the bar value |
| 34 | b1.update(value, { |
| 35 | speed: (currentSpeedData.reduce(function(a, b) { return a + b; }, 0) / currentSpeedData.length).toFixed(2) + "Mb/s" |
| 36 | }); |
| 37 | |
| 38 | // set limit |
| 39 | if (value >= b1.getTotal()){ |
| 40 | // stop timer |
| 41 | clearInterval(timer); |
| 42 | |
| 43 | b1.stop(); |
| 44 | } |
| 45 | }, 20); |
| 46 | } |
| 47 | |
| 48 | Example5(); |