[ADD] styleName to template

This commit is contained in:
Maxime Allanic 2020-04-27 14:42:19 +02:00
parent e5b69d9e43
commit 6e1f707083
No known key found for this signature in database
GPG key ID: AA17A3D7C7B6F14D
2 changed files with 8 additions and 6 deletions

View file

@ -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');
}

View file

@ -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');
});