From 172098b1f56c9fa3aa4d278e1f1f2349fb75eb0b Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Tue, 3 Apr 2018 13:35:15 +0700 Subject: [PATCH 01/10] Add multiline handling functionality Allows multiline logs to have each line styled properly. --- lib/colors.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index 1fb08b5..dc2e9dd 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -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); } From e020f97ea9708a6dc6c81fee79a638f9c19f91c1 Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Tue, 3 Apr 2018 13:42:53 +0700 Subject: [PATCH 02/10] Fixed test case Test failed because /n was appended even when line count was 1 --- lib/colors.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index dc2e9dd..c0a1835 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -107,7 +107,7 @@ function applyStyle() { var nestedStyles = this._styles; var i = nestedStyles.length; - + var rebuiltString = ''; while (i--) { @@ -116,10 +116,14 @@ function applyStyle() { // 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') - }) - + if (lines == 1) { + rebuiltString += code.open + line.replace(code.closeRe, code.open) + code.close + } else { + lines.forEach(line => { + rebuiltString += code.open + line.replace(code.closeRe, code.open) + code.close + '\n' + }) + } + } return rebuiltString; From 00ee8f1ecd4416a5120c9023466684dc076285fd Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Tue, 3 Apr 2018 13:45:37 +0700 Subject: [PATCH 03/10] Fixed typo --- lib/colors.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/colors.js b/lib/colors.js index c0a1835..77918a9 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -115,7 +115,6 @@ function applyStyle() { // multiline handling functionality var lines = str.split(/[\r\n]+/) - var rebuiltString = ''; if (lines == 1) { rebuiltString += code.open + line.replace(code.closeRe, code.open) + code.close } else { From 01bb5cf845e408a79308e052ca6a5a652aad3e09 Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Mon, 16 Apr 2018 12:02:15 +0700 Subject: [PATCH 04/10] fixed spacing, condensed line replacement call --- lib/colors.js | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index 77918a9..43f83ab 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -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,26 +107,14 @@ function applyStyle() { var nestedStyles = this._styles; var i = nestedStyles.length; - - var rebuiltString = ''; - while (i--) { var code = ansiStyles[nestedStyles[i]]; - - // multiline handling functionality - var lines = str.split(/[\r\n]+/) - if (lines == 1) { - rebuiltString += code.open + line.replace(code.closeRe, code.open) + code.close - } else { - lines.forEach(line => { - rebuiltString += code.open + line.replace(code.closeRe, code.open) + code.close + '\n' - }) - } - + str = code.open + str.replace(code.closeRe, code.open) + code.close; + str = str.replace(new RegExp(/[\r\n]+/, 'g'), code.close + '\n' + code.open); } - return rebuiltString; - + return str; + } colors.setTheme = function (theme) { @@ -138,11 +126,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; @@ -165,7 +153,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(""); @@ -183,11 +171,11 @@ 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); } })(map) } -defineProps(colors, init()); +defineProps(colors, init()); \ No newline at end of file From 5fe02b870b64f7944801a9328f33a39e2c971941 Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Mon, 16 Apr 2018 12:08:00 +0700 Subject: [PATCH 05/10] fixed compatibility issues with node <=5 --- lib/colors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/colors.js b/lib/colors.js index 43f83ab..13b0085 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -110,7 +110,7 @@ function applyStyle() { while (i--) { var code = ansiStyles[nestedStyles[i]]; str = code.open + str.replace(code.closeRe, code.open) + code.close; - str = str.replace(new RegExp(/[\r\n]+/, 'g'), code.close + '\n' + code.open); + str = str.replace(new RegExp(/[\r\n]+/g), code.close + '\n' + code.open); } return str; From c067109a2912833b9c668d43dcb079de6fa7b95a Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 15 Apr 2018 22:18:32 -0700 Subject: [PATCH 06/10] rm blank line --- lib/colors.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index 13b0085..eb27302 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -114,7 +114,6 @@ function applyStyle() { } return str; - } colors.setTheme = function (theme) { @@ -178,4 +177,4 @@ for (var map in colors.maps) { })(map) } -defineProps(colors, init()); \ No newline at end of file +defineProps(colors, init()); From 373be07fab9f71edd44c04b2ade5e4aa74f893c2 Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Mon, 23 Apr 2018 17:10:59 +0700 Subject: [PATCH 07/10] Performance related tweaks --- lib/colors.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/colors.js b/lib/colors.js index 13b0085..7fa952f 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -35,6 +35,7 @@ colors.themes = {}; var ansiStyles = colors.styles = require('./styles'); var defineProps = Object.defineProperties; +var newLineRegex = new RegExp(/[\r\n]+/g); colors.supportsColor = require('./system/supports-colors').supportsColor; @@ -104,13 +105,20 @@ function applyStyle() { return str; } + var newLinesPresent = false; + if(str.indexOf('\n') != -1){ + newLinesPresent = true; + } + var nestedStyles = this._styles; var i = nestedStyles.length; while (i--) { var code = ansiStyles[nestedStyles[i]]; str = code.open + str.replace(code.closeRe, code.open) + code.close; - str = str.replace(new RegExp(/[\r\n]+/g), code.close + '\n' + code.open); + if(newLinesPresent){ + str = str.replace(newLineRegex, code.close + '\n' + code.open); + } } return str; From 0a5a26c5e8d0019091978cf73be1533356035f39 Mon Sep 17 00:00:00 2001 From: DABH Date: Mon, 23 Apr 2018 15:41:19 -0700 Subject: [PATCH 08/10] style nits --- lib/colors.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index 7a5a980..0b39272 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -105,10 +105,7 @@ function applyStyle() { return str; } - var newLinesPresent = false; - if(str.indexOf('\n') != -1){ - newLinesPresent = true; - } + var newLinesPresent = str.indexOf('\n') != -1; var nestedStyles = this._styles; @@ -116,7 +113,7 @@ function applyStyle() { while (i--) { var code = ansiStyles[nestedStyles[i]]; str = code.open + str.replace(code.closeRe, code.open) + code.close; - if(newLinesPresent){ + if (newLinesPresent){ str = str.replace(newLineRegex, code.close + '\n' + code.open); } } From 21448e0e9c25e1aac018539de4871feece616b93 Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Thu, 26 Apr 2018 15:54:22 +0700 Subject: [PATCH 09/10] improved tests to handle newlines --- tests/basic-test.js | 21 +++++++++++++++++---- tests/safe-test.js | 21 +++++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/basic-test.js b/tests/basic-test.js index fda8af4..b22a9e7 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -1,5 +1,5 @@ var assert = require('assert'), - colors = require('../lib/index'); + colors = require('../lib/index'); var s = 'string'; @@ -43,8 +43,21 @@ aE(s, 'yellow', 33); assert.equal(s, 'string'); -colors.setTheme({error:'red'}); +var testStringWithNewLines = s + ` +` + s; -assert.equal(typeof("astring".red),'string'); -assert.equal(typeof("astring".error),'string'); +// single style +assert.equal(testStringWithNewLines.red, '\x1b[31m' + s + '\n' + s + '\x1b[39m'); +var testStringWithNewLinesStyled = s.underline + ` +` + s.bold; + +// nested styles +assert.equal(testStringWithNewLinesStyled.red, '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\n' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m'); + +colors.setTheme({ error: 'red' }); + +assert.equal(typeof ("astring".red), 'string'); +assert.equal(typeof ("astring".error), 'string'); + +assert.equal(s, 'string'); diff --git a/tests/safe-test.js b/tests/safe-test.js index daad4f9..7d13129 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -1,5 +1,5 @@ var assert = require('assert'), - colors = require('../safe'); + colors = require('../safe'); var s = 'string'; @@ -39,7 +39,20 @@ aE(s, 'red', 31); aE(s, 'yellow', 33); assert.equal(s, 'string'); -colors.setTheme({error:'red'}); -assert.equal(typeof(colors.red("astring")), 'string'); -assert.equal(typeof(colors.error("astring")), 'string'); \ No newline at end of file +var testStringWithNewLines = s + ` +` + s; + +// single style +assert.equal(colors.red(testStringWithNewLines), '\x1b[31m' + s + '\x1b[39m' + '\n' + '\x1b[31m' + s + '\x1b[39m'); + +var testStringWithNewLinesStyled = colors.underline(s) + ` +` + colors.bold(s); + +// nested styles +assert.equal(colors.red(testStringWithNewLinesStyled), '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\x1b[39m' + '\n' + '\x1b[31m' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m'); + +colors.setTheme({ error: 'red' }); + +assert.equal(typeof (colors.red("astring")), 'string'); +assert.equal(typeof (colors.error("astring")), 'string'); \ No newline at end of file From 118cb0a9eca0cb81fd999602ccb5fe7cc9f816ca Mon Sep 17 00:00:00 2001 From: Josh Pike Date: Thu, 26 Apr 2018 15:59:18 +0700 Subject: [PATCH 10/10] used older syntax for tests for old node versions --- tests/basic-test.js | 6 ++---- tests/safe-test.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/basic-test.js b/tests/basic-test.js index b22a9e7..4924be1 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -43,14 +43,12 @@ aE(s, 'yellow', 33); assert.equal(s, 'string'); -var testStringWithNewLines = s + ` -` + s; +var testStringWithNewLines = s + '\n' + s; // single style assert.equal(testStringWithNewLines.red, '\x1b[31m' + s + '\n' + s + '\x1b[39m'); -var testStringWithNewLinesStyled = s.underline + ` -` + s.bold; +var testStringWithNewLinesStyled = s.underline + '\n' + s.bold; // nested styles assert.equal(testStringWithNewLinesStyled.red, '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\n' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m'); diff --git a/tests/safe-test.js b/tests/safe-test.js index 7d13129..1d8a5aa 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -40,14 +40,12 @@ aE(s, 'yellow', 33); assert.equal(s, 'string'); -var testStringWithNewLines = s + ` -` + s; +var testStringWithNewLines = s + '\n' + s; // single style assert.equal(colors.red(testStringWithNewLines), '\x1b[31m' + s + '\x1b[39m' + '\n' + '\x1b[31m' + s + '\x1b[39m'); -var testStringWithNewLinesStyled = colors.underline(s) + ` -` + colors.bold(s); +var testStringWithNewLinesStyled = colors.underline(s) + '\n' + colors.bold(s); // nested styles assert.equal(colors.red(testStringWithNewLinesStyled), '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\x1b[39m' + '\n' + '\x1b[31m' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m');