blob: b34b7d48daed027b9c965cbc93badd21f6c34325 [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 */
rginda87b86462011-12-14 13:48:03 -080022hterm.Terminal = function(fontSize, opt_lineHeight) {
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
36 // The pixel dimensions of a single character on the screen.
37 this.characterSize_ = new hterm.Size(0, 0);
38
39 // The scroll port we'll be using to display the visible rows.
rginda87b86462011-12-14 13:48:03 -080040 this.scrollPort_ = new hterm.ScrollPort(this, fontSize, opt_lineHeight);
rginda8ba33642011-12-14 12:31:31 -080041 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
42 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
43
rginda87b86462011-12-14 13:48:03 -080044 // The div that contains this terminal.
45 this.div_ = null;
46
rgindac9bc5502012-01-18 11:48:44 -080047 // The document that contains the scrollPort. Defaulted to the global
48 // document here so that the terminal is functional even if it hasn't been
49 // inserted into a document yet, but re-set in decorate().
50 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080051
rginda8ba33642011-12-14 12:31:31 -080052 // The rows that have scrolled off screen and are no longer addressable.
53 this.scrollbackRows_ = [];
54
rgindac9bc5502012-01-18 11:48:44 -080055 // Saved tab stops.
56 this.tabStops_ = [];
57
rginda8ba33642011-12-14 12:31:31 -080058 // The VT's notion of the top and bottom rows. Used during some VT
59 // cursor positioning and scrolling commands.
60 this.vtScrollTop_ = null;
61 this.vtScrollBottom_ = null;
62
63 // The DIV element for the visible cursor.
64 this.cursorNode_ = null;
65
66 // The default colors for text with no other color attributes.
67 this.backgroundColor = 'black';
68 this.foregroundColor = 'white';
69
rgindac9bc5502012-01-18 11:48:44 -080070 // Default tab with of 8 to match xterm.
71 this.tabWidth = 8;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The color of the cursor.
74 this.cursorColor = 'rgba(255,0,0,0.5)';
75
rginda87b86462011-12-14 13:48:03 -080076 // If true, scroll to the bottom on any keystroke.
77 this.scrollOnKeystroke = true;
78
rginda0f5c0292012-01-13 11:00:13 -080079 // If true, scroll to the bottom on terminal output.
80 this.scrollOnOutput = false;
81
rginda6d397402012-01-17 10:58:29 -080082 // Cursor position and attributes saved with DECSC.
83 this.savedOptions_ = {};
84
rginda8ba33642011-12-14 12:31:31 -080085 // The current mode bits for the terminal.
86 this.options_ = new hterm.Options();
87
88 // Timeouts we might need to clear.
89 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080090
91 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080092 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080093
94 // General IO interface that can be given to third parties without exposing
95 // the entire terminal object.
96 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080097
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040098 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -080099 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800100};
101
102/**
103 * Create a new instance of a terminal command and run it with a given
104 * argument string.
105 *
106 * @param {function} commandClass The constructor for a terminal command.
107 * @param {string} argString The argument string to pass to the command.
108 */
109hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
110 var self = this;
111 this.command = new commandClass(
112 { argString: argString || '',
113 io: this.io.push(),
114 onExit: function(code) {
115 self.io.pop();
116 self.io.println(hterm.msg('COMMAND_COMPLETE',
117 [self.command.commandName, code]));
118 }
119 });
120
121 this.command.run();
122};
123
124/**
125 * Return a copy of the current cursor position.
126 *
127 * @return {hterm.RowCol} The RowCol object representing the current position.
128 */
129hterm.Terminal.prototype.saveCursor = function() {
130 return this.screen_.cursorPosition.clone();
131};
132
133/**
134 * Restore a previously saved cursor position.
135 *
136 * @param {hterm.RowCol} cursor The position to restore.
137 */
138hterm.Terminal.prototype.restoreCursor = function(cursor) {
139 this.screen_.setCursorPosition(cursor.row, cursor.column);
rginda2312fff2012-01-05 16:20:52 -0800140 this.screen_.cursorPosition.overflow = cursor.overflow;
rginda87b86462011-12-14 13:48:03 -0800141};
142
143/**
144 * Set the width of the terminal, resizing the UI to match.
145 */
146hterm.Terminal.prototype.setWidth = function(columnCount) {
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400147 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px';
148 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800149 this.scheduleSyncCursorPosition_();
150};
rginda87b86462011-12-14 13:48:03 -0800151
rgindac9bc5502012-01-18 11:48:44 -0800152/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400153 * Deal with terminal size changes.
154 *
155 */
156hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
157 if (columnCount != this.screenSize.width)
158 this.realizeWidth_(columnCount);
159
160 if (rowCount != this.screenSize.height)
161 this.realizeHeight_(rowCount);
162
163 // Send new terminal size to plugin.
164 this.io.onTerminalResize(columnCount, rowCount);
165};
166
167/**
rgindac9bc5502012-01-18 11:48:44 -0800168 * Deal with terminal width changes.
169 *
170 * This function does what needs to be done when the terminal width changes
171 * out from under us. It happens here rather than in onResize_() because this
172 * code may need to run synchronously to handle programmatic changes of
173 * terminal width.
174 *
175 * Relying on the browser to send us an async resize event means we may not be
176 * in the correct state yet when the next escape sequence hits.
177 */
178hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
179 var deltaColumns = columnCount - this.screen_.getWidth();
180
rginda87b86462011-12-14 13:48:03 -0800181 this.screenSize.width = columnCount;
182 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800183
184 if (deltaColumns > 0) {
185 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
186 } else {
187 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
188 if (this.tabStops_[i] <= columnCount)
189 break;
190
191 this.tabStops_.pop();
192 }
193 }
194
195 this.screen_.setColumnCount(this.screenSize.width);
196};
197
198/**
199 * Deal with terminal height changes.
200 *
201 * This function does what needs to be done when the terminal height changes
202 * out from under us. It happens here rather than in onResize_() because this
203 * code may need to run synchronously to handle programmatic changes of
204 * terminal height.
205 *
206 * Relying on the browser to send us an async resize event means we may not be
207 * in the correct state yet when the next escape sequence hits.
208 */
209hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
210 var deltaRows = rowCount - this.screen_.getHeight();
211
212 this.screenSize.height = rowCount;
213
214 var cursor = this.saveCursor();
215
216 if (deltaRows < 0) {
217 // Screen got smaller.
218 deltaRows *= -1;
219 while (deltaRows) {
220 var lastRow = this.getRowCount() - 1;
221 if (lastRow - this.scrollbackRows_.length == cursor.row)
222 break;
223
224 if (this.getRowText(lastRow))
225 break;
226
227 this.screen_.popRow();
228 deltaRows--;
229 }
230
231 var ary = this.screen_.shiftRows(deltaRows);
232 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
233
234 // We just removed rows from the top of the screen, we need to update
235 // the cursor to match.
236 cursor.row -= deltaRows;
237
238 } else if (deltaRows > 0) {
239 // Screen got larger.
240
241 if (deltaRows <= this.scrollbackRows_.length) {
242 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
243 var rows = this.scrollbackRows_.splice(
244 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
245 this.screen_.unshiftRows(rows);
246 deltaRows -= scrollbackCount;
247 cursor.row += scrollbackCount;
248 }
249
250 if (deltaRows)
251 this.appendRows_(deltaRows);
252 }
253
254 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800255};
256
257/**
258 * Scroll the terminal to the top of the scrollback buffer.
259 */
260hterm.Terminal.prototype.scrollHome = function() {
261 this.scrollPort_.scrollRowToTop(0);
262};
263
264/**
265 * Scroll the terminal to the end.
266 */
267hterm.Terminal.prototype.scrollEnd = function() {
268 this.scrollPort_.scrollRowToBottom(this.getRowCount());
269};
270
271/**
272 * Scroll the terminal one page up (minus one line) relative to the current
273 * position.
274 */
275hterm.Terminal.prototype.scrollPageUp = function() {
276 var i = this.scrollPort_.getTopRowIndex();
277 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
278};
279
280/**
281 * Scroll the terminal one page down (minus one line) relative to the current
282 * position.
283 */
284hterm.Terminal.prototype.scrollPageDown = function() {
285 var i = this.scrollPort_.getTopRowIndex();
286 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800287};
288
rgindac9bc5502012-01-18 11:48:44 -0800289/**
290 * Full terminal reset.
291 */
rginda87b86462011-12-14 13:48:03 -0800292hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800293 this.clearAllTabStops();
294 this.setDefaultTabStops();
295 this.clearColorAndAttributes();
296 this.setVTScrollRegion(null, null);
297 this.clear();
298 this.setAbsoluteCursorPosition(0, 0);
299 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800300};
301
rgindac9bc5502012-01-18 11:48:44 -0800302/**
303 * Soft terminal reset.
304 */
rginda0f5c0292012-01-13 11:00:13 -0800305hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800306 this.options_ = new hterm.Options();
rginda0f5c0292012-01-13 11:00:13 -0800307};
308
rginda87b86462011-12-14 13:48:03 -0800309hterm.Terminal.prototype.clearColorAndAttributes = function() {
310 //console.log('clearColorAndAttributes');
311};
312
313hterm.Terminal.prototype.setForegroundColor256 = function() {
314 console.log('setForegroundColor256');
315};
316
317hterm.Terminal.prototype.setBackgroundColor256 = function() {
318 console.log('setBackgroundColor256');
319};
320
321hterm.Terminal.prototype.setForegroundColor = function() {
322 //console.log('setForegroundColor');
323};
324
325hterm.Terminal.prototype.setBackgroundColor = function() {
326 //console.log('setBackgroundColor');
327};
328
329hterm.Terminal.prototype.setAttributes = function() {
330 //console.log('setAttributes');
331};
332
333hterm.Terminal.prototype.resize = function() {
334 console.log('resize');
335};
336
rgindae4d29232012-01-19 10:47:13 -0800337hterm.Terminal.prototype.setCharacterSet = function() {
338 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800339};
340
rgindac9bc5502012-01-18 11:48:44 -0800341/**
342 * Move the cursor forward to the next tab stop, or to the last column
343 * if no more tab stops are set.
344 */
345hterm.Terminal.prototype.forwardTabStop = function() {
346 var column = this.screen_.cursorPosition.column;
347
348 for (var i = 0; i < this.tabStops_.length; i++) {
349 if (this.tabStops_[i] > column) {
350 this.setCursorColumn(this.tabStops_[i]);
351 return;
352 }
353 }
354
355 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800356};
357
rgindac9bc5502012-01-18 11:48:44 -0800358/**
359 * Move the cursor backward to the previous tab stop, or to the first column
360 * if no previous tab stops are set.
361 */
362hterm.Terminal.prototype.backwardTabStop = function() {
363 var column = this.screen_.cursorPosition.column;
364
365 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
366 if (this.tabStops_[i] < column) {
367 this.setCursorColumn(this.tabStops_[i]);
368 return;
369 }
370 }
371
372 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800373};
374
rgindac9bc5502012-01-18 11:48:44 -0800375/**
376 * Set a tab stop at the given column.
377 *
378 * @param {int} column Zero based column.
379 */
380hterm.Terminal.prototype.setTabStop = function(column) {
381 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
382 if (this.tabStops_[i] == column)
383 return;
384
385 if (this.tabStops_[i] < column) {
386 this.tabStops_.splice(i + 1, 0, column);
387 return;
388 }
389 }
390
391 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800392};
393
rgindac9bc5502012-01-18 11:48:44 -0800394/**
395 * Clear the tab stop at the current cursor position.
396 *
397 * No effect if there is no tab stop at the current cursor position.
398 */
399hterm.Terminal.prototype.clearTabStopAtCursor = function() {
400 var column = this.screen_.cursorPosition.column;
401
402 var i = this.tabStops_.indexOf(column);
403 if (i == -1)
404 return;
405
406 this.tabStops_.splice(i, 1);
407};
408
409/**
410 * Clear all tab stops.
411 */
412hterm.Terminal.prototype.clearAllTabStops = function() {
413 this.tabStops_.length = 0;
414};
415
416/**
417 * Set up the default tab stops, starting from a given column.
418 *
419 * This sets a tabstop every (column % this.tabWidth) column, starting
420 * from the specified column, or 0 if no column is provided.
421 *
422 * This does not clear the existing tab stops first, use clearAllTabStops
423 * for that.
424 *
425 * @param {int} opt_start Optional starting zero based starting column, useful
426 * for filling out missing tab stops when the terminal is resized.
427 */
428hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
429 var start = opt_start || 0;
430 var w = this.tabWidth;
431 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
432 for (var i = 0; i < stopCount; i++) {
433 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
434 }
rginda87b86462011-12-14 13:48:03 -0800435};
436
rginda6d397402012-01-17 10:58:29 -0800437/**
438 * Save cursor position and attributes.
439 *
440 * TODO(rginda): Save attributes once we support them.
441 */
rginda87b86462011-12-14 13:48:03 -0800442hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800443 this.savedOptions_.cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -0800444};
445
rginda6d397402012-01-17 10:58:29 -0800446/**
447 * Restore cursor position and attributes.
448 *
449 * TODO(rginda): Restore attributes once we support them.
450 */
rginda87b86462011-12-14 13:48:03 -0800451hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800452 if (this.savedOptions_.cursor)
453 this.restoreCursor(this.savedOptions_.cursor);
rginda8ba33642011-12-14 12:31:31 -0800454};
455
456/**
457 * Interpret a sequence of characters.
458 *
459 * Incomplete escape sequences are buffered until the next call.
460 *
461 * @param {string} str Sequence of characters to interpret or pass through.
462 */
463hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800464 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800465 this.scheduleSyncCursorPosition_();
466};
467
468/**
469 * Take over the given DIV for use as the terminal display.
470 *
471 * @param {HTMLDivElement} div The div to use as the terminal display.
472 */
473hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800474 this.div_ = div;
475
rginda8ba33642011-12-14 12:31:31 -0800476 this.scrollPort_.decorate(div);
477 this.document_ = this.scrollPort_.getDocument();
478
479 // Get character dimensions from the scrollPort.
480 this.characterSize_.height = this.scrollPort_.getRowHeight();
481 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
482
483 this.cursorNode_ = this.document_.createElement('div');
484 this.cursorNode_.style.cssText =
485 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800486 'top: -99px;' +
487 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800488 'width: ' + this.characterSize_.width + 'px;' +
489 'height: ' + this.characterSize_.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800490 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800491 'background-color: ' + this.cursorColor);
492 this.document_.body.appendChild(this.cursorNode_);
493
494 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800495
496 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
497
498 var link = this.document_.createElement('link');
499 link.setAttribute('href', '../css/dialogs.css');
500 link.setAttribute('rel', 'stylesheet');
501 this.document_.head.appendChild(link);
502
503 this.alertDialog = new AlertDialog(this.document_.body);
504 this.promptDialog = new PromptDialog(this.document_.body);
505 this.confirmDialog = new ConfirmDialog(this.document_.body);
506
507 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800508 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800509};
510
511hterm.Terminal.prototype.getDocument = function() {
512 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800513};
514
515/**
516 * Return the HTML Element for a given row index.
517 *
518 * This is a method from the RowProvider interface. The ScrollPort uses
519 * it to fetch rows on demand as they are scrolled into view.
520 *
521 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
522 * pairs to conserve memory.
523 *
524 * @param {integer} index The zero-based row index, measured relative to the
525 * start of the scrollback buffer. On-screen rows will always have the
526 * largest indicies.
527 * @return {HTMLElement} The 'x-row' element containing for the requested row.
528 */
529hterm.Terminal.prototype.getRowNode = function(index) {
530 if (index < this.scrollbackRows_.length)
531 return this.scrollbackRows_[index];
532
533 var screenIndex = index - this.scrollbackRows_.length;
534 return this.screen_.rowsArray[screenIndex];
535};
536
537/**
538 * Return the text content for a given range of rows.
539 *
540 * This is a method from the RowProvider interface. The ScrollPort uses
541 * it to fetch text content on demand when the user attempts to copy their
542 * selection to the clipboard.
543 *
544 * @param {integer} start The zero-based row index to start from, measured
545 * relative to the start of the scrollback buffer. On-screen rows will
546 * always have the largest indicies.
547 * @param {integer} end The zero-based row index to end on, measured
548 * relative to the start of the scrollback buffer.
549 * @return {string} A single string containing the text value of the range of
550 * rows. Lines will be newline delimited, with no trailing newline.
551 */
552hterm.Terminal.prototype.getRowsText = function(start, end) {
553 var ary = [];
554 for (var i = start; i < end; i++) {
555 var node = this.getRowNode(i);
556 ary.push(node.textContent);
557 }
558
559 return ary.join('\n');
560};
561
562/**
563 * Return the text content for a given row.
564 *
565 * This is a method from the RowProvider interface. The ScrollPort uses
566 * it to fetch text content on demand when the user attempts to copy their
567 * selection to the clipboard.
568 *
569 * @param {integer} index The zero-based row index to return, measured
570 * relative to the start of the scrollback buffer. On-screen rows will
571 * always have the largest indicies.
572 * @return {string} A string containing the text value of the selected row.
573 */
574hterm.Terminal.prototype.getRowText = function(index) {
575 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800576 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800577};
578
579/**
580 * Return the total number of rows in the addressable screen and in the
581 * scrollback buffer of this terminal.
582 *
583 * This is a method from the RowProvider interface. The ScrollPort uses
584 * it to compute the size of the scrollbar.
585 *
586 * @return {integer} The number of rows in this terminal.
587 */
588hterm.Terminal.prototype.getRowCount = function() {
589 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
590};
591
592/**
593 * Create DOM nodes for new rows and append them to the end of the terminal.
594 *
595 * This is the only correct way to add a new DOM node for a row. Notice that
596 * the new row is appended to the bottom of the list of rows, and does not
597 * require renumbering (of the rowIndex property) of previous rows.
598 *
599 * If you think you want a new blank row somewhere in the middle of the
600 * terminal, look into moveRows_().
601 *
602 * This method does not pay attention to vtScrollTop/Bottom, since you should
603 * be using moveRows() in cases where they would matter.
604 *
605 * The cursor will be positioned at column 0 of the first inserted line.
606 */
607hterm.Terminal.prototype.appendRows_ = function(count) {
608 var cursorRow = this.screen_.rowsArray.length;
609 var offset = this.scrollbackRows_.length + cursorRow;
610 for (var i = 0; i < count; i++) {
611 var row = this.document_.createElement('x-row');
612 row.appendChild(this.document_.createTextNode(''));
613 row.rowIndex = offset + i;
614 this.screen_.pushRow(row);
615 }
616
617 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
618 if (extraRows > 0) {
619 var ary = this.screen_.shiftRows(extraRows);
620 Array.prototype.push.apply(this.scrollbackRows_, ary);
621 this.scheduleScrollDown_();
622 }
623
624 if (cursorRow >= this.screen_.rowsArray.length)
625 cursorRow = this.screen_.rowsArray.length - 1;
626
rginda87b86462011-12-14 13:48:03 -0800627 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800628};
629
630/**
631 * Relocate rows from one part of the addressable screen to another.
632 *
633 * This is used to recycle rows during VT scrolls (those which are driven
634 * by VT commands, rather than by the user manipulating the scrollbar.)
635 *
636 * In this case, the blank lines scrolled into the scroll region are made of
637 * the nodes we scrolled off. These have their rowIndex properties carefully
638 * renumbered so as not to confuse the ScrollPort.
639 *
640 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
641 * It may just be luck. I wouldn't be surprised if we actually needed to call
642 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
643 * adding it.
644 */
645hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
646 var ary = this.screen_.removeRows(fromIndex, count);
647 this.screen_.insertRows(toIndex, ary);
648
649 var start, end;
650 if (fromIndex < toIndex) {
651 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800652 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800653 } else {
654 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800655 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800656 }
657
658 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800659 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800660};
661
662/**
663 * Renumber the rowIndex property of the given range of rows.
664 *
665 * The start and end indicies are relative to the screen, not the scrollback.
666 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800667 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800668 * no need to renumber scrollback rows.
669 */
670hterm.Terminal.prototype.renumberRows_ = function(start, end) {
671 var offset = this.scrollbackRows_.length;
672 for (var i = start; i < end; i++) {
673 this.screen_.rowsArray[i].rowIndex = offset + i;
674 }
675};
676
677/**
678 * Print a string to the terminal.
679 *
680 * This respects the current insert and wraparound modes. It will add new lines
681 * to the end of the terminal, scrolling off the top into the scrollback buffer
682 * if necessary.
683 *
684 * The string is *not* parsed for escape codes. Use the interpret() method if
685 * that's what you're after.
686 *
687 * @param{string} str The string to print.
688 */
689hterm.Terminal.prototype.print = function(str) {
690 do {
rginda2312fff2012-01-05 16:20:52 -0800691 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
692 this.newLine();
693
rginda8ba33642011-12-14 12:31:31 -0800694 if (this.options_.insertMode) {
695 str = this.screen_.insertString(str);
696 } else {
697 str = this.screen_.overwriteString(str);
698 }
rginda2312fff2012-01-05 16:20:52 -0800699 } while (this.options_.wraparound && str);
rginda8ba33642011-12-14 12:31:31 -0800700
701 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800702
703 if (this.scrollOnOutput)
704 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800705};
706
707/**
rginda87b86462011-12-14 13:48:03 -0800708 * Set the VT scroll region.
709 *
rginda87b86462011-12-14 13:48:03 -0800710 * This also resets the cursor position to the absolute (0, 0) position, since
711 * that's what xterm appears to do.
712 *
713 * @param {integer} scrollTop The zero-based top of the scroll region.
714 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
715 * inclusive.
716 */
717hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
718 this.vtScrollTop_ = scrollTop;
719 this.vtScrollBottom_ = scrollBottom;
720 this.setAbsoluteCursorPosition(0, 0);
721};
722
723/**
rginda8ba33642011-12-14 12:31:31 -0800724 * Return the top row index according to the VT.
725 *
726 * This will return 0 unless the terminal has been told to restrict scrolling
727 * to some lower row. It is used for some VT cursor positioning and scrolling
728 * commands.
729 *
730 * @return {integer} The topmost row in the terminal's scroll region.
731 */
732hterm.Terminal.prototype.getVTScrollTop = function() {
733 if (this.vtScrollTop_ != null)
734 return this.vtScrollTop_;
735
736 return 0;
rginda87b86462011-12-14 13:48:03 -0800737};
rginda8ba33642011-12-14 12:31:31 -0800738
739/**
740 * Return the bottom row index according to the VT.
741 *
742 * This will return the height of the terminal unless the it has been told to
743 * restrict scrolling to some higher row. It is used for some VT cursor
744 * positioning and scrolling commands.
745 *
746 * @return {integer} The bottommost row in the terminal's scroll region.
747 */
748hterm.Terminal.prototype.getVTScrollBottom = function() {
749 if (this.vtScrollBottom_ != null)
750 return this.vtScrollBottom_;
751
rginda87b86462011-12-14 13:48:03 -0800752 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800753}
754
755/**
756 * Process a '\n' character.
757 *
758 * If the cursor is on the final row of the terminal this will append a new
759 * blank row to the screen and scroll the topmost row into the scrollback
760 * buffer.
761 *
762 * Otherwise, this moves the cursor to column zero of the next row.
763 */
764hterm.Terminal.prototype.newLine = function() {
765 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800766 // If we're at the end of the screen we need to append a new line and
767 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800768 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800769 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
770 // End of the scroll region does not affect the scrollback buffer.
771 this.vtScrollUp(1);
772 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800773 } else {
rginda87b86462011-12-14 13:48:03 -0800774 // Anywhere else in the screen just moves the cursor.
775 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800776 }
777};
778
779/**
780 * Like newLine(), except maintain the cursor column.
781 */
782hterm.Terminal.prototype.lineFeed = function() {
783 var column = this.screen_.cursorPosition.column;
784 this.newLine();
785 this.setCursorColumn(column);
786};
787
788/**
rginda87b86462011-12-14 13:48:03 -0800789 * If autoCarriageReturn is set then newLine(), else lineFeed().
790 */
791hterm.Terminal.prototype.formFeed = function() {
792 if (this.options_.autoCarriageReturn) {
793 this.newLine();
794 } else {
795 this.lineFeed();
796 }
797};
798
799/**
800 * Move the cursor up one row, possibly inserting a blank line.
801 *
802 * The cursor column is not changed.
803 */
804hterm.Terminal.prototype.reverseLineFeed = function() {
805 var scrollTop = this.getVTScrollTop();
806 var currentRow = this.screen_.cursorPosition.row;
807
808 if (currentRow == scrollTop) {
809 this.insertLines(1);
810 } else {
811 this.setAbsoluteCursorRow(currentRow - 1);
812 }
813};
814
815/**
rginda8ba33642011-12-14 12:31:31 -0800816 * Replace all characters to the left of the current cursor with the space
817 * character.
818 *
819 * TODO(rginda): This should probably *remove* the characters (not just replace
820 * with a space) if there are no characters at or beyond the current cursor
821 * position. Once it does that, it'll have the same text-attribute related
822 * issues as hterm.Screen.prototype.clearCursorRow :/
823 */
824hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800825 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800826 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800827 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
828 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800829};
830
831/**
832 * Erase a given number of characters to the right of the cursor, shifting
833 * remaining characters to the left.
834 *
835 * The cursor position is unchanged.
836 *
837 * TODO(rginda): Test that this works even when the cursor is positioned beyond
838 * the end of the text.
839 *
840 * TODO(rginda): This likely has text-attribute related troubles similar to the
841 * todo on hterm.Screen.prototype.clearCursorRow.
842 */
843hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800844 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800845
rginda87b86462011-12-14 13:48:03 -0800846 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800847 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
848 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800849 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800850};
851
852/**
853 * Erase the current line.
854 *
855 * The cursor position is unchanged.
856 *
857 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
858 * has a text-attribute related TODO.
859 */
860hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800861 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800862 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800863 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800864};
865
866/**
867 * Erase all characters from the start of the scroll region to the current
868 * cursor position.
869 *
870 * The cursor position is unchanged.
871 *
872 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
873 * has a text-attribute related TODO.
874 */
875hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800876 var cursor = this.saveCursor();
877
878 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800879
880 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800881 for (var i = top; i < cursor.row; i++) {
882 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800883 this.screen_.clearCursorRow();
884 }
885
rginda87b86462011-12-14 13:48:03 -0800886 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800887};
888
889/**
890 * Erase all characters from the current cursor position to the end of the
891 * scroll region.
892 *
893 * The cursor position is unchanged.
894 *
895 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
896 * has a text-attribute related TODO.
897 */
898hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800899 var cursor = this.saveCursor();
900
901 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800902
903 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800904 for (var i = cursor.row + 1; i <= bottom; i++) {
905 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800906 this.screen_.clearCursorRow();
907 }
908
rginda87b86462011-12-14 13:48:03 -0800909 this.restoreCursor(cursor);
910};
911
912/**
913 * Fill the terminal with a given character.
914 *
915 * This methods does not respect the VT scroll region.
916 *
917 * @param {string} ch The character to use for the fill.
918 */
919hterm.Terminal.prototype.fill = function(ch) {
920 var cursor = this.saveCursor();
921
922 this.setAbsoluteCursorPosition(0, 0);
923 for (var row = 0; row < this.screenSize.height; row++) {
924 for (var col = 0; col < this.screenSize.width; col++) {
925 this.setAbsoluteCursorPosition(row, col);
926 this.screen_.overwriteString(ch);
927 }
928 }
929
930 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800931};
932
933/**
rgindae4d29232012-01-19 10:47:13 -0800934 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -0800935 *
rgindae4d29232012-01-19 10:47:13 -0800936 * The cursor position is unchanged. This does not respect the scroll
937 * region.
rginda8ba33642011-12-14 12:31:31 -0800938 *
939 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
940 * has a text-attribute related TODO.
941 */
942hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800943 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800944
rgindae4d29232012-01-19 10:47:13 -0800945 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -0800946
rgindae4d29232012-01-19 10:47:13 -0800947 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800948 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800949 this.screen_.clearCursorRow();
950 }
951
rginda87b86462011-12-14 13:48:03 -0800952 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800953};
954
955/**
956 * VT command to insert lines at the current cursor row.
957 *
958 * This respects the current scroll region. Rows pushed off the bottom are
959 * lost (they won't show up in the scrollback buffer).
960 *
961 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
962 * has a text-attribute related TODO.
963 *
964 * @param {integer} count The number of lines to insert.
965 */
966hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800967 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800968
969 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800970 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800971
rgindae4d29232012-01-19 10:47:13 -0800972 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -0800973 if (start != cursor.row)
974 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800975
976 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800977 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800978 this.screen_.clearCursorRow();
979 }
980
rginda87b86462011-12-14 13:48:03 -0800981 cursor.column = 0;
982 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800983};
984
985/**
986 * VT command to delete lines at the current cursor row.
987 *
988 * New rows are added to the bottom of scroll region to take their place. New
989 * rows are strictly there to take up space and have no content or style.
990 */
991hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800992 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800993
rginda87b86462011-12-14 13:48:03 -0800994 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -0800995 var bottom = this.getVTScrollBottom();
996
rginda87b86462011-12-14 13:48:03 -0800997 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -0800998 count = Math.min(count, maxCount);
999
rginda87b86462011-12-14 13:48:03 -08001000 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001001 if (count != maxCount)
1002 this.moveRows_(top, count, moveStart);
1003
1004 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001005 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001006 this.screen_.clearCursorRow();
1007 }
1008
rginda87b86462011-12-14 13:48:03 -08001009 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001010};
1011
1012/**
1013 * Inserts the given number of spaces at the current cursor position.
1014 *
rginda87b86462011-12-14 13:48:03 -08001015 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001016 */
1017hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001018 var cursor = this.saveCursor();
1019
rginda0f5c0292012-01-13 11:00:13 -08001020 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001021 this.screen_.insertString(ws);
rginda87b86462011-12-14 13:48:03 -08001022
1023 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001024};
1025
1026/**
1027 * Forward-delete the specified number of characters starting at the cursor
1028 * position.
1029 *
1030 * @param {integer} count The number of characters to delete.
1031 */
1032hterm.Terminal.prototype.deleteChars = function(count) {
1033 this.screen_.deleteChars(count);
1034};
1035
1036/**
1037 * Shift rows in the scroll region upwards by a given number of lines.
1038 *
1039 * New rows are inserted at the bottom of the scroll region to fill the
1040 * vacated rows. The new rows not filled out with the current text attributes.
1041 *
1042 * This function does not affect the scrollback rows at all. Rows shifted
1043 * off the top are lost.
1044 *
rginda87b86462011-12-14 13:48:03 -08001045 * The cursor position is not altered.
1046 *
rginda8ba33642011-12-14 12:31:31 -08001047 * @param {integer} count The number of rows to scroll.
1048 */
1049hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001050 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001051
rginda87b86462011-12-14 13:48:03 -08001052 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001053 this.deleteLines(count);
1054
rginda87b86462011-12-14 13:48:03 -08001055 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001056};
1057
1058/**
1059 * Shift rows below the cursor down by a given number of lines.
1060 *
1061 * This function respects the current scroll region.
1062 *
1063 * New rows are inserted at the top of the scroll region to fill the
1064 * vacated rows. The new rows not filled out with the current text attributes.
1065 *
1066 * This function does not affect the scrollback rows at all. Rows shifted
1067 * off the bottom are lost.
1068 *
1069 * @param {integer} count The number of rows to scroll.
1070 */
1071hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001072 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001073
rginda87b86462011-12-14 13:48:03 -08001074 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001075 this.insertLines(opt_count);
1076
rginda87b86462011-12-14 13:48:03 -08001077 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001078};
1079
rginda87b86462011-12-14 13:48:03 -08001080
rginda8ba33642011-12-14 12:31:31 -08001081/**
1082 * Set the cursor position.
1083 *
1084 * The cursor row is relative to the scroll region if the terminal has
1085 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1086 *
1087 * @param {integer} row The new zero-based cursor row.
1088 * @param {integer} row The new zero-based cursor column.
1089 */
1090hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1091 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001092 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001093 } else {
rginda87b86462011-12-14 13:48:03 -08001094 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001095 }
rginda87b86462011-12-14 13:48:03 -08001096};
rginda8ba33642011-12-14 12:31:31 -08001097
rginda87b86462011-12-14 13:48:03 -08001098hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1099 var scrollTop = this.getVTScrollTop();
1100 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001101 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001102 this.screen_.setCursorPosition(row, column);
1103};
1104
1105hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001106 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1107 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001108 this.screen_.setCursorPosition(row, column);
1109};
1110
1111/**
1112 * Set the cursor column.
1113 *
1114 * @param {integer} column The new zero-based cursor column.
1115 */
1116hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001117 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001118};
1119
1120/**
1121 * Return the cursor column.
1122 *
1123 * @return {integer} The zero-based cursor column.
1124 */
1125hterm.Terminal.prototype.getCursorColumn = function() {
1126 return this.screen_.cursorPosition.column;
1127};
1128
1129/**
1130 * Set the cursor row.
1131 *
1132 * The cursor row is relative to the scroll region if the terminal has
1133 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1134 *
1135 * @param {integer} row The new cursor row.
1136 */
rginda87b86462011-12-14 13:48:03 -08001137hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1138 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001139};
1140
1141/**
1142 * Return the cursor row.
1143 *
1144 * @return {integer} The zero-based cursor row.
1145 */
1146hterm.Terminal.prototype.getCursorRow = function(row) {
1147 return this.screen_.cursorPosition.row;
1148};
1149
1150/**
1151 * Request that the ScrollPort redraw itself soon.
1152 *
1153 * The redraw will happen asynchronously, soon after the call stack winds down.
1154 * Multiple calls will be coalesced into a single redraw.
1155 */
1156hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001157 if (this.timeouts_.redraw)
1158 return;
rginda8ba33642011-12-14 12:31:31 -08001159
1160 var self = this;
rginda87b86462011-12-14 13:48:03 -08001161 this.timeouts_.redraw = setTimeout(function() {
1162 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001163 self.scrollPort_.redraw_();
1164 }, 0);
1165};
1166
1167/**
1168 * Request that the ScrollPort be scrolled to the bottom.
1169 *
1170 * The scroll will happen asynchronously, soon after the call stack winds down.
1171 * Multiple calls will be coalesced into a single scroll.
1172 *
1173 * This affects the scrollbar position of the ScrollPort, and has nothing to
1174 * do with the VT scroll commands.
1175 */
1176hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1177 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001178 return;
rginda8ba33642011-12-14 12:31:31 -08001179
1180 var self = this;
1181 this.timeouts_.scrollDown = setTimeout(function() {
1182 delete self.timeouts_.scrollDown;
1183 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1184 }, 10);
1185};
1186
1187/**
1188 * Move the cursor up a specified number of rows.
1189 *
1190 * @param {integer} count The number of rows to move the cursor.
1191 */
1192hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001193 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001194};
1195
1196/**
1197 * Move the cursor down a specified number of rows.
1198 *
1199 * @param {integer} count The number of rows to move the cursor.
1200 */
1201hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001202 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001203 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1204 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1205 this.screenSize.height - 1);
1206
1207 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1208 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001209 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001210};
1211
1212/**
1213 * Move the cursor left a specified number of columns.
1214 *
1215 * @param {integer} count The number of columns to move the cursor.
1216 */
1217hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001218 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001219};
1220
1221/**
1222 * Move the cursor right a specified number of columns.
1223 *
1224 * @param {integer} count The number of columns to move the cursor.
1225 */
1226hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001227 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001228 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001229 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001230 this.setCursorColumn(column);
1231};
1232
1233/**
1234 * Reverse the foreground and background colors of the terminal.
1235 *
1236 * This only affects text that was drawn with no attributes.
1237 *
1238 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1239 * been drawn with attributes that happen to coincide with the default
1240 * 'no-attribute' colors. My guess is probably not.
1241 */
1242hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001243 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001244 if (state) {
1245 this.scrollPort_.setForegroundColor(this.backgroundColor);
1246 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1247 } else {
1248 this.scrollPort_.setForegroundColor(this.foregroundColor);
1249 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1250 }
1251};
1252
1253/**
rginda87b86462011-12-14 13:48:03 -08001254 * Ring the terminal bell.
1255 *
1256 * We only have a visual bell, which quickly toggles inverse video in the
1257 * terminal.
1258 */
1259hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001260 this.cursorNode_.style.backgroundColor =
1261 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001262
1263 var self = this;
1264 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001265 self.cursorNode_.style.backgroundColor = self.cursorColor;
1266 }, 200);
rginda87b86462011-12-14 13:48:03 -08001267};
1268
1269/**
rginda8ba33642011-12-14 12:31:31 -08001270 * Set the origin mode bit.
1271 *
1272 * If origin mode is on, certain VT cursor and scrolling commands measure their
1273 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1274 * to the top of the addressable screen.
1275 *
1276 * Defaults to off.
1277 *
1278 * @param {boolean} state True to set origin mode, false to unset.
1279 */
1280hterm.Terminal.prototype.setOriginMode = function(state) {
1281 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001282 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001283};
1284
1285/**
1286 * Set the insert mode bit.
1287 *
1288 * If insert mode is on, existing text beyond the cursor position will be
1289 * shifted right to make room for new text. Otherwise, new text overwrites
1290 * any existing text.
1291 *
1292 * Defaults to off.
1293 *
1294 * @param {boolean} state True to set insert mode, false to unset.
1295 */
1296hterm.Terminal.prototype.setInsertMode = function(state) {
1297 this.options_.insertMode = state;
1298};
1299
1300/**
rginda87b86462011-12-14 13:48:03 -08001301 * Set the auto carriage return bit.
1302 *
1303 * If auto carriage return is on then a formfeed character is interpreted
1304 * as a newline, otherwise it's the same as a linefeed. The difference boils
1305 * down to whether or not the cursor column is reset.
1306 */
1307hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1308 this.options_.autoCarriageReturn = state;
1309};
1310
1311/**
rginda8ba33642011-12-14 12:31:31 -08001312 * Set the wraparound mode bit.
1313 *
1314 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1315 * to the start of the following row. Otherwise, the cursor is clamped to the
1316 * end of the screen and attempts to write past it are ignored.
1317 *
1318 * Defaults to on.
1319 *
1320 * @param {boolean} state True to set wraparound mode, false to unset.
1321 */
1322hterm.Terminal.prototype.setWraparound = function(state) {
1323 this.options_.wraparound = state;
1324};
1325
1326/**
1327 * Set the reverse-wraparound mode bit.
1328 *
1329 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1330 * to the end of the previous row. Otherwise, the cursor is clamped to column
1331 * 0.
1332 *
1333 * Defaults to off.
1334 *
1335 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1336 */
1337hterm.Terminal.prototype.setReverseWraparound = function(state) {
1338 this.options_.reverseWraparound = state;
1339};
1340
1341/**
1342 * Selects between the primary and alternate screens.
1343 *
1344 * If alternate mode is on, the alternate screen is active. Otherwise the
1345 * primary screen is active.
1346 *
1347 * Swapping screens has no effect on the scrollback buffer.
1348 *
1349 * Each screen maintains its own cursor position.
1350 *
1351 * Defaults to off.
1352 *
1353 * @param {boolean} state True to set alternate mode, false to unset.
1354 */
1355hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001356 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001357 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1358
1359 this.screen_.setColumnCount(this.screenSize.width);
1360
1361 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1362 if (rowDelta > 0)
1363 this.appendRows_(rowDelta);
1364
rginda6d397402012-01-17 10:58:29 -08001365 this.restoreCursor(cursor);
1366
rginda2312fff2012-01-05 16:20:52 -08001367 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001368 this.syncCursorPosition_();
1369};
1370
1371/**
1372 * Set the cursor-blink mode bit.
1373 *
1374 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1375 * a visible cursor does not blink.
1376 *
1377 * You should make sure to turn blinking off if you're going to dispose of a
1378 * terminal, otherwise you'll leak a timeout.
1379 *
1380 * Defaults to on.
1381 *
1382 * @param {boolean} state True to set cursor-blink mode, false to unset.
1383 */
1384hterm.Terminal.prototype.setCursorBlink = function(state) {
1385 this.options_.cursorBlink = state;
1386
1387 if (!state && this.timeouts_.cursorBlink) {
1388 clearTimeout(this.timeouts_.cursorBlink);
1389 delete this.timeouts_.cursorBlink;
1390 }
1391
1392 if (this.options_.cursorVisible)
1393 this.setCursorVisible(true);
1394};
1395
1396/**
1397 * Set the cursor-visible mode bit.
1398 *
1399 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1400 *
1401 * Defaults to on.
1402 *
1403 * @param {boolean} state True to set cursor-visible mode, false to unset.
1404 */
1405hterm.Terminal.prototype.setCursorVisible = function(state) {
1406 this.options_.cursorVisible = state;
1407
1408 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001409 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001410 return;
1411 }
1412
rginda87b86462011-12-14 13:48:03 -08001413 this.syncCursorPosition_();
1414
1415 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001416
1417 if (this.options_.cursorBlink) {
1418 if (this.timeouts_.cursorBlink)
1419 return;
1420
1421 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1422 500);
1423 } else {
1424 if (this.timeouts_.cursorBlink) {
1425 clearTimeout(this.timeouts_.cursorBlink);
1426 delete this.timeouts_.cursorBlink;
1427 }
1428 }
1429};
1430
1431/**
rginda87b86462011-12-14 13:48:03 -08001432 * Synchronizes the visible cursor and document selection with the current
1433 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001434 */
1435hterm.Terminal.prototype.syncCursorPosition_ = function() {
1436 var topRowIndex = this.scrollPort_.getTopRowIndex();
1437 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1438 var cursorRowIndex = this.scrollbackRows_.length +
1439 this.screen_.cursorPosition.row;
1440
1441 if (cursorRowIndex > bottomRowIndex) {
1442 // Cursor is scrolled off screen, move it outside of the visible area.
1443 this.cursorNode_.style.top = -this.characterSize_.height;
1444 return;
1445 }
1446
1447 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1448 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1449 this.cursorNode_.style.left = this.characterSize_.width *
1450 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001451
1452 this.cursorNode_.setAttribute('title',
1453 '(' + this.screen_.cursorPosition.row +
1454 ', ' + this.screen_.cursorPosition.column +
1455 ')');
1456
1457 // Update the caret for a11y purposes.
1458 var selection = this.document_.getSelection();
1459 if (selection && selection.isCollapsed)
1460 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001461};
1462
1463/**
1464 * Synchronizes the visible cursor with the current cursor coordinates.
1465 *
1466 * The sync will happen asynchronously, soon after the call stack winds down.
1467 * Multiple calls will be coalesced into a single sync.
1468 */
1469hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1470 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001471 return;
rginda8ba33642011-12-14 12:31:31 -08001472
1473 var self = this;
1474 this.timeouts_.syncCursor = setTimeout(function() {
1475 self.syncCursorPosition_();
1476 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001477 }, 0);
1478};
1479
1480/**
1481 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1482 *
1483 * @param {string} string The VT string representing the keystroke.
1484 */
1485hterm.Terminal.prototype.onVTKeystroke = function(string) {
1486 if (this.scrollOnKeystroke)
1487 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1488
1489 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001490};
1491
1492/**
1493 * React when the ScrollPort is scrolled.
1494 */
1495hterm.Terminal.prototype.onScroll_ = function() {
1496 this.scheduleSyncCursorPosition_();
1497};
1498
1499/**
1500 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001501 *
1502 * Note: This function should not directly contain code that alters the internal
1503 * state of the terminal. That kind of code belongs in realizeWidth or
1504 * realizeHeight, so that it can be executed synchronously in the case of a
1505 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001506 */
1507hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001508 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
1509 this.characterSize_.width);
rgindac9bc5502012-01-18 11:48:44 -08001510 var rowCount = this.scrollPort_.visibleRowCount;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001511 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001512 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001513};
1514
1515/**
1516 * Service the cursor blink timeout.
1517 */
1518hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001519 if (this.cursorNode_.style.opacity == '0') {
1520 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001521 } else {
rginda87b86462011-12-14 13:48:03 -08001522 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001523 }
1524};