progress

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

灵活的 ASCII 进度栏

中文使用文档
文档目录9 节
  1. 安装
  2. 使用量
  3. 选项
  4. 键
  5. 自定义键
  6. 实例
  7. 下载
  8. 中断
  9. 许可证

弹性ASCii 进度栏.

安装

$ npm install progress

使用量

首先我们创建 ProgressBar,给出格式字符串 此外 total,告诉进度栏何时会 视为完整。 在那之后,我们需要做的就是 tick() 适当的.

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);

选项

这些是选项对象中的密钥,您可以随同进程栏传递 total 如上文的例子所示.

  • curr 当前完成的索引
  • total 要完成的勾选总数
  • width 默认进度栏的显示宽度到总计
  • stream 输出流默认为 stderr
  • head 头字符默认为完整字符
  • complete 补全字符默认为"="
  • incomplete 默认为 "-" 的字符不完整
  • renderThrottle 默认为 16 毫秒的更新之间的最小时间
  • clear 选项,在完成时清除栏中默认为虚假
  • callback 可选函数, 当进度栏完成时调用

键

这些是您可以在进度栏格式中使用的符号 .

  • :bar 进度栏本身
  • :current 当前勾选编号
  • :total 全部勾选
  • :elapsed 时间( 秒)
  • :percent 完成百分比
  • :eta 估计完成时间(秒)
  • :rate 每秒滴答比例

自定义键

您可以添加一个自定义符 {'name': value} 方法的对象参数(tick(), (中文(简体) ), update())电话.

var bar = new ProgressBar(':current: :token1 :token2', { total: 3 })
bar.tick({
  'token1': "Hello",
  'token2': "World!\n"
})
bar.tick(2, {
  'token1': "Goodbye",
  'token2': "World!"
})

上述例子将导致以下产出.

1: Hello World!
3: Goodbye World!

实例

下载

在我们的下载例子中,每个勾勾都有可变的影响,所以我们通过块 相对于总数适当调整进度栏的长度 长度.

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();

以上例子形成了一个与下文类似的进步栏.

downloading [=====             ] 39/bps 29% 3.7s

中断

在进度栏执行时显示消息, 使用 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);

您可以在 examples 文件夹.

许可证

麻省理工学院

版本与发布记录

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 是什么?

灵活的 ASCII 进度栏

如何安装 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 和提交记录判断。