Flexible ascii progress bar
Flexible ascii progress bar.
$ npm install progress
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
var ProgressBar = require'progress';var bar = ':bar' total: 10 ;var timer = setIntervalbartick;if barcompleteconsole.log'\ncomplete\n';clearIntervaltimer;100;
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 complete
widththe displayed width of the progress bar defaulting to total
streamthe output stream defaulting to stderr
completecompletion character defaulting to "="
incompleteincomplete character defaulting to "-"
clearoption to clear the bar on completion defaulting to false
callbackoptional function to call when the progress bar completes
These are tokens you can use in the format of your progress bar.
:barthe progress bar itself
:currentcurrent tick number
:elapsedtime elapsed in seconds
:etaestimated completion time in seconds
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 = httpsrequesthost: 'download.github.com'port: 443path: '/visionmedia-node-jscoverage-0d4608a.zip';reqon'response'var len = parseIntresheaders'content-length' 10;console.log;var bar = ' downloading [:bar] :percent :etas'complete: '='incomplete: ' 'width: 20total: len;reson'data'bartickchunklength;;reson'end'console.log'\n';;;reqend;
The above example result in a progress bar like the one below.
downloading [===== ] 29% 3.7s
You can see more examples in the