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