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


当前回答

我重载了控制台方法。

var colors={
Reset: "\x1b[0m",
Red: "\x1b[31m",
Green: "\x1b[32m",
Yellow: "\x1b[33m"
};

var infoLog = console.info;
var logLog = console.log;
var errorLog = console.error;
var warnLog = console.warn;

console.info= function(args)
{
    var copyArgs = Array.prototype.slice.call(arguments);
    copyArgs.unshift(colors.Green);
    copyArgs.push(colors.Reset);
    infoLog.apply(null,copyArgs);
};

console.warn= function(args)
{
    var copyArgs = Array.prototype.slice.call(arguments);
    copyArgs.unshift(colors.Yellow);
    copyArgs.push(colors.Reset);
    warnLog.apply(null,copyArgs);
};
console.error= function(args)
{
    var copyArgs = Array.prototype.slice.call(arguments);
    copyArgs.unshift(colors.Red);
    copyArgs.push(colors.Reset);
    errorLog.apply(null,copyArgs);
};

// examples
console.info("Numeros",1,2,3);
console.warn("pares",2,4,6);
console.error("reiniciandooo");

输出为。

其他回答

node-colorify

提供功能打印文本的颜色,也做文本格式,如粗体,闪烁等。

内联typescript解决方案

export const color = (function (colors) {
    const fn = (code: number, str: string) => `\x1b[${code}m${str}\x1b[39m`;
    const obj = { grey: fn.bind(null, 90) };
    for (let i = 0; i < colors.length; i++) obj[colors[i]] = fn.bind(null, 30 + i);
    return obj as { [K in typeof colors[any] | 'grey']: (str: string) => string };
})(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] as const);

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

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

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

我为不能有依赖的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分别代表红色,绿色,蓝色,白色,青色,品红,黄色和黑色。

你可以使用下面的例子: https://help.ubuntu.com/community/CustomizingBashPrompt

也是nodeJs的一个要旨

例如,如果你想让部分文本显示为红色,只需执行console.log with:

"\033[31m this will be red \033[91m and this will be normal"

基于此,我为Node.js创建了“colog”扩展。你可以使用以下方法安装:

npm install colog

回购和npm: https://github.com/dariuszp/colog