NodeJS中的大多数终端应用程序或工具都是单一颜色的。"Chalk "是一个nodejs包,它允许你将你的应用程序终端输出字符串样式化为各种颜色和风格,使其更具可读性和色彩。
安装是通过npm进行的。
npm install chalk
使用方法很短,很简单。
const chalk = require('chalk');
console.log(chalk.blue('Hello world!')); // Displays string in Red
一个更详细的例子。
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.bgYellow.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!'
));
输出如下所示。
你也可以轻松地定义你自己的主题。
const chalk = require('chalk');
const error = chalk.keyword('red');
const warning = chalk.keyword('orange');
const success = chalk.keyword('green');
console.log(error('Error!'));
console.log(warning('Warning!'));
console.log(success('Done!'));
假设平台支持该特定的风格,Chalk的风格可以被链在一起。
console.log(chalk.red.bold.underline('Hello', 'world'));
更详细的信息可以在chalk nodejs npm找到。