Adds support for nested chalk expressions.
green(a + blue(b) + c) will now look the same as green(a) + blue(b) + green(c), as expected. In the previous implementation the output would have been green(a) + blue(b) + c, because the reset code of the second expression would close all expressions around it as well. Now this reset code is replaced by a start code of the outer lying expression, both stopping the inner and re-starting the outer.
This commit is contained in:
parent
144421dc16
commit
d16b0ce367
3 changed files with 29 additions and 1 deletions
20
index.js
20
index.js
|
|
@ -22,6 +22,19 @@ var styles = (function () {
|
|||
return ret;
|
||||
})();
|
||||
|
||||
// enrich each ansiStyle object with regular expression matching
|
||||
// all instances of the corresponding code.close
|
||||
(function () {
|
||||
var _matchUnescapedCharacters = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
|
||||
function escapeStr(str) {
|
||||
return str.replace(_matchUnescapedCharacters, '\\$&');
|
||||
}
|
||||
|
||||
Object.keys(ansiStyles).forEach(function (key) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStr(ansiStyles[key].close), 'g');
|
||||
});
|
||||
})();
|
||||
|
||||
function init() {
|
||||
var ret = {};
|
||||
|
||||
|
|
@ -37,7 +50,12 @@ function init() {
|
|||
|
||||
return self._styles.reduce(function (str, name) {
|
||||
var code = ansiStyles[name];
|
||||
return str ? code.open + str + code.close : '';
|
||||
return str ?
|
||||
// Replace any instances already present with a re-opening code
|
||||
// otherwise only the part of the string until said closing code will be coloured, and the rest will be
|
||||
// simply 'plain'.
|
||||
code.open + str.replace(code.closeRe, code.open) + code.close :
|
||||
'';
|
||||
}, str);
|
||||
}, styles);
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ console.log( chalk.blue.bgRed.bold('Hello world!') );
|
|||
// nest styles
|
||||
console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
|
||||
|
||||
// nest styles of the same type even (colour, underline, background)
|
||||
console.log( chalk.green('Hello, I'm a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') );
|
||||
|
||||
// pass in multiple arguments
|
||||
console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
|
||||
```
|
||||
|
|
|
|||
7
test.js
7
test.js
|
|
@ -21,6 +21,13 @@ describe('chalk', function () {
|
|||
);
|
||||
});
|
||||
|
||||
it('should support nesting styles of the same type (color, underline, bg)', function () {
|
||||
assert.equal(
|
||||
chalk.red('a' + chalk.blue('b' + chalk.green('c') + 'b') + 'c'),
|
||||
'\u001b[31ma\u001b[34mb\u001b[32mc\u001b[34mb\u001b[31mc\u001b[39m'
|
||||
);
|
||||
});
|
||||
|
||||
it('should reset all styles with `.reset()`', function () {
|
||||
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue