Chalk Stencil
The tagged template literal for colorful command line outputs that you didn't know you needed!
Note: The asciinema video still references the module by its original name
chalk-template
, sorry about that. Please don't let it confuses you, the module is calledchalk-stencil
🙏
Install
$ npm install --save chalk-stencil # or $ yarn add chalk-stencil
Usage
You can create a colorful template using all the Chalk's available styles:
const chalk = const msg = chalk`This is going to be red, and this yellow` console
Features
Multiple props
As seen above, you can create a template that accepts multiple properties, each one with its own style.
chalk`This is going to be red, and this yellow`
This will return a template function which will accept an object which properties will be used to fill the placeholders.
Style chaining
You can also chain multiple styles for each property:
chalk`Red, white background and bold , cyan underlined `
Default style
You may want to give a default style to a whole template, well, you can by adding the ::<style>
at the very end of the string:
const errorMsg = chalk`Error: The exploded with the power of a thousand rainbows!::red` console
Unstyled template
You can even use this as a simple templating system, without styles; just drop the ::<style>
part:
const msg = chalk`Just a with `console// = Just a simple template with no style
Plain values
You can also use the special _
key, to pass in simple values, instead of objects:
const tpl = chalk`I just want this to be magenta` // = I just want this thing to be magenta// ^^^^^ magenta// I just want this 42 to be magenta// ^^ magenta
Raw strings
You can use raw strings as well, it Just Works:
chalk`The following .`
Simple usage
The chalk-stencil
tagged literal will always return a function, so that you can pass properties
to it, but you can also use it as if it was a plain tag:
console// = I'm going to rock tonight.// ^^^^ this one is green - everything else is just bold// note how you don't have to "call" the function at the end// i.e. no chalk`something`() to produce the actual string
Related
Chalk: chalk
License
MIT © Federico Giovagnoli