JS知识点归纳 : Chalk颜色库使用

1.基本介绍

chalk.js可以为console.log()的输出信息配置不同的显示颜色和样式

2.安装

npm install chalk

3.使用

基本格式:

const chalk = require('chalk');

console.log(chalk.blue('Hello world!'));

示例:

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 ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);

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

自定义主题:

const chalk = require('chalk');

const error = chalk.bold.red;
const warning = chalk.keyword('orange');

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

样式api

  • reset – 样式重置

  • bold – 加粗

  • dim – 浅高亮

  • italic – 斜体

  • underline – 下划线

  • inverse– 反转前景和背景色

  • hidden – 隐藏内容

  • strikethrough – 删除线

  • visible– 显示chalk level > 0 的内容

前景色关键字(非全支持)

  • black

  • red

  • green

  • yellow

  • blue

  • magenta

  • cyan

  • white

  • blackBright (alias: gray, grey)

  • redBright

  • greenBright

  • yellowBright

  • blueBright

  • magentaBright

  • cyanBright

  • whiteBright

背景色关键字(非全支持)

  • bgBlack

  • bgRed

  • bgGreen

  • bgYellow

  • bgBlue

  • bgMagenta

  • bgCyan

  • bgWhite

  • bgBlackBright (alias: bgGray, bgGrey)

  • 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

学习更多知识,加QQ群:1098090823
威武网 » JS知识点归纳 : Chalk颜色库使用

提供最优质的资源集合

立即查看 了解详情