progress

中文文档
2.0.3发布于 2018/12/06MITCommonJS零依赖近 30 天下载 2.4亿

Flexible ascii progress bar

原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. Options
  4. Tokens
  5. Custom Tokens
  6. Examples
  7. Download
  8. Interrupt
  9. License

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.

  • curr current completed index
  • total total number of ticks to complete
  • width the displayed width of the progress bar defaulting to total
  • stream the output stream defaulting to stderr
  • head head character defaulting to complete character
  • complete completion character defaulting to "="
  • incomplete incomplete character defaulting to "-"
  • renderThrottle minimum time between updates in milliseconds defaulting to 16
  • clear option to clear the bar on completion defaulting to false
  • callback optional function to call when the progress bar completes

Tokens

These are tokens you can use in the format of your progress bar.

  • :bar the progress bar itself
  • :current current tick number
  • :total total ticks
  • :elapsed time elapsed in seconds
  • :percent completion percentage
  • :eta estimated completion time in seconds
  • :rate rate of ticks per second

Custom Tokens

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!

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('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

Interrupt

To display a message during progress bar execution, use interrupt()

var ProgressBar = require('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.

License

MIT

版本与发布记录

progress 共发布 20 个版本,首次发布于 2011/04/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2018/12/067 年前
2.0.22018/12/037 年前
2.0.12018/10/178 年前
2.0.02017/04/059 年前
1.1.82014/08/1012 年前
1.1.72014/07/0112 年前

关于 progress 的常见问题

progress 是什么?

Flexible ascii progress bar

如何安装 progress?

在项目目录运行 npm install progress,或使用 pnpm add / yarn add / bun add。

progress 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/progress 是否存在。

progress 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

progress 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

progress 可以商用吗?

progress 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

progress 的下载量有多少?

根据 npm 官方下载统计,progress 在 2026/10/05 – 2026/11/03 这 30 天内被下载 236,024,363 次,日均约 7,867,479 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

progress 还在维护吗?

最新版本 2.0.3 发布于 2018/12/06(7 年前);至今共发布 20 个版本,其中过去 12 个月发布了 0 个;首次发布于 2011/04/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。