由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
我发现上面的答案(https://stackoverflow.com/a/41407246/4808079)非常有用,但不完整。如果你只想给某样东西上色一次,我想这没问题,但我认为以可运行的函数形式共享它更适用于现实生活中的用例。
const Color = {
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",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
BgGray: "\x1b[100m",
}
function colorString(color, string) {
return `${color}${string}${Color.Reset}`;
}
function colorLog(color, ...args) {
console.log(...args.map(
(it) => typeof it === "string" ? colorString(color, string) : it
));
}
像这样使用它:
colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true });
console.log([
colorString(Color.FgRed, "red"),
colorString(Color.FgGreen, "green"),
colorString(Color.FgBlue, "blue"),
].join(", "));
其他回答
如果你想改变颜色直接自己没有模块尝试
console.log('\x1b[36m', 'sometext' ,'\x1b[0m');
先\x1b[36m改变颜色为36,然后回到终端颜色0。
下面是ANSI颜色代码列表
对于一个不影响String对象的内置方法的流行替代颜色,我推荐使用cli-color。
包括颜色和可链接样式,如粗体、斜体和下划线。
有关此类别中各个模块的比较,请参见这里。
这是控制台中可用颜色(包括背景和前景)的列表,带有一些可用的操作(如重置,反转等)。
const colours = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
fg: {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
gray: "\x1b[90m",
crimson: "\x1b[38m" // Scarlet
},
bg: {
black: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m",
gray: "\x1b[100m",
crimson: "\x1b[48m"
}
};
下面是一个如何使用它的例子:
console.log(colours.bg.blue, colours.fg.white, "I am a white message with a blue background", colours.reset) ;
// Make sure that you don't forget "colours.reset" at the so that you can reset the console back to it's original colours.
或者你可以安装一些实用模块:
npm install console-info console-warn console-error --save-dev
当你使用这些模块时,它们会在控制台显示如下内容:
If you are using Windows CMD then go to the terminal Properties/Colors (CMD top left) and then redefine the RGB value of the offensive color. In my case I believe it's the fifth color square from the left, which I changed to (222,222,222). It does not matter if the currently selected radio button shows Screen Text or Screen Background as you just redefine that specific "system" color. Once you changed the color don't forget to select back the preferred color for the background or text before clicking OK.
更改后,所有来自Node(在我的情况下是Ember)的红色消息都清晰可见。
你可以使用下面的例子: https://help.ubuntu.com/community/CustomizingBashPrompt
也是nodeJs的一个要旨
例如,如果你想让部分文本显示为红色,只需执行console.log with:
"\033[31m this will be red \033[91m and this will be normal"
基于此,我为Node.js创建了“colog”扩展。你可以使用以下方法安装:
npm install colog
回购和npm: https://github.com/dariuszp/colog
推荐文章
- 检查已安装的angular-cli版本?
- 如何将Blob转换为JavaScript文件
- 我如何获得iOS 7默认的蓝色编程?
- 如何用Express/Node以编程方式发送404响应?
- 如何安装包从github回购在纱线
- 什么时候.then(success, fail)被认为是承诺的反模式?
- 将Chrome中的console.log保存为文件
- 自动HTTPS连接/重定向使用node.js/express
- 在nodejs http中body在哪里。得到回应?
- 如何在一张图中为不同的地块得到不同颜色的线条
- 如何在猫鼬排序?
- 清除javascript控制台在谷歌Chrome
- Nodemon错误:“已达到文件监视器数量的系统限制”
- 如何使webpack开发服务器运行在端口80和0.0.0.0使其公开访问?
- 如何在node.js模块中访问和测试内部(非导出)函数?