Comparing version 0.0.4 to 0.0.5
@@ -1,18 +0,56 @@ | ||
var code = { | ||
reset : 0, | ||
// formatting | ||
bold : 1, faint : 2, italic : 3, underline : 4, blink : 5, blinkfast : 5, invert : 7, hide : 8, strike : 9, | ||
// foreground colours | ||
black : 30, red : 31, green : 32, yellow : 33, blue : 34, magenta : 35, cyan : 36, white : 37, | ||
// background colours | ||
bg_black : 40, bg_red : 41, bg_green : 42, bg_yellow : 43, bg_blue : 44, bg_magenta : 45, bg_cyan : 46, bg_white : 47 | ||
}; | ||
var colours = { black : 0, red : 1, green : 2, yellow : 3, blue : 4, magenta : 5, cyan : 6, white : 7 }, | ||
formats = { bold : 1, faint : 2, italic : 3, underline : 4, blink : 5, blinkfast : 5, invert : 7, hide : 8, strike : 9 }, | ||
views = { background : '4', text : '3' }; | ||
module.exports = function ansi_pansi( str, colour ) { | ||
var f = colour.split( /[\s\+]/ ).reduce( function( s, a ) { return ( a in code ) ? s += code[a] + ';' : s; }, '' ); | ||
return ( f.length ? '\033[' + f + 'm' : f ) + str + '\033[0m'; | ||
}; | ||
[[1,31,4], ['string']] | ||
[[],['foo']] | ||
function AnsiPansi( str ) { | ||
this.formats = []; this.output = []; | ||
module.exports.clear = function ansi_pansi_clear() { | ||
process.stdout.write( '\033[2J\033[1;1H' ); | ||
}; | ||
this.reset().concat( str ); | ||
} | ||
!function( P ) { | ||
function has( o, k ) { return Object.prototype.hasOwnProperty.call( o, k ); } | ||
function setColour( v ) { return function() { | ||
this.view || this.text(); | ||
this.current[0].push( this.view + v ); | ||
return this.text(); | ||
}; } | ||
function setFormat( v ) { return function() { this.current[0].push( v ); return this; }; } | ||
function setView( v ) { return function() { this.view = v; return this; }; } | ||
function tostring( s, v ) { | ||
switch ( Object.prototype.toString.call( v ) ) { | ||
case '[object String]' : s += v; break; | ||
case '[object Array]' : s += ( s.length ? ' ' : '' ) + '\033[' + v[0].join( ';' ) + 'm' + v[1].join( ' ' ); break; | ||
} | ||
return s; | ||
} | ||
var k; | ||
for ( k in formats ) !has( formats, k ) || ( P[k] = setFormat( formats[k] ) ); | ||
for ( k in views ) !has( views, k ) || ( P[k] = setView( views[k] ) ); | ||
for ( k in colours ) !has( colours, k ) || ( P[k] = setColour( colours[k] ) ); | ||
P.concat = function( str ) { | ||
this.current || ( this.current = [[],[]] ); | ||
this.current[1].push( str ); | ||
return this; | ||
}; | ||
P.reset = function() { | ||
if ( this.current ) { | ||
this.output.push( this.current ); | ||
delete this.current; | ||
} | ||
this.output.push( '\033[0m' ); | ||
this.current = [[], []]; | ||
return this; | ||
}; | ||
P.toString = function() { return tostring( this.output.reduce( tostring, '' ), this.current ); }; | ||
}( AnsiPansi.prototype = Object.create( null ) ); | ||
module.exports = function ansi_pansi( str ) { return new AnsiPansi( str ); }; | ||
module.exports.clear = function ansi_pansi_clear() { process.stdout.write( '\033[2J\033[1;1H' ); }; |
@@ -19,3 +19,3 @@ { | ||
}, | ||
"version" : "0.0.4" | ||
"version" : "0.0.5" | ||
} |
@@ -17,24 +17,98 @@ # ansi pansi | ||
var ansiformat = require( 'ansi-pansi' ); | ||
var ansi = require( 'ansi-pansi' ), | ||
foo = ansi( 'foo' ); | ||
console.log( ansiformat( 'foo', 'bold blue bg_yellow' ) ); | ||
// prints => 'foo' to the console in blue, bold on a yellow background.\ | ||
console.log( foo.bold().blue().background().yellow().toString() ); | ||
// logs in bold, blue text on a yellow background => foo | ||
ansiformat.clear(); // same as executing – $> clear – from the command line | ||
console.log( foo.reset().concat( '\nbar' ).italic().text().white().background().red().toString() ); | ||
// logs in bold, blue text on a yellow background => foo | ||
// logs in italic, white text on a red background => bar | ||
ansi.clear(); // same as executing – $> clear – from the command line | ||
``` | ||
## formatting and colour options | ||
You can use the below codes concatenated with either a plus `+` or a space ` ` character as shown in the example above. | ||
## API | ||
The following convenience methods are available on the `AnsiPansi` instance returned by the `ansi` method. | ||
### concat( string:String ):this | ||
Concatenated the passed `string` to the current String. Any formatting options already set are still valid. | ||
#### Example: | ||
```javascript | ||
var ansi = require( 'ansi-pansi' ), | ||
foo = ansi( 'foo' ); | ||
foo.red().bold(); // red and bold formatting is applied to 'foo' | ||
foo.concat( 'bar' ); // red and bold formatting is ALSO applied to 'bar' | ||
``` | ||
### reset():this | ||
Resets the formatting on the current instance, so any new Strings which are concatenated to it will not have any previous formatting applied. | ||
#### Example: | ||
```javascript | ||
var ansi = require( 'ansi-pansi' ), | ||
foo = ansi( 'foo' ); | ||
foo.red().bold(); // red and bold formatting is applied to 'foo' | ||
foo.concat( 'bar' ); // red and bold formatting is ALSO applied to 'bar' | ||
foo.reset().concat( 'lorem ipsum' ); // no formatting currently applied to 'lorem ipsum' | ||
``` | ||
### toString():ANSIFormattedString | ||
Returns the ANSI formatted String created. | ||
#### Example: | ||
```javascript | ||
var ansi = require( 'ansi-pansi' ), | ||
foo = ansi( 'foo' ); | ||
foo.red().bold().concat( 'bar' ); | ||
foo.concat( 'lorem ipsum' ).italic().blue(); | ||
foo.toString(); // returns => '\u001b[0m\u001b[1;31mfoo bar\u001b[0m \u001b[3;34mlorem ipsum\u001b[0m\u001b[m' | ||
``` | ||
## API:Formatting | ||
All the options below are methods on the `AnsiPansi` instance returned by calling `ansi`. | ||
**NOTE: ** Not all formatting across all CLIs. | ||
### formatting | ||
`bold`, `faint`, `italic`, `underline`, `blink`, `blinkfast`, `invert`, `hide`, `strike` | ||
### foreground colours | ||
### views | ||
`background`, `text` | ||
**NOTE:** All colours are automaticallu assigned to `text` unless you call `background` first. Once a background colour is set, `text` is called to allow setting the text colour. | ||
#### Example: | ||
``` javascript | ||
var ansi = require( 'ansi-pansi' ), | ||
str = ansi( 'lorem ipsum ' ); // string | ||
str.background().red().white() // format above text to be white on a red background | ||
str.concat( ' dolor sit amet' ).red().background().white() // concatenate text and format it with red text on a white background | ||
``` | ||
### colours | ||
`black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white` | ||
### background colours | ||
`bg_black`, `bg_red`, `bg_green`, `bg_yellow`, `bg_blue`, `bg_magenta`, `bg_cyan`, `bg_white` | ||
**NOTE:** When assigning a colour it will automatically be assigned as the `text` colour **unless** you call `background` first. | ||
@@ -41,0 +115,0 @@ ## License |
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
8181
47
125