Permalink
Browse files

fixed a few "issues" created from the API changed

  • Loading branch information...
1 parent 4e83882 commit 416b951fd3e78f193e7a2232308098457da6816b @constantology committed Mar 2, 2012
Showing with 88 additions and 23 deletions.
  1. +61 −8 README.md
  2. +27 −15 ansi-pansi.js
View
@@ -14,25 +14,78 @@ Basic ansi formatting, foreground and background colours for use with CLIs.
```javascript
- var ansiformat = require( 'ansi-pansi' ),
- foo = ansiformat( 'foo' );
+ var ansi = require( 'ansi-pansi' ),
+ foo = ansi( 'foo' );
console.log( foo.bold().blue().background().yellow().toString() );
// logs in bold, blue text on a yellow background => foo
- console.log( foo.concat( '\nbar' ).italic().text().white().background().red().toString() );
+ 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
- ansiformat.clear(); // same as executing – $> clear – from the command line
+ ansi.clear(); // same as executing – $> clear – from the command line
```
-## formatting and colour options
-All the options below are methods on the instance returned by calling `ansiformat`.
+## 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`
@@ -45,8 +98,8 @@ All the options below are methods on the instance returned by calling `ansiforma
``` javascript
- var ansiformat = require( 'ansi-pansi' ),
- str = ansiformat( 'lorem ipsum ' ); // string
+ 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
View
@@ -2,25 +2,29 @@ var colours = { black : 0, red : 1, green : 2, yellow : 3, blue : 4, mage
formats = { bold : 1, faint : 2, italic : 3, underline : 4, blink : 5, blinkfast : 5, invert : 7, hide : 8, strike : 9 },
views = { background : '4', text : '3' };
+[[1,31,4], ['string']]
+[[],['foo']]
function AnsiPansi( str ) {
- this.current = []; this.output = [];
- this.concat( str );
+ this.formats = []; this.output = [];
+
+ 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.push( this.view + v );
+ this.current[0].push( this.view + v );
return this.text();
}; }
- function setFormat( v ) { return function() { this.current.push( v ); return this; }; }
- function setView( v ) { return function() { this.view = v; return this; }; }
- function update( ansi ) {
- !ansi.current.length || ansi.output.push( '\033[' + ansi.current.join( ';' ) + 'm' );
- ansi.output.push( '' + ( ansi.str || '' ) );
- ansi.reset().str = '';
- return ansi;
+ 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;
@@ -29,13 +33,21 @@ function AnsiPansi( str ) {
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 ) {
- update( this ).current = [];
- this.str = str || '';
+ 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.reset = function() { this.output.push( '\033[0m' ); return this; };
- P.toString = function() { return update( this ).output.join( '' ); };
+ P.toString = function() { return tostring( this.output.reduce( tostring, '' ), this.current ); };
}( AnsiPansi.prototype = Object.create( null ) );

0 comments on commit 416b951

Please sign in to comment.