5 import { IInputHandler, ITerminal } from './Interfaces';
6 import { C0 } from './EscapeSequences';
7 import { DEFAULT_CHARSET } from './Charsets';
10 * The terminal's standard implementation of IInputHandler, this handles all
11 * input from the Parser.
13 * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand
14 * each function's header comment.
16 export class InputHandler implements IInputHandler {
17 // TODO: We want to type _terminal when it's pulled into TS
18 constructor(private _terminal: any) { }
20 public addChar(char: string, code: number): void {
22 // calculate print space
23 // expensive call, therefore we save width in line buffer
24 const ch_width = wcwidth(code);
26 if (this._terminal.charset && this._terminal.charset[char]) {
27 char = this._terminal.charset[char];
30 let row = this._terminal.buffer.y + this._terminal.buffer.ybase;
32 // insert combining char in last cell
33 // FIXME: needs handling after cursor jumps
34 if (!ch_width && this._terminal.buffer.x) {
36 if (this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1]) {
37 if (!this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][2]) {
39 // found empty cell after fullwidth, need to go 2 cells back
40 if (this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 2])
41 this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 2][1] += char;
44 this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][1] += char;
46 this._terminal.updateRange(this._terminal.buffer.y);
51 // goto next line if ch would overflow
52 // TODO: needs a global min terminal width of 2
53 if (this._terminal.buffer.x + ch_width - 1 >= this._terminal.cols) {
55 if (this._terminal.wraparoundMode) {
56 this._terminal.buffer.x = 0;
57 this._terminal.buffer.y++;
58 if (this._terminal.buffer.y > this._terminal.buffer.scrollBottom) {
59 this._terminal.buffer.y--;
60 this._terminal.scroll(true);
62 // The line already exists (eg. the initial viewport), mark it as a
64 this._terminal.buffer.lines.get(this._terminal.buffer.y).isWrapped = true;
67 if (ch_width === 2) // FIXME: check for xterm behavior
71 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
73 // insert mode: move characters to right
74 if (this._terminal.insertMode) {
75 // do this twice for a fullwidth char
76 for (let moves = 0; moves < ch_width; ++moves) {
77 // remove last cell, if it's width is 0
78 // we have to adjust the second last cell as well
79 const removed = this._terminal.buffer.lines.get(this._terminal.buffer.y + this._terminal.buffer.ybase).pop();
81 && this._terminal.buffer.lines.get(row)[this._terminal.cols - 2]
82 && this._terminal.buffer.lines.get(row)[this._terminal.cols - 2][2] === 2) {
83 this._terminal.buffer.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
86 // insert empty cell at cursor
87 this._terminal.buffer.lines.get(row).splice(this._terminal.buffer.x, 0, [this._terminal.curAttr, ' ', 1]);
91 this._terminal.buffer.lines.get(row)[this._terminal.buffer.x] = [this._terminal.curAttr, char, ch_width];
92 this._terminal.buffer.x++;
93 this._terminal.updateRange(this._terminal.buffer.y);
95 // fullwidth char - set next cell width to zero and advance cursor
97 this._terminal.buffer.lines.get(row)[this._terminal.buffer.x] = [this._terminal.curAttr, '', 0];
98 this._terminal.buffer.x++;
107 public bell(): void {
108 if (!this._terminal.visualBell) {
111 this._terminal.element.style.borderColor = 'white';
112 setTimeout(() => this._terminal.element.style.borderColor = '', 10);
113 if (this._terminal.popOnBell) {
114 this._terminal.focus();
120 * Line Feed or New Line (NL). (LF is Ctrl-J).
122 public lineFeed(): void {
123 if (this._terminal.convertEol) {
124 this._terminal.buffer.x = 0;
126 this._terminal.buffer.y++;
127 if (this._terminal.buffer.y > this._terminal.buffer.scrollBottom) {
128 this._terminal.buffer.y--;
129 this._terminal.scroll();
131 // If the end of the line is hit, prevent this action from wrapping around to the next line.
132 if (this._terminal.buffer.x >= this._terminal.cols) {
133 this._terminal.buffer.x--;
136 * This event is emitted whenever the terminal outputs a LF or NL.
140 this._terminal.emit('lineFeed');
145 * Carriage Return (Ctrl-M).
147 public carriageReturn(): void {
148 this._terminal.buffer.x = 0;
153 * Backspace (Ctrl-H).
155 public backspace(): void {
156 if (this._terminal.buffer.x > 0) {
157 this._terminal.buffer.x--;
163 * Horizontal Tab (HT) (Ctrl-I).
166 this._terminal.buffer.x = this._terminal.nextStop();
171 * Shift Out (Ctrl-N) -> Switch to Alternate Character Set. This invokes the
174 public shiftOut(): void {
175 this._terminal.setgLevel(1);
180 * Shift In (Ctrl-O) -> Switch to Standard Character Set. This invokes the G0
181 * character set (the default).
183 public shiftIn(): void {
184 this._terminal.setgLevel(0);
189 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
191 public insertChars(params: number[]): void {
192 let param, row, j, ch;
195 if (param < 1) param = 1;
197 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
198 j = this._terminal.buffer.x;
199 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
201 while (param-- && j < this._terminal.cols) {
202 this._terminal.buffer.lines.get(row).splice(j++, 0, ch);
203 this._terminal.buffer.lines.get(row).pop();
209 * Cursor Up Ps Times (default = 1) (CUU).
211 public cursorUp(params: number[]): void {
212 let param = params[0];
216 this._terminal.buffer.y -= param;
217 if (this._terminal.buffer.y < 0) {
218 this._terminal.buffer.y = 0;
224 * Cursor Down Ps Times (default = 1) (CUD).
226 public cursorDown(params: number[]) {
227 let param = params[0];
231 this._terminal.buffer.y += param;
232 if (this._terminal.buffer.y >= this._terminal.rows) {
233 this._terminal.buffer.y = this._terminal.rows - 1;
235 // If the end of the line is hit, prevent this action from wrapping around to the next line.
236 if (this._terminal.buffer.x >= this._terminal.cols) {
237 this._terminal.buffer.x--;
243 * Cursor Forward Ps Times (default = 1) (CUF).
245 public cursorForward(params: number[]) {
246 let param = params[0];
250 this._terminal.buffer.x += param;
251 if (this._terminal.buffer.x >= this._terminal.cols) {
252 this._terminal.buffer.x = this._terminal.cols - 1;
258 * Cursor Backward Ps Times (default = 1) (CUB).
260 public cursorBackward(params: number[]) {
261 let param = params[0];
265 // If the end of the line is hit, prevent this action from wrapping around to the next line.
266 if (this._terminal.buffer.x >= this._terminal.cols) {
267 this._terminal.buffer.x--;
269 this._terminal.buffer.x -= param;
270 if (this._terminal.buffer.x < 0) {
271 this._terminal.buffer.x = 0;
277 * Cursor Next Line Ps Times (default = 1) (CNL).
280 public cursorNextLine(params: number[]): void {
281 let param = params[0];
285 this._terminal.buffer.y += param;
286 if (this._terminal.buffer.y >= this._terminal.rows) {
287 this._terminal.buffer.y = this._terminal.rows - 1;
289 this._terminal.buffer.x = 0;
295 * Cursor Preceding Line Ps Times (default = 1) (CNL).
298 public cursorPrecedingLine(params: number[]): void {
299 let param = params[0];
303 this._terminal.buffer.y -= param;
304 if (this._terminal.buffer.y < 0) {
305 this._terminal.buffer.y = 0;
307 this._terminal.buffer.x = 0;
313 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
315 public cursorCharAbsolute(params: number[]): void {
316 let param = params[0];
320 this._terminal.buffer.x = param - 1;
325 * Cursor Position [row;column] (default = [1,1]) (CUP).
327 public cursorPosition(params: number[]): void {
332 if (params.length >= 2) {
340 } else if (row >= this._terminal.rows) {
341 row = this._terminal.rows - 1;
346 } else if (col >= this._terminal.cols) {
347 col = this._terminal.cols - 1;
350 this._terminal.buffer.x = col;
351 this._terminal.buffer.y = row;
356 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
358 public cursorForwardTab(params: number[]): void {
359 let param = params[0] || 1;
361 this._terminal.buffer.x = this._terminal.nextStop();
366 * CSI Ps J Erase in Display (ED).
367 * Ps = 0 -> Erase Below (default).
368 * Ps = 1 -> Erase Above.
369 * Ps = 2 -> Erase All.
370 * Ps = 3 -> Erase Saved Lines (xterm).
372 * Erase in Display (DECSED).
373 * Ps = 0 -> Selective Erase Below (default).
374 * Ps = 1 -> Selective Erase Above.
375 * Ps = 2 -> Selective Erase All.
377 public eraseInDisplay(params: number[]): void {
381 this._terminal.eraseRight(this._terminal.buffer.x, this._terminal.buffer.y);
382 j = this._terminal.buffer.y + 1;
383 for (; j < this._terminal.rows; j++) {
384 this._terminal.eraseLine(j);
388 this._terminal.eraseLeft(this._terminal.buffer.x, this._terminal.buffer.y);
389 j = this._terminal.buffer.y;
391 this._terminal.eraseLine(j);
395 j = this._terminal.rows;
396 while (j--) this._terminal.eraseLine(j);
399 // Clear scrollback (everything not in viewport)
400 const scrollBackSize = this._terminal.buffer.lines.length - this._terminal.rows;
401 if (scrollBackSize > 0) {
402 this._terminal.buffer.lines.trimStart(scrollBackSize);
403 this._terminal.buffer.ybase = Math.max(this._terminal.buffer.ybase - scrollBackSize, 0);
404 this._terminal.buffer.ydisp = Math.max(this._terminal.buffer.ydisp - scrollBackSize, 0);
405 // Force a scroll event to refresh viewport
406 this._terminal.emit('scroll', 0);
413 * CSI Ps K Erase in Line (EL).
414 * Ps = 0 -> Erase to Right (default).
415 * Ps = 1 -> Erase to Left.
416 * Ps = 2 -> Erase All.
418 * Erase in Line (DECSEL).
419 * Ps = 0 -> Selective Erase to Right (default).
420 * Ps = 1 -> Selective Erase to Left.
421 * Ps = 2 -> Selective Erase All.
423 public eraseInLine(params: number[]): void {
426 this._terminal.eraseRight(this._terminal.buffer.x, this._terminal.buffer.y);
429 this._terminal.eraseLeft(this._terminal.buffer.x, this._terminal.buffer.y);
432 this._terminal.eraseLine(this._terminal.buffer.y);
439 * Insert Ps Line(s) (default = 1) (IL).
441 public insertLines(params: number[]): void {
448 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
450 j = this._terminal.rows - 1 - this._terminal.buffer.scrollBottom;
451 j = this._terminal.rows - 1 + this._terminal.buffer.ybase - j + 1;
454 if (this._terminal.buffer.lines.length === this._terminal.buffer.lines.maxLength) {
455 // Trim the start of lines to make room for the new line
456 this._terminal.buffer.lines.trimStart(1);
457 this._terminal.buffer.ybase--;
458 this._terminal.buffer.ydisp--;
462 // test: echo -e '\e[44m\e[1L\e[0m'
463 // blankLine(true) - xterm/linux behavior
464 this._terminal.buffer.lines.splice(row, 0, this._terminal.blankLine(true));
465 this._terminal.buffer.lines.splice(j, 1);
469 this._terminal.updateRange(this._terminal.buffer.y);
470 this._terminal.updateRange(this._terminal.buffer.scrollBottom);
475 * Delete Ps Line(s) (default = 1) (DL).
477 public deleteLines(params: number[]): void {
484 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
486 j = this._terminal.rows - 1 - this._terminal.buffer.scrollBottom;
487 j = this._terminal.rows - 1 + this._terminal.buffer.ybase - j;
490 if (this._terminal.buffer.lines.length === this._terminal.buffer.lines.maxLength) {
491 // Trim the start of lines to make room for the new line
492 this._terminal.buffer.lines.trimStart(1);
493 this._terminal.buffer.ybase -= 1;
494 this._terminal.buffer.ydisp -= 1;
496 // test: echo -e '\e[44m\e[1M\e[0m'
497 // blankLine(true) - xterm/linux behavior
498 this._terminal.buffer.lines.splice(j + 1, 0, this._terminal.blankLine(true));
499 this._terminal.buffer.lines.splice(row, 1);
503 this._terminal.updateRange(this._terminal.buffer.y);
504 this._terminal.updateRange(this._terminal.buffer.scrollBottom);
509 * Delete Ps Character(s) (default = 1) (DCH).
511 public deleteChars(params: number[]): void {
519 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
520 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
523 this._terminal.buffer.lines.get(row).splice(this._terminal.buffer.x, 1);
524 this._terminal.buffer.lines.get(row).push(ch);
529 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
531 public scrollUp(params: number[]): void {
532 let param = params[0] || 1;
534 this._terminal.buffer.lines.splice(this._terminal.buffer.ybase + this._terminal.buffer.scrollTop, 1);
535 this._terminal.buffer.lines.splice(this._terminal.buffer.ybase + this._terminal.buffer.scrollBottom, 0, this._terminal.blankLine());
538 this._terminal.updateRange(this._terminal.buffer.scrollTop);
539 this._terminal.updateRange(this._terminal.buffer.scrollBottom);
543 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
545 public scrollDown(params: number[]): void {
546 let param = params[0] || 1;
548 this._terminal.buffer.lines.splice(this._terminal.buffer.ybase + this._terminal.buffer.scrollBottom, 1);
549 this._terminal.buffer.lines.splice(this._terminal.buffer.ybase + this._terminal.buffer.scrollTop, 0, this._terminal.blankLine());
552 this._terminal.updateRange(this._terminal.buffer.scrollTop);
553 this._terminal.updateRange(this._terminal.buffer.scrollBottom);
558 * Erase Ps Character(s) (default = 1) (ECH).
560 public eraseChars(params: number[]): void {
561 let param, row, j, ch;
568 row = this._terminal.buffer.y + this._terminal.buffer.ybase;
569 j = this._terminal.buffer.x;
570 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
572 while (param-- && j < this._terminal.cols) {
573 this._terminal.buffer.lines.get(row)[j++] = ch;
578 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
580 public cursorBackwardTab(params: number[]): void {
581 let param = params[0] || 1;
583 this._terminal.buffer.x = this._terminal.prevStop();
588 * CSI Pm ` Character Position Absolute
589 * [column] (default = [row,1]) (HPA).
591 public charPosAbsolute(params: number[]): void {
592 let param = params[0];
596 this._terminal.buffer.x = param - 1;
597 if (this._terminal.buffer.x >= this._terminal.cols) {
598 this._terminal.buffer.x = this._terminal.cols - 1;
603 * CSI Pm a Character Position Relative
604 * [columns] (default = [row,col+1]) (HPR)
607 public HPositionRelative(params: number[]): void {
608 let param = params[0];
612 this._terminal.buffer.x += param;
613 if (this._terminal.buffer.x >= this._terminal.cols) {
614 this._terminal.buffer.x = this._terminal.cols - 1;
619 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
621 public repeatPrecedingCharacter(params: number[]): void {
622 let param = params[0] || 1
623 , line = this._terminal.buffer.lines.get(this._terminal.buffer.ybase + this._terminal.buffer.y)
624 , ch = line[this._terminal.buffer.x - 1] || [this._terminal.defAttr, ' ', 1];
627 line[this._terminal.buffer.x++] = ch;
632 * CSI Ps c Send Device Attributes (Primary DA).
633 * Ps = 0 or omitted -> request attributes from terminal. The
634 * response depends on the decTerminalID resource setting.
635 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
636 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
637 * -> CSI ? 6 c (``VT102'')
638 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
639 * The VT100-style response parameters do not mean anything by
640 * themselves. VT220 parameters do, telling the host what fea-
641 * tures the terminal supports:
642 * Ps = 1 -> 132-columns.
644 * Ps = 6 -> Selective erase.
645 * Ps = 8 -> User-defined keys.
646 * Ps = 9 -> National replacement character sets.
647 * Ps = 1 5 -> Technical characters.
648 * Ps = 2 2 -> ANSI color, e.g., VT525.
649 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
651 * Send Device Attributes (Secondary DA).
652 * Ps = 0 or omitted -> request the terminal's identification
653 * code. The response depends on the decTerminalID resource set-
654 * ting. It should apply only to VT220 and up, but xterm extends
656 * -> CSI > Pp ; Pv ; Pc c
657 * where Pp denotes the terminal type
658 * Pp = 0 -> ``VT100''.
659 * Pp = 1 -> ``VT220''.
660 * and Pv is the firmware version (for xterm, this was originally
661 * the XFree86 patch number, starting with 95). In a DEC termi-
662 * nal, Pc indicates the ROM cartridge registration number and is
665 * xterm/charproc.c - line 2012, for more information.
666 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
668 public sendDeviceAttributes(params: number[]): void {
673 if (!this._terminal.prefix) {
674 if (this._terminal.is('xterm') || this._terminal.is('rxvt-unicode') || this._terminal.is('screen')) {
675 this._terminal.send(C0.ESC + '[?1;2c');
676 } else if (this._terminal.is('linux')) {
677 this._terminal.send(C0.ESC + '[?6c');
679 } else if (this._terminal.prefix === '>') {
682 // out around ~370 times (?).
683 if (this._terminal.is('xterm')) {
684 this._terminal.send(C0.ESC + '[>0;276;0c');
685 } else if (this._terminal.is('rxvt-unicode')) {
686 this._terminal.send(C0.ESC + '[>85;95;0c');
687 } else if (this._terminal.is('linux')) {
688 // not supported by linux console.
689 // linux console echoes parameters.
690 this._terminal.send(params[0] + 'c');
691 } else if (this._terminal.is('screen')) {
692 this._terminal.send(C0.ESC + '[>83;40003;0c');
698 * CSI Pm d Vertical Position Absolute (VPA)
699 * [row] (default = [1,column])
701 public linePosAbsolute(params: number[]): void {
702 let param = params[0];
706 this._terminal.buffer.y = param - 1;
707 if (this._terminal.buffer.y >= this._terminal.rows) {
708 this._terminal.buffer.y = this._terminal.rows - 1;
713 * CSI Pm e Vertical Position Relative (VPR)
714 * [rows] (default = [row+1,column])
717 public VPositionRelative(params: number[]): void {
718 let param = params[0];
722 this._terminal.buffer.y += param;
723 if (this._terminal.buffer.y >= this._terminal.rows) {
724 this._terminal.buffer.y = this._terminal.rows - 1;
726 // If the end of the line is hit, prevent this action from wrapping around to the next line.
727 if (this._terminal.buffer.x >= this._terminal.cols) {
728 this._terminal.buffer.x--;
734 * Horizontal and Vertical Position [row;column] (default =
737 public HVPosition(params: number[]): void {
738 if (params[0] < 1) params[0] = 1;
739 if (params[1] < 1) params[1] = 1;
741 this._terminal.buffer.y = params[0] - 1;
742 if (this._terminal.buffer.y >= this._terminal.rows) {
743 this._terminal.buffer.y = this._terminal.rows - 1;
746 this._terminal.buffer.x = params[1] - 1;
747 if (this._terminal.buffer.x >= this._terminal.cols) {
748 this._terminal.buffer.x = this._terminal.cols - 1;
753 * CSI Ps g Tab Clear (TBC).
754 * Ps = 0 -> Clear Current Column (default).
755 * Ps = 3 -> Clear All.
757 * Ps = 2 -> Clear Stops on Line.
758 * http://vt100.net/annarbor/aaa-ug/section6.html
760 public tabClear(params: number[]): void {
761 let param = params[0];
763 delete this._terminal.buffer.tabs[this._terminal.buffer.x];
764 } else if (param === 3) {
765 this._terminal.buffer.tabs = {};
770 * CSI Pm h Set Mode (SM).
771 * Ps = 2 -> Keyboard Action Mode (AM).
772 * Ps = 4 -> Insert Mode (IRM).
773 * Ps = 1 2 -> Send/receive (SRM).
774 * Ps = 2 0 -> Automatic Newline (LNM).
776 * DEC Private Mode Set (DECSET).
777 * Ps = 1 -> Application Cursor Keys (DECCKM).
778 * Ps = 2 -> Designate USASCII for character sets G0-G3
779 * (DECANM), and set VT100 mode.
780 * Ps = 3 -> 132 Column Mode (DECCOLM).
781 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
782 * Ps = 5 -> Reverse Video (DECSCNM).
783 * Ps = 6 -> Origin Mode (DECOM).
784 * Ps = 7 -> Wraparound Mode (DECAWM).
785 * Ps = 8 -> Auto-repeat Keys (DECARM).
786 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
787 * tion Mouse Tracking.
788 * Ps = 1 0 -> Show toolbar (rxvt).
789 * Ps = 1 2 -> Start Blinking Cursor (att610).
790 * Ps = 1 8 -> Print form feed (DECPFF).
791 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
792 * Ps = 2 5 -> Show Cursor (DECTCEM).
793 * Ps = 3 0 -> Show scrollbar (rxvt).
794 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
795 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
796 * Ps = 4 0 -> Allow 80 -> 132 Mode.
797 * Ps = 4 1 -> more(1) fix (see curses resource).
798 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
800 * Ps = 4 4 -> Turn On Margin Bell.
801 * Ps = 4 5 -> Reverse-wraparound Mode.
802 * Ps = 4 6 -> Start Logging. This is normally disabled by a
803 * compile-time option.
804 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
805 * abled by the titeInhibit resource).
806 * Ps = 6 6 -> Application keypad (DECNKM).
807 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
808 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
809 * release. See the section Mouse Tracking.
810 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
811 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
812 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
813 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
814 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
815 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
816 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
817 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
818 * (enables the eightBitInput resource).
819 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
820 * Lock keys. (This enables the numLock resource).
821 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
822 * enables the metaSendsEscape resource).
823 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
825 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
826 * enables the altSendsEscape resource).
827 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
828 * (This enables the keepSelection resource).
829 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
830 * the selectToClipboard resource).
831 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
832 * Control-G is received. (This enables the bellIsUrgent
834 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
835 * is received. (enables the popOnBell resource).
836 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
837 * disabled by the titeInhibit resource).
838 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
839 * abled by the titeInhibit resource).
840 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
841 * Screen Buffer, clearing it first. (This may be disabled by
842 * the titeInhibit resource). This combines the effects of the 1
843 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
844 * applications rather than the 4 7 mode.
845 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
846 * Ps = 1 0 5 1 -> Set Sun function-key mode.
847 * Ps = 1 0 5 2 -> Set HP function-key mode.
848 * Ps = 1 0 5 3 -> Set SCO function-key mode.
849 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
850 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
851 * Ps = 2 0 0 4 -> Set bracketed paste mode.
853 * http: *vt100.net/docs/vt220-rm/chapter4.html
855 public setMode(params: number[]): void {
856 if (params.length > 1) {
857 for (let i = 0; i < params.length; i++) {
858 this.setMode([params[i]]);
864 if (!this._terminal.prefix) {
867 this._terminal.insertMode = true;
870 // this._terminal.convertEol = true;
873 } else if (this._terminal.prefix === '?') {
876 this._terminal.applicationCursor = true;
879 this._terminal.setgCharset(0, DEFAULT_CHARSET);
880 this._terminal.setgCharset(1, DEFAULT_CHARSET);
881 this._terminal.setgCharset(2, DEFAULT_CHARSET);
882 this._terminal.setgCharset(3, DEFAULT_CHARSET);
883 // set VT100 mode here
885 case 3: // 132 col mode
886 this._terminal.savedCols = this._terminal.cols;
887 this._terminal.resize(132, this._terminal.rows);
890 this._terminal.originMode = true;
893 this._terminal.wraparoundMode = true;
896 // this.cursorBlink = true;
899 this._terminal.log('Serial port requested application keypad.');
900 this._terminal.applicationKeypad = true;
901 this._terminal.viewport.syncScrollArea();
904 // no release, no motion, no wheel, no modifiers.
905 case 1000: // vt200 mouse
907 // no modifiers, except control on the wheel.
908 case 1002: // button event mouse
909 case 1003: // any event mouse
910 // any event - sends motion events,
911 // even if there is no button held down.
913 // TODO: Why are params[0] compares nested within a switch for params[0]?
915 this._terminal.x10Mouse = params[0] === 9;
916 this._terminal.vt200Mouse = params[0] === 1000;
917 this._terminal.normalMouse = params[0] > 1000;
918 this._terminal.mouseEvents = true;
919 this._terminal.element.classList.add('enable-mouse-events');
920 this._terminal.selectionManager.disable();
921 this._terminal.log('Binding to mouse events.');
923 case 1004: // send focusin/focusout events
926 this._terminal.sendFocus = true;
928 case 1005: // utf8 ext mode mouse
929 this._terminal.utfMouse = true;
930 // for wide terminals
931 // simply encodes large values as utf8 characters
933 case 1006: // sgr ext mode mouse
934 this._terminal.sgrMouse = true;
935 // for wide terminals
936 // does not add 32 to fields
938 // release: ^[[<b;x;ym
940 case 1015: // urxvt ext mode mouse
941 this._terminal.urxvtMouse = true;
942 // for wide terminals
943 // numbers for fields
947 case 25: // show cursor
948 this._terminal.cursorHidden = false;
950 case 1049: // alt screen buffer cursor
951 // TODO: Not sure if we need to save/restore after switching the buffer
952 // this.saveCursor(params);
954 case 47: // alt screen buffer
955 case 1047: // alt screen buffer
956 this._terminal.buffers.activateAltBuffer();
957 this._terminal.reset();
958 this._terminal.viewport.syncScrollArea();
959 this._terminal.showCursor();
966 * CSI Pm l Reset Mode (RM).
967 * Ps = 2 -> Keyboard Action Mode (AM).
968 * Ps = 4 -> Replace Mode (IRM).
969 * Ps = 1 2 -> Send/receive (SRM).
970 * Ps = 2 0 -> Normal Linefeed (LNM).
972 * DEC Private Mode Reset (DECRST).
973 * Ps = 1 -> Normal Cursor Keys (DECCKM).
974 * Ps = 2 -> Designate VT52 mode (DECANM).
975 * Ps = 3 -> 80 Column Mode (DECCOLM).
976 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
977 * Ps = 5 -> Normal Video (DECSCNM).
978 * Ps = 6 -> Normal Cursor Mode (DECOM).
979 * Ps = 7 -> No Wraparound Mode (DECAWM).
980 * Ps = 8 -> No Auto-repeat Keys (DECARM).
981 * Ps = 9 -> Don't send Mouse X & Y on button press.
982 * Ps = 1 0 -> Hide toolbar (rxvt).
983 * Ps = 1 2 -> Stop Blinking Cursor (att610).
984 * Ps = 1 8 -> Don't print form feed (DECPFF).
985 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
986 * Ps = 2 5 -> Hide Cursor (DECTCEM).
987 * Ps = 3 0 -> Don't show scrollbar (rxvt).
988 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
989 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
990 * Ps = 4 1 -> No more(1) fix (see curses resource).
991 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
993 * Ps = 4 4 -> Turn Off Margin Bell.
994 * Ps = 4 5 -> No Reverse-wraparound Mode.
995 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
996 * compile-time option).
997 * Ps = 4 7 -> Use Normal Screen Buffer.
998 * Ps = 6 6 -> Numeric keypad (DECNKM).
999 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
1000 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
1001 * release. See the section Mouse Tracking.
1002 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
1003 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
1004 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
1005 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
1006 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
1007 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
1009 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
1010 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
1011 * the eightBitInput resource).
1012 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
1013 * Lock keys. (This disables the numLock resource).
1014 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
1015 * (This disables the metaSendsEscape resource).
1016 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
1018 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
1019 * (This disables the altSendsEscape resource).
1020 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
1021 * (This disables the keepSelection resource).
1022 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
1023 * the selectToClipboard resource).
1024 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
1025 * Control-G is received. (This disables the bellIsUrgent
1027 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
1028 * G is received. (This disables the popOnBell resource).
1029 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
1030 * first if in the Alternate Screen. (This may be disabled by
1031 * the titeInhibit resource).
1032 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
1033 * disabled by the titeInhibit resource).
1034 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
1035 * as in DECRC. (This may be disabled by the titeInhibit
1036 * resource). This combines the effects of the 1 0 4 7 and 1 0
1037 * 4 8 modes. Use this with terminfo-based applications rather
1038 * than the 4 7 mode.
1039 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
1040 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
1041 * Ps = 1 0 5 2 -> Reset HP function-key mode.
1042 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
1043 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
1044 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
1045 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
1047 public resetMode(params: number[]): void {
1048 if (params.length > 1) {
1049 for (let i = 0; i < params.length; i++) {
1050 this.resetMode([params[i]]);
1056 if (!this._terminal.prefix) {
1057 switch (params[0]) {
1059 this._terminal.insertMode = false;
1062 // this._terminal.convertEol = false;
1065 } else if (this._terminal.prefix === '?') {
1066 switch (params[0]) {
1068 this._terminal.applicationCursor = false;
1071 if (this._terminal.cols === 132 && this._terminal.savedCols) {
1072 this._terminal.resize(this._terminal.savedCols, this._terminal.rows);
1074 delete this._terminal.savedCols;
1077 this._terminal.originMode = false;
1080 this._terminal.wraparoundMode = false;
1083 // this.cursorBlink = false;
1086 this._terminal.log('Switching back to normal keypad.');
1087 this._terminal.applicationKeypad = false;
1088 this._terminal.viewport.syncScrollArea();
1090 case 9: // X10 Mouse
1091 case 1000: // vt200 mouse
1092 case 1002: // button event mouse
1093 case 1003: // any event mouse
1094 this._terminal.x10Mouse = false;
1095 this._terminal.vt200Mouse = false;
1096 this._terminal.normalMouse = false;
1097 this._terminal.mouseEvents = false;
1098 this._terminal.element.classList.remove('enable-mouse-events');
1099 this._terminal.selectionManager.enable();
1101 case 1004: // send focusin/focusout events
1102 this._terminal.sendFocus = false;
1104 case 1005: // utf8 ext mode mouse
1105 this._terminal.utfMouse = false;
1107 case 1006: // sgr ext mode mouse
1108 this._terminal.sgrMouse = false;
1110 case 1015: // urxvt ext mode mouse
1111 this._terminal.urxvtMouse = false;
1113 case 25: // hide cursor
1114 this._terminal.cursorHidden = true;
1116 case 1049: // alt screen buffer cursor
1118 case 47: // normal screen buffer
1119 case 1047: // normal screen buffer - clearing it first
1120 // Ensure the selection manager has the correct buffer
1121 this._terminal.buffers.activateNormalBuffer();
1122 // TODO: Not sure if we need to save/restore after switching the buffer
1123 // if (params[0] === 1049) {
1124 // this.restoreCursor(params);
1126 this._terminal.selectionManager.setBuffer(this._terminal.buffer.lines);
1127 this._terminal.refresh(0, this._terminal.rows - 1);
1128 this._terminal.viewport.syncScrollArea();
1129 this._terminal.showCursor();
1136 * CSI Pm m Character Attributes (SGR).
1137 * Ps = 0 -> Normal (default).
1139 * Ps = 4 -> Underlined.
1140 * Ps = 5 -> Blink (appears as Bold).
1141 * Ps = 7 -> Inverse.
1142 * Ps = 8 -> Invisible, i.e., hidden (VT300).
1143 * Ps = 2 2 -> Normal (neither bold nor faint).
1144 * Ps = 2 4 -> Not underlined.
1145 * Ps = 2 5 -> Steady (not blinking).
1146 * Ps = 2 7 -> Positive (not inverse).
1147 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
1148 * Ps = 3 0 -> Set foreground color to Black.
1149 * Ps = 3 1 -> Set foreground color to Red.
1150 * Ps = 3 2 -> Set foreground color to Green.
1151 * Ps = 3 3 -> Set foreground color to Yellow.
1152 * Ps = 3 4 -> Set foreground color to Blue.
1153 * Ps = 3 5 -> Set foreground color to Magenta.
1154 * Ps = 3 6 -> Set foreground color to Cyan.
1155 * Ps = 3 7 -> Set foreground color to White.
1156 * Ps = 3 9 -> Set foreground color to default (original).
1157 * Ps = 4 0 -> Set background color to Black.
1158 * Ps = 4 1 -> Set background color to Red.
1159 * Ps = 4 2 -> Set background color to Green.
1160 * Ps = 4 3 -> Set background color to Yellow.
1161 * Ps = 4 4 -> Set background color to Blue.
1162 * Ps = 4 5 -> Set background color to Magenta.
1163 * Ps = 4 6 -> Set background color to Cyan.
1164 * Ps = 4 7 -> Set background color to White.
1165 * Ps = 4 9 -> Set background color to default (original).
1167 * If 16-color support is compiled, the following apply. Assume
1168 * that xterm's resources are set so that the ISO color codes are
1169 * the first 8 of a set of 16. Then the aixterm colors are the
1170 * bright versions of the ISO colors:
1171 * Ps = 9 0 -> Set foreground color to Black.
1172 * Ps = 9 1 -> Set foreground color to Red.
1173 * Ps = 9 2 -> Set foreground color to Green.
1174 * Ps = 9 3 -> Set foreground color to Yellow.
1175 * Ps = 9 4 -> Set foreground color to Blue.
1176 * Ps = 9 5 -> Set foreground color to Magenta.
1177 * Ps = 9 6 -> Set foreground color to Cyan.
1178 * Ps = 9 7 -> Set foreground color to White.
1179 * Ps = 1 0 0 -> Set background color to Black.
1180 * Ps = 1 0 1 -> Set background color to Red.
1181 * Ps = 1 0 2 -> Set background color to Green.
1182 * Ps = 1 0 3 -> Set background color to Yellow.
1183 * Ps = 1 0 4 -> Set background color to Blue.
1184 * Ps = 1 0 5 -> Set background color to Magenta.
1185 * Ps = 1 0 6 -> Set background color to Cyan.
1186 * Ps = 1 0 7 -> Set background color to White.
1188 * If xterm is compiled with the 16-color support disabled, it
1189 * supports the following, from rxvt:
1190 * Ps = 1 0 0 -> Set foreground and background color to
1193 * If 88- or 256-color support is compiled, the following apply.
1194 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
1196 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
1199 public charAttributes(params: number[]): void {
1200 // Optimize a single SGR0.
1201 if (params.length === 1 && params[0] === 0) {
1202 this._terminal.curAttr = this._terminal.defAttr;
1206 let l = params.length
1208 , flags = this._terminal.curAttr >> 18
1209 , fg = (this._terminal.curAttr >> 9) & 0x1ff
1210 , bg = this._terminal.curAttr & 0x1ff
1213 for (; i < l; i++) {
1215 if (p >= 30 && p <= 37) {
1218 } else if (p >= 40 && p <= 47) {
1221 } else if (p >= 90 && p <= 97) {
1225 } else if (p >= 100 && p <= 107) {
1229 } else if (p === 0) {
1231 flags = this._terminal.defAttr >> 18;
1232 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1233 bg = this._terminal.defAttr & 0x1ff;
1237 } else if (p === 1) {
1240 } else if (p === 4) {
1243 } else if (p === 5) {
1246 } else if (p === 7) {
1247 // inverse and positive
1248 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
1250 } else if (p === 8) {
1253 } else if (p === 22) {
1256 } else if (p === 24) {
1259 } else if (p === 25) {
1262 } else if (p === 27) {
1265 } else if (p === 28) {
1268 } else if (p === 39) {
1270 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1271 } else if (p === 49) {
1273 bg = this._terminal.defAttr & 0x1ff;
1274 } else if (p === 38) {
1276 if (params[i + 1] === 2) {
1278 fg = this._terminal.matchColor(
1280 params[i + 1] & 0xff,
1281 params[i + 2] & 0xff);
1282 if (fg === -1) fg = 0x1ff;
1284 } else if (params[i + 1] === 5) {
1286 p = params[i] & 0xff;
1289 } else if (p === 48) {
1291 if (params[i + 1] === 2) {
1293 bg = this._terminal.matchColor(
1295 params[i + 1] & 0xff,
1296 params[i + 2] & 0xff);
1297 if (bg === -1) bg = 0x1ff;
1299 } else if (params[i + 1] === 5) {
1301 p = params[i] & 0xff;
1304 } else if (p === 100) {
1306 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1307 bg = this._terminal.defAttr & 0x1ff;
1309 this._terminal.error('Unknown SGR attribute: %d.', p);
1313 this._terminal.curAttr = (flags << 18) | (fg << 9) | bg;
1317 * CSI Ps n Device Status Report (DSR).
1318 * Ps = 5 -> Status Report. Result (``OK'') is
1320 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
1324 * Device Status Report (DSR, DEC-specific).
1325 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
1326 * ? r ; c R (assumes page is zero).
1327 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
1328 * or CSI ? 1 1 n (not ready).
1329 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
1330 * or CSI ? 2 1 n (locked).
1331 * Ps = 2 6 -> Report Keyboard status as
1332 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
1333 * The last two parameters apply to VT400 & up, and denote key-
1334 * board ready and LK01 respectively.
1335 * Ps = 5 3 -> Report Locator status as
1336 * CSI ? 5 3 n Locator available, if compiled-in, or
1337 * CSI ? 5 0 n No Locator, if not.
1339 public deviceStatus(params: number[]): void {
1340 if (!this._terminal.prefix) {
1341 switch (params[0]) {
1344 this._terminal.send(C0.ESC + '[0n');
1348 this._terminal.send(C0.ESC + '['
1349 + (this._terminal.buffer.y + 1)
1351 + (this._terminal.buffer.x + 1)
1355 } else if (this._terminal.prefix === '?') {
1356 // modern xterm doesnt seem to
1357 // respond to any of these except ?6, 6, and 5
1358 switch (params[0]) {
1361 this._terminal.send(C0.ESC + '[?'
1362 + (this._terminal.buffer.y + 1)
1364 + (this._terminal.buffer.x + 1)
1369 // this.send(C0.ESC + '[?11n');
1372 // dont support user defined keys
1373 // this.send(C0.ESC + '[?21n');
1376 // north american keyboard
1377 // this.send(C0.ESC + '[?27;1;0;0n');
1380 // no dec locator/mouse
1381 // this.send(C0.ESC + '[?50n');
1388 * CSI ! p Soft terminal reset (DECSTR).
1389 * http://vt100.net/docs/vt220-rm/table4-10.html
1391 public softReset(params: number[]): void {
1392 this._terminal.cursorHidden = false;
1393 this._terminal.insertMode = false;
1394 this._terminal.originMode = false;
1395 this._terminal.wraparoundMode = true; // defaults: xterm - true, vt100 - false
1396 this._terminal.applicationKeypad = false; // ?
1397 this._terminal.viewport.syncScrollArea();
1398 this._terminal.applicationCursor = false;
1399 this._terminal.buffer.scrollTop = 0;
1400 this._terminal.buffer.scrollBottom = this._terminal.rows - 1;
1401 this._terminal.curAttr = this._terminal.defAttr;
1402 this._terminal.buffer.x = this._terminal.buffer.y = 0; // ?
1403 this._terminal.charset = null;
1404 this._terminal.glevel = 0; // ??
1405 this._terminal.charsets = [null]; // ??
1409 * CSI Ps SP q Set cursor style (DECSCUSR, VT520).
1410 * Ps = 0 -> blinking block.
1411 * Ps = 1 -> blinking block (default).
1412 * Ps = 2 -> steady block.
1413 * Ps = 3 -> blinking underline.
1414 * Ps = 4 -> steady underline.
1415 * Ps = 5 -> blinking bar (xterm).
1416 * Ps = 6 -> steady bar (xterm).
1418 public setCursorStyle(params?: number[]): void {
1419 const param = params[0] < 1 ? 1 : params[0];
1423 this._terminal.setOption('cursorStyle', 'block');
1427 this._terminal.setOption('cursorStyle', 'underline');
1431 this._terminal.setOption('cursorStyle', 'bar');
1434 const isBlinking = param % 2 === 1;
1435 this._terminal.setOption('cursorBlink', isBlinking);
1440 * Set Scrolling Region [top;bottom] (default = full size of win-
1444 public setScrollRegion(params: number[]): void {
1445 if (this._terminal.prefix) return;
1446 this._terminal.buffer.scrollTop = (params[0] || 1) - 1;
1447 this._terminal.buffer.scrollBottom = (params[1] && params[1] <= this._terminal.rows ? params[1] : this._terminal.rows) - 1;
1448 this._terminal.buffer.x = 0;
1449 this._terminal.buffer.y = 0;
1455 * Save cursor (ANSI.SYS).
1457 public saveCursor(params: number[]): void {
1458 this._terminal.buffer.savedX = this._terminal.buffer.x;
1459 this._terminal.buffer.savedY = this._terminal.buffer.y;
1465 * Restore cursor (ANSI.SYS).
1467 public restoreCursor(params: number[]): void {
1468 this._terminal.buffer.x = this._terminal.buffer.savedX || 0;
1469 this._terminal.buffer.y = this._terminal.buffer.savedY || 0;
1473 export const wcwidth = (function(opts) {
1474 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1475 // combining characters
1476 const COMBINING_BMP = [
1477 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1478 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1479 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1480 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1481 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1482 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1483 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1484 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1485 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1486 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1487 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1488 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1489 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1490 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1491 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1492 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1493 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1494 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1495 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1496 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1497 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1498 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1499 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1500 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1501 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1502 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1503 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1504 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1505 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1506 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1507 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1508 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1509 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1510 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1511 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1512 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1513 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1514 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1515 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1516 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1517 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1518 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1519 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1521 const COMBINING_HIGH = [
1522 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1523 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1524 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1525 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1529 function bisearch(ucs, data) {
1531 let max = data.length - 1;
1533 if (ucs < data[0][0] || ucs > data[max][1])
1535 while (max >= min) {
1536 mid = (min + max) >> 1;
1537 if (ucs > data[mid][1])
1539 else if (ucs < data[mid][0])
1546 function wcwidthBMP(ucs) {
1547 // test for 8-bit control characters
1550 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1551 return opts.control;
1552 // binary search in table of non-spacing characters
1553 if (bisearch(ucs, COMBINING_BMP))
1555 // if we arrive here, ucs is not a combining or C0/C1 control character
1556 if (isWideBMP(ucs)) {
1561 function isWideBMP(ucs) {
1564 ucs <= 0x115f || // Hangul Jamo init. consonants
1567 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1568 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1569 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1570 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1571 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1572 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1573 (ucs >= 0xffe0 && ucs <= 0xffe6)));
1575 function wcwidthHigh(ucs) {
1576 if (bisearch(ucs, COMBINING_HIGH))
1578 if ((ucs >= 0x20000 && ucs <= 0x2fffd) || (ucs >= 0x30000 && ucs <= 0x3fffd)) {
1583 const control = opts.control | 0;
1585 function init_table() {
1586 // lookup table for BMP
1587 const CODEPOINTS = 65536; // BMP holds 65536 codepoints
1588 const BITWIDTH = 2; // a codepoint can have a width of 0, 1 or 2
1589 const ITEMSIZE = 32; // using uint32_t
1590 const CONTAINERSIZE = CODEPOINTS * BITWIDTH / ITEMSIZE;
1591 const CODEPOINTS_PER_ITEM = ITEMSIZE / BITWIDTH;
1592 table = (typeof Uint32Array === 'undefined')
1593 ? new Array(CONTAINERSIZE)
1594 : new Uint32Array(CONTAINERSIZE);
1595 for (let i = 0; i < CONTAINERSIZE; ++i) {
1597 let pos = CODEPOINTS_PER_ITEM;
1599 num = (num << 2) | wcwidthBMP(CODEPOINTS_PER_ITEM * i + pos);
1604 // get width from lookup table
1605 // position in container : num / CODEPOINTS_PER_ITEM
1606 // ==> n = table[Math.floor(num / 16)]
1607 // ==> n = table[num >> 4]
1608 // 16 codepoints per number: FFEEDDCCBBAA99887766554433221100
1609 // position in number : (num % CODEPOINTS_PER_ITEM) * BITWIDTH
1610 // ==> m = (n % 16) * 2
1611 // ==> m = (num & 15) << 1
1612 // right shift to position m
1613 // ==> n = n >> m e.g. m=12 000000000000FFEEDDCCBBAA99887766
1614 // we are only interested in 2 LSBs, cut off higher bits
1615 // ==> n = n & 3 e.g. 000000000000000000000000000000XX
1616 return function (num) {
1617 num = num | 0; // get asm.js like optimization under V8
1622 let t = table || init_table();
1624 return t[num >> 4] >> ((num & 15) << 1) & 3;
1625 // do a full search for high codepoints
1626 return wcwidthHigh(num);
1628 })({nul: 0, control: 0}); // configurable options