blob: 3347ed1f8fb2d604919d6bcac0e6057e09f0727a [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * Constructor for the Terminal class.
7 *
8 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
9 * classes to provide the complete terminal functionality.
10 *
11 * There are a number of lower-level Terminal methods that can be called
12 * directly to manipulate the cursor, text, scroll region, and other terminal
13 * attributes. However, the primary method is interpret(), which parses VT
14 * escape sequences and invokes the appropriate Terminal methods.
15 *
16 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
17 *
18 * TODO(rginda): Eventually we're going to need to support characters which are
19 * displayed twice as wide as standard latin characters. This is to support
20 * CJK (and possibly other character sets).
21 */
rginda35c456b2012-02-09 17:29:05 -080022hterm.Terminal = function() {
rginda8ba33642011-12-14 12:31:31 -080023 // Two screen instances.
24 this.primaryScreen_ = new hterm.Screen();
25 this.alternateScreen_ = new hterm.Screen();
26
27 // The "current" screen.
28 this.screen_ = this.primaryScreen_;
29
rginda8ba33642011-12-14 12:31:31 -080030 // The local notion of the screen size. ScreenBuffers also have a size which
31 // indicates their present size. During size changes, the two may disagree.
32 // Also, the inactive screen's size is not altered until it is made the active
33 // screen.
34 this.screenSize = new hterm.Size(0, 0);
35
rginda8ba33642011-12-14 12:31:31 -080036 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080037 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080038 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
39 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080040 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080041
rginda87b86462011-12-14 13:48:03 -080042 // The div that contains this terminal.
43 this.div_ = null;
44
rgindac9bc5502012-01-18 11:48:44 -080045 // The document that contains the scrollPort. Defaulted to the global
46 // document here so that the terminal is functional even if it hasn't been
47 // inserted into a document yet, but re-set in decorate().
48 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080049
rginda8ba33642011-12-14 12:31:31 -080050 // The rows that have scrolled off screen and are no longer addressable.
51 this.scrollbackRows_ = [];
52
rgindac9bc5502012-01-18 11:48:44 -080053 // Saved tab stops.
54 this.tabStops_ = [];
55
rginda8ba33642011-12-14 12:31:31 -080056 // The VT's notion of the top and bottom rows. Used during some VT
57 // cursor positioning and scrolling commands.
58 this.vtScrollTop_ = null;
59 this.vtScrollBottom_ = null;
60
61 // The DIV element for the visible cursor.
62 this.cursorNode_ = null;
63
rgindaf0090c92012-02-10 14:58:52 -080064 // Terminal bell sound.
65 this.bellAudio_ = this.document_.createElement('audio');
66 this.bellAudio_.setAttribute('src', '../audio/bell.ogg');
67 this.bellAudio_.setAttribute('preload', 'auto');
68
rginda6d397402012-01-17 10:58:29 -080069 // Cursor position and attributes saved with DECSC.
70 this.savedOptions_ = {};
71
rginda8ba33642011-12-14 12:31:31 -080072 // The current mode bits for the terminal.
73 this.options_ = new hterm.Options();
74
75 // Timeouts we might need to clear.
76 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080077
78 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080079 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080080
rgindafeaf3142012-01-31 15:14:20 -080081 // The keyboard hander.
82 this.keyboard = new hterm.Keyboard(this);
83
rginda87b86462011-12-14 13:48:03 -080084 // General IO interface that can be given to third parties without exposing
85 // the entire terminal object.
86 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080087
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040088 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -080089 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -080090};
91
92/**
rginda35c456b2012-02-09 17:29:05 -080093 * Default font family for the terminal text.
94 */
95
96hterm.Terminal.prototype.defaultFontFamily =
97 '"DejaVu Sans Mono", "Everson Mono", FreeMono, ' +
rgindafd19b252012-02-28 15:30:59 -080098 '"Menlo", "Lucida Console", monospace';
rginda35c456b2012-02-09 17:29:05 -080099
100/**
101 * The default colors for text with no other color attributes.
102 */
103hterm.Terminal.prototype.backgroundColor = 'black';
104hterm.Terminal.prototype.foregroundColor = 'white';
105
106/**
107 * Default tab with of 8 to match xterm.
108 */
109hterm.Terminal.prototype.tabWidth = 8;
110
111/**
112 * The color of the visible cursor.
113 */
114hterm.Terminal.prototype.cursorColor = 'rgba(255,0,0,0.5)';
115
116/**
117 * If true, scroll to the bottom on any keystroke.
118 */
119hterm.Terminal.prototype.scrollOnKeystroke = true;
120
121/**
122 * If true, scroll to the bottom on terminal output.
123 */
124hterm.Terminal.prototype.scrollOnOutput = false;
125
126/**
127 * The default font size in pixels.
128 */
129hterm.Terminal.prototype.defaultFontSizePx = 15;
130
131/**
132 * The assumed width of a scrollbar.
133 */
134hterm.Terminal.prototype.scrollbarWidthPx = 16;
135
136/**
rginda87b86462011-12-14 13:48:03 -0800137 * Create a new instance of a terminal command and run it with a given
138 * argument string.
139 *
140 * @param {function} commandClass The constructor for a terminal command.
141 * @param {string} argString The argument string to pass to the command.
142 */
143hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
144 var self = this;
145 this.command = new commandClass(
146 { argString: argString || '',
147 io: this.io.push(),
148 onExit: function(code) {
149 self.io.pop();
150 self.io.println(hterm.msg('COMMAND_COMPLETE',
151 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800152 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800153 }
154 });
155
rgindafeaf3142012-01-31 15:14:20 -0800156 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800157 this.command.run();
158};
159
160/**
rgindafeaf3142012-01-31 15:14:20 -0800161 * Returns true if the current screen is the primary screen, false otherwise.
162 */
163hterm.Terminal.prototype.isPrimaryScreen = function() {
164 return this.screen_ = this.primaryScreen_;
165};
166
167/**
168 * Install the keyboard handler for this terminal.
169 *
170 * This will prevent the browser from seeing any keystrokes sent to the
171 * terminal.
172 */
173hterm.Terminal.prototype.installKeyboard = function() {
174 this.keyboard.installKeyboard(this.document_.body.firstChild);
175}
176
177/**
178 * Uninstall the keyboard handler for this terminal.
179 */
180hterm.Terminal.prototype.uninstallKeyboard = function() {
181 this.keyboard.installKeyboard(null);
182}
183
184/**
rginda35c456b2012-02-09 17:29:05 -0800185 * Set the font size for this terminal.
186 */
187hterm.Terminal.prototype.setFontSize = function(px) {
188 this.scrollPort_.setFontSize(px);
189};
190
191/**
192 * Get the current font size.
193 */
194hterm.Terminal.prototype.getFontSize = function() {
195 return this.scrollPort_.getFontSize();
196};
197
198/**
199 * Set the CSS "font-family" for this terminal.
200 */
201hterm.Terminal.prototype.setFontFamily = function(str) {
202 this.scrollPort_.setFontFamily(str);
rgindaf7521392012-02-28 17:20:34 -0800203 var normalSize = this.scrollPort_.measureCharacterSize();
204 var boldSize = this.scrollPort_.measureCharacterSize('bold');
205
206 var isBoldSafe = normalSize.equals(boldSize);
207 this.screen_.textAttributes.enableBold = isBoldSafe;
208 if (!isBoldSafe) {
209 console.warn('Bold characters disabled: Size of bold weight differs ' +
210 'from normal. Font family is: ' + str);
211 }
rginda35c456b2012-02-09 17:29:05 -0800212};
213
214/**
rginda87b86462011-12-14 13:48:03 -0800215 * Return a copy of the current cursor position.
216 *
217 * @return {hterm.RowCol} The RowCol object representing the current position.
218 */
219hterm.Terminal.prototype.saveCursor = function() {
220 return this.screen_.cursorPosition.clone();
221};
222
rgindaa19afe22012-01-25 15:40:22 -0800223hterm.Terminal.prototype.getTextAttributes = function() {
224 return this.screen_.textAttributes;
225};
226
rginda87b86462011-12-14 13:48:03 -0800227/**
rginda9846e2f2012-01-27 13:53:33 -0800228 * Change the title of this terminal's window.
229 */
230hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800231 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800232};
233
234/**
rginda87b86462011-12-14 13:48:03 -0800235 * Restore a previously saved cursor position.
236 *
237 * @param {hterm.RowCol} cursor The position to restore.
238 */
239hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800240 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
241 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
242 this.screen_.setCursorPosition(row, column);
243 if (cursor.column > column ||
244 cursor.column == column && cursor.overflow) {
245 this.screen_.cursorPosition.overflow = true;
246 }
rginda87b86462011-12-14 13:48:03 -0800247};
248
249/**
250 * Set the width of the terminal, resizing the UI to match.
251 */
252hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800253 if (columnCount == null) {
254 this.div_.style.width = '100%';
255 return;
256 }
257
rginda35c456b2012-02-09 17:29:05 -0800258 this.div_.style.width = this.scrollPort_.characterSize.width *
259 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400260 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800261 this.scheduleSyncCursorPosition_();
262};
rginda87b86462011-12-14 13:48:03 -0800263
rgindac9bc5502012-01-18 11:48:44 -0800264/**
rginda35c456b2012-02-09 17:29:05 -0800265 * Set the height of the terminal, resizing the UI to match.
266 */
267hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800268 if (rowCount == null) {
269 this.div_.style.height = '100%';
270 return;
271 }
272
rginda35c456b2012-02-09 17:29:05 -0800273 this.div_.style.height =
274 this.scrollPort_.characterSize.height * rowCount + 'px';
275 this.realizeSize_(this.screenSize.width, rowCount);
276 this.scheduleSyncCursorPosition_();
277};
278
279/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400280 * Deal with terminal size changes.
281 *
282 */
283hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
284 if (columnCount != this.screenSize.width)
285 this.realizeWidth_(columnCount);
286
287 if (rowCount != this.screenSize.height)
288 this.realizeHeight_(rowCount);
289
290 // Send new terminal size to plugin.
291 this.io.onTerminalResize(columnCount, rowCount);
292};
293
294/**
rgindac9bc5502012-01-18 11:48:44 -0800295 * Deal with terminal width changes.
296 *
297 * This function does what needs to be done when the terminal width changes
298 * out from under us. It happens here rather than in onResize_() because this
299 * code may need to run synchronously to handle programmatic changes of
300 * terminal width.
301 *
302 * Relying on the browser to send us an async resize event means we may not be
303 * in the correct state yet when the next escape sequence hits.
304 */
305hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
306 var deltaColumns = columnCount - this.screen_.getWidth();
307
rginda87b86462011-12-14 13:48:03 -0800308 this.screenSize.width = columnCount;
309 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800310
311 if (deltaColumns > 0) {
312 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
313 } else {
314 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
315 if (this.tabStops_[i] <= columnCount)
316 break;
317
318 this.tabStops_.pop();
319 }
320 }
321
322 this.screen_.setColumnCount(this.screenSize.width);
323};
324
325/**
326 * Deal with terminal height changes.
327 *
328 * This function does what needs to be done when the terminal height changes
329 * out from under us. It happens here rather than in onResize_() because this
330 * code may need to run synchronously to handle programmatic changes of
331 * terminal height.
332 *
333 * Relying on the browser to send us an async resize event means we may not be
334 * in the correct state yet when the next escape sequence hits.
335 */
336hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
337 var deltaRows = rowCount - this.screen_.getHeight();
338
339 this.screenSize.height = rowCount;
340
341 var cursor = this.saveCursor();
342
343 if (deltaRows < 0) {
344 // Screen got smaller.
345 deltaRows *= -1;
346 while (deltaRows) {
347 var lastRow = this.getRowCount() - 1;
348 if (lastRow - this.scrollbackRows_.length == cursor.row)
349 break;
350
351 if (this.getRowText(lastRow))
352 break;
353
354 this.screen_.popRow();
355 deltaRows--;
356 }
357
358 var ary = this.screen_.shiftRows(deltaRows);
359 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
360
361 // We just removed rows from the top of the screen, we need to update
362 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800363 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800364 } else if (deltaRows > 0) {
365 // Screen got larger.
366
367 if (deltaRows <= this.scrollbackRows_.length) {
368 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
369 var rows = this.scrollbackRows_.splice(
370 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
371 this.screen_.unshiftRows(rows);
372 deltaRows -= scrollbackCount;
373 cursor.row += scrollbackCount;
374 }
375
376 if (deltaRows)
377 this.appendRows_(deltaRows);
378 }
379
rginda35c456b2012-02-09 17:29:05 -0800380 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800381 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800382};
383
384/**
385 * Scroll the terminal to the top of the scrollback buffer.
386 */
387hterm.Terminal.prototype.scrollHome = function() {
388 this.scrollPort_.scrollRowToTop(0);
389};
390
391/**
392 * Scroll the terminal to the end.
393 */
394hterm.Terminal.prototype.scrollEnd = function() {
395 this.scrollPort_.scrollRowToBottom(this.getRowCount());
396};
397
398/**
399 * Scroll the terminal one page up (minus one line) relative to the current
400 * position.
401 */
402hterm.Terminal.prototype.scrollPageUp = function() {
403 var i = this.scrollPort_.getTopRowIndex();
404 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
405};
406
407/**
408 * Scroll the terminal one page down (minus one line) relative to the current
409 * position.
410 */
411hterm.Terminal.prototype.scrollPageDown = function() {
412 var i = this.scrollPort_.getTopRowIndex();
413 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800414};
415
rgindac9bc5502012-01-18 11:48:44 -0800416/**
417 * Full terminal reset.
418 */
rginda87b86462011-12-14 13:48:03 -0800419hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800420 this.clearAllTabStops();
421 this.setDefaultTabStops();
422 this.clearColorAndAttributes();
423 this.setVTScrollRegion(null, null);
424 this.clear();
425 this.setAbsoluteCursorPosition(0, 0);
426 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800427};
428
rgindac9bc5502012-01-18 11:48:44 -0800429/**
430 * Soft terminal reset.
431 */
rginda0f5c0292012-01-13 11:00:13 -0800432hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800433 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800434 this.setCursorVisible(true);
435 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800436};
437
rginda87b86462011-12-14 13:48:03 -0800438hterm.Terminal.prototype.clearColorAndAttributes = function() {
439 //console.log('clearColorAndAttributes');
440};
441
442hterm.Terminal.prototype.setForegroundColor256 = function() {
443 console.log('setForegroundColor256');
444};
445
446hterm.Terminal.prototype.setBackgroundColor256 = function() {
447 console.log('setBackgroundColor256');
448};
449
450hterm.Terminal.prototype.setForegroundColor = function() {
451 //console.log('setForegroundColor');
452};
453
454hterm.Terminal.prototype.setBackgroundColor = function() {
455 //console.log('setBackgroundColor');
456};
457
458hterm.Terminal.prototype.setAttributes = function() {
459 //console.log('setAttributes');
460};
461
462hterm.Terminal.prototype.resize = function() {
463 console.log('resize');
464};
465
rgindae4d29232012-01-19 10:47:13 -0800466hterm.Terminal.prototype.setCharacterSet = function() {
467 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800468};
469
rgindac9bc5502012-01-18 11:48:44 -0800470/**
471 * Move the cursor forward to the next tab stop, or to the last column
472 * if no more tab stops are set.
473 */
474hterm.Terminal.prototype.forwardTabStop = function() {
475 var column = this.screen_.cursorPosition.column;
476
477 for (var i = 0; i < this.tabStops_.length; i++) {
478 if (this.tabStops_[i] > column) {
479 this.setCursorColumn(this.tabStops_[i]);
480 return;
481 }
482 }
483
484 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800485};
486
rgindac9bc5502012-01-18 11:48:44 -0800487/**
488 * Move the cursor backward to the previous tab stop, or to the first column
489 * if no previous tab stops are set.
490 */
491hterm.Terminal.prototype.backwardTabStop = function() {
492 var column = this.screen_.cursorPosition.column;
493
494 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
495 if (this.tabStops_[i] < column) {
496 this.setCursorColumn(this.tabStops_[i]);
497 return;
498 }
499 }
500
501 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800502};
503
rgindac9bc5502012-01-18 11:48:44 -0800504/**
505 * Set a tab stop at the given column.
506 *
507 * @param {int} column Zero based column.
508 */
509hterm.Terminal.prototype.setTabStop = function(column) {
510 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
511 if (this.tabStops_[i] == column)
512 return;
513
514 if (this.tabStops_[i] < column) {
515 this.tabStops_.splice(i + 1, 0, column);
516 return;
517 }
518 }
519
520 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800521};
522
rgindac9bc5502012-01-18 11:48:44 -0800523/**
524 * Clear the tab stop at the current cursor position.
525 *
526 * No effect if there is no tab stop at the current cursor position.
527 */
528hterm.Terminal.prototype.clearTabStopAtCursor = function() {
529 var column = this.screen_.cursorPosition.column;
530
531 var i = this.tabStops_.indexOf(column);
532 if (i == -1)
533 return;
534
535 this.tabStops_.splice(i, 1);
536};
537
538/**
539 * Clear all tab stops.
540 */
541hterm.Terminal.prototype.clearAllTabStops = function() {
542 this.tabStops_.length = 0;
543};
544
545/**
546 * Set up the default tab stops, starting from a given column.
547 *
548 * This sets a tabstop every (column % this.tabWidth) column, starting
549 * from the specified column, or 0 if no column is provided.
550 *
551 * This does not clear the existing tab stops first, use clearAllTabStops
552 * for that.
553 *
554 * @param {int} opt_start Optional starting zero based starting column, useful
555 * for filling out missing tab stops when the terminal is resized.
556 */
557hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
558 var start = opt_start || 0;
559 var w = this.tabWidth;
560 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
561 for (var i = 0; i < stopCount; i++) {
562 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
563 }
rginda87b86462011-12-14 13:48:03 -0800564};
565
rginda6d397402012-01-17 10:58:29 -0800566/**
567 * Save cursor position and attributes.
568 *
569 * TODO(rginda): Save attributes once we support them.
570 */
rginda87b86462011-12-14 13:48:03 -0800571hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800572 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800573 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800574};
575
rginda6d397402012-01-17 10:58:29 -0800576/**
577 * Restore cursor position and attributes.
578 *
579 * TODO(rginda): Restore attributes once we support them.
580 */
rginda87b86462011-12-14 13:48:03 -0800581hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800582 if (this.savedOptions_.cursor)
583 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800584 if (this.savedOptions_.textAttributes)
585 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800586};
587
588/**
589 * Interpret a sequence of characters.
590 *
591 * Incomplete escape sequences are buffered until the next call.
592 *
593 * @param {string} str Sequence of characters to interpret or pass through.
594 */
595hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800596 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800597 this.scheduleSyncCursorPosition_();
598};
599
600/**
601 * Take over the given DIV for use as the terminal display.
602 *
603 * @param {HTMLDivElement} div The div to use as the terminal display.
604 */
605hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800606 this.div_ = div;
607
rginda8ba33642011-12-14 12:31:31 -0800608 this.scrollPort_.decorate(div);
rgindaf7521392012-02-28 17:20:34 -0800609
610 this.setFontSize(this.defaultFontSize);
611 this.setFontFamily(this.defaultFontFamily);
rgindaa19afe22012-01-25 15:40:22 -0800612
rginda8ba33642011-12-14 12:31:31 -0800613 this.document_ = this.scrollPort_.getDocument();
614
rginda8ba33642011-12-14 12:31:31 -0800615 this.cursorNode_ = this.document_.createElement('div');
616 this.cursorNode_.style.cssText =
617 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800618 'top: -99px;' +
619 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800620 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
621 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800622 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800623 'background-color: ' + this.cursorColor);
624 this.document_.body.appendChild(this.cursorNode_);
625
626 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800627
rginda87b86462011-12-14 13:48:03 -0800628 var link = this.document_.createElement('link');
629 link.setAttribute('href', '../css/dialogs.css');
630 link.setAttribute('rel', 'stylesheet');
631 this.document_.head.appendChild(link);
632
633 this.alertDialog = new AlertDialog(this.document_.body);
634 this.promptDialog = new PromptDialog(this.document_.body);
635 this.confirmDialog = new ConfirmDialog(this.document_.body);
636
637 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800638 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800639};
640
641hterm.Terminal.prototype.getDocument = function() {
642 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800643};
644
645/**
646 * Return the HTML Element for a given row index.
647 *
648 * This is a method from the RowProvider interface. The ScrollPort uses
649 * it to fetch rows on demand as they are scrolled into view.
650 *
651 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
652 * pairs to conserve memory.
653 *
654 * @param {integer} index The zero-based row index, measured relative to the
655 * start of the scrollback buffer. On-screen rows will always have the
656 * largest indicies.
657 * @return {HTMLElement} The 'x-row' element containing for the requested row.
658 */
659hterm.Terminal.prototype.getRowNode = function(index) {
660 if (index < this.scrollbackRows_.length)
661 return this.scrollbackRows_[index];
662
663 var screenIndex = index - this.scrollbackRows_.length;
664 return this.screen_.rowsArray[screenIndex];
665};
666
667/**
668 * Return the text content for a given range of rows.
669 *
670 * This is a method from the RowProvider interface. The ScrollPort uses
671 * it to fetch text content on demand when the user attempts to copy their
672 * selection to the clipboard.
673 *
674 * @param {integer} start The zero-based row index to start from, measured
675 * relative to the start of the scrollback buffer. On-screen rows will
676 * always have the largest indicies.
677 * @param {integer} end The zero-based row index to end on, measured
678 * relative to the start of the scrollback buffer.
679 * @return {string} A single string containing the text value of the range of
680 * rows. Lines will be newline delimited, with no trailing newline.
681 */
682hterm.Terminal.prototype.getRowsText = function(start, end) {
683 var ary = [];
684 for (var i = start; i < end; i++) {
685 var node = this.getRowNode(i);
686 ary.push(node.textContent);
687 }
688
689 return ary.join('\n');
690};
691
692/**
693 * Return the text content for a given row.
694 *
695 * This is a method from the RowProvider interface. The ScrollPort uses
696 * it to fetch text content on demand when the user attempts to copy their
697 * selection to the clipboard.
698 *
699 * @param {integer} index The zero-based row index to return, measured
700 * relative to the start of the scrollback buffer. On-screen rows will
701 * always have the largest indicies.
702 * @return {string} A string containing the text value of the selected row.
703 */
704hterm.Terminal.prototype.getRowText = function(index) {
705 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800706 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800707};
708
709/**
710 * Return the total number of rows in the addressable screen and in the
711 * scrollback buffer of this terminal.
712 *
713 * This is a method from the RowProvider interface. The ScrollPort uses
714 * it to compute the size of the scrollbar.
715 *
716 * @return {integer} The number of rows in this terminal.
717 */
718hterm.Terminal.prototype.getRowCount = function() {
719 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
720};
721
722/**
723 * Create DOM nodes for new rows and append them to the end of the terminal.
724 *
725 * This is the only correct way to add a new DOM node for a row. Notice that
726 * the new row is appended to the bottom of the list of rows, and does not
727 * require renumbering (of the rowIndex property) of previous rows.
728 *
729 * If you think you want a new blank row somewhere in the middle of the
730 * terminal, look into moveRows_().
731 *
732 * This method does not pay attention to vtScrollTop/Bottom, since you should
733 * be using moveRows() in cases where they would matter.
734 *
735 * The cursor will be positioned at column 0 of the first inserted line.
736 */
737hterm.Terminal.prototype.appendRows_ = function(count) {
738 var cursorRow = this.screen_.rowsArray.length;
739 var offset = this.scrollbackRows_.length + cursorRow;
740 for (var i = 0; i < count; i++) {
741 var row = this.document_.createElement('x-row');
742 row.appendChild(this.document_.createTextNode(''));
743 row.rowIndex = offset + i;
744 this.screen_.pushRow(row);
745 }
746
747 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
748 if (extraRows > 0) {
749 var ary = this.screen_.shiftRows(extraRows);
750 Array.prototype.push.apply(this.scrollbackRows_, ary);
751 this.scheduleScrollDown_();
752 }
753
754 if (cursorRow >= this.screen_.rowsArray.length)
755 cursorRow = this.screen_.rowsArray.length - 1;
756
rginda87b86462011-12-14 13:48:03 -0800757 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800758};
759
760/**
761 * Relocate rows from one part of the addressable screen to another.
762 *
763 * This is used to recycle rows during VT scrolls (those which are driven
764 * by VT commands, rather than by the user manipulating the scrollbar.)
765 *
766 * In this case, the blank lines scrolled into the scroll region are made of
767 * the nodes we scrolled off. These have their rowIndex properties carefully
768 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800769 */
770hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
771 var ary = this.screen_.removeRows(fromIndex, count);
772 this.screen_.insertRows(toIndex, ary);
773
774 var start, end;
775 if (fromIndex < toIndex) {
776 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800777 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800778 } else {
779 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800780 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800781 }
782
783 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800784 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800785};
786
787/**
788 * Renumber the rowIndex property of the given range of rows.
789 *
790 * The start and end indicies are relative to the screen, not the scrollback.
791 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800792 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800793 * no need to renumber scrollback rows.
794 */
795hterm.Terminal.prototype.renumberRows_ = function(start, end) {
796 var offset = this.scrollbackRows_.length;
797 for (var i = start; i < end; i++) {
798 this.screen_.rowsArray[i].rowIndex = offset + i;
799 }
800};
801
802/**
803 * Print a string to the terminal.
804 *
805 * This respects the current insert and wraparound modes. It will add new lines
806 * to the end of the terminal, scrolling off the top into the scrollback buffer
807 * if necessary.
808 *
809 * The string is *not* parsed for escape codes. Use the interpret() method if
810 * that's what you're after.
811 *
812 * @param{string} str The string to print.
813 */
814hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800815 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
816 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800817
rgindaa19afe22012-01-25 15:40:22 -0800818 if (this.options_.insertMode) {
819 this.screen_.insertString(str);
820 } else {
821 this.screen_.overwriteString(str);
822 }
823
824 var overflow = this.screen_.maybeClipCurrentRow();
825
826 if (this.options_.wraparound && overflow) {
827 var lastColumn;
828
829 do {
rginda35c456b2012-02-09 17:29:05 -0800830 this.newLine();
831 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800832
833 if (!this.options_.insertMode)
834 this.screen_.deleteChars(overflow.characterLength);
835
836 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800837
838 overflow = this.screen_.maybeClipCurrentRow();
839 } while (overflow);
840
841 this.setCursorColumn(lastColumn);
842 }
rginda8ba33642011-12-14 12:31:31 -0800843
844 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800845
846 if (this.scrollOnOutput)
847 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800848};
849
850/**
rginda87b86462011-12-14 13:48:03 -0800851 * Set the VT scroll region.
852 *
rginda87b86462011-12-14 13:48:03 -0800853 * This also resets the cursor position to the absolute (0, 0) position, since
854 * that's what xterm appears to do.
855 *
856 * @param {integer} scrollTop The zero-based top of the scroll region.
857 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
858 * inclusive.
859 */
860hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
861 this.vtScrollTop_ = scrollTop;
862 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800863};
864
865/**
rginda8ba33642011-12-14 12:31:31 -0800866 * Return the top row index according to the VT.
867 *
868 * This will return 0 unless the terminal has been told to restrict scrolling
869 * to some lower row. It is used for some VT cursor positioning and scrolling
870 * commands.
871 *
872 * @return {integer} The topmost row in the terminal's scroll region.
873 */
874hterm.Terminal.prototype.getVTScrollTop = function() {
875 if (this.vtScrollTop_ != null)
876 return this.vtScrollTop_;
877
878 return 0;
rginda87b86462011-12-14 13:48:03 -0800879};
rginda8ba33642011-12-14 12:31:31 -0800880
881/**
882 * Return the bottom row index according to the VT.
883 *
884 * This will return the height of the terminal unless the it has been told to
885 * restrict scrolling to some higher row. It is used for some VT cursor
886 * positioning and scrolling commands.
887 *
888 * @return {integer} The bottommost row in the terminal's scroll region.
889 */
890hterm.Terminal.prototype.getVTScrollBottom = function() {
891 if (this.vtScrollBottom_ != null)
892 return this.vtScrollBottom_;
893
rginda87b86462011-12-14 13:48:03 -0800894 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800895}
896
897/**
898 * Process a '\n' character.
899 *
900 * If the cursor is on the final row of the terminal this will append a new
901 * blank row to the screen and scroll the topmost row into the scrollback
902 * buffer.
903 *
904 * Otherwise, this moves the cursor to column zero of the next row.
905 */
906hterm.Terminal.prototype.newLine = function() {
907 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800908 // If we're at the end of the screen we need to append a new line and
909 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800910 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800911 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
912 // End of the scroll region does not affect the scrollback buffer.
913 this.vtScrollUp(1);
914 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800915 } else {
rginda87b86462011-12-14 13:48:03 -0800916 // Anywhere else in the screen just moves the cursor.
917 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800918 }
919};
920
921/**
922 * Like newLine(), except maintain the cursor column.
923 */
924hterm.Terminal.prototype.lineFeed = function() {
925 var column = this.screen_.cursorPosition.column;
926 this.newLine();
927 this.setCursorColumn(column);
928};
929
930/**
rginda87b86462011-12-14 13:48:03 -0800931 * If autoCarriageReturn is set then newLine(), else lineFeed().
932 */
933hterm.Terminal.prototype.formFeed = function() {
934 if (this.options_.autoCarriageReturn) {
935 this.newLine();
936 } else {
937 this.lineFeed();
938 }
939};
940
941/**
942 * Move the cursor up one row, possibly inserting a blank line.
943 *
944 * The cursor column is not changed.
945 */
946hterm.Terminal.prototype.reverseLineFeed = function() {
947 var scrollTop = this.getVTScrollTop();
948 var currentRow = this.screen_.cursorPosition.row;
949
950 if (currentRow == scrollTop) {
951 this.insertLines(1);
952 } else {
953 this.setAbsoluteCursorRow(currentRow - 1);
954 }
955};
956
957/**
rginda8ba33642011-12-14 12:31:31 -0800958 * Replace all characters to the left of the current cursor with the space
959 * character.
960 *
961 * TODO(rginda): This should probably *remove* the characters (not just replace
962 * with a space) if there are no characters at or beyond the current cursor
963 * position. Once it does that, it'll have the same text-attribute related
964 * issues as hterm.Screen.prototype.clearCursorRow :/
965 */
966hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800967 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800968 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800969 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
970 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800971};
972
973/**
974 * Erase a given number of characters to the right of the cursor, shifting
975 * remaining characters to the left.
976 *
977 * The cursor position is unchanged.
978 *
979 * TODO(rginda): Test that this works even when the cursor is positioned beyond
980 * the end of the text.
981 *
982 * TODO(rginda): This likely has text-attribute related troubles similar to the
983 * todo on hterm.Screen.prototype.clearCursorRow.
984 */
985hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800986 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800987
rginda87b86462011-12-14 13:48:03 -0800988 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800989 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
990 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800991 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800992};
993
994/**
995 * Erase the current line.
996 *
997 * The cursor position is unchanged.
998 *
999 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1000 * has a text-attribute related TODO.
1001 */
1002hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001003 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001004 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001005 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001006};
1007
1008/**
1009 * Erase all characters from the start of the scroll region to the current
1010 * cursor position.
1011 *
1012 * The cursor position is unchanged.
1013 *
1014 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1015 * has a text-attribute related TODO.
1016 */
1017hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001018 var cursor = this.saveCursor();
1019
1020 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001021
1022 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001023 for (var i = top; i < cursor.row; i++) {
1024 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001025 this.screen_.clearCursorRow();
1026 }
1027
rginda87b86462011-12-14 13:48:03 -08001028 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001029};
1030
1031/**
1032 * Erase all characters from the current cursor position to the end of the
1033 * scroll region.
1034 *
1035 * The cursor position is unchanged.
1036 *
1037 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1038 * has a text-attribute related TODO.
1039 */
1040hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001041 var cursor = this.saveCursor();
1042
1043 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001044
1045 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001046 for (var i = cursor.row + 1; i <= bottom; i++) {
1047 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001048 this.screen_.clearCursorRow();
1049 }
1050
rginda87b86462011-12-14 13:48:03 -08001051 this.restoreCursor(cursor);
1052};
1053
1054/**
1055 * Fill the terminal with a given character.
1056 *
1057 * This methods does not respect the VT scroll region.
1058 *
1059 * @param {string} ch The character to use for the fill.
1060 */
1061hterm.Terminal.prototype.fill = function(ch) {
1062 var cursor = this.saveCursor();
1063
1064 this.setAbsoluteCursorPosition(0, 0);
1065 for (var row = 0; row < this.screenSize.height; row++) {
1066 for (var col = 0; col < this.screenSize.width; col++) {
1067 this.setAbsoluteCursorPosition(row, col);
1068 this.screen_.overwriteString(ch);
1069 }
1070 }
1071
1072 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001073};
1074
1075/**
rgindae4d29232012-01-19 10:47:13 -08001076 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -08001077 *
rgindae4d29232012-01-19 10:47:13 -08001078 * The cursor position is unchanged. This does not respect the scroll
1079 * region.
rginda8ba33642011-12-14 12:31:31 -08001080 *
1081 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1082 * has a text-attribute related TODO.
1083 */
1084hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -08001085 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001086
rgindae4d29232012-01-19 10:47:13 -08001087 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001088
rgindae4d29232012-01-19 10:47:13 -08001089 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08001090 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001091 this.screen_.clearCursorRow();
1092 }
1093
rginda87b86462011-12-14 13:48:03 -08001094 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001095};
1096
1097/**
1098 * VT command to insert lines at the current cursor row.
1099 *
1100 * This respects the current scroll region. Rows pushed off the bottom are
1101 * lost (they won't show up in the scrollback buffer).
1102 *
1103 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1104 * has a text-attribute related TODO.
1105 *
1106 * @param {integer} count The number of lines to insert.
1107 */
1108hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001109 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001110
1111 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001112 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001113
rgindae4d29232012-01-19 10:47:13 -08001114 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001115 if (start != cursor.row)
1116 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001117
1118 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001119 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001120 this.screen_.clearCursorRow();
1121 }
1122
rginda87b86462011-12-14 13:48:03 -08001123 cursor.column = 0;
1124 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001125};
1126
1127/**
1128 * VT command to delete lines at the current cursor row.
1129 *
1130 * New rows are added to the bottom of scroll region to take their place. New
1131 * rows are strictly there to take up space and have no content or style.
1132 */
1133hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001134 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001135
rginda87b86462011-12-14 13:48:03 -08001136 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001137 var bottom = this.getVTScrollBottom();
1138
rginda87b86462011-12-14 13:48:03 -08001139 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001140 count = Math.min(count, maxCount);
1141
rginda87b86462011-12-14 13:48:03 -08001142 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001143 if (count != maxCount)
1144 this.moveRows_(top, count, moveStart);
1145
1146 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001147 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001148 this.screen_.clearCursorRow();
1149 }
1150
rginda87b86462011-12-14 13:48:03 -08001151 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001152};
1153
1154/**
1155 * Inserts the given number of spaces at the current cursor position.
1156 *
rginda87b86462011-12-14 13:48:03 -08001157 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001158 */
1159hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001160 var cursor = this.saveCursor();
1161
rginda0f5c0292012-01-13 11:00:13 -08001162 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001163 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001164 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001165
1166 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001167};
1168
1169/**
1170 * Forward-delete the specified number of characters starting at the cursor
1171 * position.
1172 *
1173 * @param {integer} count The number of characters to delete.
1174 */
1175hterm.Terminal.prototype.deleteChars = function(count) {
1176 this.screen_.deleteChars(count);
1177};
1178
1179/**
1180 * Shift rows in the scroll region upwards by a given number of lines.
1181 *
1182 * New rows are inserted at the bottom of the scroll region to fill the
1183 * vacated rows. The new rows not filled out with the current text attributes.
1184 *
1185 * This function does not affect the scrollback rows at all. Rows shifted
1186 * off the top are lost.
1187 *
rginda87b86462011-12-14 13:48:03 -08001188 * The cursor position is not altered.
1189 *
rginda8ba33642011-12-14 12:31:31 -08001190 * @param {integer} count The number of rows to scroll.
1191 */
1192hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001193 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001194
rginda87b86462011-12-14 13:48:03 -08001195 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001196 this.deleteLines(count);
1197
rginda87b86462011-12-14 13:48:03 -08001198 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001199};
1200
1201/**
1202 * Shift rows below the cursor down by a given number of lines.
1203 *
1204 * This function respects the current scroll region.
1205 *
1206 * New rows are inserted at the top of the scroll region to fill the
1207 * vacated rows. The new rows not filled out with the current text attributes.
1208 *
1209 * This function does not affect the scrollback rows at all. Rows shifted
1210 * off the bottom are lost.
1211 *
1212 * @param {integer} count The number of rows to scroll.
1213 */
1214hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001215 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001216
rginda87b86462011-12-14 13:48:03 -08001217 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001218 this.insertLines(opt_count);
1219
rginda87b86462011-12-14 13:48:03 -08001220 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001221};
1222
rginda87b86462011-12-14 13:48:03 -08001223
rginda8ba33642011-12-14 12:31:31 -08001224/**
1225 * Set the cursor position.
1226 *
1227 * The cursor row is relative to the scroll region if the terminal has
1228 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1229 *
1230 * @param {integer} row The new zero-based cursor row.
1231 * @param {integer} row The new zero-based cursor column.
1232 */
1233hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1234 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001235 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001236 } else {
rginda87b86462011-12-14 13:48:03 -08001237 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001238 }
rginda87b86462011-12-14 13:48:03 -08001239};
rginda8ba33642011-12-14 12:31:31 -08001240
rginda87b86462011-12-14 13:48:03 -08001241hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1242 var scrollTop = this.getVTScrollTop();
1243 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001244 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001245 this.screen_.setCursorPosition(row, column);
1246};
1247
1248hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001249 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1250 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001251 this.screen_.setCursorPosition(row, column);
1252};
1253
1254/**
1255 * Set the cursor column.
1256 *
1257 * @param {integer} column The new zero-based cursor column.
1258 */
1259hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001260 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001261};
1262
1263/**
1264 * Return the cursor column.
1265 *
1266 * @return {integer} The zero-based cursor column.
1267 */
1268hterm.Terminal.prototype.getCursorColumn = function() {
1269 return this.screen_.cursorPosition.column;
1270};
1271
1272/**
1273 * Set the cursor row.
1274 *
1275 * The cursor row is relative to the scroll region if the terminal has
1276 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1277 *
1278 * @param {integer} row The new cursor row.
1279 */
rginda87b86462011-12-14 13:48:03 -08001280hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1281 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001282};
1283
1284/**
1285 * Return the cursor row.
1286 *
1287 * @return {integer} The zero-based cursor row.
1288 */
1289hterm.Terminal.prototype.getCursorRow = function(row) {
1290 return this.screen_.cursorPosition.row;
1291};
1292
1293/**
1294 * Request that the ScrollPort redraw itself soon.
1295 *
1296 * The redraw will happen asynchronously, soon after the call stack winds down.
1297 * Multiple calls will be coalesced into a single redraw.
1298 */
1299hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001300 if (this.timeouts_.redraw)
1301 return;
rginda8ba33642011-12-14 12:31:31 -08001302
1303 var self = this;
rginda87b86462011-12-14 13:48:03 -08001304 this.timeouts_.redraw = setTimeout(function() {
1305 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001306 self.scrollPort_.redraw_();
1307 }, 0);
1308};
1309
1310/**
1311 * Request that the ScrollPort be scrolled to the bottom.
1312 *
1313 * The scroll will happen asynchronously, soon after the call stack winds down.
1314 * Multiple calls will be coalesced into a single scroll.
1315 *
1316 * This affects the scrollbar position of the ScrollPort, and has nothing to
1317 * do with the VT scroll commands.
1318 */
1319hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1320 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001321 return;
rginda8ba33642011-12-14 12:31:31 -08001322
1323 var self = this;
1324 this.timeouts_.scrollDown = setTimeout(function() {
1325 delete self.timeouts_.scrollDown;
1326 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1327 }, 10);
1328};
1329
1330/**
1331 * Move the cursor up a specified number of rows.
1332 *
1333 * @param {integer} count The number of rows to move the cursor.
1334 */
1335hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001336 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001337};
1338
1339/**
1340 * Move the cursor down a specified number of rows.
1341 *
1342 * @param {integer} count The number of rows to move the cursor.
1343 */
1344hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001345 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001346 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1347 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1348 this.screenSize.height - 1);
1349
1350 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1351 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001352 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001353};
1354
1355/**
1356 * Move the cursor left a specified number of columns.
1357 *
1358 * @param {integer} count The number of columns to move the cursor.
1359 */
1360hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001361 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001362};
1363
1364/**
1365 * Move the cursor right a specified number of columns.
1366 *
1367 * @param {integer} count The number of columns to move the cursor.
1368 */
1369hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001370 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001371 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001372 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001373 this.setCursorColumn(column);
1374};
1375
1376/**
1377 * Reverse the foreground and background colors of the terminal.
1378 *
1379 * This only affects text that was drawn with no attributes.
1380 *
1381 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1382 * been drawn with attributes that happen to coincide with the default
1383 * 'no-attribute' colors. My guess is probably not.
1384 */
1385hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001386 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001387 if (state) {
1388 this.scrollPort_.setForegroundColor(this.backgroundColor);
1389 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1390 } else {
1391 this.scrollPort_.setForegroundColor(this.foregroundColor);
1392 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1393 }
1394};
1395
1396/**
rginda87b86462011-12-14 13:48:03 -08001397 * Ring the terminal bell.
1398 *
1399 * We only have a visual bell, which quickly toggles inverse video in the
1400 * terminal.
1401 */
1402hterm.Terminal.prototype.ringBell = function() {
rgindaf0090c92012-02-10 14:58:52 -08001403 this.bellAudio_.play();
1404
rginda6d397402012-01-17 10:58:29 -08001405 this.cursorNode_.style.backgroundColor =
1406 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001407
1408 var self = this;
1409 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001410 self.cursorNode_.style.backgroundColor = self.cursorColor;
1411 }, 200);
rginda87b86462011-12-14 13:48:03 -08001412};
1413
1414/**
rginda8ba33642011-12-14 12:31:31 -08001415 * Set the origin mode bit.
1416 *
1417 * If origin mode is on, certain VT cursor and scrolling commands measure their
1418 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1419 * to the top of the addressable screen.
1420 *
1421 * Defaults to off.
1422 *
1423 * @param {boolean} state True to set origin mode, false to unset.
1424 */
1425hterm.Terminal.prototype.setOriginMode = function(state) {
1426 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001427 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001428};
1429
1430/**
1431 * Set the insert mode bit.
1432 *
1433 * If insert mode is on, existing text beyond the cursor position will be
1434 * shifted right to make room for new text. Otherwise, new text overwrites
1435 * any existing text.
1436 *
1437 * Defaults to off.
1438 *
1439 * @param {boolean} state True to set insert mode, false to unset.
1440 */
1441hterm.Terminal.prototype.setInsertMode = function(state) {
1442 this.options_.insertMode = state;
1443};
1444
1445/**
rginda87b86462011-12-14 13:48:03 -08001446 * Set the auto carriage return bit.
1447 *
1448 * If auto carriage return is on then a formfeed character is interpreted
1449 * as a newline, otherwise it's the same as a linefeed. The difference boils
1450 * down to whether or not the cursor column is reset.
1451 */
1452hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1453 this.options_.autoCarriageReturn = state;
1454};
1455
1456/**
rginda8ba33642011-12-14 12:31:31 -08001457 * Set the wraparound mode bit.
1458 *
1459 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1460 * to the start of the following row. Otherwise, the cursor is clamped to the
1461 * end of the screen and attempts to write past it are ignored.
1462 *
1463 * Defaults to on.
1464 *
1465 * @param {boolean} state True to set wraparound mode, false to unset.
1466 */
1467hterm.Terminal.prototype.setWraparound = function(state) {
1468 this.options_.wraparound = state;
1469};
1470
1471/**
1472 * Set the reverse-wraparound mode bit.
1473 *
1474 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1475 * to the end of the previous row. Otherwise, the cursor is clamped to column
1476 * 0.
1477 *
1478 * Defaults to off.
1479 *
1480 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1481 */
1482hterm.Terminal.prototype.setReverseWraparound = function(state) {
1483 this.options_.reverseWraparound = state;
1484};
1485
1486/**
1487 * Selects between the primary and alternate screens.
1488 *
1489 * If alternate mode is on, the alternate screen is active. Otherwise the
1490 * primary screen is active.
1491 *
1492 * Swapping screens has no effect on the scrollback buffer.
1493 *
1494 * Each screen maintains its own cursor position.
1495 *
1496 * Defaults to off.
1497 *
1498 * @param {boolean} state True to set alternate mode, false to unset.
1499 */
1500hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001501 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001502 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1503
rginda35c456b2012-02-09 17:29:05 -08001504 if (this.screen_.rowsArray.length &&
1505 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1506 // If the screen changed sizes while we were away, our rowIndexes may
1507 // be incorrect.
1508 var offset = this.scrollbackRows_.length;
1509 var ary = this.screen_.rowsArray;
1510 for (i = 0; i < ary.length; i++) {
1511 ary[i].rowIndex = offset + i;
1512 }
1513 }
rginda8ba33642011-12-14 12:31:31 -08001514
rginda35c456b2012-02-09 17:29:05 -08001515 this.realizeWidth_(this.screenSize.width);
1516 this.realizeHeight_(this.screenSize.height);
1517 this.scrollPort_.syncScrollHeight();
1518 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001519
rginda6d397402012-01-17 10:58:29 -08001520 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001521 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001522};
1523
1524/**
1525 * Set the cursor-blink mode bit.
1526 *
1527 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1528 * a visible cursor does not blink.
1529 *
1530 * You should make sure to turn blinking off if you're going to dispose of a
1531 * terminal, otherwise you'll leak a timeout.
1532 *
1533 * Defaults to on.
1534 *
1535 * @param {boolean} state True to set cursor-blink mode, false to unset.
1536 */
1537hterm.Terminal.prototype.setCursorBlink = function(state) {
1538 this.options_.cursorBlink = state;
1539
1540 if (!state && this.timeouts_.cursorBlink) {
1541 clearTimeout(this.timeouts_.cursorBlink);
1542 delete this.timeouts_.cursorBlink;
1543 }
1544
1545 if (this.options_.cursorVisible)
1546 this.setCursorVisible(true);
1547};
1548
1549/**
1550 * Set the cursor-visible mode bit.
1551 *
1552 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1553 *
1554 * Defaults to on.
1555 *
1556 * @param {boolean} state True to set cursor-visible mode, false to unset.
1557 */
1558hterm.Terminal.prototype.setCursorVisible = function(state) {
1559 this.options_.cursorVisible = state;
1560
1561 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001562 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001563 return;
1564 }
1565
rginda87b86462011-12-14 13:48:03 -08001566 this.syncCursorPosition_();
1567
1568 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001569
1570 if (this.options_.cursorBlink) {
1571 if (this.timeouts_.cursorBlink)
1572 return;
1573
1574 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1575 500);
1576 } else {
1577 if (this.timeouts_.cursorBlink) {
1578 clearTimeout(this.timeouts_.cursorBlink);
1579 delete this.timeouts_.cursorBlink;
1580 }
1581 }
1582};
1583
1584/**
rginda87b86462011-12-14 13:48:03 -08001585 * Synchronizes the visible cursor and document selection with the current
1586 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001587 */
1588hterm.Terminal.prototype.syncCursorPosition_ = function() {
1589 var topRowIndex = this.scrollPort_.getTopRowIndex();
1590 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1591 var cursorRowIndex = this.scrollbackRows_.length +
1592 this.screen_.cursorPosition.row;
1593
1594 if (cursorRowIndex > bottomRowIndex) {
1595 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001596 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001597 return;
1598 }
1599
rginda35c456b2012-02-09 17:29:05 -08001600 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1601 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1602
rginda8ba33642011-12-14 12:31:31 -08001603 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001604 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1605 'px';
1606 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1607 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001608
1609 this.cursorNode_.setAttribute('title',
1610 '(' + this.screen_.cursorPosition.row +
1611 ', ' + this.screen_.cursorPosition.column +
1612 ')');
1613
1614 // Update the caret for a11y purposes.
1615 var selection = this.document_.getSelection();
1616 if (selection && selection.isCollapsed)
1617 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001618};
1619
1620/**
1621 * Synchronizes the visible cursor with the current cursor coordinates.
1622 *
1623 * The sync will happen asynchronously, soon after the call stack winds down.
1624 * Multiple calls will be coalesced into a single sync.
1625 */
1626hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1627 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001628 return;
rginda8ba33642011-12-14 12:31:31 -08001629
1630 var self = this;
1631 this.timeouts_.syncCursor = setTimeout(function() {
1632 self.syncCursorPosition_();
1633 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001634 }, 0);
1635};
1636
rgindacc2996c2012-02-24 14:59:31 -08001637/**
1638 * Show the terminal overlay for a given amount of time.
1639 *
1640 * The terminal overlay appears in inverse video in a large font, centered
1641 * over the terminal. You should probably keep the overlay message brief,
1642 * since it's in a large font and you probably aren't going to check the size
1643 * of the terminal first.
1644 *
1645 * @param {string} msg The text (not HTML) message to display in the overlay.
1646 * @param {number} opt_timeout The amount of time to wait before fading out
1647 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1648 * stay up forever (or until the next overlay).
1649 */
1650hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001651 if (!this.overlayNode_) {
1652 if (!this.div_)
1653 return;
1654
1655 this.overlayNode_ = this.document_.createElement('div');
1656 this.overlayNode_.style.cssText = (
1657 'background-color: ' + this.foregroundColor + ';' +
1658 'border-radius: 15px;' +
1659 'color: ' + this.backgroundColor + ';' +
1660 'font-family: ' + this.defaultFontFamily + ';' +
1661 'font-size: xx-large;' +
1662 'opacity: 0.75;' +
1663 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1664 'position: absolute;' +
1665 '-webkit-user-select: none;' +
1666 '-webkit-transition: opacity 180ms ease-in;');
1667 }
1668
1669 this.overlayNode_.textContent = msg;
1670 this.overlayNode_.style.opacity = '0.75';
1671
1672 if (!this.overlayNode_.parentNode)
1673 this.div_.appendChild(this.overlayNode_);
1674
1675 this.overlayNode_.style.top = (
1676 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1677 this.overlayNode_.style.left = (
1678 this.div_.clientWidth - this.overlayNode_.clientWidth -
1679 this.scrollbarWidthPx) / 2;
1680
1681 var self = this;
1682
1683 if (this.overlayTimeout_)
1684 clearTimeout(this.overlayTimeout_);
1685
rgindacc2996c2012-02-24 14:59:31 -08001686 if (opt_timeout === null)
1687 return;
1688
rgindaf0090c92012-02-10 14:58:52 -08001689 this.overlayTimeout_ = setTimeout(function() {
1690 self.overlayNode_.style.opacity = '0';
1691 setTimeout(function() {
1692 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
1693 self.overlayTimeout_ = null;
1694 self.overlayNode_.style.opacity = '0.75';
1695 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001696 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001697};
1698
1699hterm.Terminal.prototype.overlaySize = function() {
1700 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1701};
1702
rginda87b86462011-12-14 13:48:03 -08001703/**
1704 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1705 *
1706 * @param {string} string The VT string representing the keystroke.
1707 */
1708hterm.Terminal.prototype.onVTKeystroke = function(string) {
1709 if (this.scrollOnKeystroke)
1710 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1711
1712 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001713};
1714
1715/**
1716 * React when the ScrollPort is scrolled.
1717 */
1718hterm.Terminal.prototype.onScroll_ = function() {
1719 this.scheduleSyncCursorPosition_();
1720};
1721
1722/**
rginda9846e2f2012-01-27 13:53:33 -08001723 * React when text is pasted into the scrollPort.
1724 */
1725hterm.Terminal.prototype.onPaste_ = function(e) {
1726 this.io.onVTKeystroke(e.text);
1727};
1728
1729/**
rginda8ba33642011-12-14 12:31:31 -08001730 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001731 *
1732 * Note: This function should not directly contain code that alters the internal
1733 * state of the terminal. That kind of code belongs in realizeWidth or
1734 * realizeHeight, so that it can be executed synchronously in the case of a
1735 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001736 */
1737hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001738 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001739 this.scrollPort_.characterSize.width);
1740 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1741 this.scrollPort_.characterSize.height);
1742
1743 if (!(columnCount || rowCount)) {
1744 // We avoid these situations since they happen sometimes when the terminal
1745 // gets removed from the document, and we can't deal with that.
1746 return;
1747 }
1748
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001749 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001750 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001751 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001752};
1753
1754/**
1755 * Service the cursor blink timeout.
1756 */
1757hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001758 if (this.cursorNode_.style.opacity == '0') {
1759 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001760 } else {
rginda87b86462011-12-14 13:48:03 -08001761 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001762 }
1763};