-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathmisc.js
More file actions
395 lines (337 loc) · 8.83 KB
/
Copy pathmisc.js
File metadata and controls
395 lines (337 loc) · 8.83 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
var exec = require('child_process').exec;
/**
* Process shortcut return values
*
* Accepts input of the forms:
*
* false, null, undefined, 0 (success)
* true, 1 (error)
* exitCode (integer, specific error)
* [ exitCode, data ] (integer exitCode + object to return)
*
*
*/
exports.returnMeta = function (status, meta) {
var code = 0;
meta = meta || {};
if (typeof status == 'number') {
status = number > 0;
code = number;
}
else if (typeof status == 'boolean') {
code = +status;
}
else {
status = false;
}
meta.status = status;
meta.code = code;
return meta;
};
exports.composePath = function (name, path) {
if (path != '' && path !== undefined && path !== null) {
name = path.replace(/\/$/, '') +'/'+ name;
};
return name;
};
/**
* Generator for decorator to track asynchronous tasks.
*
* Allows you to execute a complicated dynamic callback hierarchy and
* call a handler when all marked callbacks have finished.
*
* @param done
* Callback to call when all callbacks are done.
* @param ...
* Additional arguments for the done callback.
*
* @return function queue (callback)
* Decorator for a callback to track.
*
*/
exports.whenDone = function (done) {
// Initialize closure variable.
var count = 0,
// Store additional arguments.
done_args = [].slice.call(arguments, 1);
return function (callback) {
// Register new task.
count++;
// Decorate callback with exit-checker.
return function () {
callback && callback.apply(this, arguments);
if (--count == 0) {
done.apply(this, done_args);
}
}
}
}
/**
* Execute a function asynchronously.
*/
exports.async = function (func) {
var that = this,
args = [].slice.call(arguments, 1);
setTimeout(function () { func.apply(that, args); }, 0);
}
/**
* Make an asynchronously executed callback.
*/
exports.asyncCallback = function (func) {
var that = this,
args = [].slice.call(arguments, 1);
return function () {
setTimeout(function () { func.apply(that, args); }, 0);
};
}
/**
* Extend object with getter/setter support.
*/
exports.extend = function (a,b) {
for ( var i in b ) {
var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
if ( g || s ) {
if ( g )
a.__defineGetter__(i, g);
if ( s )
a.__defineSetter__(i, s);
} else
a[i] = b[i];
}
return a;
}
/**
* Expand a local file path.
*/
exports.expandPath = function (path, callback) {
if (path[0] == '~') {
if (path.length == 1 || path[1] == '/') {
return callback(process.env.HOME + path.substring(1));
}
else {
// TODO: support ~user syntax. Need getpwnam bindings to work across BSD/Linux.
}
}
return callback(path);
}
/**
* JSON pretty printer.
*/
exports.JSONPretty = function (data) {
// Normalize to compact JSON.
if (typeof data == 'string') {
data = JSON.parse(data);
}
data = JSON.stringify(data);
// Add spaces around operators and quotes.
data = data.replace(/("[^"]*"|'[^']*')?([\[{:,}\]](?!\s))/g, '$1$2 ');
data = data.replace(/(\S)([\]}])/g, '$1 $2');
// Add linebreaks around element/list separators.
data = data.replace(/[ \n\t]+([\]}])/g, " $1");
data = data.replace(/ ([\]}])/g, "\n$1");
data = data.replace(/(("[^"]*"|'[^']*')?[\[{,])[ \n\t]+/g, "$1 ");
data = data.replace(/(("[^"]*"|'[^']*')?[\[{,] )/g, "$1\n");
// Indent
var m = [],
indent = 0,
data = data.split("\n");
for (i in data) (function (line) {
// Count closing chars.
indent -= (m = line.match(/[\]}]/g)) && m.length;
// Two spaced indent.
line = Array(indent + 1).join(' ') + line;
// Count opening chars.
indent += (m = line.match(/[\[{]/g)) && m.length;
data[i] = line;
})(data[i]);
return data.join("\n");
}
/**
* Parse command arguments.
*
* -x -> options[x] = true
* --foo -> options[foo] = true
* --foo bar -> options[foo] = 'bar'
*/
exports.parseArgs = function (tokens) {
var options = {},
values = [],
n = tokens.length,
i = 1, m;
for (; i < n; ++i) (function (token) {
if (m = /^-([A-Za-z0-9_])$/.exec(tokens[i])) {
options[m[1]] = true;
}
else if (m = /^-([A-Za-z0-9_][A-Za-z0-9_-]*)$/.exec(tokens[i])) {
var flags = m[1].split(''), j;
for (j in flags) {
options[flags[j]] = true;
}
}
else if (m = /^--([A-Za-z0-9_-]+)$/.exec(tokens[i])) {
if (typeof tokens[i + 1] != 'undefined' && tokens[i + 1][0] != '-') {
++i;
options[m[1]] = tokens[i];
}
else {
options[m[1]] = true;
}
}
else {
values.push(tokens[i]);
}
})(tokens[i]);
return { values: values, options: options };
}
/**
* Return array of object keys.
*/
exports.objectKeys = function (object) {
var keys = [];
for (i in object) keys.push(i);
return keys;
}
/**
* Escape binary data for display as HTML.
*/
exports.escapeBinary = function (data) {
if (typeof data != 'string') {
data = data.toString('utf-8');
}
var binary = data, n = binary.length, i = 0;
// Escape non-printables
binary = binary.replace(/([\u0000-\u001F\u0080-\u009F])/g, function (x, chr) {
if (/[^\r\n\t]/.exec(chr)) {
var num = chr.charCodeAt(0).toString(16);
while (num.length < 4) num = '0' + num;
return '\\u' + num;
}
return chr;
});
return binary;
}
/**
* Escape textual Unix data for display as HTML.
*/
exports.escapeUnixText = function (data) {
if (typeof data != 'string') {
data = data.toString('utf-8');
}
var binary = data, n = binary.length, i = 0;
// Escape HTML characters.
binary = binary.replace(/[<&]/g, function (x) {
return { '<': '<', '&': '&' }[x];
});
// ANSI state.
var bold = false,
italic = false,
underline = false,
blink = false,
strike = false,
invert = false,
fg = 0,
bg = 0;
// Handle ANSI escapes.
binary = binary.replace(/\u001b\[([0-9]+(?:;[0-9]+)*)?([A-Za-z])/g, function (x, codes, command) {
// Remove non-color codes.
if (command != 'm') return '';
codes = codes.split(';');
// Replace each code with a closing and opening span.
function span() {
var out = '</span>';
var styles = [];
if (bold) styles.push('font-weight: bold');
if (italic) styles.push('font-variant: italic');
var deco = [];
if (blink) deco.push('blink');
if (underline) deco.push('underline');
if (strike) styles.push('line-through');
if (deco.length) styles.push('text-decoration: ' + deco.join(' '));
var color = invert ? bg : fg + (bold ? 'b' : ''),
background = invert ? fg : bg;
var klass = [ 'termkitAnsiFg' + color, 'termkitAnsiBg' + background ];
out += '<span class="'+ klass.join(' ') +'" style="' + styles.join(';') + '">';
return out;
}
// Supported codes.
for (i in codes) {
// process.stderr.write('code ' + + "\n");
switch (parseInt(codes[i], 10)) {
case 0:
bold = italic = underline = blink = strike = bright = invert = false;
fg = bg = 0;
break;
case 1:
bold = true;
break;
case 3:
italic = true;
break;
case 4:
underline = true;
break;
case 5:
blink = true;
break;
case 7:
invert = true;
break;
case 9:
strike = true;
break;
case 21:
bold = false;
break;
case 23:
italic = false;
break;
case 24:
underline = false;
break;
case 25:
blink = false;
break;
case 27:
invert = false;
break;
case 29:
strike = false;
break;
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
fg = codes[i] - 30;
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
bg = codes[i] - 40;
break;
}
return span();
}
});
binary = '<span>'+ binary +'</span>';
// Handle antique bold/italic escapes used by grotty -c.
binary = binary
.replace(/_\u0008(.)\u0008\1/g, function (x, char) {
return '<b><i>' + char + '</i></b>';
})
.replace(/(.)\u0008\1/g, function (x, char) {
return '<b>' + char + '</b>';
})
.replace(/_\u0008(.)/g, function (x, char) {
return '<i>' + char + '</i>';
});
return exports.escapeBinary(binary);
}