◆

Node.js chalk 终端彩色输出使用方法详解

2023-02-02 01:370

使用方法

const chalk = require('chalk');
const log = console.log;

// 基本用法
log(chalk.blue.bgRed.underline.bold('Hello world!'));
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 error = chalk.bold.red;
const warning = chalk.keyword('blue');

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

// es6 写法
const obj = {
  totalPercent: 10,
  totalPercent1: 10,
};

log(chalk`CPU: {red ${obj.totalPercent}%}CPU2: {red ${obj.totalPercent1}%}`);

/*
 chalk.hex('#DEADED').underline('Hello, world!')
 chalk.keyword('orange')('Some orange text')
 chalk.rgb(15, 100, 204).inverse('Hello!')
 
 chalk.bgHex('#DEADED').underline('Hello, world!')
 chalk.bgKeyword('orange')('Some orange text')
 chalk.bgRgb(15, 100, 204).inverse('Hello!')
 */

参数修饰符

  • reset - Resets the current color chain.
  • bold - Make text bold.
  • dim - Emitting only a small amount of light.
  • italic - Make text italic. (Not widely supported)
  • underline - Make text underline. (Not widely supported)
  • inverse- Inverse background and foreground colors.
  • hidden - Prints the text, but makes it invisible.
  • strikethrough - Puts a horizontal line through the center of the text. (Not widely supported)
  • visible- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.

颜色

  • 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
💬留言板0

还没有留言,来发表第一条吧~

PAGE LOAD / LIVENode.js chalk 终端彩色输出使用方法详解加载中