From 0c4af7249c0ea9e17cc4f8463ea4459048a7d732 Mon Sep 17 00:00:00 2001 From: Preetam Date: Sun, 8 Feb 2015 11:26:45 +1100 Subject: [PATCH] * add more ANSI color codes. * feature: neon - similar to rainbow but user can choose the colors: Eg: "Neon Test".neon('red', 'green', 'blue') * some tests, update examples, and ReadMe.md for neon --- .gitignore | 1 + ReadMe.md | 10 +++++++++ examples/normal-usage.js | 2 ++ examples/safe-string.js | 3 ++- lib/colors.js | 1 + lib/custom/neon.js | 39 ++++++++++++++++++++++++++++++++++++ lib/extendStringPrototype.js | 6 ++++++ lib/styles.js | 7 +++++++ tests/basic-test.js | 1 + tests/safe-test.js | 1 + 10 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 lib/custom/neon.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..723ef36f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/ReadMe.md b/ReadMe.md index beb5b145..95416780 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -22,6 +22,13 @@ - white - gray - grey + - neonRed + - neonGreen + - neonYellow + - neonBlue + - neonMagenta + - neonCyan + - neonWhite ### background colors @@ -54,6 +61,7 @@ - america - trap - random + - neon ## Usage @@ -70,6 +78,7 @@ console.log('i like cake and pies'.underline.red) // outputs red underlined text console.log('inverse the color'.inverse); // inverses the color console.log('OMG Rainbows!'.rainbow); // rainbow console.log('Run the trap'.trap); // Drops the bass +console.log('Show this is neon'.neon('red', 'blue', 'green')) // neon ``` @@ -83,6 +92,7 @@ console.log(colors.red.underline('i like cake and pies')) // outputs red underli console.log(colors.inverse('inverse the color')); // inverses the color console.log(colors.rainbow('OMG Rainbows!')); // rainbow console.log(colors.trap('Run the trap')); // Drops the bass +console.log(colors.neon('Show this is neon', 'red', 'blue', 'green')); // neon ``` diff --git a/examples/normal-usage.js b/examples/normal-usage.js index 2818741e..97c05b1e 100644 --- a/examples/normal-usage.js +++ b/examples/normal-usage.js @@ -12,6 +12,8 @@ console.log("Drop the bass".trap) console.log("DROP THE RAINBOW BASS".trap.rainbow) +console.log("Show this in neon".neon("neonRed", "blue", "neonYellow")) + console.log('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported diff --git a/examples/safe-string.js b/examples/safe-string.js index 111b363a..bb55b2ce 100644 --- a/examples/safe-string.js +++ b/examples/safe-string.js @@ -12,8 +12,9 @@ console.log(colors.trap("Drop the bass")) console.log(colors.rainbow(colors.trap("DROP THE RAINBOW BASS"))); -console.log(colors.bold.italic.underline.red('Chains are also cool.')); // styles not widely supported +console.log(colors.neon("Show this in neon", "neonRed", "blue", "neonYellow")); +console.log(colors.bold.italic.underline.red('Chains are also cool.')); // styles not widely supported console.log(colors.green('So ') + colors.underline('are') + ' ' + colors.inverse('inverse') + colors.yellow.bold(' styles! ')); // styles not widely supported diff --git a/lib/colors.js b/lib/colors.js index 59898de8..647a7b14 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -157,6 +157,7 @@ var sequencer = function sequencer (map, str) { // custom formatter methods colors.trap = require('./custom/trap'); colors.zalgo = require('./custom/zalgo'); +colors.neon = require('./custom/neon'); // maps colors.maps = {}; diff --git a/lib/custom/neon.js b/lib/custom/neon.js new file mode 100644 index 00000000..3a844fe3 --- /dev/null +++ b/lib/custom/neon.js @@ -0,0 +1,39 @@ +var colors = require('../colors'); + +var getValidColors = function(clrs) { + var validColors = []; + clrs.forEach(function(clr) { + if(colors[clr]) { + validColors.push(clr); + } + }); + return validColors; +}; + +module['exports'] = function(str) { + if(!str) { + return ''; + } + + var clrs = Array.prototype.slice.call(arguments, 1); + + if(clrs.length == 0) { + return str.toString(); + } + + var validColors = getValidColors(clrs); + var chars = str.split(''); + var neonChars = []; + + chars.forEach(function(c) { + if(c === ' ') { + neonChars.push(c) + } else { + var colorCode = validColors.shift(); + neonChars.push(colors[colorCode](c)); + validColors.push(colorCode); + } + }); + + return neonChars.join(''); +} \ No newline at end of file diff --git a/lib/extendStringPrototype.js b/lib/extendStringPrototype.js index b6b5b031..6a57f96c 100644 --- a/lib/extendStringPrototype.js +++ b/lib/extendStringPrototype.js @@ -54,6 +54,12 @@ module['exports'] = function () { return colors.america(this); }); + String.prototype.neon = function() { + var args = Array.prototype.slice.call(arguments); + args.unshift(this); + return colors.neon.apply(null, args); + } + // // Iterate through all default styles and colors // diff --git a/lib/styles.js b/lib/styles.js index 067d5907..a596202d 100644 --- a/lib/styles.js +++ b/lib/styles.js @@ -47,6 +47,13 @@ var codes = { white: [37, 39], gray: [90, 39], grey: [90, 39], + neonRed: [91, 39], + neonGreen: [92, 39], + neonYellow: [93, 39], + neonBlue: [94, 39], + neonMagenta: [95, 39], + neonCyan: [96, 39], + neonWhite: [97, 39], bgBlack: [40, 49], bgRed: [41, 49], diff --git a/tests/basic-test.js b/tests/basic-test.js index fda8af4e..48b2dbf3 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -30,6 +30,7 @@ assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m'); assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m'); assert.ok(s.rainbow); +assert.ok(s.neon("red", "blue", "green")) aE(s, 'white', 37); aE(s, 'grey', 90); diff --git a/tests/safe-test.js b/tests/safe-test.js index daad4f96..8ef386cd 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -27,6 +27,7 @@ assert.equal(colors.strikethrough(s), '\x1B[9m' + s + '\x1B[29m'); assert.equal(colors.inverse(s), '\x1B[7m' + s + '\x1B[27m'); assert.ok(colors.rainbow); +assert.ok(colors.neon); aE(s, 'white', 37); aE(s, 'grey', 90);