在Chalk文档中,只是暗示这个库在 Windows 中不能正常工作:
如果您使用的是 Windows,请帮自己一个忙,使用 Windows 终端而不是 cmd.exe。
恕我直言,这是不可接受的,因为现代开发的基本工具是带有自己终端的 IDE,因此终端高亮库不能依赖特定的终端。
从这里,我们至少有三个问题:
- 为什么 Chalk 不能正确输出字体/背景颜色?
- 如何在指定的任何终端中输出背景或字体颜色?
- 如果 Chalk 做不到,哪些 Node.js 实用程序可以?
我知道我不能在一个问题中提出以上所有问题,因此当前的主题集中在为什么 Chalk 不能按指定输出颜色。
console.log(
Chalk.bgRed.bold(" Error ") + " " + Chalk.bgRedBright(" Short title ") + "\n" +
Chalk.red.bold("Long title. More than 2 words.") + "\n" +
Chalk.redBright("Description") + "\n"
);
console.log(
Chalk.bgYellow.bold(" Warning ") + " " + Chalk.bgYellowBright(" Short title ") + "\n" +
Chalk.yellow.bold("Long title. More than 2 words.") + "\n" +
Chalk.yellowBright("Description") + "\n"
);
console.log(
Chalk.bgGreen.bold(" Success ") + " " + Chalk.bgGreenBright(" Short title ") + "\n" +
Chalk.green.bold("Long title. More than 2 words.") + "\n" +
Chalk.greenBright("Description") + "\n"
);
console.log(
Chalk.bgBlue.bold(" Info ") + " " + Chalk.bgBlueBright(" Short title ") + "\n" +
Chalk.blue.bold("Long title. More than 2 words.") + "\n" +
Chalk.blueBright("Description") + "\n"
);
console.log(
Chalk.bgCyan.bold(" Info ") + " " + Chalk.bgCyanBright(" Short title ") + "\n" +
Chalk.cyan.bold("Long title. More than 2 words.") + "\n" +
Chalk.cyanBright("Description") + "\n"
);
console.log(
Chalk.bgMagenta.bold(" Info ") + " " + Chalk.bgMagentaBright(" Short title ") + "\n" +
Chalk.magenta.bold("Long title. More than 2 words.") + "\n" +
Chalk.magentaBright("Description") + "\n"
);
console.log(
Chalk.bgWhite.bold(" Info ") + " " + Chalk.bgWhiteBright(" Short title ") + "\n" +
Chalk.white.bold("Long title. More than 2 words.") + "\n" +
Chalk.whiteBright("Description") + "\n"
);
IntellIJ IDEA,Monokai 颜色主题:
- 对于每个
cosole.log()
,“长标题。超过 2 个单词。” 和“描述”具有相同的颜色。 - 有些
bg**
s 比bg**Bright
! bold
除了 white 之外的所有s 都被忽略了。- 字体颜色已在定义背景颜色的位置自动设置。我没有问这个,有时字体几乎不可读。
命令
稍微好一点,但大部分问题仍然存在。
默认 Windows 终端
与上面的相比,还不错。