学习标准输入输出流

根据老外的教程,动手做demo,感觉棒棒哒 ^_^

//实践是检验真理的唯一标准
var q = ['what\'s your name', 'how old are you', 'what do you like']

var question_proxy = function () {
  var count = 0;

  function question () {
    process.stdout.write(q[count] + '>');
    count++;
  }

  function question_proxy() {
    if (count === q.length) {
      process.exit();
    } else {
      question();
    }
  }

  return question_proxy;
}();

question_proxy();

process.stdin.on('data', function(d) {
  question_proxy();
});

图片描述

loading百分比demo

var setLoading = function(total, delay) {
  var over = 0;
  var timer = setInterval(function() {
    over += delay;
    process.stdout.cursorTo(0);
    process.stdout.write(`waiting...${Math.floor(over / total * 100)}%`);
    if (over >= total) {
      clearInterval(timer);
      process.exit();
    }
  }, delay);
};

setLoading(3000, 50);

图片描述


erichooooow
229 声望5 粉丝

simple is not easy