]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/InputHandler.ts
Prevent wraparound mode from overriding last character
[mirror_xterm.js.git] / src / InputHandler.ts
1 /**
2 * @license MIT
3 */
4
5 import { IInputHandler, ITerminal } from './Interfaces';
6 import { C0 } from './EscapeSequences';
7 import { CHARSETS } from './Charsets';
8
9 /**
10 * The terminal's standard implementation of IInputHandler, this handles all
11 * input from the Parser.
12 *
13 * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand
14 * each function's header comment.
15 */
16 export class InputHandler implements IInputHandler {
17 // TODO: We want to type _terminal when it's pulled into TS
18 constructor(private _terminal: any) { }
19
20 public addChar(char: string, code: number): void {
21 if (char >= ' ') {
22 // calculate print space
23 // expensive call, therefore we save width in line buffer
24 const ch_width = wcwidth(code);
25
26 if (this._terminal.charset && this._terminal.charset[char]) {
27 char = this._terminal.charset[char];
28 }
29
30 let row = this._terminal.y + this._terminal.ybase;
31
32 // insert combining char in last cell
33 // FIXME: needs handling after cursor jumps
34 if (!ch_width && this._terminal.x) {
35 // dont overflow left
36 if (this._terminal.lines.get(row)[this._terminal.x - 1]) {
37 if (!this._terminal.lines.get(row)[this._terminal.x - 1][2]) {
38
39 // found empty cell after fullwidth, need to go 2 cells back
40 if (this._terminal.lines.get(row)[this._terminal.x - 2])
41 this._terminal.lines.get(row)[this._terminal.x - 2][1] += char;
42
43 } else {
44 this._terminal.lines.get(row)[this._terminal.x - 1][1] += char;
45 }
46 this._terminal.updateRange(this._terminal.y);
47 }
48 return;
49 }
50
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) {
54 // autowrap - DECAWM
55 if (this._terminal.wraparoundMode) {
56 this._terminal.x = 0;
57 this._terminal.y++;
58 if (this._terminal.y > this._terminal.scrollBottom) {
59 this._terminal.y--;
60 this._terminal.scroll();
61 }
62 } else {
63 if (ch_width === 2) // FIXME: check for xterm behavior
64 return;
65 }
66 }
67 row = this._terminal.y + this._terminal.ybase;
68
69 // insert mode: move characters to right
70 if (this._terminal.insertMode) {
71 // do this twice for a fullwidth char
72 for (let moves = 0; moves < ch_width; ++moves) {
73 // remove last cell, if it's width is 0
74 // we have to adjust the second last cell as well
75 const removed = this._terminal.lines.get(this._terminal.y + this._terminal.ybase).pop();
76 if (removed[2] === 0
77 && this._terminal.lines.get(row)[this._terminal.cols - 2]
78 && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2)
79 this._terminal.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
80
81 // insert empty cell at cursor
82 this._terminal.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
83 }
84 }
85
86 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, char, ch_width];
87 this._terminal.x++;
88 this._terminal.updateRange(this._terminal.y);
89
90 // fullwidth char - set next cell width to zero and advance cursor
91 if (ch_width === 2) {
92 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, '', 0];
93 this._terminal.x++;
94 }
95 }
96 }
97
98 /**
99 * BEL
100 * Bell (Ctrl-G).
101 */
102 public bell(): void {
103 if (!this._terminal.visualBell) {
104 return;
105 }
106 this._terminal.element.style.borderColor = 'white';
107 setTimeout(() => this._terminal.element.style.borderColor = '', 10);
108 if (this._terminal.popOnBell) {
109 this._terminal.focus();
110 }
111 }
112
113 /**
114 * LF
115 * Line Feed or New Line (NL). (LF is Ctrl-J).
116 */
117 public lineFeed(): void {
118 if (this._terminal.convertEol) {
119 this._terminal.x = 0;
120 }
121 this._terminal.y++;
122 if (this._terminal.y > this._terminal.scrollBottom) {
123 this._terminal.y--;
124 this._terminal.scroll();
125 }
126 }
127
128 /**
129 * CR
130 * Carriage Return (Ctrl-M).
131 */
132 public carriageReturn(): void {
133 this._terminal.x = 0;
134 }
135
136 /**
137 * BS
138 * Backspace (Ctrl-H).
139 */
140 public backspace(): void {
141 if (this._terminal.x > 0) {
142 this._terminal.x--;
143 }
144 }
145
146 /**
147 * TAB
148 * Horizontal Tab (HT) (Ctrl-I).
149 */
150 public tab(): void {
151 this._terminal.x = this._terminal.nextStop();
152 }
153
154 /**
155 * SO
156 * Shift Out (Ctrl-N) -> Switch to Alternate Character Set. This invokes the
157 * G1 character set.
158 */
159 public shiftOut(): void {
160 this._terminal.setgLevel(1);
161 }
162
163 /**
164 * SI
165 * Shift In (Ctrl-O) -> Switch to Standard Character Set. This invokes the G0
166 * character set (the default).
167 */
168 public shiftIn(): void {
169 this._terminal.setgLevel(0);
170 }
171
172 /**
173 * CSI Ps @
174 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
175 */
176 public insertChars(params: number[]): void {
177 let param, row, j, ch;
178
179 param = params[0];
180 if (param < 1) param = 1;
181
182 row = this._terminal.y + this._terminal.ybase;
183 j = this._terminal.x;
184 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
185
186 while (param-- && j < this._terminal.cols) {
187 this._terminal.lines.get(row).splice(j++, 0, ch);
188 this._terminal.lines.get(row).pop();
189 }
190 }
191
192 /**
193 * CSI Ps A
194 * Cursor Up Ps Times (default = 1) (CUU).
195 */
196 public cursorUp(params: number[]): void {
197 let param = params[0];
198 if (param < 1) {
199 param = 1;
200 }
201 this._terminal.y -= param;
202 if (this._terminal.y < 0) {
203 this._terminal.y = 0;
204 }
205 }
206
207 /**
208 * CSI Ps B
209 * Cursor Down Ps Times (default = 1) (CUD).
210 */
211 public cursorDown(params: number[]) {
212 let param = params[0];
213 if (param < 1) {
214 param = 1;
215 }
216 this._terminal.y += param;
217 if (this._terminal.y >= this._terminal.rows) {
218 this._terminal.y = this._terminal.rows - 1;
219 }
220 }
221
222 /**
223 * CSI Ps C
224 * Cursor Forward Ps Times (default = 1) (CUF).
225 */
226 public cursorForward(params: number[]) {
227 let param = params[0];
228 if (param < 1) {
229 param = 1;
230 }
231 this._terminal.x += param;
232 if (this._terminal.x >= this._terminal.cols) {
233 this._terminal.x = this._terminal.cols - 1;
234 }
235 }
236
237 /**
238 * CSI Ps D
239 * Cursor Backward Ps Times (default = 1) (CUB).
240 */
241 public cursorBackward(params: number[]) {
242 let param = params[0];
243 if (param < 1) {
244 param = 1;
245 }
246 this._terminal.x -= param;
247 if (this._terminal.x < 0) {
248 this._terminal.x = 0;
249 }
250 }
251
252 /**
253 * CSI Ps E
254 * Cursor Next Line Ps Times (default = 1) (CNL).
255 * same as CSI Ps B ?
256 */
257 public cursorNextLine(params: number[]): void {
258 let param = params[0];
259 if (param < 1) {
260 param = 1;
261 }
262 this._terminal.y += param;
263 if (this._terminal.y >= this._terminal.rows) {
264 this._terminal.y = this._terminal.rows - 1;
265 }
266 this._terminal.x = 0;
267 };
268
269
270 /**
271 * CSI Ps F
272 * Cursor Preceding Line Ps Times (default = 1) (CNL).
273 * reuse CSI Ps A ?
274 */
275 public cursorPrecedingLine(params: number[]): void {
276 let param = params[0];
277 if (param < 1) {
278 param = 1;
279 }
280 this._terminal.y -= param;
281 if (this._terminal.y < 0) {
282 this._terminal.y = 0;
283 }
284 this._terminal.x = 0;
285 };
286
287
288 /**
289 * CSI Ps G
290 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
291 */
292 public cursorCharAbsolute(params: number[]): void {
293 let param = params[0];
294 if (param < 1) {
295 param = 1;
296 }
297 this._terminal.x = param - 1;
298 }
299
300 /**
301 * CSI Ps ; Ps H
302 * Cursor Position [row;column] (default = [1,1]) (CUP).
303 */
304 public cursorPosition(params: number[]): void {
305 let row, col;
306
307 row = params[0] - 1;
308
309 if (params.length >= 2) {
310 col = params[1] - 1;
311 } else {
312 col = 0;
313 }
314
315 if (row < 0) {
316 row = 0;
317 } else if (row >= this._terminal.rows) {
318 row = this._terminal.rows - 1;
319 }
320
321 if (col < 0) {
322 col = 0;
323 } else if (col >= this._terminal.cols) {
324 col = this._terminal.cols - 1;
325 }
326
327 this._terminal.x = col;
328 this._terminal.y = row;
329 }
330
331 /**
332 * CSI Ps I
333 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
334 */
335 public cursorForwardTab(params: number[]): void {
336 let param = params[0] || 1;
337 while (param--) {
338 this._terminal.x = this._terminal.nextStop();
339 }
340 }
341
342 /**
343 * CSI Ps J Erase in Display (ED).
344 * Ps = 0 -> Erase Below (default).
345 * Ps = 1 -> Erase Above.
346 * Ps = 2 -> Erase All.
347 * Ps = 3 -> Erase Saved Lines (xterm).
348 * CSI ? Ps J
349 * Erase in Display (DECSED).
350 * Ps = 0 -> Selective Erase Below (default).
351 * Ps = 1 -> Selective Erase Above.
352 * Ps = 2 -> Selective Erase All.
353 */
354 public eraseInDisplay(params: number[]): void {
355 let j;
356 switch (params[0]) {
357 case 0:
358 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
359 j = this._terminal.y + 1;
360 for (; j < this._terminal.rows; j++) {
361 this._terminal.eraseLine(j);
362 }
363 break;
364 case 1:
365 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
366 j = this._terminal.y;
367 while (j--) {
368 this._terminal.eraseLine(j);
369 }
370 break;
371 case 2:
372 j = this._terminal.rows;
373 while (j--) this._terminal.eraseLine(j);
374 break;
375 case 3:
376 ; // no saved lines
377 break;
378 }
379 }
380
381 /**
382 * CSI Ps K Erase in Line (EL).
383 * Ps = 0 -> Erase to Right (default).
384 * Ps = 1 -> Erase to Left.
385 * Ps = 2 -> Erase All.
386 * CSI ? Ps K
387 * Erase in Line (DECSEL).
388 * Ps = 0 -> Selective Erase to Right (default).
389 * Ps = 1 -> Selective Erase to Left.
390 * Ps = 2 -> Selective Erase All.
391 */
392 public eraseInLine(params: number[]): void {
393 switch (params[0]) {
394 case 0:
395 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
396 break;
397 case 1:
398 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
399 break;
400 case 2:
401 this._terminal.eraseLine(this._terminal.y);
402 break;
403 }
404 }
405
406 /**
407 * CSI Ps L
408 * Insert Ps Line(s) (default = 1) (IL).
409 */
410 public insertLines(params: number[]): void {
411 let param, row, j;
412
413 param = params[0];
414 if (param < 1) {
415 param = 1;
416 }
417 row = this._terminal.y + this._terminal.ybase;
418
419 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
420 j = this._terminal.rows - 1 + this._terminal.ybase - j + 1;
421
422 while (param--) {
423 if (this._terminal.lines.length === this._terminal.lines.maxLength) {
424 // Trim the start of lines to make room for the new line
425 this._terminal.lines.trimStart(1);
426 this._terminal.ybase--;
427 this._terminal.ydisp--;
428 row--;
429 j--;
430 }
431 // test: echo -e '\e[44m\e[1L\e[0m'
432 // blankLine(true) - xterm/linux behavior
433 this._terminal.lines.splice(row, 0, this._terminal.blankLine(true));
434 this._terminal.lines.splice(j, 1);
435 }
436
437 // this.maxRange();
438 this._terminal.updateRange(this._terminal.y);
439 this._terminal.updateRange(this._terminal.scrollBottom);
440 }
441
442 /**
443 * CSI Ps M
444 * Delete Ps Line(s) (default = 1) (DL).
445 */
446 public deleteLines(params: number[]): void {
447 let param, row, j;
448
449 param = params[0];
450 if (param < 1) {
451 param = 1;
452 }
453 row = this._terminal.y + this._terminal.ybase;
454
455 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
456 j = this._terminal.rows - 1 + this._terminal.ybase - j;
457
458 while (param--) {
459 if (this._terminal.lines.length === this._terminal.lines.maxLength) {
460 // Trim the start of lines to make room for the new line
461 this._terminal.lines.trimStart(1);
462 this._terminal.ybase -= 1;
463 this._terminal.ydisp -= 1;
464 }
465 // test: echo -e '\e[44m\e[1M\e[0m'
466 // blankLine(true) - xterm/linux behavior
467 this._terminal.lines.splice(j + 1, 0, this._terminal.blankLine(true));
468 this._terminal.lines.splice(row, 1);
469 }
470
471 // this.maxRange();
472 this._terminal.updateRange(this._terminal.y);
473 this._terminal.updateRange(this._terminal.scrollBottom);
474 }
475
476 /**
477 * CSI Ps P
478 * Delete Ps Character(s) (default = 1) (DCH).
479 */
480 public deleteChars(params: number[]): void {
481 let param, row, ch;
482
483 param = params[0];
484 if (param < 1) {
485 param = 1;
486 }
487
488 row = this._terminal.y + this._terminal.ybase;
489 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
490
491 while (param--) {
492 this._terminal.lines.get(row).splice(this._terminal.x, 1);
493 this._terminal.lines.get(row).push(ch);
494 }
495 }
496
497 /**
498 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
499 */
500 public scrollUp(params: number[]): void {
501 let param = params[0] || 1;
502 while (param--) {
503 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 1);
504 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 0, this._terminal.blankLine());
505 }
506 // this.maxRange();
507 this._terminal.updateRange(this._terminal.scrollTop);
508 this._terminal.updateRange(this._terminal.scrollBottom);
509 }
510
511 /**
512 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
513 */
514 public scrollDown(params: number[]): void {
515 let param = params[0] || 1;
516 while (param--) {
517 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 1);
518 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 0, this._terminal.blankLine());
519 }
520 // this.maxRange();
521 this._terminal.updateRange(this._terminal.scrollTop);
522 this._terminal.updateRange(this._terminal.scrollBottom);
523 }
524
525 /**
526 * CSI Ps X
527 * Erase Ps Character(s) (default = 1) (ECH).
528 */
529 public eraseChars(params: number[]): void {
530 let param, row, j, ch;
531
532 param = params[0];
533 if (param < 1) {
534 param = 1;
535 }
536
537 row = this._terminal.y + this._terminal.ybase;
538 j = this._terminal.x;
539 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
540
541 while (param-- && j < this._terminal.cols) {
542 this._terminal.lines.get(row)[j++] = ch;
543 }
544 }
545
546 /**
547 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
548 */
549 public cursorBackwardTab(params: number[]): void {
550 let param = params[0] || 1;
551 while (param--) {
552 this._terminal.x = this._terminal.prevStop();
553 }
554 }
555
556 /**
557 * CSI Pm ` Character Position Absolute
558 * [column] (default = [row,1]) (HPA).
559 */
560 public charPosAbsolute(params: number[]): void {
561 let param = params[0];
562 if (param < 1) {
563 param = 1;
564 }
565 this._terminal.x = param - 1;
566 if (this._terminal.x >= this._terminal.cols) {
567 this._terminal.x = this._terminal.cols - 1;
568 }
569 }
570
571 /**
572 * CSI Pm a Character Position Relative
573 * [columns] (default = [row,col+1]) (HPR)
574 * reuse CSI Ps C ?
575 */
576 public HPositionRelative(params: number[]): void {
577 let param = params[0];
578 if (param < 1) {
579 param = 1;
580 }
581 this._terminal.x += param;
582 if (this._terminal.x >= this._terminal.cols) {
583 this._terminal.x = this._terminal.cols - 1;
584 }
585 }
586
587 /**
588 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
589 */
590 public repeatPrecedingCharacter(params: number[]): void {
591 let param = params[0] || 1
592 , line = this._terminal.lines.get(this._terminal.ybase + this._terminal.y)
593 , ch = line[this._terminal.x - 1] || [this._terminal.defAttr, ' ', 1];
594
595 while (param--) {
596 line[this._terminal.x++] = ch;
597 }
598 }
599
600 /**
601 * CSI Ps c Send Device Attributes (Primary DA).
602 * Ps = 0 or omitted -> request attributes from terminal. The
603 * response depends on the decTerminalID resource setting.
604 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
605 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
606 * -> CSI ? 6 c (``VT102'')
607 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
608 * The VT100-style response parameters do not mean anything by
609 * themselves. VT220 parameters do, telling the host what fea-
610 * tures the terminal supports:
611 * Ps = 1 -> 132-columns.
612 * Ps = 2 -> Printer.
613 * Ps = 6 -> Selective erase.
614 * Ps = 8 -> User-defined keys.
615 * Ps = 9 -> National replacement character sets.
616 * Ps = 1 5 -> Technical characters.
617 * Ps = 2 2 -> ANSI color, e.g., VT525.
618 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
619 * CSI > Ps c
620 * Send Device Attributes (Secondary DA).
621 * Ps = 0 or omitted -> request the terminal's identification
622 * code. The response depends on the decTerminalID resource set-
623 * ting. It should apply only to VT220 and up, but xterm extends
624 * this to VT100.
625 * -> CSI > Pp ; Pv ; Pc c
626 * where Pp denotes the terminal type
627 * Pp = 0 -> ``VT100''.
628 * Pp = 1 -> ``VT220''.
629 * and Pv is the firmware version (for xterm, this was originally
630 * the XFree86 patch number, starting with 95). In a DEC termi-
631 * nal, Pc indicates the ROM cartridge registration number and is
632 * always zero.
633 * More information:
634 * xterm/charproc.c - line 2012, for more information.
635 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
636 */
637 public sendDeviceAttributes(params: number[]): void {
638 if (params[0] > 0) {
639 return;
640 }
641
642 if (!this._terminal.prefix) {
643 if (this._terminal.is('xterm') || this._terminal.is('rxvt-unicode') || this._terminal.is('screen')) {
644 this._terminal.send(C0.ESC + '[?1;2c');
645 } else if (this._terminal.is('linux')) {
646 this._terminal.send(C0.ESC + '[?6c');
647 }
648 } else if (this._terminal.prefix === '>') {
649 // xterm and urxvt
650 // seem to spit this
651 // out around ~370 times (?).
652 if (this._terminal.is('xterm')) {
653 this._terminal.send(C0.ESC + '[>0;276;0c');
654 } else if (this._terminal.is('rxvt-unicode')) {
655 this._terminal.send(C0.ESC + '[>85;95;0c');
656 } else if (this._terminal.is('linux')) {
657 // not supported by linux console.
658 // linux console echoes parameters.
659 this._terminal.send(params[0] + 'c');
660 } else if (this._terminal.is('screen')) {
661 this._terminal.send(C0.ESC + '[>83;40003;0c');
662 }
663 }
664 }
665
666 /**
667 * CSI Pm d Vertical Position Absolute (VPA)
668 * [row] (default = [1,column])
669 */
670 public linePosAbsolute(params: number[]): void {
671 let param = params[0];
672 if (param < 1) {
673 param = 1;
674 }
675 this._terminal.y = param - 1;
676 if (this._terminal.y >= this._terminal.rows) {
677 this._terminal.y = this._terminal.rows - 1;
678 }
679 }
680
681 /**
682 * CSI Pm e Vertical Position Relative (VPR)
683 * [rows] (default = [row+1,column])
684 * reuse CSI Ps B ?
685 */
686 public VPositionRelative(params: number[]): void {
687 let param = params[0];
688 if (param < 1) {
689 param = 1;
690 }
691 this._terminal.y += param;
692 if (this._terminal.y >= this._terminal.rows) {
693 this._terminal.y = this._terminal.rows - 1;
694 }
695 }
696
697 /**
698 * CSI Ps ; Ps f
699 * Horizontal and Vertical Position [row;column] (default =
700 * [1,1]) (HVP).
701 */
702 public HVPosition(params: number[]): void {
703 if (params[0] < 1) params[0] = 1;
704 if (params[1] < 1) params[1] = 1;
705
706 this._terminal.y = params[0] - 1;
707 if (this._terminal.y >= this._terminal.rows) {
708 this._terminal.y = this._terminal.rows - 1;
709 }
710
711 this._terminal.x = params[1] - 1;
712 if (this._terminal.x >= this._terminal.cols) {
713 this._terminal.x = this._terminal.cols - 1;
714 }
715 }
716
717 /**
718 * CSI Ps g Tab Clear (TBC).
719 * Ps = 0 -> Clear Current Column (default).
720 * Ps = 3 -> Clear All.
721 * Potentially:
722 * Ps = 2 -> Clear Stops on Line.
723 * http://vt100.net/annarbor/aaa-ug/section6.html
724 */
725 public tabClear(params: number[]): void {
726 let param = params[0];
727 if (param <= 0) {
728 delete this._terminal.tabs[this._terminal.x];
729 } else if (param === 3) {
730 this._terminal.tabs = {};
731 }
732 }
733
734 /**
735 * CSI Pm h Set Mode (SM).
736 * Ps = 2 -> Keyboard Action Mode (AM).
737 * Ps = 4 -> Insert Mode (IRM).
738 * Ps = 1 2 -> Send/receive (SRM).
739 * Ps = 2 0 -> Automatic Newline (LNM).
740 * CSI ? Pm h
741 * DEC Private Mode Set (DECSET).
742 * Ps = 1 -> Application Cursor Keys (DECCKM).
743 * Ps = 2 -> Designate USASCII for character sets G0-G3
744 * (DECANM), and set VT100 mode.
745 * Ps = 3 -> 132 Column Mode (DECCOLM).
746 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
747 * Ps = 5 -> Reverse Video (DECSCNM).
748 * Ps = 6 -> Origin Mode (DECOM).
749 * Ps = 7 -> Wraparound Mode (DECAWM).
750 * Ps = 8 -> Auto-repeat Keys (DECARM).
751 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
752 * tion Mouse Tracking.
753 * Ps = 1 0 -> Show toolbar (rxvt).
754 * Ps = 1 2 -> Start Blinking Cursor (att610).
755 * Ps = 1 8 -> Print form feed (DECPFF).
756 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
757 * Ps = 2 5 -> Show Cursor (DECTCEM).
758 * Ps = 3 0 -> Show scrollbar (rxvt).
759 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
760 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
761 * Ps = 4 0 -> Allow 80 -> 132 Mode.
762 * Ps = 4 1 -> more(1) fix (see curses resource).
763 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
764 * RCM).
765 * Ps = 4 4 -> Turn On Margin Bell.
766 * Ps = 4 5 -> Reverse-wraparound Mode.
767 * Ps = 4 6 -> Start Logging. This is normally disabled by a
768 * compile-time option.
769 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
770 * abled by the titeInhibit resource).
771 * Ps = 6 6 -> Application keypad (DECNKM).
772 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
773 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
774 * release. See the section Mouse Tracking.
775 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
776 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
777 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
778 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
779 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
780 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
781 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
782 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
783 * (enables the eightBitInput resource).
784 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
785 * Lock keys. (This enables the numLock resource).
786 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
787 * enables the metaSendsEscape resource).
788 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
789 * key.
790 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
791 * enables the altSendsEscape resource).
792 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
793 * (This enables the keepSelection resource).
794 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
795 * the selectToClipboard resource).
796 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
797 * Control-G is received. (This enables the bellIsUrgent
798 * resource).
799 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
800 * is received. (enables the popOnBell resource).
801 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
802 * disabled by the titeInhibit resource).
803 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
804 * abled by the titeInhibit resource).
805 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
806 * Screen Buffer, clearing it first. (This may be disabled by
807 * the titeInhibit resource). This combines the effects of the 1
808 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
809 * applications rather than the 4 7 mode.
810 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
811 * Ps = 1 0 5 1 -> Set Sun function-key mode.
812 * Ps = 1 0 5 2 -> Set HP function-key mode.
813 * Ps = 1 0 5 3 -> Set SCO function-key mode.
814 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
815 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
816 * Ps = 2 0 0 4 -> Set bracketed paste mode.
817 * Modes:
818 * http: *vt100.net/docs/vt220-rm/chapter4.html
819 */
820 public setMode(params: number[]): void {
821 if (params.length > 1) {
822 for (let i = 0; i < params.length; i++) {
823 this._terminal.setMode(params[i]);
824 }
825
826 return;
827 }
828
829 if (!this._terminal.prefix) {
830 switch (params[0]) {
831 case 4:
832 this._terminal.insertMode = true;
833 break;
834 case 20:
835 // this._terminal.convertEol = true;
836 break;
837 }
838 } else if (this._terminal.prefix === '?') {
839 switch (params[0]) {
840 case 1:
841 this._terminal.applicationCursor = true;
842 break;
843 case 2:
844 this._terminal.setgCharset(0, CHARSETS.US);
845 this._terminal.setgCharset(1, CHARSETS.US);
846 this._terminal.setgCharset(2, CHARSETS.US);
847 this._terminal.setgCharset(3, CHARSETS.US);
848 // set VT100 mode here
849 break;
850 case 3: // 132 col mode
851 this._terminal.savedCols = this._terminal.cols;
852 this._terminal.resize(132, this._terminal.rows);
853 break;
854 case 6:
855 this._terminal.originMode = true;
856 break;
857 case 7:
858 this._terminal.wraparoundMode = true;
859 break;
860 case 12:
861 // this.cursorBlink = true;
862 break;
863 case 66:
864 this._terminal.log('Serial port requested application keypad.');
865 this._terminal.applicationKeypad = true;
866 this._terminal.viewport.syncScrollArea();
867 break;
868 case 9: // X10 Mouse
869 // no release, no motion, no wheel, no modifiers.
870 case 1000: // vt200 mouse
871 // no motion.
872 // no modifiers, except control on the wheel.
873 case 1002: // button event mouse
874 case 1003: // any event mouse
875 // any event - sends motion events,
876 // even if there is no button held down.
877
878 // TODO: Why are params[0] compares nested within a switch for params[0]?
879
880 this._terminal.x10Mouse = params[0] === 9;
881 this._terminal.vt200Mouse = params[0] === 1000;
882 this._terminal.normalMouse = params[0] > 1000;
883 this._terminal.mouseEvents = true;
884 this._terminal.element.style.cursor = 'default';
885 this._terminal.log('Binding to mouse events.');
886 break;
887 case 1004: // send focusin/focusout events
888 // focusin: ^[[I
889 // focusout: ^[[O
890 this._terminal.sendFocus = true;
891 break;
892 case 1005: // utf8 ext mode mouse
893 this._terminal.utfMouse = true;
894 // for wide terminals
895 // simply encodes large values as utf8 characters
896 break;
897 case 1006: // sgr ext mode mouse
898 this._terminal.sgrMouse = true;
899 // for wide terminals
900 // does not add 32 to fields
901 // press: ^[[<b;x;yM
902 // release: ^[[<b;x;ym
903 break;
904 case 1015: // urxvt ext mode mouse
905 this._terminal.urxvtMouse = true;
906 // for wide terminals
907 // numbers for fields
908 // press: ^[[b;x;yM
909 // motion: ^[[b;x;yT
910 break;
911 case 25: // show cursor
912 this._terminal.cursorHidden = false;
913 break;
914 case 1049: // alt screen buffer cursor
915 // this._terminal.saveCursor();
916 ; // FALL-THROUGH
917 case 47: // alt screen buffer
918 case 1047: // alt screen buffer
919 if (!this._terminal.normal) {
920 let normal = {
921 lines: this._terminal.lines,
922 ybase: this._terminal.ybase,
923 ydisp: this._terminal.ydisp,
924 x: this._terminal.x,
925 y: this._terminal.y,
926 scrollTop: this._terminal.scrollTop,
927 scrollBottom: this._terminal.scrollBottom,
928 tabs: this._terminal.tabs
929 // XXX save charset(s) here?
930 // charset: this._terminal.charset,
931 // glevel: this._terminal.glevel,
932 // charsets: this._terminal.charsets
933 };
934 this._terminal.reset();
935 this._terminal.viewport.syncScrollArea();
936 this._terminal.normal = normal;
937 this._terminal.showCursor();
938 }
939 break;
940 }
941 }
942 }
943
944 /**
945 * CSI Pm l Reset Mode (RM).
946 * Ps = 2 -> Keyboard Action Mode (AM).
947 * Ps = 4 -> Replace Mode (IRM).
948 * Ps = 1 2 -> Send/receive (SRM).
949 * Ps = 2 0 -> Normal Linefeed (LNM).
950 * CSI ? Pm l
951 * DEC Private Mode Reset (DECRST).
952 * Ps = 1 -> Normal Cursor Keys (DECCKM).
953 * Ps = 2 -> Designate VT52 mode (DECANM).
954 * Ps = 3 -> 80 Column Mode (DECCOLM).
955 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
956 * Ps = 5 -> Normal Video (DECSCNM).
957 * Ps = 6 -> Normal Cursor Mode (DECOM).
958 * Ps = 7 -> No Wraparound Mode (DECAWM).
959 * Ps = 8 -> No Auto-repeat Keys (DECARM).
960 * Ps = 9 -> Don't send Mouse X & Y on button press.
961 * Ps = 1 0 -> Hide toolbar (rxvt).
962 * Ps = 1 2 -> Stop Blinking Cursor (att610).
963 * Ps = 1 8 -> Don't print form feed (DECPFF).
964 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
965 * Ps = 2 5 -> Hide Cursor (DECTCEM).
966 * Ps = 3 0 -> Don't show scrollbar (rxvt).
967 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
968 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
969 * Ps = 4 1 -> No more(1) fix (see curses resource).
970 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
971 * NRCM).
972 * Ps = 4 4 -> Turn Off Margin Bell.
973 * Ps = 4 5 -> No Reverse-wraparound Mode.
974 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
975 * compile-time option).
976 * Ps = 4 7 -> Use Normal Screen Buffer.
977 * Ps = 6 6 -> Numeric keypad (DECNKM).
978 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
979 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
980 * release. See the section Mouse Tracking.
981 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
982 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
983 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
984 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
985 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
986 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
987 * (rxvt).
988 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
989 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
990 * the eightBitInput resource).
991 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
992 * Lock keys. (This disables the numLock resource).
993 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
994 * (This disables the metaSendsEscape resource).
995 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
996 * Delete key.
997 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
998 * (This disables the altSendsEscape resource).
999 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
1000 * (This disables the keepSelection resource).
1001 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
1002 * the selectToClipboard resource).
1003 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
1004 * Control-G is received. (This disables the bellIsUrgent
1005 * resource).
1006 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
1007 * G is received. (This disables the popOnBell resource).
1008 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
1009 * first if in the Alternate Screen. (This may be disabled by
1010 * the titeInhibit resource).
1011 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
1012 * disabled by the titeInhibit resource).
1013 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
1014 * as in DECRC. (This may be disabled by the titeInhibit
1015 * resource). This combines the effects of the 1 0 4 7 and 1 0
1016 * 4 8 modes. Use this with terminfo-based applications rather
1017 * than the 4 7 mode.
1018 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
1019 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
1020 * Ps = 1 0 5 2 -> Reset HP function-key mode.
1021 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
1022 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
1023 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
1024 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
1025 */
1026 public resetMode(params: number[]): void {
1027 if (params.length > 1) {
1028 for (let i = 0; i < params.length; i++) {
1029 this._terminal.resetMode(params[i]);
1030 }
1031
1032 return;
1033 }
1034
1035 if (!this._terminal.prefix) {
1036 switch (params[0]) {
1037 case 4:
1038 this._terminal.insertMode = false;
1039 break;
1040 case 20:
1041 // this._terminal.convertEol = false;
1042 break;
1043 }
1044 } else if (this._terminal.prefix === '?') {
1045 switch (params[0]) {
1046 case 1:
1047 this._terminal.applicationCursor = false;
1048 break;
1049 case 3:
1050 if (this._terminal.cols === 132 && this._terminal.savedCols) {
1051 this._terminal.resize(this._terminal.savedCols, this._terminal.rows);
1052 }
1053 delete this._terminal.savedCols;
1054 break;
1055 case 6:
1056 this._terminal.originMode = false;
1057 break;
1058 case 7:
1059 this._terminal.wraparoundMode = false;
1060 break;
1061 case 12:
1062 // this.cursorBlink = false;
1063 break;
1064 case 66:
1065 this._terminal.log('Switching back to normal keypad.');
1066 this._terminal.applicationKeypad = false;
1067 this._terminal.viewport.syncScrollArea();
1068 break;
1069 case 9: // X10 Mouse
1070 case 1000: // vt200 mouse
1071 case 1002: // button event mouse
1072 case 1003: // any event mouse
1073 this._terminal.x10Mouse = false;
1074 this._terminal.vt200Mouse = false;
1075 this._terminal.normalMouse = false;
1076 this._terminal.mouseEvents = false;
1077 this._terminal.element.style.cursor = '';
1078 break;
1079 case 1004: // send focusin/focusout events
1080 this._terminal.sendFocus = false;
1081 break;
1082 case 1005: // utf8 ext mode mouse
1083 this._terminal.utfMouse = false;
1084 break;
1085 case 1006: // sgr ext mode mouse
1086 this._terminal.sgrMouse = false;
1087 break;
1088 case 1015: // urxvt ext mode mouse
1089 this._terminal.urxvtMouse = false;
1090 break;
1091 case 25: // hide cursor
1092 this._terminal.cursorHidden = true;
1093 break;
1094 case 1049: // alt screen buffer cursor
1095 ; // FALL-THROUGH
1096 case 47: // normal screen buffer
1097 case 1047: // normal screen buffer - clearing it first
1098 if (this._terminal.normal) {
1099 this._terminal.lines = this._terminal.normal.lines;
1100 this._terminal.ybase = this._terminal.normal.ybase;
1101 this._terminal.ydisp = this._terminal.normal.ydisp;
1102 this._terminal.x = this._terminal.normal.x;
1103 this._terminal.y = this._terminal.normal.y;
1104 this._terminal.scrollTop = this._terminal.normal.scrollTop;
1105 this._terminal.scrollBottom = this._terminal.normal.scrollBottom;
1106 this._terminal.tabs = this._terminal.normal.tabs;
1107 this._terminal.normal = null;
1108 // if (params === 1049) {
1109 // this.x = this.savedX;
1110 // this.y = this.savedY;
1111 // }
1112 this._terminal.queueRefresh(0, this._terminal.rows - 1);
1113 this._terminal.viewport.syncScrollArea();
1114 this._terminal.showCursor();
1115 }
1116 break;
1117 }
1118 }
1119 }
1120
1121 /**
1122 * CSI Pm m Character Attributes (SGR).
1123 * Ps = 0 -> Normal (default).
1124 * Ps = 1 -> Bold.
1125 * Ps = 4 -> Underlined.
1126 * Ps = 5 -> Blink (appears as Bold).
1127 * Ps = 7 -> Inverse.
1128 * Ps = 8 -> Invisible, i.e., hidden (VT300).
1129 * Ps = 2 2 -> Normal (neither bold nor faint).
1130 * Ps = 2 4 -> Not underlined.
1131 * Ps = 2 5 -> Steady (not blinking).
1132 * Ps = 2 7 -> Positive (not inverse).
1133 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
1134 * Ps = 3 0 -> Set foreground color to Black.
1135 * Ps = 3 1 -> Set foreground color to Red.
1136 * Ps = 3 2 -> Set foreground color to Green.
1137 * Ps = 3 3 -> Set foreground color to Yellow.
1138 * Ps = 3 4 -> Set foreground color to Blue.
1139 * Ps = 3 5 -> Set foreground color to Magenta.
1140 * Ps = 3 6 -> Set foreground color to Cyan.
1141 * Ps = 3 7 -> Set foreground color to White.
1142 * Ps = 3 9 -> Set foreground color to default (original).
1143 * Ps = 4 0 -> Set background color to Black.
1144 * Ps = 4 1 -> Set background color to Red.
1145 * Ps = 4 2 -> Set background color to Green.
1146 * Ps = 4 3 -> Set background color to Yellow.
1147 * Ps = 4 4 -> Set background color to Blue.
1148 * Ps = 4 5 -> Set background color to Magenta.
1149 * Ps = 4 6 -> Set background color to Cyan.
1150 * Ps = 4 7 -> Set background color to White.
1151 * Ps = 4 9 -> Set background color to default (original).
1152 *
1153 * If 16-color support is compiled, the following apply. Assume
1154 * that xterm's resources are set so that the ISO color codes are
1155 * the first 8 of a set of 16. Then the aixterm colors are the
1156 * bright versions of the ISO colors:
1157 * Ps = 9 0 -> Set foreground color to Black.
1158 * Ps = 9 1 -> Set foreground color to Red.
1159 * Ps = 9 2 -> Set foreground color to Green.
1160 * Ps = 9 3 -> Set foreground color to Yellow.
1161 * Ps = 9 4 -> Set foreground color to Blue.
1162 * Ps = 9 5 -> Set foreground color to Magenta.
1163 * Ps = 9 6 -> Set foreground color to Cyan.
1164 * Ps = 9 7 -> Set foreground color to White.
1165 * Ps = 1 0 0 -> Set background color to Black.
1166 * Ps = 1 0 1 -> Set background color to Red.
1167 * Ps = 1 0 2 -> Set background color to Green.
1168 * Ps = 1 0 3 -> Set background color to Yellow.
1169 * Ps = 1 0 4 -> Set background color to Blue.
1170 * Ps = 1 0 5 -> Set background color to Magenta.
1171 * Ps = 1 0 6 -> Set background color to Cyan.
1172 * Ps = 1 0 7 -> Set background color to White.
1173 *
1174 * If xterm is compiled with the 16-color support disabled, it
1175 * supports the following, from rxvt:
1176 * Ps = 1 0 0 -> Set foreground and background color to
1177 * default.
1178 *
1179 * If 88- or 256-color support is compiled, the following apply.
1180 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
1181 * Ps.
1182 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
1183 * Ps.
1184 */
1185 public charAttributes(params: number[]): void {
1186 // Optimize a single SGR0.
1187 if (params.length === 1 && params[0] === 0) {
1188 this._terminal.curAttr = this._terminal.defAttr;
1189 return;
1190 }
1191
1192 let l = params.length
1193 , i = 0
1194 , flags = this._terminal.curAttr >> 18
1195 , fg = (this._terminal.curAttr >> 9) & 0x1ff
1196 , bg = this._terminal.curAttr & 0x1ff
1197 , p;
1198
1199 for (; i < l; i++) {
1200 p = params[i];
1201 if (p >= 30 && p <= 37) {
1202 // fg color 8
1203 fg = p - 30;
1204 } else if (p >= 40 && p <= 47) {
1205 // bg color 8
1206 bg = p - 40;
1207 } else if (p >= 90 && p <= 97) {
1208 // fg color 16
1209 p += 8;
1210 fg = p - 90;
1211 } else if (p >= 100 && p <= 107) {
1212 // bg color 16
1213 p += 8;
1214 bg = p - 100;
1215 } else if (p === 0) {
1216 // default
1217 flags = this._terminal.defAttr >> 18;
1218 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1219 bg = this._terminal.defAttr & 0x1ff;
1220 // flags = 0;
1221 // fg = 0x1ff;
1222 // bg = 0x1ff;
1223 } else if (p === 1) {
1224 // bold text
1225 flags |= 1;
1226 } else if (p === 4) {
1227 // underlined text
1228 flags |= 2;
1229 } else if (p === 5) {
1230 // blink
1231 flags |= 4;
1232 } else if (p === 7) {
1233 // inverse and positive
1234 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
1235 flags |= 8;
1236 } else if (p === 8) {
1237 // invisible
1238 flags |= 16;
1239 } else if (p === 22) {
1240 // not bold
1241 flags &= ~1;
1242 } else if (p === 24) {
1243 // not underlined
1244 flags &= ~2;
1245 } else if (p === 25) {
1246 // not blink
1247 flags &= ~4;
1248 } else if (p === 27) {
1249 // not inverse
1250 flags &= ~8;
1251 } else if (p === 28) {
1252 // not invisible
1253 flags &= ~16;
1254 } else if (p === 39) {
1255 // reset fg
1256 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1257 } else if (p === 49) {
1258 // reset bg
1259 bg = this._terminal.defAttr & 0x1ff;
1260 } else if (p === 38) {
1261 // fg color 256
1262 if (params[i + 1] === 2) {
1263 i += 2;
1264 fg = this._terminal.matchColor(
1265 params[i] & 0xff,
1266 params[i + 1] & 0xff,
1267 params[i + 2] & 0xff);
1268 if (fg === -1) fg = 0x1ff;
1269 i += 2;
1270 } else if (params[i + 1] === 5) {
1271 i += 2;
1272 p = params[i] & 0xff;
1273 fg = p;
1274 }
1275 } else if (p === 48) {
1276 // bg color 256
1277 if (params[i + 1] === 2) {
1278 i += 2;
1279 bg = this._terminal.matchColor(
1280 params[i] & 0xff,
1281 params[i + 1] & 0xff,
1282 params[i + 2] & 0xff);
1283 if (bg === -1) bg = 0x1ff;
1284 i += 2;
1285 } else if (params[i + 1] === 5) {
1286 i += 2;
1287 p = params[i] & 0xff;
1288 bg = p;
1289 }
1290 } else if (p === 100) {
1291 // reset fg/bg
1292 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1293 bg = this._terminal.defAttr & 0x1ff;
1294 } else {
1295 this._terminal.error('Unknown SGR attribute: %d.', p);
1296 }
1297 }
1298
1299 this._terminal.curAttr = (flags << 18) | (fg << 9) | bg;
1300 }
1301
1302 /**
1303 * CSI Ps n Device Status Report (DSR).
1304 * Ps = 5 -> Status Report. Result (``OK'') is
1305 * CSI 0 n
1306 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
1307 * Result is
1308 * CSI r ; c R
1309 * CSI ? Ps n
1310 * Device Status Report (DSR, DEC-specific).
1311 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
1312 * ? r ; c R (assumes page is zero).
1313 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
1314 * or CSI ? 1 1 n (not ready).
1315 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
1316 * or CSI ? 2 1 n (locked).
1317 * Ps = 2 6 -> Report Keyboard status as
1318 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
1319 * The last two parameters apply to VT400 & up, and denote key-
1320 * board ready and LK01 respectively.
1321 * Ps = 5 3 -> Report Locator status as
1322 * CSI ? 5 3 n Locator available, if compiled-in, or
1323 * CSI ? 5 0 n No Locator, if not.
1324 */
1325 public deviceStatus(params: number[]): void {
1326 if (!this._terminal.prefix) {
1327 switch (params[0]) {
1328 case 5:
1329 // status report
1330 this._terminal.send(C0.ESC + '[0n');
1331 break;
1332 case 6:
1333 // cursor position
1334 this._terminal.send(C0.ESC + '['
1335 + (this._terminal.y + 1)
1336 + ';'
1337 + (this._terminal.x + 1)
1338 + 'R');
1339 break;
1340 }
1341 } else if (this._terminal.prefix === '?') {
1342 // modern xterm doesnt seem to
1343 // respond to any of these except ?6, 6, and 5
1344 switch (params[0]) {
1345 case 6:
1346 // cursor position
1347 this._terminal.send(C0.ESC + '[?'
1348 + (this._terminal.y + 1)
1349 + ';'
1350 + (this._terminal.x + 1)
1351 + 'R');
1352 break;
1353 case 15:
1354 // no printer
1355 // this.send(C0.ESC + '[?11n');
1356 break;
1357 case 25:
1358 // dont support user defined keys
1359 // this.send(C0.ESC + '[?21n');
1360 break;
1361 case 26:
1362 // north american keyboard
1363 // this.send(C0.ESC + '[?27;1;0;0n');
1364 break;
1365 case 53:
1366 // no dec locator/mouse
1367 // this.send(C0.ESC + '[?50n');
1368 break;
1369 }
1370 }
1371 }
1372
1373 /**
1374 * CSI ! p Soft terminal reset (DECSTR).
1375 * http://vt100.net/docs/vt220-rm/table4-10.html
1376 */
1377 public softReset(params: number[]): void {
1378 this._terminal.cursorHidden = false;
1379 this._terminal.insertMode = false;
1380 this._terminal.originMode = false;
1381 this._terminal.wraparoundMode = false; // autowrap
1382 this._terminal.applicationKeypad = false; // ?
1383 this._terminal.viewport.syncScrollArea();
1384 this._terminal.applicationCursor = false;
1385 this._terminal.scrollTop = 0;
1386 this._terminal.scrollBottom = this._terminal.rows - 1;
1387 this._terminal.curAttr = this._terminal.defAttr;
1388 this._terminal.x = this._terminal.y = 0; // ?
1389 this._terminal.charset = null;
1390 this._terminal.glevel = 0; // ??
1391 this._terminal.charsets = [null]; // ??
1392 }
1393
1394 /**
1395 * CSI Ps SP q Set cursor style (DECSCUSR, VT520).
1396 * Ps = 0 -> blinking block.
1397 * Ps = 1 -> blinking block (default).
1398 * Ps = 2 -> steady block.
1399 * Ps = 3 -> blinking underline.
1400 * Ps = 4 -> steady underline.
1401 * Ps = 5 -> blinking bar (xterm).
1402 * Ps = 6 -> steady bar (xterm).
1403 */
1404 public setCursorStyle(params?: number[]): void {
1405 const param = params[0] < 1 ? 1 : params[0];
1406 switch (param) {
1407 case 1:
1408 case 2:
1409 this._terminal.setOption('cursorStyle', 'block');
1410 break;
1411 case 3:
1412 case 4:
1413 this._terminal.setOption('cursorStyle', 'underline');
1414 break;
1415 case 5:
1416 case 6:
1417 this._terminal.setOption('cursorStyle', 'bar');
1418 break;
1419 }
1420 const isBlinking = param % 2 === 1;
1421 this._terminal.setOption('cursorBlink', isBlinking);
1422 }
1423
1424 /**
1425 * CSI Ps ; Ps r
1426 * Set Scrolling Region [top;bottom] (default = full size of win-
1427 * dow) (DECSTBM).
1428 * CSI ? Pm r
1429 */
1430 public setScrollRegion(params: number[]): void {
1431 if (this._terminal.prefix) return;
1432 this._terminal.scrollTop = (params[0] || 1) - 1;
1433 this._terminal.scrollBottom = (params[1] || this._terminal.rows) - 1;
1434 this._terminal.x = 0;
1435 this._terminal.y = 0;
1436 }
1437
1438
1439 /**
1440 * CSI s
1441 * Save cursor (ANSI.SYS).
1442 */
1443 public saveCursor(params: number[]): void {
1444 this._terminal.savedX = this._terminal.x;
1445 this._terminal.savedY = this._terminal.y;
1446 }
1447
1448
1449 /**
1450 * CSI u
1451 * Restore cursor (ANSI.SYS).
1452 */
1453 public restoreCursor(params: number[]): void {
1454 this._terminal.x = this._terminal.savedX || 0;
1455 this._terminal.y = this._terminal.savedY || 0;
1456 }
1457 }
1458
1459 const wcwidth = (function(opts) {
1460 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1461 // combining characters
1462 const COMBINING = [
1463 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1464 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1465 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1466 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1467 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1468 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1469 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1470 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1471 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1472 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1473 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1474 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1475 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1476 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1477 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1478 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1479 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1480 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1481 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1482 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1483 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1484 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1485 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1486 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1487 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1488 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1489 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1490 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1491 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1492 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1493 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1494 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1495 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1496 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1497 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1498 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1499 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1500 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1501 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1502 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1503 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1504 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1505 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1506 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1507 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1508 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1509 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1510 [0xE0100, 0xE01EF]
1511 ];
1512 // binary search
1513 function bisearch(ucs) {
1514 let min = 0;
1515 let max = COMBINING.length - 1;
1516 let mid;
1517 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
1518 return false;
1519 while (max >= min) {
1520 mid = Math.floor((min + max) / 2);
1521 if (ucs > COMBINING[mid][1])
1522 min = mid + 1;
1523 else if (ucs < COMBINING[mid][0])
1524 max = mid - 1;
1525 else
1526 return true;
1527 }
1528 return false;
1529 }
1530 function wcwidth(ucs) {
1531 // test for 8-bit control characters
1532 if (ucs === 0)
1533 return opts.nul;
1534 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1535 return opts.control;
1536 // binary search in table of non-spacing characters
1537 if (bisearch(ucs))
1538 return 0;
1539 // if we arrive here, ucs is not a combining or C0/C1 control character
1540 if (isWide(ucs)) {
1541 return 2;
1542 }
1543 return 1;
1544 }
1545 function isWide(ucs) {
1546 return (
1547 ucs >= 0x1100 && (
1548 ucs <= 0x115f || // Hangul Jamo init. consonants
1549 ucs === 0x2329 ||
1550 ucs === 0x232a ||
1551 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1552 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1553 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1554 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1555 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1556 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1557 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1558 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1559 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1560 }
1561 return wcwidth;
1562 })({nul: 0, control: 0}); // configurable options