chalk
1 是什么
chalk 是一个用于在 Node.js 应用程序中给终端输出上色的库,主要通过在字符串中插入 ANSI 转义序列来改变文字的颜色、背景色以及其他样式。它使得日志信息和命令行输出更加美观和易读,特别是在调试和输出信息时。
补充:在浏览器控制台输入带颜色的内容需要使用console.log(‘%c目标文本’,‘样式代码’),比如:console.log(‘%c你好!!!’,‘color:red;’)
2 用法
- 安装
npm install chalk -D
- 使用
const chalk = require('chalk');
// 基本颜色
console.log(chalk.red('Hello world!')); // 红色
console.log(chalk.green('Hello world!')); // 绿色
// 背景色
console.log(chalk.bgYellow('Hello world!')); // 黄色背景
// 多个样式组合
console.log(chalk.blue.bgWhite.bold('Hello world!')); // 蓝色文字、白色背景、加粗
// 字体修饰
console.log(chalk.underline('Underlined text')); // 下划线
// 自定义颜色
console.log(chalk.hex('#FF5733')('Custom Hex Color')); // 使用 hex 颜色
3 替代品
3.1 Colors.js
3.2 cli-color
3.3 Chalk7
3.4 kleur
3.5 terminal-colors
99 参考文档
https://blog.csdn.net/qq_46123200/article/details/143684391
https://blog.csdn.net/sdasadasds/article/details/129875582