由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
这在一定程度上取决于你所在的平台。最常见的方式 要做到这一点,需要打印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}`)
其他回答
遇到这个问题,想在没有任何依赖的情况下在stdout上使用一些颜色。这结合了其他一些很棒的答案。
这是我得到的。(需要节点v4或更高版本)
// colors.js
const util = require('util')
function colorize (color, text) {
const codes = util.inspect.colors[color]
return `\x1b[${codes[0]}m${text}\x1b[${codes[1]}m`
}
function colors () {
let returnValue = {}
Object.keys(util.inspect.colors).forEach((color) => {
returnValue[color] = (text) => colorize(color, text)
})
return returnValue
}
module.exports = colors()
只需要文件,然后像这样使用它:
const colors = require('./colors')
console.log(colors.green("I'm green!"))
预定义的颜色代码可以在这里找到
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");
颜色和效果的顺序似乎不是那么重要,但总是记得在最后重置颜色和效果。
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);
根据这个文档,你可以根据输出的数据类型改变颜色:
// you'll need the util module
var util = require('util');
// let's look at the defaults:
util.inspect.styles
{ special: 'cyan',
number: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
date: 'magenta',
regexp: 'red' }
// what are the predefined colors?
util.inspect.colors
{ bold: [ 1, 22 ],
italic: [ 3, 23 ],
underline: [ 4, 24 ],
inverse: [ 7, 27 ],
white: [ 37, 39 ],
grey: [ 90, 39 ],
black: [ 30, 39 ],
blue: [ 34, 39 ],
cyan: [ 36, 39 ],
green: [ 32, 39 ],
magenta: [ 35, 39 ],
red: [ 31, 39 ],
yellow: [ 33, 39 ] }
这些似乎是ANSI SGR转义码,其中第一个数字是在输出之前发出的代码,第二个数字是在输出之后发出的代码。因此,如果我们看一下维基百科上的ANSI SGR代码图表,你会发现大多数代码都以数字30-37开始设置前景色,以39结束重置为默认前景色。
我不喜欢的一点是有些颜色太深了。特别是日期。继续,在控制台中尝试new Date()。深色洋红色加黑色真的很难辨认。让我们把它换成浅洋红色。
// first define a new color
util.inspect.colors.lightmagenta = [95,39];
// now assign it to the output for date types
util.inspect.styles.date = 'lightmagenta';
现在,当您尝试new Date()时,输出的可读性大大提高。
如果你想在启动node时自动设置颜色,创建一个脚本来启动repl,就像这样:
// set your colors however desired
var util = require('util');
util.inspect.colors.lightmagenta = [95,39];
util.inspect.styles.date = 'lightmagenta';
// start the repl
require('repl').start({});
保存这个文件(例如init.js),然后运行node.exe init.js。它将设置颜色并启动node.js命令提示符。
(感谢loganfsmyth对repl想法的回答。)
你可以使用下面的例子: 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
推荐文章
- 很好的初学者教程socket.io?
- 如何在Visual c++中保持控制台窗口打开?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- 如何在新的材质主题中改变背面箭头的颜色?
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- Node.js同步执行系统命令
- 我如何改变我的提示在zsh的颜色(不同于正常的文本)?
- 如何改变循环进度指示器的颜色
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统