由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
我发现上面的答案(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(", "));
其他回答
我创建了自己的模块StyleMe。我这样做,我可以用很少的打字做很多事情。例子:
var StyleMe = require('styleme');
StyleMe.extend() // extend the string prototype
console.log("gre{Hello} blu{world}!".styleMe()) // Logs hello world! with 'hello' being green, and 'world' being blue with '!' being normal.
它也可以被嵌套:
console.log("This is normal red{this is red blu{this is blue} back to red}".styleMe())
或者,如果你不想扩展字符串原型,你可以选择其他3个选项:
console.log(styleme.red("a string"))
console.log("Hello, this is yellow text".yellow().end())
console.log(styleme.style("some text","red,bbl"))
我不想要任何依赖于此,只有这些在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
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模块
这适用于(我所知道的)Node控制台。
该包是快捷方式,您可以使用此命令安装它。 Const short = require('@testgrandma/shortcuts');
您可以执行两个命令来更改颜色。它是RGB颜色和Hex颜色short.colorRGB(r,g,b);
short.colorhex(十六进制);
你可以执行console.log(short.colorhex('d50000') + 'This is red!');
包裹可以在这里找到。
https://www.npmjs.com/package/@testgrandma/shortcuts
在Node.js中有多个可用于格式化控制台文本的包。最流行的有:
粉笔, cli-color - 颜色- >编辑:颜色不再推荐,因为它有拒绝服务漏洞 详情见:https://snyk.io/blog/open-source-npm-packages-colors-faker/
用法:
粉笔:
const chalk = require('chalk');
console.log(chalk.red('Text in red'));
CLI-COLOR:
const clc = require('cli-color');
console.log(clc.red('Text in red'));
推荐文章
- 更改UITextField和UITextView光标/插入符颜色
- npm犯错!代码UNABLE_TO_GET_ISSUER_CERT_LOCALLY
- Access-Control-Allow-Origin不允许Origin < Origin >
- 如何获得所有已注册的快捷路线?
- 你可以为你的组织托管一个私有的存储库来使用npm吗?
- 如何定位父文件夹?
- Gulp命令未找到-安装Gulp后错误
- 在Node.js中写入文件时创建目录
- 如何将自定义脚本添加到包中。Json文件,运行javascript文件?
- 使用child_process。execSync但保持输出在控制台
- SyntaxError:在严格模式下使用const
- 在Node.js中递归复制文件夹
- 如何在node.js中设置默认时区?
- “react-scripts”不被视为内部或外部命令
- 如何正确地读取异步/等待文件?