Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ansi-rainbow

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ansi-rainbow - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

199

index.js

@@ -1,26 +0,126 @@

var ansi = require('ansi-styles');
var ansi = require('ansi-styles')
/**
* black, white and gray are not included
* black, white and gray (buggy?) are not included
*/
var Pony = function() {
this._colors = [
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
]
this._colors_num = this._colors.length - 1
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'black'
]
this._colors_num = this._colors.length
this._backgrounds = []
for(var i = 0; i < this._colors_num; i++) {
this._backgrounds[i] = 'bg'+this._colors[i].charAt(0).toUpperCase()+this._colors[i].slice(1)
}
this._skip = ['black', 'white', 'bgBlack', 'bgWhite']
this._skip_num = this._skip.length - 1
this._next = 0
this._prev = 0
this._prev = -1
return this
this.options = {color_space: false, gap: 1}
var self = this
this.wrapper = {
bg: this.ponyfy(true),
r: this.ponyfy(),
add: this.addorskip('add'),
skip: this.addorskip('skip'),
options: function(opts) {
for(var i in opts)
self.options[i] = opts[i]
return self.wrapper
},
colors: function() {
return self.colors.call(self)
},
reset: function() {
self._skip = ['black', 'white', 'bgBlack', 'bgWhite']
return self.wrapper
},
_colors: this._colors,
_backgrounds: this._backgrounds
}
return this.wrapper
}
Pony.prototype = {
bgify: function(color) {
return 'bg'+color.charAt(0).toUpperCase()+color.slice(1)
},
top: function(color) {
var index = this._colors.indexOf(color)
if(~index) {
this._colors.splice(index, 1)
this._colors.unshift(color)
}
index = this._backgrounds.indexOf(color)
if(~index) {
this._backgrounds.splice(index, 1)
this._backgrounds.unshift(color)
}
return this
},
colors: function(bg) {
var res = [],
items = bg ? this._backgrounds : this._colors,
l = items.length
for(var i = 0; i < l; i++) {
if(this._skip.indexOf(items[i]) === -1)
res.push(items[i])
}
return res;
},
allowed: function(color) {
return this._colors.indexOf(color) !== -1 || this._backgrounds.indexOf(color) !== -1
},
addorskip: function(which, wrapper) {
var self = this
return function() {
colors = [].slice.call(arguments)
colors = colors.length == 1 ? colors[0] : colors
colors = 'string' == typeof colors ? colors.split(' ') : colors
colors = colors instanceof Array ? colors : [colors], l = colors.length
while(l--) {
if(self.allowed(colors[l])) {
index = self._skip.indexOf(colors[l])
if(index === -1 && which === 'skip') {
self._skip_num++
self._skip[self._skip_num] = colors[l]
}
else if(~index && which === 'add') {
self.top(colors[l])
self._skip.splice(index, 1)
self._skip_num--
}
} else {
//console.log(colors[l])
throw new Error('Color '+ colors[l] +' is not recognized')
}
}
return self.wrapper
}
},
//wrapper to pass bg

@@ -31,37 +131,57 @@ ponyfy: function(bg) {

return function() {
return self.output([].slice.call(arguments).join(' '), bg)
return self.output([].slice.call(arguments).join(' '), self.colors(bg))
}
},
nextColor: function(s, bg) {
if(s != ' ') {
var color
nextColor: function(s, colors) {
//not a space
if(!this.options.color_space && s == ' ') {
return s
}
if(this._prev == this._colors_num) {
this._prev = 0, this._next = 1
var l = colors.length - 1
color = bg ? this.bgify(this._colors[0]) : this._colors[0]
if(this._prev >= l) {
this._prev = 0, this._next = 1
s = ansi[color].open + s + ansi[color].close
} else {
s = ansi[colors[0]].open + s + ansi[colors[0]].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++
}
s = ansi[colors[this._next]].open + s + ansi[colors[this._next]].close
this._next++
this._prev++
}
this._current_gap++
return s
},
output: function(input, bg) {
var l = input.length, output = '', i = 0
},
currentColor: function(s, colors) {
var l = colors.length - 1
for (i; i < l; i++) {
output += this.nextColor(input.charAt(i), bg)
if(!this.options.color_space && s == ' ') {
return s
}
this._current_gap++
if(this._prev >= l) {
return ansi[colors[0]].open + s + ansi[colors[0]].close
} else {
return ansi[colors[this._next]].open + s + ansi[colors[this._next]].close
}
},
output: function(input, colors) {
var l = input.length, output = '', i = 0, gap = this.options.gap, s
this._current_gap = 1
for (i; i < l; i++) {
this._current_gap = this._current_gap > gap ? 1 : this._current_gap
output += this._current_gap == gap ? this.nextColor(input.charAt(i), colors) : this.currentColor(input.charAt(i), colors)
}
//reset state
this._next = 0, this._prev = 0
this._next = 0, this._prev = -1

@@ -72,5 +192,2 @@ return output

var pony = new Pony()
module.exports = pony.ponyfy()
module.exports.bg = pony.ponyfy(true)
module.exports = new Pony()
{
"name": "ansi-rainbow",
"version": "0.0.3",
"version": "0.0.4",
"description": "Ainsi rainbowify",

@@ -8,3 +8,3 @@ "main": "index.js",

"scripts": {
"test": "node test.js"
"test": "mocha test.js --reporter spec"
},

@@ -21,3 +21,8 @@ "keywords": [

"ansi-styles": "~1.0.0"
},
"devDependencies": {
"chai": "~1.9.1",
"mocha": "~1.18.2",
"chalk": "~0.4.0"
}
}

@@ -10,3 +10,3 @@ ansi-rainbow

# Usage
# Basic example

@@ -17,8 +17,56 @@ ```javascript

console.log(rainbow("I am in the pony world"))
console.log(rainbow.r("I am in the pony world"))
//or
console.log(rainbow("I", "am", "in", "the", "pony", "world"))
console.log(rainbow.r("I", "am", "in", "the", "pony", "world"))
//what about background rainbows?
console.log(rainbow.bg("I am a background in the pony world"))
```
![pony pony](http://www.zupmage.eu/i/hLhbow7WHN.png)
That outputs:
![pony pony](https://github.com/soyuka/ansi-rainbow/blob/screen/ksh1.png)
# Features
Rainbow is skipping black and white colors (did you ever seen these on a rainbow?). But, we could imagine that in a world where Pony's are flying, black could be part of a rainbow so:
```javascript
rainbow.add('black white') //adds black and white
```
Let's write all features together to print a rainbow flag \o/:
```javascript
rainbow
.add('black', 'white')
.reset() //we do not want black and white anymore - note it does not reset options only colors
.skip(rainbow._backgrounds) //skips backgrounds
.add('bgBlue bgMagenta bgCyan')
.options({color_space: true, gap: 3}) //this will force color spaces and change color every 3 characters
console.log(rainbow.bg(' '))
console.log(rainbow.bg('123456789'))
console.log(rainbow.bg(' '))
```
That outputs:
![pony pony](https://github.com/soyuka/ansi-rainbow/blob/screen/ksh2.png)
# API
- 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
# TODO
What about everything that is not a string?

@@ -0,10 +1,72 @@

var rainbow = require('./'), expect = require('chai').expect, colors, chalk = require('chalk')
console.log(require('./')("I am in the pony world"))
console.log("space should not be colored")
console.log(require('./')(' t'))
console.log("long text")
console.log(require('./')('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(require('./')('First', 'Second', 'Third'))
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()
})
console.log(require('./').bg("I a background in the pony world"))
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 add all colors', function() {
expect(rainbow.add(rainbow._colors).colors()).to.eql(rainbow._colors)
})
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 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 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 skip 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 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 color characters with a 3 gap', function() {
rainbow.reset().options({color_space: false})
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')
console.log(rainbow.bg(' '))
console.log(rainbow.bg('123456789'))
console.log(rainbow.bg(' '))
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc