-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
814 lines (713 loc) · 20.3 KB
/
index.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
'use strict';
var koalas = require('koalas');
var isNum = require('is-number');
var size = require('window-size');
var readline = require('readline');
var isBuffer = require('is-buffer');
var flatten = require('arr-flatten');
var extend = require('extend-shallow');
var isWindows = require('is-windows');
var MuteStream = require('mute-stream');
var stripColor = require('strip-color');
var sizeUtils = require('window-size/utils');
var utils = module.exports;
/**
* Create default options
*/
utils.createOptions = function(options) {
var opts = extend({ terminal: true }, options);
opts.output = opts.output || process.stdout;
opts.input = opts.input || process.stdin;
return opts;
};
/**
* Create a readline interface with the given `options`.
* @param {Object} `options`
* @api public
*/
utils.createInterface = function(options) {
var opts = utils.createOptions(options);
var ms = new MuteStream();
ms.pipe(opts.output);
opts.output = ms;
return readline.createInterface(opts);
};
/**
* Move cursor up by `n` lines.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Lines up to move. Default is `1`.
* @api public
*/
utils.up = function(rl, n) {
readline.moveCursor(rl.output, 0, -(n || 1));
return this;
};
/**
* Move cursor down by `n` lines.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Lines down to move. Default is `1`.
* @api public
*/
utils.down = function(rl, n) {
readline.moveCursor(rl.output, 0, n || 1);
return this;
};
/**
* Move cursor left by `n` colums.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Characters to move left. Default is `1`.
* @api public
*/
utils.left = function(rl, n) {
readline.moveCursor(rl.output, -(n || 1));
return this;
};
/**
* Move cursor right by `n` colums.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Characters to move right. Default is `1`.
* @api public
*/
utils.right = function(rl, n) {
readline.moveCursor(rl.output, n || 1);
return this;
};
/**
* Move cursor up, down, left or right by `1` line.
*
* ```js
* var utils = require('readline-utils');
* var rl = utils.createInterface();
* rl.input.on('keypress', function(str, key) {
* utils.move(rl, key);
* });
* ```
* @param {Readline} `rl` Readline interface
* @api public
*/
utils.move = function(rl, key, n) {
if (key && exports[key.name]) {
exports[key.name](rl, n);
}
return this;
};
/**
* Callback function for the `keypress` event, to automatically move cursor
* up, down, left or right by `1` line.
*
* ```js
* var utils = require('readline-utils');
* var rl = utils.createInterface();
* rl.input.on('keypress', utils.auto(rl));
* ```
* @param {Readline} `rl` Readline interface
* @api public
*/
utils.auto = function(rl) {
return function(s, key) {
utils.move(rl, key);
};
};
/**
* Clear `n` lines.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Number of lines to clear
*/
utils.clearLine = function(rl, n) {
rl.output.write(utils.eraseLines(n));
return this;
};
/**
* Clear `n` lines after the cursor.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Number of lines to clear
* @api public
*/
utils.clearAfter = function(rl, n) {
utils.clearLine(rl, n || 1);
return this;
};
/**
* Clear the terminal.
*
* @param {Readline} `rl` Readline interface
* @param {Number} `n` Number of lines to clear
* @api public
*/
utils.clearScreen = function(rl) {
rl.write(null, { ctrl: true, name: 'l' });
return this;
};
/**
* Get the last line from the given `str`
*
* @param {String} `str`
* @return {String}
* @api public
*/
utils.lastLine = function(str) {
return last(str.split('\n'));
};
/**
* Get the height (rows) of the given `str`
*
* @param {String} `str`
* @return {Number}
* @api public
*/
utils.height = function(str) {
return str.split('\n').length;
};
/**
* Hide the cursor so it doesn't show during a prompt. This is
* useful for multiple-choice or list prompts, or any prompt
* where the user will not be entering input.
*
* @param {Readline} `rl` Readline interface
* @return {Object} readline-utils object for chaining
* @api public
*/
utils.hideCursor = utils.cursorHide = function(rl) {
rl.output.write('\x1B[?25l');
return this;
};
/**
* Show the cursor.
*
* @param {Readline} `rl` Readline interface
* @return {Object} readline-utils object for chaining
* @api public
*/
utils.showCursor = utils.cursorShow = function(rl) {
rl.output.write('\x1B[?25h');
return this;
};
/**
* Close the interface, remove event listeners, and restore/unmute prompt functionality
*
* @param {Readline} `rl` Readline interface
* @return {Object} readline-utils object for chaining
* @api public
*/
utils.close = function(rl, fn) {
fn = fn || utils.forceClose.bind(exports, rl);
process.removeListener('exit', fn);
rl.removeListener('SIGINT', fn);
if (typeof rl.output.unmute === 'function') {
rl.output.unmute();
rl.output.end();
}
rl.pause();
rl.close();
return this;
};
/**
* Close the interface when the keypress is `^C`
*
* @param {Readline} `rl` Readline interface
* @return {Object} readline-utils object for chaining
* @api public
*/
utils.forceClose = function(rl) {
utils.close(rl);
return this;
};
/**
* Erase `n` lines
*
* ```js
* utils.eraseLines(3);
* ```
* @param {Number} `n`
* @return {String} Returns the unicode to erase lines
* @api public
*/
utils.eraseLines = function(n) {
var num = toNumber(n);
var lines = '';
var i = -1;
while (++i < num) {
lines += '\u001b[1000D\u001b[K';
if (i < num - 1) {
lines += '\u001b[1A';
}
}
return lines;
};
/**
* Remove lines from the bottom of the terminal.
* @param {Number} `rl` Readline interface
* @param {Number} `lines` Number of lines to remove
* @param {Number} `height` Content height
* @return {Object} Returns the readline-utils object for chaining
* @api public
*/
utils.clearTrailingLines = function(rl, lines, height) {
if (!isNumber(lines)) lines = 0;
var len = height + lines;
while (len--) {
readline.moveCursor(rl.output, -utils.cliWidth(80), 0);
readline.clearLine(rl.output, 0);
if (len) readline.moveCursor(rl.output, 0, -1);
}
return this;
};
/**
* Remember the cursor position
* @return {Object} readline-utils object
* @api public
*/
utils.cursorPosition = function(rl) {
return rl._getCursorPos();
};
/**
* Restore the cursor position to where it has been previously stored.
* @return {Object} readline-utils object
* @api public
*/
utils.restoreCursorPos = function(rl, cursorPos) {
if (!cursorPos) return;
var line = rl._prompt + rl.line;
readline.moveCursor(rl.output, -line.length, 0);
readline.moveCursor(rl.output, cursorPos.cols, 0);
cursorPos = null;
return this;
};
/**
* Get the width of the terminal
*
* @param {Number} `fallback` A fallback width to use if the actual width is not found.
* @return {Number} Returns the number of columns.
* @api public
*/
utils.cliWidth = function(fallback) {
var windows = isWindows();
var modifier = windows ? 1 : 0;
size = size || (windows ? sizeUtils.win() : sizeUtils.tput());
return koalas(size && size.width, fallback, modifier) - modifier;
};
/**
* Break lines longer than the cli width so we can normalize the
* natural line returns behavior accross terminals. (I don't see how
* this can work consistently. It seems brittle and will probably be replaced
* with https://github.com/jonschlinkert/word-wrap)
*
* @param {Array} `lines` Array of lines
* @param {Number} `width` Terminal width
* @api public
*/
utils.breakLines = function(lines, width) {
var quantifier = '';
if (width > 1) {
quantifier = '{1,' + width + '}';
}
var regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?)' + quantifier, 'g');
return lines.map(function(line) {
var matches = line.match(regex);
if (!matches) return '';
// last match is always empty
matches.pop();
return matches || '';
});
};
/**
* Joins the lines returned from [.breakLines](#breakLines).
*
* @param {Array|String} `lines` String or array of lines.
* @param {Number} `width` Terminal width
* @return {String}
* @api public
*/
utils.forceLineReturn = function(lines, width) {
if (typeof lines === 'string') {
lines = utils.breakLines(lines.split('\n'), width);
}
return flatten(lines).join('\n');
};
/**
* Ensure the given `str` ends in a newline.
*
* ```js
* console.log(utils.normalizeLF('foo'));
* //=> 'foo\n'
* ```
* @param {String} `str` The input string
* @return {String}
* @api public
*/
utils.normalizeLF = function(str) {
if (str.slice(-1) !== '\n') {
str += '\n';
}
return str;
};
/**
* Strip ansi styles from the given string.
*/
utils.unstyle = function(str) {
return stripColor(str);
};
/**
* The following code is based on code from the node.js core
* readline module and https://github.com/TooTallNate/keypress.
*/
/**
* This module offers the internal "keypress" functionality from node-core's
* `readline` module, for your own programs and modules to use.
*
* The `keypress` function accepts a readable Stream instance and makes it
* emit "keypress" events.
*
* Usage:
*
* ``` js
* require('keypress')(process.stdin);
*
* process.stdin.on('keypress', function(ch, key) {
* console.log(ch, key);
* if (key.ctrl && key.name === 'c') {
* process.stdin.pause();
* }
* });
* proces.stdin.resume();
* ```
*
* @param {Stream} stream
* @api public
*/
utils.keypress = function(stream) {
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
stream._keypressDecoder = new StringDecoder('utf8');
function onData(b) {
if (stream.listenerCount('keypress') > 0) {
var r = stream._keypressDecoder.write(b);
if (r) utils.emitKeypress(stream, r);
} else {
// Nobody's watching anyway
stream.removeListener('data', onData);
stream.on('newListener', onNewListener);
}
}
function onNewListener(event) {
if (event === 'keypress') {
stream.on('data', onData);
stream.removeListener('newListener', onNewListener);
}
}
if (stream.listenerCount('keypress') > 0) {
stream.on('data', onData);
} else {
stream.on('newListener', onNewListener);
}
};
/**
* Enables "mousepress" events on the *input* stream. Note
* that `stream` must be an *output* stream (i.e. a Writable
* Stream instance), usually `process.stdout`.
*
* @param {Stream} stream writable stream instance
* @api public
*/
utils.enableMouse = function(stream) {
stream.write('\x1b[?1000h');
};
/**
* Disables "mousepress" events from being sent to the *input*
* stream. Note that `stream` must be an *output* stream (i.e.
* a Writable Stream instance), usually `process.stdout`.
*
* @param {Stream} stream writable stream instance
* @api public
*/
utils.disableMouse = function(stream) {
stream.write('\x1b[?1000l');
};
///////////////////////////////////////////////////////////////////////
// Below this function is code from node-core's `readline.js` module //
///////////////////////////////////////////////////////////////////////
/*
Some patterns seen in terminal key escape codes, derived from combos seen
at http://www.midnight-commander.org/browser/lib/tty/key.c
ESC letter
ESC [ letter
ESC [ modifier letter
ESC [ 1 ; modifier letter
ESC [ num char
ESC [ num ; modifier char
ESC O letter
ESC O modifier letter
ESC O 1 ; modifier letter
ESC N letter
ESC [ [ num ; modifier char
ESC [ [ 1 ; modifier letter
ESC ESC [ num char
ESC ESC O letter
- char is usually ~ but $ and ^ also happen with rxvt
- modifier is 1 +
(shift * 1) +
(left_alt * 2) +
(ctrl * 4) +
(right_alt * 8)
- two leading ESCs apparently mean the same as one leading ESC
*/
utils.normalize = function(s, key) {
var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
var functionKeyCodeRe = /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
var events = [];
var parts;
var ch = s;
key = extend({
name: undefined,
ctrl: false,
meta: false,
shift: false,
value: s
}, key);
if (s === null && key.name === 'down') {
key.ctrl = true;
s = 'n';
}
if (s === null && key.name === 'up') {
key.ctrl = true;
s = 'p';
}
if (isBuffer(s)) {
if (s[0] > 127 && s[1] === undefined) {
s[0] -= 128;
s = '\x1b' + s.toString();
} else {
s = s.toString();
}
}
if (!s && key && key.sequence) {
s = key.sequence;
}
if (typeof s === 'number') {
s = String(s);
key.value = Number(s);
key.name = 'number';
}
key.sequence = String(s);
if (s === '\r') {
// carriage return
key.name = 'return';
} else if (s === '\n') {
// enter, should have been called linefeed
key.name = 'enter';
} else if (s === '\t') {
// tab
key.name = 'tab';
} else if (s === '\b' || s === '\x7f' || s === '\x1b\x7f' || s === '\x1b\b') {
// backspace or ctrl+h
key.name = 'backspace';
key.meta = s.charAt(0) === '\x1b';
} else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key
key.name = 'escape';
key.meta = s.length === 2;
} else if (s === ' ' || s === '\x1b ') {
key.name = 'space';
key.meta = s.length === 2;
} else if (s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
} else if (s.length === 1 && s >= '0' && s <= '9') {
// number
key.value = Number(s);
key.name = 'number';
} else if (s.length === 1 && '!"#$%&()*+:<>?@^_{|}~'.indexOf(s) !== -1) {
// shift
key.name = s;
key.shift = true;
} else if ('.,-\\/;=[]`\''.indexOf(s) !== -1) {
key.name = s;
} else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter
key.name = s;
} else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter
key.name = s.toLowerCase();
key.shift = true;
} else if ((parts = metaKeyCodeRe.exec(s))) {
// meta+character key
key.name = parts[1].toLowerCase();
key.meta = true;
key.shift = /^[A-Z]$/.test(parts[1]);
} else if ((parts = functionKeyCodeRe.exec(s))) {
// ansi escape sequence
// reassemble the key code leaving out leading \x1b's,
// the modifier key bitflag and any meaningless "1;" sequence
var code = (parts[1] || '')
+ (parts[2] || '')
+ (parts[4] || '')
+ (parts[6] || '');
var modifier = (parts[3] || parts[5] || 1) - 1;
// Parse the key modifier
key.ctrl = !!(modifier & 4);
key.meta = !!(modifier & 10);
key.shift = !!(modifier & 1);
key.code = code;
// Parse the key itself
switch (code) {
/* xterm/gnome ESC O letter */
case 'OP': key.name = 'f1'; break;
case 'OQ': key.name = 'f2'; break;
case 'OR': key.name = 'f3'; break;
case 'OS': key.name = 'f4'; break;
/* xterm/rxvt ESC [ number ~ */
case '[11~': key.name = 'f1'; break;
case '[12~': key.name = 'f2'; break;
case '[13~': key.name = 'f3'; break;
case '[14~': key.name = 'f4'; break;
/* from Cygwin and used in libuv */
case '[[A': key.name = 'f1'; break;
case '[[B': key.name = 'f2'; break;
case '[[C': key.name = 'f3'; break;
case '[[D': key.name = 'f4'; break;
case '[[E': key.name = 'f5'; break;
/* common */
case '[15~': key.name = 'f5'; break;
case '[17~': key.name = 'f6'; break;
case '[18~': key.name = 'f7'; break;
case '[19~': key.name = 'f8'; break;
case '[20~': key.name = 'f9'; break;
case '[21~': key.name = 'f10'; break;
case '[23~': key.name = 'f11'; break;
case '[24~': key.name = 'f12'; break;
/* xterm ESC [ letter */
case '[A': key.name = 'up'; break;
case '[B': key.name = 'down'; break;
case '[C': key.name = 'right'; break;
case '[D': key.name = 'left'; break;
case '[E': key.name = 'clear'; break;
case '[F': key.name = 'end'; break;
case '[H': key.name = 'home'; break;
/* xterm/gnome ESC O letter */
case 'OA': key.name = 'up'; break;
case 'OB': key.name = 'down'; break;
case 'OC': key.name = 'right'; break;
case 'OD': key.name = 'left'; break;
case 'OE': key.name = 'clear'; break;
case 'OF': key.name = 'end'; break;
case 'OH': key.name = 'home'; break;
/* xterm/rxvt ESC [ number ~ */
case '[1~': key.name = 'home'; break;
case '[2~': key.name = 'insert'; break;
case '[3~': key.name = 'delete'; break;
case '[4~': key.name = 'end'; break;
case '[5~': key.name = 'pageup'; break;
case '[6~': key.name = 'pagedown'; break;
/* putty */
case '[[5~': key.name = 'pageup'; break;
case '[[6~': key.name = 'pagedown'; break;
/* rxvt */
case '[7~': key.name = 'home'; break;
case '[8~': key.name = 'end'; break;
/* rxvt keys with modifiers */
case '[a': key.name = 'up'; key.shift = true; break;
case '[b': key.name = 'down'; key.shift = true; break;
case '[c': key.name = 'right'; key.shift = true; break;
case '[d': key.name = 'left'; key.shift = true; break;
case '[e': key.name = 'clear'; key.shift = true; break;
case '[2$': key.name = 'insert'; key.shift = true; break;
case '[3$': key.name = 'delete'; key.shift = true; break;
case '[5$': key.name = 'pageup'; key.shift = true; break;
case '[6$': key.name = 'pagedown'; key.shift = true; break;
case '[7$': key.name = 'home'; key.shift = true; break;
case '[8$': key.name = 'end'; key.shift = true; break;
case 'Oa': key.name = 'up'; key.ctrl = true; break;
case 'Ob': key.name = 'down'; key.ctrl = true; break;
case 'Oc': key.name = 'right'; key.ctrl = true; break;
case 'Od': key.name = 'left'; key.ctrl = true; break;
case 'Oe': key.name = 'clear'; key.ctrl = true; break;
case '[2^': key.name = 'insert'; key.ctrl = true; break;
case '[3^': key.name = 'delete'; key.ctrl = true; break;
case '[5^': key.name = 'pageup'; key.ctrl = true; break;
case '[6^': key.name = 'pagedown'; key.ctrl = true; break;
case '[7^': key.name = 'home'; key.ctrl = true; break;
case '[8^': key.name = 'end'; key.ctrl = true; break;
/* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break;
default:
key.name = 'undefined';
break;
}
} else if (s.length > 1 && s[0] !== '\x1b') {
// Got a longer-than-one string of characters.
// Probably a paste, since it wasn't a control sequence.
for (var i = 0; i < s.length; i++) {
events = events.concat(utils.normalize(s[i]));
}
}
// XXX: this "mouse" parsing code is NOT part of the node-core standard
// `readline.js` module, and is a `keypress` module non-standard extension.
if (key.code === '[M') {
key.name = 'mouse';
s = key.sequence;
var b = s.charCodeAt(3);
key.x = s.charCodeAt(4) - parseInt('040', 8);
key.y = s.charCodeAt(5) - parseInt('040', 8);
key.scroll = 0;
key.ctrl = !!((1 << 4) & b);
key.meta = !!((1 << 3) & b);
key.shift = !!((1 << 2) & b);
key.release = (3 & b) === 3;
if ((1 << 6) & b) {
// scroll
key.scroll = 1 & b ? 1 : -1;
}
if (!key.release && !key.scroll) {
key.button = b & 3;
}
}
if (key.name === 'p' && key.ctrl && !key.shift && !key.meta) {
key.name = 'up';
}
if (key.name === 'n' && key.ctrl && !key.shift && !key.meta) {
key.name = 'down';
}
// Don't emit a key if no name was found
if (!key.name) {
return events;
}
if (s.length === 1) {
ch = s;
}
events.push({ch: ch, key: key});
return events;
};
utils.emitKeypress = function(emitter, s, key) {
var events = utils.normalize(s, key);
for (var i = 0; i < events.length; i++) {
var event = events[i];
if (event.key && event.key.name === 'mouse') {
emitter.emit('mousepress', event.key);
} else {
emitter.emit('keypress', event.key.name, event.key);
emitter.emit(event.key.name, event.key);
}
}
};
utils.key = {
up: '\u001b[A',
down: '\u001b[B',
left: '\u001b[D',
right: '\u001b[C',
ctrlc: '\u0003'
};
function last(arr) {
return arr[arr.length - 1];
}
function isNumber(n) {
return isNum(n) && String(n).trim() !== '';
}
function toNumber(n) {
return isNumber(n) ? Number(n) : 1;
}