-
Notifications
You must be signed in to change notification settings - Fork 11
/
status.js
311 lines (274 loc) · 7.83 KB
/
status.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const colors = require('colors');
const util = require('util');
const tty = require('tty');
const charm = require('charm')(process);
const cliSpinners = require('cli-spinners');
const cliCursor = require('cli-cursor');
const PADDING = ' ';
var iterations = 0;
var looper = null;
var running = false;
var items = {};
var defaultPattern = null;
var settings = {
invert: false,
interval: 250,
pattern: null,
bottom: false
};
var isatty = tty.isatty(1) && tty.isatty(2);
var tty_size = {
width: isatty
? process.stdout.getWindowSize
? process.stdout.getWindowSize(1)[0]
: tty.getWindowSize()[1]
: 75,
height: isatty
? process.stdout.getWindowSize
? process.stdout.getWindowSize(1)[1]
: tty.getWindowSize()[2]
: 75
};
//
// This is a single item (Or cell or whatever you call it) in the status display
//
var Item = function (options) {
const defaults = {
name: null,
max: null,
precision: 2,
steps: false
};
for (var attrname in defaults) {
this[attrname] = options.hasOwnProperty(attrname) && options[attrname] !== null ? options[attrname] : defaults[attrname];
}
if(options.custom) this.custom = options.custom.bind(this);
this.val = options.count || 0;
};
//
// Item functions for value changes, rendering, etc
//
Item.prototype = {
inc: function (amount) {
this.val += (amount !== undefined) ? amount : 1;
},
dec: function (amount) {
this.val -= (amount !== undefined) ? amount : 1;
},
doneStep: function (success, message) {
if(!this.steps || this.count >= this.steps.length) return;
charm.erase('line').erase('down');
message = message ? ` - ${message}` : '';
write(`${success ? '✔'.green : '✖'.red} ${this.render('step')}${message}\n`);
this.inc();
},
render: function (style) {
switch (style) {
case 'step':
if(!this.steps || this.count >= this.steps.length) return '';
return `${this.steps[this.count]}`;
case 'custom':
return this.custom ? this.custom() : '';
case 'percentage':
if (!this.max) return '';
var max = typeof this.max == 'function'
? this.max()
: this.max;
return (100 * this.count / max).toFixed(this.precision) + '%';
case 'time':
return nicetime(this.count);
case 'bar':
if (!this.max) return '';
var bar_len = 10;
var max = typeof this.max == 'function'
? this.max()
: this.max;
var done = Math.round(bar_len * this.count / max);
return '[' + '▒'.repeat(Math.min(bar_len, done)) + '-'.repeat(Math.max(0,bar_len - done)) + ']';
case 'default':
case 'count':
default:
var max = typeof this.max == 'function'
? this.max()
: this.max;
return this.count + (max ? '/' + max : '');
}
}
};
//
// Getter/setter for count. Auto-rendering, basically.
//
Object.defineProperties(Item.prototype, {
count: {
get: function () {
return this.val;
},
set: function (newValue) {
this.val = newValue;
}
}
});
//
// Repeats a string, using it for the status bar instead of loops
//
String.prototype.repeat = function (len) {
return new Array(len + 1).join(this);
};
//
// Render the status bar row
// If stamp is true, it will console.log it instead of doing an stdout
//
const render = () => {
iterations++;
if (!running) return;
var color_len = 0;
for (var i = 0; i < items.length; i++) {
if (items[i].color) {
color_len += (items[i].color('')).length;
}
}
var out = generateBar();
var bar = ' '.repeat(tty_size.width);
if (settings.invert) {
bar = bar.inverse;
out = out.inverse;
}
var current_height = Math.ceil((out.length - color_len) / tty_size.width );
charm.position(function (x, y) {
var current_row = y;
// If the current cursor row was on the bar, we need to make a gap
if (settings.bottom && current_row > tty_size.height - current_height) {
for(var i = 0; i < current_height; i++) {
// charm.delete('line', 1);
charm.erase('line');
write('\n');
}
y -= current_height - (tty_size.height - current_row);
}
charm
.move(0, settings.bottom ? tty_size.height : 0)
.left(tty_size.width)
.write(bar);
if(settings.bottom) {
for(var i = 0; i < Math.max(0, current_height - 1); i++) {
charm.left(tty_size.width).write(bar).up(1);
}
}
charm
.left(tty_size.width)
.write(out)
.position(x, y);
});
};
const write = (string) => process.stdout.write(string);
const generateBar = (withPattern) => {
var pattern = withPattern ? withPattern : (settings.pattern ? settings.pattern : defaultPattern);
return pattern.replace(/\{([a-zA-z0-9\s\.]*)\}/g, (match, id) => {
var tokens = id.split('.');
var portion = '';
var color = null;
var modifier = null;
if(tokens.length > 1 && colors[tokens[1]]) {
color = colors[tokens[1]];
modifier = tokens.length > 2 ? tokens[2] : null;
} else if (tokens.length > 2 && colors[tokens[2]]) {
color = colors[tokens[2]];
modifier = tokens[1];
} else if(tokens.length > 1) {
modifier = tokens[1];
}
switch (tokens[0]) {
case 'timestamp':
case 'uptime':
portion = nicetime(process.uptime(), true);
break;
case 'spinner':
var spinnerType = modifier || 'dots';
portion = cliSpinners[spinnerType].frames[iterations % cliSpinners[spinnerType].frames.length];
break;
default:
if(items[tokens[0]]) portion = items[tokens[0]].render(modifier);
break;
}
return color ? color(portion) : portion;
});
};
//
// Currently just changes the milliseconds to either a number of seconds or number of minutes
//
var nicetime = (ms, use_seconds) => {
var seconds = (ms / (use_seconds ? 1 : 1000)).toFixed((use_seconds ? 0 : 3));
var minutes = (seconds / 60).toFixed(3);
var time = (minutes < 2) ? seconds : minutes;
return time + (minutes < 2 ? 's' : 'm');
};
process.on('exit', function () {
if(running) stamp();
});
exports.addItem = (name, options) => {
if(!name || typeof name !== 'string') return null;
options = options || {};
options.name = name;
var item = new Item(options);
items[name] = item;
rebuildPattern();
return items[name];
};
var rebuildPattern = () => {
defaultPattern = Object.keys(items).reduce((memo, item) => {
return `${memo}${PADDING}${item}: {${item}}${PADDING}|`;
}, `Status @ {uptime}${PADDING}|`);
};
exports.removeItem = (item) => {
if(typeof item === 'string') {
delete items[item];
} else if(item instanceof Item) {
delete items[item.name];
}
rebuildPattern();
}
exports.removeAll = () => {
items = {};
rebuildPattern();
}
exports.toString = () => generateBar();
exports.clear = () => charm.erase('line').erase('down');
exports.console = function () {
var methods = {};
['log', 'info', 'error', 'warn'].forEach(m => {
methods[m] = function () {
if(m !== 'log' || running) exports.clear();
console[m].apply(this, arguments);
if(running) render();
}
});
return methods;
};
//
// Turns it on, will start rendering on interval now
//
exports.start = (opts) => {
settings = Object.assign(settings, opts)
running = true;
if(!settings.bottom) cliCursor.hide();
render();
looper = setInterval(render, settings.interval);
};
exports.setPattern = (pattern) => settings.pattern = pattern;
exports.stop = () => {
running = false;
clearTimeout(looper);
cliCursor.show();
charm.end();
};
//
// Stamps the current status to the console
//
var stamp = exports.stamp = (withPattern) => {
charm.erase('line').erase('down');
return console.log(generateBar(withPattern));
}
//
// Gets the total number of cells in the bar
//
exports.cellCount = () => Object.keys(items).length;