zoukankan      html  css  js  c++  java
  • node_readline (逐行读取)

    node官方文档

    用于逐行读取文件流, 和终端交互

    读取文件流

    const fs = require('fs');
    const readline = require('readline');
    
    var rl = readline.createInterface({
      input: fs.createReadStream('a.js'),
      crlfDelay: Infinity
    });
    
    rl.on('line', lineFd =>{
      console.log( lineFd);
    });
    
    // print
    第一行
    第2行
    第3行
    第4行
    

    终端交互

    const readline = require('readline');
    const util = require('util');
    
    const rl = readline.createInterface({input: process.stdin, output: process.stdout});
    
    function random(min, max) {
      return Math.random() * (max - min + 1)
    }
    
    function logger(target, obj = {}) {
      var inspect = util.inspect;
      if (typeof obj === 'object') {
        Object.assign(inspect.styles, obj);
      } else {
        Object.assign(inspect.styles, {
          [typeof target]: obj
        });
      }
      return inspect(target, {colors: true}).replace(/'/g, '');
    }
    
    class flEvent {
      constructor() {}
    
      // 流被暂停
      pause() {
        rl.on('pause', () => {});
      }
    
      // 流被恢复
      resume() {
        rl.on('resume', () => {
          this.downloadTimer = setInterval(() => this.downloadFeedback(), this.t);
        });
      }
    }
    
    // 模拟下载文件
    class Test extends flEvent {
      constructor() {
        super();
        this.progress = 1;
        this.downloadTimer;
        this.t = 1000 * 1;
      }
      run() {
        this.sigint();
        this.pause();
        this.resume();
    
        this.hello();
      }
    
      // 是否确定下载
      hello() {
        rl.question( logger('你确定下载这个文件吗? yes/no  >', 'green'), data => {
          if (this.isDownload(data)) {
            rl.close();
          } else {
            this.downloading()
          }
        })
      }
    
      // 下载中
      downloading() {
        rl.write(`开始下载! ${logger(this.progress, 'yellow')}% 
    `);
        // 每隔1秒 更与用户反馈
        this.downloadTimer = setInterval(() => this.downloadFeedback(), this.t);
      }
    
      // 监听ctrl+c的退出事件
      sigint() {
        rl.on('SIGINT', () => {
          clearInterval(this.downloadTimer);
          util.inspect.styles.string = 'red'
          rl.question( logger("确定要退出下载吗?yes/no >", 'red'), data => {
            rl.pause();
            if (!this.isDownload(data)) {
              rl.close(); // yes关闭流
              return;
            } else {
              rl.resume(); // no重启流
            }
          });
        });
      }
      downloadFeedback() {
        var num = Math.floor(random(10, 20));
        this.progress += num;
        if (this.isDownloadOk(this.progress)) {
          rl.write(`正在下载! ${logger('100', 'green')}% 
    `);
          rl.write( logger('全部下载完成!! ^_^', 'green') );
          rl.close();
          clearInterval(this.downloadTimer);
          this.downloadTimer = num = null;
          return;
        }
        rl.write(`正在下载! ${logger(this.progress, 'yellow')}% 
    `);
      }
    
      isDownload(data) {
        if (data.match(/^y(es)?$/i)) {
          return false;
        } else {
          return true;
        }
      }
    
      isDownloadOk(num) {
        if (num >= 100) {
          return true;
        } else {
          return false;
        }
      }
    }
    
    var test = new Test();
    test.run();
    
  • 相关阅读:
    【特种兵系列】String中的==和equals()
    windows7 x64下maven安装和配置
    微信开发之本地接口调试(非80端口,无需上传服务器服务器访问)
    Java微信公众平台接口封装源码分享
    jdk安装
    用户权限管理
    Linux常用命令(一)
    Xshell显示中文乱码问题
    伪装虽易测试不易之微信浏览器
    每次新建项目出现appcompat_v7 解决方法
  • 原文地址:https://www.cnblogs.com/ajanuw/p/8196194.html
Copyright © 2011-2022 走看看