如何修改 Node.js console.log()
字体颜色
console.log('\x1b[36m%s\x1b[0m', '我是青色文字','颜色还原');
console.log('\x1b[33m%s', '我是黄色文字','我还是黄色文字');
我们将 \x1b[36m%s\x1b[0m
它拆解为3部分 \x1b[36m
,%s
,\x1b[0m
%s
是console.log()
第二个参数的占位符。\x1b[36m
将文本设为青色,\x1b[0m
将文本设为预置颜色,在这之后的文本颜色会还原。
可以看到,\x1b[36m
,\x1b[0m
的区别是里面的数字不同,36 表示青色,0 是预置颜色,我们只要更改里面的数字,就能更换颜色
更多颜色如下所示
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"
展开全部
这里的颜色代码是标准的 ANSI 转义码,他可以在任何平台的控制台中正常使用。
另外 github 上还有许多开箱即用的控制台颜色库
参考资料 Reference :
How to change node.js's console font color?版权声明:[自由转载-注明出处-非商用-非衍生] (知识共享许可协议)