single-line-log应用

本文共--字 阅读约--分钟 | 浏览: -- Last Updated: 2021-12-15

命令行单行输出的简单应用

var slog = require('single-line-log').stdout;

var index = 0;

var timer = setInterval(() => {
  index++;
  if (index > 100) {
    clearInterval(timer)
    console.log('\n下载完成')
  } else {
    slog(`下载进度${index}%`)
  }
}, 50)

应用2:单行输出进度条

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 += '█';
  }

  // 拼接灰色条
  var empty = '';
  for (var i=0;i<this.length-cell_num;i++) {
   empty += '░';
  }

  // 拼接最终文本
  var cmdText = this.description + ': ' + (100*percent).toFixed(2) + '% ' + cell + empty + ' ' + opts.completed + '/' + opts.total;
  
  // 在单行输出文本
  slog(cmdText);
 };
}

// 初始化一个进度条长度为 50 的 ProgressBar 实例
var pb = new ProgressBar('下载进度', 50);

// 这里只是一个 pb 的使用示例,不包含任何功能
var num = 0, total = 200;
(function downloading() {
 if (num <= total) {
  // 更新进度条
  pb.render({ completed: num, total: total });

  num++;
  setTimeout(function (){
   downloading();
  }, 25)
 }
}())