前言
Console的各种方法默认没有颜色,这样的话,在信息量大的时候真不好找.幸好已经有人提供了Node.Js的颜色模块 Colors.
基本使用方法
const colors = require( "colors")
//使用 colors 模块
console.log('红色'.red);
console.log('绿色'.green);
console.log('下划线加白色'.underline.white)
console.log('反转颜色'.inverse);
console.log('彩虹色'.rainbow);
console.log("乱七八糟".zalgo);
自定义名称方法
const colors = require( "colors")
//使用 colors 模块
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
console.log("this is an error".error);
console.log("this is a warning".warn);
编码写法
%s
是后面的字符串插入的位置,\x1b[0m
重置了控制台的颜色,这样在此之后才不会将代码编程选定的颜色。\x1b[36m
被称作escape sequence
他将拦截你的字符串并将其转换成指定的颜色,escape sequence
处理颜色和样式的模式被称做ANSI escape code
,这个颜色模式已经被标准化,所以可以运行在不同的操作系统上。
console.log('\x1b[36m%s\x1b[0m', 'I am cyan'); //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); //yellow
更多的颜色参考如下
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
网友评论