135 | | 137 | |
136 | const createBuilder = (self, _styler, _isEmpty) => { | 138 | const createBuilder = (self, _styler, _isEmpty) => { |
137 | const builder = (...arguments_) => { | 139 | const builder = (...arguments_) => { |
| | 140 | if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) { |
| | 141 | // Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}` |
| | 142 | return applyStyle(builder, chalkTag(builder, ...arguments_)); |
| | 143 | } |
| | 144 | |
138 | // Single argument is hot path, implicit coercion is faster than anything | 145 | // Single argument is hot path, implicit coercion is faster than anything |
139 | // eslint-disable-next-line no-implicit-coercion | 146 | // eslint-disable-next-line no-implicit-coercion |
140 | return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); | 147 | return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); |
189 | const chalkTag = (chalk, ...strings) => { | 196 | const chalkTag = (chalk, ...strings) => { |
190 | const [firstString] = strings; | 197 | const [firstString] = strings; |
191 | | 198 | |
192 | if (!Array.isArray(firstString)) { | 199 | if (!isArray(firstString) || !isArray(firstString.raw)) { |
193 | // If chalk() was called by itself or with a string, | 200 | // If chalk() was called by itself or with a string, |
194 | // return the string itself as a string. | 201 | // return the string itself as a string. |
195 | return strings.join(' ');--- package/package.json | 202 | return strings.join(' ');--- package/package.json |
| | 203 | ++ package/package.json |
9 | | 9 | |
10 | > Terminal string styling done right | 10 | > Terminal string styling done right |
11 | | 11 | |
12 | [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg) [![run on repl.it](http://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk) | 12 | [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg) [![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk) |
13 | | 13 | |
14 | <img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900"> | 14 | <img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900"> |
15 | | 15 | |
215 | | 215 | |
216 | Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`). | 216 | Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`). |
217 | | 217 | |
218 | Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent: | 218 | Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent: |
219 | | 219 | |
220 | ```js | 220 | ```js |
221 | console.log(chalk.bold.rgb(10, 100, 200)('Hello!')); | 221 | console.log(chalk.bold.rgb(10, 100, 200)('Hello!')); |
| | 222 | console.log(chalk.bold.rgb(10, 100, 200)`Hello!`); |
222 | console.log(chalk`{bold.rgb(10,100,200) Hello!}`); | 223 | console.log(chalk`{bold.rgb(10,100,200) Hello!}`); |
223 | ``` | 224 | ``` |
224 | --- package/index.d.ts | 225 | --- package/index.d.ts |
| | 226 | ++ package/index.d.ts |
137 | DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%} | 137 | DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%} |
138 | `); | 138 | `); |
139 | ``` | 139 | ``` |
| | 140 | |
| | 141 | @example |
| | 142 | ``` |
| | 143 | import chalk = require('chalk'); |
| | 144 | |
| | 145 | log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`) |
| | 146 | ``` |
140 | */ | 147 | */ |
141 | (text: TemplateStringsArray, ...placeholders: unknown[]): string; | 148 | (text: TemplateStringsArray, ...placeholders: unknown[]): string; |