1 Flexible ascii progress bar.
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.
16 var ProgressBar = require('progress');
18 var bar = new ProgressBar(':bar', { total: 10 });
19 var timer = setInterval(function () {
22 console.log('\ncomplete\n');
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.
33 - `curr` current completed index
34 - `total` total number of ticks to complete
35 - `width` the displayed width of the progress bar defaulting to total
36 - `stream` the output stream defaulting to stderr
37 - `head` head character defaulting to complete character
38 - `complete` completion character defaulting to "="
39 - `incomplete` incomplete character defaulting to "-"
40 - `renderThrottle` minimum time between updates in milliseconds defaulting to 16
41 - `clear` option to clear the bar on completion defaulting to false
42 - `callback` optional function to call when the progress bar completes
46 These are tokens you can use in the format of your progress bar.
48 - `:bar` the progress bar itself
49 - `:current` current tick number
50 - `:total` total ticks
51 - `:elapsed` time elapsed in seconds
52 - `:percent` completion percentage
53 - `:eta` estimated completion time in seconds
54 - `:rate` rate of ticks per second
58 You can define custom tokens by adding a `{'name': value}` object parameter to your method (`tick()`, `update()`, etc.) calls.
61 var bar = new ProgressBar(':current: :token1 :token2', { total: 3 })
71 The above example would result in the output below.
82 In our download example each tick has a variable influence, so we pass the chunk
83 length which adjusts the progress bar appropriately relative to the total
87 var ProgressBar = require('progress');
88 var https = require('https');
90 var req = https.request({
91 host: 'download.github.com',
93 path: '/visionmedia-node-jscoverage-0d4608a.zip'
96 req.on('response', function(res){
97 var len = parseInt(res.headers['content-length'], 10);
100 var bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', {
107 res.on('data', function (chunk) {
108 bar.tick(chunk.length);
111 res.on('end', function () {
119 The above example result in a progress bar like the one below.
122 downloading [===== ] 39/bps 29% 3.7s
127 To display a message during progress bar execution, use `interrupt()`
129 var ProgressBar = require('progress');
131 var bar = new ProgressBar(':bar :current/:total', { total: 10 });
132 var timer = setInterval(function () {
135 clearInterval(timer);
136 } else if (bar.curr === 5) {
137 bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
142 You can see more examples in the `examples` folder.