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

本文翻译自:How to change node.js's console font color?

I had to change the console background color to white because of eye problems, but the font is gray colored and it makes the messages unreadable. 由于眼睛的问题,我不得不将控制台的背景色更改为白色,但是字体为灰色,这使消息不可读。 How can I change it? 我该如何更改?


#1楼

参考:https://stackoom.com/question/f2Xa/如何更改node-js的控制台字体颜色


#2楼

There are multiple packages available for formatting console text in Node.js. 有多个软件包可用于在Node.js中格式化控制台文本。 The most popular are: 最受欢迎的是:

Usage: 用法:


CHALK: 粉笔:

const chalk = require('chalk');
console.log(chalk.red('Text in red'));

CLI-COLOR: CLI颜色:

const clc = require('cli-color');
console.log(clc.red('Text in red'));

COLORS: 颜色:

const colors = require('colors');
console.log('Text in red'.red);

Many people have noted their disapproval of colors altering the String prototype . 许多人注意到他们不赞成更改String原型colors If you prefer your prototypes to be left alone, use the following code instead: 如果您希望保留原型,请使用以下代码:

const colors = require('colors/safe');
console.log(colors.red('Text in red'));

#3楼

to color your output You can use examples from there: 为输出着色您可以使用以下示例:
https://help.ubuntu.com/community/CustomizingBashPrompt https://help.ubuntu.com/community/CustomizingBashPrompt

Also a Gist for nodeJs 也是nodeJs要点

For example if you want part of the text in red color, just do console.log with: 例如,如果您希望部分文本为红色,则只需使用以下命令进行console.log:

"\033[31m this will be red \033[91m and this will be normal"

Based on that I've created "colog" extension for Node.js. 基于此,我为Node.js创建了“ colog”扩展。 You can install it using: 您可以使用以下方法安装它:

npm install colog

Repo and npm: https://github.com/dariuszp/colog 回购和npm: https : //github.com/dariuszp/colog


#4楼

Per this documentation , you can change the colors based on the data type of the output: 根据本文档 ,您可以根据输出的数据类型更改颜色:

// 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 ] }

These appear to be ANSI SGR escape codes, where the first number is the code to emit before the output, and the second number is the code to emit after. 这些似乎是ANSI SGR转义码,其中第一个数字是在输出之前发出的代码,第二个数字是在输出之后发出的代码。 So if we look at the chart of ANSI SGR codes on Wikipedia , you'll see that most of these start with a number 30-37 to set the foreground color, and end in 39 to reset to the default foreground color. 因此,如果我们在Wikipedia上查看ANSI SGR代码的图表 ,您会看到其中大多数以数字30-37开头来设置前景色,并以39结尾来重置为默认前景色。

So one thing I don't like is how dark some of these are. 因此,我不喜欢的一件事是其中有些暗。 Especially dates. 特别是日期。 Go ahead and try new Date() in the console. 继续并在控制台中尝试new Date() Dark magenta on black is really hard to read. 黑色的深洋红色真的很难看清。 Let's change that to a light magenta instead. 让我们将其更改为浅洋红色。

// 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';

Now when you try new Date() , the output is much more readable. 现在,当您尝试使用new Date() ,输出将更具可读性。

If you'd like to set colors automatically when launching node, create a script that launches the repl, like this: 如果您想在启动节点时自动设置颜色,请创建一个启动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({});

Save this file (for example, init.js ), then run node.exe init.js . 保存此文件(例如, init.js ),然后运行node.exe init.js It will set the colors and launch the node.js command prompt. 它将设置颜色并启动node.js命令提示符。

(Thanks to loganfsmyth in this answer for the repl idea.) (感谢loganfsmyth 回答这个想法。)


#5楼

For a popular alternative to colors that doesn't mess with the built-in methods of the String object, I recommend checking out cli-color . 对于不干扰String对象的内置方法的流行颜色替代方法,建议您检查cli-color

Includes both colors and chainable styles such as bold, italic, and underline. 包括颜色和可链接样式,例如粗体,斜体和下划线。

For a comparison of various modules in this category, see here . 有关此类别中各个模块的比较,请参见此处


#6楼

This library by Sindre Sorhus is the best at the moment: Sindre Sorhus的这个库是目前最好的库:

chalk 粉笔

  • Highly performant 高绩效
  • Doesn't extend String.prototype 不扩展String.prototype
  • Expressive API 富有表现力的API
  • Ability to nest styles 嵌套样式的能力
  • Clean and focused 干净而专注
  • Auto-detects color support 自动检测颜色支持
  • Actively maintained 积极维护
  • Used by 5500+ modules 5500+模块使用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值