1.1.0 • Published 3 years ago

chalk-template v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

chalk-template

Terminal string styling with tagged template literals

Install

npm install chalk-template

Usage

For printing to standard output (stdout):

import chalkTemplate from 'chalk-template';
import chalk from 'chalk';

console.log(chalkTemplate`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);

console.log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));

const miles = 18;
const calculateFeet = miles => miles * 5280;

console.log(chalkTemplate`
	There are {bold 5280 feet} in a mile.
	In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
`);

console.log(chalkTemplate`
	There are also {#FF0000 shorthand hex styles} for
	both the {#ABCDEF foreground}, {#:123456 background},
	or {#ABCDEF:123456 both}.
`);

For printing to standard error (stderr):

import {chalkTemplateStderr} from 'chalk-template';

console.error(chalkTemplateStderr`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);

API

Blocks are delimited by an opening curly brace ({), a style, some content, and a closing curly brace (}).

Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent:

import chalk from 'chalk';
import chalkTemplate from 'chalk-template';

console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
console.log(chalkTemplate`{bold.rgb(10,100,200) Hello!}`);

Note that function styles (rgb(), etc.) may not contain spaces between parameters.

All interpolated values (chalkTemplate`${foo}`) are converted to strings via the .toString() method. All curly braces ({ and }) in interpolated value strings are escaped.

Template function

This function can be useful if you need to wrap the template function. However, prefer the default export whenever possible.

Note: It's up to you to properly escape the input.

import {template} from 'chalk-template';

console.log(template('Today is {red hot}'));
import {templateStderr} from 'chalk-template';

console.error(templateStderr('Today is {red hot}'));

Create template functions using a custom Chalk instance

The makeTemplate and makeTaggedTemplate functions are exported so functions can be created using a custom Chalk instance.

Note: When using a function created with makeTemplate, it's up to you to properly escape the input.

import {Chalk} from 'chalk'
import {makeTemplate, makeTaggedTemplate} from 'chalk-template';

const template = makeTemplate(new Chalk({level: 3}));
const chalkTemplate = makeTaggedTemplate(new Chalk({level: 3}));

console.log(template('Today is {red hot}'));
console.log(chalkTemplate`Today is {red hot}`);

Related

  • chalk - Terminal string styling done right
  • chalk-cli - Style text from the terminal

Maintainers

template-master@everything-registry/sub-chunk-1311tasktree-clisyncpackt_mastersynopkgterragrunt-statusspermjsbrain_servekickstartdskm-generatoriiif-manifest-editorhasura-cliload-esm-config@typestream/sdk@therealbenpai/dbb@temporalio/create@yvu/dri@zalastax/nolb-chaladopterazureopenailive2dchatbot-developappstore-connect-jwt-generator-clifpdf2picfreesolfuzz-runelection-tonight-clientfirst-lib-guivicconnnorconnormcli-todo-applicationcli-meow-helpconfectionerycommand-line-usagecode2docxcreate-weightmans-appcreate-jsx-emailcreate-sandstonecreate-polkadot-dappcreate-monotscspellblossom-auditauto-stepsbpsbot-discord-easydicebeardiff-detectivestrigitm_serverxuyumxfloworder-fetcherpdstoverdrivedownloadservesandstone-buildsandstone-clirelease-nowpushoo-clinocolnjknews-fragmentsnextauth-clinpgitweaver@storm-stack/core@linthtml/linthtml@ad1m/logger@ad1m/djb@aftdev/dev-env-manager@agilorunner/gitdeploy@agilorunner/html@agilorunner/js@agilorunner/run@agilorunner/sass@agilorunner/svg@agilorunner/wordpress@adobe/helix-onedrive-cli@adobe/helix-deploy-plugin-edge@adobe/helix-deploy-plugin-webpack@adobe/helix-cli@adobe/helix-deploy@adobe/create-franklin-library@adobe/create-helix-library@adobe/aem-cli@bluish/logger@btc-vision/as-pect-cli@btc-vision/as-pect-core@bytecodealliance/jco@as-pect/core@as-pect/cli@douganderson444/jco@gambitnash/cli-core@gambitnash/cli-plugin-help@geewizwow/serve@golemcloud/jco@reuters-graphics/graphics-bin@reuters-graphics/rngs-io-client@reuters-graphics/server-client@ricardomatias/latest-release@jay-js/cli
1.1.0

3 years ago

1.0.0

3 years ago

0.5.0

3 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.0

4 years ago

0.1.0

9 years ago