使用更简单的样式字符串建立粉笔样式方案git
yarn add chalk-pipe
const chalkPipe = require('chalk-pipe'); console.log(chalkPipe('blue.bold')('Hello world!'));
使用点.
区分多种样式:github
const chalkPipe = require('chalk-pipe'); const link = chalkPipe('blue.underline'); const error = chalkPipe('bgRed.#cccccc'); const warning = chalkPipe('orange.bold'); console.log(link('Link!')); console.log(error('Error!')); console.log(warning('Warning!'));
chalkPipe
is also chalk
:函数
const chalkPipe = require('chalk-pipe'); const blue = chalkPipe('blue'); const link = blue.underline; console.log(link('Link!'));
使用定制的chalk
性能
const chalk = require('chalk'); const chalkPipe = require('chalk-pipe'); const text = chalkPipe('underline', chalk.blue)('Link!'); console.log(text);
chalkPipe('blue.underline')('Link!');
const chalk = require('chalk'); chalk.enable = true; chalkPipe('underline', chalk.blue)('Link!');
基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档测试
若是须要更加细致的需求,就要使用下面的chalk
了,毕竟这只是它简化版的库spa
正确处理终端字符串样式
yarn add chalk
Chalk提供了一个易于使用的可组合API,您只需链嵌您想要的样式。prototype
const chalk = require('chalk'); const log = console.log; // Combine styled and normal strings log(chalk.blue('Hello') + ' World' + chalk.red('!')); // Compose multiple styles using the chainable API log(chalk.blue.bgRed.bold('Hello world!')); // Pass in multiple arguments log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz')); // Nest styles log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!')); // Nest styles of the same type even (color, underline, background) log(chalk.green( 'I am a green line ' + chalk.blue.underline.bold('with a blue substring') + ' that becomes green again!' )); // ES2015 template literal log(` CPU: ${chalk.red('90%')} RAM: ${chalk.green('40%')} DISK: ${chalk.yellow('70%')} `); // ES2015 tagged template literal log(chalk` CPU: {red 20%} RAM: {green 30%} DISK: {rgb(255,131,0) 40%} `); // Use RGB colors in terminal emulators that support it. log(chalk.keyword('orange')('Yay for orange colored text!')); log(chalk.rgb(123, 45, 67).underline('Underlined reddish color')); log(chalk.hex('#DEADED').bold('Bold gray!'));
<style>[.<style>...](string, [string...])
Example: chalk.red.bold.underline('Hello', 'world');
code
链样式,并将最后一个做为带有字符串参数的方法调用。顺序不重要,之后的样式在发生冲突的状况下会有先例. 这仅仅意味着chalk.red.yellow.green
等价于 chalk.green
.orm
多个参数将用空格分隔。
指定颜色支持的级别。
颜色支持是自动检测到的,可是您能够经过设置level属性来覆盖它。不过,您应该只在本身的代码中这样作,由于它将全局应用于全部chalk使用者。
若是您须要在可重用模块中更改此内容,则建立一个新实例:
const ctx = new chalk.Instance({level: 0});
Level | Description |
---|---|
0 |
All colors disabled |
1 |
Basic color support (16 colors) |
2 |
256 color support |
3 |
Truecolor support (16 million colors) |
检测终端是否支持颜色。内部使用,为您处理,但为了方便暴露。
能够由用户使用标志--color
和--no-color
覆盖。对于不可能使用--color
的状况,使用环境变量 FORCE_COLOR=1
(级别1)、FORCE_COLOR=2
(级别2)或FORCE_COLOR=3
(级别3)强制启用color,或FORCE_COLOR=0
强制禁用。使用FORCE_COLOR
会覆盖全部其余颜色支持检查。
显式256/Truecolor模式可分别使用--color=256
和--color=16m
标志启用。
chalk.stderr
包含一个单独的实例,该实例配置了针对 stderr
流而不是stdout
检测到的颜色支持.重写规则chalk.supportsColor
也适用于此,chalk.stderr.supportsColor
为了方便暴露
reset
- 重置当前颜色链。bold
- 加粗文本。dim
- 只发出少许的光。italic
- 使文本斜体。(不是普遍支持)underline
- 使文本下划线。(不是普遍支持)inverse
- 背景色和前景色反转。hidden
- 打印文本,但使其不可见。strikethrough
- 在文本中心放置一条水平线。(不是普遍支持)visible
- 仅当粉笔的颜色级别为>时打印文本。对于纯粹修饰的东西颇有用。black
red
green
yellow
blue
magenta
cyan
white
blackBright
(alias: gray
, grey
)redBright
greenBright
yellowBright
blueBright
magentaBright
cyanBright
whiteBright
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
bgBlackBright
(alias: bgGray
, bgGrey
)bgRedBright
bgGreenBright
bgYellowBright
bgBlueBright
bgMagentaBright
bgCyanBright
bgWhiteBright
Chalk能够用做已标记的模板文字
const chalk = require('chalk'); const miles = 18; const calculateFeet = miles => miles * 5280; console.log(chalk` There are {bold 5280 feet} in a mile. In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}. `);
块由左花括号({)、样式、一些内容和右花括号(})分隔。
模板样式与普通Chalk样式彻底相同。如下三句话是等价的:
console.log(chalk.bold.rgb(10, 100, 200)('Hello!')); console.log(chalk.bold.rgb(10, 100, 200)`Hello!`); console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
注意函数样式(rgb()
, hsl()
, keyword()
, 等等.)参数之间可能不包含空格
全部插入值(chalk`${foo}`) 经过 .toString()
方法转换为字符串,内插值字符串中的全部花括号({和})都要转义。
从Chrome 69开始,ANSI转义码就在开发者控制台获得了本地支持。
若是你使用的是Windows,帮你本身一个忙,使用Windows终端而不是cmd.exe。
基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档
检测终端是否支持颜色
yarn add supports-color
const supportsColor = require('supports-color'); if (supportsColor.stdout) { console.log('Terminal stdout supports color'); } if (supportsColor.stdout.has256) { console.log('Terminal stdout supports 256 colors'); } if (supportsColor.stderr.has16m) { console.log('Terminal stderr supports 16 million colors (truecolor)'); }
返回一个带有stdout
和stderr
属性的对象,用于测试这两个流。每一个属性都是一个对象,若是不支持颜色,则为false。
stdout
/stderr
对象经过一个.level
属性和一个对应的标志来指定对颜色的支持程度:
.level = 1
and .hasBasic = true
: Basic color support (16 colors).level = 2
and .has256 = true
: 256 color support.level = 3
and .has16m = true
: Truecolor support (16 million colors)基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档