ansi-rainbow
Advanced tools
Comparing version 1.0.0 to 1.0.1
133
index.js
@@ -1,7 +0,14 @@ | ||
var ansi = require('ansi-styles') | ||
'use strict' | ||
const ansi = require('ansi-styles') | ||
const options = { | ||
'color_space': {alias: ['colorSpace']}, | ||
'space_color': {alias: ['spaceColor']}, | ||
'gap': {} | ||
} | ||
/** | ||
* black, white and gray (buggy?) are not included | ||
*/ | ||
var Pony = function() { | ||
const Pony = function() { | ||
this._colors = [ | ||
@@ -22,3 +29,3 @@ 'red', | ||
for(var i = 0; i < this._colors_num; i++) { | ||
for (var i = 0; i < this._colors_num; i++) { | ||
this._backgrounds[i] = 'bg'+this._colors[i].charAt(0).toUpperCase()+this._colors[i].slice(1) | ||
@@ -35,4 +42,2 @@ } | ||
var self = this | ||
this.wrapper = { | ||
@@ -43,14 +48,24 @@ bg: this.ponyfy(true), | ||
skip: this.addorskip('skip'), | ||
options: function(opts) { | ||
for(var i in opts) | ||
self.options[i] = opts[i] | ||
return self.wrapper | ||
options: (opts) => { | ||
for (let i in options) { | ||
if (opts[i]) { | ||
this.options[i] = opts[i] | ||
continue | ||
} | ||
for (let j in opts) { | ||
if (options[i].alias && ~options[i].alias.indexOf(j)) { | ||
this.options[i] = opts[j] | ||
break | ||
} | ||
} | ||
} | ||
return this.wrapper | ||
}, | ||
colors: function() { | ||
return self.colors.call(self) | ||
colors: () => this.colors(), | ||
reset: () => { | ||
this._skip = ['black', 'white', 'bgBlack', 'bgWhite'] | ||
return this.wrapper | ||
}, | ||
reset: function() { | ||
self._skip = ['black', 'white', 'bgBlack', 'bgWhite'] | ||
return self.wrapper | ||
}, | ||
_colors: this._colors, | ||
@@ -67,27 +82,27 @@ _backgrounds: this._backgrounds | ||
var index = this._colors.indexOf(color) | ||
let index = this._colors.indexOf(color) | ||
if(~index) { | ||
if (~index) { | ||
this._colors.splice(index, 1) | ||
this._colors.unshift(color) | ||
this._colors.unshift(color) | ||
} | ||
index = this._backgrounds.indexOf(color) | ||
if(~index) { | ||
if (~index) { | ||
this._backgrounds.splice(index, 1) | ||
this._backgrounds.unshift(color) | ||
this._backgrounds.unshift(color) | ||
} | ||
return this | ||
}, | ||
colors: function(bg) { | ||
var res = [], | ||
items = bg ? this._backgrounds : this._colors, | ||
l = items.length | ||
let res = [] | ||
let items = bg ? this._backgrounds : this._colors | ||
let l = items.length | ||
for(var i = 0; i < l; i++) { | ||
if(this._skip.indexOf(items[i]) === -1) | ||
for (let i = 0; i < l; i++) { | ||
if (this._skip.indexOf(items[i]) === -1) | ||
res.push(items[i]) | ||
} | ||
return res; | ||
@@ -99,31 +114,28 @@ }, | ||
addorskip: function(which, wrapper) { | ||
var self = this | ||
return function() { | ||
colors = [].slice.call(arguments) | ||
return (...colors) => { | ||
colors = colors.length == 1 ? colors[0] : colors | ||
colors = 'string' == typeof colors ? colors.split(' ') : colors | ||
colors = colors instanceof Array ? colors : [colors], l = colors.length | ||
colors = colors instanceof Array ? colors : [colors] | ||
let l = colors.length | ||
while(l--) { | ||
if(self.allowed(colors[l])) { | ||
index = self._skip.indexOf(colors[l]) | ||
if (this.allowed(colors[l])) { | ||
let index = this._skip.indexOf(colors[l]) | ||
if(index === -1 && which === 'skip') { | ||
self._skip_num++ | ||
self._skip[self._skip_num] = colors[l] | ||
if (index === -1 && which === 'skip') { | ||
this._skip_num++ | ||
this._skip[this._skip_num] = colors[l] | ||
} | ||
else if(~index && which === 'add') { | ||
self.top(colors[l]) | ||
self._skip.splice(index, 1) | ||
self._skip_num-- | ||
else if (~index && which === 'add') { | ||
this.top(colors[l]) | ||
this._skip.splice(index, 1) | ||
this._skip_num-- | ||
} | ||
} else { | ||
//console.log(colors[l]) | ||
throw new Error('Color '+ colors[l] +' is not recognized') | ||
} | ||
} | ||
return self.wrapper | ||
return this.wrapper | ||
} | ||
@@ -133,12 +145,12 @@ }, | ||
ponyfy: function(bg) { | ||
var self = this | ||
bg = bg ? bg : false | ||
return function() { | ||
return self.output([].slice.call(arguments).join(' '), self.colors(bg)) | ||
return (...args) => { | ||
return this.output(args.join(' '), this.colors(bg)) | ||
} | ||
}, | ||
nextColor: function(s, colors) { | ||
var l = colors.length - 1 | ||
let l = colors.length - 1 | ||
if(this._prev >= l) { | ||
if (this._prev >= l) { | ||
this._prev = 0, this._next = 1 | ||
@@ -157,9 +169,9 @@ | ||
return s | ||
}, | ||
}, | ||
currentColor: function(s, colors) { | ||
var l = colors.length - 1 | ||
let l = colors.length - 1 | ||
this._current_gap++ | ||
if(this._prev >= l) { | ||
if (this._prev >= l) { | ||
return ansi[colors[0]].open + s + ansi[colors[0]].close | ||
@@ -172,3 +184,3 @@ } else { | ||
colorSpace: function(s, color) { | ||
if(!this.allowed(color)) | ||
if (!this.allowed(color)) | ||
throw new Error('Color '+ colors[l] +' is not recognized') | ||
@@ -179,17 +191,20 @@ | ||
output: function(input, colors) { | ||
var l = input.length, output = '', i = 0, gap = this.options.gap, s | ||
let l = input.length | ||
let output = '' | ||
let i = 0 | ||
let gap = this.options.gap | ||
this._current_gap = 1 | ||
for (i; i < l; i++) { | ||
this._current_gap = this._current_gap > gap ? 1 : this._current_gap | ||
s = input.charAt(i) | ||
let s = input.charAt(i) | ||
if(s == '\n') { | ||
if (s == '\n') { | ||
output += s | ||
} else if(s == ' ' && !this.options.color_space) { | ||
} else if (s == ' ' && !this.options.color_space) { | ||
output += s | ||
} else { | ||
if(s == ' ' && this.options.space_color != null) | ||
if (s == ' ' && this.options.space_color != null) | ||
output += this.colorSpace(s, this.options.space_color) | ||
@@ -196,0 +211,0 @@ else |
{ | ||
"name": "ansi-rainbow", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "One of the most advanced ansi string rainbow stylizer tool", | ||
@@ -16,2 +16,5 @@ "main": "index.js", | ||
], | ||
"engines": { | ||
"node": ">6.0" | ||
}, | ||
"author": "soyuka", | ||
@@ -18,0 +21,0 @@ "license": "WTFPL", |
ansi-rainbow | ||
============= | ||
Rainbowify string with https://github.com/sindresorhus/ansi-styles | ||
Rainbowify string with https://github.com/sindresorhus/ansi-styles | ||
![rainicorn](http://www.zupmage.eu/i/W8xC5EsoJb.jpg) | ||
@@ -61,8 +61,11 @@ # Install | ||
- r(string) rainbowify | ||
- bg(string) background-rainbowify | ||
- add(colors) do not skip colors | ||
- skip(colors) skip colors | ||
- reset() reset colors (skips ['black', 'white', 'bgBlack', 'bgWhite']) | ||
- options({}) set options - options are (bool) color_space, (int) gap | ||
- r(string) rainbowify | ||
- bg(string) background-rainbowify | ||
- add(colors) do not skip colors | ||
- skip(colors) skip colors | ||
- reset() reset colors (skips ['black', 'white', 'bgBlack', 'bgWhite']) | ||
- options({}) set options | ||
- (bool) .color_space or .colorSpace - whether to color spaces | ||
- (string) .space_color or .spaceColor - space color string | ||
- (int) .gap - the gap size between a color change | ||
@@ -69,0 +72,0 @@ # TODO |
124
test/test.js
var rainbow = require('../'), expect = require('chai').expect, colors, chalk = require('chalk') | ||
describe('PONY', function() { | ||
it('you really thing I\'ll do visual tests programmaticaly?', function() { | ||
console.log(rainbow.r("I am in the pony world")) | ||
console.log("space should not be colored") | ||
console.log(rainbow.r(' t')) | ||
console.log("long text") | ||
console.log(rainbow.r('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')) | ||
console.log("should join arguments") | ||
console.log(rainbow.r('First', 'Second', 'Third')) | ||
console.log(rainbow.bg("I am a background in the pony world")) | ||
colors = rainbow.colors() | ||
}) | ||
it('should add black color', function() { | ||
rainbow.add('black') | ||
expect(rainbow.colors()).to.include('black') | ||
console.log(rainbow.r('A pony shits sequins')) | ||
}) | ||
it('you really think I\'ll do visual tests programmaticaly?', function() { | ||
console.log(rainbow.r("I am in the pony world")) | ||
console.log("space should not be colored") | ||
console.log(rainbow.r(' t')) | ||
console.log("long text") | ||
console.log(rainbow.r('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')) | ||
console.log("should join arguments") | ||
console.log(rainbow.r('First', 'Second', 'Third')) | ||
console.log(rainbow.bg("I am a background in the pony world")) | ||
colors = rainbow.colors() | ||
}) | ||
it('should add all colors', function() { | ||
expect(rainbow.add(rainbow._colors).colors()).to.eql(rainbow._colors) | ||
}) | ||
it('should add black color', function() { | ||
rainbow.add('black') | ||
expect(rainbow.colors()).to.include('black') | ||
console.log(rainbow.r('A pony shits sequins')) | ||
}) | ||
it('should skip red color', function() { | ||
rainbow.skip('red') | ||
expect(rainbow.colors()).not.to.include('red') | ||
console.log(rainbow.r('A pony shits sequins')) | ||
}) | ||
it('should add all colors', function() { | ||
expect(rainbow.add(rainbow._colors).colors()).to.eql(rainbow._colors) | ||
}) | ||
it('should remove all colors instead of black and white ☯', function() { | ||
rainbow.skip(rainbow.colors()).add(['black', 'white']) | ||
expect(rainbow.colors()).not.to.equal(['black', 'white']) | ||
console.log(rainbow.r('Does a pony even shit?')) | ||
}) | ||
it('should skip red color', function() { | ||
rainbow.skip('red') | ||
expect(rainbow.colors()).not.to.include('red') | ||
console.log(rainbow.r('A pony shits sequins')) | ||
}) | ||
it('should reset colors', function() { | ||
rainbow.reset() | ||
expect(rainbow.colors()).to.have.members(colors) | ||
colors = rainbow.colors() | ||
console.log(rainbow.r('My sweet colors (:')) | ||
expect(rainbow.add(rainbow._colors).skip('black white').colors()).to.have.members(colors) | ||
}) | ||
it('should remove all colors instead of black and white ☯', function() { | ||
rainbow.skip(rainbow.colors()).add(['black', 'white']) | ||
expect(rainbow.colors()).not.to.equal(['black', 'white']) | ||
console.log(rainbow.r('Does a pony even shit?')) | ||
}) | ||
it('should keep background colors order', function() { | ||
rainbow.skip(rainbow._backgrounds).add('bgGreen bgYellow bgRed') | ||
console.log(chalk.bold.black(rainbow.bg('Chuck norris is born in the pony world'))) | ||
}) | ||
it('should reset colors', function() { | ||
rainbow.reset() | ||
expect(rainbow.colors()).to.have.members(colors) | ||
colors = rainbow.colors() | ||
console.log(rainbow.r('My sweet colors (:')) | ||
expect(rainbow.add(rainbow._colors).skip('black white').colors()).to.have.members(colors) | ||
}) | ||
it("should color spaces with 3 gap - it's a flag \\o/", function() { | ||
rainbow.options({color_space: true, gap: 3}) | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.bg(' ')) | ||
}) | ||
it('should keep background colors order', function() { | ||
rainbow.skip(rainbow._backgrounds).add('bgGreen bgYellow bgRed') | ||
console.log(chalk.bold.black(rainbow.bg('Chuck norris is born in the pony world'))) | ||
}) | ||
it('should color characters with a 3 gap', function() { | ||
rainbow.reset().options({color_space: false}) | ||
it("should color spaces with 3 gap - it's a flag \\o/", function() { | ||
rainbow.options({color_space: true, gap: 3}) | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.bg(' ')) | ||
}) | ||
console.log(rainbow.r(' testtest test')) | ||
console.log(rainbow.r('Some three caracters gap')) | ||
rainbow.options({color_space: true}).reset().skip(rainbow._backgrounds).add('bgBlue bgMagenta bgCyan') | ||
it('should color characters with a 3 gap', function() { | ||
rainbow.reset().options({color_space: false}) | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.bg('123456789')) | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.r(' testtest test')) | ||
console.log(rainbow.r('Some three caracters gap')) | ||
}) | ||
rainbow.options({color_space: true}).reset().skip(rainbow._backgrounds).add('bgBlue bgMagenta bgCyan') | ||
it('should color spaces with a blue bg', function() { | ||
rainbow.reset().options({gap: 1, color_space: true, space_color: 'bgBlue'}).skip('bgBlue') | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.bg('123456789')) | ||
console.log(rainbow.bg(' ')) | ||
console.log(rainbow.bg('text with some space')) | ||
}) | ||
}) | ||
it('should color spaces with a blue bg', function() { | ||
rainbow.reset().options({gap: 1, color_space: true, space_color: 'bgBlue'}).skip('bgBlue') | ||
console.log(rainbow.bg('text with some space')) | ||
}) | ||
}) | ||
it('should use options alias #1', function() { | ||
rainbow.reset().options({gap: 1, colorSpace: true, spaceColor: 'bgBlue'}).skip('bgBlue') | ||
console.log(rainbow.bg('text with some space')) | ||
}) | ||
}) |
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
12288
250
74