Home > Web Front-end > JS Tutorial > body text

Share an example code of nodejs terminal printing progress bar

零下一度
Release: 2017-04-25 10:44:47
Original
1692 people have browsed it

This article mainly introduces the nodejs terminal printing progress bar example code. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look

1. Scene import

When we batch process a large number of files (for example: upload/download, Saving, compiling, etc.), we often want to know the current progress or how many failed (successful) tasks; when our code or program has been released and the user is performing the installation process, an appropriate (terminal/command line) progress The bar can accurately reflect the steps and processes of the installation, improve the usability of the program, and alleviate the user's worries while waiting to a certain extent...

2. Basic Principle

First of all, printing out text in the terminal is relatively easy. Then using simple text and symbols, you can piece together the effect of the command line (example below):

File uploaded: 43.60% ████████████████ 150/344

Of course, the progress bar You can design the effect yourself according to your needs. I am just giving you a reference here.

Here, I construct the method of printing the command line into a tool module progress-bar.js, the specific implementation is as follows:-)


// 这里用到一个很实用的 npm 模块,用以在同一行打印文本
var slog = require('single-line-log').stdout;
// 封装的 ProgressBar 工具
function ProgressBar(description, bar_length){
 // 两个基本参数(属性)
 this.description = description || 'Progress';    // 命令行开头的文字信息
 this.length = bar_length || 25;           // 进度条的长度(单位:字符),默认设为 25
 // 刷新进度条图案、文字的方法
 this.render = function (opts){
  var percent = (opts.completed / opts.total).toFixed(4);  // 计算进度(子任务的 完成数 除以 总数)
  var cell_num = Math.floor(percent * this.length);       // 计算需要多少个 █ 符号来拼凑图案
  // 拼接黑色条
  var cell = '';
  for (var i=0;i<cell_num;i++) {
   cell += &#39;█&#39;;
  }
  // 拼接灰色条
  var empty = &#39;&#39;;
  for (var i=0;i<this.length-cell_num;i++) {
   empty += &#39;░&#39;;
  }
  // 拼接最终文本
  var cmdText = this.description + &#39;: &#39; + (100*percent).toFixed(2) + &#39;% &#39; + cell + empty + &#39; &#39; + opts.completed + &#39;/&#39; + opts.total;
  // 在单行输出文本
  slog(cmdText);
 };
}
// 模块导出
module.exports = ProgressBar;
Copy after login

3. Run

Based on the above implementation, let’s talk about the usage of this progress-bar.js :


// 引入工具模块
var ProgressBar = require(&#39;./progress_bar&#39;);
// 初始化一个进度条长度为 50 的 ProgressBar 实例
var pb = new ProgressBar(&#39;下载进度&#39;, 50);
// 这里只是一个 pb 的使用示例,不包含任何功能
var num = 0, total = 200;
function downloading() {
 if (num <= total) {
  // 更新进度条
  pb.render({ completed: num, total: total });
  num++;
  setTimeout(function (){
   downloading();
  }, 500)
 }
}
downloading();
Copy after login

Run the above code, the execution effect is as follows:

Students who need to learn js please pay attention to php Chinese website js video tutorial, many js online video tutorials can be watched for free!

The above is the detailed content of Share an example code of nodejs terminal printing progress bar. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template