Permalink
Browse files

Implement new syntax for cli.color

  • Loading branch information...
1 parent cfbad90 commit 3d6e5b01941e6b1274df8301edb1d0d172e0e533 @xkxx xkxx committed Oct 19, 2011
Showing with 54 additions and 75 deletions.
  1. +54 −75 node-cli.js
View
@@ -15,11 +15,11 @@
var util = require("util");
-
function NodeCli () {
this.bold = true;
this.colors = {
+ default: 39,
grey: 30,
red: 31,
green: 32,
@@ -30,6 +30,7 @@ function NodeCli () {
white: 37
};
this.bgcolors = {
+ default: 49,
gray: 40,
red: 41,
green: 42,
@@ -40,151 +41,129 @@ function NodeCli () {
white: 47,
};
this.textstyles = {
- bold: 1,
- underscore: 4,
- blink: 5,
- inverse: 7,
- conceal: 8,
- nobold: 22,
- nounderline:24,
- noblink: 25,
- noinverse: 27,
- noconceal: 28,
- frame: 51,
- encircle: 52,
- overline: 53,
+ bold: 1,
+ underscore: 4,
+ blink: 5,
+ inverse: 7,
+ conceal: 8,
+ nobold: 22,
+ nounderscore:24,
+ noblink: 25,
+ noinverse: 27,
+ noconceal: 28,
+ frame: 51,
+ encircle: 52,
+ overline: 53,
no_frame_or_circle:54,
nooverline: 55
}
- /**
- * Set colors and styles
- * future: color, bold(boolean), background or: color, background, others
- */
+ //Set colors and styles
+ //Syntax: color, bold(boolean), background or: color, background, others...
this.color = function(color, bold, background) {
- bg = (background && this.bgcolors[background]) ? ';'+this.bgcolors[background] : '';
- //if(typeof bold == 'boolean')
- this.write('\x1B['+(bold ? 1 : 0)+';'+this.colors[color]+bg+'m');
- return(this);
- };
-
- /**
- * Set bckground color only
- */
+ var output = '\x1B[' + this.colors[color];
+ if(typeof bold == 'boolean') {
+ output += (bold ? ';1' : '') + (background? ';'+this.bgcolors[background] : '');
+ }
+ else {
+ output += bold ? ';'+this.bgcolors[bold] : '';
+ for(var i = 2; i < arguments.length; i++) {
+ output += ';' + (typeof arguments[i] == 'string' ? this.textstyles[arguments[i]] : arguments[i] + ';');
+ }
+ }
+ return this.write(output + 'm');
+ };
+
+ //Set bckground color only
this.bgcolor = function(color) {
return this.write('\x1B[' + this.bgcolors[color] + 'm');
};
- /**
- * Set text styles only
- */
+ //Set text styles only
this.style = function(style) {
return this.write('\x1B[' + this.textstyles[style] + 'm');
};
- /**
- * Reset terminal to default attributes (colors, styles)
- */
+ //Reset terminal to default attributes (colors, styles)
this.resetColors = function() {
return this.write('\x1B[0m');
};
- /**
- * Reset terminal to default text color
- */
+ //Reset terminal to default text color
this.resetColor = function() {
return this.write('\x1B[39m');
};
- /**
- * Reset terminal to default background color
- */
+ //Reset terminal to default background color
this.resetBg = function() {
return this.write('\x1B[49m');
};
- /**
- * Reset terminal to all default text styles
- */
+ //Reset terminal to all default text styles
this.resetStyle = function() {
return this.write('\x1B[10;22;23;24;25;27;28;29;54;55m');
};
- /**
- * Output string @ current x/y
- */
+ //Output string @ current x/y
this.write = function(string) {
util.print(string);
return(this);
};
- /**
- * Output string at new line
- */
+ //Output string at new line
this.print = function(string) {
util.puts(string);
return(this);
};
- /**
- * Position the Cursor to x/y
- */
+ //Position the Cursor to x/y
this.move = function(x,y) {
return this.write('\x1B['+x+';'+y+'H');
};
- /**
- * Move the cursor up x rows
- */
+ //Move the cursor up x rows
this.up = function(x) {
return this.write('\x1B['+x+'A');
};
- /**
- * Move the cursor down x rows
- */
+ //Move the cursor down x rows
this.down = function(x) {
return this.write('\x1B['+x+'B');
};
- /**
- * Move the cursor forward x rows
- */
+ //Move the cursor forward x rows
this.fwd = function(x) {
return this.write('\x1B['+x+'C');
};
- /**
- * Move the cursor backwards x columns
- */
+ //Move the cursor backwards x columns
this.back = function(x) {
return this.write('\x1B['+x+'D');
};
- /**
- * Clear the entire screen
- */
+ //Clear the entire screen
this.clear = function(x) {
return this.write('\x1B['+(x ? x : 2)+'J');
};
- /**
- * Clear the current line
- */
+ //Clear the current line
this.clearLine = function(x) {
return this.write('\x1B['+(x ? x : 2)+'K');
};
- /**
- * Clear the next x chars, keep cursor positioned where it started.
- */
+ //Clear the next x chars, keep cursor positioned where it started
this.clearNext = function(x) {
return this.write(new Array(x+1).join(' ')).back(x);
- }
-
+ };
+
+ //Customized commands
+ this.custom = function(cmd) {
+ return this.write('\x1B['+cmd);
+ };
+
}
module.exports = new NodeCli();
//cli.clear().move(38, 5).write('Node.js').down(1).back(7).write('Rocks!');
-module.exports.clear().move(20,20).color('red').write('Node.js').down(1).back(7).color('yellow').write('Rocks!').down(10);
+module.exports.clear().move(20,20).color('red', true).write('Node.js').down(1).back(7).color('yellow', 'default', 'underscore').write('Rocks!').resetStyle().down(10);

0 comments on commit 3d6e5b0

Please sign in to comment.