[github地址:https://github.com/ABCDdouyae…]chalk美化终端的字符串样式你可以像下面这样简单的使用它const chalk = require(‘chalk’);const log = console.log;//将样式字符串和普通字符串合并使用log(chalk.blue(‘Hello’) + ’ world’ + chalk.red(’!’));//采用链式方法调用多个APIlog(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(chalkCPU: {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);APIchalk.enable: 是否启用chalkconst 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 disabled2.Basic color support (16 colors)3.256 color support4.Truecolor support (16 million colors)chalk.supportsColor:检测terminal支持的色值格式,直接调用的包supports-color的stdoutlog(chalk.supportsColor)//{ level: 2, hasBasic: true, has256: true, has16m: false }支持的样式和颜色样式resetbolddimitalic (Not widely supported)underlineinversehiddenstrikethrough (Not widely supported)visible (Text is emitted only if enabled)颜色blackredgreenyellowblue (On Windows the bright version is used since normal blue is illegible)magentacyanwhitegray (“bright black”)redBrightgreenBrightyellowBrightblueBrightmagentaBrightcyanBrightwhiteBright背景色bgBlackbgRedbgGreenbgYellowbgBluebgMagentabgCyanbgWhitebgBlackBrightbgRedBrightbgGreenBrightbgYellowBrightbgBlueBrightbgMagentaBrightbgCyanBrightbgWhiteBright颜色格式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!’)ansi16ansi256