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


当前回答

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

其他回答

这在一定程度上取决于你所在的平台。最常见的方式 要做到这一点,需要打印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}`)

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

最小的别名:

{
  const f = (color) => (...args) => {
    for (const x of [color, ...args, "\33[0m"]) console.log(x);
  };

  Object.assign(console, {
    black: f("\33[30m"),
    red: f("\33[31m"),
    green: f("\33[32m"),
    yellow: f("\33[33m"),
    blue: f("\33[34m"),
    magenta: f("\33[35m"),
    cyan: f("\33[36m"),
    white: f("\33[37m"),
  });
}

// Usage
console.blue("Blue world");

If you are using Windows CMD then go to the terminal Properties/Colors (CMD top left) and then redefine the RGB value of the offensive color. In my case I believe it's the fifth color square from the left, which I changed to (222,222,222). It does not matter if the currently selected radio button shows Screen Text or Screen Background as you just redefine that specific "system" color. Once you changed the color don't forget to select back the preferred color for the background or text before clicking OK.

更改后,所有来自Node(在我的情况下是Ember)的红色消息都清晰可见。

我重载了控制台方法。

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

输出为。