chalk-stencil
v1.0.0
Published
Tagged template literal for stylish outputs
Downloads
22
Maintainers
Readme
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 = require('chalk-stencil')
const msg = chalk`This ${'text::red'} is going to be red,
and this ${'one::yellow'} yellow`
console.log(msg({ text: 'text', one: 'other text' }))
Features
Multiple props
As seen above, you can create a template that accepts multiple properties, each one with its own style.
chalk`This ${'text::red'} is going to be red,
and this ${'one::yellow'} 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 ${'prop1::red.bgWhite.bold'},
cyan underlined ${'prop2::cyan.underline'}`
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 ${'errorOrigin::underline.cyan'}
exploded with the power of a thousand rainbows!::red`
console.error(errorMsg({ errorOrigin: 'Spaceship' }))
Unstyled template
You can even use this as a simple templating system, without styles; just drop the ::<style>
part:
const msg = chalk`Just a ${'prop1'} with ${'prop2'}`
console.log(msg({ prop1: 'simple template', prop2: 'no style' }))
// = 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 ${'_::magenta'} to be magenta`
tpl('thing')
// = I just want this thing to be magenta
// ^^^^^ magenta
tpl(42)
// I just want this 42 to be magenta
// ^^ magenta
Raw strings
You can use raw strings as well, it Just Works:
chalk`The following ${'text will be colored::magenta'}.`
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.log(chalk`I'm going to ${'rock::green'} tonight.::bold`)
// = 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