zoukankan      html  css  js  c++  java
  • nodejs终端字符样式和进度条

    Nodejs为终端字符增加样式

    只有黑白的色调对于比较复杂的命令行程序来说就显得太单调了,我们可以为命令行程序增加样式使得程序更加友好!

    安装package: npm install -S chalk

    #!/bin/env node
    
    const chalk = require('chalk');
    const log = console.log;
    
    // Pass in multiple arguments
    log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
    
    // Nest styles
    log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
    
    log(`
    CPU: ${chalk.red('90%')}
    RAM: ${chalk.green('40%')}
    DISK: ${chalk.yellow('70%')}
    `);
    
    
    const error = chalk.bold.red;
    const warning = chalk.keyword('orange');
    
    console.log(error('Error!'));
    console.log(warning('Warning!'));
    

    为长时间任务增加进度显示

    progress是当前最流行的用于渲染进度条的npm包。

    var ProgressBar = require('progress');
    
    var bar = new ProgressBar(':bar :current/:total', { total: 10 });
    var timer = setInterval(function () {
      bar.tick();
      if (bar.complete) {
        clearInterval(timer);
      } else if (bar.curr === 5) {
          bar.interrupt('this message appears above the progress bar
    current progress is ' + bar.curr + '/' + bar.total);
      }
    }, 1000);
    

    输出:
    this message appears above the progress bar
    current progress is 5/10
    ========== 10/10

  • 相关阅读:
    「考试」省选27
    「考试」省选26
    「考试」省选25
    $dy$讲课总结
    「笔记」$Min\_25$筛
    「考试」省选24
    「总结」多项式生成函数例题(4)
    「总结」多项式生成函数相关(4)
    「考试」省选23
    「总结」后缀3
  • 原文地址:https://www.cnblogs.com/itech/p/13155641.html
Copyright © 2011-2022 走看看