0

Thanks to the question How to change node.js's console font color?, I learned how to change the console output's font color natively.

The next step was the applying of the multiple formattings to single string. The solution like

console.log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");

sometimes works, sometimes - no, but experimentally I explored that we can just concatenate multiple... what is it called? ANSII command sequence? The escape sequence?

console.log("\x1b[41m\x1b[33m%s\x1b[0m", "Formatted output");

enter image description here

Now, how can I join the multiple strings with different formatting? For example, the red background and yellow foreground first, than yellow background? Below experiment gave the output does not match wit desired

console.log("\x1b[41m\x1b[33m%s\x1b[0m", "Output", "\x1b[43m\x1b[33m%s\x1b[0m", "Another formatted output");

enter image description here

Please, no recommendations of third-party libraries here, because current topics is focusing on native solutions.

Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124

1 Answers1

1

You are using substitution strings, but the parameters are

console.log(msg [, subst1, ..., substN]);

Only the first argument should contain the substitution strings. The other arguments contain the strings that are used to substitute. You can log with

console.log("\x1b[41m\x1b[33m%s\x1b[0m\x1b[43m\x1b[31m%s\x1b[0m", "Output", "Another formatted output");

Here you can find more details about console.log.

jabaa
  • 5,844
  • 3
  • 9
  • 30