This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
terminal.js
263 lines (233 loc) · 8.07 KB
/
terminal.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var sys = require('util');
// Terminal object
// Allows for controlling the terminal by outputting control characters
var terminal = {
// Terminal escape character
escape_code: '\033',
// Display attributes reset
reset_code: '\033[0m',
// Terminal display colors
color_codes: {
foreground: {
'black': 30,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'purple': 35,
'cyan': 36,
'white': 37
},
background: {
'black': 40,
'red': 41,
'green': 42,
'yellow': 43,
'blue': 44,
'magenta': 45,
'cyan': 46,
'white': 47
},
attribute: {
'reset': 0,
'bright': 1,
'bold': 1,
'dim': 2,
'underscore': 4,
'underline': 4,
'blink': 5,
'inverse': 6,
'hidden': 8
}
},
// Get the terminal command for a color. A color command is build up of a
// foreground, a background and a display attribute. These can be defined as
// an object resulting in a compound command. When the color is passed as a
// string it is assumed this is a foreground color.
_color: function(color) {
if (undefined === color) {
throw 'Color not defined';
}
// Check if color is only a string
if (typeof color == 'string') {
// Assume we want a foreground color
color = {foreground: color};
}
// Collect all parts of the color code
var code = [];
for (type in color) {
var colorCode = this._colorCode(color[type], type);
if (null !== colorCode) {
code.push(colorCode);
}
}
// Construct the complete code
return this.escape_code + '[' + code.join(';') + 'm';
},
// Print a terminal color command. This uses the `_color` function to
// retreive the color command.
color: function(color) {
process.stdout.write(this._color(color));
return this;
},
// Print the color reset code.
reset: function() {
process.stdout.write(this.reset_code);
return this;
},
// Get a color code for a color type. Color types are `foreground`,
// `background` or `attribute`.
_colorCode: function(name, type) {
type = type || 'foreground';
if (type in this.color_codes) {
if (name in this.color_codes[type]) {
return this.color_codes[type][name];
}
}
return null;
},
// Colorize a message and return it. The message can be colorized with
// modifiers that are preceded by a `%` character. The following modifiers
// are supported:
// text text background
// ------------------------------------------------
// %k %K %0 black dark grey black
// %r %R %1 red bold red red
// %g %G %2 green bold green green
// %y %Y %3 yellow bold yellow yellow
// %b %B %4 blue bold blue blue
// %m %M %5 magenta bold magenta magenta
// %p %P magenta (think: purple)
// %c %C %6 cyan bold cyan cyan
// %w %W %7 white bold white white
//
// %F Blinking, Flashing
// %U Underline
// %8 Reverse
// %_,%9 Bold
//
// %n,%N Resets the color
// %% A single %
_colorize: function(message) {
var conversions = {
'%y': {foreground: 'yellow'},
'%g': {foreground: 'green'},
'%b': {foreground: 'blue'},
'%r': {foreground: 'red'},
'%p': {foreground: 'magenta'},
'%m': {foreground: 'magenta'},
'%c': {foreground: 'cyan'},
'%w': {foreground: 'grey'},
'%k': {foreground: 'black'},
'%n': 'reset',
'%Y': {foreground: 'yellow', attribute: 'bold'},
'%G': {foreground: 'green', attribute: 'bold'},
'%B': {foreground: 'blue', attribute: 'bold'},
'%R': {foreground: 'red', attribute: 'bold'},
'%P': {foreground: 'magenta', attribute: 'bold'},
'%M': {foreground: 'magenta', attribute: 'bold'},
'%C': {foreground: 'cyan', attribute: 'bold'},
'%W': {foreground: 'grey', attribute: 'bold'},
'%K': {foreground: 'black', attribute: 'bold'},
'%N': 'reset',
'%0': {background: 'black'},
'%1': {background: 'red'},
'%2': {background: 'green'},
'%3': {background: 'yellow'},
'%4': {background: 'blue'},
'%5': {background: 'magenta'},
'%6': {background: 'cyan'},
'%7': {background: 'grey'},
'%F': {attribute: 'blink'},
'%U': {attribute: 'underline'},
'%8': {attribute: 'inverse'},
'%9': {attribute: 'bold'},
'%_': {attribute: 'bold'}
};
// Replace escaped '%' characters
message = message.replace('%%', '% ');
// Convert all tokens with color codes
for (conversion in conversions) {
// Special case for `reset`
if ('reset' === conversions[conversion]) {
message = message.replace(conversion, this.reset_code);
} else {
message = message.replace(new RegExp(conversion, ['g']), this._color(conversions[conversion]));
}
}
// Reset all escape '%' characters
message = message.replace('% ', '%');
// Return the message
return message;
},
// This uses the `_colorize` function to __print__ a colorized message.
colorize: function(message) {
process.stdout.write(this._colorize(message));
return this;
},
// Write a message in the terminal
write: function(message) {
process.stdout.write(message);
return this;
},
// Print one or more new line characters
nl: function(n) {
n = n || 1;
for (var i = 0; i < n; i++) {
process.stdout.write('\n');
}
return this;
},
// Move the terminal cursor
move: function(x, y) {
x = x || 0;
y = y || 0;
var command = this.escape_code + '[';
if (undefined !== x && 0 < x) {
command += ++x;
}
if (undefined !== y && 0 < y) {
command += ';' + ++y ;
}
process.stdout.write(command + 'H');
return this;
},
// Move the terminal cursor up `x` positions
up: function(x) {
process.stdout.write(this.escape_code + '[' + x + 'A');
return this;
},
// Move the terminal cursor down x positions
down: function(x) {
process.stdout.write(this.escape_code + '[' + x + 'B');
return this;
},
// Move the terminal cursor `p` positions right
right: function(p) {
process.stdout.write(this.escape_code + '[' + p + 'C');
return this;
},
// Move the terminal cursor `p` positions left
left: function(p) {
process.stdout.write(this.escape_code + '[' + p + 'D');
return this;
},
// Clear all characters from the terminal screen
clear: function() {
process.stdout.write(this.escape_code + '[2J');
return this;
},
// Clear the line the cursor is at
clearLine: function() {
process.stdout.write(this.escape_code + '[2K');
return this;
},
// Clear the next `n` characters from the current cursor position.
clearCharacters: function(n) {
this.write(new Array(n + 2).join(' ')).left(n + 2);
return this;
}
};
// Export the command object
module.exports = terminal;