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