Add multiline handling functionality

Allows multiline logs to have each line styled properly.
This commit is contained in:
Josh Pike 2018-04-03 13:35:15 +07:00 committed by GitHub
parent dc82cc01d0
commit 172098b1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,14 +42,14 @@ if (typeof colors.enabled === "undefined") {
colors.enabled = colors.supportsColor() !== false;
}
colors.stripColors = colors.strip = function(str){
colors.stripColors = colors.strip = function (str) {
return ("" + str).replace(/\x1B\[\d+m/g, '');
};
var stylize = colors.stylize = function stylize (str, style) {
var stylize = colors.stylize = function stylize(str, style) {
if (!colors.enabled) {
return str+'';
return str + '';
}
return ansiStyles[style].open + str + ansiStyles[style].close;
@ -60,7 +60,7 @@ var escapeStringRegexp = function (str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string');
}
return str.replace(matchOperatorsRe, '\\$&');
return str.replace(matchOperatorsRe, '\\$&');
}
function build(_styles) {
@ -88,7 +88,7 @@ var styles = (function () {
return ret;
})();
var proto = defineProps(function colors() {}, styles);
var proto = defineProps(function colors() { }, styles);
function applyStyle() {
var args = arguments;
@ -107,12 +107,23 @@ function applyStyle() {
var nestedStyles = this._styles;
var i = nestedStyles.length;
var rebuiltString = '';
while (i--) {
var code = ansiStyles[nestedStyles[i]];
str = code.open + str.replace(code.closeRe, code.open) + code.close;
// multiline handling functionality
var lines = str.split(/[\r\n]+/)
var rebuiltString = '';
lines.forEach(line => {
rebuiltString += (code.open + line.replace(code.closeRe, code.open) + code.close + '\n')
})
}
return str;
return rebuiltString;
}
colors.setTheme = function (theme) {
@ -124,11 +135,11 @@ colors.setTheme = function (theme) {
return;
}
for (var style in theme) {
(function(style){
colors[style] = function(str){
if (typeof theme[style] === 'object'){
(function (style) {
colors[style] = function (str) {
if (typeof theme[style] === 'object') {
var out = str;
for (var i in theme[style]){
for (var i in theme[style]) {
out = colors[theme[style][i]](out);
}
return out;
@ -151,7 +162,7 @@ function init() {
return ret;
}
var sequencer = function sequencer (map, str) {
var sequencer = function sequencer(map, str) {
var exploded = str.split(""), i = 0;
exploded = exploded.map(map);
return exploded.join("");
@ -169,7 +180,7 @@ colors.maps.rainbow = require('./maps/rainbow');
colors.maps.random = require('./maps/random')
for (var map in colors.maps) {
(function(map){
(function (map) {
colors[map] = function (str) {
return sequencer(colors.maps[map], str);
}