Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

terminal - Why the Chalk, the Node.js package can not output correct formatting in Windows?

In the Chalk documentation, there is just allusion that this library will not work properly in Windows:

If you're on Windows, do yourself a favor and use Windows Terminal instead of cmd.exe.

IMHO it's not acceptable because the basic tool of modern development is IDE with own terminal, so the terminal highlight library must not depend on specific terminal.

From here, we have at least three questions:

  1. Why Chalk can not correctly output the font/background color?
  2. How to output background or font color in any terminal as specified?
  3. If Chalk can't do it, which Node.js utilities can?

I know I can't ask all of above in single question, so current topic is focused on why Chalk can't output colors as specified.

console.log(
    Chalk.bgRed.bold(" Error ") + "   " + Chalk.bgRedBright(" Short title ") + "
" +
    Chalk.red.bold("Long title. More than 2 words.") + "
" +
    Chalk.redBright("Description") + "
"
);

console.log(
    Chalk.bgYellow.bold(" Warning ") + "   " + Chalk.bgYellowBright(" Short title ") + "
" +
    Chalk.yellow.bold("Long title. More than 2 words.") + "
" +
    Chalk.yellowBright("Description") + "
"
);

console.log(
    Chalk.bgGreen.bold(" Success ") + "   " + Chalk.bgGreenBright(" Short title ") + "
" +
    Chalk.green.bold("Long title. More than 2 words.") + "
" +
    Chalk.greenBright("Description") + "
"
);

console.log(
    Chalk.bgBlue.bold(" Info ") + "   " + Chalk.bgBlueBright(" Short title ") + "
" +
    Chalk.blue.bold("Long title. More than 2 words.") + "
" +
    Chalk.blueBright("Description") + "
"
);

console.log(
    Chalk.bgCyan.bold(" Info ") + "   " + Chalk.bgCyanBright(" Short title ") + "
" +
    Chalk.cyan.bold("Long title. More than 2 words.") + "
" +
    Chalk.cyanBright("Description") + "
"
);

console.log(
    Chalk.bgMagenta.bold(" Info ") + "   " + Chalk.bgMagentaBright(" Short title ") + "
" +
    Chalk.magenta.bold("Long title. More than 2 words.") + "
" +
    Chalk.magentaBright("Description") + "
"
);

console.log(
    Chalk.bgWhite.bold(" Info ") + "   " + Chalk.bgWhiteBright(" Short title ") + "
" +
    Chalk.white.bold("Long title. More than 2 words.") + "
" +
    Chalk.whiteBright("Description") + "
"
);

IntellIJ IDEA, Monokai color theme:

enter image description here

  1. For each cosole.log(), "Long title. More than 2 words." and "Description" has same color.
  2. Some bg**s are brighter than bg**Bright!
  3. All bolds except white has been ignored.
  4. The font color has been set automatically where background color defined. I did not ask this, and also sometimes the font is almost unreadable.

Cmder

A little bit better, but most of problems left.

enter image description here

Default Windows terminal

Not so bad in the comparison with above ones.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Chalk cannot display colors in cmd.exe because it relies on ANSI escape sequences and this is simply not supported out-of-the-box in cmd.exe. In newer versions of Windows 10 it seems to be possible, but has to be activated. There are also some tools that make it available on older versions. You can find a lot of useful information in this SO question: How to make win32 console recognize ANSI/VT100 escape sequences?

To your other two questions about which tool can be used to output colors in any terminal: I think Chalk is already a very good library and it already uses the ANSI standard to ensure compatibility. So if a terminal does not support ANSI, the terminal is the bottleneck. I'm not aware of any other more compatible technology.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...