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