ansi-rainbow
Advanced tools
Comparing version 0.0.2 to 0.0.3
78
index.js
@@ -6,3 +6,4 @@ var ansi = require('ansi-styles'); | ||
*/ | ||
var colors = [ | ||
var Pony = function() { | ||
this._colors = [ | ||
'red', | ||
@@ -15,32 +16,61 @@ 'green', | ||
] | ||
, colors_num = colors.length - 1 | ||
, next = 0, prev = 0 | ||
this._colors_num = this._colors.length - 1 | ||
this._next = 0 | ||
this._prev = 0 | ||
var nextColor = function(s) { | ||
if(s != ' ') { | ||
if(prev == colors_num) { | ||
prev = 0, next = 1 | ||
s = ansi[colors[0]].open + s + ansi[colors[0]].close | ||
} else { | ||
s = ansi[colors[next]].open + s + ansi[colors[next]].close | ||
next++ | ||
prev++ | ||
return this | ||
} | ||
Pony.prototype = { | ||
bgify: function(color) { | ||
return 'bg'+color.charAt(0).toUpperCase()+color.slice(1) | ||
}, | ||
//wrapper to pass bg | ||
ponyfy: function(bg) { | ||
var self = this | ||
bg = bg ? bg : false | ||
return function() { | ||
return self.output([].slice.call(arguments).join(' '), bg) | ||
} | ||
}, | ||
nextColor: function(s, bg) { | ||
if(s != ' ') { | ||
var color | ||
if(this._prev == this._colors_num) { | ||
this._prev = 0, this._next = 1 | ||
color = bg ? this.bgify(this._colors[0]) : this._colors[0] | ||
s = ansi[color].open + s + ansi[color].close | ||
} else { | ||
color = bg ? this.bgify(this._colors[this._next]) : this._colors[this._next] | ||
s = ansi[color].open + s + ansi[color].close | ||
this._next++ | ||
this._prev++ | ||
} | ||
} | ||
} | ||
return s | ||
} | ||
return s | ||
}, | ||
output: function(input, bg) { | ||
var l = input.length, output = '', i = 0 | ||
module.exports = function () { | ||
for (i; i < l; i++) { | ||
output += this.nextColor(input.charAt(i), bg) | ||
} | ||
var input = [].slice.call(arguments).join(' ') | ||
, l = input.length, output = '' | ||
//reset state | ||
this._next = 0, this._prev = 0 | ||
for (var i = 0; i < l; i++) { | ||
output += nextColor(input.charAt(i)) | ||
return output | ||
} | ||
next = 0, prev = 0 | ||
} | ||
return output | ||
} | ||
var pony = new Pony() | ||
module.exports = pony.ponyfy() | ||
module.exports.bg = pony.ponyfy(true) |
{ | ||
"name": "ansi-rainbow", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Ainsi rainbowify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,1 +9,3 @@ | ||
console.log(require('./')('First', 'Second', 'Third')) | ||
console.log(require('./').bg("I a background in the pony world")) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3975
67