• 控制台下的字符图像界面


     
    // 输出流
    var stream = process.stdout;
     
    // 输出
    var write = function (str) {
      stream.write(str);
    };
     
    // 生成ANSI escape sequences代码:http://en.wikipedia.org/wiki/ANSI_escape_code
    var CSI = exports.CSI = function (str, c) {
      return '\x1b[' + str + c;
    };
     
    // 生成带颜色的文本
    var color = exports.color = function (text, c, bgc) {
      if (isNaN(bgc)) bgc = 0;
      c = Number(c) + 30;
      bgc = Number(bgc) + 40;
      var ret = CSI(c + ';' + bgc, 'm') + text + CSI('37;40', 'm');
      return ret;
    };
     
    // 设置光标位置,位置从1开始
    var setCursor = exports.setCursor = function (row, column) {
      write(CSI(row + ';' + column, 'H'));
    };
     
    // 清屏
    var clear = function (bgcolor) {
      setCursor(1, 1);
      for (var i = 0; i < stream.rows; i++) {
        var line = '';
        for (var j = 0; j < stream.columns; j++) {
          line += ' ';
        }
        write(color(line, 0, 6));
      }
    };
     
    // 画矩形
    var DOUBLE_LINE = '══════════════════════════════════════════════════════════' +
                      '══════════════════════════════════════════';
    var SPACE_LINE = '                                                           ' +
                     '                                         ';
    var drawRect = function (top, left, width, height, bgcolor) {
      setCursor(top, left);
      var w = width / 2 - 2;
      var c = function (str) {
        return color(str, 0, 7);
      };
      write(c('╔') + c(DOUBLE_LINE.substr(0, w)) + c('╗'));
      for (var i = 1; i < height - 1; i++) {
        setCursor(top + i, left)
        write(c('‖') + color(SPACE_LINE.substr(0, w), 0, bgcolor) +
              color(SPACE_LINE.substr(0, w), 0, bgcolor) + c('‖'));
      }
      setCursor(top + height - 1, left);
      write(c('╚') + c(DOUBLE_LINE.substr(0, w)) + c('╝'));
      setCursor(top + 1, left + 2);
    };
     
     
    // -----------------------------------------------------------------------------
    // --------------- 开始 --------------------------------------------------------
     
    // 开始
    clear();
    // 要绘制的窗口宽度和高度
    var w = 68;
    var h = 5;
    var t = (stream.rows - h) / 2;
    var l = (stream.columns - w) / 2;
    drawRect(t, l, w, h, 7);
    var randomColor = function () {
      var r = Math.round(Math.random() * 5);
      return r + 1;
    };
    var updateTime = function () {
      setCursor(t + 2, (stream.columns - 48) / 2);
      write(color(new Date().toLocaleString() + '   ', randomColor(), 7));
    };
    updateTime();
    setInterval(updateTime, 1000);
    
    机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年
  • 相关阅读:
    javaweb地图定位demo
    java基础循环
    java实现时钟
    栈和队列
    线程池
    java死锁及解决方案
    克隆
    算法与数据结构基础一
    重定向与转发的区别
    省选模拟57
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2888075.html
Copyright © 2020-2023  润新知