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