blob: f03d98c274dc6818898aed79c6c864b780e6eed7 [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
rgindaa19afe22012-01-25 15:40:22 -080066 // Default font family for the terminal text.
67 this.defaultFontFamily_ = '"DejaVu Sans Mono", "Everson Mono", FreeMono, ' +
68 '"Andale Mono", "Lucida Console", monospace'
69
rginda8ba33642011-12-14 12:31:31 -080070 // The default colors for text with no other color attributes.
71 this.backgroundColor = 'black';
72 this.foregroundColor = 'white';
73
rgindac9bc5502012-01-18 11:48:44 -080074 // Default tab with of 8 to match xterm.
75 this.tabWidth = 8;
76
rgindaa19afe22012-01-25 15:40:22 -080077 // The color of the visible cursor.
rginda8ba33642011-12-14 12:31:31 -080078 this.cursorColor = 'rgba(255,0,0,0.5)';
79
rginda87b86462011-12-14 13:48:03 -080080 // If true, scroll to the bottom on any keystroke.
81 this.scrollOnKeystroke = true;
82
rginda0f5c0292012-01-13 11:00:13 -080083 // If true, scroll to the bottom on terminal output.
84 this.scrollOnOutput = false;
85
rginda6d397402012-01-17 10:58:29 -080086 // Cursor position and attributes saved with DECSC.
87 this.savedOptions_ = {};
88
rginda8ba33642011-12-14 12:31:31 -080089 // The current mode bits for the terminal.
90 this.options_ = new hterm.Options();
91
92 // Timeouts we might need to clear.
93 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080094
95 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080096 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080097
98 // General IO interface that can be given to third parties without exposing
99 // the entire terminal object.
100 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800101
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400102 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800103 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800104};
105
106/**
107 * Create a new instance of a terminal command and run it with a given
108 * argument string.
109 *
110 * @param {function} commandClass The constructor for a terminal command.
111 * @param {string} argString The argument string to pass to the command.
112 */
113hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
114 var self = this;
115 this.command = new commandClass(
116 { argString: argString || '',
117 io: this.io.push(),
118 onExit: function(code) {
119 self.io.pop();
120 self.io.println(hterm.msg('COMMAND_COMPLETE',
121 [self.command.commandName, code]));
122 }
123 });
124
125 this.command.run();
126};
127
128/**
129 * Return a copy of the current cursor position.
130 *
131 * @return {hterm.RowCol} The RowCol object representing the current position.
132 */
133hterm.Terminal.prototype.saveCursor = function() {
134 return this.screen_.cursorPosition.clone();
135};
136
rgindaa19afe22012-01-25 15:40:22 -0800137hterm.Terminal.prototype.getTextAttributes = function() {
138 return this.screen_.textAttributes;
139};
140
rginda87b86462011-12-14 13:48:03 -0800141/**
142 * Restore a previously saved cursor position.
143 *
144 * @param {hterm.RowCol} cursor The position to restore.
145 */
146hterm.Terminal.prototype.restoreCursor = function(cursor) {
147 this.screen_.setCursorPosition(cursor.row, cursor.column);
rginda2312fff2012-01-05 16:20:52 -0800148 this.screen_.cursorPosition.overflow = cursor.overflow;
rginda87b86462011-12-14 13:48:03 -0800149};
150
151/**
152 * Set the width of the terminal, resizing the UI to match.
153 */
154hterm.Terminal.prototype.setWidth = function(columnCount) {
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400155 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px';
156 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800157 this.scheduleSyncCursorPosition_();
158};
rginda87b86462011-12-14 13:48:03 -0800159
rgindac9bc5502012-01-18 11:48:44 -0800160/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400161 * Deal with terminal size changes.
162 *
163 */
164hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
165 if (columnCount != this.screenSize.width)
166 this.realizeWidth_(columnCount);
167
168 if (rowCount != this.screenSize.height)
169 this.realizeHeight_(rowCount);
170
171 // Send new terminal size to plugin.
172 this.io.onTerminalResize(columnCount, rowCount);
173};
174
175/**
rgindac9bc5502012-01-18 11:48:44 -0800176 * Deal with terminal width changes.
177 *
178 * This function does what needs to be done when the terminal width changes
179 * out from under us. It happens here rather than in onResize_() because this
180 * code may need to run synchronously to handle programmatic changes of
181 * terminal width.
182 *
183 * Relying on the browser to send us an async resize event means we may not be
184 * in the correct state yet when the next escape sequence hits.
185 */
186hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
187 var deltaColumns = columnCount - this.screen_.getWidth();
188
rginda87b86462011-12-14 13:48:03 -0800189 this.screenSize.width = columnCount;
190 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800191
192 if (deltaColumns > 0) {
193 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
194 } else {
195 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
196 if (this.tabStops_[i] <= columnCount)
197 break;
198
199 this.tabStops_.pop();
200 }
201 }
202
203 this.screen_.setColumnCount(this.screenSize.width);
204};
205
206/**
207 * Deal with terminal height changes.
208 *
209 * This function does what needs to be done when the terminal height changes
210 * out from under us. It happens here rather than in onResize_() because this
211 * code may need to run synchronously to handle programmatic changes of
212 * terminal height.
213 *
214 * Relying on the browser to send us an async resize event means we may not be
215 * in the correct state yet when the next escape sequence hits.
216 */
217hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
218 var deltaRows = rowCount - this.screen_.getHeight();
219
220 this.screenSize.height = rowCount;
221
222 var cursor = this.saveCursor();
223
224 if (deltaRows < 0) {
225 // Screen got smaller.
226 deltaRows *= -1;
227 while (deltaRows) {
228 var lastRow = this.getRowCount() - 1;
229 if (lastRow - this.scrollbackRows_.length == cursor.row)
230 break;
231
232 if (this.getRowText(lastRow))
233 break;
234
235 this.screen_.popRow();
236 deltaRows--;
237 }
238
239 var ary = this.screen_.shiftRows(deltaRows);
240 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
241
242 // We just removed rows from the top of the screen, we need to update
243 // the cursor to match.
244 cursor.row -= deltaRows;
245
246 } else if (deltaRows > 0) {
247 // Screen got larger.
248
249 if (deltaRows <= this.scrollbackRows_.length) {
250 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
251 var rows = this.scrollbackRows_.splice(
252 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
253 this.screen_.unshiftRows(rows);
254 deltaRows -= scrollbackCount;
255 cursor.row += scrollbackCount;
256 }
257
258 if (deltaRows)
259 this.appendRows_(deltaRows);
260 }
261
262 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800263};
264
265/**
266 * Scroll the terminal to the top of the scrollback buffer.
267 */
268hterm.Terminal.prototype.scrollHome = function() {
269 this.scrollPort_.scrollRowToTop(0);
270};
271
272/**
273 * Scroll the terminal to the end.
274 */
275hterm.Terminal.prototype.scrollEnd = function() {
276 this.scrollPort_.scrollRowToBottom(this.getRowCount());
277};
278
279/**
280 * Scroll the terminal one page up (minus one line) relative to the current
281 * position.
282 */
283hterm.Terminal.prototype.scrollPageUp = function() {
284 var i = this.scrollPort_.getTopRowIndex();
285 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
286};
287
288/**
289 * Scroll the terminal one page down (minus one line) relative to the current
290 * position.
291 */
292hterm.Terminal.prototype.scrollPageDown = function() {
293 var i = this.scrollPort_.getTopRowIndex();
294 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800295};
296
rgindac9bc5502012-01-18 11:48:44 -0800297/**
298 * Full terminal reset.
299 */
rginda87b86462011-12-14 13:48:03 -0800300hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800301 this.clearAllTabStops();
302 this.setDefaultTabStops();
303 this.clearColorAndAttributes();
304 this.setVTScrollRegion(null, null);
305 this.clear();
306 this.setAbsoluteCursorPosition(0, 0);
307 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800308};
309
rgindac9bc5502012-01-18 11:48:44 -0800310/**
311 * Soft terminal reset.
312 */
rginda0f5c0292012-01-13 11:00:13 -0800313hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800314 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800315 this.setCursorVisible(true);
316 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800317};
318
rginda87b86462011-12-14 13:48:03 -0800319hterm.Terminal.prototype.clearColorAndAttributes = function() {
320 //console.log('clearColorAndAttributes');
321};
322
323hterm.Terminal.prototype.setForegroundColor256 = function() {
324 console.log('setForegroundColor256');
325};
326
327hterm.Terminal.prototype.setBackgroundColor256 = function() {
328 console.log('setBackgroundColor256');
329};
330
331hterm.Terminal.prototype.setForegroundColor = function() {
332 //console.log('setForegroundColor');
333};
334
335hterm.Terminal.prototype.setBackgroundColor = function() {
336 //console.log('setBackgroundColor');
337};
338
339hterm.Terminal.prototype.setAttributes = function() {
340 //console.log('setAttributes');
341};
342
343hterm.Terminal.prototype.resize = function() {
344 console.log('resize');
345};
346
rgindae4d29232012-01-19 10:47:13 -0800347hterm.Terminal.prototype.setCharacterSet = function() {
348 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800349};
350
rgindac9bc5502012-01-18 11:48:44 -0800351/**
352 * Move the cursor forward to the next tab stop, or to the last column
353 * if no more tab stops are set.
354 */
355hterm.Terminal.prototype.forwardTabStop = function() {
356 var column = this.screen_.cursorPosition.column;
357
358 for (var i = 0; i < this.tabStops_.length; i++) {
359 if (this.tabStops_[i] > column) {
360 this.setCursorColumn(this.tabStops_[i]);
361 return;
362 }
363 }
364
365 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800366};
367
rgindac9bc5502012-01-18 11:48:44 -0800368/**
369 * Move the cursor backward to the previous tab stop, or to the first column
370 * if no previous tab stops are set.
371 */
372hterm.Terminal.prototype.backwardTabStop = function() {
373 var column = this.screen_.cursorPosition.column;
374
375 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
376 if (this.tabStops_[i] < column) {
377 this.setCursorColumn(this.tabStops_[i]);
378 return;
379 }
380 }
381
382 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800383};
384
rgindac9bc5502012-01-18 11:48:44 -0800385/**
386 * Set a tab stop at the given column.
387 *
388 * @param {int} column Zero based column.
389 */
390hterm.Terminal.prototype.setTabStop = function(column) {
391 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
392 if (this.tabStops_[i] == column)
393 return;
394
395 if (this.tabStops_[i] < column) {
396 this.tabStops_.splice(i + 1, 0, column);
397 return;
398 }
399 }
400
401 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800402};
403
rgindac9bc5502012-01-18 11:48:44 -0800404/**
405 * Clear the tab stop at the current cursor position.
406 *
407 * No effect if there is no tab stop at the current cursor position.
408 */
409hterm.Terminal.prototype.clearTabStopAtCursor = function() {
410 var column = this.screen_.cursorPosition.column;
411
412 var i = this.tabStops_.indexOf(column);
413 if (i == -1)
414 return;
415
416 this.tabStops_.splice(i, 1);
417};
418
419/**
420 * Clear all tab stops.
421 */
422hterm.Terminal.prototype.clearAllTabStops = function() {
423 this.tabStops_.length = 0;
424};
425
426/**
427 * Set up the default tab stops, starting from a given column.
428 *
429 * This sets a tabstop every (column % this.tabWidth) column, starting
430 * from the specified column, or 0 if no column is provided.
431 *
432 * This does not clear the existing tab stops first, use clearAllTabStops
433 * for that.
434 *
435 * @param {int} opt_start Optional starting zero based starting column, useful
436 * for filling out missing tab stops when the terminal is resized.
437 */
438hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
439 var start = opt_start || 0;
440 var w = this.tabWidth;
441 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
442 for (var i = 0; i < stopCount; i++) {
443 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
444 }
rginda87b86462011-12-14 13:48:03 -0800445};
446
rginda6d397402012-01-17 10:58:29 -0800447/**
448 * Save cursor position and attributes.
449 *
450 * TODO(rginda): Save attributes once we support them.
451 */
rginda87b86462011-12-14 13:48:03 -0800452hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800453 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800454 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800455};
456
rginda6d397402012-01-17 10:58:29 -0800457/**
458 * Restore cursor position and attributes.
459 *
460 * TODO(rginda): Restore attributes once we support them.
461 */
rginda87b86462011-12-14 13:48:03 -0800462hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800463 if (this.savedOptions_.cursor)
464 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800465 if (this.savedOptions_.textAttributes)
466 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800467};
468
469/**
470 * Interpret a sequence of characters.
471 *
472 * Incomplete escape sequences are buffered until the next call.
473 *
474 * @param {string} str Sequence of characters to interpret or pass through.
475 */
476hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800477 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800478 this.scheduleSyncCursorPosition_();
479};
480
481/**
482 * Take over the given DIV for use as the terminal display.
483 *
484 * @param {HTMLDivElement} div The div to use as the terminal display.
485 */
486hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800487 this.div_ = div;
488
rginda8ba33642011-12-14 12:31:31 -0800489 this.scrollPort_.decorate(div);
rgindaa19afe22012-01-25 15:40:22 -0800490 this.scrollPort_.setFontFamily(this.defaultFontFamily_);
491
rginda8ba33642011-12-14 12:31:31 -0800492 this.document_ = this.scrollPort_.getDocument();
493
494 // Get character dimensions from the scrollPort.
495 this.characterSize_.height = this.scrollPort_.getRowHeight();
496 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
497
498 this.cursorNode_ = this.document_.createElement('div');
499 this.cursorNode_.style.cssText =
500 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800501 'top: -99px;' +
502 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800503 'width: ' + this.characterSize_.width + 'px;' +
504 'height: ' + this.characterSize_.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800505 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800506 'background-color: ' + this.cursorColor);
507 this.document_.body.appendChild(this.cursorNode_);
508
509 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800510
511 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
512
513 var link = this.document_.createElement('link');
514 link.setAttribute('href', '../css/dialogs.css');
515 link.setAttribute('rel', 'stylesheet');
516 this.document_.head.appendChild(link);
517
518 this.alertDialog = new AlertDialog(this.document_.body);
519 this.promptDialog = new PromptDialog(this.document_.body);
520 this.confirmDialog = new ConfirmDialog(this.document_.body);
521
522 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800523 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800524};
525
526hterm.Terminal.prototype.getDocument = function() {
527 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800528};
529
530/**
531 * Return the HTML Element for a given row index.
532 *
533 * This is a method from the RowProvider interface. The ScrollPort uses
534 * it to fetch rows on demand as they are scrolled into view.
535 *
536 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
537 * pairs to conserve memory.
538 *
539 * @param {integer} index The zero-based row index, measured relative to the
540 * start of the scrollback buffer. On-screen rows will always have the
541 * largest indicies.
542 * @return {HTMLElement} The 'x-row' element containing for the requested row.
543 */
544hterm.Terminal.prototype.getRowNode = function(index) {
545 if (index < this.scrollbackRows_.length)
546 return this.scrollbackRows_[index];
547
548 var screenIndex = index - this.scrollbackRows_.length;
549 return this.screen_.rowsArray[screenIndex];
550};
551
552/**
553 * Return the text content for a given range of rows.
554 *
555 * This is a method from the RowProvider interface. The ScrollPort uses
556 * it to fetch text content on demand when the user attempts to copy their
557 * selection to the clipboard.
558 *
559 * @param {integer} start The zero-based row index to start from, measured
560 * relative to the start of the scrollback buffer. On-screen rows will
561 * always have the largest indicies.
562 * @param {integer} end The zero-based row index to end on, measured
563 * relative to the start of the scrollback buffer.
564 * @return {string} A single string containing the text value of the range of
565 * rows. Lines will be newline delimited, with no trailing newline.
566 */
567hterm.Terminal.prototype.getRowsText = function(start, end) {
568 var ary = [];
569 for (var i = start; i < end; i++) {
570 var node = this.getRowNode(i);
571 ary.push(node.textContent);
572 }
573
574 return ary.join('\n');
575};
576
577/**
578 * Return the text content for a given row.
579 *
580 * This is a method from the RowProvider interface. The ScrollPort uses
581 * it to fetch text content on demand when the user attempts to copy their
582 * selection to the clipboard.
583 *
584 * @param {integer} index The zero-based row index to return, measured
585 * relative to the start of the scrollback buffer. On-screen rows will
586 * always have the largest indicies.
587 * @return {string} A string containing the text value of the selected row.
588 */
589hterm.Terminal.prototype.getRowText = function(index) {
590 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800591 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800592};
593
594/**
595 * Return the total number of rows in the addressable screen and in the
596 * scrollback buffer of this terminal.
597 *
598 * This is a method from the RowProvider interface. The ScrollPort uses
599 * it to compute the size of the scrollbar.
600 *
601 * @return {integer} The number of rows in this terminal.
602 */
603hterm.Terminal.prototype.getRowCount = function() {
604 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
605};
606
607/**
608 * Create DOM nodes for new rows and append them to the end of the terminal.
609 *
610 * This is the only correct way to add a new DOM node for a row. Notice that
611 * the new row is appended to the bottom of the list of rows, and does not
612 * require renumbering (of the rowIndex property) of previous rows.
613 *
614 * If you think you want a new blank row somewhere in the middle of the
615 * terminal, look into moveRows_().
616 *
617 * This method does not pay attention to vtScrollTop/Bottom, since you should
618 * be using moveRows() in cases where they would matter.
619 *
620 * The cursor will be positioned at column 0 of the first inserted line.
621 */
622hterm.Terminal.prototype.appendRows_ = function(count) {
623 var cursorRow = this.screen_.rowsArray.length;
624 var offset = this.scrollbackRows_.length + cursorRow;
625 for (var i = 0; i < count; i++) {
626 var row = this.document_.createElement('x-row');
627 row.appendChild(this.document_.createTextNode(''));
628 row.rowIndex = offset + i;
629 this.screen_.pushRow(row);
630 }
631
632 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
633 if (extraRows > 0) {
634 var ary = this.screen_.shiftRows(extraRows);
635 Array.prototype.push.apply(this.scrollbackRows_, ary);
636 this.scheduleScrollDown_();
637 }
638
639 if (cursorRow >= this.screen_.rowsArray.length)
640 cursorRow = this.screen_.rowsArray.length - 1;
641
rginda87b86462011-12-14 13:48:03 -0800642 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800643};
644
645/**
646 * Relocate rows from one part of the addressable screen to another.
647 *
648 * This is used to recycle rows during VT scrolls (those which are driven
649 * by VT commands, rather than by the user manipulating the scrollbar.)
650 *
651 * In this case, the blank lines scrolled into the scroll region are made of
652 * the nodes we scrolled off. These have their rowIndex properties carefully
653 * renumbered so as not to confuse the ScrollPort.
654 *
655 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
656 * It may just be luck. I wouldn't be surprised if we actually needed to call
657 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
658 * adding it.
659 */
660hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
661 var ary = this.screen_.removeRows(fromIndex, count);
662 this.screen_.insertRows(toIndex, ary);
663
664 var start, end;
665 if (fromIndex < toIndex) {
666 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800667 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800668 } else {
669 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800670 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800671 }
672
673 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800674 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800675};
676
677/**
678 * Renumber the rowIndex property of the given range of rows.
679 *
680 * The start and end indicies are relative to the screen, not the scrollback.
681 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800682 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800683 * no need to renumber scrollback rows.
684 */
685hterm.Terminal.prototype.renumberRows_ = function(start, end) {
686 var offset = this.scrollbackRows_.length;
687 for (var i = start; i < end; i++) {
688 this.screen_.rowsArray[i].rowIndex = offset + i;
689 }
690};
691
692/**
693 * Print a string to the terminal.
694 *
695 * This respects the current insert and wraparound modes. It will add new lines
696 * to the end of the terminal, scrolling off the top into the scrollback buffer
697 * if necessary.
698 *
699 * The string is *not* parsed for escape codes. Use the interpret() method if
700 * that's what you're after.
701 *
702 * @param{string} str The string to print.
703 */
704hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800705 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
706 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800707
rgindaa19afe22012-01-25 15:40:22 -0800708 if (this.options_.insertMode) {
709 this.screen_.insertString(str);
710 } else {
711 this.screen_.overwriteString(str);
712 }
713
714 var overflow = this.screen_.maybeClipCurrentRow();
715
716 if (this.options_.wraparound && overflow) {
717 var lastColumn;
718
719 do {
720 if (this.screen_.cursorPosition.overflow)
721 this.newLine();
722
723 if (!this.options_.insertMode)
724 this.screen_.deleteChars(overflow.characterLength);
725
726 this.screen_.prependNodes(overflow);
727 lastColumn = overflow.characterCount;
728
729 overflow = this.screen_.maybeClipCurrentRow();
730 } while (overflow);
731
732 this.setCursorColumn(lastColumn);
733 }
rginda8ba33642011-12-14 12:31:31 -0800734
735 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800736
737 if (this.scrollOnOutput)
738 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800739};
740
741/**
rginda87b86462011-12-14 13:48:03 -0800742 * Set the VT scroll region.
743 *
rginda87b86462011-12-14 13:48:03 -0800744 * This also resets the cursor position to the absolute (0, 0) position, since
745 * that's what xterm appears to do.
746 *
747 * @param {integer} scrollTop The zero-based top of the scroll region.
748 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
749 * inclusive.
750 */
751hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
752 this.vtScrollTop_ = scrollTop;
753 this.vtScrollBottom_ = scrollBottom;
754 this.setAbsoluteCursorPosition(0, 0);
755};
756
757/**
rginda8ba33642011-12-14 12:31:31 -0800758 * Return the top row index according to the VT.
759 *
760 * This will return 0 unless the terminal has been told to restrict scrolling
761 * to some lower row. It is used for some VT cursor positioning and scrolling
762 * commands.
763 *
764 * @return {integer} The topmost row in the terminal's scroll region.
765 */
766hterm.Terminal.prototype.getVTScrollTop = function() {
767 if (this.vtScrollTop_ != null)
768 return this.vtScrollTop_;
769
770 return 0;
rginda87b86462011-12-14 13:48:03 -0800771};
rginda8ba33642011-12-14 12:31:31 -0800772
773/**
774 * Return the bottom row index according to the VT.
775 *
776 * This will return the height of the terminal unless the it has been told to
777 * restrict scrolling to some higher row. It is used for some VT cursor
778 * positioning and scrolling commands.
779 *
780 * @return {integer} The bottommost row in the terminal's scroll region.
781 */
782hterm.Terminal.prototype.getVTScrollBottom = function() {
783 if (this.vtScrollBottom_ != null)
784 return this.vtScrollBottom_;
785
rginda87b86462011-12-14 13:48:03 -0800786 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800787}
788
789/**
790 * Process a '\n' character.
791 *
792 * If the cursor is on the final row of the terminal this will append a new
793 * blank row to the screen and scroll the topmost row into the scrollback
794 * buffer.
795 *
796 * Otherwise, this moves the cursor to column zero of the next row.
797 */
798hterm.Terminal.prototype.newLine = function() {
799 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800800 // If we're at the end of the screen we need to append a new line and
801 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800802 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800803 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
804 // End of the scroll region does not affect the scrollback buffer.
805 this.vtScrollUp(1);
806 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800807 } else {
rginda87b86462011-12-14 13:48:03 -0800808 // Anywhere else in the screen just moves the cursor.
809 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800810 }
811};
812
813/**
814 * Like newLine(), except maintain the cursor column.
815 */
816hterm.Terminal.prototype.lineFeed = function() {
817 var column = this.screen_.cursorPosition.column;
818 this.newLine();
819 this.setCursorColumn(column);
820};
821
822/**
rginda87b86462011-12-14 13:48:03 -0800823 * If autoCarriageReturn is set then newLine(), else lineFeed().
824 */
825hterm.Terminal.prototype.formFeed = function() {
826 if (this.options_.autoCarriageReturn) {
827 this.newLine();
828 } else {
829 this.lineFeed();
830 }
831};
832
833/**
834 * Move the cursor up one row, possibly inserting a blank line.
835 *
836 * The cursor column is not changed.
837 */
838hterm.Terminal.prototype.reverseLineFeed = function() {
839 var scrollTop = this.getVTScrollTop();
840 var currentRow = this.screen_.cursorPosition.row;
841
842 if (currentRow == scrollTop) {
843 this.insertLines(1);
844 } else {
845 this.setAbsoluteCursorRow(currentRow - 1);
846 }
847};
848
849/**
rginda8ba33642011-12-14 12:31:31 -0800850 * Replace all characters to the left of the current cursor with the space
851 * character.
852 *
853 * TODO(rginda): This should probably *remove* the characters (not just replace
854 * with a space) if there are no characters at or beyond the current cursor
855 * position. Once it does that, it'll have the same text-attribute related
856 * issues as hterm.Screen.prototype.clearCursorRow :/
857 */
858hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800859 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800860 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800861 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
862 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800863};
864
865/**
866 * Erase a given number of characters to the right of the cursor, shifting
867 * remaining characters to the left.
868 *
869 * The cursor position is unchanged.
870 *
871 * TODO(rginda): Test that this works even when the cursor is positioned beyond
872 * the end of the text.
873 *
874 * TODO(rginda): This likely has text-attribute related troubles similar to the
875 * todo on hterm.Screen.prototype.clearCursorRow.
876 */
877hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800878 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800879
rginda87b86462011-12-14 13:48:03 -0800880 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800881 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
882 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800883 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800884};
885
886/**
887 * Erase the current line.
888 *
889 * The cursor position is unchanged.
890 *
891 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
892 * has a text-attribute related TODO.
893 */
894hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800895 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800896 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800897 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800898};
899
900/**
901 * Erase all characters from the start of the scroll region to the current
902 * cursor position.
903 *
904 * The cursor position is unchanged.
905 *
906 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
907 * has a text-attribute related TODO.
908 */
909hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800910 var cursor = this.saveCursor();
911
912 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800913
914 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800915 for (var i = top; i < cursor.row; i++) {
916 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800917 this.screen_.clearCursorRow();
918 }
919
rginda87b86462011-12-14 13:48:03 -0800920 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800921};
922
923/**
924 * Erase all characters from the current cursor position to the end of the
925 * scroll region.
926 *
927 * The cursor position is unchanged.
928 *
929 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
930 * has a text-attribute related TODO.
931 */
932hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800933 var cursor = this.saveCursor();
934
935 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800936
937 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800938 for (var i = cursor.row + 1; i <= bottom; i++) {
939 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800940 this.screen_.clearCursorRow();
941 }
942
rginda87b86462011-12-14 13:48:03 -0800943 this.restoreCursor(cursor);
944};
945
946/**
947 * Fill the terminal with a given character.
948 *
949 * This methods does not respect the VT scroll region.
950 *
951 * @param {string} ch The character to use for the fill.
952 */
953hterm.Terminal.prototype.fill = function(ch) {
954 var cursor = this.saveCursor();
955
956 this.setAbsoluteCursorPosition(0, 0);
957 for (var row = 0; row < this.screenSize.height; row++) {
958 for (var col = 0; col < this.screenSize.width; col++) {
959 this.setAbsoluteCursorPosition(row, col);
960 this.screen_.overwriteString(ch);
961 }
962 }
963
964 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800965};
966
967/**
rgindae4d29232012-01-19 10:47:13 -0800968 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -0800969 *
rgindae4d29232012-01-19 10:47:13 -0800970 * The cursor position is unchanged. This does not respect the scroll
971 * region.
rginda8ba33642011-12-14 12:31:31 -0800972 *
973 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
974 * has a text-attribute related TODO.
975 */
976hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800977 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800978
rgindae4d29232012-01-19 10:47:13 -0800979 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -0800980
rgindae4d29232012-01-19 10:47:13 -0800981 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800982 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800983 this.screen_.clearCursorRow();
984 }
985
rginda87b86462011-12-14 13:48:03 -0800986 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800987};
988
989/**
990 * VT command to insert lines at the current cursor row.
991 *
992 * This respects the current scroll region. Rows pushed off the bottom are
993 * lost (they won't show up in the scrollback buffer).
994 *
995 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
996 * has a text-attribute related TODO.
997 *
998 * @param {integer} count The number of lines to insert.
999 */
1000hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001001 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001002
1003 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001004 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001005
rgindae4d29232012-01-19 10:47:13 -08001006 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001007 if (start != cursor.row)
1008 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001009
1010 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001011 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001012 this.screen_.clearCursorRow();
1013 }
1014
rginda87b86462011-12-14 13:48:03 -08001015 cursor.column = 0;
1016 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001017};
1018
1019/**
1020 * VT command to delete lines at the current cursor row.
1021 *
1022 * New rows are added to the bottom of scroll region to take their place. New
1023 * rows are strictly there to take up space and have no content or style.
1024 */
1025hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001026 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001027
rginda87b86462011-12-14 13:48:03 -08001028 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001029 var bottom = this.getVTScrollBottom();
1030
rginda87b86462011-12-14 13:48:03 -08001031 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001032 count = Math.min(count, maxCount);
1033
rginda87b86462011-12-14 13:48:03 -08001034 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001035 if (count != maxCount)
1036 this.moveRows_(top, count, moveStart);
1037
1038 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001039 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001040 this.screen_.clearCursorRow();
1041 }
1042
rginda87b86462011-12-14 13:48:03 -08001043 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001044};
1045
1046/**
1047 * Inserts the given number of spaces at the current cursor position.
1048 *
rginda87b86462011-12-14 13:48:03 -08001049 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001050 */
1051hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001052 var cursor = this.saveCursor();
1053
rginda0f5c0292012-01-13 11:00:13 -08001054 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001055 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001056 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001057
1058 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001059};
1060
1061/**
1062 * Forward-delete the specified number of characters starting at the cursor
1063 * position.
1064 *
1065 * @param {integer} count The number of characters to delete.
1066 */
1067hterm.Terminal.prototype.deleteChars = function(count) {
1068 this.screen_.deleteChars(count);
1069};
1070
1071/**
1072 * Shift rows in the scroll region upwards by a given number of lines.
1073 *
1074 * New rows are inserted at the bottom of the scroll region to fill the
1075 * vacated rows. The new rows not filled out with the current text attributes.
1076 *
1077 * This function does not affect the scrollback rows at all. Rows shifted
1078 * off the top are lost.
1079 *
rginda87b86462011-12-14 13:48:03 -08001080 * The cursor position is not altered.
1081 *
rginda8ba33642011-12-14 12:31:31 -08001082 * @param {integer} count The number of rows to scroll.
1083 */
1084hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001085 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001086
rginda87b86462011-12-14 13:48:03 -08001087 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001088 this.deleteLines(count);
1089
rginda87b86462011-12-14 13:48:03 -08001090 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001091};
1092
1093/**
1094 * Shift rows below the cursor down by a given number of lines.
1095 *
1096 * This function respects the current scroll region.
1097 *
1098 * New rows are inserted at the top of the scroll region to fill the
1099 * vacated rows. The new rows not filled out with the current text attributes.
1100 *
1101 * This function does not affect the scrollback rows at all. Rows shifted
1102 * off the bottom are lost.
1103 *
1104 * @param {integer} count The number of rows to scroll.
1105 */
1106hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001107 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001108
rginda87b86462011-12-14 13:48:03 -08001109 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001110 this.insertLines(opt_count);
1111
rginda87b86462011-12-14 13:48:03 -08001112 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001113};
1114
rginda87b86462011-12-14 13:48:03 -08001115
rginda8ba33642011-12-14 12:31:31 -08001116/**
1117 * Set the cursor position.
1118 *
1119 * The cursor row is relative to the scroll region if the terminal has
1120 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1121 *
1122 * @param {integer} row The new zero-based cursor row.
1123 * @param {integer} row The new zero-based cursor column.
1124 */
1125hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1126 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001127 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001128 } else {
rginda87b86462011-12-14 13:48:03 -08001129 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001130 }
rginda87b86462011-12-14 13:48:03 -08001131};
rginda8ba33642011-12-14 12:31:31 -08001132
rginda87b86462011-12-14 13:48:03 -08001133hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1134 var scrollTop = this.getVTScrollTop();
1135 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001136 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001137 this.screen_.setCursorPosition(row, column);
1138};
1139
1140hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001141 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1142 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001143 this.screen_.setCursorPosition(row, column);
1144};
1145
1146/**
1147 * Set the cursor column.
1148 *
1149 * @param {integer} column The new zero-based cursor column.
1150 */
1151hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001152 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001153};
1154
1155/**
1156 * Return the cursor column.
1157 *
1158 * @return {integer} The zero-based cursor column.
1159 */
1160hterm.Terminal.prototype.getCursorColumn = function() {
1161 return this.screen_.cursorPosition.column;
1162};
1163
1164/**
1165 * Set the cursor row.
1166 *
1167 * The cursor row is relative to the scroll region if the terminal has
1168 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1169 *
1170 * @param {integer} row The new cursor row.
1171 */
rginda87b86462011-12-14 13:48:03 -08001172hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1173 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001174};
1175
1176/**
1177 * Return the cursor row.
1178 *
1179 * @return {integer} The zero-based cursor row.
1180 */
1181hterm.Terminal.prototype.getCursorRow = function(row) {
1182 return this.screen_.cursorPosition.row;
1183};
1184
1185/**
1186 * Request that the ScrollPort redraw itself soon.
1187 *
1188 * The redraw will happen asynchronously, soon after the call stack winds down.
1189 * Multiple calls will be coalesced into a single redraw.
1190 */
1191hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001192 if (this.timeouts_.redraw)
1193 return;
rginda8ba33642011-12-14 12:31:31 -08001194
1195 var self = this;
rginda87b86462011-12-14 13:48:03 -08001196 this.timeouts_.redraw = setTimeout(function() {
1197 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001198 self.scrollPort_.redraw_();
1199 }, 0);
1200};
1201
1202/**
1203 * Request that the ScrollPort be scrolled to the bottom.
1204 *
1205 * The scroll will happen asynchronously, soon after the call stack winds down.
1206 * Multiple calls will be coalesced into a single scroll.
1207 *
1208 * This affects the scrollbar position of the ScrollPort, and has nothing to
1209 * do with the VT scroll commands.
1210 */
1211hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1212 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001213 return;
rginda8ba33642011-12-14 12:31:31 -08001214
1215 var self = this;
1216 this.timeouts_.scrollDown = setTimeout(function() {
1217 delete self.timeouts_.scrollDown;
1218 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1219 }, 10);
1220};
1221
1222/**
1223 * Move the cursor up a specified number of rows.
1224 *
1225 * @param {integer} count The number of rows to move the cursor.
1226 */
1227hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001228 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001229};
1230
1231/**
1232 * Move the cursor down a specified number of rows.
1233 *
1234 * @param {integer} count The number of rows to move the cursor.
1235 */
1236hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001237 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001238 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1239 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1240 this.screenSize.height - 1);
1241
1242 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1243 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001244 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001245};
1246
1247/**
1248 * Move the cursor left a specified number of columns.
1249 *
1250 * @param {integer} count The number of columns to move the cursor.
1251 */
1252hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001253 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001254};
1255
1256/**
1257 * Move the cursor right a specified number of columns.
1258 *
1259 * @param {integer} count The number of columns to move the cursor.
1260 */
1261hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001262 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001263 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001264 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001265 this.setCursorColumn(column);
1266};
1267
1268/**
1269 * Reverse the foreground and background colors of the terminal.
1270 *
1271 * This only affects text that was drawn with no attributes.
1272 *
1273 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1274 * been drawn with attributes that happen to coincide with the default
1275 * 'no-attribute' colors. My guess is probably not.
1276 */
1277hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001278 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001279 if (state) {
1280 this.scrollPort_.setForegroundColor(this.backgroundColor);
1281 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1282 } else {
1283 this.scrollPort_.setForegroundColor(this.foregroundColor);
1284 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1285 }
1286};
1287
1288/**
rginda87b86462011-12-14 13:48:03 -08001289 * Ring the terminal bell.
1290 *
1291 * We only have a visual bell, which quickly toggles inverse video in the
1292 * terminal.
1293 */
1294hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001295 this.cursorNode_.style.backgroundColor =
1296 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001297
1298 var self = this;
1299 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001300 self.cursorNode_.style.backgroundColor = self.cursorColor;
1301 }, 200);
rginda87b86462011-12-14 13:48:03 -08001302};
1303
1304/**
rginda8ba33642011-12-14 12:31:31 -08001305 * Set the origin mode bit.
1306 *
1307 * If origin mode is on, certain VT cursor and scrolling commands measure their
1308 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1309 * to the top of the addressable screen.
1310 *
1311 * Defaults to off.
1312 *
1313 * @param {boolean} state True to set origin mode, false to unset.
1314 */
1315hterm.Terminal.prototype.setOriginMode = function(state) {
1316 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001317 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001318};
1319
1320/**
1321 * Set the insert mode bit.
1322 *
1323 * If insert mode is on, existing text beyond the cursor position will be
1324 * shifted right to make room for new text. Otherwise, new text overwrites
1325 * any existing text.
1326 *
1327 * Defaults to off.
1328 *
1329 * @param {boolean} state True to set insert mode, false to unset.
1330 */
1331hterm.Terminal.prototype.setInsertMode = function(state) {
1332 this.options_.insertMode = state;
1333};
1334
1335/**
rginda87b86462011-12-14 13:48:03 -08001336 * Set the auto carriage return bit.
1337 *
1338 * If auto carriage return is on then a formfeed character is interpreted
1339 * as a newline, otherwise it's the same as a linefeed. The difference boils
1340 * down to whether or not the cursor column is reset.
1341 */
1342hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1343 this.options_.autoCarriageReturn = state;
1344};
1345
1346/**
rginda8ba33642011-12-14 12:31:31 -08001347 * Set the wraparound mode bit.
1348 *
1349 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1350 * to the start of the following row. Otherwise, the cursor is clamped to the
1351 * end of the screen and attempts to write past it are ignored.
1352 *
1353 * Defaults to on.
1354 *
1355 * @param {boolean} state True to set wraparound mode, false to unset.
1356 */
1357hterm.Terminal.prototype.setWraparound = function(state) {
1358 this.options_.wraparound = state;
1359};
1360
1361/**
1362 * Set the reverse-wraparound mode bit.
1363 *
1364 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1365 * to the end of the previous row. Otherwise, the cursor is clamped to column
1366 * 0.
1367 *
1368 * Defaults to off.
1369 *
1370 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1371 */
1372hterm.Terminal.prototype.setReverseWraparound = function(state) {
1373 this.options_.reverseWraparound = state;
1374};
1375
1376/**
1377 * Selects between the primary and alternate screens.
1378 *
1379 * If alternate mode is on, the alternate screen is active. Otherwise the
1380 * primary screen is active.
1381 *
1382 * Swapping screens has no effect on the scrollback buffer.
1383 *
1384 * Each screen maintains its own cursor position.
1385 *
1386 * Defaults to off.
1387 *
1388 * @param {boolean} state True to set alternate mode, false to unset.
1389 */
1390hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001391 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001392 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1393
1394 this.screen_.setColumnCount(this.screenSize.width);
1395
1396 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1397 if (rowDelta > 0)
1398 this.appendRows_(rowDelta);
1399
rginda6d397402012-01-17 10:58:29 -08001400 this.restoreCursor(cursor);
1401
rginda2312fff2012-01-05 16:20:52 -08001402 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001403 this.syncCursorPosition_();
1404};
1405
1406/**
1407 * Set the cursor-blink mode bit.
1408 *
1409 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1410 * a visible cursor does not blink.
1411 *
1412 * You should make sure to turn blinking off if you're going to dispose of a
1413 * terminal, otherwise you'll leak a timeout.
1414 *
1415 * Defaults to on.
1416 *
1417 * @param {boolean} state True to set cursor-blink mode, false to unset.
1418 */
1419hterm.Terminal.prototype.setCursorBlink = function(state) {
1420 this.options_.cursorBlink = state;
1421
1422 if (!state && this.timeouts_.cursorBlink) {
1423 clearTimeout(this.timeouts_.cursorBlink);
1424 delete this.timeouts_.cursorBlink;
1425 }
1426
1427 if (this.options_.cursorVisible)
1428 this.setCursorVisible(true);
1429};
1430
1431/**
1432 * Set the cursor-visible mode bit.
1433 *
1434 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1435 *
1436 * Defaults to on.
1437 *
1438 * @param {boolean} state True to set cursor-visible mode, false to unset.
1439 */
1440hterm.Terminal.prototype.setCursorVisible = function(state) {
1441 this.options_.cursorVisible = state;
1442
1443 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001444 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001445 return;
1446 }
1447
rginda87b86462011-12-14 13:48:03 -08001448 this.syncCursorPosition_();
1449
1450 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001451
1452 if (this.options_.cursorBlink) {
1453 if (this.timeouts_.cursorBlink)
1454 return;
1455
1456 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1457 500);
1458 } else {
1459 if (this.timeouts_.cursorBlink) {
1460 clearTimeout(this.timeouts_.cursorBlink);
1461 delete this.timeouts_.cursorBlink;
1462 }
1463 }
1464};
1465
1466/**
rginda87b86462011-12-14 13:48:03 -08001467 * Synchronizes the visible cursor and document selection with the current
1468 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001469 */
1470hterm.Terminal.prototype.syncCursorPosition_ = function() {
1471 var topRowIndex = this.scrollPort_.getTopRowIndex();
1472 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1473 var cursorRowIndex = this.scrollbackRows_.length +
1474 this.screen_.cursorPosition.row;
1475
1476 if (cursorRowIndex > bottomRowIndex) {
1477 // Cursor is scrolled off screen, move it outside of the visible area.
1478 this.cursorNode_.style.top = -this.characterSize_.height;
1479 return;
1480 }
1481
1482 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1483 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1484 this.cursorNode_.style.left = this.characterSize_.width *
1485 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001486
1487 this.cursorNode_.setAttribute('title',
1488 '(' + this.screen_.cursorPosition.row +
1489 ', ' + this.screen_.cursorPosition.column +
1490 ')');
1491
1492 // Update the caret for a11y purposes.
1493 var selection = this.document_.getSelection();
1494 if (selection && selection.isCollapsed)
1495 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001496};
1497
1498/**
1499 * Synchronizes the visible cursor with the current cursor coordinates.
1500 *
1501 * The sync will happen asynchronously, soon after the call stack winds down.
1502 * Multiple calls will be coalesced into a single sync.
1503 */
1504hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1505 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001506 return;
rginda8ba33642011-12-14 12:31:31 -08001507
1508 var self = this;
1509 this.timeouts_.syncCursor = setTimeout(function() {
1510 self.syncCursorPosition_();
1511 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001512 }, 0);
1513};
1514
1515/**
1516 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1517 *
1518 * @param {string} string The VT string representing the keystroke.
1519 */
1520hterm.Terminal.prototype.onVTKeystroke = function(string) {
1521 if (this.scrollOnKeystroke)
1522 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1523
1524 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001525};
1526
1527/**
1528 * React when the ScrollPort is scrolled.
1529 */
1530hterm.Terminal.prototype.onScroll_ = function() {
1531 this.scheduleSyncCursorPosition_();
1532};
1533
1534/**
1535 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001536 *
1537 * Note: This function should not directly contain code that alters the internal
1538 * state of the terminal. That kind of code belongs in realizeWidth or
1539 * realizeHeight, so that it can be executed synchronously in the case of a
1540 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001541 */
1542hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001543 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
1544 this.characterSize_.width);
rgindac9bc5502012-01-18 11:48:44 -08001545 var rowCount = this.scrollPort_.visibleRowCount;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001546 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001547 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001548};
1549
1550/**
1551 * Service the cursor blink timeout.
1552 */
1553hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001554 if (this.cursorNode_.style.opacity == '0') {
1555 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001556 } else {
rginda87b86462011-12-14 13:48:03 -08001557 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001558 }
1559};