diff --git a/source/index.js b/source/index.js index f16c054..67cfba4 100644 --- a/source/index.js +++ b/source/index.js @@ -58,7 +58,7 @@ function Chalk(options) { for (const [styleName, style] of Object.entries(ansiStyles)) { styles[styleName] = { get() { - const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty); + const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty, styleName); Object.defineProperty(this, styleName, {value: builder}); return builder; } @@ -81,7 +81,7 @@ for (const model of usedModels) { const {level} = this; return function (...arguments_) { const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler); - return createBuilder(this, styler, this._isEmpty); + return createBuilder(this, styler, this._isEmpty, model); }; } }; @@ -94,7 +94,7 @@ for (const model of usedModels) { const {level} = this; return function (...arguments_) { const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler); - return createBuilder(this, styler, this._isEmpty); + return createBuilder(this, styler, this._isEmpty, bgModel); }; } }; @@ -133,7 +133,7 @@ const createStyler = (open, close, parent) => { }; }; -const createBuilder = (self, _styler, _isEmpty) => { +const createBuilder = (self, _styler, _isEmpty, styleName) => { const builder = (...arguments_) => { if (arguments_.length === 1) { // Single argument is hot path, implicit coercion is faster than anything @@ -150,7 +150,7 @@ const createBuilder = (self, _styler, _isEmpty) => { } arguments_ = arguments_.slice(1); - const parts = [firstString.raw[0]]; + const parts = ['{' + styleName + ' ', firstString.raw[0]]; for (let i = 1; i < firstString.length; i++) { parts.push( @@ -159,6 +159,8 @@ const createBuilder = (self, _styler, _isEmpty) => { ); } + parts.push('}'); + if (template === undefined) { template = require('./templates'); } diff --git a/test/template-literal.js b/test/template-literal.js index ce0ded9..d538397 100644 --- a/test/template-literal.js +++ b/test/template-literal.js @@ -180,6 +180,6 @@ test('should support nested calls', t => { const instance = new chalk.Instance({level: 3}); const name = 'Sindre'; const exclamation = 'Neat'; - const result = instance.bold`{bold Hello, {cyan.inverse ${name}!} This is a test. {green ${exclamation}!}}`; + const result = instance.bold`Hello, {cyan.inverse ${name}!} This is a test. {green ${exclamation}!}`; t.is(result, '\u001B[1mHello, \u001B[22m\u001B[1m\u001B[36m\u001B[7mSindre!\u001B[27m\u001B[39m\u001B[22m\u001B[1m This is a test. \u001B[22m\u001B[1m\u001B[32mNeat!\u001B[39m\u001B[22m'); });