Flexible ascii progress bar.
Installation
$ npm install progress
Usage
First we create a ProgressBar, giving it a format string as well as the total, telling the progress bar when it will be considered complete. After that all we need to do is tick() appropriately.
var ProgressBar = require('progress');
var bar = new ProgressBar(':bar', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(timer);
}
}, 100);
Options
These are keys in the options object you can pass to the progress bar along with total as seen in the example above.
totaltotal number of ticks to completewidththe displayed width of the progress bar defaulting to totalstreamthe output stream defaulting to stderrcompletecompletion character defaulting to "="incompleteincomplete character defaulting to "-"clearoption to clear the bar on completion defaulting to falsecallbackoptional function to call when the progress bar completes
Tokens
These are tokens you can use in the format of your progress bar.
:barthe progress bar itself:currentcurrent tick number:totaltotal ticks:elapsedtime elapsed in seconds:percentcompletion percentage:etaestimated completion time in seconds
Examples
Download
In our download example each tick has a variable influence, so we pass the chunk length which adjusts the progress bar appropriately relative to the total length.
var ProgressBar = require('../');
var https = require('https');
var req = https.request({
host: 'download.github.com',
port: 443,
path: '/visionmedia-node-jscoverage-0d4608a.zip'
});
req.on('response', function(res){
var len = parseInt(res.headers['content-length'], 10);
console.log();
var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 20,
total: len
});
res.on('data', function (chunk) {
bar.tick(chunk.length);
});
res.on('end', function () {
console.log('\n');
});
});
req.end();
The above example result in a progress bar like the one below.
downloading [===== ] 29% 3.7s
You can see more examples in the examples folder.
License
MIT
Source
| 1 | Flexible ascii progress bar. |
| 2 | |
| 3 | ## Installation |
| 4 | |
| 5 | ```bash |
| 6 | $ npm install progress |
| 7 | ``` |
| 8 | |
| 9 | ## Usage |
| 10 | |
| 11 | First we create a `ProgressBar`, giving it a format string |
| 12 | as well as the `total`, telling the progress bar when it will |
| 13 | be considered complete. After that all we need to do is `tick()` appropriately. |
| 14 | |
| 15 | ```javascript |
| 16 | var ProgressBar = require('progress'); |
| 17 | |
| 18 | var bar = new ProgressBar(':bar', { total: 10 }); |
| 19 | var timer = setInterval(function () { |
| 20 | bar.tick(); |
| 21 | if (bar.complete) { |
| 22 | console.log('\ncomplete\n'); |
| 23 | clearInterval(timer); |
| 24 | } |
| 25 | }, 100); |
| 26 | ``` |
| 27 | |
| 28 | ### Options |
| 29 | |
| 30 | These are keys in the options object you can pass to the progress bar along with |
| 31 | `total` as seen in the example above. |
| 32 | |
| 33 | - `total` total number of ticks to complete |
| 34 | - `width` the displayed width of the progress bar defaulting to total |
| 35 | - `stream` the output stream defaulting to stderr |
| 36 | - `complete` completion character defaulting to "=" |
| 37 | - `incomplete` incomplete character defaulting to "-" |
| 38 | - `clear` option to clear the bar on completion defaulting to false |
| 39 | - `callback` optional function to call when the progress bar completes |
| 40 | |
| 41 | ### Tokens |
| 42 | |
| 43 | These are tokens you can use in the format of your progress bar. |
| 44 | |
| 45 | - `:bar` the progress bar itself |
| 46 | - `:current` current tick number |
| 47 | - `:total` total ticks |
| 48 | - `:elapsed` time elapsed in seconds |
| 49 | - `:percent` completion percentage |
| 50 | - `:eta` estimated completion time in seconds |
| 51 | |
| 52 | ## Examples |
| 53 | |
| 54 | ### Download |
| 55 | |
| 56 | In our download example each tick has a variable influence, so we pass the chunk |
| 57 | length which adjusts the progress bar appropriately relative to the total |
| 58 | length. |
| 59 | |
| 60 | ```javascript |
| 61 | var ProgressBar = require('../'); |
| 62 | var https = require('https'); |
| 63 | |
| 64 | var req = https.request({ |
| 65 | host: 'download.github.com', |
| 66 | port: 443, |
| 67 | path: '/visionmedia-node-jscoverage-0d4608a.zip' |
| 68 | }); |
| 69 | |
| 70 | req.on('response', function(res){ |
| 71 | var len = parseInt(res.headers['content-length'], 10); |
| 72 | |
| 73 | console.log(); |
| 74 | var bar = new ProgressBar(' downloading [:bar] :percent :etas', { |
| 75 | complete: '=', |
| 76 | incomplete: ' ', |
| 77 | width: 20, |
| 78 | total: len |
| 79 | }); |
| 80 | |
| 81 | res.on('data', function (chunk) { |
| 82 | bar.tick(chunk.length); |
| 83 | }); |
| 84 | |
| 85 | res.on('end', function () { |
| 86 | console.log('\n'); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | req.end(); |
| 91 | ``` |
| 92 | |
| 93 | The above example result in a progress bar like the one below. |
| 94 | |
| 95 | ``` |
| 96 | downloading [===== ] 29% 3.7s |
| 97 | ``` |
| 98 | |
| 99 | You can see more examples in the `examples` folder. |
| 100 | |
| 101 | ## License |
| 102 | |
| 103 | MIT |