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