2 * xterm.js: xterm, in the browser
3 * Originally forked from (with the author's permission):
4 * Fabrice Bellard's javascript vt100 for jslinux:
5 * http://bellard.org/jslinux/
6 * Copyright (c) 2011 Fabrice Bellard
7 * The original design remains. The terminal itself
8 * has been extended to include xterm CSI codes, among
13 import { CompositionHelper
} from './CompositionHelper';
14 import { EventEmitter
} from './EventEmitter';
15 import { Viewport
} from './Viewport';
16 import { rightClickHandler
, pasteHandler
, copyHandler
} from './handlers/Clipboard';
17 import { CircularList
} from './utils/CircularList';
18 import { C0
} from './EscapeSequences';
19 import { InputHandler
} from './InputHandler';
20 import { Parser
} from './Parser';
21 import { Renderer
} from './Renderer';
22 import { Linkifier
} from './Linkifier';
23 import { CharMeasure
} from './utils/CharMeasure';
24 import * as Browser
from './utils/Browser';
25 import * as Keyboard
from './utils/Keyboard';
26 import { CHARSETS
} from './Charsets';
29 * Terminal Emulation References:
31 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
32 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
33 * http://invisible-island.net/vttest/
34 * http://www.inwap.com/pdp10/ansicode.txt
35 * http://linux.die.net/man/4/console_codes
36 * http://linux.die.net/man/7/urxvt
39 // Let it work inside Node.js for automated testing purposes.
40 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
43 * The amount of write requests to queue before sending an XOFF signal to the
44 * pty process. This number must be small in order for ^C and similar sequences
47 var WRITE_BUFFER_PAUSE_THRESHOLD
= 5;
50 * The number of writes to perform in a single batch before allowing the
51 * renderer to catch up with a 0ms setTimeout.
53 var WRITE_BATCH_SIZE
= 300;
60 * Creates a new `Terminal` object.
62 * @param {object} options An object containing a set of options, the available options are:
63 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
64 * - `cols` (number): The number of columns of the terminal (horizontal size)
65 * - `rows` (number): The number of rows of the terminal (vertical size)
69 * @alias module:xterm/src/xterm
71 function Terminal(options
) {
74 if (!(this instanceof Terminal
)) {
75 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
78 self
.browser
= Browser
;
79 self
.cancel
= Terminal
.cancel
;
81 EventEmitter
.call(this);
83 if (typeof options
=== 'number') {
91 options
= options
|| {};
94 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
95 if (options
[key
] == null) {
96 options
[key
] = Terminal
.options
[key
];
98 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
99 options
[key
] = Terminal
[key
];
102 self
[key
] = options
[key
];
105 if (options
.colors
.length
=== 8) {
106 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
107 } else if (options
.colors
.length
=== 16) {
108 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
109 } else if (options
.colors
.length
=== 10) {
110 options
.colors
= options
.colors
.slice(0, -2).concat(
111 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
112 } else if (options
.colors
.length
=== 18) {
113 options
.colors
= options
.colors
.concat(
114 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
116 this.colors
= options
.colors
;
118 this.options
= options
;
120 // this.context = options.context || window;
121 // this.document = options.document || document;
122 this.parent
= options
.body
|| options
.parent
|| (
123 document
? document
.getElementsByTagName('body')[0] : null
126 this.cols
= options
.cols
|| options
.geometry
[0];
127 this.rows
= options
.rows
|| options
.geometry
[1];
128 this.geometry
= [this.cols
, this.rows
];
130 if (options
.handler
) {
131 this.on('data', options
.handler
);
135 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
141 * The scroll position of the viewport
146 * The cursor's x position after ybase
151 * The cursor's y position after ybase
155 this.cursorState
= 0;
156 this.cursorHidden
= false;
160 this.scrollBottom
= this.rows
- 1;
161 this.customKeydownHandler
= null;
164 this.applicationKeypad
= false;
165 this.applicationCursor
= false;
166 this.originMode
= false;
167 this.insertMode
= false;
168 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
173 this.gcharset
= null;
175 this.charsets
= [null];
199 this.readable
= true;
200 this.writable
= true;
202 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
203 this.curAttr
= this.defAttr
;
206 this.currentParam
= 0;
210 this.inputHandler
= new InputHandler(this);
211 this.parser
= new Parser(this.inputHandler
, this);
212 // Reuse renderer if the Terminal is being recreated via a Terminal.reset call.
213 this.renderer
= this.renderer
|| null;
214 this.linkifier
= this.linkifier
|| null;;
217 this.writeBuffer
= [];
218 this.writeInProgress
= false;
221 * Whether _xterm.js_ sent XOFF in order to catch up with the pty process.
222 * This is a distinct state from writeStopped so that if the user requested
223 * XOFF via ^S that it will not automatically resume when the writeBuffer goes
226 this.xoffSentToCatchUp
= false;
228 /** Whether writing has been stopped as a result of XOFF */
229 this.writeStopped
= false;
231 // leftover surrogate high from previous write invocation
232 this.surrogate_high
= '';
235 * An array of all lines in the entire buffer, including the prompt. The lines are array of
236 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
238 this.lines
= new CircularList(this.scrollback
);
241 this.lines
.push(this.blankLine());
247 // Store if user went browsing history in scrollback
248 this.userScrolling
= false;
251 inherits(Terminal
, EventEmitter
);
254 * back_color_erase feature for xterm.
256 Terminal
.prototype.eraseAttr = function() {
257 // if (this.is('screen')) return this.defAttr;
258 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
266 Terminal
.tangoColors
= [
287 // Colors 0-15 + 16-255
288 // Much thanks to TooTallNate for writing this.
289 Terminal
.colors
= (function() {
290 var colors
= Terminal
.tangoColors
.slice()
291 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
296 for (; i
< 216; i
++) {
297 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
302 for (; i
< 24; i
++) {
307 function out(r
, g
, b
) {
308 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
313 return c
.length
< 2 ? '0' + c
: c
;
319 Terminal
._colors
= Terminal
.colors
.slice();
321 Terminal
.vcolors
= (function() {
323 , colors
= Terminal
.colors
327 for (; i
< 256; i
++) {
328 color
= parseInt(colors
[i
].substring(1), 16);
330 (color
>> 16) & 0xff,
343 Terminal
.defaults
= {
344 colors
: Terminal
.colors
,
350 cursorStyle
: 'block',
358 useFlowControl
: false,
360 // programFeatures: false,
364 Terminal
.options
= {};
366 Terminal
.focus
= null;
368 each(keys(Terminal
.defaults
), function(key
) {
369 Terminal
[key
] = Terminal
.defaults
[key
];
370 Terminal
.options
[key
] = Terminal
.defaults
[key
];
374 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
376 Terminal
.prototype.focus = function() {
377 return this.textarea
.focus();
381 * Retrieves an option's value from the terminal.
382 * @param {string} key The option key.
384 Terminal
.prototype.getOption = function(key
, value
) {
385 if (!(key
in Terminal
.defaults
)) {
386 throw new Error('No option with key "' + key
+ '"');
389 if (typeof this.options
[key
] !== 'undefined') {
390 return this.options
[key
];
397 * Sets an option on the terminal.
398 * @param {string} key The option key.
399 * @param {string} value The option value.
401 Terminal
.prototype.setOption = function(key
, value
) {
402 if (!(key
in Terminal
.defaults
)) {
403 throw new Error('No option with key "' + key
+ '"');
407 if (this.options
[key
] !== value
) {
408 if (this.lines
.length
> value
) {
409 const amountToTrim
= this.lines
.length
- value
;
410 const needsRefresh
= (this.ydisp
- amountToTrim
< 0);
411 this.lines
.trimStart(amountToTrim
);
412 this.ybase
= Math
.max(this.ybase
- amountToTrim
, 0);
413 this.ydisp
= Math
.max(this.ydisp
- amountToTrim
, 0);
415 this.refresh(0, this.rows
- 1);
418 this.lines
.maxLength
= value
;
419 this.viewport
.syncScrollArea();
424 this.options
[key
] = value
;
426 case 'cursorBlink': this.element
.classList
.toggle('xterm-cursor-blink', value
); break;
428 // Style 'block' applies with no class
429 this.element
.classList
.toggle(`xterm-cursor-style-underline`, value
=== 'underline');
430 this.element
.classList
.toggle(`xterm-cursor-style-bar`, value
=== 'bar');
432 case 'tabStopWidth': this.setupStops(); break;
437 * Binds the desired focus behavior on a given terminal object.
441 Terminal
.bindFocus = function (term
) {
442 on(term
.textarea
, 'focus', function (ev
) {
443 if (term
.sendFocus
) {
444 term
.send(C0
.ESC
+ '[I');
446 term
.element
.classList
.add('focus');
448 Terminal
.focus
= term
;
449 term
.emit('focus', {terminal
: term
});
454 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
456 Terminal
.prototype.blur = function() {
457 return this.textarea
.blur();
461 * Binds the desired blur behavior on a given terminal object.
465 Terminal
.bindBlur = function (term
) {
466 on(term
.textarea
, 'blur', function (ev
) {
467 term
.refresh(term
.y
, term
.y
);
468 if (term
.sendFocus
) {
469 term
.send(C0
.ESC
+ '[O');
471 term
.element
.classList
.remove('focus');
472 Terminal
.focus
= null;
473 term
.emit('blur', {terminal
: term
});
478 * Initialize default behavior
480 Terminal
.prototype.initGlobal = function() {
483 Terminal
.bindKeys(this);
484 Terminal
.bindFocus(this);
485 Terminal
.bindBlur(this);
487 // Bind clipboard functionality
488 on(this.element
, 'copy', function (ev
) {
489 copyHandler
.call(this, ev
, term
);
491 on(this.textarea
, 'paste', function (ev
) {
492 pasteHandler
.call(this, ev
, term
);
494 on(this.element
, 'paste', function (ev
) {
495 pasteHandler
.call(this, ev
, term
);
498 function rightClickHandlerWrapper (ev
) {
499 rightClickHandler
.call(this, ev
, term
);
502 if (term
.browser
.isFirefox
) {
503 on(this.element
, 'mousedown', function (ev
) {
504 if (ev
.button
== 2) {
505 rightClickHandlerWrapper(ev
);
509 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
514 * Apply key handling to the terminal
516 Terminal
.bindKeys = function(term
) {
517 on(term
.element
, 'keydown', function(ev
) {
518 if (document
.activeElement
!= this) {
524 on(term
.element
, 'keypress', function(ev
) {
525 if (document
.activeElement
!= this) {
531 on(term
.element
, 'keyup', function(ev
) {
532 if (!wasMondifierKeyOnlyEvent(ev
)) {
537 on(term
.textarea
, 'keydown', function(ev
) {
541 on(term
.textarea
, 'keypress', function(ev
) {
543 // Truncate the textarea's value, since it is not needed
547 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
548 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
549 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
550 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
555 * Insert the given row to the terminal or produce a new one
556 * if no row argument is passed. Return the inserted row.
557 * @param {HTMLElement} row (optional) The row to append to the terminal.
559 Terminal
.prototype.insertRow = function (row
) {
560 if (typeof row
!= 'object') {
561 row
= document
.createElement('div');
564 this.rowContainer
.appendChild(row
);
565 this.children
.push(row
);
571 * Opens the terminal within an element.
573 * @param {HTMLElement} parent The element to create the terminal within.
575 Terminal
.prototype.open = function(parent
) {
576 var self
=this, i
=0, div
;
578 this.parent
= parent
|| this.parent
;
581 throw new Error('Terminal requires a parent element.');
584 // Grab global elements
585 this.context
= this.parent
.ownerDocument
.defaultView
;
586 this.document
= this.parent
.ownerDocument
;
587 this.body
= this.document
.getElementsByTagName('body')[0];
589 //Create main element container
590 this.element
= this.document
.createElement('div');
591 this.element
.classList
.add('terminal');
592 this.element
.classList
.add('xterm');
593 this.element
.classList
.add('xterm-theme-' + this.theme
);
594 this.element
.classList
.toggle('xterm-cursor-blink', this.options
.cursorBlink
);
596 this.element
.style
.height
597 this.element
.setAttribute('tabindex', 0);
599 this.viewportElement
= document
.createElement('div');
600 this.viewportElement
.classList
.add('xterm-viewport');
601 this.element
.appendChild(this.viewportElement
);
602 this.viewportScrollArea
= document
.createElement('div');
603 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
604 this.viewportElement
.appendChild(this.viewportScrollArea
);
606 // Create the container that will hold the lines of the terminal and then
607 // produce the lines the lines.
608 this.rowContainer
= document
.createElement('div');
609 this.rowContainer
.classList
.add('xterm-rows');
610 this.element
.appendChild(this.rowContainer
);
612 this.linkifier
= new Linkifier(this.children
);
614 // Create the container that will hold helpers like the textarea for
615 // capturing DOM Events. Then produce the helpers.
616 this.helperContainer
= document
.createElement('div');
617 this.helperContainer
.classList
.add('xterm-helpers');
618 // TODO: This should probably be inserted once it's filled to prevent an additional layout
619 this.element
.appendChild(this.helperContainer
);
620 this.textarea
= document
.createElement('textarea');
621 this.textarea
.classList
.add('xterm-helper-textarea');
622 this.textarea
.setAttribute('autocorrect', 'off');
623 this.textarea
.setAttribute('autocapitalize', 'off');
624 this.textarea
.setAttribute('spellcheck', 'false');
625 this.textarea
.tabIndex
= 0;
626 this.textarea
.addEventListener('focus', function() {
627 self
.emit('focus', {terminal
: self
});
629 this.textarea
.addEventListener('blur', function() {
630 self
.emit('blur', {terminal
: self
});
632 this.helperContainer
.appendChild(this.textarea
);
634 this.compositionView
= document
.createElement('div');
635 this.compositionView
.classList
.add('composition-view');
636 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
637 this.helperContainer
.appendChild(this.compositionView
);
639 this.charSizeStyleElement
= document
.createElement('style');
640 this.helperContainer
.appendChild(this.charSizeStyleElement
);
642 for (; i
< this.rows
; i
++) {
645 this.parent
.appendChild(this.element
);
647 this.charMeasure
= new CharMeasure(this.helperContainer
);
648 this.charMeasure
.on('charsizechanged', function () {
649 self
.updateCharSizeCSS();
651 this.charMeasure
.measure();
653 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasure
);
654 this.renderer
= new Renderer(this);
656 // Setup loop that draws to screen
657 this.refresh(0, this.rows
- 1);
659 // Initialize global actions that
660 // need to be taken on the document.
663 // Ensure there is a Terminal.focus.
666 on(this.element
, 'click', function() {
667 var selection
= document
.getSelection(),
668 collapsed
= selection
.isCollapsed
,
669 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
675 // Listen for mouse events and translate
676 // them into terminal mouse protocols.
680 * This event is emitted when terminal has completed opening.
689 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
690 * @param {string} addon The name of the addon to load
693 Terminal
.loadAddon = function(addon
, callback
) {
694 if (typeof exports
=== 'object' && typeof module
=== 'object') {
696 return require('./addons/' + addon
+ '/' + addon
);
697 } else if (typeof define
== 'function') {
699 return require(['./addons/' + addon
+ '/' + addon
], callback
);
701 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
707 * Updates the helper CSS class with any changes necessary after the terminal's
708 * character width has been changed.
710 Terminal
.prototype.updateCharSizeCSS = function() {
711 this.charSizeStyleElement
.textContent
= '.xterm-wide-char{width:' + (this.charMeasure
.width
* 2) + 'px;}';
716 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
717 * To better understand these
718 * the xterm code is very helpful:
720 * button.c, charproc.c, misc.c
721 * Relevant functions in xterm/button.c:
722 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
724 Terminal
.prototype.bindMouse = function() {
725 var el
= this.element
, self
= this, pressed
= 32;
727 // mouseup, mousedown, wheel
728 // left click: ^[[M 3<^[[M#3<
730 function sendButton(ev
) {
734 // get the xterm-style button
735 button
= getButton(ev
);
737 // get mouse coordinates
741 sendEvent(button
, pos
);
743 switch (ev
.overrideType
|| ev
.type
) {
748 // keep it at the left
749 // button, just in case.
760 // motion example of a left click:
761 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
762 function sendMove(ev
) {
769 // buttons marked as motions
770 // are incremented by 32
773 sendEvent(button
, pos
);
777 // position to characters
778 function encode(data
, ch
) {
779 if (!self
.utfMouse
) {
780 if (ch
=== 255) return data
.push(0);
781 if (ch
> 127) ch
= 127;
784 if (ch
=== 2047) return data
.push(0);
788 if (ch
> 2047) ch
= 2047;
789 data
.push(0xC0 | (ch
>> 6));
790 data
.push(0x80 | (ch
& 0x3F));
795 // send a mouse event:
796 // regular/utf8: ^[[M Cb Cx Cy
797 // urxvt: ^[[ Cb ; Cx ; Cy M
798 // sgr: ^[[ Cb ; Cx ; Cy M/m
799 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
800 // locator: CSI P e ; P b ; P r ; P c ; P p & w
801 function sendEvent(button
, pos
) {
802 // self.emit('mouse', {
808 if (self
.vt300Mouse
) {
810 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
814 var data
= C0
.ESC
+ '[24';
815 if (button
=== 0) data
+= '1';
816 else if (button
=== 1) data
+= '3';
817 else if (button
=== 2) data
+= '5';
818 else if (button
=== 3) return;
820 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
825 if (self
.decLocator
) {
830 if (button
=== 0) button
= 2;
831 else if (button
=== 1) button
= 4;
832 else if (button
=== 2) button
= 6;
833 else if (button
=== 3) button
= 3;
834 self
.send(C0
.ESC
+ '['
837 + (button
=== 3 ? 4 : 0)
848 if (self
.urxvtMouse
) {
853 self
.send(C0
.ESC
+ '[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
860 self
.send(C0
.ESC
+ '[<'
861 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
866 + ((button
& 3) === 3 ? 'm' : 'M'));
872 encode(data
, button
);
876 self
.send(C0
.ESC
+ '[M' + String
.fromCharCode
.apply(String
, data
));
879 function getButton(ev
) {
892 // 1, and 2 - with 64 added
893 switch (ev
.overrideType
|| ev
.type
) {
895 button
= ev
.button
!= null
901 if (self
.browser
.isMSIE
) {
902 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
908 case 'DOMMouseScroll':
909 button
= ev
.detail
< 0
914 button
= ev
.wheelDeltaY
> 0
920 // next three bits are the modifiers:
921 // 4 = shift, 8 = meta, 16 = control
922 shift
= ev
.shiftKey
? 4 : 0;
923 meta
= ev
.metaKey
? 8 : 0;
924 ctrl
= ev
.ctrlKey
? 16 : 0;
925 mod
= shift
| meta
| ctrl
;
928 if (self
.vt200Mouse
) {
931 } else if (!self
.normalMouse
) {
936 button
= (32 + (mod
<< 2)) + button
;
941 // mouse coordinates measured in cols/rows
942 function getCoords(ev
) {
945 // ignore browsers without pageX for now
946 if (ev
.pageX
== null) return;
952 // should probably check offsetParent
953 // but this is more portable
954 while (el
&& el
!== self
.document
.documentElement
) {
957 el
= 'offsetParent' in el
962 // convert to cols/rows
963 w
= self
.element
.clientWidth
;
964 h
= self
.element
.clientHeight
;
965 x
= Math
.ceil((x
/ w
) * self
.cols
);
966 y
= Math
.ceil((y
/ h
) * self
.rows
);
968 // be sure to avoid sending
969 // bad positions to the program
971 if (x
> self
.cols
) x
= self
.cols
;
973 if (y
> self
.rows
) y
= self
.rows
;
975 // xterm sends raw bytes and
976 // starts at 32 (SP) for each.
987 on(el
, 'mousedown', function(ev
) {
988 if (!self
.mouseEvents
) return;
997 //if (self.vt200Mouse && !self.normalMouse) {
998 if (self
.vt200Mouse
) {
999 ev
.overrideType
= 'mouseup';
1001 return self
.cancel(ev
);
1005 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1007 // x10 compatibility mode can't send button releases
1008 if (!self
.x10Mouse
) {
1009 on(self
.document
, 'mouseup', function up(ev
) {
1011 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1012 off(self
.document
, 'mouseup', up
);
1013 return self
.cancel(ev
);
1017 return self
.cancel(ev
);
1020 //if (self.normalMouse) {
1021 // on(self.document, 'mousemove', sendMove);
1024 on(el
, 'wheel', function(ev
) {
1025 if (!self
.mouseEvents
) return;
1028 || self
.decLocator
) return;
1030 return self
.cancel(ev
);
1033 // allow wheel scrolling in
1034 // the shell for example
1035 on(el
, 'wheel', function(ev
) {
1036 if (self
.mouseEvents
) return;
1037 self
.viewport
.onWheel(ev
);
1038 return self
.cancel(ev
);
1043 * Destroys the terminal.
1045 Terminal
.prototype.destroy = function() {
1046 this.readable
= false;
1047 this.writable
= false;
1049 this.handler = function() {};
1050 this.write = function() {};
1051 if (this.element
&& this.element
.parentNode
) {
1052 this.element
.parentNode
.removeChild(this.element
);
1054 //this.emit('close');
1058 * Tells the renderer to refresh terminal content between two rows (inclusive) at the next
1060 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1061 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1063 Terminal
.prototype.refresh = function(start
, end
) {
1064 if (this.renderer
) {
1065 this.renderer
.queueRefresh(start
, end
);
1067 // TODO: DO this better
1068 if (!this.linkifier
) {
1071 for (let i
= start
; i
<= end
; i
++) {
1072 this.linkifier
.linkifyRow(i
);
1078 * Display the cursor element
1080 Terminal
.prototype.showCursor = function() {
1081 if (!this.cursorState
) {
1082 this.cursorState
= 1;
1083 this.refresh(this.y
, this.y
);
1088 * Scroll the terminal down 1 row, creating a blank line.
1090 Terminal
.prototype.scroll = function() {
1093 // Make room for the new row in lines
1094 if (this.lines
.length
=== this.lines
.maxLength
) {
1095 this.lines
.trimStart(1);
1097 if (this.ydisp
!== 0) {
1104 // TODO: Why is this done twice?
1105 if (!this.userScrolling
) {
1106 this.ydisp
= this.ybase
;
1110 row
= this.ybase
+ this.rows
- 1;
1112 // subtract the bottom scroll region
1113 row
-= this.rows
- 1 - this.scrollBottom
;
1115 if (row
=== this.lines
.length
) {
1116 // Optimization: pushing is faster than splicing when they amount to the same behavior
1117 this.lines
.push(this.blankLine());
1120 this.lines
.splice(row
, 0, this.blankLine());
1123 if (this.scrollTop
!== 0) {
1124 if (this.ybase
!== 0) {
1126 if (!this.userScrolling
) {
1127 this.ydisp
= this.ybase
;
1130 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1134 this.updateRange(this.scrollTop
);
1135 this.updateRange(this.scrollBottom
);
1138 * This event is emitted whenever the terminal is scrolled.
1139 * The one parameter passed is the new y display position.
1143 this.emit('scroll', this.ydisp
);
1147 * Scroll the display of the terminal
1148 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1149 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1150 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1151 * viewport originally.
1153 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1155 this.userScrolling
= true;
1156 } else if (disp
+ this.ydisp
>= this.ybase
) {
1157 this.userScrolling
= false;
1162 if (this.ydisp
> this.ybase
) {
1163 this.ydisp
= this.ybase
;
1164 } else if (this.ydisp
< 0) {
1168 if (!suppressScrollEvent
) {
1169 this.emit('scroll', this.ydisp
);
1172 this.refresh(0, this.rows
- 1);
1176 * Scroll the display of the terminal by a number of pages.
1177 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1179 Terminal
.prototype.scrollPages = function(pageCount
) {
1180 this.scrollDisp(pageCount
* (this.rows
- 1));
1184 * Scrolls the display of the terminal to the top.
1186 Terminal
.prototype.scrollToTop = function() {
1187 this.scrollDisp(-this.ydisp
);
1191 * Scrolls the display of the terminal to the bottom.
1193 Terminal
.prototype.scrollToBottom = function() {
1194 this.scrollDisp(this.ybase
- this.ydisp
);
1198 * Writes text to the terminal.
1199 * @param {string} text The text to write to the terminal.
1201 Terminal
.prototype.write = function(data
) {
1202 this.writeBuffer
.push(data
);
1204 // Send XOFF to pause the pty process if the write buffer becomes too large so
1205 // xterm.js can catch up before more data is sent. This is necessary in order
1206 // to keep signals such as ^C responsive.
1207 if (this.options
.useFlowControl
&& !this.xoffSentToCatchUp
&& this.writeBuffer
.length
>= WRITE_BUFFER_PAUSE_THRESHOLD
) {
1208 // XOFF - stop pty pipe
1209 // XON will be triggered by emulator before processing data chunk
1211 this.xoffSentToCatchUp
= true;
1214 if (!this.writeInProgress
&& this.writeBuffer
.length
> 0) {
1215 // Kick off a write which will write all data in sequence recursively
1216 this.writeInProgress
= true;
1217 // Kick off an async innerWrite so more writes can come in while processing data
1219 setTimeout(function () {
1225 Terminal
.prototype.innerWrite = function() {
1226 var writeBatch
= this.writeBuffer
.splice(0, WRITE_BATCH_SIZE
);
1227 while (writeBatch
.length
> 0) {
1228 var data
= writeBatch
.shift();
1229 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1231 // If XOFF was sent in order to catch up with the pty process, resume it if
1232 // the writeBuffer is empty to allow more data to come in.
1233 if (this.xoffSentToCatchUp
&& writeBatch
.length
=== 0 && this.writeBuffer
.length
=== 0) {
1235 this.xoffSentToCatchUp
= false;
1238 this.refreshStart
= this.y
;
1239 this.refreshEnd
= this.y
;
1241 this.parser
.parse(data
);
1243 this.updateRange(this.y
);
1244 this.refresh(this.refreshStart
, this.refreshEnd
);
1246 if (this.writeBuffer
.length
> 0) {
1247 // Allow renderer to catch up before processing the next batch
1249 setTimeout(function () {
1253 this.writeInProgress
= false;
1258 * Writes text to the terminal, followed by a break line character (\n).
1259 * @param {string} text The text to write to the terminal.
1261 Terminal
.prototype.writeln = function(data
) {
1262 this.write(data
+ '\r\n');
1266 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
1267 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
1269 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
1270 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
1271 * the default action. The function returns whether the event should be processed by xterm.js.
1273 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
1274 this.customKeydownHandler
= customKeydownHandler
;
1278 * Handle a keydown event
1280 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
1281 * @param {KeyboardEvent} ev The keydown event to be handled.
1283 Terminal
.prototype.keyDown = function(ev
) {
1284 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
1288 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
1289 if (this.ybase
!== this.ydisp
) {
1290 this.scrollToBottom();
1296 var result
= this.evaluateKeyEscapeSequence(ev
);
1298 if (result
.key
=== C0
.DC3
) { // XOFF
1299 this.writeStopped
= true;
1300 } else if (result
.key
=== C0
.DC1
) { // XON
1301 this.writeStopped
= false;
1304 if (result
.scrollDisp
) {
1305 this.scrollDisp(result
.scrollDisp
);
1306 return this.cancel(ev
, true);
1309 if (isThirdLevelShift(this, ev
)) {
1313 if (result
.cancel
) {
1314 // The event is canceled at the end already, is this necessary?
1315 this.cancel(ev
, true);
1322 this.emit('keydown', ev
);
1323 this.emit('key', result
.key
, ev
);
1325 this.handler(result
.key
);
1327 return this.cancel(ev
, true);
1331 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
1332 * returned value is the new key code to pass to the PTY.
1334 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
1335 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
1337 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
1339 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
1340 // canceled at the end of keyDown
1342 // The new key even to emit
1344 // The number of characters to scroll, if this is defined it will cancel the event
1345 scrollDisp
: undefined
1347 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
1348 switch (ev
.keyCode
) {
1352 result
.key
= C0
.BS
; // ^H
1355 result
.key
= C0
.DEL
; // ^?
1360 result
.key
= C0
.ESC
+ '[Z';
1364 result
.cancel
= true;
1369 result
.cancel
= true;
1373 result
.key
= C0
.ESC
;
1374 result
.cancel
= true;
1379 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'D';
1380 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
1381 // http://unix.stackexchange.com/a/108106
1382 // macOS uses different escape sequences than linux
1383 if (result
.key
== C0
.ESC
+ '[1;3D') {
1384 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'b' : C0
.ESC
+ '[1;5D';
1386 } else if (this.applicationCursor
) {
1387 result
.key
= C0
.ESC
+ 'OD';
1389 result
.key
= C0
.ESC
+ '[D';
1395 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'C';
1396 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
1397 // http://unix.stackexchange.com/a/108106
1398 // macOS uses different escape sequences than linux
1399 if (result
.key
== C0
.ESC
+ '[1;3C') {
1400 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'f' : C0
.ESC
+ '[1;5C';
1402 } else if (this.applicationCursor
) {
1403 result
.key
= C0
.ESC
+ 'OC';
1405 result
.key
= C0
.ESC
+ '[C';
1411 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'A';
1412 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
1413 // http://unix.stackexchange.com/a/108106
1414 if (result
.key
== C0
.ESC
+ '[1;3A') {
1415 result
.key
= C0
.ESC
+ '[1;5A';
1417 } else if (this.applicationCursor
) {
1418 result
.key
= C0
.ESC
+ 'OA';
1420 result
.key
= C0
.ESC
+ '[A';
1426 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'B';
1427 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
1428 // http://unix.stackexchange.com/a/108106
1429 if (result
.key
== C0
.ESC
+ '[1;3B') {
1430 result
.key
= C0
.ESC
+ '[1;5B';
1432 } else if (this.applicationCursor
) {
1433 result
.key
= C0
.ESC
+ 'OB';
1435 result
.key
= C0
.ESC
+ '[B';
1440 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
1441 // <Ctrl> or <Shift> + <Insert> are used to
1442 // copy-paste on some systems.
1443 result
.key
= C0
.ESC
+ '[2~';
1449 result
.key
= C0
.ESC
+ '[3;' + (modifiers
+ 1) + '~';
1451 result
.key
= C0
.ESC
+ '[3~';
1457 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'H';
1458 else if (this.applicationCursor
)
1459 result
.key
= C0
.ESC
+ 'OH';
1461 result
.key
= C0
.ESC
+ '[H';
1466 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'F';
1467 else if (this.applicationCursor
)
1468 result
.key
= C0
.ESC
+ 'OF';
1470 result
.key
= C0
.ESC
+ '[F';
1475 result
.scrollDisp
= -(this.rows
- 1);
1477 result
.key
= C0
.ESC
+ '[5~';
1483 result
.scrollDisp
= this.rows
- 1;
1485 result
.key
= C0
.ESC
+ '[6~';
1491 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'P';
1493 result
.key
= C0
.ESC
+ 'OP';
1498 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'Q';
1500 result
.key
= C0
.ESC
+ 'OQ';
1505 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'R';
1507 result
.key
= C0
.ESC
+ 'OR';
1512 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'S';
1514 result
.key
= C0
.ESC
+ 'OS';
1519 result
.key
= C0
.ESC
+ '[15;' + (modifiers
+ 1) + '~';
1521 result
.key
= C0
.ESC
+ '[15~';
1526 result
.key
= C0
.ESC
+ '[17;' + (modifiers
+ 1) + '~';
1528 result
.key
= C0
.ESC
+ '[17~';
1533 result
.key
= C0
.ESC
+ '[18;' + (modifiers
+ 1) + '~';
1535 result
.key
= C0
.ESC
+ '[18~';
1540 result
.key
= C0
.ESC
+ '[19;' + (modifiers
+ 1) + '~';
1542 result
.key
= C0
.ESC
+ '[19~';
1547 result
.key
= C0
.ESC
+ '[20;' + (modifiers
+ 1) + '~';
1549 result
.key
= C0
.ESC
+ '[20~';
1554 result
.key
= C0
.ESC
+ '[21;' + (modifiers
+ 1) + '~';
1556 result
.key
= C0
.ESC
+ '[21~';
1561 result
.key
= C0
.ESC
+ '[23;' + (modifiers
+ 1) + '~';
1563 result
.key
= C0
.ESC
+ '[23~';
1568 result
.key
= C0
.ESC
+ '[24;' + (modifiers
+ 1) + '~';
1570 result
.key
= C0
.ESC
+ '[24~';
1575 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
1576 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
1577 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
1578 } else if (ev
.keyCode
=== 32) {
1580 result
.key
= String
.fromCharCode(0);
1581 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
1582 // escape, file sep, group sep, record sep, unit sep
1583 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
1584 } else if (ev
.keyCode
=== 56) {
1586 result
.key
= String
.fromCharCode(127);
1587 } else if (ev
.keyCode
=== 219) {
1588 // ^[ - Control Sequence Introducer (CSI)
1589 result
.key
= String
.fromCharCode(27);
1590 } else if (ev
.keyCode
=== 220) {
1591 // ^\ - String Terminator (ST)
1592 result
.key
= String
.fromCharCode(28);
1593 } else if (ev
.keyCode
=== 221) {
1594 // ^] - Operating System Command (OSC)
1595 result
.key
= String
.fromCharCode(29);
1597 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
1598 // On Mac this is a third level shift. Use <Esc> instead.
1599 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
1600 result
.key
= C0
.ESC
+ String
.fromCharCode(ev
.keyCode
+ 32);
1601 } else if (ev
.keyCode
=== 192) {
1602 result
.key
= C0
.ESC
+ '`';
1603 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
1604 result
.key
= C0
.ESC
+ (ev
.keyCode
- 48);
1614 * Set the G level of the terminal
1617 Terminal
.prototype.setgLevel = function(g
) {
1619 this.charset
= this.charsets
[g
];
1623 * Set the charset for the given G level of the terminal
1627 Terminal
.prototype.setgCharset = function(g
, charset
) {
1628 this.charsets
[g
] = charset
;
1629 if (this.glevel
=== g
) {
1630 this.charset
= charset
;
1635 * Handle a keypress event.
1637 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
1638 * @param {KeyboardEvent} ev The keypress event to be handled.
1640 Terminal
.prototype.keyPress = function(ev
) {
1647 } else if (ev
.which
== null) {
1649 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
1656 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
1661 key
= String
.fromCharCode(key
);
1663 this.emit('keypress', key
, ev
);
1664 this.emit('key', key
, ev
);
1672 * Send data for handling to the terminal
1673 * @param {string} data
1675 Terminal
.prototype.send = function(data
) {
1679 setTimeout(function() {
1680 self
.handler(self
.queue
);
1690 * Note: We could do sweet things with webaudio here
1692 Terminal
.prototype.bell = function() {
1693 if (!this.visualBell
) return;
1695 this.element
.style
.borderColor
= 'white';
1696 setTimeout(function() {
1697 self
.element
.style
.borderColor
= '';
1699 if (this.popOnBell
) this.focus();
1703 * Log the current state to the console.
1705 Terminal
.prototype.log = function() {
1706 if (!this.debug
) return;
1707 if (!this.context
.console
|| !this.context
.console
.log
) return;
1708 var args
= Array
.prototype.slice
.call(arguments
);
1709 this.context
.console
.log
.apply(this.context
.console
, args
);
1713 * Log the current state as error to the console.
1715 Terminal
.prototype.error = function() {
1716 if (!this.debug
) return;
1717 if (!this.context
.console
|| !this.context
.console
.error
) return;
1718 var args
= Array
.prototype.slice
.call(arguments
);
1719 this.context
.console
.error
.apply(this.context
.console
, args
);
1723 * Resizes the terminal.
1725 * @param {number} x The number of columns to resize to.
1726 * @param {number} y The number of rows to resize to.
1728 Terminal
.prototype.resize = function(x
, y
) {
1729 if (Number
.isNaN(x
) || Number
.isNaN(y
)) {
1740 if (x
=== this.cols
&& y
=== this.rows
) {
1750 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
1751 i
= this.lines
.length
;
1753 while (this.lines
.get(i
).length
< x
) {
1754 this.lines
.get(i
).push(ch
);
1758 i
= this.lines
.length
;
1760 while (this.lines
.get(i
).length
> x
) {
1761 this.lines
.get(i
).pop();
1766 this.setupStops(this.cols
);
1774 // y is rows, not this.y
1775 if (this.lines
.length
< y
+ this.ybase
) {
1776 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
1777 // There is room above the buffer and there are no empty elements below the line,
1781 if (this.ydisp
> 0) {
1782 // Viewport is at the top of the buffer, must increase downwards
1786 // Add a blank line if there is no buffer left at the top to scroll to, or if there
1787 // are blank lines after the cursor
1788 this.lines
.push(this.blankLine());
1791 if (this.children
.length
< y
) {
1797 if (this.lines
.length
> y
+ this.ybase
) {
1798 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
1799 // The line is a blank line below the cursor, remove it
1802 // The line is the cursor, scroll down
1807 if (this.children
.length
> y
) {
1808 el
= this.children
.shift();
1810 el
.parentNode
.removeChild(el
);
1816 // Make sure that the cursor stays on screen
1829 this.scrollBottom
= y
- 1;
1831 this.charMeasure
.measure();
1833 this.refresh(0, this.rows
- 1);
1837 this.geometry
= [this.cols
, this.rows
];
1838 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
1842 * Updates the range of rows to refresh
1843 * @param {number} y The number of rows to refresh next.
1845 Terminal
.prototype.updateRange = function(y
) {
1846 if (y
< this.refreshStart
) this.refreshStart
= y
;
1847 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
1848 // if (y > this.refreshEnd) {
1849 // this.refreshEnd = y;
1850 // if (y > this.rows - 1) {
1851 // this.refreshEnd = this.rows - 1;
1857 * Set the range of refreshing to the maximum value
1859 Terminal
.prototype.maxRange = function() {
1860 this.refreshStart
= 0;
1861 this.refreshEnd
= this.rows
- 1;
1867 * Setup the tab stops.
1870 Terminal
.prototype.setupStops = function(i
) {
1872 if (!this.tabs
[i
]) {
1873 i
= this.prevStop(i
);
1880 for (; i
< this.cols
; i
+= this.getOption('tabStopWidth')) {
1881 this.tabs
[i
] = true;
1887 * Move the cursor to the previous tab stop from the given position (default is current).
1888 * @param {number} x The position to move the cursor to the previous tab stop.
1890 Terminal
.prototype.prevStop = function(x
) {
1891 if (x
== null) x
= this.x
;
1892 while (!this.tabs
[--x
] && x
> 0);
1893 return x
>= this.cols
1900 * Move the cursor one tab stop forward from the given position (default is current).
1901 * @param {number} x The position to move the cursor one tab stop forward.
1903 Terminal
.prototype.nextStop = function(x
) {
1904 if (x
== null) x
= this.x
;
1905 while (!this.tabs
[++x
] && x
< this.cols
);
1906 return x
>= this.cols
1913 * Erase in the identified line everything from "x" to the end of the line (right).
1914 * @param {number} x The column from which to start erasing to the end of the line.
1915 * @param {number} y The line in which to operate.
1917 Terminal
.prototype.eraseRight = function(x
, y
) {
1918 var line
= this.lines
.get(this.ybase
+ y
);
1922 var ch
= [this.eraseAttr(), ' ', 1]; // xterm
1923 for (; x
< this.cols
; x
++) {
1926 this.updateRange(y
);
1932 * Erase in the identified line everything from "x" to the start of the line (left).
1933 * @param {number} x The column from which to start erasing to the start of the line.
1934 * @param {number} y The line in which to operate.
1936 Terminal
.prototype.eraseLeft = function(x
, y
) {
1937 var line
= this.lines
.get(this.ybase
+ y
);
1941 var ch
= [this.eraseAttr(), ' ', 1]; // xterm
1946 this.updateRange(y
);
1950 * Clears the entire buffer, making the prompt line the new first line.
1952 Terminal
.prototype.clear = function() {
1953 if (this.ybase
=== 0 && this.y
=== 0) {
1954 // Don't clear if it's already clear
1957 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
1958 this.lines
.length
= 1;
1962 for (var i
= 1; i
< this.rows
; i
++) {
1963 this.lines
.push(this.blankLine());
1965 this.refresh(0, this.rows
- 1);
1966 this.emit('scroll', this.ydisp
);
1970 * Erase all content in the given line
1971 * @param {number} y The line to erase all of its contents.
1973 Terminal
.prototype.eraseLine = function(y
) {
1974 this.eraseRight(0, y
);
1979 * Return the data array of a blank line
1980 * @param {number} cur First bunch of data for each "blank" character.
1982 Terminal
.prototype.blankLine = function(cur
) {
1987 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
1991 for (; i
< this.cols
; i
++) {
2000 * If cur return the back color xterm feature attribute. Else return defAttr.
2001 * @param {object} cur
2003 Terminal
.prototype.ch = function(cur
) {
2005 ? [this.eraseAttr(), ' ', 1]
2006 : [this.defAttr
, ' ', 1];
2011 * Evaluate if the current erminal is the given argument.
2012 * @param {object} term The terminal to evaluate
2014 Terminal
.prototype.is = function(term
) {
2015 var name
= this.termName
;
2016 return (name
+ '').indexOf(term
) === 0;
2021 * Emit the 'data' event and populate the given data.
2022 * @param {string} data The data to populate in the event.
2024 Terminal
.prototype.handler = function(data
) {
2025 // Prevents all events to pty process if stdin is disabled
2026 if (this.options
.disableStdin
) {
2030 // Input is being sent to the terminal, the terminal should focus the prompt.
2031 if (this.ybase
!== this.ydisp
) {
2032 this.scrollToBottom();
2034 this.emit('data', data
);
2039 * Emit the 'title' event and populate the given title.
2040 * @param {string} title The title to populate in the event.
2042 Terminal
.prototype.handleTitle = function(title
) {
2044 * This event is emitted when the title of the terminal is changed
2045 * from inside the terminal. The parameter is the new title.
2049 this.emit('title', title
);
2058 * ESC D Index (IND is 0x84).
2060 Terminal
.prototype.index = function() {
2062 if (this.y
> this.scrollBottom
) {
2066 // If the end of the line is hit, prevent this action from wrapping around to the next line.
2067 if (this.x
>= this.cols
) {
2074 * ESC M Reverse Index (RI is 0x8d).
2076 * Move the cursor up one row, inserting a new blank line if necessary.
2078 Terminal
.prototype.reverseIndex = function() {
2080 if (this.y
=== this.scrollTop
) {
2081 // possibly move the code below to term.reverseScroll();
2082 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2083 // blankLine(true) is xterm/linux behavior
2084 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
2085 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
2086 this.updateRange(this.scrollTop
);
2087 this.updateRange(this.scrollBottom
);
2095 * ESC c Full Reset (RIS).
2097 Terminal
.prototype.reset = function() {
2098 this.options
.rows
= this.rows
;
2099 this.options
.cols
= this.cols
;
2100 var customKeydownHandler
= this.customKeydownHandler
;
2101 Terminal
.call(this, this.options
);
2102 this.customKeydownHandler
= customKeydownHandler
;
2103 this.refresh(0, this.rows
- 1);
2104 this.viewport
.syncScrollArea();
2109 * ESC H Tab Set (HTS is 0x88).
2111 Terminal
.prototype.tabSet = function() {
2112 this.tabs
[this.x
] = true;
2119 function on(el
, type
, handler
, capture
) {
2120 if (!Array
.isArray(el
)) {
2123 el
.forEach(function (element
) {
2124 element
.addEventListener(type
, handler
, capture
|| false);
2128 function off(el
, type
, handler
, capture
) {
2129 el
.removeEventListener(type
, handler
, capture
|| false);
2132 function cancel(ev
, force
) {
2133 if (!this.cancelEvents
&& !force
) {
2136 ev
.preventDefault();
2137 ev
.stopPropagation();
2141 function inherits(child
, parent
) {
2143 this.constructor = child
;
2145 f
.prototype = parent
.prototype;
2146 child
.prototype = new f
;
2149 function indexOf(obj
, el
) {
2152 if (obj
[i
] === el
) return i
;
2157 function isThirdLevelShift(term
, ev
) {
2159 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
2160 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
2162 if (ev
.type
== 'keypress') {
2163 return thirdLevelKey
;
2166 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
2167 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
2170 // Expose to InputHandler (temporary)
2171 Terminal
.prototype.matchColor
= matchColor
;
2173 function matchColor(r1
, g1
, b1
) {
2174 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
2176 if (matchColor
._cache
[hash
] != null) {
2177 return matchColor
._cache
[hash
];
2180 var ldiff
= Infinity
2189 for (; i
< Terminal
.vcolors
.length
; i
++) {
2190 c
= Terminal
.vcolors
[i
];
2195 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
2208 return matchColor
._cache
[hash
] = li
;
2211 matchColor
._cache
= {};
2213 // http://stackoverflow.com/questions/1633828
2214 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
2215 return Math
.pow(30 * (r1
- r2
), 2)
2216 + Math
.pow(59 * (g1
- g2
), 2)
2217 + Math
.pow(11 * (b1
- b2
), 2);
2220 function each(obj
, iter
, con
) {
2221 if (obj
.forEach
) return obj
.forEach(iter
, con
);
2222 for (var i
= 0; i
< obj
.length
; i
++) {
2223 iter
.call(con
, obj
[i
], i
, obj
);
2227 function wasMondifierKeyOnlyEvent(ev
) {
2228 return ev
.keyCode
=== 16 || // Shift
2229 ev
.keyCode
=== 17 || // Ctrl
2230 ev
.keyCode
=== 18; // Alt
2233 function keys(obj
) {
2234 if (Object
.keys
) return Object
.keys(obj
);
2237 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
2248 Terminal
.EventEmitter
= EventEmitter
;
2249 Terminal
.inherits
= inherits
;
2252 * Adds an event listener to the terminal.
2254 * @param {string} event The name of the event. TODO: Document all event types
2255 * @param {function} callback The function to call when the event is triggered.
2259 Terminal
.cancel
= cancel
;
2261 module
.exports
= Terminal
;