style
Version:
color in your node.js console, without monkeypatching
171 lines (151 loc) • 4.47 kB
JavaScript
/*
style.js
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires, Dominic Tarr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
this is based on colors.js by Alexis Sellier (cloudhead) & Marak Squires
*/
exports = module.exports = style
exports.error = require('./error')
function style(string){
return new Styled(string)
}
function unstyled(string){
var styled = new Styled(string)
styled.enable = false
return styled
}
unstyled.enable = style.enable = function (on){
return on ? style : unstyled
}
exports.stylize = stylize
function stylize(str, style) {
if(style == 'rainbow'){
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = str.split("");
var i=0;
exploded = exploded.map(function(letter) {
if (letter==" ") {
return letter;
}
else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
}
});
return exploded.join("");
}
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
};
function Styled (_string){
var string = _string
, _lpad = 0
, _rpad = 0
, _rc = ' '
, _lc = ' '
this.styles = []
this.enable = true
this.__defineGetter__('length',function (){
return _lpad + (destyle("" + string).length) + _rpad
})
this.__defineSetter__('_string',function (s){
return string = s
})
this.__defineGetter__('_string',function (s){
return string
})
this.__defineGetter__('to_s',function (){
return this.toString()
})
this.__defineGetter__('styler',function (){
var self = this
return function (x,off){
var _string = string
, _enable = self.enable
self.enable = !off
string = x
var toReturn = self.toString()
string = _string
this.enable = _enable
return toReturn
}
})
this.toString = toString
function pad(l,c){
var pad = ''
, _c = c
if(_c instanceof Styled) // make pad aware of style, and not duplicate the characters too much!
c = _c._string
for(var i = 0; i < l; i ++) { pad += c }
if(_c instanceof Styled){
_c._string = pad
return _c
}
return pad
}
function toString(){
var newString = string
if(this.enable)
this.styles.forEach(function (style){
newString = stylize(newString,style)
})
return pad(_lpad,_lc) + newString + pad(_rpad,_rc)
}
this.lpad = lpad
this.rpad = rpad
function lpad (pad,c){
_lpad = this.length < pad ? _lpad = pad - this.length : 0
_lc = c || _lc
return this
}
function rpad (pad,c){
_rpad = this.length < pad ? _rpad = pad - this.length : 0
_rc = c || _rc
return this
}
}
var styles = {
//styles
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
//grayscale
'white' : [37, 39],
'grey' : [90, 39],
'black' : [90, 39],
//colors
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39],
};
exports.styles = Object.keys(styles).concat(['rainbow'])
exports.styles.forEach(function (style) {
Object.defineProperty(Styled.prototype, style, {
get: function () {
this.styles.push(style)
return this
}
});
});
var code = /\u001b\[\d+m/g
exports.destyle = destyle
function destyle (s){
return ("" + s).replace(code,'')
}