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