Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Update Node-Cli for node.js 0.4.* #1

Merged
merged 17 commits into from
Commits on Oct 17, 2011
  1. @xkxx
  2. @xkxx

    Add my name on it:)

    xkxx authored
  3. @xkxx

    Update README

    xkxx authored
  4. @xkxx

    Nothing special

    xkxx authored
Commits on Oct 18, 2011
  1. @xkxx

    Update node-cli.js

    xkxx authored
  2. @xkxx

    Update node-cli.js

    xkxx authored
  3. @xkxx

    major update: add this.textstyles; fix this.resetBg; add resetColors;…

    xkxx authored
    … add resetStyle; use module.exports for exporting
  4. @xkxx

    Update README

    xkxx authored
Commits on Oct 19, 2011
  1. @xkxx
  2. @xkxx

    Switch to cli.custom

    xkxx authored
  3. @xkxx

    Update README

    xkxx authored
Commits on Oct 20, 2011
  1. @xkxx

    Update README

    xkxx authored
Commits on Oct 21, 2011
  1. @xkxx

    set up md

    xkxx authored
  2. @xkxx

    update readme

    xkxx authored
  3. @xkxx

    add docs

    xkxx authored
  4. @xkxx

    add docs

    xkxx authored
  5. @xkxx

    add docs

    xkxx authored
This page is out of date. Refresh to see the latest.
Showing with 207 additions and 95 deletions.
  1. +0 −23 README
  2. +108 −0 README.md
  3. +99 −72 node-cli.js
View
23 README
@@ -1,23 +0,0 @@
-
-Node.CLI
-By SchizoDuckie
-
-Super simple CLI cursor position control to spice up your script's functionality in terminal/console windows
-Requires sys
-v1.0
-
-Free to use and modify.
-
-
-
-Usage:
-
-
-require('./node-cli.js');
-
-cli.clear().move(20,20).color('red').write('Node.js').down(1).back(7).color('yellow').write('Rocks!').down(10);
-
-Try node example.js to see this in action.
-See screenshot.png for an impression of what you can do.
-
-Enjoy!
View
108 README.md
@@ -0,0 +1,108 @@
+Node.CLI
+=========
+
+By SchizoDuckie
+
+Updated By xkxx
+Super simple CLI cursor position control to spice up your script's functionality in terminal/console windows
+
+Updated for 0.4.*
+
+Requires util
+
+v2.0
+
+Free to use and modify.
+Report any issues you encountered.
+
+Node.cli uses ANSI control codes to control console behavior. For details see: <http://en.wikipedia.org/wiki/ANSI_escape_code>
+
+See screenshot.png for an impression of what you can do.
+
+Enjoy!
+
+Usage:
+-------
+
+```javascript
+var cli = require('./node-cli.js');
+cli.clear().move(20,20).color('red').write('Node.js').down(1).back(7).color('yellow').write('Rocks!').down(10);
+```
+
+Try node example.js to see this in action.
+
+### cli.write(text)
+Print the text to console without adding a new line after it
+
+Example:
+
+```javascript
+cli.write('We are');
+cli.write('in the same line.');
+```
+
+### cli.print(text)
+Same as cli.write(), except will add a new line
+
+Example:
+
+```javascript
+cli.print('We are');
+cli.print('In different lines');
+```
+
+### cli.color(text_color, [bold(boolean or cli.bold), [background_color]])
+### cli.color(text_color, [[background_color, [text_styles...]])
+Set colors and styles
+
+Choices of Text and background colors: 'default', 'gray', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
+
+Choices of Text styles: 'bold', 'underscore', 'blink', 'inverse', 'conceal', 'nobold', 'nounderscore', 'noblink', 'noinverse', 'noconceal', 'frame', 'encircle', 'overline', 'no_frame_or_circle', 'nooverline'.
+
+Example:
+
+```javascript
+cli.color('yellow', cli.bold).write('Here is some yellow text.');
+```
+
+If you are not satisfied with the provided text styles, it's possible to pass SGR codes directly. For example:
+
+```javascript
+cli.color('green', 'default', 'bold', 4, 7); //same as cli.color('green', 'default', 'bold', 'underscore', 'inverse');
+```
+
+### cli.bgcolor(background_color)
+Set only the background color
+
+### cli.style(text_style)
+Set only the text style
+
+### cli.reset()
+Reset the console to default
+
+### cli.resetColor()
+Reset only the text color
+
+### cli.resetBg()
+Reset only the background color
+
+### cli.resetStyle()
+Reset only text styles
+
+### cli.move(x, y)
+Move the cursor in x,y position
+
+### cli.up(x), cli.down(x), cli.fwd(x), cli.back(x)
+Move the cursor up, down, fwd or back x rows/columns
+
+### cli.clear(x)
+Clear the screen. If no parameter is given or x = 2, the entire screen will be cleared; x = 0, the screen above the cursor; x = 1, the screen under the cursor
+
+### cli.clearLine(x)
+Clear the line. Works similarly as cli.clear().
+
+### cli.clearNext(x)
+Clear the next n letters in the line
+
+### cli.custom(command)
+Save you a '\x1B['.
View
171 node-cli.js
@@ -1,20 +1,25 @@
/**
* Node.CLI
* By SchizoDuckie
- *
+ * Updated By xkxx
+ *
* Super simple CLI cursor position control to spice up your script's functionality in terminal/console windows
- * Requires sys
- * v1.0
+ * Updated for 0.4.*
+ * Requires util only
+ * v2.0
+ *
+ * ANSI escape codes are used. For details see: http://en.wikipedia.org/wiki/ANSI_escape_code
*
* Free to use and modify, enjoy!
*/
-var sys = require("sys");
-
+var util = require("util");
function NodeCli () {
+ this.bold = true;
this.colors = {
+ default: 39,
grey: 30,
red: 31,
green: 32,
@@ -25,6 +30,8 @@ function NodeCli () {
white: 37
};
this.bgcolors = {
+ default: 49,
+ gray: 40,
red: 41,
green: 42,
yellow: 43,
@@ -33,113 +40,133 @@ function NodeCli () {
cyan: 46,
white: 47,
};
+ this.textstyles = {
+ 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
+ }
- /**
- * Echo color code, bold is optional
- */
+ //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] : '';
- sys.print('\x1B['+(bold ? 1 : 0)+';'+this.colors[color]+bg+'m');
- return(this);
+ var output = 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.custom(output + 'm');
};
- /**
- * Echo color code, bold is optional
- */
+ //Set background color only
this.bgcolor = function(color) {
- sys.print('\x1B[0;m39');
- return(this);
+ return this.custom(this.bgcolors[color] + 'm');
};
- /**
- * Reset terminal to default color
- */
+ //Set text styles only
+ this.style = function(style) {
+ return this.custom(this.textstyles[style] + 'm');
+ };
+
+ //Reset terminal to default attributes (colors, styles)
+ this.reset = function() {
+ return this.write('\x1B[0m');
+ };
+
+ //Reset terminal to default text color
this.resetColor = function() {
- sys.print('\x1B[0;0m');
- return(this);
+ return this.write('\x1B[39m');
};
- /**
- * Reset terminal to default color
- */
+ //Reset terminal to default background color
this.resetBg = function() {
- sys.print('\x1B[49m49m');
- return(this);
+ return this.write('\x1B[49m');
+ };
+
+ //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) {
- sys.print(string);
+ util.print(string);
+ return(this);
+ };
+
+ //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) {
- sys.print('\033['+x+';'+y+'H');
- return this;
+ return this.custom(x+';'+y+'H');
};
- /**
- * Move the cursor up x rows
- */
+ //Move the cursor up x rows
this.up = function(x) {
- sys.print('\033['+x+'A');
- return this;
+ return this.custom(x+'A');
};
- /**
- * Move the cursor down x rows
- */
+ //Move the cursor down x rows
this.down = function(x) {
- sys.print('\033['+x+'B');
- return this;
+ return this.custom(x+'B');
};
- /**
- * Move the cursor forward x rows
- */
+ //Move the cursor forward x rows
this.fwd = function(x) {
- sys.print('\033['+x+'C');
- return this;
+ return this.custom(x+'C');
};
- /**
- * Move the cursor backwards x columns
- */
+ //Move the cursor backwards x columns
this.back = function(x) {
- sys.print('\033['+x+'D');
- return this;
+ return this.custom(x+'D');
};
- /**
- * Clear the entire screen
- */
+ //Clear the entire screen
this.clear = function(x) {
- sys.print('\033[2J');
- return this;
+ return this.custom((x ? x : 2)+'J');
};
- /**
- * Clear the current line
- */
+ //Clear the current line
this.clearLine = function(x) {
- sys.print('\033[K');
- return this;
+ return this.custom((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);
+ };
+
}
-cli = new NodeCli();
+module.exports = new NodeCli();
+
+//Reset console when program exits
+process.on('exit', module.exports.reset);
-cli.clear().move(38, 5).write('Node.js').down(1).back(7).write('Rocks!');
+//cli.clear().move(38, 5).write('Node.js').down(1).back(7).write('Rocks!');
+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);
Something went wrong with that request. Please try again.