当前位置:网站首页>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前端开发,算法业余爱好者,有空会刷刷算法题,平时喜欢打打羽毛球 ,也喜欢写些东西,既为自己记录,也希望可以对大家有那么一丢丢的帮助,写的不好望多多谅解,写错的地方望指出,定会认真改进,在此谢谢大家的支持,我们下文再见.
边栏推荐
- CCNA-NAT协议(理论与实验练习)
- kotlin的by lazy
- 【剑指 Offe】剑指 Offer 17. 打印从1到最大的n位数
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- C# wpf 无边框窗口添加阴影效果
- ESP8266-Arduino programming example-HC-SR04 ultrasonic sensor driver
- Confluence OGNL注入漏洞复现(CVE-2022-26134)
- OSPF详解(3)
- 怎么样的框架对于开发者是友好的?
- MySQL数据类型
猜你喜欢

网络基础(三)01-网络的基础概念——URL地址组成之协议、主机地址、路径和参数&127.0.0.1本地回环地址& 查看网址IP地址并访问之ping空格+网址&netstat -anb查看本机占用端口

运营 23 年,昔日“国内第一大电商网站”黄了...
![[Summary] 1396- 60+ VSCode plugins to create a useful editor](/img/e4/65e55d0e4948c011585b72733d4d19.jpg)
[Summary] 1396- 60+ VSCode plugins to create a useful editor

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

你好好想想,你真的需要配置中心吗?

NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group

【HarmonyOS】【FAQ】鸿蒙问题合集4

MySQL data types
随机推荐
Anaconda Navigator stuck on loading applications
SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
Presto 中 lookUp Join的实现
轻量级网络 ESPNetv2
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
开源盛宴ApacheCon Asia 2022即将开幕,精彩不容错过!
网络基础(二)-Web服务器-简介——WampServer集成服务器软件之Apache+MySQL软件安装流程 & netstat -an之检测计算机的端口是否占用
荐号 | 对你有恩的人,不要请吃饭来报答
"Ruffian Heng Embedded Bimonthly" Issue 59
猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%
CMake library search function does not search LD_LIBRARY_PATH
终端分屏工具Terminalx的使用
Recommended Books | Recommend 3 database books with rave reviews
WeChat Mini Program Cloud Development | Urban Information Management
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
【HarmonyOS】【FAQ】鸿蒙问题合集4