[github地址:https://github.com/ABCDdouyae...]git
美化终端的字符串样式
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!'));
const chalk = require('chalk'); const log = console.log; const name = 'Sindre'; console.log(chalk.green('Hello %s'), name);
chalk.enable: 是否启用chalkgithub
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支持的颜色ide
const ctx = new chalk.constructor({level: 0});
chalk.supportsColor:检测terminal支持的色值格式,直接调用的包supports-color的stdoutui
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