资讯专栏INFORMATION COLUMN

【每日一包0030】chalk

Kross / 2013人阅读

摘要:地址美化终端的字符串样式你可以像下面这样简单的使用它将样式字符串和普通字符串合并使用采用链式方法调用多个传入多个参数套用支持反引号解析字符串支持各种书写颜色的方式可以定义自己的主题利用字符串替换是否启用设置支持的颜色检测支持的色值格式直接调

[github地址:https://github.com/ABCDdouyae...]

chalk
美化终端的字符串样式
你可以像下面这样简单的使用它
const chalk = require("chalk");
const log = console.log;

//将样式字符串和普通字符串合并使用
log(chalk.blue("Hello") + " world" + chalk.red("!"));

//采用链式方法调用多个API
log(chalk.blue.bgRed.bold("Hello world!"));

//传入多个参数
log(chalk.blue("Hello", "World!", "Foo", "bar", "biz", "baz"));

//API套用
log(chalk.red("Hello", chalk.underline.bgBlue("world") + "!"));
log(chalk.green(
    "I am a green line " +
    chalk.blue.underline.bold("with a blue substring") +
    " that becomes green again!"
));

//支持反引号解析字符串
log(`
CPU: ${chalk.red("90%")}
RAM: ${chalk.green("40%")}
DISK: ${chalk.yellow("70%")}
`);

let cpu = {totalPercent: 67};
let ram = {used:40, total:100};
let disk = {used: 130, total: 512}
log(chalk`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);

//支持各种书写颜色的方式
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!"));

可以定义自己的主题
const chalk = require("chalk");
const log = console.log;
const error = chalk.bold.red;
const warning = chalk.keyword("orange");

log(error("Error!"));
log(warning("Warning!"));

利用console.log字符串替换
const chalk = require("chalk");
const log = console.log;

const name = "Sindre";
console.log(chalk.green("Hello %s"), name);
API

chalk.enable: 是否启用chalk

const log = console.log;
const ctx1 = new chalk.constructor({enabled: true});
log(ctx1.red("Hello World"));
const ctx2 = new chalk.constructor({enabled: false});
log(ctx2.red("Hello World"));

chalk.level:设置chalk支持的颜色

const ctx = new chalk.constructor({level: 0});

1.All colors disabled

2.Basic color support (16 colors)

3.256 color support

4.Truecolor support (16 million colors)

chalk.supportsColor:检测terminal支持的色值格式,直接调用的包supports-color的stdout

log(chalk.supportsColor)//{ level: 2, hasBasic: true, has256: true, has16m: false }
支持的样式和颜色
样式

reset

bold

dim

italic (Not widely supported)

underline

inverse

hidden

strikethrough (Not widely supported)

visible (Text is emitted only if enabled)

颜色

black

red

green

yellow

blue (On Windows the bright version is used since normal blue is illegible)

magenta

cyan

white

gray ("bright black")

redBright

greenBright

yellowBright

blueBright

magentaBright

cyanBright

whiteBright

背景色

bgBlack

bgRed

bgGreen

bgYellow

bgBlue

bgMagenta

bgCyan

bgWhite

bgBlackBright

bgRedBright

bgGreenBright

bgYellowBright

bgBlueBright

bgMagentaBright

bgCyanBright

bgWhiteBright

颜色格式

rgb - Example: chalk.rgb(255, 136, 0).bold("Orange!")

hex - Example: chalk.hex("#FF8800").bold("Orange!")

keyword (CSS keywords) - Example: chalk.keyword("orange").bold("Orange!")

hsl - Example: chalk.hsl(32, 100, 50).bold("Orange!")

hsv - Example: chalk.hsv(32, 100, 100).bold("Orange!")

hwb - Example: chalk.hwb(32, 0, 50).bold("Orange!")

ansi16

ansi256

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/53448.html

相关文章

  • 每日一包0030chalk

    摘要:地址美化终端的字符串样式你可以像下面这样简单的使用它将样式字符串和普通字符串合并使用采用链式方法调用多个传入多个参数套用支持反引号解析字符串支持各种书写颜色的方式可以定义自己的主题利用字符串替换是否启用设置支持的颜色检测支持的色值格式直接调 [github地址:https://github.com/ABCDdouyae...] chalk 美化终端的字符串样式 你可以像下面这样简单的使...

    OnlyLing 评论0 收藏0
  • Python每日一练0030

    摘要:而操作符描述的是,一个集合是否是另一个集合的真子集接受一个可迭代对象作为参数,并且判断这个可迭代对象的所有元素是否全部为。相应的,还有函数,用来判断是否存在一个。 问题 如何判断一个list包含另一个list 例如我有两个list List1 = [a,c,c] List2 = [x,b,a,x,c,y,c] 如何判断list2包含了list1 解决方案 如果元素的数量不重要,也就是说...

    taowen 评论0 收藏0
  • 每日一包0029】merge-descriptors

    摘要:地址源码依赖一个对象的属性继承另一个对象的属性及其属性描述符用法继承者,被继承者是否继承者有该属性的时候继承默认不继承返回继承后的新的对象当第三个参数为时候,原对象又该属性则没有继承被继承者的属性和属性描述符 [github地址:https://github.com/ABCDdouyae...] merge-descriptors (express源码依赖) 一个对象的属性继承另一个对...

    kun_jian 评论0 收藏0
  • 每日一包0029】merge-descriptors

    摘要:地址源码依赖一个对象的属性继承另一个对象的属性及其属性描述符用法继承者,被继承者是否继承者有该属性的时候继承默认不继承返回继承后的新的对象当第三个参数为时候,原对象又该属性则没有继承被继承者的属性和属性描述符 [github地址:https://github.com/ABCDdouyae...] merge-descriptors (express源码依赖) 一个对象的属性继承另一个对...

    printempw 评论0 收藏0

发表评论

0条评论

Kross

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<