关于javascript:nodejs交互工具库-chalkpipe和chalk

9次阅读

共计 4888 个字符,预计需要花费 13 分钟才能阅读完成。

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

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

正文完
 0