]>
Commit | Line | Data |
---|---|---|
8bc844c0 | 1 | /** |
5af18f8e | 2 | * xterm.js: xterm, in the browser |
8bc844c0 CJ |
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 | |
9 | * other features. | |
1d300911 | 10 | * @license MIT |
8bc844c0 CJ |
11 | */ |
12 | ||
081fe3f3 PK |
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'; | |
3de3c0c6 | 18 | import { C0 } from './EscapeSequences'; |
04b1ebf1 | 19 | import { InputHandler } from './InputHandler'; |
1848fff4 | 20 | import { Parser } from './Parser'; |
92068f36 | 21 | import { Renderer } from './Renderer'; |
2207d356 | 22 | import { Linkifier } from './Linkifier'; |
081fe3f3 | 23 | import { CharMeasure } from './utils/CharMeasure'; |
bc70b3b3 | 24 | import * as Browser from './utils/Browser'; |
9937d544 | 25 | import * as Keyboard from './utils/Keyboard'; |
1848fff4 | 26 | import { CHARSETS } from './Charsets'; |
ed1a31d1 | 27 | |
db76868c PK |
28 | /** |
29 | * Terminal Emulation References: | |
30 | * http://vt100.net/ | |
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 | |
37 | */ | |
8bc844c0 | 38 | |
db76868c PK |
39 | // Let it work inside Node.js for automated testing purposes. |
40 | var document = (typeof window != 'undefined') ? window.document : null; | |
8bc844c0 | 41 | |
e66b1c57 DI |
42 | /** |
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 | |
45 | * to be responsive. | |
46 | */ | |
2b8820fd DI |
47 | var WRITE_BUFFER_PAUSE_THRESHOLD = 5; |
48 | ||
49 | /** | |
50 | * The number of writes to perform in a single batch before allowing the | |
51 | * renderer to catch up with a 0ms setTimeout. | |
52 | */ | |
53 | var WRITE_BATCH_SIZE = 300; | |
e66b1c57 | 54 | |
aca81c76 DI |
55 | /** |
56 | * The time between cursor blinks. This is driven by JS rather than a CSS | |
57 | * animation due to a bug in Chromium that causes it to use excessive CPU time. | |
58 | * See https://github.com/Microsoft/vscode/issues/22900 | |
59 | */ | |
60 | var CURSOR_BLINK_INTERVAL = 600; | |
61 | ||
db76868c PK |
62 | /** |
63 | * Terminal | |
64 | */ | |
8bc844c0 | 65 | |
db76868c PK |
66 | /** |
67 | * Creates a new `Terminal` object. | |
68 | * | |
69 | * @param {object} options An object containing a set of options, the available options are: | |
a9417c68 PK |
70 | * - `cursorBlink` (boolean): Whether the terminal cursor blinks |
71 | * - `cols` (number): The number of columns of the terminal (horizontal size) | |
72 | * - `rows` (number): The number of rows of the terminal (vertical size) | |
db76868c PK |
73 | * |
74 | * @public | |
75 | * @class Xterm Xterm | |
76 | * @alias module:xterm/src/xterm | |
77 | */ | |
78 | function Terminal(options) { | |
79 | var self = this; | |
8bc844c0 | 80 | |
db76868c PK |
81 | if (!(this instanceof Terminal)) { |
82 | return new Terminal(arguments[0], arguments[1], arguments[2]); | |
83 | } | |
8bc844c0 | 84 | |
bc70b3b3 | 85 | self.browser = Browser; |
db76868c | 86 | self.cancel = Terminal.cancel; |
8bc844c0 | 87 | |
db76868c | 88 | EventEmitter.call(this); |
5fd1948b | 89 | |
db76868c PK |
90 | if (typeof options === 'number') { |
91 | options = { | |
92 | cols: arguments[0], | |
93 | rows: arguments[1], | |
94 | handler: arguments[2] | |
95 | }; | |
96 | } | |
8bc844c0 | 97 | |
db76868c | 98 | options = options || {}; |
8bc844c0 | 99 | |
86dad1b0 | 100 | |
db76868c PK |
101 | Object.keys(Terminal.defaults).forEach(function(key) { |
102 | if (options[key] == null) { | |
103 | options[key] = Terminal.options[key]; | |
91273161 | 104 | |
db76868c PK |
105 | if (Terminal[key] !== Terminal.defaults[key]) { |
106 | options[key] = Terminal[key]; | |
3f455f90 | 107 | } |
db76868c PK |
108 | } |
109 | self[key] = options[key]; | |
110 | }); | |
111 | ||
112 | if (options.colors.length === 8) { | |
113 | options.colors = options.colors.concat(Terminal._colors.slice(8)); | |
114 | } else if (options.colors.length === 16) { | |
115 | options.colors = options.colors.concat(Terminal._colors.slice(16)); | |
116 | } else if (options.colors.length === 10) { | |
117 | options.colors = options.colors.slice(0, -2).concat( | |
118 | Terminal._colors.slice(8, -2), options.colors.slice(-2)); | |
119 | } else if (options.colors.length === 18) { | |
120 | options.colors = options.colors.concat( | |
121 | Terminal._colors.slice(16, -2), options.colors.slice(-2)); | |
122 | } | |
123 | this.colors = options.colors; | |
124 | ||
125 | this.options = options; | |
126 | ||
127 | // this.context = options.context || window; | |
128 | // this.document = options.document || document; | |
129 | this.parent = options.body || options.parent || ( | |
130 | document ? document.getElementsByTagName('body')[0] : null | |
131 | ); | |
132 | ||
133 | this.cols = options.cols || options.geometry[0]; | |
134 | this.rows = options.rows || options.geometry[1]; | |
a9417c68 | 135 | this.geometry = [this.cols, this.rows]; |
db76868c PK |
136 | |
137 | if (options.handler) { | |
138 | this.on('data', options.handler); | |
139 | } | |
140 | ||
141 | /** | |
142 | * The scroll position of the y cursor, ie. ybase + y = the y position within the entire | |
143 | * buffer | |
144 | */ | |
145 | this.ybase = 0; | |
146 | ||
147 | /** | |
148 | * The scroll position of the viewport | |
149 | */ | |
150 | this.ydisp = 0; | |
151 | ||
152 | /** | |
153 | * The cursor's x position after ybase | |
154 | */ | |
155 | this.x = 0; | |
156 | ||
157 | /** | |
158 | * The cursor's y position after ybase | |
159 | */ | |
160 | this.y = 0; | |
161 | ||
db76868c PK |
162 | this.cursorState = 0; |
163 | this.cursorHidden = false; | |
164 | this.convertEol; | |
db76868c PK |
165 | this.queue = ''; |
166 | this.scrollTop = 0; | |
167 | this.scrollBottom = this.rows - 1; | |
168 | this.customKeydownHandler = null; | |
aca81c76 | 169 | this.cursorBlinkInterval = null; |
db76868c PK |
170 | |
171 | // modes | |
172 | this.applicationKeypad = false; | |
173 | this.applicationCursor = false; | |
174 | this.originMode = false; | |
175 | this.insertMode = false; | |
176 | this.wraparoundMode = true; // defaults: xterm - true, vt100 - false | |
177 | this.normal = null; | |
178 | ||
179 | // charset | |
180 | this.charset = null; | |
181 | this.gcharset = null; | |
182 | this.glevel = 0; | |
183 | this.charsets = [null]; | |
184 | ||
185 | // mouse properties | |
186 | this.decLocator; | |
187 | this.x10Mouse; | |
188 | this.vt200Mouse; | |
189 | this.vt300Mouse; | |
190 | this.normalMouse; | |
191 | this.mouseEvents; | |
192 | this.sendFocus; | |
193 | this.utfMouse; | |
194 | this.sgrMouse; | |
195 | this.urxvtMouse; | |
196 | ||
197 | // misc | |
198 | this.element; | |
199 | this.children; | |
200 | this.refreshStart; | |
201 | this.refreshEnd; | |
202 | this.savedX; | |
203 | this.savedY; | |
204 | this.savedCols; | |
205 | ||
206 | // stream | |
207 | this.readable = true; | |
208 | this.writable = true; | |
209 | ||
210 | this.defAttr = (0 << 18) | (257 << 9) | (256 << 0); | |
211 | this.curAttr = this.defAttr; | |
212 | ||
213 | this.params = []; | |
214 | this.currentParam = 0; | |
215 | this.prefix = ''; | |
216 | this.postfix = ''; | |
217 | ||
04b1ebf1 | 218 | this.inputHandler = new InputHandler(this); |
a31921ae | 219 | this.parser = new Parser(this.inputHandler, this); |
cc78fdd6 DI |
220 | // Reuse renderer if the Terminal is being recreated via a Terminal.reset call. |
221 | this.renderer = this.renderer || null; | |
2207d356 | 222 | this.linkifier = this.linkifier || null;; |
04b1ebf1 | 223 | |
b9d374af DI |
224 | // user input states |
225 | this.writeBuffer = []; | |
226 | this.writeInProgress = false; | |
e66b1c57 DI |
227 | |
228 | /** | |
229 | * Whether _xterm.js_ sent XOFF in order to catch up with the pty process. | |
230 | * This is a distinct state from writeStopped so that if the user requested | |
231 | * XOFF via ^S that it will not automatically resume when the writeBuffer goes | |
232 | * below threshold. | |
233 | */ | |
234 | this.xoffSentToCatchUp = false; | |
235 | ||
236 | /** Whether writing has been stopped as a result of XOFF */ | |
237 | this.writeStopped = false; | |
b9d374af | 238 | |
db76868c PK |
239 | // leftover surrogate high from previous write invocation |
240 | this.surrogate_high = ''; | |
241 | ||
242 | /** | |
243 | * An array of all lines in the entire buffer, including the prompt. The lines are array of | |
244 | * characters which are 2-length arrays where [0] is an attribute and [1] is the character. | |
245 | */ | |
607c8191 | 246 | this.lines = new CircularList(this.scrollback); |
db76868c PK |
247 | var i = this.rows; |
248 | while (i--) { | |
249 | this.lines.push(this.blankLine()); | |
250 | } | |
251 | ||
252 | this.tabs; | |
253 | this.setupStops(); | |
5e68acfc MK |
254 | |
255 | // Store if user went browsing history in scrollback | |
256 | this.userScrolling = false; | |
db76868c PK |
257 | } |
258 | ||
259 | inherits(Terminal, EventEmitter); | |
260 | ||
261 | /** | |
262 | * back_color_erase feature for xterm. | |
263 | */ | |
264 | Terminal.prototype.eraseAttr = function() { | |
265 | // if (this.is('screen')) return this.defAttr; | |
266 | return (this.defAttr & ~0x1ff) | (this.curAttr & 0x1ff); | |
267 | }; | |
8bc844c0 | 268 | |
db76868c PK |
269 | /** |
270 | * Colors | |
271 | */ | |
91273161 | 272 | |
db76868c PK |
273 | // Colors 0-15 |
274 | Terminal.tangoColors = [ | |
275 | // dark: | |
276 | '#2e3436', | |
277 | '#cc0000', | |
278 | '#4e9a06', | |
279 | '#c4a000', | |
280 | '#3465a4', | |
281 | '#75507b', | |
282 | '#06989a', | |
283 | '#d3d7cf', | |
284 | // bright: | |
285 | '#555753', | |
286 | '#ef2929', | |
287 | '#8ae234', | |
288 | '#fce94f', | |
289 | '#729fcf', | |
290 | '#ad7fa8', | |
291 | '#34e2e2', | |
292 | '#eeeeec' | |
293 | ]; | |
294 | ||
295 | // Colors 0-15 + 16-255 | |
296 | // Much thanks to TooTallNate for writing this. | |
297 | Terminal.colors = (function() { | |
298 | var colors = Terminal.tangoColors.slice() | |
299 | , r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff] | |
300 | , i; | |
301 | ||
302 | // 16-231 | |
303 | i = 0; | |
304 | for (; i < 216; i++) { | |
305 | out(r[(i / 36) % 6 | 0], r[(i / 6) % 6 | 0], r[i % 6]); | |
306 | } | |
307 | ||
308 | // 232-255 (grey) | |
309 | i = 0; | |
310 | for (; i < 24; i++) { | |
311 | r = 8 + i * 10; | |
312 | out(r, r, r); | |
313 | } | |
314 | ||
315 | function out(r, g, b) { | |
316 | colors.push('#' + hex(r) + hex(g) + hex(b)); | |
317 | } | |
318 | ||
319 | function hex(c) { | |
320 | c = c.toString(16); | |
321 | return c.length < 2 ? '0' + c : c; | |
322 | } | |
323 | ||
324 | return colors; | |
325 | })(); | |
326 | ||
327 | Terminal._colors = Terminal.colors.slice(); | |
328 | ||
329 | Terminal.vcolors = (function() { | |
330 | var out = [] | |
331 | , colors = Terminal.colors | |
332 | , i = 0 | |
333 | , color; | |
334 | ||
335 | for (; i < 256; i++) { | |
336 | color = parseInt(colors[i].substring(1), 16); | |
337 | out.push([ | |
338 | (color >> 16) & 0xff, | |
339 | (color >> 8) & 0xff, | |
340 | color & 0xff | |
341 | ]); | |
342 | } | |
343 | ||
344 | return out; | |
345 | })(); | |
5fd1948b | 346 | |
db76868c PK |
347 | /** |
348 | * Options | |
349 | */ | |
3f455f90 | 350 | |
db76868c PK |
351 | Terminal.defaults = { |
352 | colors: Terminal.colors, | |
353 | theme: 'default', | |
354 | convertEol: false, | |
355 | termName: 'xterm', | |
356 | geometry: [80, 24], | |
357 | cursorBlink: false, | |
0bd469f5 | 358 | cursorStyle: 'block', |
db76868c PK |
359 | visualBell: false, |
360 | popOnBell: false, | |
361 | scrollback: 1000, | |
362 | screenKeys: false, | |
363 | debug: false, | |
d9d60063 | 364 | cancelEvents: false, |
f4293a6d | 365 | disableStdin: false, |
c2a69721 | 366 | useFlowControl: false, |
f4293a6d | 367 | tabStopWidth: 8 |
db76868c PK |
368 | // programFeatures: false, |
369 | // focusKeys: false, | |
370 | }; | |
371 | ||
372 | Terminal.options = {}; | |
373 | ||
374 | Terminal.focus = null; | |
375 | ||
376 | each(keys(Terminal.defaults), function(key) { | |
377 | Terminal[key] = Terminal.defaults[key]; | |
378 | Terminal.options[key] = Terminal.defaults[key]; | |
379 | }); | |
3f455f90 | 380 | |
db76868c PK |
381 | /** |
382 | * Focus the terminal. Delegates focus handling to the terminal's DOM element. | |
383 | */ | |
384 | Terminal.prototype.focus = function() { | |
385 | return this.textarea.focus(); | |
386 | }; | |
3f455f90 | 387 | |
4b459fe0 PK |
388 | /** |
389 | * Retrieves an option's value from the terminal. | |
390 | * @param {string} key The option key. | |
391 | */ | |
392 | Terminal.prototype.getOption = function(key, value) { | |
393 | if (!(key in Terminal.defaults)) { | |
394 | throw new Error('No option with key "' + key + '"'); | |
395 | } | |
396 | ||
53e8ac9b | 397 | if (typeof this.options[key] !== 'undefined') { |
4b459fe0 PK |
398 | return this.options[key]; |
399 | } | |
400 | ||
401 | return this[key]; | |
402 | }; | |
403 | ||
ab5cc0ad DI |
404 | /** |
405 | * Sets an option on the terminal. | |
15e56bd8 DI |
406 | * @param {string} key The option key. |
407 | * @param {string} value The option value. | |
ab5cc0ad DI |
408 | */ |
409 | Terminal.prototype.setOption = function(key, value) { | |
410 | if (!(key in Terminal.defaults)) { | |
411 | throw new Error('No option with key "' + key + '"'); | |
412 | } | |
5a932b2a DI |
413 | switch (key) { |
414 | case 'scrollback': | |
415 | if (this.options[key] !== value) { | |
416 | if (this.lines.length > value) { | |
417 | const amountToTrim = this.lines.length - value; | |
418 | const needsRefresh = (this.ydisp - amountToTrim < 0); | |
419 | this.lines.trimStart(amountToTrim); | |
420 | this.ybase = Math.max(this.ybase - amountToTrim, 0); | |
421 | this.ydisp = Math.max(this.ydisp - amountToTrim, 0); | |
422 | if (needsRefresh) { | |
423 | this.refresh(0, this.rows - 1); | |
424 | } | |
425 | } | |
426 | this.lines.maxLength = value; | |
427 | this.viewport.syncScrollArea(); | |
428 | } | |
429 | break; | |
430 | } | |
ab5cc0ad DI |
431 | this[key] = value; |
432 | this.options[key] = value; | |
7679475b | 433 | switch (key) { |
aca81c76 | 434 | case 'cursorBlink': this.setCursorBlinking(value); break; |
0bd469f5 DI |
435 | case 'cursorStyle': |
436 | // Style 'block' applies with no class | |
437 | this.element.classList.toggle(`xterm-cursor-style-underline`, value === 'underline'); | |
438 | this.element.classList.toggle(`xterm-cursor-style-bar`, value === 'bar'); | |
439 | break; | |
8cb46f27 | 440 | case 'tabStopWidth': this.setupStops(); break; |
7679475b | 441 | } |
ab5cc0ad DI |
442 | }; |
443 | ||
aca81c76 DI |
444 | Terminal.prototype.restartCursorBlinking = function () { |
445 | this.setCursorBlinking(this.options.cursorBlink); | |
446 | } | |
447 | ||
448 | Terminal.prototype.setCursorBlinking = function (enabled) { | |
449 | this.element.classList.toggle('xterm-cursor-blink', enabled); | |
450 | this.element.classList.remove('xterm-cursor-blink-on'); | |
451 | if (this.cursorBlinkInterval) { | |
452 | clearInterval(this.cursorBlinkInterval); | |
453 | this.cursorBlinkInterval = null; | |
454 | } | |
455 | if (enabled) { | |
456 | var self = this; | |
457 | this.cursorBlinkInterval = setInterval(function () { | |
458 | self.element.classList.toggle('xterm-cursor-blink-on'); | |
459 | }, CURSOR_BLINK_INTERVAL); | |
460 | } | |
461 | } | |
462 | ||
db76868c PK |
463 | /** |
464 | * Binds the desired focus behavior on a given terminal object. | |
465 | * | |
466 | * @static | |
467 | */ | |
468 | Terminal.bindFocus = function (term) { | |
469 | on(term.textarea, 'focus', function (ev) { | |
470 | if (term.sendFocus) { | |
3de3c0c6 | 471 | term.send(C0.ESC + '[I'); |
86dad1b0 | 472 | } |
db76868c PK |
473 | term.element.classList.add('focus'); |
474 | term.showCursor(); | |
aca81c76 | 475 | term.restartCursorBlinking.apply(term); |
db76868c PK |
476 | Terminal.focus = term; |
477 | term.emit('focus', {terminal: term}); | |
478 | }); | |
479 | }; | |
8bc844c0 | 480 | |
db76868c PK |
481 | /** |
482 | * Blur the terminal. Delegates blur handling to the terminal's DOM element. | |
483 | */ | |
484 | Terminal.prototype.blur = function() { | |
485 | return this.textarea.blur(); | |
486 | }; | |
8bc844c0 | 487 | |
db76868c PK |
488 | /** |
489 | * Binds the desired blur behavior on a given terminal object. | |
490 | * | |
491 | * @static | |
492 | */ | |
493 | Terminal.bindBlur = function (term) { | |
494 | on(term.textarea, 'blur', function (ev) { | |
3c635f3c | 495 | term.refresh(term.y, term.y); |
db76868c | 496 | if (term.sendFocus) { |
3de3c0c6 | 497 | term.send(C0.ESC + '[O'); |
db76868c PK |
498 | } |
499 | term.element.classList.remove('focus'); | |
500 | Terminal.focus = null; | |
501 | term.emit('blur', {terminal: term}); | |
502 | }); | |
503 | }; | |
a68c8336 | 504 | |
db76868c PK |
505 | /** |
506 | * Initialize default behavior | |
507 | */ | |
508 | Terminal.prototype.initGlobal = function() { | |
42a1e4ef PK |
509 | var term = this; |
510 | ||
db76868c | 511 | Terminal.bindKeys(this); |
db76868c PK |
512 | Terminal.bindFocus(this); |
513 | Terminal.bindBlur(this); | |
3f455f90 | 514 | |
42a1e4ef | 515 | // Bind clipboard functionality |
5808de64 | 516 | on(this.element, 'copy', function (ev) { |
517 | copyHandler.call(this, ev, term); | |
518 | }); | |
42a1e4ef PK |
519 | on(this.textarea, 'paste', function (ev) { |
520 | pasteHandler.call(this, ev, term); | |
521 | }); | |
ab87dece PK |
522 | on(this.element, 'paste', function (ev) { |
523 | pasteHandler.call(this, ev, term); | |
524 | }); | |
35637797 | 525 | |
35637797 | 526 | function rightClickHandlerWrapper (ev) { |
42a1e4ef | 527 | rightClickHandler.call(this, ev, term); |
35637797 PK |
528 | } |
529 | ||
547db926 | 530 | if (term.browser.isFirefox) { |
35637797 PK |
531 | on(this.element, 'mousedown', function (ev) { |
532 | if (ev.button == 2) { | |
533 | rightClickHandlerWrapper(ev); | |
534 | } | |
535 | }); | |
536 | } else { | |
537 | on(this.element, 'contextmenu', rightClickHandlerWrapper); | |
538 | } | |
db76868c | 539 | }; |
8bc844c0 | 540 | |
db76868c PK |
541 | /** |
542 | * Apply key handling to the terminal | |
543 | */ | |
544 | Terminal.bindKeys = function(term) { | |
545 | on(term.element, 'keydown', function(ev) { | |
546 | if (document.activeElement != this) { | |
547 | return; | |
548 | } | |
549 | term.keyDown(ev); | |
550 | }, true); | |
8bc844c0 | 551 | |
db76868c PK |
552 | on(term.element, 'keypress', function(ev) { |
553 | if (document.activeElement != this) { | |
554 | return; | |
555 | } | |
556 | term.keyPress(ev); | |
557 | }, true); | |
8bc844c0 | 558 | |
a05bb502 DI |
559 | on(term.element, 'keyup', function(ev) { |
560 | if (!wasMondifierKeyOnlyEvent(ev)) { | |
561 | term.focus(term); | |
562 | } | |
563 | }, true); | |
3b322929 | 564 | |
db76868c PK |
565 | on(term.textarea, 'keydown', function(ev) { |
566 | term.keyDown(ev); | |
567 | }, true); | |
31161ffe | 568 | |
db76868c PK |
569 | on(term.textarea, 'keypress', function(ev) { |
570 | term.keyPress(ev); | |
571 | // Truncate the textarea's value, since it is not needed | |
572 | this.value = ''; | |
573 | }, true); | |
8bc844c0 | 574 | |
db76868c PK |
575 | on(term.textarea, 'compositionstart', term.compositionHelper.compositionstart.bind(term.compositionHelper)); |
576 | on(term.textarea, 'compositionupdate', term.compositionHelper.compositionupdate.bind(term.compositionHelper)); | |
577 | on(term.textarea, 'compositionend', term.compositionHelper.compositionend.bind(term.compositionHelper)); | |
578 | term.on('refresh', term.compositionHelper.updateCompositionElements.bind(term.compositionHelper)); | |
a1d71c91 DI |
579 | term.on('refresh', function (data) { |
580 | term.queueLinkification(data.start, data.end) | |
581 | }); | |
db76868c | 582 | }; |
5fd1948b | 583 | |
3f455f90 | 584 | |
db76868c PK |
585 | /** |
586 | * Insert the given row to the terminal or produce a new one | |
587 | * if no row argument is passed. Return the inserted row. | |
588 | * @param {HTMLElement} row (optional) The row to append to the terminal. | |
589 | */ | |
590 | Terminal.prototype.insertRow = function (row) { | |
591 | if (typeof row != 'object') { | |
592 | row = document.createElement('div'); | |
593 | } | |
cc7f4d0d | 594 | |
db76868c PK |
595 | this.rowContainer.appendChild(row); |
596 | this.children.push(row); | |
cd956bca | 597 | |
db76868c PK |
598 | return row; |
599 | }; | |
7988f634 | 600 | |
db76868c PK |
601 | /** |
602 | * Opens the terminal within an element. | |
603 | * | |
604 | * @param {HTMLElement} parent The element to create the terminal within. | |
605 | */ | |
606 | Terminal.prototype.open = function(parent) { | |
607 | var self=this, i=0, div; | |
608 | ||
609 | this.parent = parent || this.parent; | |
610 | ||
611 | if (!this.parent) { | |
612 | throw new Error('Terminal requires a parent element.'); | |
613 | } | |
614 | ||
615 | // Grab global elements | |
616 | this.context = this.parent.ownerDocument.defaultView; | |
617 | this.document = this.parent.ownerDocument; | |
618 | this.body = this.document.getElementsByTagName('body')[0]; | |
619 | ||
db76868c PK |
620 | //Create main element container |
621 | this.element = this.document.createElement('div'); | |
622 | this.element.classList.add('terminal'); | |
623 | this.element.classList.add('xterm'); | |
624 | this.element.classList.add('xterm-theme-' + this.theme); | |
aca81c76 | 625 | this.setCursorBlinking(this.options.cursorBlink); |
db76868c PK |
626 | |
627 | this.element.style.height | |
628 | this.element.setAttribute('tabindex', 0); | |
629 | ||
630 | this.viewportElement = document.createElement('div'); | |
631 | this.viewportElement.classList.add('xterm-viewport'); | |
632 | this.element.appendChild(this.viewportElement); | |
633 | this.viewportScrollArea = document.createElement('div'); | |
634 | this.viewportScrollArea.classList.add('xterm-scroll-area'); | |
635 | this.viewportElement.appendChild(this.viewportScrollArea); | |
636 | ||
637 | // Create the container that will hold the lines of the terminal and then | |
638 | // produce the lines the lines. | |
639 | this.rowContainer = document.createElement('div'); | |
640 | this.rowContainer.classList.add('xterm-rows'); | |
641 | this.element.appendChild(this.rowContainer); | |
642 | this.children = []; | |
26ebc3d9 | 643 | this.linkifier = new Linkifier(document, this.children); |
db76868c PK |
644 | |
645 | // Create the container that will hold helpers like the textarea for | |
646 | // capturing DOM Events. Then produce the helpers. | |
647 | this.helperContainer = document.createElement('div'); | |
648 | this.helperContainer.classList.add('xterm-helpers'); | |
649 | // TODO: This should probably be inserted once it's filled to prevent an additional layout | |
650 | this.element.appendChild(this.helperContainer); | |
651 | this.textarea = document.createElement('textarea'); | |
652 | this.textarea.classList.add('xterm-helper-textarea'); | |
653 | this.textarea.setAttribute('autocorrect', 'off'); | |
654 | this.textarea.setAttribute('autocapitalize', 'off'); | |
655 | this.textarea.setAttribute('spellcheck', 'false'); | |
656 | this.textarea.tabIndex = 0; | |
657 | this.textarea.addEventListener('focus', function() { | |
658 | self.emit('focus', {terminal: self}); | |
659 | }); | |
660 | this.textarea.addEventListener('blur', function() { | |
661 | self.emit('blur', {terminal: self}); | |
662 | }); | |
663 | this.helperContainer.appendChild(this.textarea); | |
664 | ||
665 | this.compositionView = document.createElement('div'); | |
666 | this.compositionView.classList.add('composition-view'); | |
667 | this.compositionHelper = new CompositionHelper(this.textarea, this.compositionView, this); | |
668 | this.helperContainer.appendChild(this.compositionView); | |
669 | ||
74483fb2 DI |
670 | this.charSizeStyleElement = document.createElement('style'); |
671 | this.helperContainer.appendChild(this.charSizeStyleElement); | |
db76868c PK |
672 | |
673 | for (; i < this.rows; i++) { | |
674 | this.insertRow(); | |
675 | } | |
676 | this.parent.appendChild(this.element); | |
677 | ||
202b54af | 678 | this.charMeasure = new CharMeasure(document, this.helperContainer); |
74483fb2 DI |
679 | this.charMeasure.on('charsizechanged', function () { |
680 | self.updateCharSizeCSS(); | |
681 | }); | |
4f18d842 DI |
682 | this.charMeasure.measure(); |
683 | ||
74483fb2 | 684 | this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure); |
92068f36 | 685 | this.renderer = new Renderer(this); |
db76868c | 686 | |
97feb332 | 687 | // Setup loop that draws to screen |
3c635f3c | 688 | this.refresh(0, this.rows - 1); |
db76868c PK |
689 | |
690 | // Initialize global actions that | |
691 | // need to be taken on the document. | |
692 | this.initGlobal(); | |
693 | ||
694 | // Ensure there is a Terminal.focus. | |
695 | this.focus(); | |
696 | ||
4e1bbee6 | 697 | on(this.element, 'click', function() { |
db76868c PK |
698 | var selection = document.getSelection(), |
699 | collapsed = selection.isCollapsed, | |
700 | isRange = typeof collapsed == 'boolean' ? !collapsed : selection.type == 'Range'; | |
701 | if (!isRange) { | |
702 | self.focus(); | |
703 | } | |
704 | }); | |
3f455f90 | 705 | |
db76868c PK |
706 | // Listen for mouse events and translate |
707 | // them into terminal mouse protocols. | |
708 | this.bindMouse(); | |
8bc844c0 | 709 | |
5712365c Y |
710 | /** |
711 | * This event is emitted when terminal has completed opening. | |
712 | * | |
713 | * @event open | |
714 | */ | |
db76868c PK |
715 | this.emit('open'); |
716 | }; | |
8bc844c0 | 717 | |
8bc844c0 | 718 | |
db76868c PK |
719 | /** |
720 | * Attempts to load an add-on using CommonJS or RequireJS (whichever is available). | |
721 | * @param {string} addon The name of the addon to load | |
722 | * @static | |
723 | */ | |
724 | Terminal.loadAddon = function(addon, callback) { | |
725 | if (typeof exports === 'object' && typeof module === 'object') { | |
726 | // CommonJS | |
56ecc77d | 727 | return require('./addons/' + addon + '/' + addon); |
db76868c PK |
728 | } else if (typeof define == 'function') { |
729 | // RequireJS | |
56ecc77d | 730 | return require(['./addons/' + addon + '/' + addon], callback); |
db76868c PK |
731 | } else { |
732 | console.error('Cannot load a module without a CommonJS or RequireJS environment.'); | |
733 | return false; | |
734 | } | |
735 | }; | |
8bc844c0 | 736 | |
74483fb2 DI |
737 | /** |
738 | * Updates the helper CSS class with any changes necessary after the terminal's | |
739 | * character width has been changed. | |
740 | */ | |
741 | Terminal.prototype.updateCharSizeCSS = function() { | |
742 | this.charSizeStyleElement.textContent = '.xterm-wide-char{width:' + (this.charMeasure.width * 2) + 'px;}'; | |
743 | } | |
8bc844c0 | 744 | |
db76868c PK |
745 | /** |
746 | * XTerm mouse events | |
747 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking | |
748 | * To better understand these | |
749 | * the xterm code is very helpful: | |
750 | * Relevant files: | |
751 | * button.c, charproc.c, misc.c | |
752 | * Relevant functions in xterm/button.c: | |
753 | * BtnCode, EmitButtonCode, EditorButton, SendMousePosition | |
754 | */ | |
755 | Terminal.prototype.bindMouse = function() { | |
756 | var el = this.element, self = this, pressed = 32; | |
757 | ||
758 | // mouseup, mousedown, wheel | |
759 | // left click: ^[[M 3<^[[M#3< | |
760 | // wheel up: ^[[M`3> | |
761 | function sendButton(ev) { | |
762 | var button | |
763 | , pos; | |
764 | ||
765 | // get the xterm-style button | |
766 | button = getButton(ev); | |
767 | ||
768 | // get mouse coordinates | |
769 | pos = getCoords(ev); | |
770 | if (!pos) return; | |
771 | ||
772 | sendEvent(button, pos); | |
773 | ||
774 | switch (ev.overrideType || ev.type) { | |
775 | case 'mousedown': | |
776 | pressed = button; | |
777 | break; | |
778 | case 'mouseup': | |
779 | // keep it at the left | |
780 | // button, just in case. | |
781 | pressed = 32; | |
782 | break; | |
783 | case 'wheel': | |
784 | // nothing. don't | |
785 | // interfere with | |
786 | // `pressed`. | |
787 | break; | |
788 | } | |
789 | } | |
790 | ||
791 | // motion example of a left click: | |
792 | // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7< | |
793 | function sendMove(ev) { | |
794 | var button = pressed | |
795 | , pos; | |
796 | ||
797 | pos = getCoords(ev); | |
798 | if (!pos) return; | |
799 | ||
800 | // buttons marked as motions | |
801 | // are incremented by 32 | |
802 | button += 32; | |
803 | ||
804 | sendEvent(button, pos); | |
805 | } | |
806 | ||
807 | // encode button and | |
808 | // position to characters | |
809 | function encode(data, ch) { | |
810 | if (!self.utfMouse) { | |
811 | if (ch === 255) return data.push(0); | |
812 | if (ch > 127) ch = 127; | |
813 | data.push(ch); | |
814 | } else { | |
815 | if (ch === 2047) return data.push(0); | |
816 | if (ch < 127) { | |
817 | data.push(ch); | |
818 | } else { | |
819 | if (ch > 2047) ch = 2047; | |
820 | data.push(0xC0 | (ch >> 6)); | |
821 | data.push(0x80 | (ch & 0x3F)); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
826 | // send a mouse event: | |
827 | // regular/utf8: ^[[M Cb Cx Cy | |
828 | // urxvt: ^[[ Cb ; Cx ; Cy M | |
829 | // sgr: ^[[ Cb ; Cx ; Cy M/m | |
830 | // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r | |
831 | // locator: CSI P e ; P b ; P r ; P c ; P p & w | |
832 | function sendEvent(button, pos) { | |
833 | // self.emit('mouse', { | |
834 | // x: pos.x - 32, | |
835 | // y: pos.x - 32, | |
836 | // button: button | |
837 | // }); | |
838 | ||
839 | if (self.vt300Mouse) { | |
840 | // NOTE: Unstable. | |
841 | // http://www.vt100.net/docs/vt3xx-gp/chapter15.html | |
842 | button &= 3; | |
843 | pos.x -= 32; | |
844 | pos.y -= 32; | |
3de3c0c6 | 845 | var data = C0.ESC + '[24'; |
db76868c PK |
846 | if (button === 0) data += '1'; |
847 | else if (button === 1) data += '3'; | |
848 | else if (button === 2) data += '5'; | |
849 | else if (button === 3) return; | |
850 | else data += '0'; | |
851 | data += '~[' + pos.x + ',' + pos.y + ']\r'; | |
852 | self.send(data); | |
853 | return; | |
854 | } | |
00f4232e | 855 | |
db76868c PK |
856 | if (self.decLocator) { |
857 | // NOTE: Unstable. | |
858 | button &= 3; | |
859 | pos.x -= 32; | |
860 | pos.y -= 32; | |
861 | if (button === 0) button = 2; | |
862 | else if (button === 1) button = 4; | |
863 | else if (button === 2) button = 6; | |
864 | else if (button === 3) button = 3; | |
3de3c0c6 | 865 | self.send(C0.ESC + '[' |
db76868c PK |
866 | + button |
867 | + ';' | |
868 | + (button === 3 ? 4 : 0) | |
869 | + ';' | |
870 | + pos.y | |
871 | + ';' | |
872 | + pos.x | |
873 | + ';' | |
874 | + (pos.page || 0) | |
875 | + '&w'); | |
876 | return; | |
877 | } | |
00f4232e | 878 | |
db76868c PK |
879 | if (self.urxvtMouse) { |
880 | pos.x -= 32; | |
881 | pos.y -= 32; | |
882 | pos.x++; | |
883 | pos.y++; | |
3de3c0c6 | 884 | self.send(C0.ESC + '[' + button + ';' + pos.x + ';' + pos.y + 'M'); |
db76868c PK |
885 | return; |
886 | } | |
8bc844c0 | 887 | |
db76868c PK |
888 | if (self.sgrMouse) { |
889 | pos.x -= 32; | |
890 | pos.y -= 32; | |
3de3c0c6 | 891 | self.send(C0.ESC + '[<' |
a75bafc4 | 892 | + (((button & 3) === 3 ? button & ~3 : button) - 32) |
db76868c PK |
893 | + ';' |
894 | + pos.x | |
895 | + ';' | |
896 | + pos.y | |
897 | + ((button & 3) === 3 ? 'm' : 'M')); | |
898 | return; | |
899 | } | |
8bc844c0 | 900 | |
db76868c PK |
901 | var data = []; |
902 | ||
903 | encode(data, button); | |
904 | encode(data, pos.x); | |
905 | encode(data, pos.y); | |
906 | ||
3de3c0c6 | 907 | self.send(C0.ESC + '[M' + String.fromCharCode.apply(String, data)); |
db76868c PK |
908 | } |
909 | ||
910 | function getButton(ev) { | |
911 | var button | |
912 | , shift | |
913 | , meta | |
914 | , ctrl | |
915 | , mod; | |
916 | ||
917 | // two low bits: | |
918 | // 0 = left | |
919 | // 1 = middle | |
920 | // 2 = right | |
921 | // 3 = release | |
922 | // wheel up/down: | |
923 | // 1, and 2 - with 64 added | |
924 | switch (ev.overrideType || ev.type) { | |
925 | case 'mousedown': | |
926 | button = ev.button != null | |
927 | ? +ev.button | |
928 | : ev.which != null | |
929 | ? ev.which - 1 | |
930 | : null; | |
931 | ||
bc70b3b3 | 932 | if (self.browser.isMSIE) { |
db76868c PK |
933 | button = button === 1 ? 0 : button === 4 ? 1 : button; |
934 | } | |
935 | break; | |
936 | case 'mouseup': | |
937 | button = 3; | |
938 | break; | |
939 | case 'DOMMouseScroll': | |
940 | button = ev.detail < 0 | |
941 | ? 64 | |
942 | : 65; | |
943 | break; | |
944 | case 'wheel': | |
945 | button = ev.wheelDeltaY > 0 | |
946 | ? 64 | |
947 | : 65; | |
948 | break; | |
949 | } | |
8bc844c0 | 950 | |
db76868c PK |
951 | // next three bits are the modifiers: |
952 | // 4 = shift, 8 = meta, 16 = control | |
953 | shift = ev.shiftKey ? 4 : 0; | |
954 | meta = ev.metaKey ? 8 : 0; | |
955 | ctrl = ev.ctrlKey ? 16 : 0; | |
956 | mod = shift | meta | ctrl; | |
957 | ||
958 | // no mods | |
959 | if (self.vt200Mouse) { | |
960 | // ctrl only | |
961 | mod &= ctrl; | |
962 | } else if (!self.normalMouse) { | |
963 | mod = 0; | |
964 | } | |
8bc844c0 | 965 | |
db76868c PK |
966 | // increment to SP |
967 | button = (32 + (mod << 2)) + button; | |
a52b7e7a | 968 | |
db76868c PK |
969 | return button; |
970 | } | |
8bc844c0 | 971 | |
db76868c PK |
972 | // mouse coordinates measured in cols/rows |
973 | function getCoords(ev) { | |
974 | var x, y, w, h, el; | |
8bc844c0 | 975 | |
db76868c PK |
976 | // ignore browsers without pageX for now |
977 | if (ev.pageX == null) return; | |
8bc844c0 | 978 | |
db76868c PK |
979 | x = ev.pageX; |
980 | y = ev.pageY; | |
981 | el = self.element; | |
8bc844c0 | 982 | |
db76868c PK |
983 | // should probably check offsetParent |
984 | // but this is more portable | |
985 | while (el && el !== self.document.documentElement) { | |
986 | x -= el.offsetLeft; | |
987 | y -= el.offsetTop; | |
988 | el = 'offsetParent' in el | |
989 | ? el.offsetParent | |
990 | : el.parentNode; | |
991 | } | |
3f455f90 | 992 | |
db76868c | 993 | // convert to cols/rows |
5452986c DI |
994 | x = Math.ceil(x / self.charMeasure.width); |
995 | y = Math.ceil(y / self.charMeasure.height); | |
db76868c PK |
996 | |
997 | // be sure to avoid sending | |
998 | // bad positions to the program | |
999 | if (x < 0) x = 0; | |
1000 | if (x > self.cols) x = self.cols; | |
1001 | if (y < 0) y = 0; | |
1002 | if (y > self.rows) y = self.rows; | |
1003 | ||
1004 | // xterm sends raw bytes and | |
1005 | // starts at 32 (SP) for each. | |
1006 | x += 32; | |
1007 | y += 32; | |
1008 | ||
1009 | return { | |
1010 | x: x, | |
1011 | y: y, | |
1012 | type: 'wheel' | |
f3bd6145 | 1013 | }; |
db76868c | 1014 | } |
8bc844c0 | 1015 | |
db76868c PK |
1016 | on(el, 'mousedown', function(ev) { |
1017 | if (!self.mouseEvents) return; | |
8bc844c0 | 1018 | |
db76868c PK |
1019 | // send the button |
1020 | sendButton(ev); | |
8bc844c0 | 1021 | |
db76868c PK |
1022 | // ensure focus |
1023 | self.focus(); | |
8bc844c0 | 1024 | |
db76868c PK |
1025 | // fix for odd bug |
1026 | //if (self.vt200Mouse && !self.normalMouse) { | |
1027 | if (self.vt200Mouse) { | |
1028 | ev.overrideType = 'mouseup'; | |
1029 | sendButton(ev); | |
1030 | return self.cancel(ev); | |
1031 | } | |
8bc844c0 | 1032 | |
db76868c PK |
1033 | // bind events |
1034 | if (self.normalMouse) on(self.document, 'mousemove', sendMove); | |
b01165c1 | 1035 | |
db76868c PK |
1036 | // x10 compatibility mode can't send button releases |
1037 | if (!self.x10Mouse) { | |
1038 | on(self.document, 'mouseup', function up(ev) { | |
1039 | sendButton(ev); | |
1040 | if (self.normalMouse) off(self.document, 'mousemove', sendMove); | |
1041 | off(self.document, 'mouseup', up); | |
1042 | return self.cancel(ev); | |
4595a181 | 1043 | }); |
db76868c | 1044 | } |
29000fb7 | 1045 | |
db76868c PK |
1046 | return self.cancel(ev); |
1047 | }); | |
1048 | ||
1049 | //if (self.normalMouse) { | |
1050 | // on(self.document, 'mousemove', sendMove); | |
1051 | //} | |
1052 | ||
1053 | on(el, 'wheel', function(ev) { | |
1054 | if (!self.mouseEvents) return; | |
1055 | if (self.x10Mouse | |
1056 | || self.vt300Mouse | |
1057 | || self.decLocator) return; | |
1058 | sendButton(ev); | |
1059 | return self.cancel(ev); | |
1060 | }); | |
1061 | ||
1062 | // allow wheel scrolling in | |
1063 | // the shell for example | |
1064 | on(el, 'wheel', function(ev) { | |
1065 | if (self.mouseEvents) return; | |
db76868c PK |
1066 | self.viewport.onWheel(ev); |
1067 | return self.cancel(ev); | |
1068 | }); | |
1069 | }; | |
fc7b22dc | 1070 | |
db76868c PK |
1071 | /** |
1072 | * Destroys the terminal. | |
1073 | */ | |
1074 | Terminal.prototype.destroy = function() { | |
1075 | this.readable = false; | |
1076 | this.writable = false; | |
1077 | this._events = {}; | |
1078 | this.handler = function() {}; | |
1079 | this.write = function() {}; | |
833eb521 | 1080 | if (this.element && this.element.parentNode) { |
db76868c PK |
1081 | this.element.parentNode.removeChild(this.element); |
1082 | } | |
1083 | //this.emit('close'); | |
1084 | }; | |
670b0d58 | 1085 | |
db76868c | 1086 | /** |
92068f36 DI |
1087 | * Tells the renderer to refresh terminal content between two rows (inclusive) at the next |
1088 | * opportunity. | |
a1d71c91 DI |
1089 | * @param {number} start The row to start from (between 0 and this.rows - 1). |
1090 | * @param {number} end The row to end at (between start and this.rows - 1). | |
7234bfb6 | 1091 | */ |
92068f36 | 1092 | Terminal.prototype.refresh = function(start, end) { |
92068f36 DI |
1093 | if (this.renderer) { |
1094 | this.renderer.queueRefresh(start, end); | |
a1d71c91 DI |
1095 | } |
1096 | }; | |
2207d356 | 1097 | |
a1d71c91 DI |
1098 | /** |
1099 | * Queues linkification for the specified rows. | |
1100 | * @param {number} start The row to start from (between 0 and this.rows - 1). | |
1101 | * @param {number} end The row to end at (between start and this.rows - 1). | |
1102 | */ | |
1103 | Terminal.prototype.queueLinkification = function(start, end) { | |
1104 | if (this.linkifier) { | |
2207d356 DI |
1105 | for (let i = start; i <= end; i++) { |
1106 | this.linkifier.linkifyRow(i); | |
1107 | } | |
db76868c | 1108 | } |
a1d71c91 | 1109 | } |
8bc844c0 | 1110 | |
db76868c PK |
1111 | /** |
1112 | * Display the cursor element | |
1113 | */ | |
1114 | Terminal.prototype.showCursor = function() { | |
1115 | if (!this.cursorState) { | |
1116 | this.cursorState = 1; | |
3c635f3c | 1117 | this.refresh(this.y, this.y); |
db76868c PK |
1118 | } |
1119 | }; | |
8bc844c0 | 1120 | |
db76868c | 1121 | /** |
be56c72b | 1122 | * Scroll the terminal down 1 row, creating a blank line. |
db76868c PK |
1123 | */ |
1124 | Terminal.prototype.scroll = function() { | |
1125 | var row; | |
1126 | ||
c3f46e4a DI |
1127 | // Make room for the new row in lines |
1128 | if (this.lines.length === this.lines.maxLength) { | |
1129 | this.lines.trimStart(1); | |
1130 | this.ybase--; | |
1131 | if (this.ydisp !== 0) { | |
1132 | this.ydisp--; | |
1133 | } | |
1134 | } | |
1135 | ||
30a1f1cc | 1136 | this.ybase++; |
db76868c | 1137 | |
3b35d12e | 1138 | // TODO: Why is this done twice? |
5e68acfc MK |
1139 | if (!this.userScrolling) { |
1140 | this.ydisp = this.ybase; | |
1141 | } | |
db76868c PK |
1142 | |
1143 | // last line | |
1144 | row = this.ybase + this.rows - 1; | |
1145 | ||
1146 | // subtract the bottom scroll region | |
1147 | row -= this.rows - 1 - this.scrollBottom; | |
1148 | ||
6f7cb990 DI |
1149 | if (row === this.lines.length) { |
1150 | // Optimization: pushing is faster than splicing when they amount to the same behavior | |
1151 | this.lines.push(this.blankLine()); | |
1152 | } else { | |
1153 | // add our new line | |
1154 | this.lines.splice(row, 0, this.blankLine()); | |
1155 | } | |
db76868c PK |
1156 | |
1157 | if (this.scrollTop !== 0) { | |
1158 | if (this.ybase !== 0) { | |
1159 | this.ybase--; | |
5e68acfc MK |
1160 | if (!this.userScrolling) { |
1161 | this.ydisp = this.ybase; | |
1162 | } | |
db76868c PK |
1163 | } |
1164 | this.lines.splice(this.ybase + this.scrollTop, 1); | |
1165 | } | |
8bc844c0 | 1166 | |
db76868c PK |
1167 | // this.maxRange(); |
1168 | this.updateRange(this.scrollTop); | |
1169 | this.updateRange(this.scrollBottom); | |
8bc844c0 | 1170 | |
6dcf7267 Y |
1171 | /** |
1172 | * This event is emitted whenever the terminal is scrolled. | |
1173 | * The one parameter passed is the new y display position. | |
1174 | * | |
1175 | * @event scroll | |
1176 | */ | |
db76868c PK |
1177 | this.emit('scroll', this.ydisp); |
1178 | }; | |
1179 | ||
1180 | /** | |
1181 | * Scroll the display of the terminal | |
1182 | * @param {number} disp The number of lines to scroll down (negatives scroll up). | |
1183 | * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used | |
1184 | * to avoid unwanted events being handled by the veiwport when the event was triggered from the | |
1185 | * viewport originally. | |
1186 | */ | |
1187 | Terminal.prototype.scrollDisp = function(disp, suppressScrollEvent) { | |
5e68acfc MK |
1188 | if (disp < 0) { |
1189 | this.userScrolling = true; | |
1190 | } else if (disp + this.ydisp >= this.ybase) { | |
1191 | this.userScrolling = false; | |
1192 | } | |
1193 | ||
db76868c | 1194 | this.ydisp += disp; |
8bc844c0 | 1195 | |
db76868c PK |
1196 | if (this.ydisp > this.ybase) { |
1197 | this.ydisp = this.ybase; | |
1198 | } else if (this.ydisp < 0) { | |
1199 | this.ydisp = 0; | |
1200 | } | |
8bc844c0 | 1201 | |
db76868c PK |
1202 | if (!suppressScrollEvent) { |
1203 | this.emit('scroll', this.ydisp); | |
1204 | } | |
8bc844c0 | 1205 | |
3c635f3c | 1206 | this.refresh(0, this.rows - 1); |
db76868c | 1207 | }; |
8bc844c0 | 1208 | |
fe0d878b DI |
1209 | /** |
1210 | * Scroll the display of the terminal by a number of pages. | |
0ad02a4a | 1211 | * @param {number} pageCount The number of pages to scroll (negative scrolls up). |
fe0d878b DI |
1212 | */ |
1213 | Terminal.prototype.scrollPages = function(pageCount) { | |
1214 | this.scrollDisp(pageCount * (this.rows - 1)); | |
1215 | } | |
1216 | ||
0bf7bf56 DI |
1217 | /** |
1218 | * Scrolls the display of the terminal to the top. | |
1219 | */ | |
e5d130b6 DI |
1220 | Terminal.prototype.scrollToTop = function() { |
1221 | this.scrollDisp(-this.ydisp); | |
1222 | } | |
1223 | ||
0bf7bf56 DI |
1224 | /** |
1225 | * Scrolls the display of the terminal to the bottom. | |
1226 | */ | |
e5d130b6 DI |
1227 | Terminal.prototype.scrollToBottom = function() { |
1228 | this.scrollDisp(this.ybase - this.ydisp); | |
1229 | } | |
1230 | ||
db76868c PK |
1231 | /** |
1232 | * Writes text to the terminal. | |
1233 | * @param {string} text The text to write to the terminal. | |
1234 | */ | |
1235 | Terminal.prototype.write = function(data) { | |
b9d374af | 1236 | this.writeBuffer.push(data); |
6b8c43ed | 1237 | |
e66b1c57 DI |
1238 | // Send XOFF to pause the pty process if the write buffer becomes too large so |
1239 | // xterm.js can catch up before more data is sent. This is necessary in order | |
1240 | // to keep signals such as ^C responsive. | |
c2a69721 | 1241 | if (this.options.useFlowControl && !this.xoffSentToCatchUp && this.writeBuffer.length >= WRITE_BUFFER_PAUSE_THRESHOLD) { |
6b8c43ed DI |
1242 | // XOFF - stop pty pipe |
1243 | // XON will be triggered by emulator before processing data chunk | |
0ec12555 | 1244 | this.send(C0.DC3); |
e66b1c57 | 1245 | this.xoffSentToCatchUp = true; |
6b8c43ed DI |
1246 | } |
1247 | ||
1248 | if (!this.writeInProgress && this.writeBuffer.length > 0) { | |
b9d374af DI |
1249 | // Kick off a write which will write all data in sequence recursively |
1250 | this.writeInProgress = true; | |
1251 | // Kick off an async innerWrite so more writes can come in while processing data | |
6b8c43ed DI |
1252 | var self = this; |
1253 | setTimeout(function () { | |
dc5efa88 | 1254 | self.innerWrite(); |
6b8c43ed | 1255 | }); |
b9d374af DI |
1256 | } |
1257 | } | |
1258 | ||
dc5efa88 | 1259 | Terminal.prototype.innerWrite = function() { |
2b8820fd DI |
1260 | var writeBatch = this.writeBuffer.splice(0, WRITE_BATCH_SIZE); |
1261 | while (writeBatch.length > 0) { | |
1262 | var data = writeBatch.shift(); | |
dc5efa88 | 1263 | var l = data.length, i = 0, j, cs, ch, code, low, ch_width, row; |
e66b1c57 | 1264 | |
dc5efa88 DI |
1265 | // If XOFF was sent in order to catch up with the pty process, resume it if |
1266 | // the writeBuffer is empty to allow more data to come in. | |
2b8820fd | 1267 | if (this.xoffSentToCatchUp && writeBatch.length === 0 && this.writeBuffer.length === 0) { |
0ec12555 | 1268 | this.send(C0.DC1); |
dc5efa88 DI |
1269 | this.xoffSentToCatchUp = false; |
1270 | } | |
db76868c | 1271 | |
dc5efa88 DI |
1272 | this.refreshStart = this.y; |
1273 | this.refreshEnd = this.y; | |
db76868c | 1274 | |
342e862d | 1275 | this.parser.parse(data); |
3f455f90 | 1276 | |
dc5efa88 | 1277 | this.updateRange(this.y); |
3c635f3c | 1278 | this.refresh(this.refreshStart, this.refreshEnd); |
b9d374af | 1279 | } |
2b8820fd DI |
1280 | if (this.writeBuffer.length > 0) { |
1281 | // Allow renderer to catch up before processing the next batch | |
1282 | var self = this; | |
1283 | setTimeout(function () { | |
1284 | self.innerWrite(); | |
1285 | }, 0); | |
1286 | } else { | |
1287 | this.writeInProgress = false; | |
1288 | } | |
db76868c | 1289 | }; |
3f455f90 | 1290 | |
db76868c PK |
1291 | /** |
1292 | * Writes text to the terminal, followed by a break line character (\n). | |
1293 | * @param {string} text The text to write to the terminal. | |
1294 | */ | |
1295 | Terminal.prototype.writeln = function(data) { | |
1296 | this.write(data + '\r\n'); | |
1297 | }; | |
3f455f90 | 1298 | |
db76868c PK |
1299 | /** |
1300 | * Attaches a custom keydown handler which is run before keys are processed, giving consumers of | |
1301 | * xterm.js ultimate control as to what keys should be processed by the terminal and what keys | |
1302 | * should not. | |
1303 | * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a | |
1304 | * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent | |
1305 | * the default action. The function returns whether the event should be processed by xterm.js. | |
1306 | */ | |
1307 | Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { | |
1308 | this.customKeydownHandler = customKeydownHandler; | |
1309 | } | |
3f455f90 | 1310 | |
a1d71c91 DI |
1311 | /** |
1312 | * Attaches a http(s) link handler, forcing web links to behave differently to | |
1313 | * regular <a> tags. This will trigger a refresh as links potentially need to be | |
1314 | * reconstructed. Calling this with null will remove the handler. | |
1315 | * @param {LinkHandler} handler The handler callback function. | |
1316 | */ | |
0f3ee21d DI |
1317 | Terminal.prototype.attachHypertextLinkHandler = function(handler) { |
1318 | if (!this.linkifier) { | |
1319 | throw new Error('Cannot attach a hypertext link handler before Terminal.open is called'); | |
1320 | } | |
1321 | this.linkifier.attachHypertextLinkHandler(handler); | |
1322 | // Refresh to force links to refresh | |
1323 | this.refresh(0, this.rows - 1); | |
1324 | } | |
1325 | ||
c8bb3216 | 1326 | /** |
3b62aa44 DI |
1327 | * Registers a link matcher, allowing custom link patterns to be matched and |
1328 | * handled. | |
1329 | * @param {RegExp} regex The regular expression to search for, specifically | |
1330 | * this searches the textContent of the rows. You will want to use \s to match | |
1331 | * a space ' ' character for example. | |
1332 | * @param {LinkHandler} handler The callback when the link is called. | |
6198556e | 1333 | * @param {LinkMatcherOptions} [options] Options for the link matcher. |
3b62aa44 | 1334 | * @return {number} The ID of the new matcher, this can be used to deregister. |
c8bb3216 | 1335 | */ |
6198556e | 1336 | Terminal.prototype.registerLinkMatcher = function(regex, handler, options) { |
c8bb3216 | 1337 | if (this.linkifier) { |
6198556e | 1338 | var matcherId = this.linkifier.registerLinkMatcher(regex, handler, options); |
1c030f57 DI |
1339 | this.refresh(0, this.rows - 1); |
1340 | return matcherId; | |
c8bb3216 DI |
1341 | } |
1342 | } | |
1343 | ||
1344 | /** | |
1345 | * Deregisters a link matcher if it has been registered. | |
1346 | * @param {number} matcherId The link matcher's ID (returned after register) | |
1347 | */ | |
1348 | Terminal.prototype.deregisterLinkMatcher = function(matcherId) { | |
1349 | if (this.linkifier) { | |
1c030f57 DI |
1350 | if (this.linkifier.deregisterLinkMatcher(matcherId)) { |
1351 | this.refresh(0, this.rows - 1); | |
1352 | } | |
c8bb3216 DI |
1353 | } |
1354 | } | |
1355 | ||
db76868c PK |
1356 | /** |
1357 | * Handle a keydown event | |
1358 | * Key Resources: | |
1359 | * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent | |
1360 | * @param {KeyboardEvent} ev The keydown event to be handled. | |
1361 | */ | |
1362 | Terminal.prototype.keyDown = function(ev) { | |
c15fed38 DI |
1363 | if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) { |
1364 | return false; | |
1365 | } | |
1366 | ||
aca81c76 DI |
1367 | this.restartCursorBlinking(); |
1368 | ||
db76868c | 1369 | if (!this.compositionHelper.keydown.bind(this.compositionHelper)(ev)) { |
3b2e89d8 DI |
1370 | if (this.ybase !== this.ydisp) { |
1371 | this.scrollToBottom(); | |
1372 | } | |
db76868c PK |
1373 | return false; |
1374 | } | |
3f455f90 | 1375 | |
db76868c PK |
1376 | var self = this; |
1377 | var result = this.evaluateKeyEscapeSequence(ev); | |
3f455f90 | 1378 | |
0ec12555 | 1379 | if (result.key === C0.DC3) { // XOFF |
e66b1c57 | 1380 | this.writeStopped = true; |
0ec12555 | 1381 | } else if (result.key === C0.DC1) { // XON |
e66b1c57 | 1382 | this.writeStopped = false; |
b9d374af DI |
1383 | } |
1384 | ||
db76868c PK |
1385 | if (result.scrollDisp) { |
1386 | this.scrollDisp(result.scrollDisp); | |
446c3958 | 1387 | return this.cancel(ev, true); |
db76868c | 1388 | } |
3f455f90 | 1389 | |
db76868c PK |
1390 | if (isThirdLevelShift(this, ev)) { |
1391 | return true; | |
1392 | } | |
3f455f90 | 1393 | |
446c3958 | 1394 | if (result.cancel) { |
db76868c PK |
1395 | // The event is canceled at the end already, is this necessary? |
1396 | this.cancel(ev, true); | |
1397 | } | |
3f455f90 | 1398 | |
db76868c PK |
1399 | if (!result.key) { |
1400 | return true; | |
1401 | } | |
3f455f90 | 1402 | |
db76868c PK |
1403 | this.emit('keydown', ev); |
1404 | this.emit('key', result.key, ev); | |
1405 | this.showCursor(); | |
1406 | this.handler(result.key); | |
3f455f90 | 1407 | |
db76868c PK |
1408 | return this.cancel(ev, true); |
1409 | }; | |
3f455f90 | 1410 | |
db76868c PK |
1411 | /** |
1412 | * Returns an object that determines how a KeyboardEvent should be handled. The key of the | |
1413 | * returned value is the new key code to pass to the PTY. | |
1414 | * | |
1415 | * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html | |
1416 | * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence. | |
1417 | */ | |
1418 | Terminal.prototype.evaluateKeyEscapeSequence = function(ev) { | |
1419 | var result = { | |
1420 | // Whether to cancel event propogation (NOTE: this may not be needed since the event is | |
1421 | // canceled at the end of keyDown | |
1422 | cancel: false, | |
1423 | // The new key even to emit | |
1424 | key: undefined, | |
1425 | // The number of characters to scroll, if this is defined it will cancel the event | |
1426 | scrollDisp: undefined | |
1427 | }; | |
1428 | var modifiers = ev.shiftKey << 0 | ev.altKey << 1 | ev.ctrlKey << 2 | ev.metaKey << 3; | |
1429 | switch (ev.keyCode) { | |
db76868c | 1430 | case 8: |
fca673d6 | 1431 | // backspace |
db76868c | 1432 | if (ev.shiftKey) { |
3de3c0c6 | 1433 | result.key = C0.BS; // ^H |
db76868c PK |
1434 | break; |
1435 | } | |
3de3c0c6 | 1436 | result.key = C0.DEL; // ^? |
db76868c | 1437 | break; |
db76868c | 1438 | case 9: |
fca673d6 | 1439 | // tab |
db76868c | 1440 | if (ev.shiftKey) { |
3de3c0c6 | 1441 | result.key = C0.ESC + '[Z'; |
db76868c PK |
1442 | break; |
1443 | } | |
3de3c0c6 | 1444 | result.key = C0.HT; |
db76868c PK |
1445 | result.cancel = true; |
1446 | break; | |
db76868c | 1447 | case 13: |
fca673d6 | 1448 | // return/enter |
3de3c0c6 | 1449 | result.key = C0.CR; |
db76868c PK |
1450 | result.cancel = true; |
1451 | break; | |
db76868c | 1452 | case 27: |
fca673d6 | 1453 | // escape |
3de3c0c6 | 1454 | result.key = C0.ESC; |
db76868c PK |
1455 | result.cancel = true; |
1456 | break; | |
db76868c | 1457 | case 37: |
fca673d6 | 1458 | // left-arrow |
db76868c | 1459 | if (modifiers) { |
3de3c0c6 | 1460 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'D'; |
db76868c PK |
1461 | // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards |
1462 | // http://unix.stackexchange.com/a/108106 | |
2824da37 | 1463 | // macOS uses different escape sequences than linux |
3de3c0c6 DI |
1464 | if (result.key == C0.ESC + '[1;3D') { |
1465 | result.key = (this.browser.isMac) ? C0.ESC + 'b' : C0.ESC + '[1;5D'; | |
3f455f90 | 1466 | } |
db76868c | 1467 | } else if (this.applicationCursor) { |
3de3c0c6 | 1468 | result.key = C0.ESC + 'OD'; |
db76868c | 1469 | } else { |
3de3c0c6 | 1470 | result.key = C0.ESC + '[D'; |
3f455f90 | 1471 | } |
db76868c | 1472 | break; |
db76868c | 1473 | case 39: |
fca673d6 | 1474 | // right-arrow |
db76868c | 1475 | if (modifiers) { |
3de3c0c6 | 1476 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'C'; |
db76868c PK |
1477 | // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward |
1478 | // http://unix.stackexchange.com/a/108106 | |
2824da37 | 1479 | // macOS uses different escape sequences than linux |
3de3c0c6 DI |
1480 | if (result.key == C0.ESC + '[1;3C') { |
1481 | result.key = (this.browser.isMac) ? C0.ESC + 'f' : C0.ESC + '[1;5C'; | |
db76868c PK |
1482 | } |
1483 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1484 | result.key = C0.ESC + 'OC'; |
db76868c | 1485 | } else { |
3de3c0c6 | 1486 | result.key = C0.ESC + '[C'; |
d4e9d34d | 1487 | } |
db76868c | 1488 | break; |
db76868c | 1489 | case 38: |
fca673d6 | 1490 | // up-arrow |
db76868c | 1491 | if (modifiers) { |
3de3c0c6 | 1492 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'A'; |
db76868c PK |
1493 | // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow |
1494 | // http://unix.stackexchange.com/a/108106 | |
3de3c0c6 DI |
1495 | if (result.key == C0.ESC + '[1;3A') { |
1496 | result.key = C0.ESC + '[1;5A'; | |
db76868c PK |
1497 | } |
1498 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1499 | result.key = C0.ESC + 'OA'; |
db76868c | 1500 | } else { |
3de3c0c6 | 1501 | result.key = C0.ESC + '[A'; |
8faea59e | 1502 | } |
db76868c | 1503 | break; |
db76868c | 1504 | case 40: |
fca673d6 | 1505 | // down-arrow |
db76868c | 1506 | if (modifiers) { |
3de3c0c6 | 1507 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'B'; |
db76868c PK |
1508 | // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow |
1509 | // http://unix.stackexchange.com/a/108106 | |
3de3c0c6 DI |
1510 | if (result.key == C0.ESC + '[1;3B') { |
1511 | result.key = C0.ESC + '[1;5B'; | |
db76868c PK |
1512 | } |
1513 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1514 | result.key = C0.ESC + 'OB'; |
db76868c | 1515 | } else { |
3de3c0c6 | 1516 | result.key = C0.ESC + '[B'; |
3a866cf2 | 1517 | } |
db76868c | 1518 | break; |
db76868c | 1519 | case 45: |
fca673d6 | 1520 | // insert |
db76868c PK |
1521 | if (!ev.shiftKey && !ev.ctrlKey) { |
1522 | // <Ctrl> or <Shift> + <Insert> are used to | |
1523 | // copy-paste on some systems. | |
3de3c0c6 | 1524 | result.key = C0.ESC + '[2~'; |
b01165c1 | 1525 | } |
db76868c | 1526 | break; |
db76868c | 1527 | case 46: |
fca673d6 | 1528 | // delete |
db76868c | 1529 | if (modifiers) { |
3de3c0c6 | 1530 | result.key = C0.ESC + '[3;' + (modifiers + 1) + '~'; |
db76868c | 1531 | } else { |
3de3c0c6 | 1532 | result.key = C0.ESC + '[3~'; |
3a866cf2 | 1533 | } |
db76868c | 1534 | break; |
db76868c | 1535 | case 36: |
fca673d6 | 1536 | // home |
db76868c | 1537 | if (modifiers) |
3de3c0c6 | 1538 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'H'; |
db76868c | 1539 | else if (this.applicationCursor) |
3de3c0c6 | 1540 | result.key = C0.ESC + 'OH'; |
db76868c | 1541 | else |
3de3c0c6 | 1542 | result.key = C0.ESC + '[H'; |
db76868c | 1543 | break; |
db76868c | 1544 | case 35: |
fca673d6 | 1545 | // end |
db76868c | 1546 | if (modifiers) |
3de3c0c6 | 1547 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'F'; |
db76868c | 1548 | else if (this.applicationCursor) |
3de3c0c6 | 1549 | result.key = C0.ESC + 'OF'; |
db76868c | 1550 | else |
3de3c0c6 | 1551 | result.key = C0.ESC + '[F'; |
db76868c | 1552 | break; |
db76868c | 1553 | case 33: |
fca673d6 | 1554 | // page up |
db76868c PK |
1555 | if (ev.shiftKey) { |
1556 | result.scrollDisp = -(this.rows - 1); | |
1557 | } else { | |
3de3c0c6 | 1558 | result.key = C0.ESC + '[5~'; |
3a866cf2 | 1559 | } |
db76868c | 1560 | break; |
db76868c | 1561 | case 34: |
fca673d6 | 1562 | // page down |
db76868c PK |
1563 | if (ev.shiftKey) { |
1564 | result.scrollDisp = this.rows - 1; | |
1565 | } else { | |
3de3c0c6 | 1566 | result.key = C0.ESC + '[6~'; |
3f455f90 | 1567 | } |
db76868c | 1568 | break; |
db76868c | 1569 | case 112: |
fca673d6 | 1570 | // F1-F12 |
db76868c | 1571 | if (modifiers) { |
3de3c0c6 | 1572 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'P'; |
db76868c | 1573 | } else { |
3de3c0c6 | 1574 | result.key = C0.ESC + 'OP'; |
3f455f90 | 1575 | } |
db76868c PK |
1576 | break; |
1577 | case 113: | |
1578 | if (modifiers) { | |
3de3c0c6 | 1579 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'Q'; |
3f455f90 | 1580 | } else { |
3de3c0c6 | 1581 | result.key = C0.ESC + 'OQ'; |
3f455f90 | 1582 | } |
db76868c PK |
1583 | break; |
1584 | case 114: | |
1585 | if (modifiers) { | |
3de3c0c6 | 1586 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'R'; |
db76868c | 1587 | } else { |
3de3c0c6 | 1588 | result.key = C0.ESC + 'OR'; |
3f455f90 | 1589 | } |
db76868c PK |
1590 | break; |
1591 | case 115: | |
1592 | if (modifiers) { | |
3de3c0c6 | 1593 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'S'; |
db76868c | 1594 | } else { |
3de3c0c6 | 1595 | result.key = C0.ESC + 'OS'; |
3f455f90 | 1596 | } |
db76868c PK |
1597 | break; |
1598 | case 116: | |
1599 | if (modifiers) { | |
3de3c0c6 | 1600 | result.key = C0.ESC + '[15;' + (modifiers + 1) + '~'; |
db76868c | 1601 | } else { |
3de3c0c6 | 1602 | result.key = C0.ESC + '[15~'; |
e721bdc9 | 1603 | } |
db76868c PK |
1604 | break; |
1605 | case 117: | |
1606 | if (modifiers) { | |
3de3c0c6 | 1607 | result.key = C0.ESC + '[17;' + (modifiers + 1) + '~'; |
db76868c | 1608 | } else { |
3de3c0c6 | 1609 | result.key = C0.ESC + '[17~'; |
3f455f90 | 1610 | } |
db76868c PK |
1611 | break; |
1612 | case 118: | |
1613 | if (modifiers) { | |
3de3c0c6 | 1614 | result.key = C0.ESC + '[18;' + (modifiers + 1) + '~'; |
db76868c | 1615 | } else { |
3de3c0c6 | 1616 | result.key = C0.ESC + '[18~'; |
3f455f90 | 1617 | } |
db76868c PK |
1618 | break; |
1619 | case 119: | |
1620 | if (modifiers) { | |
3de3c0c6 | 1621 | result.key = C0.ESC + '[19;' + (modifiers + 1) + '~'; |
db76868c | 1622 | } else { |
3de3c0c6 | 1623 | result.key = C0.ESC + '[19~'; |
3f455f90 | 1624 | } |
db76868c PK |
1625 | break; |
1626 | case 120: | |
1627 | if (modifiers) { | |
3de3c0c6 | 1628 | result.key = C0.ESC + '[20;' + (modifiers + 1) + '~'; |
db76868c | 1629 | } else { |
3de3c0c6 | 1630 | result.key = C0.ESC + '[20~'; |
eee99f62 | 1631 | } |
db76868c PK |
1632 | break; |
1633 | case 121: | |
1634 | if (modifiers) { | |
3de3c0c6 | 1635 | result.key = C0.ESC + '[21;' + (modifiers + 1) + '~'; |
db76868c | 1636 | } else { |
3de3c0c6 | 1637 | result.key = C0.ESC + '[21~'; |
3f455f90 | 1638 | } |
db76868c PK |
1639 | break; |
1640 | case 122: | |
1641 | if (modifiers) { | |
3de3c0c6 | 1642 | result.key = C0.ESC + '[23;' + (modifiers + 1) + '~'; |
3f455f90 | 1643 | } else { |
3de3c0c6 | 1644 | result.key = C0.ESC + '[23~'; |
3f455f90 | 1645 | } |
db76868c PK |
1646 | break; |
1647 | case 123: | |
1648 | if (modifiers) { | |
3de3c0c6 | 1649 | result.key = C0.ESC + '[24;' + (modifiers + 1) + '~'; |
db76868c | 1650 | } else { |
3de3c0c6 | 1651 | result.key = C0.ESC + '[24~'; |
3f455f90 | 1652 | } |
db76868c PK |
1653 | break; |
1654 | default: | |
1655 | // a-z and space | |
1656 | if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) { | |
1657 | if (ev.keyCode >= 65 && ev.keyCode <= 90) { | |
1658 | result.key = String.fromCharCode(ev.keyCode - 64); | |
1659 | } else if (ev.keyCode === 32) { | |
1660 | // NUL | |
1661 | result.key = String.fromCharCode(0); | |
1662 | } else if (ev.keyCode >= 51 && ev.keyCode <= 55) { | |
1663 | // escape, file sep, group sep, record sep, unit sep | |
1664 | result.key = String.fromCharCode(ev.keyCode - 51 + 27); | |
1665 | } else if (ev.keyCode === 56) { | |
1666 | // delete | |
1667 | result.key = String.fromCharCode(127); | |
1668 | } else if (ev.keyCode === 219) { | |
15a94240 | 1669 | // ^[ - Control Sequence Introducer (CSI) |
db76868c | 1670 | result.key = String.fromCharCode(27); |
15a94240 DI |
1671 | } else if (ev.keyCode === 220) { |
1672 | // ^\ - String Terminator (ST) | |
1673 | result.key = String.fromCharCode(28); | |
db76868c | 1674 | } else if (ev.keyCode === 221) { |
15a94240 | 1675 | // ^] - Operating System Command (OSC) |
db76868c PK |
1676 | result.key = String.fromCharCode(29); |
1677 | } | |
bc70b3b3 | 1678 | } else if (!this.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) { |
db76868c PK |
1679 | // On Mac this is a third level shift. Use <Esc> instead. |
1680 | if (ev.keyCode >= 65 && ev.keyCode <= 90) { | |
3de3c0c6 | 1681 | result.key = C0.ESC + String.fromCharCode(ev.keyCode + 32); |
db76868c | 1682 | } else if (ev.keyCode === 192) { |
3de3c0c6 | 1683 | result.key = C0.ESC + '`'; |
db76868c | 1684 | } else if (ev.keyCode >= 48 && ev.keyCode <= 57) { |
3de3c0c6 | 1685 | result.key = C0.ESC + (ev.keyCode - 48); |
db76868c | 1686 | } |
3f455f90 | 1687 | } |
db76868c PK |
1688 | break; |
1689 | } | |
b9d374af | 1690 | |
db76868c PK |
1691 | return result; |
1692 | }; | |
3f455f90 | 1693 | |
db76868c PK |
1694 | /** |
1695 | * Set the G level of the terminal | |
1696 | * @param g | |
1697 | */ | |
1698 | Terminal.prototype.setgLevel = function(g) { | |
1699 | this.glevel = g; | |
1700 | this.charset = this.charsets[g]; | |
1701 | }; | |
12a150a4 | 1702 | |
db76868c PK |
1703 | /** |
1704 | * Set the charset for the given G level of the terminal | |
1705 | * @param g | |
1706 | * @param charset | |
1707 | */ | |
1708 | Terminal.prototype.setgCharset = function(g, charset) { | |
1709 | this.charsets[g] = charset; | |
1710 | if (this.glevel === g) { | |
1711 | this.charset = charset; | |
1712 | } | |
1713 | }; | |
12a150a4 | 1714 | |
db76868c PK |
1715 | /** |
1716 | * Handle a keypress event. | |
1717 | * Key Resources: | |
1718 | * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent | |
1719 | * @param {KeyboardEvent} ev The keypress event to be handled. | |
1720 | */ | |
1721 | Terminal.prototype.keyPress = function(ev) { | |
1722 | var key; | |
3f455f90 | 1723 | |
db76868c | 1724 | this.cancel(ev); |
3f455f90 | 1725 | |
db76868c PK |
1726 | if (ev.charCode) { |
1727 | key = ev.charCode; | |
1728 | } else if (ev.which == null) { | |
1729 | key = ev.keyCode; | |
1730 | } else if (ev.which !== 0 && ev.charCode !== 0) { | |
1731 | key = ev.which; | |
1732 | } else { | |
1733 | return false; | |
1734 | } | |
3f455f90 | 1735 | |
db76868c PK |
1736 | if (!key || ( |
1737 | (ev.altKey || ev.ctrlKey || ev.metaKey) && !isThirdLevelShift(this, ev) | |
1738 | )) { | |
1739 | return false; | |
1740 | } | |
12a150a4 | 1741 | |
db76868c | 1742 | key = String.fromCharCode(key); |
3f455f90 | 1743 | |
db76868c PK |
1744 | this.emit('keypress', key, ev); |
1745 | this.emit('key', key, ev); | |
1746 | this.showCursor(); | |
1747 | this.handler(key); | |
12a150a4 | 1748 | |
db76868c PK |
1749 | return false; |
1750 | }; | |
3f455f90 | 1751 | |
db76868c PK |
1752 | /** |
1753 | * Send data for handling to the terminal | |
1754 | * @param {string} data | |
1755 | */ | |
1756 | Terminal.prototype.send = function(data) { | |
1757 | var self = this; | |
3f455f90 | 1758 | |
db76868c PK |
1759 | if (!this.queue) { |
1760 | setTimeout(function() { | |
1761 | self.handler(self.queue); | |
1762 | self.queue = ''; | |
1763 | }, 1); | |
1764 | } | |
3f455f90 | 1765 | |
db76868c PK |
1766 | this.queue += data; |
1767 | }; | |
3f455f90 | 1768 | |
db76868c PK |
1769 | /** |
1770 | * Ring the bell. | |
1771 | * Note: We could do sweet things with webaudio here | |
1772 | */ | |
1773 | Terminal.prototype.bell = function() { | |
1774 | if (!this.visualBell) return; | |
1775 | var self = this; | |
1776 | this.element.style.borderColor = 'white'; | |
1777 | setTimeout(function() { | |
1778 | self.element.style.borderColor = ''; | |
1779 | }, 10); | |
1780 | if (this.popOnBell) this.focus(); | |
1781 | }; | |
c3cf6a22 | 1782 | |
db76868c PK |
1783 | /** |
1784 | * Log the current state to the console. | |
1785 | */ | |
1786 | Terminal.prototype.log = function() { | |
1787 | if (!this.debug) return; | |
1788 | if (!this.context.console || !this.context.console.log) return; | |
1789 | var args = Array.prototype.slice.call(arguments); | |
1790 | this.context.console.log.apply(this.context.console, args); | |
1791 | }; | |
3f455f90 | 1792 | |
db76868c PK |
1793 | /** |
1794 | * Log the current state as error to the console. | |
1795 | */ | |
1796 | Terminal.prototype.error = function() { | |
1797 | if (!this.debug) return; | |
1798 | if (!this.context.console || !this.context.console.error) return; | |
1799 | var args = Array.prototype.slice.call(arguments); | |
1800 | this.context.console.error.apply(this.context.console, args); | |
1801 | }; | |
c3cf6a22 | 1802 | |
db76868c PK |
1803 | /** |
1804 | * Resizes the terminal. | |
1805 | * | |
1806 | * @param {number} x The number of columns to resize to. | |
1807 | * @param {number} y The number of rows to resize to. | |
1808 | */ | |
1809 | Terminal.prototype.resize = function(x, y) { | |
c3256556 | 1810 | if (isNaN(x) || isNaN(y)) { |
ee0d6b45 LB |
1811 | return; |
1812 | } | |
1813 | ||
db76868c PK |
1814 | var line |
1815 | , el | |
1816 | , i | |
1817 | , j | |
1818 | , ch | |
1819 | , addToY; | |
1820 | ||
1821 | if (x === this.cols && y === this.rows) { | |
1822 | return; | |
1823 | } | |
1824 | ||
1825 | if (x < 1) x = 1; | |
1826 | if (y < 1) y = 1; | |
1827 | ||
1828 | // resize cols | |
1829 | j = this.cols; | |
1830 | if (j < x) { | |
1831 | ch = [this.defAttr, ' ', 1]; // does xterm use the default attr? | |
1832 | i = this.lines.length; | |
1833 | while (i--) { | |
607c8191 DI |
1834 | while (this.lines.get(i).length < x) { |
1835 | this.lines.get(i).push(ch); | |
db76868c PK |
1836 | } |
1837 | } | |
db76868c | 1838 | } |
cb77b9dc | 1839 | |
db76868c | 1840 | this.cols = x; |
9a86e4e1 | 1841 | this.setupStops(this.cols); |
db76868c PK |
1842 | |
1843 | // resize rows | |
1844 | j = this.rows; | |
1845 | addToY = 0; | |
1846 | if (j < y) { | |
1847 | el = this.element; | |
1848 | while (j++ < y) { | |
1849 | // y is rows, not this.y | |
1850 | if (this.lines.length < y + this.ybase) { | |
1851 | if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) { | |
1852 | // There is room above the buffer and there are no empty elements below the line, | |
1853 | // scroll up | |
1854 | this.ybase--; | |
1855 | addToY++ | |
1856 | if (this.ydisp > 0) { | |
1857 | // Viewport is at the top of the buffer, must increase downwards | |
1858 | this.ydisp--; | |
1859 | } | |
1860 | } else { | |
1861 | // Add a blank line if there is no buffer left at the top to scroll to, or if there | |
1862 | // are blank lines after the cursor | |
1863 | this.lines.push(this.blankLine()); | |
1864 | } | |
1865 | } | |
1866 | if (this.children.length < y) { | |
1867 | this.insertRow(); | |
1868 | } | |
1869 | } | |
1870 | } else { // (j > y) | |
1871 | while (j-- > y) { | |
1872 | if (this.lines.length > y + this.ybase) { | |
1873 | if (this.lines.length > this.ybase + this.y + 1) { | |
1874 | // The line is a blank line below the cursor, remove it | |
1875 | this.lines.pop(); | |
1876 | } else { | |
1877 | // The line is the cursor, scroll down | |
1878 | this.ybase++; | |
1879 | this.ydisp++; | |
1880 | } | |
1881 | } | |
1882 | if (this.children.length > y) { | |
1883 | el = this.children.shift(); | |
1884 | if (!el) continue; | |
1885 | el.parentNode.removeChild(el); | |
1886 | } | |
1887 | } | |
1888 | } | |
1889 | this.rows = y; | |
3f455f90 | 1890 | |
db76868c PK |
1891 | // Make sure that the cursor stays on screen |
1892 | if (this.y >= y) { | |
1893 | this.y = y - 1; | |
1894 | } | |
1895 | if (addToY) { | |
1896 | this.y += addToY; | |
1897 | } | |
12a150a4 | 1898 | |
db76868c PK |
1899 | if (this.x >= x) { |
1900 | this.x = x - 1; | |
1901 | } | |
3f455f90 | 1902 | |
db76868c PK |
1903 | this.scrollTop = 0; |
1904 | this.scrollBottom = y - 1; | |
12a150a4 | 1905 | |
4f18d842 DI |
1906 | this.charMeasure.measure(); |
1907 | ||
3c635f3c | 1908 | this.refresh(0, this.rows - 1); |
3f455f90 | 1909 | |
db76868c | 1910 | this.normal = null; |
12a150a4 | 1911 | |
a9417c68 | 1912 | this.geometry = [this.cols, this.rows]; |
db76868c PK |
1913 | this.emit('resize', {terminal: this, cols: x, rows: y}); |
1914 | }; | |
3f455f90 | 1915 | |
db76868c PK |
1916 | /** |
1917 | * Updates the range of rows to refresh | |
1918 | * @param {number} y The number of rows to refresh next. | |
1919 | */ | |
1920 | Terminal.prototype.updateRange = function(y) { | |
1921 | if (y < this.refreshStart) this.refreshStart = y; | |
1922 | if (y > this.refreshEnd) this.refreshEnd = y; | |
1923 | // if (y > this.refreshEnd) { | |
1924 | // this.refreshEnd = y; | |
1925 | // if (y > this.rows - 1) { | |
1926 | // this.refreshEnd = this.rows - 1; | |
1927 | // } | |
1928 | // } | |
1929 | }; | |
3f455f90 | 1930 | |
db76868c | 1931 | /** |
0de3d839 | 1932 | * Set the range of refreshing to the maximum value |
db76868c PK |
1933 | */ |
1934 | Terminal.prototype.maxRange = function() { | |
1935 | this.refreshStart = 0; | |
1936 | this.refreshEnd = this.rows - 1; | |
1937 | }; | |
12a150a4 | 1938 | |
3f455f90 | 1939 | |
12a150a4 | 1940 | |
db76868c PK |
1941 | /** |
1942 | * Setup the tab stops. | |
1943 | * @param {number} i | |
1944 | */ | |
1945 | Terminal.prototype.setupStops = function(i) { | |
1946 | if (i != null) { | |
1947 | if (!this.tabs[i]) { | |
1948 | i = this.prevStop(i); | |
1949 | } | |
1950 | } else { | |
1951 | this.tabs = {}; | |
1952 | i = 0; | |
1953 | } | |
3f455f90 | 1954 | |
f4293a6d | 1955 | for (; i < this.cols; i += this.getOption('tabStopWidth')) { |
db76868c PK |
1956 | this.tabs[i] = true; |
1957 | } | |
1958 | }; | |
12a150a4 | 1959 | |
3f455f90 | 1960 | |
db76868c PK |
1961 | /** |
1962 | * Move the cursor to the previous tab stop from the given position (default is current). | |
1963 | * @param {number} x The position to move the cursor to the previous tab stop. | |
1964 | */ | |
1965 | Terminal.prototype.prevStop = function(x) { | |
1966 | if (x == null) x = this.x; | |
1967 | while (!this.tabs[--x] && x > 0); | |
1968 | return x >= this.cols | |
1969 | ? this.cols - 1 | |
1970 | : x < 0 ? 0 : x; | |
1971 | }; | |
12a150a4 | 1972 | |
3f455f90 | 1973 | |
db76868c PK |
1974 | /** |
1975 | * Move the cursor one tab stop forward from the given position (default is current). | |
1976 | * @param {number} x The position to move the cursor one tab stop forward. | |
1977 | */ | |
1978 | Terminal.prototype.nextStop = function(x) { | |
1979 | if (x == null) x = this.x; | |
1980 | while (!this.tabs[++x] && x < this.cols); | |
1981 | return x >= this.cols | |
1982 | ? this.cols - 1 | |
1983 | : x < 0 ? 0 : x; | |
1984 | }; | |
3f455f90 | 1985 | |
12a150a4 | 1986 | |
db76868c PK |
1987 | /** |
1988 | * Erase in the identified line everything from "x" to the end of the line (right). | |
1989 | * @param {number} x The column from which to start erasing to the end of the line. | |
1990 | * @param {number} y The line in which to operate. | |
1991 | */ | |
1992 | Terminal.prototype.eraseRight = function(x, y) { | |
6cd5c2b8 DI |
1993 | var line = this.lines.get(this.ybase + y); |
1994 | if (!line) { | |
1995 | return; | |
1996 | } | |
1997 | var ch = [this.eraseAttr(), ' ', 1]; // xterm | |
db76868c PK |
1998 | for (; x < this.cols; x++) { |
1999 | line[x] = ch; | |
2000 | } | |
db76868c PK |
2001 | this.updateRange(y); |
2002 | }; | |
12a150a4 | 2003 | |
3f455f90 | 2004 | |
12a150a4 | 2005 | |
db76868c PK |
2006 | /** |
2007 | * Erase in the identified line everything from "x" to the start of the line (left). | |
2008 | * @param {number} x The column from which to start erasing to the start of the line. | |
2009 | * @param {number} y The line in which to operate. | |
2010 | */ | |
2011 | Terminal.prototype.eraseLeft = function(x, y) { | |
6cd5c2b8 DI |
2012 | var line = this.lines.get(this.ybase + y); |
2013 | if (!line) { | |
2014 | return; | |
2015 | } | |
2016 | var ch = [this.eraseAttr(), ' ', 1]; // xterm | |
db76868c | 2017 | x++; |
6cd5c2b8 DI |
2018 | while (x--) { |
2019 | line[x] = ch; | |
2020 | } | |
db76868c PK |
2021 | this.updateRange(y); |
2022 | }; | |
3f455f90 | 2023 | |
76719413 DI |
2024 | /** |
2025 | * Clears the entire buffer, making the prompt line the new first line. | |
2026 | */ | |
2027 | Terminal.prototype.clear = function() { | |
852dac4d DI |
2028 | if (this.ybase === 0 && this.y === 0) { |
2029 | // Don't clear if it's already clear | |
2030 | return; | |
2031 | } | |
607c8191 DI |
2032 | this.lines.set(0, this.lines.get(this.ybase + this.y)); |
2033 | this.lines.length = 1; | |
76719413 DI |
2034 | this.ydisp = 0; |
2035 | this.ybase = 0; | |
2036 | this.y = 0; | |
76719413 DI |
2037 | for (var i = 1; i < this.rows; i++) { |
2038 | this.lines.push(this.blankLine()); | |
2039 | } | |
3c635f3c | 2040 | this.refresh(0, this.rows - 1); |
76719413 DI |
2041 | this.emit('scroll', this.ydisp); |
2042 | }; | |
3f455f90 | 2043 | |
db76868c PK |
2044 | /** |
2045 | * Erase all content in the given line | |
2046 | * @param {number} y The line to erase all of its contents. | |
2047 | */ | |
2048 | Terminal.prototype.eraseLine = function(y) { | |
2049 | this.eraseRight(0, y); | |
2050 | }; | |
3f455f90 | 2051 | |
3f455f90 | 2052 | |
db76868c | 2053 | /** |
cc5ae819 | 2054 | * Return the data array of a blank line |
db76868c PK |
2055 | * @param {number} cur First bunch of data for each "blank" character. |
2056 | */ | |
2057 | Terminal.prototype.blankLine = function(cur) { | |
2058 | var attr = cur | |
2059 | ? this.eraseAttr() | |
2060 | : this.defAttr; | |
12a150a4 | 2061 | |
db76868c PK |
2062 | var ch = [attr, ' ', 1] // width defaults to 1 halfwidth character |
2063 | , line = [] | |
2064 | , i = 0; | |
3f455f90 | 2065 | |
db76868c PK |
2066 | for (; i < this.cols; i++) { |
2067 | line[i] = ch; | |
2068 | } | |
12a150a4 | 2069 | |
db76868c PK |
2070 | return line; |
2071 | }; | |
3f455f90 | 2072 | |
12a150a4 | 2073 | |
db76868c PK |
2074 | /** |
2075 | * If cur return the back color xterm feature attribute. Else return defAttr. | |
2076 | * @param {object} cur | |
2077 | */ | |
2078 | Terminal.prototype.ch = function(cur) { | |
2079 | return cur | |
2080 | ? [this.eraseAttr(), ' ', 1] | |
2081 | : [this.defAttr, ' ', 1]; | |
2082 | }; | |
3f455f90 | 2083 | |
3f455f90 | 2084 | |
db76868c PK |
2085 | /** |
2086 | * Evaluate if the current erminal is the given argument. | |
2087 | * @param {object} term The terminal to evaluate | |
2088 | */ | |
2089 | Terminal.prototype.is = function(term) { | |
2090 | var name = this.termName; | |
2091 | return (name + '').indexOf(term) === 0; | |
2092 | }; | |
3f455f90 | 2093 | |
12a150a4 | 2094 | |
db76868c | 2095 | /** |
32e878db DI |
2096 | * Emit the 'data' event and populate the given data. |
2097 | * @param {string} data The data to populate in the event. | |
2098 | */ | |
db76868c | 2099 | Terminal.prototype.handler = function(data) { |
d9d60063 DI |
2100 | // Prevents all events to pty process if stdin is disabled |
2101 | if (this.options.disableStdin) { | |
2102 | return; | |
2103 | } | |
2104 | ||
2bc8adee DI |
2105 | // Input is being sent to the terminal, the terminal should focus the prompt. |
2106 | if (this.ybase !== this.ydisp) { | |
2107 | this.scrollToBottom(); | |
2108 | } | |
db76868c PK |
2109 | this.emit('data', data); |
2110 | }; | |
3f455f90 | 2111 | |
12a150a4 | 2112 | |
db76868c PK |
2113 | /** |
2114 | * Emit the 'title' event and populate the given title. | |
2115 | * @param {string} title The title to populate in the event. | |
2116 | */ | |
2117 | Terminal.prototype.handleTitle = function(title) { | |
1fc5a9aa Y |
2118 | /** |
2119 | * This event is emitted when the title of the terminal is changed | |
2120 | * from inside the terminal. The parameter is the new title. | |
2121 | * | |
2122 | * @event title | |
2123 | */ | |
db76868c PK |
2124 | this.emit('title', title); |
2125 | }; | |
3f455f90 | 2126 | |
3f455f90 | 2127 | |
db76868c PK |
2128 | /** |
2129 | * ESC | |
2130 | */ | |
3f455f90 | 2131 | |
db76868c PK |
2132 | /** |
2133 | * ESC D Index (IND is 0x84). | |
2134 | */ | |
2135 | Terminal.prototype.index = function() { | |
2136 | this.y++; | |
2137 | if (this.y > this.scrollBottom) { | |
2138 | this.y--; | |
2139 | this.scroll(); | |
2140 | } | |
5850cbcd | 2141 | // If the end of the line is hit, prevent this action from wrapping around to the next line. |
72063329 DI |
2142 | if (this.x >= this.cols) { |
2143 | this.x--; | |
2144 | } | |
db76868c | 2145 | }; |
3f455f90 | 2146 | |
3f455f90 | 2147 | |
db76868c PK |
2148 | /** |
2149 | * ESC M Reverse Index (RI is 0x8d). | |
3b35d12e DI |
2150 | * |
2151 | * Move the cursor up one row, inserting a new blank line if necessary. | |
db76868c PK |
2152 | */ |
2153 | Terminal.prototype.reverseIndex = function() { | |
2154 | var j; | |
3b35d12e | 2155 | if (this.y === this.scrollTop) { |
db76868c PK |
2156 | // possibly move the code below to term.reverseScroll(); |
2157 | // test: echo -ne '\e[1;1H\e[44m\eM\e[0m' | |
2158 | // blankLine(true) is xterm/linux behavior | |
bf16fdc0 DI |
2159 | this.lines.shiftElements(this.y + this.ybase, this.rows - 1, 1); |
2160 | this.lines.set(this.y + this.ybase, this.blankLine(true)); | |
db76868c PK |
2161 | this.updateRange(this.scrollTop); |
2162 | this.updateRange(this.scrollBottom); | |
3b35d12e DI |
2163 | } else { |
2164 | this.y--; | |
db76868c | 2165 | } |
db76868c | 2166 | }; |
3f455f90 | 2167 | |
12a150a4 | 2168 | |
db76868c PK |
2169 | /** |
2170 | * ESC c Full Reset (RIS). | |
2171 | */ | |
2172 | Terminal.prototype.reset = function() { | |
2173 | this.options.rows = this.rows; | |
2174 | this.options.cols = this.cols; | |
2175 | var customKeydownHandler = this.customKeydownHandler; | |
2176 | Terminal.call(this, this.options); | |
2177 | this.customKeydownHandler = customKeydownHandler; | |
3c635f3c | 2178 | this.refresh(0, this.rows - 1); |
c8b19493 | 2179 | this.viewport.syncScrollArea(); |
db76868c | 2180 | }; |
3f455f90 | 2181 | |
12a150a4 | 2182 | |
db76868c PK |
2183 | /** |
2184 | * ESC H Tab Set (HTS is 0x88). | |
2185 | */ | |
2186 | Terminal.prototype.tabSet = function() { | |
2187 | this.tabs[this.x] = true; | |
db76868c | 2188 | }; |
3f455f90 | 2189 | |
db76868c PK |
2190 | /** |
2191 | * Helpers | |
2192 | */ | |
2193 | ||
db76868c PK |
2194 | function on(el, type, handler, capture) { |
2195 | if (!Array.isArray(el)) { | |
2196 | el = [el]; | |
2197 | } | |
2198 | el.forEach(function (element) { | |
2199 | element.addEventListener(type, handler, capture || false); | |
2200 | }); | |
2201 | } | |
2202 | ||
2203 | function off(el, type, handler, capture) { | |
2204 | el.removeEventListener(type, handler, capture || false); | |
2205 | } | |
2206 | ||
2207 | function cancel(ev, force) { | |
2208 | if (!this.cancelEvents && !force) { | |
2209 | return; | |
2210 | } | |
2211 | ev.preventDefault(); | |
2212 | ev.stopPropagation(); | |
2213 | return false; | |
2214 | } | |
2215 | ||
2216 | function inherits(child, parent) { | |
2217 | function f() { | |
2218 | this.constructor = child; | |
2219 | } | |
2220 | f.prototype = parent.prototype; | |
2221 | child.prototype = new f; | |
2222 | } | |
2223 | ||
db76868c PK |
2224 | function indexOf(obj, el) { |
2225 | var i = obj.length; | |
2226 | while (i--) { | |
2227 | if (obj[i] === el) return i; | |
2228 | } | |
2229 | return -1; | |
2230 | } | |
2231 | ||
2232 | function isThirdLevelShift(term, ev) { | |
2233 | var thirdLevelKey = | |
bc70b3b3 PK |
2234 | (term.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) || |
2235 | (term.browser.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey); | |
db76868c PK |
2236 | |
2237 | if (ev.type == 'keypress') { | |
2238 | return thirdLevelKey; | |
2239 | } | |
2240 | ||
2241 | // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events) | |
2242 | return thirdLevelKey && (!ev.keyCode || ev.keyCode > 47); | |
2243 | } | |
2244 | ||
db81c28b DI |
2245 | // Expose to InputHandler (temporary) |
2246 | Terminal.prototype.matchColor = matchColor; | |
2247 | ||
db76868c PK |
2248 | function matchColor(r1, g1, b1) { |
2249 | var hash = (r1 << 16) | (g1 << 8) | b1; | |
2250 | ||
2251 | if (matchColor._cache[hash] != null) { | |
2252 | return matchColor._cache[hash]; | |
2253 | } | |
2254 | ||
2255 | var ldiff = Infinity | |
2256 | , li = -1 | |
2257 | , i = 0 | |
2258 | , c | |
2259 | , r2 | |
2260 | , g2 | |
2261 | , b2 | |
2262 | , diff; | |
2263 | ||
2264 | for (; i < Terminal.vcolors.length; i++) { | |
2265 | c = Terminal.vcolors[i]; | |
2266 | r2 = c[0]; | |
2267 | g2 = c[1]; | |
2268 | b2 = c[2]; | |
2269 | ||
2270 | diff = matchColor.distance(r1, g1, b1, r2, g2, b2); | |
2271 | ||
2272 | if (diff === 0) { | |
2273 | li = i; | |
2274 | break; | |
2275 | } | |
2276 | ||
2277 | if (diff < ldiff) { | |
2278 | ldiff = diff; | |
2279 | li = i; | |
2280 | } | |
2281 | } | |
2282 | ||
2283 | return matchColor._cache[hash] = li; | |
2284 | } | |
2285 | ||
2286 | matchColor._cache = {}; | |
2287 | ||
2288 | // http://stackoverflow.com/questions/1633828 | |
2289 | matchColor.distance = function(r1, g1, b1, r2, g2, b2) { | |
2290 | return Math.pow(30 * (r1 - r2), 2) | |
2291 | + Math.pow(59 * (g1 - g2), 2) | |
2292 | + Math.pow(11 * (b1 - b2), 2); | |
2293 | }; | |
2294 | ||
2295 | function each(obj, iter, con) { | |
2296 | if (obj.forEach) return obj.forEach(iter, con); | |
2297 | for (var i = 0; i < obj.length; i++) { | |
2298 | iter.call(con, obj[i], i, obj); | |
2299 | } | |
2300 | } | |
2301 | ||
5e1ccb35 DI |
2302 | function wasMondifierKeyOnlyEvent(ev) { |
2303 | return ev.keyCode === 16 || // Shift | |
2304 | ev.keyCode === 17 || // Ctrl | |
2305 | ev.keyCode === 18; // Alt | |
2306 | } | |
db76868c | 2307 | |
db76868c PK |
2308 | function keys(obj) { |
2309 | if (Object.keys) return Object.keys(obj); | |
2310 | var key, keys = []; | |
2311 | for (key in obj) { | |
2312 | if (Object.prototype.hasOwnProperty.call(obj, key)) { | |
2313 | keys.push(key); | |
2314 | } | |
2315 | } | |
2316 | return keys; | |
2317 | } | |
2318 | ||
db76868c PK |
2319 | /** |
2320 | * Expose | |
2321 | */ | |
2322 | ||
2323 | Terminal.EventEmitter = EventEmitter; | |
db76868c PK |
2324 | Terminal.inherits = inherits; |
2325 | ||
2326 | /** | |
2327 | * Adds an event listener to the terminal. | |
2328 | * | |
2329 | * @param {string} event The name of the event. TODO: Document all event types | |
2330 | * @param {function} callback The function to call when the event is triggered. | |
2331 | */ | |
2332 | Terminal.on = on; | |
2333 | Terminal.off = off; | |
2334 | Terminal.cancel = cancel; | |
8bc844c0 | 2335 | |
ed1a31d1 | 2336 | module.exports = Terminal; |