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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.