Part of 360 Bench, tested fixes for abandoned npm packages.
Flexible ascii progress bar.
This is a fork of progress by TJ Holowaychuk and its contributors, published as a drop-in replacement that fixes long-standing crashes. The upstream package has not been released since v2.0.3 (December 2018), and the reported crash bugs below are still open. All credit for the original library goes to its author and contributors; it remains available under the same MIT license.
Based on upstream [email protected]; the API and the normal output are unchanged.
- No more
RangeError: Invalid array lengthwhentotalis not a usable number. The classic case is a download progress bar built withtotal: parseInt(res.headers['content-length'], 10)when the server sends noContent-Length(chunked responses, many CDNs), which makestotalNaN. The bar now renders as empty (0%) instead of killing the process. Atotalof0no longer crashes either (the bar completes at 100%), nor does a fractionaltotalsuch as10.5when nowidthis given. Upstream issue: #166 (also reported in #171). - No more
RangeError: Invalid array lengthwhen the output stream has nocolumns. This happens when a piped/non-terminal stream is forced into TTY mode (the workaround suggested in upstream #110 and #179) or with custom streams. The bar now assumes an 80-column terminal in that case. Upstream issue: #166. - A
widththat is not a number now falls back to the available terminal width instead of crashing, as proposed in upstream PR #205. - No more
TypeError: this.stream.clearLine is not a functionwhen callinginterrupt()while output is piped or redirected to a file. The message is written as a plain line instead, the same way upstream already handlesterminate()(fixed there in #144). Upstream issue: #224.
$ npm install @fitzyracing/progressprogress is mostly installed as a transitive dependency of CLI tools. To make your
whole dependency tree use this fork without changing any code, add an override to
your package.json:
{
"overrides": {
"progress": "npm:@fitzyracing/progress@^2.0.4"
}
}(Yarn: use "resolutions"; pnpm: "pnpm": { "overrides": { ... } }. Tested with npm 10 and 11;
very early npm 9 releases such as 9.2.0 reject aliased overrides with "Invalid comparator", so upgrade npm if you see that.)
Or alias it directly in your own dependencies, so require('progress') keeps working:
$ npm install progress@npm:@fitzyracing/progressTypes for progress live in @types/progress
and this fork does not change the API. With the override or the alias above, the package is
still installed as progress, so @types/progress keeps working unchanged.
If you import the scoped name directly (import ProgressBar = require('@fitzyracing/progress')),
install @types/progress and add a small declaration file to your project:
// progress-fork.d.ts
declare module '@fitzyracing/progress' {
import ProgressBar = require('progress');
export = ProgressBar;
}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('@fitzyracing/progress');
var bar = new ProgressBar(':bar', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(timer);
}
}, 100);These are keys in the options object you can pass to the progress bar along with
total as seen in the example above.
currcurrent completed indextotaltotal number of ticks to completewidththe displayed width of the progress bar defaulting to totalstreamthe output stream defaulting to stderrheadhead character defaulting to complete charactercompletecompletion character defaulting to "="incompleteincomplete character defaulting to "-"renderThrottleminimum time between updates in milliseconds defaulting to 16clearoption to clear the bar on completion defaulting to falsecallbackoptional 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:totaltotal ticks:elapsedtime elapsed in seconds:percentcompletion percentage:etaestimated completion time in seconds:raterate of ticks per second
You can define custom tokens by adding a {'name': value} object parameter to your method (tick(), update(), etc.) calls.
var bar = new ProgressBar(':current: :token1 :token2', { total: 3 })
bar.tick({
'token1': "Hello",
'token2': "World!\n"
})
bar.tick(2, {
'token1': "Goodbye",
'token2': "World!"
})The above example would result in the output below.
1: Hello World!
3: Goodbye World!
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('@fitzyracing/progress');
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] :rate/bps :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 [===== ] 39/bps 29% 3.7s
To display a message during progress bar execution, use interrupt()
var ProgressBar = require('@fitzyracing/progress');
var bar = new ProgressBar(':bar :current/:total', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
clearInterval(timer);
} else if (bar.curr === 5) {
bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
}
}, 1000);You can see more examples in the examples folder.
To report a security vulnerability in this fork, please use GitHub private vulnerability reporting rather than a public issue.
MIT - Copyright (c) 2017 TJ Holowaychuk. The original license and copyright notice are retained unchanged; fork changes are released under the same license.
Original project: https://github.com/visionmedia/node-progress
- In-repo docs: docs/
- Wiki: https://github.com/fitzyracing1/progress/wiki
This project is free, and its license stays as is. Support it through GitHub Sponsors:
- Free: use the code, community support.
- Pro ($49/month): priority issues and support.
- Team ($199/month): priority support for your team plus early access.