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