如何更改node.js的控制台字体颜色?

由于眼睛的问题,我不得不将控制台的背景色更改为白色,但是字体为灰色,这使消息不可读。我该如何更改?

小哥Tom2020/03/16 14:42:40

This is an approach for Windows 10 (maybe for 7) and it changes the color scheme (theme) for cmd, npm terminal itself, not only console output for a particular app.

I found the working Windows plugin - Color Tool, which is presumably developed under Windows umbrella. A description is available at the link.

I added colortool directory into system environment path variable and now it is available whenever I start terminal (NodeJs command prompt, cmd).

凯斯丁2020/03/16 14:42:40

node-colorify

Provides functions to print texts in color and also to do text formatting such as bold, blink, etc..

猴子飞云2020/03/16 14:42:40
var colorSet = {
    Reset: "\x1b[0m",
    Red: "\x1b[31m",
    Green: "\x1b[32m",
    Yellow: "\x1b[33m",
    Blue: "\x1b[34m",
    Magenta: "\x1b[35m"
};

var funcNames = ["info", "log", "warn", "error"];
var colors = [colorSet.Green, colorSet.Blue, colorSet.Yellow, colorSet.Red];

for (var i = 0; i < funcNames.length; i++) {
    let funcName = funcNames[i];
    let color = colors[i];
    let oldFunc = console[funcName];
    console[funcName] = function () {
        var args = Array.prototype.slice.call(arguments);
        if (args.length) {
            args = [color + args[0]].concat(args.slice(1), colorSet.Reset);
        }
        oldFunc.apply(null, args);
    };
}

// Test:
console.info("Info is green.");
console.log("Log is blue.");
console.warn("Warn is orange.");
console.error("Error is red.");
console.info("--------------------");
console.info("Formatting works as well. The number = %d", 123);
番长前端2020/03/16 14:42:40

我重载了控制台方法。

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");

输出是。

在此处输入图片说明

逆天2020/03/16 14:42:40

我发现上面的这个答案(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",

  BgBlack: "\x1b[40m",
  BgRed: "\x1b[41m",
  BgGreen: "\x1b[42m",
  BgYellow: "\x1b[43m",
  BgBlue: "\x1b[44m",
  BgMagenta: "\x1b[45m",
  BgCyan: "\x1b[46m",
  BgWhite: "\x1b[47m"
}

function colorString(color, string) {
  return `${color}${string}${Color.Reset}`;
}

function colorStringLog(color, string) {
  console.log(colorString(color, string));
}

像这样使用它:

colorStringLog(Color.FgYellow, "Some Yellow text to console log");

console.log([
  colorString(Color.FgRed, "red"),
  colorString(Color.FgGreen, "green"),
  colorString(Color.FgBlue, "blue"),
].join(", "));
卡卡西卡卡西2020/03/16 14:42:40

我不希望有任何依赖关系,只有这些在OS X上对我Octal literal有用。这里答案中的所有其他示例给了我错误。

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"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"

来源:https : //coderwall.com/p/yphywg/printing-colorful-text-in-terminal-when-run-node-js-script

前端Itachi凯2020/03/16 14:42:39

表情符号

您可以像其他答案中提到的那样为文本使用颜色。

但是您可以改用表情符号例如,您可以使用您可以⚠️用于警告消息和🛑错误消息。

或者只是将这些笔记本用作颜色:

📕: error message
📙: warning message
📗: ok status message
📘: action message
📓: canceled status message
📔: Or anything you like and want to recognize immediately by color

奖金:

此方法还可以帮助您直接在源代码中快速扫描和查找日志

但是linux默认的emoji字体默认情况下不是彩色的,您可能首先要使其彩色。

小小逆天2020/03/16 14:42:39

对于不干扰String对象的内置方法的流行颜色,我建议您检查cli-color

包括颜色和可链接样式,例如粗体,斜体和下划线。

有关此类别中各个模块的比较,请参见此处

卡卡西Itachi2020/03/16 14:42:39

颜色代码如前所述

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"

BgBlack: "\x1b[40m"
BgRed: "\x1b[41m"
BgGreen: "\x1b[42m"
BgYellow: "\x1b[43m"
BgBlue: "\x1b[44m"
BgMagenta: "\x1b[45m"
BgCyan: "\x1b[46m"
BgWhite: "\x1b[47m"

例如,如果您要使用带有蓝色背景的Dim,Red文本,则可以在Javascript中执行以下操作:

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

颜色和效果的顺序似乎并不那么重要,但始终记得最后要重置颜色和效果。

小小Near阳光2020/03/16 14:42:39

Sindre Sorhus的此库是目前最好的库:

粉笔

  • 高效能
  • 不扩展 String.prototype
  • 富有表现力的API
  • 嵌套样式的能力
  • 干净而专注
  • 自动检测颜色支持
  • 积极维护
  • 由5500多个模块使用
神奇乐2020/03/16 14:42:39

为输出着色您可以在此处使用示例:https :
//help.ubuntu.com/community/CustomizingBashPrompt

也是nodeJs要点

例如,如果您希望部分文本为红色,则只需使用以下命令进行console.log:

"\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