由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
var to_rgb = function (_text, _r, _g, _b) {
return "\x1b[38;2;" + _r + ";" + _g + ";" + _b + "m" + _text + "\x1b[0m";
};
此代码帮助设置前景色:\x1b[38;2;R;G;Bm
这在某些地方可能行不通
其他回答
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)的红色消息都清晰可见。
Coolors
它非常适合使用或扩展。你可以简单地使用:
var coolors = require('coolors');
console.log(coolors('My cool console log', 'red'));
或者使用config:
var coolors = require('coolors');
console.log(coolors('My cool console log', {
text: 'yellow',
background: 'red',
bold: true,
underline: true,
inverse: true,
strikethrough: true
}));
延伸一下似乎很有趣:
var coolors = require('coolors');
function rainbowLog(msg){
var colorsText = coolors.availableStyles().text;
var rainbowColors = colorsText.splice(3);
var lengthRainbowColors = rainbowColors.length;
var msgInLetters = msg.split('');
var rainbowEndText = '';
var i = 0;
msgInLetters.forEach(function(letter){
if(letter != ' '){
if(i === lengthRainbowColors) i = 0;
rainbowEndText += coolors(letter, rainbowColors[i]);
i++;
}else{
rainbowEndText += ' ';
}
});
return rainbowEndText;
}
coolors.addPlugin('rainbow', rainbowLog);
console.log(coolorsExtended('This its a creative example extending core with a cool rainbown style', 'rainbown'));
视图Coolors模块
根据这个文档,你可以根据输出的数据类型改变颜色:
// 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 ] }
这些似乎是ANSI SGR转义码,其中第一个数字是在输出之前发出的代码,第二个数字是在输出之后发出的代码。因此,如果我们看一下维基百科上的ANSI SGR代码图表,你会发现大多数代码都以数字30-37开始设置前景色,以39结束重置为默认前景色。
我不喜欢的一点是有些颜色太深了。特别是日期。继续,在控制台中尝试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';
现在,当您尝试new Date()时,输出的可读性大大提高。
如果你想在启动node时自动设置颜色,创建一个脚本来启动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({});
保存这个文件(例如init.js),然后运行node.exe init.js。它将设置颜色并启动node.js命令提示符。
(感谢loganfsmyth对repl想法的回答。)
我不想要任何依赖于此,只有这些在OS x上为我工作。所有其他示例从这里的答案给了我八进制文字错误。
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"
来源:https://coderwall.com/p/yphywg/printing-colorful-text-in-terminal-when-run-node-js-script
var colorSet = {
Reset: "\x1b[0m",
Red: "\x1b[31m",
Green: "\x1b[32m",
Yellow: "\x1b[33m",
Blue: "\x1b[34m",
Magenta: "\x1b[35m"
};
var funcNames = ["info", "log", "warn", "error"];
var colors = [colorSet.Green, colorSet.Blue, colorSet.Yellow, colorSet.Red];
for (var i = 0; i < funcNames.length; i++) {
let funcName = funcNames[i];
let color = colors[i];
let oldFunc = console[funcName];
console[funcName] = function () {
var args = Array.prototype.slice.call(arguments);
if (args.length) {
args = [color + args[0]].concat(args.slice(1), colorSet.Reset);
}
oldFunc.apply(null, args);
};
}
// Test:
console.info("Info is green.");
console.log("Log is blue.");
console.warn("Warn is orange.");
console.error("Error is red.");
console.info("--------------------");
console.info("Formatting works as well. The number = %d", 123);
推荐文章
- 检查已安装的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模块中访问和测试内部(非导出)函数?