Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

… add resetStyle; use module.exports for exporting
  • Loading branch information...
commit cb3eb5958db26de7762acd675cfc8ed1cdcede69 1 parent d09c3c3
xkxx xkxx authored

Showing 1 changed file with 60 additions and 26 deletions. Show diff stats Hide diff stats

  1. +60 26 node-cli.js
86 node-cli.js
@@ -8,6 +8,8 @@
8 8 * Requires util only
9 9 * v2.0
10 10 *
  11 + * ANSI escape codes are used. For details see: http://en.wikipedia.org/wiki/ANSI_escape_code
  12 + *
11 13 * Free to use and modify, enjoy!
12 14 */
13 15
@@ -16,6 +18,7 @@ var util = require("util");
16 18
17 19 function NodeCli () {
18 20
  21 + this.bold = true;
19 22 this.colors = {
20 23 grey: 30,
21 24 red: 31,
@@ -27,6 +30,7 @@ function NodeCli () {
27 30 white: 37
28 31 };
29 32 this.bgcolors = {
  33 + gray: 40,
30 34 red: 41,
31 35 green: 42,
32 36 yellow: 43,
@@ -35,38 +39,75 @@ function NodeCli () {
35 39 cyan: 46,
36 40 white: 47,
37 41 };
  42 + this.textstyles = {
  43 + bold: 1,
  44 + underscore: 4,
  45 + blink: 5,
  46 + inverse: 7,
  47 + conceal: 8,
  48 + nobold: 22,
  49 + nounderline:24,
  50 + noblink: 25,
  51 + noinverse: 27,
  52 + noconceal: 28,
  53 + frame: 51,
  54 + encircle: 52,
  55 + overline: 53,
  56 + no_frame_or_circle:54,
  57 + nooverline: 55
  58 + }
38 59
39 60 /**
40   - * Set colors
  61 + * Set colors and styles
  62 + * future: color, bold(boolean), background or: color, background, others
41 63 */
42 64 this.color = function(color, bold, background) {
43 65 bg = (background && this.bgcolors[background]) ? ';'+this.bgcolors[background] : '';
  66 + //if(typeof bold == 'boolean')
44 67 this.write('\x1B['+(bold ? 1 : 0)+';'+this.colors[color]+bg+'m');
45 68 return(this);
46 69 };
47 70
48 71 /**
49   - * Set background color only
  72 + * Set bckground color only
50 73 */
51 74 this.bgcolor = function(color) {
52   - this.write('\x1B[' + this.bgcolors[color] + 'm');
53   - return(this);
  75 + return this.write('\x1B[' + this.bgcolors[color] + 'm');
54 76 };
55 77
56 78 /**
57   - * Reset terminal to default color
  79 + * Set text styles only
  80 + */
  81 + this.style = function(style) {
  82 + return this.write('\x1B[' + this.textstyles[style] + 'm');
  83 + };
  84 +
  85 + /**
  86 + * Reset terminal to default attributes (colors, styles)
  87 + */
  88 + this.resetColors = function() {
  89 + return this.write('\x1B[0m');
  90 + };
  91 +
  92 + /**
  93 + * Reset terminal to default text color
58 94 */
59 95 this.resetColor = function() {
60   - this.write('\x1B[0;0m');
61   - return(this);
  96 + return this.write('\x1B[39m');
62 97 };
63 98
64 99 /**
65   - * Reset terminal to default color
  100 + * Reset terminal to default background color
66 101 */
67 102 this.resetBg = function() {
68   - this.write('\x1B[49m49m');
69   - return(this);
  103 + return this.write('\x1B[49m');
  104 + };
  105 +
  106 + /**
  107 + * Reset terminal to all default text styles
  108 + */
  109 + this.resetStyle = function() {
  110 + return this.write('\x1B[10;22;23;24;25;27;28;29;54;55m');
70 111 };
71 112
72 113 /**
@@ -89,56 +130,49 @@ function NodeCli () {
89 130 * Position the Cursor to x/y
90 131 */
91 132 this.move = function(x,y) {
92   - this.write('\033['+x+';'+y+'H');
93   - return this;
  133 + return this.write('\x1B['+x+';'+y+'H');
94 134 };
95 135
96 136 /**
97 137 * Move the cursor up x rows
98 138 */
99 139 this.up = function(x) {
100   - this.write('\033['+x+'A');
101   - return this;
  140 + return this.write('\x1B['+x+'A');
102 141 };
103 142
104 143 /**
105 144 * Move the cursor down x rows
106 145 */
107 146 this.down = function(x) {
108   - this.write('\033['+x+'B');
109   - return this;
  147 + return this.write('\x1B['+x+'B');
110 148 };
111 149
112 150 /**
113 151 * Move the cursor forward x rows
114 152 */
115 153 this.fwd = function(x) {
116   - this.write('\033['+x+'C');
117   - return this;
  154 + return this.write('\x1B['+x+'C');
118 155 };
119 156
120 157 /**
121 158 * Move the cursor backwards x columns
122 159 */
123 160 this.back = function(x) {
124   - this.write('\033['+x+'D');
125   - return this;
  161 + return this.write('\x1B['+x+'D');
126 162 };
127 163
128 164 /**
129 165 * Clear the entire screen
130 166 */
131 167 this.clear = function(x) {
132   - this.write('\033[2J');
133   - return this;
  168 + return this.write('\x1B['+(x ? x : 2)+'J');
134 169 };
135 170
136 171 /**
137 172 * Clear the current line
138 173 */
139 174 this.clearLine = function(x) {
140   - this.write('\033[K');
141   - return this;
  175 + return this.write('\x1B['+(x ? x : 2)+'K');
142 176 };
143 177
144 178 /**
@@ -150,7 +184,7 @@ function NodeCli () {
150 184
151 185 }
152 186
153   -cli = new NodeCli();
  187 +module.exports = new NodeCli();
154 188
155 189 //cli.clear().move(38, 5).write('Node.js').down(1).back(7).write('Rocks!');
156   -cli.clear().move(20,20).color('red').write('Node.js').down(1).back(7).color('yellow').write('Rocks!').down(10);
  190 +module.exports.clear().move(20,20).color('red').write('Node.js').down(1).back(7).color('yellow').write('Rocks!').down(10);

0 comments on commit cb3eb59

Please sign in to comment.
Something went wrong with that request. Please try again.