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