譯自 https://www.npmjs.com/package/colors
colors
get colors in your node.js console
在你的 node.js 控制臺中獲取顏色
get color and style in your node.js console(在你的 node.js 控制臺中獲取顏色和風(fēng)格)

安裝
npm install colors
colors and styles(顏色和風(fēng)格)
text colors(文本顏色)
black(黑色)red(紅色)green(綠色)yellow(黃色)blue(藍(lán)色)magenta(品紅/洋紅/紫紅)cyan(青色)white(白色)gray(灰色)grey(灰色)background colors(背景顏色)
bgBlack(黑色)bgRed(紅色)bgGreen(綠色)bgYellow(黃色)bgBlue(藍(lán)色)bgMagenta(品紅/洋紅/紫紅)bgCyan(青色)bgWhite(白色)styles(風(fēng)格)
reset(重置)bold(加粗)dim(暗淡)【無效】italic(斜體)【無效】underline(下劃線)【無效】inverse(反色)【無效】hidden(隱藏)【無效】strikethrough(刪除線)【無效】rainbow(彩虹 - 紅,黃,綠,藍(lán),紫紅,)zebra(有斑紋的)【無效】america(美國國旗顏色 - 紅,白,藍(lán))trap(圈套 - 亂碼)random(隨機(jī)顏色)【無效】指的是在 Win10 下的測試結(jié)果
Usage(使用)
By popular demand, colors now ships with two types of usages!
應(yīng)大眾的要求,colors現(xiàn)在提供了兩種使用方式!
The super nifty way
超級漂亮的方式
var colors = require('colors');console.log('hello'.green); // outputs green text console.log('i like cake and pies'.underline.red) // outputs red underlined text console.log('inverse the color'.inverse); // inverses the color console.log('OMG Rainbows!'.rainbow); // rainbow console.log('Run the trap'.trap); // Drops the bass(降低音)在 Win10 下測試發(fā)現(xiàn):underline、inverse、trap 均無效。
or a slightly less nifty way which doesn’t extend String.PRototype
或者一個沒有繼承 String.prototype 的稍微不那么漂亮的方式
var colors = require('colors/safe');console.log(colors.green('hello')); // outputs green text console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text console.log(colors.inverse('inverse the color')); // inverses the color console.log(colors.rainbow('OMG Rainbows!')); // rainbow console.log(colors.trap('Run the trap')); // Drops the bass I prefer the first way. Some people seem to be afraid of extending String.prototype and prefer the second way.
我更喜歡第一種方式。相當(dāng)多的人看起來害怕繼承 String.prototype 并且更喜歡第二種方式。
If you are writing good code you will never have an issue with the first approach. If you really don’t want to touch String.prototype, the second usage will not touch String native object.
如果你的編碼能力較好,那么對第一種方式將不會有任何問題。如果你實(shí)在不想接觸 String.prototype,第二種使用方式將不會接觸到字符串的對象。
Disabling Colors(禁用 colors)
To disable colors you can pass the following arguments in the command line to your application:
你可以在命令行中通過傳遞如下的參數(shù)到你的應(yīng)用程序來禁用 colors:
node myapp.js -no-colorConsole.log string substitution(控制臺打印替換后的字符串)
var name = 'Marak';console.log(colors.green('Hello %s'), name);// outputs -> 'Hello Marak' Custom themes(自定義主題)
Using standard API(使用標(biāo)準(zhǔn)的 API)
var colors = require('colors');colors.setTheme({ silly: 'rainbow', input: 'grey', verbose: 'cyan', prompt: 'grey', info: 'green', data: 'grey', help: 'cyan', warn: 'yellow', debug: 'blue', error: 'red'});// outputs red text console.log("this is an error".error);// outputs yellow text console.log("this is a warning".warn);Using string safe API(使用字符串安全的 API)
var colors = require('colors/safe');// set single property var error = colors.red;error('this is red');// set theme colors.setTheme({ silly: 'rainbow', input: 'grey', verbose: 'cyan', prompt: 'grey', info: 'green', data: 'grey', help: 'cyan', warn: 'yellow', debug: 'blue', error: 'red'});// outputs red text console.log(colors.error("this is an error"));// outputs yellow text console.log(colors.warn("this is a warning")); You can also combine them:
你也可以將他們合并:
var colors = require('colors');colors.setTheme({ custom: ['red', 'underline']});console.log('test'.custom);Protip: There is a secret undocumented style in colors. If you find the style you can summon him.
提示:colors 中有一個秘密的無明文規(guī)定的風(fēng)格。如果你發(fā)現(xiàn)了這種風(fēng)格,你就可以召喚它。
Protip 是新一代的 jQuery 提示插件。