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


当前回答

我为不能有依赖的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) => ({ ...关口,[坳[0]]:f = > ' \ x1b[3{坳[1]}美元$ {f} \ x1b [0 m ' }, {}) console.log(' ${g('I')} love ${r('Italy')} ')

R,g,b,w,c,m,y,k分别代表红色,绿色,蓝色,白色,青色,品红,黄色和黑色。

其他回答

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

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

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

这在一定程度上取决于你所在的平台。最常见的方式 要做到这一点,需要打印ANSI转义序列。举个简单的例子, 下面是blender构建脚本中的一些python代码:

// This is a object for use ANSI escape to color the text in the terminal
const bColors = {
    HEADER    : '\033[95m',
    OKBLUE    : '\033[94m',
    OKGREEN   : '\033[92m',
    WARNING   : '\033[93m',
    FAIL      : '\033[91m',
    ENDC      : '\033[0m', 
    BOLD      : '\033[1m',   
    UNDERLINE : '\033[4m'
}

要使用这样的代码,可以执行如下操作

console.log(`${bColors.WARNING} My name is sami ${bColors.ENDC}`)

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

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

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

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"
FgGray: "\x1b[100m"

例如,如果你想有一个昏暗的红色文本和蓝色背景,你可以在Javascript中这样做:

console.log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");

颜色和效果的顺序似乎不是那么重要,但总是记得在最后重置颜色和效果。

下面你可以找到运行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