/
node-cli.js
190 lines (166 loc) · 3.57 KB
/
node-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* 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 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 util = require("util");
function NodeCli () {
this.bold = true;
this.colors = {
grey: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37
};
this.bgcolors = {
gray: 40,
red: 41,
green: 42,
yellow: 43,
blue: 44,
magenta: 45,
cyan: 46,
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,
no_frame_or_circle:54,
nooverline: 55
}
/**
* Set colors and styles
* future: 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
*/
this.bgcolor = function(color) {
return this.write('\x1B[' + this.bgcolors[color] + 'm');
};
/**
* Set text styles only
*/
this.style = function(style) {
return this.write('\x1B[' + this.textstyles[style] + 'm');
};
/**
* Reset terminal to default attributes (colors, styles)
*/
this.resetColors = function() {
return this.write('\x1B[0m');
};
/**
* Reset terminal to default text color
*/
this.resetColor = function() {
return this.write('\x1B[39m');
};
/**
* Reset terminal to default background color
*/
this.resetBg = function() {
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
*/
this.write = function(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
*/
this.move = function(x,y) {
return this.write('\x1B['+x+';'+y+'H');
};
/**
* Move the cursor up x rows
*/
this.up = function(x) {
return this.write('\x1B['+x+'A');
};
/**
* Move the cursor down x rows
*/
this.down = function(x) {
return this.write('\x1B['+x+'B');
};
/**
* Move the cursor forward x rows
*/
this.fwd = function(x) {
return this.write('\x1B['+x+'C');
};
/**
* Move the cursor backwards x columns
*/
this.back = function(x) {
return this.write('\x1B['+x+'D');
};
/**
* Clear the entire screen
*/
this.clear = function(x) {
return this.write('\x1B['+(x ? x : 2)+'J');
};
/**
* 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.
*/
this.clearNext = function(x) {
return this.write(new Array(x+1).join(' ')).back(x);
}
}
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);