Created
August 14, 2015 06:59
-
-
Save tudousi/757360239a04aed732a3 to your computer and use it in GitHub Desktop.
tty和控制台颜色学习
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tty = require('tty'); | |
var util = require('util'); | |
/* | |
console.log('isTTY: ' + process.stdout.isTTY); // 当标准输出为终端时,isTTY 就是为 true。 | |
console.log(util.inspect(process.stdin)); | |
console.log('isatty 0: ' + tty.isatty(0)); | |
console.log('isatty 1: ' + tty.isatty(1)); | |
console.log('isatty 2: ' + tty.isatty(2)); // 若 fd 关联于中端则返回 true,反之返回 false | |
*/ | |
/* | |
console.log('stdin.fd: ' + process.stdin.fd); | |
console.log('stdout.fd: ' + process.stdout.fd); | |
console.log('stderr.fd: ' + process.stderr.fd); | |
console.log(util.inspect(process.stdin)); | |
*/ | |
/* | |
// 终端输入输出fd | |
stdin.fd: 0 | |
stdout.fd: 1 | |
stderr.fd: 2 | |
*/ | |
/** | |
* 关于控制台颜色,更多信息看wiki | |
* https://en.wikipedia.org/wiki/ANSI_escape_code#graphics | |
*/ | |
var outColor = (function(){ | |
var colors = [6, 2, 3, 4, 5, 1]; | |
var prevColor = 0; | |
var isatty = tty.isatty(2); | |
console.log(isatty); | |
// 打印颜色文本 | |
function txtColor(txt){ | |
if(!isatty){ | |
console.log(txt); | |
return; | |
} | |
var c = colors[prevColor++ % colors.length]; | |
console.log('\033[3' + c +'m ' + txt + '\033[0m'); | |
} | |
return txtColor; | |
})(); | |
outColor('颜色代码 033[36m '); | |
outColor('颜色代码 033[32m '); | |
outColor('颜色代码 033[33m '); | |
outColor('颜色代码 033[34m '); | |
outColor('颜色代码 033[35m '); | |
outColor('颜色代码 033[31m '); | |
//console.log("\033[34m aaa \033[90m bbb"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment