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


当前回答

内联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);

其他回答

如果你想保持简单,而不使用任何外部模块/学习新的api /破解核心控制台功能:

const LCERROR = '\x1b[31m%s\x1b[0m'; //red
const LCWARN = '\x1b[33m%s\x1b[0m'; //yellow
const LCINFO = '\x1b[36m%s\x1b[0m'; //cyan
const LCSUCCESS = '\x1b[32m%s\x1b[0m'; //green

const logger = class {
  static error(message, ...optionalParams) { console.error(LCERROR, message, ...optionalParams) }
  static warn(message, ...optionalParams) { console.warn(LCWARN, message, ...optionalParams) }
  static info(message, ...optionalParams) { console.info(LCINFO, message, ...optionalParams) }
  static success(message, ...optionalParams) { console.info(LCSUCCESS, message, ...optionalParams) }
}

// then instead (as presented in the accepted answer)
// console.error(LCERROR, 'Error message in red.');
// you write:

logger.error('Error message in red.');

// or with multiple parameters (only the message will be red):

logger.error('Error message in red.', 1, false, null, {someKey: 'whatever'});

// or use backticks (template literal) instead multiple params:

logger.error(`This will be red as ${foo} and ${bar} too.`);

现在您可以像使用控制台一样使用记录器。没有新的API需要记住……通常你会把它放到一个模块(logger.js)中,并导出类,以便在你的应用程序中随处使用它,因为const logger = require('./logger');

你可以使用下面的例子: 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

我不想要任何依赖于此,只有这些在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

现在有两种方法来查看Node.js控制台的颜色变化。

一种是通过通用库,这些库可以用颜色标记装饰文本字符串,然后通过标准console.log输出。

今天的顶级图书馆:

粉笔 颜色 cli-color

另一种方法是修补现有的控制台方法。manakin就是这样一个库,它可以让你为所有的控制台方法(log, warn, error和info)自动设置标准颜色。

与通用颜色库的一个显著区别是-它可以全局或局部设置颜色,同时为每个Node.js控制台方法保持一致的语法和输出格式,然后使用这些方法而不必指定颜色,因为它们都是自动设置的。

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

针对你的问题,这里有一个最简单的解决方案:

var con = require('manakin').global;
con.log.color = 30; // Use black color for console.log

它将为应用程序中的每个console.log调用设置黑色。查看更多颜色代码。

manakin使用的默认颜色:

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

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

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