Created
May 31, 2017 12:11
-
-
Save bennadel/6766102feecdf2d94d19f605c5d38692 to your computer and use it in GitHub Desktop.
The Chalk Module Can Be Used With The Console's String Interpolation In Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require the core node modules. | |
var chalk = require( "chalk" ); | |
// ----------------------------------------------------------------------------------- // | |
// ----------------------------------------------------------------------------------- // | |
// The approach that I normally use when nesting styles is to include a variable | |
// number of arguments in the root chalk() call. This allows the styles to build upon | |
// each other. | |
// -- | |
// NOTE: Using the "+" in order to prevent a space before the ",". | |
process.stdout.write( "1: " ); | |
console.log( chalk.red( "Hello", chalk.bold( "Kim" ) + ", I hope you are well." ) ); | |
// The above works perfectly; but, it leaves the code with some readability problems. | |
// We can try to make the logic and formatting easier to read by using the "formatting" | |
// functionality of the Console (supplied under the hood by util.format()). Notice that | |
// in this version, we're using "%s" to interpolate original log message AND that we're | |
// using chalk in the interpolation value. | |
process.stdout.write( "2: " ); | |
console.log( chalk.red( "Hello %s, I hope you are well." ), chalk.bold( "Kim" ) ); | |
// Using the Console interpolation, however, doesn't work in all cases. It works for | |
// any case in which we are NOT OVERRIDING COLORS (foreground or background). Colors | |
// get some special treatment. But, any non-color modifiers can be used safely in the | |
// interpolation value. | |
process.stdout.write( "3: " ); | |
console.log( | |
chalk.red( "Hello %s, I %s you are %s." ), | |
// Here, are keeping the same COLOR (background / foreground), but we're adding | |
// style modifiers. This works! This is cool beans! | |
chalk.bold( "Kim" ), | |
chalk.underline( "rather hope" ), | |
chalk.italic( "happy" ) | |
); | |
// If we NEST THE STYLES directly, however, we can definitely override colors. This is | |
// because chalk is smart enough to know styles are being nested and will RE-OPEN the | |
// previous style when the nested style is closed. | |
process.stdout.write( "4: " ); | |
console.log( chalk.red( "Hello", chalk.black( "Kim" ) + ", I think you are great!" ) ); | |
// ... however, chalk can only be smart if it is doing the nesting directly. If we rely | |
// on the console interpolation to do the nesting, chalk doesn't see it, so IT CANNOT | |
// RE-OPEN THE STYLE. This leaves the remainder of the text with the DEFAULT colors. | |
// -- | |
// CAUTION: THIS APPROACH DOES NOT WORK (for foreground or background conflicts). | |
process.stdout.write( "5: " ); | |
console.log( chalk.red( "Hello %s, I think you are great!" ), chalk.black( "Kim" ) ); | |
// Of course, if the outer console item has the DEFAULT background and foreground colors, | |
// then we can totally use color styles in the interpolation - since the reset just puts | |
// the style back into the default state. | |
process.stdout.write( "6: " ); | |
console.log( "Hello %s, so great running into you!", chalk.red.bold( "Kim" ) ); | |
// NOTE: We don't need to use variable arguments in order to get the nested styles to | |
// work properly. We can use string templates to get the same thing to work. This is | |
// because chalk is actually using PATTERN MATCHING to find the CLOSE pattern of any | |
// nested styles in order to replace them with an OPEN pattern of the parent style. | |
process.stdout.write( "7: " ); | |
var value = chalk.black.bold.underline( "Kim" ); | |
console.log( chalk.red( `Hello ${ value }, rock on with your bad self.` ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment