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:
Joshua Appelman 2014-06-21 06:22:24 +02:00
parent 144421dc16
commit d16b0ce367
3 changed files with 29 additions and 1 deletions

View file

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