From aa012aab59f3a41a58a4da97dea781d08ed6f06c Mon Sep 17 00:00:00 2001 From: Vladimir Boretskyi Date: Mon, 23 Sep 2019 02:04:21 +0300 Subject: [PATCH 1/7] Redo weak equality check so we can colorize null in safe mode (#257) --- lib/colors.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/colors.js b/lib/colors.js index 7ca90fa9..0c1089f1 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -105,7 +105,8 @@ function applyStyle() { var args = Array.prototype.slice.call(arguments); var str = args.map(function(arg) { - if (arg !== undefined && arg.constructor === String) { + // Use weak equality check so we can colorize null in safe mode + if (arg != undefined && arg.constructor === String) { return arg; } else { return util.inspect(arg); From 5d9eb90263169521707ce75201ac6011e17d8f9a Mon Sep 17 00:00:00 2001 From: Josh Weinstein Date: Sun, 22 Sep 2019 16:10:31 -0700 Subject: [PATCH 2/7] Fixed: throws non-intuitive error on color.red(null) but not on colors.red(undefined) (#261) --- lib/colors.js | 4 ++-- package-lock.json | 2 +- package.json | 2 +- tests/safe-test.js | 7 +++++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index 0c1089f1..76c6d564 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -105,8 +105,8 @@ function applyStyle() { var args = Array.prototype.slice.call(arguments); var str = args.map(function(arg) { - // Use weak equality check so we can colorize null in safe mode - if (arg != undefined && arg.constructor === String) { + // Use weak equality check so we can colorize null/undefined in safe mode + if (arg != null && arg.constructor === String) { return arg; } else { return util.inspect(arg); diff --git a/package-lock.json b/package-lock.json index 045ee7f8..9986b6ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "colors", - "version": "1.3.3", + "version": "1.3.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index caf84035..30e1a717 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "colors", "description": "get colors in your node.js console", - "version": "1.3.3", + "version": "1.3.4", "author": "Marak Squires", "contributors": [ { diff --git a/tests/safe-test.js b/tests/safe-test.js index fd54acb7..95a21b7e 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -66,3 +66,10 @@ colors.setTheme({custom: ['red', 'italic', 'inverse']}); assert.equal(colors.custom(s), '\x1b[7m' + '\x1b[3m' + '\x1b[31m' + s + '\x1b[39m' + '\x1b[23m' + '\x1b[27m' ); + +// should not throw error on null or undefined values +var undef; +assert.equal(colors.yellow(undef), '\x1b[33mundefined\x1b[39m'); + +// was failing: +assert.equal(colors.red(null), '\x1b[31mnull\x1b[39m'); From 9bfb136eecf6e81f08dc2dfe0d2ea9c89968fa51 Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 22 Sep 2019 16:10:58 -0700 Subject: [PATCH 3/7] more node versions --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index d072a6d4..fb11fc4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: node_js node_js: + - "12" + - "11" - "10" - "9" - "8" From a1407aee041be8427e7a3399c60bc57360bd01d3 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 22 Sep 2019 17:12:39 -0600 Subject: [PATCH 4/7] Document colors.enable() and .disable() (#255) Resolves #251 requesting documentation for the enable/disable functions. This documents them in the readme. My explanation doesn't really fit the tone of the rest of the readme, so I'm open to changing the wording --- README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4bebb6c9..1d784da6 100644 --- a/README.md +++ b/README.md @@ -94,12 +94,27 @@ I prefer the first way. Some people seem to be afraid of extending `String.proto If you are writing good code you will never have an issue with the first approach. If you really don't want to touch `String.prototype`, the second usage will not touch `String` native object. -## Disabling Colors +## Enabling/Disabling Colors -To disable colors you can pass the following arguments in the command line to your application: +The package will auto-detect whether your terminal can use colors and enable/disable accordingly. When colors are disabled, the color functions do nothing. You can override this with a command-line flag: ```bash node myapp.js --no-color +node myapp.js --color=false + +node myapp.js --color +node myapp.js --color=true +node myapp.js --color=always + +FORCE_COLOR=1 node myapp.js +``` + +Or in code: + +```javascript +var colors = require('colors'); +colors.enable(); +colors.disable(); ``` ## Console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data) From b4d964b514037a3f06a37fa84c6fede0f0d41139 Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 22 Sep 2019 16:27:58 -0700 Subject: [PATCH 5/7] Make stylize() work for non-ASCI styles (#155) --- lib/colors.js | 11 ++++++++++- tests/basic-test.js | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/colors.js b/lib/colors.js index 76c6d564..9c7f1d14 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -62,7 +62,16 @@ var stylize = colors.stylize = function stylize(str, style) { return str+''; } - return ansiStyles[style].open + str + ansiStyles[style].close; + var styleMap = ansiStyles[style]; + + // Stylize should work for non-ANSI styles, too + if(!styleMap && style in colors){ + // Style maps like trap operate as functions on strings; + // they don't have properties like open or close. + return colors[style](str); + } + + return styleMap.open + str + styleMap.close; }; var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; diff --git a/tests/basic-test.js b/tests/basic-test.js index 11887b04..11c21c88 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -30,6 +30,12 @@ assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m'); assert.ok(s.rainbow); +assert.equal(colors.stylize("foo", "rainbow"), '\u001b[31mf\u001b[39m\u001b[33mo\u001b[39m\u001b[32mo\u001b[39m'); +assert.ok(colors.stylize(s, "america")); +assert.ok(colors.stylize(s, "zebra")); +assert.ok(colors.stylize(s, "trap")); +assert.ok(colors.stylize(s, "random")); + aE(s, 'white', 37); aE(s, 'grey', 90); aE(s, 'black', 30); From 56de9f0983f68cd0a08c5b76d10a783e4b881716 Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 22 Sep 2019 16:36:30 -0700 Subject: [PATCH 6/7] Add bright/light colors, closes #128 --- README.md | 22 ++++++++++++++++++++++ examples/normal-usage.js | 1 + examples/safe-string.js | 2 ++ lib/maps/random.js | 3 ++- lib/styles.js | 18 ++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- tests/basic-test.js | 13 ++++++++++++- tests/safe-test.js | 12 +++++++++++- 9 files changed, 70 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1d784da6..fabe5589 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,16 @@ Please check out the [roadmap](ROADMAP.md) for upcoming features and releases. - gray - grey +### bright text colors + + - brightRed + - brightGreen + - brightYellow + - brightBlue + - brightMagenta + - brightCyan + - brightWhite + ### background colors - bgBlack @@ -39,6 +49,18 @@ Please check out the [roadmap](ROADMAP.md) for upcoming features and releases. - bgMagenta - bgCyan - bgWhite + - bgGray + - bgGrey + +### bright background colors + + - bgBrightRed + - bgBrightGreen + - bgBrightYellow + - bgBrightBlue + - bgBrightMagenta + - bgBrightCyan + - bgBrightWhite ### styles diff --git a/examples/normal-usage.js b/examples/normal-usage.js index cc8d05ff..822db1cc 100644 --- a/examples/normal-usage.js +++ b/examples/normal-usage.js @@ -29,6 +29,7 @@ console.log('Background color attack!'.black.bgWhite); console.log('Use random styles on everything!'.random); console.log('America, Heck Yeah!'.america); +console.log('Blindingly '.brightCyan + 'bright? '.brightRed + 'Why '.brightYellow + 'not?!'.brightGreen); console.log('Setting themes is useful'); diff --git a/examples/safe-string.js b/examples/safe-string.js index 98994873..5bc0168e 100644 --- a/examples/safe-string.js +++ b/examples/safe-string.js @@ -28,6 +28,8 @@ console.log(colors.black.bgWhite('Background color attack!')); console.log(colors.random('Use random styles on everything!')); console.log(colors.america('America, Heck Yeah!')); +console.log(colors.brightCyan('Blindingly ') + colors.brightRed('bright? ') + colors.brightYellow('Why ') + colors.brightGreen('not?!')); + console.log('Setting themes is useful'); // diff --git a/lib/maps/random.js b/lib/maps/random.js index 6f8f2f8e..3d82a39e 100644 --- a/lib/maps/random.js +++ b/lib/maps/random.js @@ -1,6 +1,7 @@ module['exports'] = function(colors) { var available = ['underline', 'inverse', 'grey', 'yellow', 'red', 'green', - 'blue', 'white', 'cyan', 'magenta']; + 'blue', 'white', 'cyan', 'magenta', 'brightYellow', 'brightRed', + 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan', 'brightMagenta']; return function(letter, i, exploded) { return letter === ' ' ? letter : colors[ diff --git a/lib/styles.js b/lib/styles.js index 02db9acf..011dafd8 100644 --- a/lib/styles.js +++ b/lib/styles.js @@ -48,6 +48,14 @@ var codes = { gray: [90, 39], grey: [90, 39], + brightRed: [91, 39], + brightGreen: [92, 39], + brightYellow: [93, 39], + brightBlue: [94, 39], + brightMagenta: [95, 39], + brightCyan: [96, 39], + brightWhite: [97, 39], + bgBlack: [40, 49], bgRed: [41, 49], bgGreen: [42, 49], @@ -56,6 +64,16 @@ var codes = { bgMagenta: [45, 49], bgCyan: [46, 49], bgWhite: [47, 49], + bgGray: [100, 49], + bgGrey: [100, 49], + + bgBrightRed: [101, 49], + bgBrightGreen: [102, 49], + bgBrightYellow: [103, 49], + bgBrightBlue: [104, 49], + bgBrightMagenta: [105, 49], + bgBrightCyan: [106, 49], + bgBrightWhite: [107, 49], // legacy styles for colors pre v1.0.0 blackBG: [40, 49], diff --git a/package-lock.json b/package-lock.json index 9986b6ae..8570ee68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "colors", - "version": "1.3.4", + "version": "1.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 30e1a717..dbd71ba5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "colors", "description": "get colors in your node.js console", - "version": "1.3.4", + "version": "1.4.0", "author": "Marak Squires", "contributors": [ { diff --git a/tests/basic-test.js b/tests/basic-test.js index 11c21c88..9713e50e 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -16,7 +16,10 @@ function aE(s, color, code) { } var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta', - 'red', 'yellow']; + 'red', 'yellow', 'brightYellow', 'brightRed', + 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan', + 'brightMagenta']; + // eslint-disable-next-line var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse', 'rainbow']); @@ -46,6 +49,14 @@ aE(s, 'magenta', 35); aE(s, 'red', 31); aE(s, 'yellow', 33); +aE(s, 'brightWhite', 97); +aE(s, 'brightBlue', 94); +aE(s, 'brightCyan', 96); +aE(s, 'brightGreen', 92); +aE(s, 'brightMagenta', 95); +aE(s, 'brightRed', 91); +aE(s, 'brightYellow', 93); + assert.equal(s, 'string'); var testStringWithNewLines = s + '\n' + s; diff --git a/tests/safe-test.js b/tests/safe-test.js index 95a21b7e..ce09b029 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -13,7 +13,9 @@ function aE(s, color, code) { } var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta', - 'red', 'yellow']; + 'red', 'yellow', 'brightYellow', 'brightRed', + 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan', + 'brightMagenta']; // eslint-disable-next-line var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse', 'rainbow']); @@ -37,6 +39,14 @@ aE(s, 'magenta', 35); aE(s, 'red', 31); aE(s, 'yellow', 33); +aE(s, 'brightWhite', 97); +aE(s, 'brightBlue', 94); +aE(s, 'brightCyan', 96); +aE(s, 'brightGreen', 92); +aE(s, 'brightMagenta', 95); +aE(s, 'brightRed', 91); +aE(s, 'brightYellow', 93); + assert.equal(s, 'string'); var testStringWithNewLines = s + '\n' + s; From baa0e1c7dc50d868354206b9ea71273e3f05f593 Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 22 Sep 2019 16:45:29 -0700 Subject: [PATCH 7/7] update roadmap --- ROADMAP.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index fe9c4afa..dad20148 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,12 +4,17 @@ Here we describe upcoming and recent releases and the key features/fixes they in ## Currently Planned Releases -### 1.4.0 +### 1.5.0 * Support custom colors -### 1.3.4 +### 1.4.1 * Refactor tests to use a testing library like jest (only affects dev/testing) +### ~~1.4.0 (9/22/19)~~ + * ~~Allow colorizing null/undefined in safe mode (@givehug, @jweinsteincbt)~~ + * ~~Add bright/background colors (ASCI standard) (@vsimonian, @mejenborg)~~ + * ~~Improve docs around enable()/disable() (@mrjacobbloom)~~ + ### ~~1.3.3 (12/9/18)~~ * ~~Remove extraneous swap files~~