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.y + this._terminal.ybase;
32 // insert combining char in last cell
33 // FIXME: needs handling after cursor jumps
34 if (!ch_width && this._terminal.x) {
36 if (this._terminal.buffer.lines.get(row)[this._terminal.x - 1]) {
37 if (!this._terminal.buffer.lines.get(row)[this._terminal.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.x - 2])
41 this._terminal.buffer.lines.get(row)[this._terminal.x - 2][1] += char;
44 this._terminal.buffer.lines.get(row)[this._terminal.x - 1][1] += char;
46 this._terminal.updateRange(this._terminal.y);
51 // goto next line if ch would overflow
52 // TODO: needs a global min terminal width of 2
53 if (this._terminal.x + ch_width - 1 >= this._terminal.cols) {
55 if (this._terminal.wraparoundMode) {
58 if (this._terminal.y > this._terminal.scrollBottom) {
59 // Insert a new line, scroll and mark as a wrapped line
61 this._terminal.scroll(true);
63 // The line already exists (eg. the initial viewport), mark it as a
65 this._terminal.lines.get(this._terminal.y).isWrapped = true;
68 if (ch_width === 2) // FIXME: check for xterm behavior
72 row = this._terminal.y + this._terminal.ybase;
74 // insert mode: move characters to right
75 if (this._terminal.insertMode) {
76 // do this twice for a fullwidth char
77 for (let moves = 0; moves < ch_width; ++moves) {
78 // remove last cell, if it's width is 0
79 // we have to adjust the second last cell as well
80 const removed = this._terminal.buffer.lines.get(this._terminal.y + this._terminal.ybase).pop();
82 && this._terminal.buffer.lines.get(row)[this._terminal.cols - 2]
83 && this._terminal.buffer.lines.get(row)[this._terminal.cols - 2][2] === 2) {
84 this._terminal.buffer.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
87 // insert empty cell at cursor
88 this._terminal.buffer.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
92 this._terminal.buffer.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, char, ch_width];
94 this._terminal.updateRange(this._terminal.y);
96 // fullwidth char - set next cell width to zero and advance cursor
98 this._terminal.buffer.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, '', 0];
108 public bell(): void {
109 if (!this._terminal.visualBell) {
112 this._terminal.element.style.borderColor = 'white';
113 setTimeout(() => this._terminal.element.style.borderColor = '', 10);
114 if (this._terminal.popOnBell) {
115 this._terminal.focus();
121 * Line Feed or New Line (NL). (LF is Ctrl-J).
123 public lineFeed(): void {
124 if (this._terminal.convertEol) {
125 this._terminal.x = 0;
128 if (this._terminal.y > this._terminal.scrollBottom) {
130 this._terminal.scroll();
132 // If the end of the line is hit, prevent this action from wrapping around to the next line.
133 if (this._terminal.x >= this._terminal.cols) {
140 * Carriage Return (Ctrl-M).
142 public carriageReturn(): void {
143 this._terminal.x = 0;
148 * Backspace (Ctrl-H).
150 public backspace(): void {
151 if (this._terminal.x > 0) {
158 * Horizontal Tab (HT) (Ctrl-I).
161 this._terminal.x = this._terminal.nextStop();
166 * Shift Out (Ctrl-N) -> Switch to Alternate Character Set. This invokes the
169 public shiftOut(): void {
170 this._terminal.setgLevel(1);
175 * Shift In (Ctrl-O) -> Switch to Standard Character Set. This invokes the G0
176 * character set (the default).
178 public shiftIn(): void {
179 this._terminal.setgLevel(0);
184 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
186 public insertChars(params: number[]): void {
187 let param, row, j, ch;
190 if (param < 1) param = 1;
192 row = this._terminal.y + this._terminal.ybase;
193 j = this._terminal.x;
194 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
196 while (param-- && j < this._terminal.cols) {
197 this._terminal.buffer.lines.get(row).splice(j++, 0, ch);
198 this._terminal.buffer.lines.get(row).pop();
204 * Cursor Up Ps Times (default = 1) (CUU).
206 public cursorUp(params: number[]): void {
207 let param = params[0];
211 this._terminal.y -= param;
212 if (this._terminal.y < 0) {
213 this._terminal.y = 0;
219 * Cursor Down Ps Times (default = 1) (CUD).
221 public cursorDown(params: number[]) {
222 let param = params[0];
226 this._terminal.y += param;
227 if (this._terminal.y >= this._terminal.rows) {
228 this._terminal.y = this._terminal.rows - 1;
230 // If the end of the line is hit, prevent this action from wrapping around to the next line.
231 if (this._terminal.x >= this._terminal.cols) {
238 * Cursor Forward Ps Times (default = 1) (CUF).
240 public cursorForward(params: number[]) {
241 let param = params[0];
245 this._terminal.x += param;
246 if (this._terminal.x >= this._terminal.cols) {
247 this._terminal.x = this._terminal.cols - 1;
253 * Cursor Backward Ps Times (default = 1) (CUB).
255 public cursorBackward(params: number[]) {
256 let param = params[0];
260 // If the end of the line is hit, prevent this action from wrapping around to the next line.
261 if (this._terminal.x >= this._terminal.cols) {
264 this._terminal.x -= param;
265 if (this._terminal.x < 0) {
266 this._terminal.x = 0;
272 * Cursor Next Line Ps Times (default = 1) (CNL).
275 public cursorNextLine(params: number[]): void {
276 let param = params[0];
280 this._terminal.y += param;
281 if (this._terminal.y >= this._terminal.rows) {
282 this._terminal.y = this._terminal.rows - 1;
284 this._terminal.x = 0;
290 * Cursor Preceding Line Ps Times (default = 1) (CNL).
293 public cursorPrecedingLine(params: number[]): void {
294 let param = params[0];
298 this._terminal.y -= param;
299 if (this._terminal.y < 0) {
300 this._terminal.y = 0;
302 this._terminal.x = 0;
308 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
310 public cursorCharAbsolute(params: number[]): void {
311 let param = params[0];
315 this._terminal.x = param - 1;
320 * Cursor Position [row;column] (default = [1,1]) (CUP).
322 public cursorPosition(params: number[]): void {
327 if (params.length >= 2) {
335 } else if (row >= this._terminal.rows) {
336 row = this._terminal.rows - 1;
341 } else if (col >= this._terminal.cols) {
342 col = this._terminal.cols - 1;
345 this._terminal.x = col;
346 this._terminal.y = row;
351 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
353 public cursorForwardTab(params: number[]): void {
354 let param = params[0] || 1;
356 this._terminal.x = this._terminal.nextStop();
361 * CSI Ps J Erase in Display (ED).
362 * Ps = 0 -> Erase Below (default).
363 * Ps = 1 -> Erase Above.
364 * Ps = 2 -> Erase All.
365 * Ps = 3 -> Erase Saved Lines (xterm).
367 * Erase in Display (DECSED).
368 * Ps = 0 -> Selective Erase Below (default).
369 * Ps = 1 -> Selective Erase Above.
370 * Ps = 2 -> Selective Erase All.
372 public eraseInDisplay(params: number[]): void {
376 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
377 j = this._terminal.y + 1;
378 for (; j < this._terminal.rows; j++) {
379 this._terminal.eraseLine(j);
383 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
384 j = this._terminal.y;
386 this._terminal.eraseLine(j);
390 j = this._terminal.rows;
391 while (j--) this._terminal.eraseLine(j);
394 // Clear scrollback (everything not in viewport)
395 const scrollBackSize = this._terminal.buffer.lines.length - this._terminal.rows;
396 if (scrollBackSize > 0) {
397 this._terminal.buffer.lines.trimStart(scrollBackSize);
398 this._terminal.ybase = Math.max(this._terminal.ybase - scrollBackSize, 0);
399 this._terminal.ydisp = Math.max(this._terminal.ydisp - scrollBackSize, 0);
400 // Force a scroll event to refresh viewport
401 this._terminal.emit('scroll', 0);
408 * CSI Ps K Erase in Line (EL).
409 * Ps = 0 -> Erase to Right (default).
410 * Ps = 1 -> Erase to Left.
411 * Ps = 2 -> Erase All.
413 * Erase in Line (DECSEL).
414 * Ps = 0 -> Selective Erase to Right (default).
415 * Ps = 1 -> Selective Erase to Left.
416 * Ps = 2 -> Selective Erase All.
418 public eraseInLine(params: number[]): void {
421 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
424 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
427 this._terminal.eraseLine(this._terminal.y);
434 * Insert Ps Line(s) (default = 1) (IL).
436 public insertLines(params: number[]): void {
443 row = this._terminal.y + this._terminal.ybase;
445 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
446 j = this._terminal.rows - 1 + this._terminal.ybase - j + 1;
449 if (this._terminal.buffer.lines.length === this._terminal.buffer.lines.maxLength) {
450 // Trim the start of lines to make room for the new line
451 this._terminal.buffer.lines.trimStart(1);
452 this._terminal.ybase--;
453 this._terminal.ydisp--;
457 // test: echo -e '\e[44m\e[1L\e[0m'
458 // blankLine(true) - xterm/linux behavior
459 this._terminal.buffer.lines.splice(row, 0, this._terminal.blankLine(true));
460 this._terminal.buffer.lines.splice(j, 1);
464 this._terminal.updateRange(this._terminal.y);
465 this._terminal.updateRange(this._terminal.scrollBottom);
470 * Delete Ps Line(s) (default = 1) (DL).
472 public deleteLines(params: number[]): void {
479 row = this._terminal.y + this._terminal.ybase;
481 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
482 j = this._terminal.rows - 1 + this._terminal.ybase - j;
485 if (this._terminal.buffer.lines.length === this._terminal.buffer.lines.maxLength) {
486 // Trim the start of lines to make room for the new line
487 this._terminal.buffer.lines.trimStart(1);
488 this._terminal.ybase -= 1;
489 this._terminal.ydisp -= 1;
491 // test: echo -e '\e[44m\e[1M\e[0m'
492 // blankLine(true) - xterm/linux behavior
493 this._terminal.buffer.lines.splice(j + 1, 0, this._terminal.blankLine(true));
494 this._terminal.buffer.lines.splice(row, 1);
498 this._terminal.updateRange(this._terminal.y);
499 this._terminal.updateRange(this._terminal.scrollBottom);
504 * Delete Ps Character(s) (default = 1) (DCH).
506 public deleteChars(params: number[]): void {
514 row = this._terminal.y + this._terminal.ybase;
515 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
518 this._terminal.buffer.lines.get(row).splice(this._terminal.x, 1);
519 this._terminal.buffer.lines.get(row).push(ch);
524 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
526 public scrollUp(params: number[]): void {
527 let param = params[0] || 1;
529 this._terminal.buffer.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 1);
530 this._terminal.buffer.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 0, this._terminal.blankLine());
533 this._terminal.updateRange(this._terminal.scrollTop);
534 this._terminal.updateRange(this._terminal.scrollBottom);
538 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
540 public scrollDown(params: number[]): void {
541 let param = params[0] || 1;
543 this._terminal.buffer.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 1);
544 this._terminal.buffer.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 0, this._terminal.blankLine());
547 this._terminal.updateRange(this._terminal.scrollTop);
548 this._terminal.updateRange(this._terminal.scrollBottom);
553 * Erase Ps Character(s) (default = 1) (ECH).
555 public eraseChars(params: number[]): void {
556 let param, row, j, ch;
563 row = this._terminal.y + this._terminal.ybase;
564 j = this._terminal.x;
565 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
567 while (param-- && j < this._terminal.cols) {
568 this._terminal.buffer.lines.get(row)[j++] = ch;
573 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
575 public cursorBackwardTab(params: number[]): void {
576 let param = params[0] || 1;
578 this._terminal.x = this._terminal.prevStop();
583 * CSI Pm ` Character Position Absolute
584 * [column] (default = [row,1]) (HPA).
586 public charPosAbsolute(params: number[]): void {
587 let param = params[0];
591 this._terminal.x = param - 1;
592 if (this._terminal.x >= this._terminal.cols) {
593 this._terminal.x = this._terminal.cols - 1;
598 * CSI Pm a Character Position Relative
599 * [columns] (default = [row,col+1]) (HPR)
602 public HPositionRelative(params: number[]): void {
603 let param = params[0];
607 this._terminal.x += param;
608 if (this._terminal.x >= this._terminal.cols) {
609 this._terminal.x = this._terminal.cols - 1;
614 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
616 public repeatPrecedingCharacter(params: number[]): void {
617 let param = params[0] || 1
618 , line = this._terminal.buffer.lines.get(this._terminal.ybase + this._terminal.y)
619 , ch = line[this._terminal.x - 1] || [this._terminal.defAttr, ' ', 1];
622 line[this._terminal.x++] = ch;
627 * CSI Ps c Send Device Attributes (Primary DA).
628 * Ps = 0 or omitted -> request attributes from terminal. The
629 * response depends on the decTerminalID resource setting.
630 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
631 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
632 * -> CSI ? 6 c (``VT102'')
633 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
634 * The VT100-style response parameters do not mean anything by
635 * themselves. VT220 parameters do, telling the host what fea-
636 * tures the terminal supports:
637 * Ps = 1 -> 132-columns.
639 * Ps = 6 -> Selective erase.
640 * Ps = 8 -> User-defined keys.
641 * Ps = 9 -> National replacement character sets.
642 * Ps = 1 5 -> Technical characters.
643 * Ps = 2 2 -> ANSI color, e.g., VT525.
644 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
646 * Send Device Attributes (Secondary DA).
647 * Ps = 0 or omitted -> request the terminal's identification
648 * code. The response depends on the decTerminalID resource set-
649 * ting. It should apply only to VT220 and up, but xterm extends
651 * -> CSI > Pp ; Pv ; Pc c
652 * where Pp denotes the terminal type
653 * Pp = 0 -> ``VT100''.
654 * Pp = 1 -> ``VT220''.
655 * and Pv is the firmware version (for xterm, this was originally
656 * the XFree86 patch number, starting with 95). In a DEC termi-
657 * nal, Pc indicates the ROM cartridge registration number and is
660 * xterm/charproc.c - line 2012, for more information.
661 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
663 public sendDeviceAttributes(params: number[]): void {
668 if (!this._terminal.prefix) {
669 if (this._terminal.is('xterm') || this._terminal.is('rxvt-unicode') || this._terminal.is('screen')) {
670 this._terminal.send(C0.ESC + '[?1;2c');
671 } else if (this._terminal.is('linux')) {
672 this._terminal.send(C0.ESC + '[?6c');
674 } else if (this._terminal.prefix === '>') {
677 // out around ~370 times (?).
678 if (this._terminal.is('xterm')) {
679 this._terminal.send(C0.ESC + '[>0;276;0c');
680 } else if (this._terminal.is('rxvt-unicode')) {
681 this._terminal.send(C0.ESC + '[>85;95;0c');
682 } else if (this._terminal.is('linux')) {
683 // not supported by linux console.
684 // linux console echoes parameters.
685 this._terminal.send(params[0] + 'c');
686 } else if (this._terminal.is('screen')) {
687 this._terminal.send(C0.ESC + '[>83;40003;0c');
693 * CSI Pm d Vertical Position Absolute (VPA)
694 * [row] (default = [1,column])
696 public linePosAbsolute(params: number[]): void {
697 let param = params[0];
701 this._terminal.y = param - 1;
702 if (this._terminal.y >= this._terminal.rows) {
703 this._terminal.y = this._terminal.rows - 1;
708 * CSI Pm e Vertical Position Relative (VPR)
709 * [rows] (default = [row+1,column])
712 public VPositionRelative(params: number[]): void {
713 let param = params[0];
717 this._terminal.y += param;
718 if (this._terminal.y >= this._terminal.rows) {
719 this._terminal.y = this._terminal.rows - 1;
721 // If the end of the line is hit, prevent this action from wrapping around to the next line.
722 if (this._terminal.x >= this._terminal.cols) {
729 * Horizontal and Vertical Position [row;column] (default =
732 public HVPosition(params: number[]): void {
733 if (params[0] < 1) params[0] = 1;
734 if (params[1] < 1) params[1] = 1;
736 this._terminal.y = params[0] - 1;
737 if (this._terminal.y >= this._terminal.rows) {
738 this._terminal.y = this._terminal.rows - 1;
741 this._terminal.x = params[1] - 1;
742 if (this._terminal.x >= this._terminal.cols) {
743 this._terminal.x = this._terminal.cols - 1;
748 * CSI Ps g Tab Clear (TBC).
749 * Ps = 0 -> Clear Current Column (default).
750 * Ps = 3 -> Clear All.
752 * Ps = 2 -> Clear Stops on Line.
753 * http://vt100.net/annarbor/aaa-ug/section6.html
755 public tabClear(params: number[]): void {
756 let param = params[0];
758 delete this._terminal.tabs[this._terminal.x];
759 } else if (param === 3) {
760 this._terminal.tabs = {};
765 * CSI Pm h Set Mode (SM).
766 * Ps = 2 -> Keyboard Action Mode (AM).
767 * Ps = 4 -> Insert Mode (IRM).
768 * Ps = 1 2 -> Send/receive (SRM).
769 * Ps = 2 0 -> Automatic Newline (LNM).
771 * DEC Private Mode Set (DECSET).
772 * Ps = 1 -> Application Cursor Keys (DECCKM).
773 * Ps = 2 -> Designate USASCII for character sets G0-G3
774 * (DECANM), and set VT100 mode.
775 * Ps = 3 -> 132 Column Mode (DECCOLM).
776 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
777 * Ps = 5 -> Reverse Video (DECSCNM).
778 * Ps = 6 -> Origin Mode (DECOM).
779 * Ps = 7 -> Wraparound Mode (DECAWM).
780 * Ps = 8 -> Auto-repeat Keys (DECARM).
781 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
782 * tion Mouse Tracking.
783 * Ps = 1 0 -> Show toolbar (rxvt).
784 * Ps = 1 2 -> Start Blinking Cursor (att610).
785 * Ps = 1 8 -> Print form feed (DECPFF).
786 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
787 * Ps = 2 5 -> Show Cursor (DECTCEM).
788 * Ps = 3 0 -> Show scrollbar (rxvt).
789 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
790 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
791 * Ps = 4 0 -> Allow 80 -> 132 Mode.
792 * Ps = 4 1 -> more(1) fix (see curses resource).
793 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
795 * Ps = 4 4 -> Turn On Margin Bell.
796 * Ps = 4 5 -> Reverse-wraparound Mode.
797 * Ps = 4 6 -> Start Logging. This is normally disabled by a
798 * compile-time option.
799 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
800 * abled by the titeInhibit resource).
801 * Ps = 6 6 -> Application keypad (DECNKM).
802 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
803 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
804 * release. See the section Mouse Tracking.
805 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
806 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
807 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
808 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
809 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
810 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
811 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
812 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
813 * (enables the eightBitInput resource).
814 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
815 * Lock keys. (This enables the numLock resource).
816 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
817 * enables the metaSendsEscape resource).
818 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
820 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
821 * enables the altSendsEscape resource).
822 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
823 * (This enables the keepSelection resource).
824 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
825 * the selectToClipboard resource).
826 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
827 * Control-G is received. (This enables the bellIsUrgent
829 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
830 * is received. (enables the popOnBell resource).
831 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
832 * disabled by the titeInhibit resource).
833 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
834 * abled by the titeInhibit resource).
835 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
836 * Screen Buffer, clearing it first. (This may be disabled by
837 * the titeInhibit resource). This combines the effects of the 1
838 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
839 * applications rather than the 4 7 mode.
840 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
841 * Ps = 1 0 5 1 -> Set Sun function-key mode.
842 * Ps = 1 0 5 2 -> Set HP function-key mode.
843 * Ps = 1 0 5 3 -> Set SCO function-key mode.
844 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
845 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
846 * Ps = 2 0 0 4 -> Set bracketed paste mode.
848 * http: *vt100.net/docs/vt220-rm/chapter4.html
850 public setMode(params: number[]): void {
851 if (params.length > 1) {
852 for (let i = 0; i < params.length; i++) {
853 this.setMode([params[i]]);
859 if (!this._terminal.prefix) {
862 this._terminal.insertMode = true;
865 // this._terminal.convertEol = true;
868 } else if (this._terminal.prefix === '?') {
871 this._terminal.applicationCursor = true;
874 this._terminal.setgCharset(0, DEFAULT_CHARSET);
875 this._terminal.setgCharset(1, DEFAULT_CHARSET);
876 this._terminal.setgCharset(2, DEFAULT_CHARSET);
877 this._terminal.setgCharset(3, DEFAULT_CHARSET);
878 // set VT100 mode here
880 case 3: // 132 col mode
881 this._terminal.savedCols = this._terminal.cols;
882 this._terminal.resize(132, this._terminal.rows);
885 this._terminal.originMode = true;
888 this._terminal.wraparoundMode = true;
891 // this.cursorBlink = true;
894 this._terminal.log('Serial port requested application keypad.');
895 this._terminal.applicationKeypad = true;
896 this._terminal.viewport.syncScrollArea();
899 // no release, no motion, no wheel, no modifiers.
900 case 1000: // vt200 mouse
902 // no modifiers, except control on the wheel.
903 case 1002: // button event mouse
904 case 1003: // any event mouse
905 // any event - sends motion events,
906 // even if there is no button held down.
908 // TODO: Why are params[0] compares nested within a switch for params[0]?
910 this._terminal.x10Mouse = params[0] === 9;
911 this._terminal.vt200Mouse = params[0] === 1000;
912 this._terminal.normalMouse = params[0] > 1000;
913 this._terminal.mouseEvents = true;
914 this._terminal.element.classList.add('enable-mouse-events');
915 this._terminal.selectionManager.disable();
916 this._terminal.log('Binding to mouse events.');
918 case 1004: // send focusin/focusout events
921 this._terminal.sendFocus = true;
923 case 1005: // utf8 ext mode mouse
924 this._terminal.utfMouse = true;
925 // for wide terminals
926 // simply encodes large values as utf8 characters
928 case 1006: // sgr ext mode mouse
929 this._terminal.sgrMouse = true;
930 // for wide terminals
931 // does not add 32 to fields
933 // release: ^[[<b;x;ym
935 case 1015: // urxvt ext mode mouse
936 this._terminal.urxvtMouse = true;
937 // for wide terminals
938 // numbers for fields
942 case 25: // show cursor
943 this._terminal.cursorHidden = false;
945 case 1049: // alt screen buffer cursor
946 // this._terminal.saveCursor();
948 case 47: // alt screen buffer
949 case 1047: // alt screen buffer
950 if (!this._terminal.normal) {
952 lines: this._terminal.buffers.normal.lines,
953 ybase: this._terminal.ybase,
954 ydisp: this._terminal.ydisp,
957 scrollTop: this._terminal.scrollTop,
958 scrollBottom: this._terminal.scrollBottom,
959 tabs: this._terminal.tabs
960 // XXX save charset(s) here?
961 // charset: this._terminal.charset,
962 // glevel: this._terminal.glevel,
963 // charsets: this._terminal.charsets
965 this._terminal.reset();
966 this._terminal.viewport.syncScrollArea();
967 this._terminal.normal = normal;
968 this._terminal.showCursor();
976 * CSI Pm l Reset Mode (RM).
977 * Ps = 2 -> Keyboard Action Mode (AM).
978 * Ps = 4 -> Replace Mode (IRM).
979 * Ps = 1 2 -> Send/receive (SRM).
980 * Ps = 2 0 -> Normal Linefeed (LNM).
982 * DEC Private Mode Reset (DECRST).
983 * Ps = 1 -> Normal Cursor Keys (DECCKM).
984 * Ps = 2 -> Designate VT52 mode (DECANM).
985 * Ps = 3 -> 80 Column Mode (DECCOLM).
986 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
987 * Ps = 5 -> Normal Video (DECSCNM).
988 * Ps = 6 -> Normal Cursor Mode (DECOM).
989 * Ps = 7 -> No Wraparound Mode (DECAWM).
990 * Ps = 8 -> No Auto-repeat Keys (DECARM).
991 * Ps = 9 -> Don't send Mouse X & Y on button press.
992 * Ps = 1 0 -> Hide toolbar (rxvt).
993 * Ps = 1 2 -> Stop Blinking Cursor (att610).
994 * Ps = 1 8 -> Don't print form feed (DECPFF).
995 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
996 * Ps = 2 5 -> Hide Cursor (DECTCEM).
997 * Ps = 3 0 -> Don't show scrollbar (rxvt).
998 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
999 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
1000 * Ps = 4 1 -> No more(1) fix (see curses resource).
1001 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
1003 * Ps = 4 4 -> Turn Off Margin Bell.
1004 * Ps = 4 5 -> No Reverse-wraparound Mode.
1005 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
1006 * compile-time option).
1007 * Ps = 4 7 -> Use Normal Screen Buffer.
1008 * Ps = 6 6 -> Numeric keypad (DECNKM).
1009 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
1010 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
1011 * release. See the section Mouse Tracking.
1012 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
1013 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
1014 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
1015 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
1016 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
1017 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
1019 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
1020 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
1021 * the eightBitInput resource).
1022 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
1023 * Lock keys. (This disables the numLock resource).
1024 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
1025 * (This disables the metaSendsEscape resource).
1026 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
1028 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
1029 * (This disables the altSendsEscape resource).
1030 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
1031 * (This disables the keepSelection resource).
1032 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
1033 * the selectToClipboard resource).
1034 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
1035 * Control-G is received. (This disables the bellIsUrgent
1037 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
1038 * G is received. (This disables the popOnBell resource).
1039 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
1040 * first if in the Alternate Screen. (This may be disabled by
1041 * the titeInhibit resource).
1042 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
1043 * disabled by the titeInhibit resource).
1044 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
1045 * as in DECRC. (This may be disabled by the titeInhibit
1046 * resource). This combines the effects of the 1 0 4 7 and 1 0
1047 * 4 8 modes. Use this with terminfo-based applications rather
1048 * than the 4 7 mode.
1049 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
1050 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
1051 * Ps = 1 0 5 2 -> Reset HP function-key mode.
1052 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
1053 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
1054 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
1055 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
1057 public resetMode(params: number[]): void {
1058 if (params.length > 1) {
1059 for (let i = 0; i < params.length; i++) {
1060 this.resetMode([params[i]]);
1066 if (!this._terminal.prefix) {
1067 switch (params[0]) {
1069 this._terminal.insertMode = false;
1072 // this._terminal.convertEol = false;
1075 } else if (this._terminal.prefix === '?') {
1076 switch (params[0]) {
1078 this._terminal.applicationCursor = false;
1081 if (this._terminal.cols === 132 && this._terminal.savedCols) {
1082 this._terminal.resize(this._terminal.savedCols, this._terminal.rows);
1084 delete this._terminal.savedCols;
1087 this._terminal.originMode = false;
1090 this._terminal.wraparoundMode = false;
1093 // this.cursorBlink = false;
1096 this._terminal.log('Switching back to normal keypad.');
1097 this._terminal.applicationKeypad = false;
1098 this._terminal.viewport.syncScrollArea();
1100 case 9: // X10 Mouse
1101 case 1000: // vt200 mouse
1102 case 1002: // button event mouse
1103 case 1003: // any event mouse
1104 this._terminal.x10Mouse = false;
1105 this._terminal.vt200Mouse = false;
1106 this._terminal.normalMouse = false;
1107 this._terminal.mouseEvents = false;
1108 this._terminal.element.classList.remove('enable-mouse-events');
1109 this._terminal.selectionManager.enable();
1111 case 1004: // send focusin/focusout events
1112 this._terminal.sendFocus = false;
1114 case 1005: // utf8 ext mode mouse
1115 this._terminal.utfMouse = false;
1117 case 1006: // sgr ext mode mouse
1118 this._terminal.sgrMouse = false;
1120 case 1015: // urxvt ext mode mouse
1121 this._terminal.urxvtMouse = false;
1123 case 25: // hide cursor
1124 this._terminal.cursorHidden = true;
1126 case 1049: // alt screen buffer cursor
1128 case 47: // normal screen buffer
1129 case 1047: // normal screen buffer - clearing it first
1130 if (this._terminal.normal) {
1131 this._terminal.buffers.activateNormalBuffer();
1132 this._terminal.ybase = this._terminal.normal.ybase;
1133 this._terminal.ydisp = this._terminal.normal.ydisp;
1134 this._terminal.x = this._terminal.normal.x;
1135 this._terminal.y = this._terminal.normal.y;
1136 this._terminal.scrollTop = this._terminal.normal.scrollTop;
1137 this._terminal.scrollBottom = this._terminal.normal.scrollBottom;
1138 this._terminal.tabs = this._terminal.normal.tabs;
1139 this._terminal.normal = null;
1140 // Ensure the selection manager has the correct buffer
1141 this._terminal.selectionManager.setBuffer(this._terminal.buffer.lines);
1142 // if (params === 1049) {
1143 // this.x = this.savedX;
1144 // this.y = this.savedY;
1146 this._terminal.refresh(0, this._terminal.rows - 1);
1147 this._terminal.viewport.syncScrollArea();
1148 this._terminal.showCursor();
1156 * CSI Pm m Character Attributes (SGR).
1157 * Ps = 0 -> Normal (default).
1159 * Ps = 4 -> Underlined.
1160 * Ps = 5 -> Blink (appears as Bold).
1161 * Ps = 7 -> Inverse.
1162 * Ps = 8 -> Invisible, i.e., hidden (VT300).
1163 * Ps = 2 2 -> Normal (neither bold nor faint).
1164 * Ps = 2 4 -> Not underlined.
1165 * Ps = 2 5 -> Steady (not blinking).
1166 * Ps = 2 7 -> Positive (not inverse).
1167 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
1168 * Ps = 3 0 -> Set foreground color to Black.
1169 * Ps = 3 1 -> Set foreground color to Red.
1170 * Ps = 3 2 -> Set foreground color to Green.
1171 * Ps = 3 3 -> Set foreground color to Yellow.
1172 * Ps = 3 4 -> Set foreground color to Blue.
1173 * Ps = 3 5 -> Set foreground color to Magenta.
1174 * Ps = 3 6 -> Set foreground color to Cyan.
1175 * Ps = 3 7 -> Set foreground color to White.
1176 * Ps = 3 9 -> Set foreground color to default (original).
1177 * Ps = 4 0 -> Set background color to Black.
1178 * Ps = 4 1 -> Set background color to Red.
1179 * Ps = 4 2 -> Set background color to Green.
1180 * Ps = 4 3 -> Set background color to Yellow.
1181 * Ps = 4 4 -> Set background color to Blue.
1182 * Ps = 4 5 -> Set background color to Magenta.
1183 * Ps = 4 6 -> Set background color to Cyan.
1184 * Ps = 4 7 -> Set background color to White.
1185 * Ps = 4 9 -> Set background color to default (original).
1187 * If 16-color support is compiled, the following apply. Assume
1188 * that xterm's resources are set so that the ISO color codes are
1189 * the first 8 of a set of 16. Then the aixterm colors are the
1190 * bright versions of the ISO colors:
1191 * Ps = 9 0 -> Set foreground color to Black.
1192 * Ps = 9 1 -> Set foreground color to Red.
1193 * Ps = 9 2 -> Set foreground color to Green.
1194 * Ps = 9 3 -> Set foreground color to Yellow.
1195 * Ps = 9 4 -> Set foreground color to Blue.
1196 * Ps = 9 5 -> Set foreground color to Magenta.
1197 * Ps = 9 6 -> Set foreground color to Cyan.
1198 * Ps = 9 7 -> Set foreground color to White.
1199 * Ps = 1 0 0 -> Set background color to Black.
1200 * Ps = 1 0 1 -> Set background color to Red.
1201 * Ps = 1 0 2 -> Set background color to Green.
1202 * Ps = 1 0 3 -> Set background color to Yellow.
1203 * Ps = 1 0 4 -> Set background color to Blue.
1204 * Ps = 1 0 5 -> Set background color to Magenta.
1205 * Ps = 1 0 6 -> Set background color to Cyan.
1206 * Ps = 1 0 7 -> Set background color to White.
1208 * If xterm is compiled with the 16-color support disabled, it
1209 * supports the following, from rxvt:
1210 * Ps = 1 0 0 -> Set foreground and background color to
1213 * If 88- or 256-color support is compiled, the following apply.
1214 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
1216 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
1219 public charAttributes(params: number[]): void {
1220 // Optimize a single SGR0.
1221 if (params.length === 1 && params[0] === 0) {
1222 this._terminal.curAttr = this._terminal.defAttr;
1226 let l = params.length
1228 , flags = this._terminal.curAttr >> 18
1229 , fg = (this._terminal.curAttr >> 9) & 0x1ff
1230 , bg = this._terminal.curAttr & 0x1ff
1233 for (; i < l; i++) {
1235 if (p >= 30 && p <= 37) {
1238 } else if (p >= 40 && p <= 47) {
1241 } else if (p >= 90 && p <= 97) {
1245 } else if (p >= 100 && p <= 107) {
1249 } else if (p === 0) {
1251 flags = this._terminal.defAttr >> 18;
1252 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1253 bg = this._terminal.defAttr & 0x1ff;
1257 } else if (p === 1) {
1260 } else if (p === 4) {
1263 } else if (p === 5) {
1266 } else if (p === 7) {
1267 // inverse and positive
1268 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
1270 } else if (p === 8) {
1273 } else if (p === 22) {
1276 } else if (p === 24) {
1279 } else if (p === 25) {
1282 } else if (p === 27) {
1285 } else if (p === 28) {
1288 } else if (p === 39) {
1290 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1291 } else if (p === 49) {
1293 bg = this._terminal.defAttr & 0x1ff;
1294 } else if (p === 38) {
1296 if (params[i + 1] === 2) {
1298 fg = this._terminal.matchColor(
1300 params[i + 1] & 0xff,
1301 params[i + 2] & 0xff);
1302 if (fg === -1) fg = 0x1ff;
1304 } else if (params[i + 1] === 5) {
1306 p = params[i] & 0xff;
1309 } else if (p === 48) {
1311 if (params[i + 1] === 2) {
1313 bg = this._terminal.matchColor(
1315 params[i + 1] & 0xff,
1316 params[i + 2] & 0xff);
1317 if (bg === -1) bg = 0x1ff;
1319 } else if (params[i + 1] === 5) {
1321 p = params[i] & 0xff;
1324 } else if (p === 100) {
1326 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1327 bg = this._terminal.defAttr & 0x1ff;
1329 this._terminal.error('Unknown SGR attribute: %d.', p);
1333 this._terminal.curAttr = (flags << 18) | (fg << 9) | bg;
1337 * CSI Ps n Device Status Report (DSR).
1338 * Ps = 5 -> Status Report. Result (``OK'') is
1340 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
1344 * Device Status Report (DSR, DEC-specific).
1345 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
1346 * ? r ; c R (assumes page is zero).
1347 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
1348 * or CSI ? 1 1 n (not ready).
1349 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
1350 * or CSI ? 2 1 n (locked).
1351 * Ps = 2 6 -> Report Keyboard status as
1352 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
1353 * The last two parameters apply to VT400 & up, and denote key-
1354 * board ready and LK01 respectively.
1355 * Ps = 5 3 -> Report Locator status as
1356 * CSI ? 5 3 n Locator available, if compiled-in, or
1357 * CSI ? 5 0 n No Locator, if not.
1359 public deviceStatus(params: number[]): void {
1360 if (!this._terminal.prefix) {
1361 switch (params[0]) {
1364 this._terminal.send(C0.ESC + '[0n');
1368 this._terminal.send(C0.ESC + '['
1369 + (this._terminal.y + 1)
1371 + (this._terminal.x + 1)
1375 } else if (this._terminal.prefix === '?') {
1376 // modern xterm doesnt seem to
1377 // respond to any of these except ?6, 6, and 5
1378 switch (params[0]) {
1381 this._terminal.send(C0.ESC + '[?'
1382 + (this._terminal.y + 1)
1384 + (this._terminal.x + 1)
1389 // this.send(C0.ESC + '[?11n');
1392 // dont support user defined keys
1393 // this.send(C0.ESC + '[?21n');
1396 // north american keyboard
1397 // this.send(C0.ESC + '[?27;1;0;0n');
1400 // no dec locator/mouse
1401 // this.send(C0.ESC + '[?50n');
1408 * CSI ! p Soft terminal reset (DECSTR).
1409 * http://vt100.net/docs/vt220-rm/table4-10.html
1411 public softReset(params: number[]): void {
1412 this._terminal.cursorHidden = false;
1413 this._terminal.insertMode = false;
1414 this._terminal.originMode = false;
1415 this._terminal.wraparoundMode = true; // defaults: xterm - true, vt100 - false
1416 this._terminal.applicationKeypad = false; // ?
1417 this._terminal.viewport.syncScrollArea();
1418 this._terminal.applicationCursor = false;
1419 this._terminal.scrollTop = 0;
1420 this._terminal.scrollBottom = this._terminal.rows - 1;
1421 this._terminal.curAttr = this._terminal.defAttr;
1422 this._terminal.x = this._terminal.y = 0; // ?
1423 this._terminal.charset = null;
1424 this._terminal.glevel = 0; // ??
1425 this._terminal.charsets = [null]; // ??
1429 * CSI Ps SP q Set cursor style (DECSCUSR, VT520).
1430 * Ps = 0 -> blinking block.
1431 * Ps = 1 -> blinking block (default).
1432 * Ps = 2 -> steady block.
1433 * Ps = 3 -> blinking underline.
1434 * Ps = 4 -> steady underline.
1435 * Ps = 5 -> blinking bar (xterm).
1436 * Ps = 6 -> steady bar (xterm).
1438 public setCursorStyle(params?: number[]): void {
1439 const param = params[0] < 1 ? 1 : params[0];
1443 this._terminal.setOption('cursorStyle', 'block');
1447 this._terminal.setOption('cursorStyle', 'underline');
1451 this._terminal.setOption('cursorStyle', 'bar');
1454 const isBlinking = param % 2 === 1;
1455 this._terminal.setOption('cursorBlink', isBlinking);
1460 * Set Scrolling Region [top;bottom] (default = full size of win-
1464 public setScrollRegion(params: number[]): void {
1465 if (this._terminal.prefix) return;
1466 this._terminal.scrollTop = (params[0] || 1) - 1;
1467 this._terminal.scrollBottom = (params[1] && params[1] <= this._terminal.rows ? params[1] : this._terminal.rows) - 1;
1468 this._terminal.x = 0;
1469 this._terminal.y = 0;
1475 * Save cursor (ANSI.SYS).
1477 public saveCursor(params: number[]): void {
1478 this._terminal.savedX = this._terminal.x;
1479 this._terminal.savedY = this._terminal.y;
1485 * Restore cursor (ANSI.SYS).
1487 public restoreCursor(params: number[]): void {
1488 this._terminal.x = this._terminal.savedX || 0;
1489 this._terminal.y = this._terminal.savedY || 0;
1493 const wcwidth = (function(opts) {
1494 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1495 // combining characters
1497 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1498 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1499 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1500 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1501 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1502 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1503 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1504 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1505 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1506 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1507 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1508 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1509 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1510 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1511 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1512 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1513 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1514 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1515 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1516 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1517 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1518 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1519 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1520 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1521 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1522 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1523 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1524 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1525 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1526 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1527 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1528 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1529 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1530 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1531 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1532 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1533 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1534 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1535 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1536 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1537 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1538 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1539 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1540 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1541 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1542 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1543 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1547 function bisearch(ucs) {
1549 let max = COMBINING.length - 1;
1551 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
1553 while (max >= min) {
1554 mid = Math.floor((min + max) / 2);
1555 if (ucs > COMBINING[mid][1])
1557 else if (ucs < COMBINING[mid][0])
1564 function wcwidth(ucs) {
1565 // test for 8-bit control characters
1568 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1569 return opts.control;
1570 // binary search in table of non-spacing characters
1573 // if we arrive here, ucs is not a combining or C0/C1 control character
1579 function isWide(ucs) {
1582 ucs <= 0x115f || // Hangul Jamo init. consonants
1585 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1586 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1587 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1588 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1589 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1590 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1591 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1592 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1593 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1596 })({nul: 0, control: 0}); // configurable options