-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprogressbar.js
More file actions
53 lines (47 loc) · 1.43 KB
/
Copy pathprogressbar.js
File metadata and controls
53 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// https://www.jianshu.com/p/9f7132967111
/**
* 进度条实现。
*/
const log = require('single-line-log').stdout;
const strformat = require('str-format').format;
const clicolor = require('cli-color');
/**
* 封装一个进度条工具。
*/
function ProgressBar(description, bar_length) {
this.description = description || "PROGRESS";
this.length = bar_length.length || 28;
// 刷新进度条图案,文字的方法
this.render = function(opts) {
var percentage = (opts.completed / opts.total).toFixed(4);
var cell_num = Math.floor(percentage * this.length);
// 拼接黑色条
var cell = '';
for (var i = 0; i < cell_num; i++) {
cell += '█';
}
// 拼接灰色条
var empty = '';
for (var i = 0; i < this.length - cell_num; i++) {
empty += '░';
}
var percent = (100 * percentage).toFixed(2);
/**
* 使用cli-color进行包装美化。
*/
this.description = clicolor.blue.bold(this.description);
cell = clicolor.green.bgBlack.bold(cell);
opts.completed = clicolor.yellow.bold(opts.completed);
opts.total = clicolor.blue.bold(opts.total);
opts.status = percent == 100.00 ? clicolor.green.bold(opts.successText) : clicolor.red.bold(opts.loadingText);
// 拼接最终文本
cmdtext = strformat("<{}:{}%> {}{} [ {}/{} `{}`]", [this.description, percent,
cell, empty, opts.completed, opts.total, opts.status
]);
log(cmdtext);
};
}
/**
* 模块导出。
*/
module.exports = ProgressBar;