@jyeontu/progress-bar
Version:
node控制台进度条输出工具
41 lines (40 loc) • 1.35 kB
JavaScript
const singleRowLog = require('single-line-log').stdout;
const chalk = require("chalk");
class progressBar{
constructor(config = {}){
this.initConfig(config);
}
initConfig(config){
const defaultConfig = {
duration: 100,
current: 0,
block:'█',
showNumber:true,
tip:{
0: '努力加载中……',
50:'加载一半啦,不要着急……',
75:'马上就加载完了……',
100:'加载完成'
},
color:'blue'
};
Object.assign(defaultConfig,config);
this.config = defaultConfig;
}
run(current){
const {block, duration, tip, color,showNumber} = this.config;
let tipList = Object.keys(tip).sort((a,b)=> b-a);
let showTip = tip[0];
const step = duration / 100;
const len = Math.ceil(current / step);
for(let i = 0; i < tipList.length; i++){
if(len >= tipList[i]) {
showTip = tip[tipList[i]];
break;
}
}
singleRowLog(chalk[color](block.repeat(Math.floor(len / 2)),(showNumber ? (len + '% ') : '') + showTip));
if(len == 100) console.log('');
}
}
module.exports = progressBar;