如何更改 node.js 的控制台字体颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9781218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change node.js's console font color?
提问by MaiaVictor
I had to change the console background color to white because of eye problems, but the font is gray colored and it makes the messages unreadable. How can I change it?
由于眼睛问题,我不得不将控制台背景颜色更改为白色,但字体是灰色的,这使得消息无法读取。我怎样才能改变它?
回答by Bud Damyanov
Below you can find colors reference of text to command when running node.js application:
在运行 node.js 应用程序时,您可以在下面找到文本到命令的颜色参考:
console.log('\x1b[36m%s\x1b[0m', 'I am cyan'); //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); //yellow
Note %sis where in the string (the second argument) gets injected. \x1b[0mresets the terminal color so it doesn't continue to be the chosen color anymore after this point.
注意%s是在字符串(第二个参数)中注入的位置。\x1b[0m重置终端颜色,因此在此之后它不再继续是所选颜色。
Colors reference
颜色参考
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"
EDIT:
编辑:
For example, \x1b[31mis an escape sequencethat will be intercepted by your terminal and instructs it to switch to the red color. In fact, \x1bis the code for the non-printable control characterescape. Escape sequences dealing only with colors and styles are also known as ANSI escape codeand are standardized, so therefore they (should) work on any platform.
例如,\x1b[31m是一个转义序列,它将被您的终端拦截并指示它切换到红色。实际上,\x1b是不可打印控制字符的代码escape。仅处理颜色和样式的转义序列也称为ANSI 转义码并且是标准化的,因此它们(应该)适用于任何平台。
Wikipedia has a nice comparison of how different terminals display colors https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
维基百科对不同终端如何显示颜色进行了很好的比较 https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
回答by nelsonic
There are multiple packages available for formatting console text in Node.js. The most popular are:
有多个包可用于在 Node.js 中格式化控制台文本。最受欢迎的是:
Usage:
用法:
CHALK:
粉笔:
const chalk = require('chalk');
console.log(chalk.red('Text in red'));
CLI-COLOR:
CLI-颜色:
const clc = require('cli-color');
console.log(clc.red('Text in red'));
COLORS:
颜色:
const colors = require('colors');
console.log('Text in red'.red);
Many people have noted their disapproval of colorsaltering the String prototype. If you prefer your prototypes to be left alone, use the following code instead:
许多人已经注意到他们不赞成colors改变String 原型。如果您更喜欢单独放置原型,请改用以下代码:
const colors = require('colors/safe');
console.log(colors.red('Text in red'));
回答by Henry Tseng
If you want to change the colors directly yourself without a module try
如果您想在没有模块的情况下直接自己更改颜色,请尝试
console.log('\x1b[36m', 'sometext' ,'\x1b[0m');
First \x1b[36mto change the colors to 36and then back to terminal color 0.
首先\x1b[36m将颜色更改36为终端颜色,然后再更改回终端颜色0。
回答by Dariuszp
to color your output You can use examples from there:
https://help.ubuntu.com/community/CustomizingBashPrompt
为您的输出着色您可以使用那里的示例:https:
//help.ubuntu.com/community/CustomizingBashPrompt
Also a Gist for nodeJs
For example if you want part of the text in red color, just do console.log with:
例如,如果您想要部分文本为红色,只需使用以下命令执行 console.log:
"3[31m this will be red 3[91m and this will be normal"
Based on that I've created "colog" extension for Node.js. You can install it using:
基于此,我为 Node.js 创建了“colog”扩展。您可以使用以下方法安装它:
npm install colog
Repo and npm: https://github.com/dariuszp/colog
回购和 npm:https: //github.com/dariuszp/colog
回答by Abdennour TOUMI
This is a list of available colors (background,foreground) in console with available actions (reset,reverse,...).
这是控制台中的可用颜色(背景、前景)列表以及可用的操作(重置、反转、...)。
const colors = {
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",
Crimson: "\x1b[38m" //???????
},
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",
Crimson: "\x1b[48m"
}
};
Use it as following :
使用它如下:
console.log(colors.bg.Blue, colors.fg.White , "I am white message with blue background", colors.Reset) ;
//don't forget "colors.Reset" to stop this color and return back to the default color
You can also install :
您还可以安装:
npm install console-info console-warn console-error --save-dev
IT will give you an output closer to console of client side :
它会给你一个更接近客户端控制台的输出:
回答by Matt Johnson-Pint
Per this documentation, you can change the colors based on the data type of the output:
根据此文档,您可以根据输出的数据类型更改颜色:
// you'll need the util module
var util = require('util');
// let's look at the defaults:
util.inspect.styles
{ special: 'cyan',
number: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
date: 'magenta',
regexp: 'red' }
// what are the predefined colors?
util.inspect.colors
{ bold: [ 1, 22 ],
italic: [ 3, 23 ],
underline: [ 4, 24 ],
inverse: [ 7, 27 ],
white: [ 37, 39 ],
grey: [ 90, 39 ],
black: [ 30, 39 ],
blue: [ 34, 39 ],
cyan: [ 36, 39 ],
green: [ 32, 39 ],
magenta: [ 35, 39 ],
red: [ 31, 39 ],
yellow: [ 33, 39 ] }
These appear to be ANSI SGR escape codes, where the first number is the code to emit before the output, and the second number is the code to emit after. So if we look at the chart of ANSI SGR codes on Wikipedia, you'll see that most of these start with a number 30-37 to set the foreground color, and end in 39 to reset to the default foreground color.
这些似乎是 ANSI SGR 转义码,其中第一个数字是在输出之前发出的代码,第二个数字是在输出之后发出的代码。因此,如果我们查看Wikipedia 上的 ANSI SGR 代码图表,您会发现其中大部分以数字 30-37 开头以设置前景色,以 39 结尾以重置为默认前景色。
So one thing I don't like is how dark some of these are. Especially dates. Go ahead and try new Date()in the console. Dark magenta on black is really hard to read. Let's change that to a light magenta instead.
所以我不喜欢的一件事是其中一些有多黑。尤其是日期。继续并new Date()在控制台中尝试。黑色上的深洋红色真的很难读。让我们将其改为浅洋红色。
// first define a new color
util.inspect.colors.lightmagenta = [95,39];
// now assign it to the output for date types
util.inspect.styles.date = 'lightmagenta';
Now when you try new Date(), the output is much more readable.
现在,当您尝试时new Date(),输出更具可读性。
If you'd like to set colors automatically when launching node, create a script that launches the repl, like this:
如果您想在启动节点时自动设置颜色,请创建一个启动 repl 的脚本,如下所示:
// set your colors however desired
var util = require('util');
util.inspect.colors.lightmagenta = [95,39];
util.inspect.styles.date = 'lightmagenta';
// start the repl
require('repl').start({});
Save this file (for example, init.js), then run node.exe init.js. It will set the colors and launch the node.js command prompt.
保存此文件(例如,init.js),然后运行node.exe init.js. 它将设置颜色并启动 node.js 命令提示符。
(Thanks to loganfsmyth in this answerfor the repl idea.)
(感谢 loganfsmyth 在这个答案中的 repl 想法。)
回答by callum
This library by Sindre Sorhus is the best at the moment:
Sindre Sorhus 的这个库是目前最好的:
chalk
粉笔
- Highly performant
- Doesn't extend
String.prototype - Expressive API
- Ability to nest styles
- Clean and focused
- Auto-detects color support
- Actively maintained
- Used by 5500+ modules
- 高性能
- 不延长
String.prototype - 富有表现力的 API
- 嵌套样式的能力
- 干净、专注
- 自动检测颜色支持
- 积极维护
- 被 5500 多个模块使用
回答by Shnd
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"
For example if you want to have a Dim, Red text with Blue background you can do it in Javascript like this:
例如,如果你想要一个带有蓝色背景的暗红色文本,你可以在 Javascript 中这样做:
console.log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");
The order of the colors and effects seems to not be that important but always remember to reset the colors and effects at the end.
颜色和效果的顺序似乎不是那么重要,但始终记得在最后重置颜色和效果。
回答by chriskelly
A handy one-liner I wrote for npm scripts that can't have dependencies:
我为没有依赖项的 npm 脚本编写的一个方便的单行:
const { r, g, b, w, c, m, y, k } = [
['r', 1], ['g', 2], ['b', 4], ['w', 7],
['c', 6], ['m', 5], ['y', 3], ['k', 0],
].reduce((cols, col) => ({
...cols, [col[0]]: f => `\x1b[3${col[1]}m${f}\x1b[0m`
}), {})
console.log(`${g('I')} love ${r('Italy')}`)
Edit:r,g,b,w,c,m,y,kstands for red, green, blue, white, cyan, magenta, yellow and blac(k)
编辑:r,g,b,w,c,m,y,k代表红色、绿色、蓝色、白色、青色、品红色、黄色和黑色(k)
回答by Mojtaba Hosseini
Emoji
表情符号
You can use colors for text as others mentioned in their answers.
您可以使用其他人在答案中提到的文本颜色。
But you can use emojisinstead! for example you can use You can use ??for warning messages and for error messages.
但是您可以使用表情符号代替!例如,您可以使用 You can use ??for warning message and for error message。
Or simply use these note books as a color:
或者简单地使用这些笔记本作为颜色:
: error message
: warning message
: ok status message
: action message
: canceled status message
: Or anything you like and want to recognize immediately by color
Bonus:
奖金:
This method also helps you to quickly scan and find logs directly in the source code.
这种方法还可以帮助您直接在源代码中快速扫描和查找日志。
But linux default emoji font is not colorful by default and you may want to make them colorful, first.
但是 linux 默认表情符号字体默认情况下不是彩色的,您可能首先想让它们变得彩色。


