如何更改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: 最受歡迎的是:
- chalk
- cli-color
- colors
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 cologRepo 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+模塊使用
總結
以上是生活随笔為你收集整理的如何更改node.js的控制台字体颜色?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中 移位运算符_java中的移位
- 下一篇: spring与jdk版本要求