// 输出流var stream = process.stdout; // 输出var write = function (str) { stream.write(str);}; // 生成ANSI escape sequences代码:http://en.wikipedia.org/wiki/ANSI_escape_codevar 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);