1 import { C0 } from './EscapeSequences';
2 import { IInputHandler } from './Interfaces';
3 import { CHARSETS } from './Charsets';
5 const normalStateHandler: {[key: string]: (handler: IInputHandler) => void} = {};
6 normalStateHandler[C0.BEL] = (handler) => handler.bell();
7 normalStateHandler[C0.LF] = (handler) => handler.lineFeed();
8 normalStateHandler[C0.VT] = normalStateHandler[C0.LF];
9 normalStateHandler[C0.FF] = normalStateHandler[C0.LF];
10 normalStateHandler[C0.CR] = (handler) => handler.carriageReturn();
11 normalStateHandler[C0.BS] = (handler) => handler.backspace();
12 normalStateHandler[C0.HT] = (handler) => handler.tab();
13 normalStateHandler[C0.SO] = (handler) => handler.shiftOut();
14 normalStateHandler[C0.SI] = (handler) => handler.shiftIn();
15 // TODO: Add ESC and Default cases to normalStateHandler
17 const csiParamStateHandler: {[key: string]: (parser: Parser) => void} = {};
18 csiParamStateHandler['?'] = (parser) => parser.setPrefix('?');
19 csiParamStateHandler['>'] = (parser) => parser.setPrefix('>');
20 csiParamStateHandler['!'] = (parser) => parser.setPrefix('!');
21 csiParamStateHandler['0'] = (parser) => parser.setParam(parser.getParam() * 10);
22 csiParamStateHandler['1'] = (parser) => parser.setParam(parser.getParam() * 10 + 1);
23 csiParamStateHandler['2'] = (parser) => parser.setParam(parser.getParam() * 10 + 2);
24 csiParamStateHandler['3'] = (parser) => parser.setParam(parser.getParam() * 10 + 3);
25 csiParamStateHandler['4'] = (parser) => parser.setParam(parser.getParam() * 10 + 4);
26 csiParamStateHandler['5'] = (parser) => parser.setParam(parser.getParam() * 10 + 5);
27 csiParamStateHandler['6'] = (parser) => parser.setParam(parser.getParam() * 10 + 6);
28 csiParamStateHandler['7'] = (parser) => parser.setParam(parser.getParam() * 10 + 7);
29 csiParamStateHandler['8'] = (parser) => parser.setParam(parser.getParam() * 10 + 8);
30 csiParamStateHandler['9'] = (parser) => parser.setParam(parser.getParam() * 10 + 9);
31 csiParamStateHandler['$'] = (parser) => parser.setPostfix('$');
32 csiParamStateHandler['"'] = (parser) => parser.setPostfix('"');
33 csiParamStateHandler[' '] = (parser) => parser.setPostfix(' ');
34 csiParamStateHandler['\''] = (parser) => parser.setPostfix('\'');
35 csiParamStateHandler[';'] = (parser) => parser.finalizeParam();
37 const csiStateHandler: {[key: string]: (handler: IInputHandler, params: number[]) => void} = {};
38 csiStateHandler['A'] = (handler, params) => handler.cursorUp(params);
39 csiStateHandler['B'] = (handler, params) => handler.cursorDown(params);
40 csiStateHandler['C'] = (handler, params) => handler.cursorForward(params);
41 csiStateHandler['D'] = (handler, params) => handler.cursorBackward(params);
42 csiStateHandler['H'] = (handler, params) => handler.cursorPosition(params);
43 csiStateHandler['J'] = (handler, params) => handler.eraseInDisplay(params);
44 csiStateHandler['K'] = (handler, params) => handler.eraseInLine(params);
45 csiStateHandler['m'] = (handler, params) => handler.charAttributes(params);
46 csiStateHandler['n'] = (handler, params) => handler.deviceStatus(params);
60 private state: ParserState;
62 // TODO: Remove terminal when handler can do everything
64 private _inputHandler: IInputHandler,
65 private _terminal: any
67 this.state = ParserState.NORMAL;
70 public parse(data: string) {
71 let l = data.length, i = 0, j, cs, ch, code, low, ch_width, row;
73 // apply leftover surrogate high from last write
74 if (this._terminal.surrogate_high) {
75 data = this._terminal.surrogate_high + data;
76 this._terminal.surrogate_high = '';
82 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
83 // --> maybe move to default
84 code = data.charCodeAt(i);
85 if (0xD800 <= code && code <= 0xDBFF) {
86 // we got a surrogate high
87 // get surrogate low (next 2 bytes)
88 low = data.charCodeAt(i + 1);
90 // end of data stream, save surrogate high
91 this._terminal.surrogate_high = ch;
94 code = ((code - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
95 ch += data.charAt(i + 1);
97 // surrogate low - already handled above
98 if (0xDC00 <= code && code <= 0xDFFF)
101 if (this.state === ParserState.NORMAL) {
102 if (ch in normalStateHandler) {
103 normalStateHandler[ch](this._inputHandler);
104 // Skip switch statement (eventually everything will be handled this way
109 switch (this.state) {
110 case ParserState.NORMAL:
113 this.state = ParserState.ESCAPED;
118 // calculate print space
119 // expensive call, therefore we save width in line buffer
120 ch_width = wcwidth(code);
123 if (this._terminal.charset && this._terminal.charset[ch]) {
124 ch = this._terminal.charset[ch];
127 row = this._terminal.y + this._terminal.ybase;
129 // insert combining char in last cell
130 // FIXME: needs handling after cursor jumps
131 if (!ch_width && this._terminal.x) {
132 // dont overflow left
133 if (this._terminal.lines.get(row)[this._terminal.x - 1]) {
134 if (!this._terminal.lines.get(row)[this._terminal.x - 1][2]) {
136 // found empty cell after fullwidth, need to go 2 cells back
137 if (this._terminal.lines.get(row)[this._terminal.x - 2])
138 this._terminal.lines.get(row)[this._terminal.x - 2][1] += ch;
141 this._terminal.lines.get(row)[this._terminal.x - 1][1] += ch;
143 this._terminal.updateRange(this._terminal.y);
148 // goto next line if ch would overflow
149 // TODO: needs a global min terminal width of 2
150 if (this._terminal.x + ch_width - 1 >= this._terminal.cols) {
152 if (this._terminal.wraparoundMode) {
153 this._terminal.x = 0;
155 if (this._terminal.y > this._terminal.scrollBottom) {
157 this._terminal.scroll();
160 this._terminal.x = this._terminal.cols - 1;
161 if (ch_width === 2) // FIXME: check for xterm behavior
165 row = this._terminal.y + this._terminal.ybase;
167 // insert mode: move characters to right
168 if (this._terminal.insertMode) {
169 // do this twice for a fullwidth char
170 for (let moves = 0; moves < ch_width; ++moves) {
171 // remove last cell, if it's width is 0
172 // we have to adjust the second last cell as well
173 const removed = this._terminal.lines.get(this._terminal.y + this._terminal.ybase).pop();
175 && this._terminal.lines.get(row)[this._terminal.cols - 2]
176 && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2)
177 this._terminal.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
179 // insert empty cell at cursor
180 this._terminal.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
184 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, ch, ch_width];
186 this._terminal.updateRange(this._terminal.y);
188 // fullwidth char - set next cell width to zero and advance cursor
189 if (ch_width === 2) {
190 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, '', 0];
197 case ParserState.ESCAPED:
199 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
201 this._terminal.params = [];
202 this._terminal.currentParam = 0;
203 this.state = ParserState.CSI_PARAM;
206 // ESC ] Operating System Command ( OSC is 0x9d).
208 this._terminal.params = [];
209 this._terminal.currentParam = 0;
210 this.state = ParserState.OSC;
213 // ESC P Device Control String ( DCS is 0x90).
215 this._terminal.params = [];
216 this._terminal.currentParam = 0;
217 this.state = ParserState.DCS;
220 // ESC _ Application Program Command ( APC is 0x9f).
222 this.state = ParserState.IGNORE;
225 // ESC ^ Privacy Message ( PM is 0x9e).
227 this.state = ParserState.IGNORE;
230 // ESC c Full Reset (RIS).
232 this._terminal.reset();
235 // ESC E Next Line ( NEL is 0x85).
236 // ESC D Index ( IND is 0x84).
238 this._terminal.x = 0;
241 this._terminal.index();
242 this.state = ParserState.NORMAL;
245 // ESC M Reverse Index ( RI is 0x8d).
247 this._terminal.reverseIndex();
248 this.state = ParserState.NORMAL;
251 // ESC % Select default/utf-8 character set.
252 // @ = default, G = utf-8
254 // this.charset = null;
255 this._terminal.setgLevel(0);
256 this._terminal.setgCharset(0, CHARSETS.US);
257 this.state = ParserState.NORMAL;
261 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
262 case '(': // <-- this seems to get all the attention
270 this._terminal.gcharset = 0;
273 this._terminal.gcharset = 1;
276 this._terminal.gcharset = 2;
279 this._terminal.gcharset = 3;
282 this._terminal.gcharset = 1;
285 this._terminal.gcharset = 2;
288 this.state = ParserState.CHARSET;
291 // Designate G3 Character Set (VT300).
292 // A = ISO Latin-1 Supplemental.
295 this._terminal.gcharset = 3;
296 this.state = ParserState.CHARSET;
301 // Single Shift Select of G2 Character Set
302 // ( SS2 is 0x8e). This affects next character only.
306 // Single Shift Select of G3 Character Set
307 // ( SS3 is 0x8f). This affects next character only.
311 // Invoke the G2 Character Set as GL (LS2).
313 this._terminal.setgLevel(2);
316 // Invoke the G3 Character Set as GL (LS3).
318 this._terminal.setgLevel(3);
321 // Invoke the G3 Character Set as GR (LS3R).
323 this._terminal.setgLevel(3);
326 // Invoke the G2 Character Set as GR (LS2R).
328 this._terminal.setgLevel(2);
331 // Invoke the G1 Character Set as GR (LS1R).
333 this._terminal.setgLevel(1);
336 // ESC 7 Save Cursor (DECSC).
338 this._terminal.saveCursor();
339 this.state = ParserState.NORMAL;
342 // ESC 8 Restore Cursor (DECRC).
344 this._terminal.restoreCursor();
345 this.state = ParserState.NORMAL;
348 // ESC # 3 DEC line height/width
350 this.state = ParserState.NORMAL;
354 // ESC H Tab Set (HTS is 0x88).
356 this._terminal.tabSet();
357 this.state = ParserState.NORMAL;
360 // ESC = Application Keypad (DECKPAM).
362 this._terminal.log('Serial port requested application keypad.');
363 this._terminal.applicationKeypad = true;
364 this._terminal.viewport.syncScrollArea();
365 this.state = ParserState.NORMAL;
368 // ESC > Normal Keypad (DECKPNM).
370 this._terminal.log('Switching back to normal keypad.');
371 this._terminal.applicationKeypad = false;
372 this._terminal.viewport.syncScrollArea();
373 this.state = ParserState.NORMAL;
377 this.state = ParserState.NORMAL;
378 this._terminal.error('Unknown ESC control: %s.', ch);
383 case ParserState.CHARSET:
385 case '0': // DEC Special Character and Line Drawing Set.
391 case 'B': // United States (USASCII).
399 cs = CHARSETS.Finnish;
402 cs = CHARSETS.French;
404 case 'Q': // FrenchCanadian
405 cs = CHARSETS.FrenchCanadian;
408 cs = CHARSETS.German;
411 cs = CHARSETS.Italian;
413 case 'E': // NorwegianDanish
415 cs = CHARSETS.NorwegianDanish;
418 cs = CHARSETS.Spanish;
422 cs = CHARSETS.Swedish;
427 case '/': // ISOLatin (actually /A)
428 cs = CHARSETS.ISOLatin;
435 this._terminal.setgCharset(this._terminal.gcharset, cs);
436 this._terminal.gcharset = null;
437 this.state = ParserState.NORMAL;
440 case ParserState.OSC:
443 // Set Text Parameters.
444 if (ch === C0.ESC || ch === C0.BEL) {
445 if (ch === C0.ESC) i++;
447 this._terminal.params.push(this._terminal.currentParam);
449 switch (this._terminal.params[0]) {
453 if (this._terminal.params[1]) {
454 this._terminal.title = this._terminal.params[1];
455 this._terminal.handleTitle(this._terminal.title);
463 // change dynamic colors
475 // change dynamic ui colors
487 // manipulate selection data
504 this._terminal.params = [];
505 this._terminal.currentParam = 0;
506 this.state = ParserState.NORMAL;
508 if (!this._terminal.params.length) {
509 if (ch >= '0' && ch <= '9') {
510 this._terminal.currentParam =
511 this._terminal.currentParam * 10 + ch.charCodeAt(0) - 48;
512 } else if (ch === ';') {
513 this._terminal.params.push(this._terminal.currentParam);
514 this._terminal.currentParam = '';
517 this._terminal.currentParam += ch;
522 case ParserState.CSI_PARAM:
523 if (ch in csiParamStateHandler) {
524 csiParamStateHandler[ch](this);
527 this.finalizeParam();
528 // Fall through the CSI as this character should be the CSI code.
529 this.state = ParserState.CSI;
531 case ParserState.CSI:
532 this.state = ParserState.NORMAL;
534 if (ch in csiStateHandler) {
535 csiStateHandler[ch](this._inputHandler, this._terminal.params);
536 // Skip below switch as this has handled these codes (eventually everything will be handled here
547 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
549 this._terminal.insertChars(this._terminal.params);
553 // Cursor Next Line Ps Times (default = 1) (CNL).
555 this._terminal.cursorNextLine(this._terminal.params);
559 // Cursor Preceding Line Ps Times (default = 1) (CNL).
561 this._terminal.cursorPrecedingLine(this._terminal.params);
565 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
567 this._terminal.cursorCharAbsolute(this._terminal.params);
571 // Insert Ps Line(s) (default = 1) (IL).
573 this._terminal.insertLines(this._terminal.params);
577 // Delete Ps Line(s) (default = 1) (DL).
579 this._terminal.deleteLines(this._terminal.params);
583 // Delete Ps Character(s) (default = 1) (DCH).
585 this._terminal.deleteChars(this._terminal.params);
589 // Erase Ps Character(s) (default = 1) (ECH).
591 this._terminal.eraseChars(this._terminal.params);
594 // CSI Pm ` Character Position Absolute
595 // [column] (default = [row,1]) (HPA).
597 this._terminal.charPosAbsolute(this._terminal.params);
601 // Horizontal Position Relative
603 this._terminal.HPositionRelative(this._terminal.params);
607 // Send Device Attributes (Primary DA).
609 // Send Device Attributes (Secondary DA)
611 this._terminal.sendDeviceAttributes(this._terminal.params);
615 // Line Position Absolute [row] (default = [1,column]) (VPA).
617 this._terminal.linePosAbsolute(this._terminal.params);
620 // 145 65 e * VPR - Vertical Position Relative
622 this._terminal.VPositionRelative(this._terminal.params);
626 // Horizontal and Vertical Position [row;column] (default =
629 this._terminal.HVPosition(this._terminal.params);
632 // CSI Pm h Set Mode (SM).
633 // CSI ? Pm h - mouse escape codes, cursor escape codes
635 this._terminal.setMode(this._terminal.params);
638 // CSI Pm l Reset Mode (RM).
641 this._terminal.resetMode(this._terminal.params);
645 // Set Scrolling Region [top;bottom] (default = full size of win-
649 this._terminal.setScrollRegion(this._terminal.params);
653 // Save cursor (ANSI.SYS).
655 this._terminal.saveCursor(this._terminal.params);
659 // Restore cursor (ANSI.SYS).
661 this._terminal.restoreCursor(this._terminal.params);
669 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
671 this._terminal.cursorForwardTab(this._terminal.params);
674 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
676 this._terminal.scrollUp(this._terminal.params);
679 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
680 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
683 // if (this.prefix === '>') {
684 // this.resetTitleModes(this.params);
687 // if (this.params.length > 2) {
688 // this.initMouseTracking(this.params);
691 if (this._terminal.params.length < 2 && !this._terminal.prefix) {
692 this._terminal.scrollDown(this._terminal.params);
697 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
699 this._terminal.cursorBackwardTab(this._terminal.params);
702 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
704 this._terminal.repeatPrecedingCharacter(this._terminal.params);
707 // CSI Ps g Tab Clear (TBC).
709 this._terminal.tabClear(this._terminal.params);
712 // CSI Pm i Media Copy (MC).
715 // this.mediaCopy(this.params);
718 // CSI Pm m Character Attributes (SGR).
720 // case 'm': // duplicate
721 // if (this.prefix === '>') {
722 // this.setResources(this.params);
724 // this.charAttributes(this.params);
728 // CSI Ps n Device Status Report (DSR).
730 // case 'n': // duplicate
731 // if (this.prefix === '>') {
732 // this.disableModifiers(this.params);
734 // this.deviceStatus(this.params);
738 // CSI > Ps p Set pointer mode.
739 // CSI ! p Soft terminal reset (DECSTR).
741 // Request ANSI mode (DECRQM).
743 // Request DEC private mode (DECRQM).
746 switch (this._terminal.prefix) {
748 // this.setPointerMode(this.params);
751 this._terminal.softReset(this._terminal.params);
754 // if (this.postfix === '$') {
755 // this.requestPrivateMode(this.params);
759 // if (this.postfix === '"') {
760 // this.setConformanceLevel(this.params);
761 // } else if (this.postfix === '$') {
762 // this.requestAnsiMode(this.params);
768 // CSI Ps q Load LEDs (DECLL).
772 // if (this.postfix === ' ') {
773 // this.setCursorStyle(this.params);
776 // if (this.postfix === '"') {
777 // this.setCharProtectionAttr(this.params);
780 // this.loadLEDs(this.params);
784 // Set Scrolling Region [top;bottom] (default = full size of win-
787 // CSI Pt; Pl; Pb; Pr; Ps$ r
788 // case 'r': // duplicate
789 // if (this.prefix === '?') {
790 // this.restorePrivateValues(this.params);
791 // } else if (this.postfix === '$') {
792 // this.setAttrInRectangle(this.params);
794 // this.setScrollRegion(this.params);
798 // CSI s Save cursor (ANSI.SYS).
800 // case 's': // duplicate
801 // if (this.prefix === '?') {
802 // this.savePrivateValues(this.params);
804 // this.saveCursor(this.params);
808 // CSI Ps ; Ps ; Ps t
809 // CSI Pt; Pl; Pb; Pr; Ps$ t
813 // if (this.postfix === '$') {
814 // this.reverseAttrInRectangle(this.params);
815 // } else if (this.postfix === ' ') {
816 // this.setWarningBellVolume(this.params);
818 // if (this.prefix === '>') {
819 // this.setTitleModeFeature(this.params);
821 // this.manipulateWindow(this.params);
826 // CSI u Restore cursor (ANSI.SYS).
828 // case 'u': // duplicate
829 // if (this.postfix === ' ') {
830 // this.setMarginBellVolume(this.params);
832 // this.restoreCursor(this.params);
836 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
838 // if (this.postfix === '$') {
839 // this.copyRectagle(this.params);
843 // CSI Pt ; Pl ; Pb ; Pr ' w
845 // if (this.postfix === '\'') {
846 // this.enableFilterRectangle(this.params);
850 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
851 // CSI Ps x Select Attribute Change Extent (DECSACE).
852 // CSI Pc; Pt; Pl; Pb; Pr$ x
854 // if (this.postfix === '$') {
855 // this.fillRectangle(this.params);
857 // this.requestParameters(this.params);
858 // //this.__(this.params);
863 // CSI Pt; Pl; Pb; Pr$ z
865 // if (this.postfix === '\'') {
866 // this.enableLocatorReporting(this.params);
867 // } else if (this.postfix === '$') {
868 // this.eraseRectangle(this.params);
873 // CSI Pt; Pl; Pb; Pr$ {
875 // if (this.postfix === '\'') {
876 // this.setLocatorEvents(this.params);
877 // } else if (this.postfix === '$') {
878 // this.selectiveEraseRectangle(this.params);
884 // if (this.postfix === '\'') {
885 // this.requestLocatorPosition(this.params);
890 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
892 // if (this.postfix === ' ') {
893 // this.insertColumns(this.params);
898 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
900 // if (this.postfix === ' ') {
901 // this.deleteColumns(this.params);
906 this._terminal.error('Unknown CSI code: %s.', ch);
910 this._terminal.prefix = '';
911 this._terminal.postfix = '';
914 case ParserState.DCS:
915 if (ch === C0.ESC || ch === C0.BEL) {
916 if (ch === C0.ESC) i++;
918 switch (this._terminal.prefix) {
919 // User-Defined Keys (DECUDK).
923 // Request Status String (DECRQSS).
924 // test: echo -e '\eP$q"p\e\\'
926 let pt = this._terminal.currentParam
943 + (this._terminal.scrollTop + 1)
945 + (this._terminal.scrollBottom + 1)
955 this._terminal.error('Unknown DCS Pt: %s.', pt);
960 this._terminal.send(C0.ESC + 'P' + +valid + '$r' + pt + C0.ESC + '\\');
963 // Set Termcap/Terminfo Data (xterm, experimental).
967 // Request Termcap/Terminfo String (xterm, experimental)
968 // Regular xterm does not even respond to this sequence.
969 // This can cause a small glitch in vim.
970 // test: echo -ne '\eP+q6b64\e\\'
972 // TODO: Don't declare pt twice
973 /*let*/ pt = this._terminal.currentParam
976 this._terminal.send(C0.ESC + 'P' + +valid + '+r' + pt + C0.ESC + '\\');
980 this._terminal.error('Unknown DCS prefix: %s.', this._terminal.prefix);
984 this._terminal.currentParam = 0;
985 this._terminal.prefix = '';
986 this.state = ParserState.NORMAL;
987 } else if (!this._terminal.currentParam) {
988 if (!this._terminal.prefix && ch !== '$' && ch !== '+') {
989 this._terminal.currentParam = ch;
990 } else if (this._terminal.prefix.length === 2) {
991 this._terminal.currentParam = ch;
993 this._terminal.prefix += ch;
996 this._terminal.currentParam += ch;
1000 case ParserState.IGNORE:
1002 if (ch === C0.ESC || ch === C0.BEL) {
1003 if (ch === C0.ESC) i++;
1004 this.state = ParserState.NORMAL;
1011 public setPrefix(prefix: string): void {
1012 this._terminal.prefix = prefix;
1015 public setParam(param: number) {
1016 this._terminal.currentParam = param;
1019 public getParam(): number {
1020 return this._terminal.currentParam;
1023 public finalizeParam(): void {
1024 this._terminal.params.push(this._terminal.currentParam);
1025 this._terminal.currentParam = 0;
1028 public setPostfix(postfix: string): void {
1029 this._terminal.postfix = postfix;
1033 const wcwidth = (function(opts) {
1034 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1035 // combining characters
1037 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1038 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1039 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1040 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1041 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1042 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1043 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1044 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1045 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1046 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1047 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1048 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1049 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1050 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1051 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1052 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1053 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1054 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1055 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1056 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1057 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1058 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1059 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1060 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1061 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1062 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1063 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1064 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1065 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1066 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1067 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1068 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1069 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1070 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1071 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1072 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1073 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1074 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1075 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1076 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1077 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1078 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1079 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1080 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1081 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1082 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1083 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1087 function bisearch(ucs) {
1089 let max = COMBINING.length - 1;
1091 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
1093 while (max >= min) {
1094 mid = Math.floor((min + max) / 2);
1095 if (ucs > COMBINING[mid][1])
1097 else if (ucs < COMBINING[mid][0])
1104 function wcwidth(ucs) {
1105 // test for 8-bit control characters
1108 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1109 return opts.control;
1110 // binary search in table of non-spacing characters
1113 // if we arrive here, ucs is not a combining or C0/C1 control character
1119 function isWide(ucs) {
1122 ucs <= 0x115f || // Hangul Jamo init. consonants
1125 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1126 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1127 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1128 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1129 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1130 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1131 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1132 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1133 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1136 })({nul: 0, control: 0}); // configurable options