blob: 5ebcbeaed3135273d94f91544205261a7ee2cf72 [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, ' +
98 '"Andale Mono", "Lucida Console", monospace';
99
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);
203};
204
205/**
rginda87b86462011-12-14 13:48:03 -0800206 * Return a copy of the current cursor position.
207 *
208 * @return {hterm.RowCol} The RowCol object representing the current position.
209 */
210hterm.Terminal.prototype.saveCursor = function() {
211 return this.screen_.cursorPosition.clone();
212};
213
rgindaa19afe22012-01-25 15:40:22 -0800214hterm.Terminal.prototype.getTextAttributes = function() {
215 return this.screen_.textAttributes;
216};
217
rginda87b86462011-12-14 13:48:03 -0800218/**
rginda9846e2f2012-01-27 13:53:33 -0800219 * Change the title of this terminal's window.
220 */
221hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800222 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800223};
224
225/**
rginda87b86462011-12-14 13:48:03 -0800226 * Restore a previously saved cursor position.
227 *
228 * @param {hterm.RowCol} cursor The position to restore.
229 */
230hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800231 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
232 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
233 this.screen_.setCursorPosition(row, column);
234 if (cursor.column > column ||
235 cursor.column == column && cursor.overflow) {
236 this.screen_.cursorPosition.overflow = true;
237 }
rginda87b86462011-12-14 13:48:03 -0800238};
239
240/**
241 * Set the width of the terminal, resizing the UI to match.
242 */
243hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800244 if (columnCount == null) {
245 this.div_.style.width = '100%';
246 return;
247 }
248
rginda35c456b2012-02-09 17:29:05 -0800249 this.div_.style.width = this.scrollPort_.characterSize.width *
250 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400251 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800252 this.scheduleSyncCursorPosition_();
253};
rginda87b86462011-12-14 13:48:03 -0800254
rgindac9bc5502012-01-18 11:48:44 -0800255/**
rginda35c456b2012-02-09 17:29:05 -0800256 * Set the height of the terminal, resizing the UI to match.
257 */
258hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800259 if (rowCount == null) {
260 this.div_.style.height = '100%';
261 return;
262 }
263
rginda35c456b2012-02-09 17:29:05 -0800264 this.div_.style.height =
265 this.scrollPort_.characterSize.height * rowCount + 'px';
266 this.realizeSize_(this.screenSize.width, rowCount);
267 this.scheduleSyncCursorPosition_();
268};
269
270/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400271 * Deal with terminal size changes.
272 *
273 */
274hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
275 if (columnCount != this.screenSize.width)
276 this.realizeWidth_(columnCount);
277
278 if (rowCount != this.screenSize.height)
279 this.realizeHeight_(rowCount);
280
281 // Send new terminal size to plugin.
282 this.io.onTerminalResize(columnCount, rowCount);
283};
284
285/**
rgindac9bc5502012-01-18 11:48:44 -0800286 * Deal with terminal width changes.
287 *
288 * This function does what needs to be done when the terminal width changes
289 * out from under us. It happens here rather than in onResize_() because this
290 * code may need to run synchronously to handle programmatic changes of
291 * terminal width.
292 *
293 * Relying on the browser to send us an async resize event means we may not be
294 * in the correct state yet when the next escape sequence hits.
295 */
296hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
297 var deltaColumns = columnCount - this.screen_.getWidth();
298
rginda87b86462011-12-14 13:48:03 -0800299 this.screenSize.width = columnCount;
300 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800301
302 if (deltaColumns > 0) {
303 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
304 } else {
305 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
306 if (this.tabStops_[i] <= columnCount)
307 break;
308
309 this.tabStops_.pop();
310 }
311 }
312
313 this.screen_.setColumnCount(this.screenSize.width);
314};
315
316/**
317 * Deal with terminal height changes.
318 *
319 * This function does what needs to be done when the terminal height changes
320 * out from under us. It happens here rather than in onResize_() because this
321 * code may need to run synchronously to handle programmatic changes of
322 * terminal height.
323 *
324 * Relying on the browser to send us an async resize event means we may not be
325 * in the correct state yet when the next escape sequence hits.
326 */
327hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
328 var deltaRows = rowCount - this.screen_.getHeight();
329
330 this.screenSize.height = rowCount;
331
332 var cursor = this.saveCursor();
333
334 if (deltaRows < 0) {
335 // Screen got smaller.
336 deltaRows *= -1;
337 while (deltaRows) {
338 var lastRow = this.getRowCount() - 1;
339 if (lastRow - this.scrollbackRows_.length == cursor.row)
340 break;
341
342 if (this.getRowText(lastRow))
343 break;
344
345 this.screen_.popRow();
346 deltaRows--;
347 }
348
349 var ary = this.screen_.shiftRows(deltaRows);
350 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
351
352 // We just removed rows from the top of the screen, we need to update
353 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800354 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800355 } else if (deltaRows > 0) {
356 // Screen got larger.
357
358 if (deltaRows <= this.scrollbackRows_.length) {
359 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
360 var rows = this.scrollbackRows_.splice(
361 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
362 this.screen_.unshiftRows(rows);
363 deltaRows -= scrollbackCount;
364 cursor.row += scrollbackCount;
365 }
366
367 if (deltaRows)
368 this.appendRows_(deltaRows);
369 }
370
rginda35c456b2012-02-09 17:29:05 -0800371 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800372 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800373};
374
375/**
376 * Scroll the terminal to the top of the scrollback buffer.
377 */
378hterm.Terminal.prototype.scrollHome = function() {
379 this.scrollPort_.scrollRowToTop(0);
380};
381
382/**
383 * Scroll the terminal to the end.
384 */
385hterm.Terminal.prototype.scrollEnd = function() {
386 this.scrollPort_.scrollRowToBottom(this.getRowCount());
387};
388
389/**
390 * Scroll the terminal one page up (minus one line) relative to the current
391 * position.
392 */
393hterm.Terminal.prototype.scrollPageUp = function() {
394 var i = this.scrollPort_.getTopRowIndex();
395 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
396};
397
398/**
399 * Scroll the terminal one page down (minus one line) relative to the current
400 * position.
401 */
402hterm.Terminal.prototype.scrollPageDown = function() {
403 var i = this.scrollPort_.getTopRowIndex();
404 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800405};
406
rgindac9bc5502012-01-18 11:48:44 -0800407/**
408 * Full terminal reset.
409 */
rginda87b86462011-12-14 13:48:03 -0800410hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800411 this.clearAllTabStops();
412 this.setDefaultTabStops();
413 this.clearColorAndAttributes();
414 this.setVTScrollRegion(null, null);
415 this.clear();
416 this.setAbsoluteCursorPosition(0, 0);
417 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800418};
419
rgindac9bc5502012-01-18 11:48:44 -0800420/**
421 * Soft terminal reset.
422 */
rginda0f5c0292012-01-13 11:00:13 -0800423hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800424 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800425 this.setCursorVisible(true);
426 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800427};
428
rginda87b86462011-12-14 13:48:03 -0800429hterm.Terminal.prototype.clearColorAndAttributes = function() {
430 //console.log('clearColorAndAttributes');
431};
432
433hterm.Terminal.prototype.setForegroundColor256 = function() {
434 console.log('setForegroundColor256');
435};
436
437hterm.Terminal.prototype.setBackgroundColor256 = function() {
438 console.log('setBackgroundColor256');
439};
440
441hterm.Terminal.prototype.setForegroundColor = function() {
442 //console.log('setForegroundColor');
443};
444
445hterm.Terminal.prototype.setBackgroundColor = function() {
446 //console.log('setBackgroundColor');
447};
448
449hterm.Terminal.prototype.setAttributes = function() {
450 //console.log('setAttributes');
451};
452
453hterm.Terminal.prototype.resize = function() {
454 console.log('resize');
455};
456
rgindae4d29232012-01-19 10:47:13 -0800457hterm.Terminal.prototype.setCharacterSet = function() {
458 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800459};
460
rgindac9bc5502012-01-18 11:48:44 -0800461/**
462 * Move the cursor forward to the next tab stop, or to the last column
463 * if no more tab stops are set.
464 */
465hterm.Terminal.prototype.forwardTabStop = function() {
466 var column = this.screen_.cursorPosition.column;
467
468 for (var i = 0; i < this.tabStops_.length; i++) {
469 if (this.tabStops_[i] > column) {
470 this.setCursorColumn(this.tabStops_[i]);
471 return;
472 }
473 }
474
475 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800476};
477
rgindac9bc5502012-01-18 11:48:44 -0800478/**
479 * Move the cursor backward to the previous tab stop, or to the first column
480 * if no previous tab stops are set.
481 */
482hterm.Terminal.prototype.backwardTabStop = function() {
483 var column = this.screen_.cursorPosition.column;
484
485 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
486 if (this.tabStops_[i] < column) {
487 this.setCursorColumn(this.tabStops_[i]);
488 return;
489 }
490 }
491
492 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800493};
494
rgindac9bc5502012-01-18 11:48:44 -0800495/**
496 * Set a tab stop at the given column.
497 *
498 * @param {int} column Zero based column.
499 */
500hterm.Terminal.prototype.setTabStop = function(column) {
501 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
502 if (this.tabStops_[i] == column)
503 return;
504
505 if (this.tabStops_[i] < column) {
506 this.tabStops_.splice(i + 1, 0, column);
507 return;
508 }
509 }
510
511 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800512};
513
rgindac9bc5502012-01-18 11:48:44 -0800514/**
515 * Clear the tab stop at the current cursor position.
516 *
517 * No effect if there is no tab stop at the current cursor position.
518 */
519hterm.Terminal.prototype.clearTabStopAtCursor = function() {
520 var column = this.screen_.cursorPosition.column;
521
522 var i = this.tabStops_.indexOf(column);
523 if (i == -1)
524 return;
525
526 this.tabStops_.splice(i, 1);
527};
528
529/**
530 * Clear all tab stops.
531 */
532hterm.Terminal.prototype.clearAllTabStops = function() {
533 this.tabStops_.length = 0;
534};
535
536/**
537 * Set up the default tab stops, starting from a given column.
538 *
539 * This sets a tabstop every (column % this.tabWidth) column, starting
540 * from the specified column, or 0 if no column is provided.
541 *
542 * This does not clear the existing tab stops first, use clearAllTabStops
543 * for that.
544 *
545 * @param {int} opt_start Optional starting zero based starting column, useful
546 * for filling out missing tab stops when the terminal is resized.
547 */
548hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
549 var start = opt_start || 0;
550 var w = this.tabWidth;
551 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
552 for (var i = 0; i < stopCount; i++) {
553 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
554 }
rginda87b86462011-12-14 13:48:03 -0800555};
556
rginda6d397402012-01-17 10:58:29 -0800557/**
558 * Save cursor position and attributes.
559 *
560 * TODO(rginda): Save attributes once we support them.
561 */
rginda87b86462011-12-14 13:48:03 -0800562hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800563 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800564 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800565};
566
rginda6d397402012-01-17 10:58:29 -0800567/**
568 * Restore cursor position and attributes.
569 *
570 * TODO(rginda): Restore attributes once we support them.
571 */
rginda87b86462011-12-14 13:48:03 -0800572hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800573 if (this.savedOptions_.cursor)
574 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800575 if (this.savedOptions_.textAttributes)
576 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800577};
578
579/**
580 * Interpret a sequence of characters.
581 *
582 * Incomplete escape sequences are buffered until the next call.
583 *
584 * @param {string} str Sequence of characters to interpret or pass through.
585 */
586hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800587 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800588 this.scheduleSyncCursorPosition_();
589};
590
591/**
592 * Take over the given DIV for use as the terminal display.
593 *
594 * @param {HTMLDivElement} div The div to use as the terminal display.
595 */
596hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800597 this.div_ = div;
598
rginda8ba33642011-12-14 12:31:31 -0800599 this.scrollPort_.decorate(div);
rginda35c456b2012-02-09 17:29:05 -0800600 this.scrollPort_.setFontFamily(this.defaultFontFamily);
601 this.scrollPort_.setFontSize(this.defaultFontSize);
rgindaa19afe22012-01-25 15:40:22 -0800602
rginda8ba33642011-12-14 12:31:31 -0800603 this.document_ = this.scrollPort_.getDocument();
604
rginda8ba33642011-12-14 12:31:31 -0800605 this.cursorNode_ = this.document_.createElement('div');
606 this.cursorNode_.style.cssText =
607 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800608 'top: -99px;' +
609 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800610 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
611 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800612 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800613 'background-color: ' + this.cursorColor);
614 this.document_.body.appendChild(this.cursorNode_);
615
616 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800617
rginda87b86462011-12-14 13:48:03 -0800618 var link = this.document_.createElement('link');
619 link.setAttribute('href', '../css/dialogs.css');
620 link.setAttribute('rel', 'stylesheet');
621 this.document_.head.appendChild(link);
622
623 this.alertDialog = new AlertDialog(this.document_.body);
624 this.promptDialog = new PromptDialog(this.document_.body);
625 this.confirmDialog = new ConfirmDialog(this.document_.body);
626
627 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800628 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800629};
630
631hterm.Terminal.prototype.getDocument = function() {
632 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800633};
634
635/**
636 * Return the HTML Element for a given row index.
637 *
638 * This is a method from the RowProvider interface. The ScrollPort uses
639 * it to fetch rows on demand as they are scrolled into view.
640 *
641 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
642 * pairs to conserve memory.
643 *
644 * @param {integer} index The zero-based row index, measured relative to the
645 * start of the scrollback buffer. On-screen rows will always have the
646 * largest indicies.
647 * @return {HTMLElement} The 'x-row' element containing for the requested row.
648 */
649hterm.Terminal.prototype.getRowNode = function(index) {
650 if (index < this.scrollbackRows_.length)
651 return this.scrollbackRows_[index];
652
653 var screenIndex = index - this.scrollbackRows_.length;
654 return this.screen_.rowsArray[screenIndex];
655};
656
657/**
658 * Return the text content for a given range of rows.
659 *
660 * This is a method from the RowProvider interface. The ScrollPort uses
661 * it to fetch text content on demand when the user attempts to copy their
662 * selection to the clipboard.
663 *
664 * @param {integer} start The zero-based row index to start from, measured
665 * relative to the start of the scrollback buffer. On-screen rows will
666 * always have the largest indicies.
667 * @param {integer} end The zero-based row index to end on, measured
668 * relative to the start of the scrollback buffer.
669 * @return {string} A single string containing the text value of the range of
670 * rows. Lines will be newline delimited, with no trailing newline.
671 */
672hterm.Terminal.prototype.getRowsText = function(start, end) {
673 var ary = [];
674 for (var i = start; i < end; i++) {
675 var node = this.getRowNode(i);
676 ary.push(node.textContent);
677 }
678
679 return ary.join('\n');
680};
681
682/**
683 * Return the text content for a given row.
684 *
685 * This is a method from the RowProvider interface. The ScrollPort uses
686 * it to fetch text content on demand when the user attempts to copy their
687 * selection to the clipboard.
688 *
689 * @param {integer} index The zero-based row index to return, measured
690 * relative to the start of the scrollback buffer. On-screen rows will
691 * always have the largest indicies.
692 * @return {string} A string containing the text value of the selected row.
693 */
694hterm.Terminal.prototype.getRowText = function(index) {
695 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800696 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800697};
698
699/**
700 * Return the total number of rows in the addressable screen and in the
701 * scrollback buffer of this terminal.
702 *
703 * This is a method from the RowProvider interface. The ScrollPort uses
704 * it to compute the size of the scrollbar.
705 *
706 * @return {integer} The number of rows in this terminal.
707 */
708hterm.Terminal.prototype.getRowCount = function() {
709 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
710};
711
712/**
713 * Create DOM nodes for new rows and append them to the end of the terminal.
714 *
715 * This is the only correct way to add a new DOM node for a row. Notice that
716 * the new row is appended to the bottom of the list of rows, and does not
717 * require renumbering (of the rowIndex property) of previous rows.
718 *
719 * If you think you want a new blank row somewhere in the middle of the
720 * terminal, look into moveRows_().
721 *
722 * This method does not pay attention to vtScrollTop/Bottom, since you should
723 * be using moveRows() in cases where they would matter.
724 *
725 * The cursor will be positioned at column 0 of the first inserted line.
726 */
727hterm.Terminal.prototype.appendRows_ = function(count) {
728 var cursorRow = this.screen_.rowsArray.length;
729 var offset = this.scrollbackRows_.length + cursorRow;
730 for (var i = 0; i < count; i++) {
731 var row = this.document_.createElement('x-row');
732 row.appendChild(this.document_.createTextNode(''));
733 row.rowIndex = offset + i;
734 this.screen_.pushRow(row);
735 }
736
737 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
738 if (extraRows > 0) {
739 var ary = this.screen_.shiftRows(extraRows);
740 Array.prototype.push.apply(this.scrollbackRows_, ary);
741 this.scheduleScrollDown_();
742 }
743
744 if (cursorRow >= this.screen_.rowsArray.length)
745 cursorRow = this.screen_.rowsArray.length - 1;
746
rginda87b86462011-12-14 13:48:03 -0800747 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800748};
749
750/**
751 * Relocate rows from one part of the addressable screen to another.
752 *
753 * This is used to recycle rows during VT scrolls (those which are driven
754 * by VT commands, rather than by the user manipulating the scrollbar.)
755 *
756 * In this case, the blank lines scrolled into the scroll region are made of
757 * the nodes we scrolled off. These have their rowIndex properties carefully
758 * renumbered so as not to confuse the ScrollPort.
759 *
760 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
761 * It may just be luck. I wouldn't be surprised if we actually needed to call
762 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
763 * adding it.
764 */
765hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
766 var ary = this.screen_.removeRows(fromIndex, count);
767 this.screen_.insertRows(toIndex, ary);
768
769 var start, end;
770 if (fromIndex < toIndex) {
771 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800772 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800773 } else {
774 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800775 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800776 }
777
778 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800779 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800780};
781
782/**
783 * Renumber the rowIndex property of the given range of rows.
784 *
785 * The start and end indicies are relative to the screen, not the scrollback.
786 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800787 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800788 * no need to renumber scrollback rows.
789 */
790hterm.Terminal.prototype.renumberRows_ = function(start, end) {
791 var offset = this.scrollbackRows_.length;
792 for (var i = start; i < end; i++) {
793 this.screen_.rowsArray[i].rowIndex = offset + i;
794 }
795};
796
797/**
798 * Print a string to the terminal.
799 *
800 * This respects the current insert and wraparound modes. It will add new lines
801 * to the end of the terminal, scrolling off the top into the scrollback buffer
802 * if necessary.
803 *
804 * The string is *not* parsed for escape codes. Use the interpret() method if
805 * that's what you're after.
806 *
807 * @param{string} str The string to print.
808 */
809hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800810 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
811 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800812
rgindaa19afe22012-01-25 15:40:22 -0800813 if (this.options_.insertMode) {
814 this.screen_.insertString(str);
815 } else {
816 this.screen_.overwriteString(str);
817 }
818
819 var overflow = this.screen_.maybeClipCurrentRow();
820
821 if (this.options_.wraparound && overflow) {
822 var lastColumn;
823
824 do {
rginda35c456b2012-02-09 17:29:05 -0800825 this.newLine();
826 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800827
828 if (!this.options_.insertMode)
829 this.screen_.deleteChars(overflow.characterLength);
830
831 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800832
833 overflow = this.screen_.maybeClipCurrentRow();
834 } while (overflow);
835
836 this.setCursorColumn(lastColumn);
837 }
rginda8ba33642011-12-14 12:31:31 -0800838
839 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800840
841 if (this.scrollOnOutput)
842 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800843};
844
845/**
rginda87b86462011-12-14 13:48:03 -0800846 * Set the VT scroll region.
847 *
rginda87b86462011-12-14 13:48:03 -0800848 * This also resets the cursor position to the absolute (0, 0) position, since
849 * that's what xterm appears to do.
850 *
851 * @param {integer} scrollTop The zero-based top of the scroll region.
852 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
853 * inclusive.
854 */
855hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
856 this.vtScrollTop_ = scrollTop;
857 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800858};
859
860/**
rginda8ba33642011-12-14 12:31:31 -0800861 * Return the top row index according to the VT.
862 *
863 * This will return 0 unless the terminal has been told to restrict scrolling
864 * to some lower row. It is used for some VT cursor positioning and scrolling
865 * commands.
866 *
867 * @return {integer} The topmost row in the terminal's scroll region.
868 */
869hterm.Terminal.prototype.getVTScrollTop = function() {
870 if (this.vtScrollTop_ != null)
871 return this.vtScrollTop_;
872
873 return 0;
rginda87b86462011-12-14 13:48:03 -0800874};
rginda8ba33642011-12-14 12:31:31 -0800875
876/**
877 * Return the bottom row index according to the VT.
878 *
879 * This will return the height of the terminal unless the it has been told to
880 * restrict scrolling to some higher row. It is used for some VT cursor
881 * positioning and scrolling commands.
882 *
883 * @return {integer} The bottommost row in the terminal's scroll region.
884 */
885hterm.Terminal.prototype.getVTScrollBottom = function() {
886 if (this.vtScrollBottom_ != null)
887 return this.vtScrollBottom_;
888
rginda87b86462011-12-14 13:48:03 -0800889 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800890}
891
892/**
893 * Process a '\n' character.
894 *
895 * If the cursor is on the final row of the terminal this will append a new
896 * blank row to the screen and scroll the topmost row into the scrollback
897 * buffer.
898 *
899 * Otherwise, this moves the cursor to column zero of the next row.
900 */
901hterm.Terminal.prototype.newLine = function() {
902 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800903 // If we're at the end of the screen we need to append a new line and
904 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800905 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800906 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
907 // End of the scroll region does not affect the scrollback buffer.
908 this.vtScrollUp(1);
909 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800910 } else {
rginda87b86462011-12-14 13:48:03 -0800911 // Anywhere else in the screen just moves the cursor.
912 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800913 }
914};
915
916/**
917 * Like newLine(), except maintain the cursor column.
918 */
919hterm.Terminal.prototype.lineFeed = function() {
920 var column = this.screen_.cursorPosition.column;
921 this.newLine();
922 this.setCursorColumn(column);
923};
924
925/**
rginda87b86462011-12-14 13:48:03 -0800926 * If autoCarriageReturn is set then newLine(), else lineFeed().
927 */
928hterm.Terminal.prototype.formFeed = function() {
929 if (this.options_.autoCarriageReturn) {
930 this.newLine();
931 } else {
932 this.lineFeed();
933 }
934};
935
936/**
937 * Move the cursor up one row, possibly inserting a blank line.
938 *
939 * The cursor column is not changed.
940 */
941hterm.Terminal.prototype.reverseLineFeed = function() {
942 var scrollTop = this.getVTScrollTop();
943 var currentRow = this.screen_.cursorPosition.row;
944
945 if (currentRow == scrollTop) {
946 this.insertLines(1);
947 } else {
948 this.setAbsoluteCursorRow(currentRow - 1);
949 }
950};
951
952/**
rginda8ba33642011-12-14 12:31:31 -0800953 * Replace all characters to the left of the current cursor with the space
954 * character.
955 *
956 * TODO(rginda): This should probably *remove* the characters (not just replace
957 * with a space) if there are no characters at or beyond the current cursor
958 * position. Once it does that, it'll have the same text-attribute related
959 * issues as hterm.Screen.prototype.clearCursorRow :/
960 */
961hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800962 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800963 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800964 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
965 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800966};
967
968/**
969 * Erase a given number of characters to the right of the cursor, shifting
970 * remaining characters to the left.
971 *
972 * The cursor position is unchanged.
973 *
974 * TODO(rginda): Test that this works even when the cursor is positioned beyond
975 * the end of the text.
976 *
977 * TODO(rginda): This likely has text-attribute related troubles similar to the
978 * todo on hterm.Screen.prototype.clearCursorRow.
979 */
980hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800981 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800982
rginda87b86462011-12-14 13:48:03 -0800983 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800984 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
985 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800986 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800987};
988
989/**
990 * Erase the current line.
991 *
992 * The cursor position is unchanged.
993 *
994 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
995 * has a text-attribute related TODO.
996 */
997hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800998 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800999 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001000 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001001};
1002
1003/**
1004 * Erase all characters from the start of the scroll region to the current
1005 * cursor position.
1006 *
1007 * The cursor position is unchanged.
1008 *
1009 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1010 * has a text-attribute related TODO.
1011 */
1012hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001013 var cursor = this.saveCursor();
1014
1015 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001016
1017 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001018 for (var i = top; i < cursor.row; i++) {
1019 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001020 this.screen_.clearCursorRow();
1021 }
1022
rginda87b86462011-12-14 13:48:03 -08001023 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001024};
1025
1026/**
1027 * Erase all characters from the current cursor position to the end of the
1028 * scroll region.
1029 *
1030 * The cursor position is unchanged.
1031 *
1032 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1033 * has a text-attribute related TODO.
1034 */
1035hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001036 var cursor = this.saveCursor();
1037
1038 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001039
1040 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001041 for (var i = cursor.row + 1; i <= bottom; i++) {
1042 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001043 this.screen_.clearCursorRow();
1044 }
1045
rginda87b86462011-12-14 13:48:03 -08001046 this.restoreCursor(cursor);
1047};
1048
1049/**
1050 * Fill the terminal with a given character.
1051 *
1052 * This methods does not respect the VT scroll region.
1053 *
1054 * @param {string} ch The character to use for the fill.
1055 */
1056hterm.Terminal.prototype.fill = function(ch) {
1057 var cursor = this.saveCursor();
1058
1059 this.setAbsoluteCursorPosition(0, 0);
1060 for (var row = 0; row < this.screenSize.height; row++) {
1061 for (var col = 0; col < this.screenSize.width; col++) {
1062 this.setAbsoluteCursorPosition(row, col);
1063 this.screen_.overwriteString(ch);
1064 }
1065 }
1066
1067 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001068};
1069
1070/**
rgindae4d29232012-01-19 10:47:13 -08001071 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -08001072 *
rgindae4d29232012-01-19 10:47:13 -08001073 * The cursor position is unchanged. This does not respect the scroll
1074 * region.
rginda8ba33642011-12-14 12:31:31 -08001075 *
1076 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1077 * has a text-attribute related TODO.
1078 */
1079hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -08001080 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001081
rgindae4d29232012-01-19 10:47:13 -08001082 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001083
rgindae4d29232012-01-19 10:47:13 -08001084 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08001085 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001086 this.screen_.clearCursorRow();
1087 }
1088
rginda87b86462011-12-14 13:48:03 -08001089 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001090};
1091
1092/**
1093 * VT command to insert lines at the current cursor row.
1094 *
1095 * This respects the current scroll region. Rows pushed off the bottom are
1096 * lost (they won't show up in the scrollback buffer).
1097 *
1098 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1099 * has a text-attribute related TODO.
1100 *
1101 * @param {integer} count The number of lines to insert.
1102 */
1103hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001104 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001105
1106 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001107 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001108
rgindae4d29232012-01-19 10:47:13 -08001109 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001110 if (start != cursor.row)
1111 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001112
1113 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001114 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001115 this.screen_.clearCursorRow();
1116 }
1117
rginda87b86462011-12-14 13:48:03 -08001118 cursor.column = 0;
1119 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001120};
1121
1122/**
1123 * VT command to delete lines at the current cursor row.
1124 *
1125 * New rows are added to the bottom of scroll region to take their place. New
1126 * rows are strictly there to take up space and have no content or style.
1127 */
1128hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001129 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001130
rginda87b86462011-12-14 13:48:03 -08001131 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001132 var bottom = this.getVTScrollBottom();
1133
rginda87b86462011-12-14 13:48:03 -08001134 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001135 count = Math.min(count, maxCount);
1136
rginda87b86462011-12-14 13:48:03 -08001137 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001138 if (count != maxCount)
1139 this.moveRows_(top, count, moveStart);
1140
1141 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001142 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001143 this.screen_.clearCursorRow();
1144 }
1145
rginda87b86462011-12-14 13:48:03 -08001146 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001147};
1148
1149/**
1150 * Inserts the given number of spaces at the current cursor position.
1151 *
rginda87b86462011-12-14 13:48:03 -08001152 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001153 */
1154hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001155 var cursor = this.saveCursor();
1156
rginda0f5c0292012-01-13 11:00:13 -08001157 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001158 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001159 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001160
1161 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001162};
1163
1164/**
1165 * Forward-delete the specified number of characters starting at the cursor
1166 * position.
1167 *
1168 * @param {integer} count The number of characters to delete.
1169 */
1170hterm.Terminal.prototype.deleteChars = function(count) {
1171 this.screen_.deleteChars(count);
1172};
1173
1174/**
1175 * Shift rows in the scroll region upwards by a given number of lines.
1176 *
1177 * New rows are inserted at the bottom of the scroll region to fill the
1178 * vacated rows. The new rows not filled out with the current text attributes.
1179 *
1180 * This function does not affect the scrollback rows at all. Rows shifted
1181 * off the top are lost.
1182 *
rginda87b86462011-12-14 13:48:03 -08001183 * The cursor position is not altered.
1184 *
rginda8ba33642011-12-14 12:31:31 -08001185 * @param {integer} count The number of rows to scroll.
1186 */
1187hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001188 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001189
rginda87b86462011-12-14 13:48:03 -08001190 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001191 this.deleteLines(count);
1192
rginda87b86462011-12-14 13:48:03 -08001193 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001194};
1195
1196/**
1197 * Shift rows below the cursor down by a given number of lines.
1198 *
1199 * This function respects the current scroll region.
1200 *
1201 * New rows are inserted at the top of the scroll region to fill the
1202 * vacated rows. The new rows not filled out with the current text attributes.
1203 *
1204 * This function does not affect the scrollback rows at all. Rows shifted
1205 * off the bottom are lost.
1206 *
1207 * @param {integer} count The number of rows to scroll.
1208 */
1209hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001210 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001211
rginda87b86462011-12-14 13:48:03 -08001212 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001213 this.insertLines(opt_count);
1214
rginda87b86462011-12-14 13:48:03 -08001215 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001216};
1217
rginda87b86462011-12-14 13:48:03 -08001218
rginda8ba33642011-12-14 12:31:31 -08001219/**
1220 * Set the cursor position.
1221 *
1222 * The cursor row is relative to the scroll region if the terminal has
1223 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1224 *
1225 * @param {integer} row The new zero-based cursor row.
1226 * @param {integer} row The new zero-based cursor column.
1227 */
1228hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1229 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001230 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001231 } else {
rginda87b86462011-12-14 13:48:03 -08001232 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001233 }
rginda87b86462011-12-14 13:48:03 -08001234};
rginda8ba33642011-12-14 12:31:31 -08001235
rginda87b86462011-12-14 13:48:03 -08001236hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1237 var scrollTop = this.getVTScrollTop();
1238 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001239 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001240 this.screen_.setCursorPosition(row, column);
1241};
1242
1243hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001244 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1245 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001246 this.screen_.setCursorPosition(row, column);
1247};
1248
1249/**
1250 * Set the cursor column.
1251 *
1252 * @param {integer} column The new zero-based cursor column.
1253 */
1254hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001255 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001256};
1257
1258/**
1259 * Return the cursor column.
1260 *
1261 * @return {integer} The zero-based cursor column.
1262 */
1263hterm.Terminal.prototype.getCursorColumn = function() {
1264 return this.screen_.cursorPosition.column;
1265};
1266
1267/**
1268 * Set the cursor row.
1269 *
1270 * The cursor row is relative to the scroll region if the terminal has
1271 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1272 *
1273 * @param {integer} row The new cursor row.
1274 */
rginda87b86462011-12-14 13:48:03 -08001275hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1276 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001277};
1278
1279/**
1280 * Return the cursor row.
1281 *
1282 * @return {integer} The zero-based cursor row.
1283 */
1284hterm.Terminal.prototype.getCursorRow = function(row) {
1285 return this.screen_.cursorPosition.row;
1286};
1287
1288/**
1289 * Request that the ScrollPort redraw itself soon.
1290 *
1291 * The redraw will happen asynchronously, soon after the call stack winds down.
1292 * Multiple calls will be coalesced into a single redraw.
1293 */
1294hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001295 if (this.timeouts_.redraw)
1296 return;
rginda8ba33642011-12-14 12:31:31 -08001297
1298 var self = this;
rginda87b86462011-12-14 13:48:03 -08001299 this.timeouts_.redraw = setTimeout(function() {
1300 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001301 self.scrollPort_.redraw_();
1302 }, 0);
1303};
1304
1305/**
1306 * Request that the ScrollPort be scrolled to the bottom.
1307 *
1308 * The scroll will happen asynchronously, soon after the call stack winds down.
1309 * Multiple calls will be coalesced into a single scroll.
1310 *
1311 * This affects the scrollbar position of the ScrollPort, and has nothing to
1312 * do with the VT scroll commands.
1313 */
1314hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1315 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001316 return;
rginda8ba33642011-12-14 12:31:31 -08001317
1318 var self = this;
1319 this.timeouts_.scrollDown = setTimeout(function() {
1320 delete self.timeouts_.scrollDown;
1321 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1322 }, 10);
1323};
1324
1325/**
1326 * Move the cursor up a specified number of rows.
1327 *
1328 * @param {integer} count The number of rows to move the cursor.
1329 */
1330hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001331 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001332};
1333
1334/**
1335 * Move the cursor down a specified number of rows.
1336 *
1337 * @param {integer} count The number of rows to move the cursor.
1338 */
1339hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001340 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001341 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1342 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1343 this.screenSize.height - 1);
1344
1345 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1346 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001347 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001348};
1349
1350/**
1351 * Move the cursor left a specified number of columns.
1352 *
1353 * @param {integer} count The number of columns to move the cursor.
1354 */
1355hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001356 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001357};
1358
1359/**
1360 * Move the cursor right a specified number of columns.
1361 *
1362 * @param {integer} count The number of columns to move the cursor.
1363 */
1364hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001365 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001366 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001367 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001368 this.setCursorColumn(column);
1369};
1370
1371/**
1372 * Reverse the foreground and background colors of the terminal.
1373 *
1374 * This only affects text that was drawn with no attributes.
1375 *
1376 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1377 * been drawn with attributes that happen to coincide with the default
1378 * 'no-attribute' colors. My guess is probably not.
1379 */
1380hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001381 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001382 if (state) {
1383 this.scrollPort_.setForegroundColor(this.backgroundColor);
1384 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1385 } else {
1386 this.scrollPort_.setForegroundColor(this.foregroundColor);
1387 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1388 }
1389};
1390
1391/**
rginda87b86462011-12-14 13:48:03 -08001392 * Ring the terminal bell.
1393 *
1394 * We only have a visual bell, which quickly toggles inverse video in the
1395 * terminal.
1396 */
1397hterm.Terminal.prototype.ringBell = function() {
rgindaf0090c92012-02-10 14:58:52 -08001398 this.bellAudio_.play();
1399
rginda6d397402012-01-17 10:58:29 -08001400 this.cursorNode_.style.backgroundColor =
1401 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001402
1403 var self = this;
1404 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001405 self.cursorNode_.style.backgroundColor = self.cursorColor;
1406 }, 200);
rginda87b86462011-12-14 13:48:03 -08001407};
1408
1409/**
rginda8ba33642011-12-14 12:31:31 -08001410 * Set the origin mode bit.
1411 *
1412 * If origin mode is on, certain VT cursor and scrolling commands measure their
1413 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1414 * to the top of the addressable screen.
1415 *
1416 * Defaults to off.
1417 *
1418 * @param {boolean} state True to set origin mode, false to unset.
1419 */
1420hterm.Terminal.prototype.setOriginMode = function(state) {
1421 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001422 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001423};
1424
1425/**
1426 * Set the insert mode bit.
1427 *
1428 * If insert mode is on, existing text beyond the cursor position will be
1429 * shifted right to make room for new text. Otherwise, new text overwrites
1430 * any existing text.
1431 *
1432 * Defaults to off.
1433 *
1434 * @param {boolean} state True to set insert mode, false to unset.
1435 */
1436hterm.Terminal.prototype.setInsertMode = function(state) {
1437 this.options_.insertMode = state;
1438};
1439
1440/**
rginda87b86462011-12-14 13:48:03 -08001441 * Set the auto carriage return bit.
1442 *
1443 * If auto carriage return is on then a formfeed character is interpreted
1444 * as a newline, otherwise it's the same as a linefeed. The difference boils
1445 * down to whether or not the cursor column is reset.
1446 */
1447hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1448 this.options_.autoCarriageReturn = state;
1449};
1450
1451/**
rginda8ba33642011-12-14 12:31:31 -08001452 * Set the wraparound mode bit.
1453 *
1454 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1455 * to the start of the following row. Otherwise, the cursor is clamped to the
1456 * end of the screen and attempts to write past it are ignored.
1457 *
1458 * Defaults to on.
1459 *
1460 * @param {boolean} state True to set wraparound mode, false to unset.
1461 */
1462hterm.Terminal.prototype.setWraparound = function(state) {
1463 this.options_.wraparound = state;
1464};
1465
1466/**
1467 * Set the reverse-wraparound mode bit.
1468 *
1469 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1470 * to the end of the previous row. Otherwise, the cursor is clamped to column
1471 * 0.
1472 *
1473 * Defaults to off.
1474 *
1475 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1476 */
1477hterm.Terminal.prototype.setReverseWraparound = function(state) {
1478 this.options_.reverseWraparound = state;
1479};
1480
1481/**
1482 * Selects between the primary and alternate screens.
1483 *
1484 * If alternate mode is on, the alternate screen is active. Otherwise the
1485 * primary screen is active.
1486 *
1487 * Swapping screens has no effect on the scrollback buffer.
1488 *
1489 * Each screen maintains its own cursor position.
1490 *
1491 * Defaults to off.
1492 *
1493 * @param {boolean} state True to set alternate mode, false to unset.
1494 */
1495hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001496 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001497 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1498
rginda35c456b2012-02-09 17:29:05 -08001499 if (this.screen_.rowsArray.length &&
1500 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1501 // If the screen changed sizes while we were away, our rowIndexes may
1502 // be incorrect.
1503 var offset = this.scrollbackRows_.length;
1504 var ary = this.screen_.rowsArray;
1505 for (i = 0; i < ary.length; i++) {
1506 ary[i].rowIndex = offset + i;
1507 }
1508 }
rginda8ba33642011-12-14 12:31:31 -08001509
rginda35c456b2012-02-09 17:29:05 -08001510 this.realizeWidth_(this.screenSize.width);
1511 this.realizeHeight_(this.screenSize.height);
1512 this.scrollPort_.syncScrollHeight();
1513 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001514
rginda6d397402012-01-17 10:58:29 -08001515 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001516 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001517};
1518
1519/**
1520 * Set the cursor-blink mode bit.
1521 *
1522 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1523 * a visible cursor does not blink.
1524 *
1525 * You should make sure to turn blinking off if you're going to dispose of a
1526 * terminal, otherwise you'll leak a timeout.
1527 *
1528 * Defaults to on.
1529 *
1530 * @param {boolean} state True to set cursor-blink mode, false to unset.
1531 */
1532hterm.Terminal.prototype.setCursorBlink = function(state) {
1533 this.options_.cursorBlink = state;
1534
1535 if (!state && this.timeouts_.cursorBlink) {
1536 clearTimeout(this.timeouts_.cursorBlink);
1537 delete this.timeouts_.cursorBlink;
1538 }
1539
1540 if (this.options_.cursorVisible)
1541 this.setCursorVisible(true);
1542};
1543
1544/**
1545 * Set the cursor-visible mode bit.
1546 *
1547 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1548 *
1549 * Defaults to on.
1550 *
1551 * @param {boolean} state True to set cursor-visible mode, false to unset.
1552 */
1553hterm.Terminal.prototype.setCursorVisible = function(state) {
1554 this.options_.cursorVisible = state;
1555
1556 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001557 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001558 return;
1559 }
1560
rginda87b86462011-12-14 13:48:03 -08001561 this.syncCursorPosition_();
1562
1563 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001564
1565 if (this.options_.cursorBlink) {
1566 if (this.timeouts_.cursorBlink)
1567 return;
1568
1569 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1570 500);
1571 } else {
1572 if (this.timeouts_.cursorBlink) {
1573 clearTimeout(this.timeouts_.cursorBlink);
1574 delete this.timeouts_.cursorBlink;
1575 }
1576 }
1577};
1578
1579/**
rginda87b86462011-12-14 13:48:03 -08001580 * Synchronizes the visible cursor and document selection with the current
1581 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001582 */
1583hterm.Terminal.prototype.syncCursorPosition_ = function() {
1584 var topRowIndex = this.scrollPort_.getTopRowIndex();
1585 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1586 var cursorRowIndex = this.scrollbackRows_.length +
1587 this.screen_.cursorPosition.row;
1588
1589 if (cursorRowIndex > bottomRowIndex) {
1590 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001591 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001592 return;
1593 }
1594
rginda35c456b2012-02-09 17:29:05 -08001595 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1596 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1597
rginda8ba33642011-12-14 12:31:31 -08001598 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001599 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1600 'px';
1601 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1602 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001603
1604 this.cursorNode_.setAttribute('title',
1605 '(' + this.screen_.cursorPosition.row +
1606 ', ' + this.screen_.cursorPosition.column +
1607 ')');
1608
1609 // Update the caret for a11y purposes.
1610 var selection = this.document_.getSelection();
1611 if (selection && selection.isCollapsed)
1612 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001613};
1614
1615/**
1616 * Synchronizes the visible cursor with the current cursor coordinates.
1617 *
1618 * The sync will happen asynchronously, soon after the call stack winds down.
1619 * Multiple calls will be coalesced into a single sync.
1620 */
1621hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1622 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001623 return;
rginda8ba33642011-12-14 12:31:31 -08001624
1625 var self = this;
1626 this.timeouts_.syncCursor = setTimeout(function() {
1627 self.syncCursorPosition_();
1628 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001629 }, 0);
1630};
1631
rgindaf0090c92012-02-10 14:58:52 -08001632hterm.Terminal.prototype.showOverlay = function(msg) {
1633 if (!this.overlayNode_) {
1634 if (!this.div_)
1635 return;
1636
1637 this.overlayNode_ = this.document_.createElement('div');
1638 this.overlayNode_.style.cssText = (
1639 'background-color: ' + this.foregroundColor + ';' +
1640 'border-radius: 15px;' +
1641 'color: ' + this.backgroundColor + ';' +
1642 'font-family: ' + this.defaultFontFamily + ';' +
1643 'font-size: xx-large;' +
1644 'opacity: 0.75;' +
1645 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1646 'position: absolute;' +
1647 '-webkit-user-select: none;' +
1648 '-webkit-transition: opacity 180ms ease-in;');
1649 }
1650
1651 this.overlayNode_.textContent = msg;
1652 this.overlayNode_.style.opacity = '0.75';
1653
1654 if (!this.overlayNode_.parentNode)
1655 this.div_.appendChild(this.overlayNode_);
1656
1657 this.overlayNode_.style.top = (
1658 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1659 this.overlayNode_.style.left = (
1660 this.div_.clientWidth - this.overlayNode_.clientWidth -
1661 this.scrollbarWidthPx) / 2;
1662
1663 var self = this;
1664
1665 if (this.overlayTimeout_)
1666 clearTimeout(this.overlayTimeout_);
1667
1668 this.overlayTimeout_ = setTimeout(function() {
1669 self.overlayNode_.style.opacity = '0';
1670 setTimeout(function() {
1671 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
1672 self.overlayTimeout_ = null;
1673 self.overlayNode_.style.opacity = '0.75';
1674 }, 200);
1675 }, 1500);
1676};
1677
1678hterm.Terminal.prototype.overlaySize = function() {
1679 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1680};
1681
rginda87b86462011-12-14 13:48:03 -08001682/**
1683 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1684 *
1685 * @param {string} string The VT string representing the keystroke.
1686 */
1687hterm.Terminal.prototype.onVTKeystroke = function(string) {
1688 if (this.scrollOnKeystroke)
1689 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1690
1691 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001692};
1693
1694/**
1695 * React when the ScrollPort is scrolled.
1696 */
1697hterm.Terminal.prototype.onScroll_ = function() {
1698 this.scheduleSyncCursorPosition_();
1699};
1700
1701/**
rginda9846e2f2012-01-27 13:53:33 -08001702 * React when text is pasted into the scrollPort.
1703 */
1704hterm.Terminal.prototype.onPaste_ = function(e) {
1705 this.io.onVTKeystroke(e.text);
1706};
1707
1708/**
rginda8ba33642011-12-14 12:31:31 -08001709 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001710 *
1711 * Note: This function should not directly contain code that alters the internal
1712 * state of the terminal. That kind of code belongs in realizeWidth or
1713 * realizeHeight, so that it can be executed synchronously in the case of a
1714 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001715 */
1716hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001717 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001718 this.scrollPort_.characterSize.width);
1719 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1720 this.scrollPort_.characterSize.height);
1721
1722 if (!(columnCount || rowCount)) {
1723 // We avoid these situations since they happen sometimes when the terminal
1724 // gets removed from the document, and we can't deal with that.
1725 return;
1726 }
1727
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001728 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001729 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001730 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001731};
1732
1733/**
1734 * Service the cursor blink timeout.
1735 */
1736hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001737 if (this.cursorNode_.style.opacity == '0') {
1738 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001739 } else {
rginda87b86462011-12-14 13:48:03 -08001740 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001741 }
1742};