+-
nodejs交互工具库 -- chalk-pipe和chalk

chalk-pipe

使用更简单的样式字符串创建粉笔样式方案
image

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!'));

image

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

image

正确处理终端字符串样式

image

亮点

富有表现力的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 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!'));

image

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

返回一个带有stdoutstderr属性的对象,用于测试这两个流。每个属性都是一个对象,如果不支持颜色,则为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