【每日一包0030】chalk

Chalk是一个用于美化Node.js终端输出的库,提供丰富的颜色和样式选项,支持链式调用和多种颜色格式,如RGB、Hex和关键字颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

clipboard.png

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

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

clipboard.png

利用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'));

clipboard.png

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值