当前位置:网站首页>Node encapsulates a console progress bar plugin
Node encapsulates a console progress bar plugin
2022-07-30 18:34:00 【JYeontu】
说在前面
You've seen a lot of progress bars on the console, right??Does everyone know how the progress bar of the console is implemented??I've been writing a fewnode脚本工具,There is a need for progress display during the period,So I wrote a customizable progress bar plugin,Can be directly imported and configured to use.
插件效果

功能实现
console single line output
这里使用了single-line-logTo realize the console output line,single-line-logis in the console(或流)output on the same line inNode.js模块.When writing a progress bar or displaying status messages during a long operation,此功能非常有用.
- 安装
npm install single-line-log
- 示例代码
var log = require('single-line-log').stdout;
var timer, i = 0;
timer = setInterval(()=>{
log(i++ + ' % loading...');
if (i > 100 ) {
clearInterval(timer);
log('100% 加载完成');
}
},100);
console output colorful
这里使用了chalkto change the console output color,chalkis a color plugin,可以通过chalk.blue(‘hello world’)to change the color of the text,You can make your console output fancy with this plugin.
- 安装
npm install chalk
- 示例代码
const chalk = require('chalk');
console.log(chalk.red('我是红色文字'));
console.log(chalk.green('i am green text'));
console.log(chalk.yellow('i am yellow text'));
console.log(chalk.yellow.bgGreen('I am yellow text with green background'));
console.log(chalk.yellow.bgWhiteBright('I am yellow text with white background'));
console.log(chalk.underline.bgBlue('我有下划线'));
console.log(chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
));
const error = chalk.bold.red;
const warning = chalk.hex('#FFA500'); // Orange color
console.log(error('Error!'));
console.log(warning('Warning!'));
具体配置可以参考文档:https://www.npmjs.com/package/chalk
progress bar effect
The effect of the progress bar is mainly achieved through a single line of output,We only need to change the length and scale of the progress bar according to the parameters, numbers and tips,Here we encapsulate it into a class.
初始化配置
Configuration information can be passed in during initialization,The configuration not passed in is the default configuration,具体配置如下
constructor(config = {
}){
this.initConfig(config);
}
initConfig(config){
const defaultConfig = {
duration: 100,
current: 0,
block:'█',
showNumber:true,
tip:{
0: '努力加载中……',
50:'half loaded,不要着急……',
75:'it will be loaded soon……',
100:'加载完成'
},
color:'blue'
};
Object.assign(defaultConfig,config);
this.config = defaultConfig;
}
Update progress bar status
By passing in the current progress,The state of the progress bar can be modified.
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.floor(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('');
}
插件说明
配置说明
The current configuration items are as follows,Subsequent expansion can be continued as needed
config = {
duration: 100, //total progress
current: 0, //当前进度
block:'█',
showNumber:true,
tip:{
0: '努力加载中……',
50:'half loaded,不要着急……',
75:'it will be loaded soon……',
100:'加载完成'
},
color:'green'
}
- duration
total progress
- current
current progress
- block
显示块,如下图:

- showNumber
Whether to show the current progress,效果如下图

- tip
Prompts when configuring different progress,here in percent,如下图:




- color
Progress bar and text color,如下图

使用
1、安装依赖
npm install @jyeontu/progress-bar
2、在代码中引用
- 引入依赖
const progressBar = require('@jyeontu/progress-bar');
- 配置信息
const config = {
duration: 100,
current: 0,
block:'█',
showNumber:true,
tip:{
0: '努力加载中……',
50:'half loaded,不要着急……',
75:'it will be loaded soon……',
100:'加载完成'
},
color:'blue'
}
- timer simulation progress
var timer, i = 0;
let progressBarC = new progressBar(config);
timer = setInterval(()=>{
progressBarC.run(i++);
if (i > 100 ) {
clearInterval(timer);
}
},100);
3、完整示例代码
// const progressBar = require('./progressBar');
const progressBar = require('@jyeontu/progress-bar');
const config = {
duration: 100,
current: 0,
block:'█',
showNumber:true,
tip:{
0: '努力加载中……',
50:'half loaded,不要着急……',
75:'it will be loaded soon……',
100:'加载完成'
},
color:'blue'
}
var timer, i = 0;
let progressBarC = new progressBar(config);
timer = setInterval(()=>{
progressBarC.run(i++);
if (i > 100 ) {
clearInterval(timer);
}
},100);
源码地址
https://gitee.com/zheng_yongtao/node-scripting-tool/tree/master/src/progressBar
说在后面
这里是JYeontu,喜欢算法,GDCPC打过卡;热爱羽毛球,大运会打过酱油.毕业一年,两年前端开发经验,目前担任H5前端开发,算法业余爱好者,有空会刷刷算法题,平时喜欢打打羽毛球 ,也喜欢写些东西,既为自己记录,也希望可以对大家有那么一丢丢的帮助,写的不好望多多谅解,写错的地方望指出,定会认真改进,在此谢谢大家的支持,我们下文再见.
边栏推荐
- WeChat Mini Program Cloud Development | Urban Information Management
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
- kotlin by lazy
- 【HarmonyOS】【FAQ】鸿蒙问题合集3
- Recommended Books | Recommend 3 database books with rave reviews
- 【HMS core】【FAQ】Account Kit、MDM能力、push Kit典型问题合集6
- SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
- 【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
- OneFlow源码解析:Op、Kernel与解释器
- Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
猜你喜欢

One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...

基础架构之Mongo

千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践

NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...

Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL

【总结】1396- 60+个 VSCode 插件,打造好用的编辑器

Fixed asset visualization intelligent management system

荐号 | 对你有恩的人,不要请吃饭来报答

while,do while,for循环语句

【HMS core】【ML Kit】机器学习服务常见问题FAQ(二)
随机推荐
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
OneFlow源码解析:Op、Kernel与解释器
第4章 控制执行流程
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
Two-point answer naked question (plus a little pigeonhole principle)
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
微信小程序云开发 | 城市信息管理
基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
reporter undercover
What kind of framework is friendly to developers?
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
【HMS core】【ML Kit】机器学习服务常见问题FAQ(二)
一文读懂“语言模型”
Kettle(二):连接SQL Server数据库
DM8:单库单实例搭建本地数据守护服务
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
设计消息队列存储消息数据的 MySQL 表格
【总结】1396- 60+个 VSCode 插件,打造好用的编辑器
Linux-安装MySQL(详细教程)