chalk-pipe
应用更简略的款式字符串创立粉笔款式计划
Install
yarn add chalk-pipe
Usage
const chalkPipe = require('chalk-pipe');console.log(chalkPipe('blue.bold')('Hello world!'));
应用点.
辨别多种款式:
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);
API
chalkPipe(styles)(text)
chalkPipe('blue.underline')('Link!');
chalkPipe(styles, chalk)(text)
const chalk = require('chalk');chalk.enable = true;chalkPipe('underline', chalk.blue)('Link!');
Valid styles
- Modifiers
- Colors
- Background colors
- Hex triplet
- CSS keywords
参考
根本罕用的办法场景就这些了,更残缺的用法能够间接查阅文档
chalk-pipe
如果须要更加粗疏的需要,就要应用上面的chalk
了,毕竟这只是它简化版的库
chalk
正确处理终端字符串款式
亮点
- 富裕表现力的API
- 高性能
- 嵌套款式的能力
- 256 /真彩色彩反对
- 主动侦测色彩反对
- 不缩短String.prototype
- 清洁和集中
- 踊跃保护
- 截至2020年1月1日,被约5万个软件包应用
Install
yarn add chalk
Usage
Chalk提供了一个易于应用的可组合API,您只需链嵌您想要的款式。
const chalk = require('chalk');const log = console.log;// Combine styled and normal stringslog(chalk.blue('Hello') + ' World' + chalk.red('!'));// Compose multiple styles using the chainable APIlog(chalk.blue.bgRed.bold('Hello world!'));// Pass in multiple argumentslog(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));// Nest styleslog(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 literallog(` CPU: ${chalk.red('90%')} RAM: ${chalk.green('40%')} DISK: ${chalk.yellow('70%')}`);// ES2015 tagged template literallog(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!'));
API
chalk.<style>[.<style>...](string, [string...])
Example: chalk.red.bold.underline('Hello', 'world');
链款式,并将最初一个作为带有字符串参数的办法调用。程序不重要,当前的款式在发生冲突的状况下会有先例. 这仅仅意味着chalk.red.yellow.green
等价于 chalk.green
.
多个参数将用空格分隔。
chalk.level
指定色彩反对的级别。
色彩反对是自动检测到的,然而您能够通过设置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) |
chalk.supportsColor
检测终端是否反对色彩。外部应用,为您解决,但为了不便裸露。
能够由用户应用标记--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 and chalk.stderr.supportsColor
chalk.stderr
蕴含一个独自的实例,该实例配置了针对 stderr
流而不是stdout
检测到的色彩反对.重写规定chalk.supportsColor
也实用于此,chalk.stderr.supportsColor
为了不便裸露
Styles
Modifiers
reset
- 重置以后色彩链。bold
- 加粗文本。dim
- 只收回大量的光。italic
- 使文本斜体。(不是广泛支持)underline
- 使文本下划线。(不是广泛支持)inverse
- 背景色和前景色反转。hidden
- 打印文本,但使其不可见。strikethrough
- 在文本核心搁置一条水平线。(不是广泛支持)visible
- 仅当粉笔的色彩级别为>时打印文本。对于纯正润饰的货色很有用。
Colors
black
red
green
yellow
blue
magenta
cyan
white
blackBright
(alias:gray
,grey
)redBright
greenBright
yellowBright
blueBright
magentaBright
cyanBright
whiteBright
Background colors
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
bgBlackBright
(alias:bgGray
,bgGrey
)bgRedBright
bgGreenBright
bgYellowBright
bgBlueBright
bgMagentaBright
bgCyanBright
bgWhiteBright
Tagged template literal
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()
办法转换为字符串,内插值字符串中的所有花括号({和})都要本义。
Browser support
从Chrome 69开始,ANSI本义码就在开发者控制台失去了本地反对。
Windows
如果你应用的是Windows,帮你本人一个忙,应用Windows终端而不是cmd.exe。
参考
根本罕用的办法场景就这些了,更残缺的用法能够间接查阅文档
chalk
supports-color
检测终端是否反对色彩
Install
yarn add supports-color
Usage
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)');}
API
返回一个带有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)
参考
根本罕用的办法场景就这些了,更残缺的用法能够间接查阅文档
supports-color