picocolors-learn
Picocolors
作用
使用ANSI颜色进行终端输出格式化的最小和最快的库。
1 | import pc from "picocolors"; |
特性:
- 无依赖
- 比
chalk
库的体积小 14 倍,速度快 2 倍 - 被像
PostCSS
、SVGO
、Stylelint
,和Browserslist
这些流行工具使用 - Node.js v6+ 和浏览器支持。支持 CJS 和 ESM 项目。
- 包括 TypeScript 类型声明
NO_COLOR
友好
使用
Picocolors
提供了一个对象,其中包括各种文本着色和格式化功能
1 | import pc from "picocolors"; |
该对象包括以下着色方法:black
,red
,green
,yellow
,blue
,magenta
,cyan
,white
,gray
.
1 | console.log(`I see a ${pc.red("red door")} and I want it painted ${pc.black("black")}`); |
该对象也包括以下背景色修改方法:bgBlack
,bgRed
,bgGreen
,bgYellow
,bgBlue
,bgMagenta
,bgCyan
,bgWhite
.
1 | console.log(pc.bgBlack(pc.white(`Tom appeared on the sidewalk with a bucket of whitewash and a long-handled brush.`))); |
除了颜色,该对象还包括以下格式化方法:dim
,bold
,hidden
,italic
,underline
,strikethrough
,reset
,inverse
.
1 | for (let task of tasks) { |
该库还提供了一些额外的工具来确保任务的最佳结果:
isColorSupported
: 布尔值,明确地告诉屏幕上是否支持出现颜色或格式1
2
3
4
5import pc from "picocolors";
if (pc.isColorSupported) {
console.log("Yay! This script can use colors and formatters");
}createColors(enabled)
: 一个函数,返回一个带有手动定义的颜色支持配置的新的 API 对象1
2
3import pc from "picocolors";
let { red, bgWhite } = pc.createColors(options.enableColors);
测试代码
1 | import pc from "picocolors"; |
结果截图: