由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?


当前回答

另一种方法是使用一个简单的ANSI代码生成器。

您不需要安装软件包 不需要搜索颜色代码,只需点击按钮

GIF演示

你可以在https://console-colors.vercel.app/上使用它

公共存储库:https://github.com/alecshoppe/console-colors

其他回答

没有库,没有复杂,只是简单:

console.log(red('Error!'));

function red(s) {
    return '\033[31m' + s;
}

下面你可以找到运行node.js应用程序时文本的颜色引用:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow

注意%s是字符串(第二个参数)被注入的位置。\x1b[0m重置终端颜色,因此在此之后它不再继续是所选的颜色。

颜色参考

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"

编辑:

例如,\x1b[31m是一个转义序列,它将被您的终端拦截,并指示它切换到红色。事实上,\x1b是不可打印控制字符转义的代码。仅处理颜色和样式的转义序列也被称为ANSI转义代码,并且是标准化的,因此它们(应该)适用于任何平台。

维基百科对不同终端显示颜色的方式做了很好的比较 https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

这是控制台中可用颜色(包括背景和前景)的列表,带有一些可用的操作(如重置,反转等)。

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

当你使用这些模块时,它们会在控制台显示如下内容:

我创建了自己的模块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"))

这是Windows 10的一种方法(可能适用于7),它改变了cmd, npm终端本身的配色方案(主题),而不仅仅是特定应用程序的控制台输出。

我找到了一个Windows插件——颜色工具,它应该是在Windows保护伞下开发的。在链接中有描述。

我将colortool目录添加到系统环境路径变量中,现在每当我启动终端(NodeJs命令提示符,cmd)时,它都可用。