由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
根据这个文档,你可以根据输出的数据类型改变颜色:
// 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想法的回答。)
其他回答
如果你想改变颜色直接自己没有模块尝试
console.log('\x1b[36m', 'sometext' ,'\x1b[0m');
先\x1b[36m改变颜色为36,然后回到终端颜色0。
下面是ANSI颜色代码列表
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)的红色消息都清晰可见。
在ubuntu中,你可以简单地使用颜色代码:
var sys = require('sys');
process.stdout.write("x1B[31m" + your_message_in_red + "\x1B[0m\r\n");
Sindre Sorhus设计的这个图书馆是目前最好的:
粉笔
高性能 不扩展String.prototype 富有表现力的API 嵌套样式的能力 干净和专注 自动检测颜色支持 积极维护 5500+模块使用
现在有两种方法来查看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使用的默认颜色:
推荐文章
- 很好的初学者教程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文件系统