blob: 9966452e491e0f17a8ccddbfcf263bfb67183ef9 [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));
rginda9846e2f2012-01-27 13:53:33 -080043 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080044
rginda87b86462011-12-14 13:48:03 -080045 // The div that contains this terminal.
46 this.div_ = null;
47
rgindac9bc5502012-01-18 11:48:44 -080048 // The document that contains the scrollPort. Defaulted to the global
49 // document here so that the terminal is functional even if it hasn't been
50 // inserted into a document yet, but re-set in decorate().
51 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080052
rginda8ba33642011-12-14 12:31:31 -080053 // The rows that have scrolled off screen and are no longer addressable.
54 this.scrollbackRows_ = [];
55
rgindac9bc5502012-01-18 11:48:44 -080056 // Saved tab stops.
57 this.tabStops_ = [];
58
rginda8ba33642011-12-14 12:31:31 -080059 // The VT's notion of the top and bottom rows. Used during some VT
60 // cursor positioning and scrolling commands.
61 this.vtScrollTop_ = null;
62 this.vtScrollBottom_ = null;
63
64 // The DIV element for the visible cursor.
65 this.cursorNode_ = null;
66
rgindaa19afe22012-01-25 15:40:22 -080067 // Default font family for the terminal text.
68 this.defaultFontFamily_ = '"DejaVu Sans Mono", "Everson Mono", FreeMono, ' +
69 '"Andale Mono", "Lucida Console", monospace'
70
rginda8ba33642011-12-14 12:31:31 -080071 // The default colors for text with no other color attributes.
72 this.backgroundColor = 'black';
73 this.foregroundColor = 'white';
74
rgindac9bc5502012-01-18 11:48:44 -080075 // Default tab with of 8 to match xterm.
76 this.tabWidth = 8;
77
rgindaa19afe22012-01-25 15:40:22 -080078 // The color of the visible cursor.
rginda8ba33642011-12-14 12:31:31 -080079 this.cursorColor = 'rgba(255,0,0,0.5)';
80
rginda87b86462011-12-14 13:48:03 -080081 // If true, scroll to the bottom on any keystroke.
82 this.scrollOnKeystroke = true;
83
rginda0f5c0292012-01-13 11:00:13 -080084 // If true, scroll to the bottom on terminal output.
85 this.scrollOnOutput = false;
86
rginda6d397402012-01-17 10:58:29 -080087 // Cursor position and attributes saved with DECSC.
88 this.savedOptions_ = {};
89
rginda8ba33642011-12-14 12:31:31 -080090 // The current mode bits for the terminal.
91 this.options_ = new hterm.Options();
92
93 // Timeouts we might need to clear.
94 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080095
96 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080097 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080098
99 // General IO interface that can be given to third parties without exposing
100 // the entire terminal object.
101 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800102
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400103 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800104 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800105};
106
107/**
108 * Create a new instance of a terminal command and run it with a given
109 * argument string.
110 *
111 * @param {function} commandClass The constructor for a terminal command.
112 * @param {string} argString The argument string to pass to the command.
113 */
114hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
115 var self = this;
116 this.command = new commandClass(
117 { argString: argString || '',
118 io: this.io.push(),
119 onExit: function(code) {
120 self.io.pop();
121 self.io.println(hterm.msg('COMMAND_COMPLETE',
122 [self.command.commandName, code]));
rginda9846e2f2012-01-27 13:53:33 -0800123 self.vt.keyboard.installKeyboard(null);
rginda87b86462011-12-14 13:48:03 -0800124 }
125 });
126
rginda9846e2f2012-01-27 13:53:33 -0800127 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
rginda87b86462011-12-14 13:48:03 -0800128 this.command.run();
129};
130
131/**
132 * Return a copy of the current cursor position.
133 *
134 * @return {hterm.RowCol} The RowCol object representing the current position.
135 */
136hterm.Terminal.prototype.saveCursor = function() {
137 return this.screen_.cursorPosition.clone();
138};
139
rgindaa19afe22012-01-25 15:40:22 -0800140hterm.Terminal.prototype.getTextAttributes = function() {
141 return this.screen_.textAttributes;
142};
143
rginda87b86462011-12-14 13:48:03 -0800144/**
rginda9846e2f2012-01-27 13:53:33 -0800145 * Change the title of this terminal's window.
146 */
147hterm.Terminal.prototype.setWindowTitle = function(title) {
148 this.document_.title = title;
149};
150
151/**
rginda87b86462011-12-14 13:48:03 -0800152 * Restore a previously saved cursor position.
153 *
154 * @param {hterm.RowCol} cursor The position to restore.
155 */
156hterm.Terminal.prototype.restoreCursor = function(cursor) {
157 this.screen_.setCursorPosition(cursor.row, cursor.column);
rginda2312fff2012-01-05 16:20:52 -0800158 this.screen_.cursorPosition.overflow = cursor.overflow;
rginda87b86462011-12-14 13:48:03 -0800159};
160
161/**
162 * Set the width of the terminal, resizing the UI to match.
163 */
164hterm.Terminal.prototype.setWidth = function(columnCount) {
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400165 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px';
166 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800167 this.scheduleSyncCursorPosition_();
168};
rginda87b86462011-12-14 13:48:03 -0800169
rgindac9bc5502012-01-18 11:48:44 -0800170/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400171 * Deal with terminal size changes.
172 *
173 */
174hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
175 if (columnCount != this.screenSize.width)
176 this.realizeWidth_(columnCount);
177
178 if (rowCount != this.screenSize.height)
179 this.realizeHeight_(rowCount);
180
181 // Send new terminal size to plugin.
182 this.io.onTerminalResize(columnCount, rowCount);
183};
184
185/**
rgindac9bc5502012-01-18 11:48:44 -0800186 * Deal with terminal width changes.
187 *
188 * This function does what needs to be done when the terminal width changes
189 * out from under us. It happens here rather than in onResize_() because this
190 * code may need to run synchronously to handle programmatic changes of
191 * terminal width.
192 *
193 * Relying on the browser to send us an async resize event means we may not be
194 * in the correct state yet when the next escape sequence hits.
195 */
196hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
197 var deltaColumns = columnCount - this.screen_.getWidth();
198
rginda87b86462011-12-14 13:48:03 -0800199 this.screenSize.width = columnCount;
200 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800201
202 if (deltaColumns > 0) {
203 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
204 } else {
205 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
206 if (this.tabStops_[i] <= columnCount)
207 break;
208
209 this.tabStops_.pop();
210 }
211 }
212
213 this.screen_.setColumnCount(this.screenSize.width);
214};
215
216/**
217 * Deal with terminal height changes.
218 *
219 * This function does what needs to be done when the terminal height changes
220 * out from under us. It happens here rather than in onResize_() because this
221 * code may need to run synchronously to handle programmatic changes of
222 * terminal height.
223 *
224 * Relying on the browser to send us an async resize event means we may not be
225 * in the correct state yet when the next escape sequence hits.
226 */
227hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
228 var deltaRows = rowCount - this.screen_.getHeight();
229
230 this.screenSize.height = rowCount;
231
232 var cursor = this.saveCursor();
233
234 if (deltaRows < 0) {
235 // Screen got smaller.
236 deltaRows *= -1;
237 while (deltaRows) {
238 var lastRow = this.getRowCount() - 1;
239 if (lastRow - this.scrollbackRows_.length == cursor.row)
240 break;
241
242 if (this.getRowText(lastRow))
243 break;
244
245 this.screen_.popRow();
246 deltaRows--;
247 }
248
249 var ary = this.screen_.shiftRows(deltaRows);
250 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
251
252 // We just removed rows from the top of the screen, we need to update
253 // the cursor to match.
254 cursor.row -= deltaRows;
255
256 } else if (deltaRows > 0) {
257 // Screen got larger.
258
259 if (deltaRows <= this.scrollbackRows_.length) {
260 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
261 var rows = this.scrollbackRows_.splice(
262 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
263 this.screen_.unshiftRows(rows);
264 deltaRows -= scrollbackCount;
265 cursor.row += scrollbackCount;
266 }
267
268 if (deltaRows)
269 this.appendRows_(deltaRows);
270 }
271
272 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800273};
274
275/**
276 * Scroll the terminal to the top of the scrollback buffer.
277 */
278hterm.Terminal.prototype.scrollHome = function() {
279 this.scrollPort_.scrollRowToTop(0);
280};
281
282/**
283 * Scroll the terminal to the end.
284 */
285hterm.Terminal.prototype.scrollEnd = function() {
286 this.scrollPort_.scrollRowToBottom(this.getRowCount());
287};
288
289/**
290 * Scroll the terminal one page up (minus one line) relative to the current
291 * position.
292 */
293hterm.Terminal.prototype.scrollPageUp = function() {
294 var i = this.scrollPort_.getTopRowIndex();
295 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
296};
297
298/**
299 * Scroll the terminal one page down (minus one line) relative to the current
300 * position.
301 */
302hterm.Terminal.prototype.scrollPageDown = function() {
303 var i = this.scrollPort_.getTopRowIndex();
304 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800305};
306
rgindac9bc5502012-01-18 11:48:44 -0800307/**
308 * Full terminal reset.
309 */
rginda87b86462011-12-14 13:48:03 -0800310hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800311 this.clearAllTabStops();
312 this.setDefaultTabStops();
313 this.clearColorAndAttributes();
314 this.setVTScrollRegion(null, null);
315 this.clear();
316 this.setAbsoluteCursorPosition(0, 0);
317 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800318};
319
rgindac9bc5502012-01-18 11:48:44 -0800320/**
321 * Soft terminal reset.
322 */
rginda0f5c0292012-01-13 11:00:13 -0800323hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800324 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800325 this.setCursorVisible(true);
326 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800327};
328
rginda87b86462011-12-14 13:48:03 -0800329hterm.Terminal.prototype.clearColorAndAttributes = function() {
330 //console.log('clearColorAndAttributes');
331};
332
333hterm.Terminal.prototype.setForegroundColor256 = function() {
334 console.log('setForegroundColor256');
335};
336
337hterm.Terminal.prototype.setBackgroundColor256 = function() {
338 console.log('setBackgroundColor256');
339};
340
341hterm.Terminal.prototype.setForegroundColor = function() {
342 //console.log('setForegroundColor');
343};
344
345hterm.Terminal.prototype.setBackgroundColor = function() {
346 //console.log('setBackgroundColor');
347};
348
349hterm.Terminal.prototype.setAttributes = function() {
350 //console.log('setAttributes');
351};
352
353hterm.Terminal.prototype.resize = function() {
354 console.log('resize');
355};
356
rgindae4d29232012-01-19 10:47:13 -0800357hterm.Terminal.prototype.setCharacterSet = function() {
358 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800359};
360
rgindac9bc5502012-01-18 11:48:44 -0800361/**
362 * Move the cursor forward to the next tab stop, or to the last column
363 * if no more tab stops are set.
364 */
365hterm.Terminal.prototype.forwardTabStop = function() {
366 var column = this.screen_.cursorPosition.column;
367
368 for (var i = 0; i < this.tabStops_.length; i++) {
369 if (this.tabStops_[i] > column) {
370 this.setCursorColumn(this.tabStops_[i]);
371 return;
372 }
373 }
374
375 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800376};
377
rgindac9bc5502012-01-18 11:48:44 -0800378/**
379 * Move the cursor backward to the previous tab stop, or to the first column
380 * if no previous tab stops are set.
381 */
382hterm.Terminal.prototype.backwardTabStop = function() {
383 var column = this.screen_.cursorPosition.column;
384
385 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
386 if (this.tabStops_[i] < column) {
387 this.setCursorColumn(this.tabStops_[i]);
388 return;
389 }
390 }
391
392 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800393};
394
rgindac9bc5502012-01-18 11:48:44 -0800395/**
396 * Set a tab stop at the given column.
397 *
398 * @param {int} column Zero based column.
399 */
400hterm.Terminal.prototype.setTabStop = function(column) {
401 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
402 if (this.tabStops_[i] == column)
403 return;
404
405 if (this.tabStops_[i] < column) {
406 this.tabStops_.splice(i + 1, 0, column);
407 return;
408 }
409 }
410
411 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800412};
413
rgindac9bc5502012-01-18 11:48:44 -0800414/**
415 * Clear the tab stop at the current cursor position.
416 *
417 * No effect if there is no tab stop at the current cursor position.
418 */
419hterm.Terminal.prototype.clearTabStopAtCursor = function() {
420 var column = this.screen_.cursorPosition.column;
421
422 var i = this.tabStops_.indexOf(column);
423 if (i == -1)
424 return;
425
426 this.tabStops_.splice(i, 1);
427};
428
429/**
430 * Clear all tab stops.
431 */
432hterm.Terminal.prototype.clearAllTabStops = function() {
433 this.tabStops_.length = 0;
434};
435
436/**
437 * Set up the default tab stops, starting from a given column.
438 *
439 * This sets a tabstop every (column % this.tabWidth) column, starting
440 * from the specified column, or 0 if no column is provided.
441 *
442 * This does not clear the existing tab stops first, use clearAllTabStops
443 * for that.
444 *
445 * @param {int} opt_start Optional starting zero based starting column, useful
446 * for filling out missing tab stops when the terminal is resized.
447 */
448hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
449 var start = opt_start || 0;
450 var w = this.tabWidth;
451 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
452 for (var i = 0; i < stopCount; i++) {
453 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
454 }
rginda87b86462011-12-14 13:48:03 -0800455};
456
rginda6d397402012-01-17 10:58:29 -0800457/**
458 * Save cursor position and attributes.
459 *
460 * TODO(rginda): Save attributes once we support them.
461 */
rginda87b86462011-12-14 13:48:03 -0800462hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800463 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800464 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800465};
466
rginda6d397402012-01-17 10:58:29 -0800467/**
468 * Restore cursor position and attributes.
469 *
470 * TODO(rginda): Restore attributes once we support them.
471 */
rginda87b86462011-12-14 13:48:03 -0800472hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800473 if (this.savedOptions_.cursor)
474 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800475 if (this.savedOptions_.textAttributes)
476 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800477};
478
479/**
480 * Interpret a sequence of characters.
481 *
482 * Incomplete escape sequences are buffered until the next call.
483 *
484 * @param {string} str Sequence of characters to interpret or pass through.
485 */
486hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800487 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800488 this.scheduleSyncCursorPosition_();
489};
490
491/**
492 * Take over the given DIV for use as the terminal display.
493 *
494 * @param {HTMLDivElement} div The div to use as the terminal display.
495 */
496hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800497 this.div_ = div;
498
rginda8ba33642011-12-14 12:31:31 -0800499 this.scrollPort_.decorate(div);
rgindaa19afe22012-01-25 15:40:22 -0800500 this.scrollPort_.setFontFamily(this.defaultFontFamily_);
501
rginda8ba33642011-12-14 12:31:31 -0800502 this.document_ = this.scrollPort_.getDocument();
503
504 // Get character dimensions from the scrollPort.
505 this.characterSize_.height = this.scrollPort_.getRowHeight();
506 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
507
508 this.cursorNode_ = this.document_.createElement('div');
509 this.cursorNode_.style.cssText =
510 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800511 'top: -99px;' +
512 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800513 'width: ' + this.characterSize_.width + 'px;' +
514 'height: ' + this.characterSize_.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800515 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800516 'background-color: ' + this.cursorColor);
517 this.document_.body.appendChild(this.cursorNode_);
518
519 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800520
rginda87b86462011-12-14 13:48:03 -0800521 var link = this.document_.createElement('link');
522 link.setAttribute('href', '../css/dialogs.css');
523 link.setAttribute('rel', 'stylesheet');
524 this.document_.head.appendChild(link);
525
526 this.alertDialog = new AlertDialog(this.document_.body);
527 this.promptDialog = new PromptDialog(this.document_.body);
528 this.confirmDialog = new ConfirmDialog(this.document_.body);
529
530 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800531 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800532};
533
534hterm.Terminal.prototype.getDocument = function() {
535 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800536};
537
538/**
539 * Return the HTML Element for a given row index.
540 *
541 * This is a method from the RowProvider interface. The ScrollPort uses
542 * it to fetch rows on demand as they are scrolled into view.
543 *
544 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
545 * pairs to conserve memory.
546 *
547 * @param {integer} index The zero-based row index, measured relative to the
548 * start of the scrollback buffer. On-screen rows will always have the
549 * largest indicies.
550 * @return {HTMLElement} The 'x-row' element containing for the requested row.
551 */
552hterm.Terminal.prototype.getRowNode = function(index) {
553 if (index < this.scrollbackRows_.length)
554 return this.scrollbackRows_[index];
555
556 var screenIndex = index - this.scrollbackRows_.length;
557 return this.screen_.rowsArray[screenIndex];
558};
559
560/**
561 * Return the text content for a given range of rows.
562 *
563 * This is a method from the RowProvider interface. The ScrollPort uses
564 * it to fetch text content on demand when the user attempts to copy their
565 * selection to the clipboard.
566 *
567 * @param {integer} start The zero-based row index to start from, measured
568 * relative to the start of the scrollback buffer. On-screen rows will
569 * always have the largest indicies.
570 * @param {integer} end The zero-based row index to end on, measured
571 * relative to the start of the scrollback buffer.
572 * @return {string} A single string containing the text value of the range of
573 * rows. Lines will be newline delimited, with no trailing newline.
574 */
575hterm.Terminal.prototype.getRowsText = function(start, end) {
576 var ary = [];
577 for (var i = start; i < end; i++) {
578 var node = this.getRowNode(i);
579 ary.push(node.textContent);
580 }
581
582 return ary.join('\n');
583};
584
585/**
586 * Return the text content for a given row.
587 *
588 * This is a method from the RowProvider interface. The ScrollPort uses
589 * it to fetch text content on demand when the user attempts to copy their
590 * selection to the clipboard.
591 *
592 * @param {integer} index The zero-based row index to return, measured
593 * relative to the start of the scrollback buffer. On-screen rows will
594 * always have the largest indicies.
595 * @return {string} A string containing the text value of the selected row.
596 */
597hterm.Terminal.prototype.getRowText = function(index) {
598 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800599 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800600};
601
602/**
603 * Return the total number of rows in the addressable screen and in the
604 * scrollback buffer of this terminal.
605 *
606 * This is a method from the RowProvider interface. The ScrollPort uses
607 * it to compute the size of the scrollbar.
608 *
609 * @return {integer} The number of rows in this terminal.
610 */
611hterm.Terminal.prototype.getRowCount = function() {
612 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
613};
614
615/**
616 * Create DOM nodes for new rows and append them to the end of the terminal.
617 *
618 * This is the only correct way to add a new DOM node for a row. Notice that
619 * the new row is appended to the bottom of the list of rows, and does not
620 * require renumbering (of the rowIndex property) of previous rows.
621 *
622 * If you think you want a new blank row somewhere in the middle of the
623 * terminal, look into moveRows_().
624 *
625 * This method does not pay attention to vtScrollTop/Bottom, since you should
626 * be using moveRows() in cases where they would matter.
627 *
628 * The cursor will be positioned at column 0 of the first inserted line.
629 */
630hterm.Terminal.prototype.appendRows_ = function(count) {
631 var cursorRow = this.screen_.rowsArray.length;
632 var offset = this.scrollbackRows_.length + cursorRow;
633 for (var i = 0; i < count; i++) {
634 var row = this.document_.createElement('x-row');
635 row.appendChild(this.document_.createTextNode(''));
636 row.rowIndex = offset + i;
637 this.screen_.pushRow(row);
638 }
639
640 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
641 if (extraRows > 0) {
642 var ary = this.screen_.shiftRows(extraRows);
643 Array.prototype.push.apply(this.scrollbackRows_, ary);
644 this.scheduleScrollDown_();
645 }
646
647 if (cursorRow >= this.screen_.rowsArray.length)
648 cursorRow = this.screen_.rowsArray.length - 1;
649
rginda87b86462011-12-14 13:48:03 -0800650 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800651};
652
653/**
654 * Relocate rows from one part of the addressable screen to another.
655 *
656 * This is used to recycle rows during VT scrolls (those which are driven
657 * by VT commands, rather than by the user manipulating the scrollbar.)
658 *
659 * In this case, the blank lines scrolled into the scroll region are made of
660 * the nodes we scrolled off. These have their rowIndex properties carefully
661 * renumbered so as not to confuse the ScrollPort.
662 *
663 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
664 * It may just be luck. I wouldn't be surprised if we actually needed to call
665 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
666 * adding it.
667 */
668hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
669 var ary = this.screen_.removeRows(fromIndex, count);
670 this.screen_.insertRows(toIndex, ary);
671
672 var start, end;
673 if (fromIndex < toIndex) {
674 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800675 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800676 } else {
677 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800678 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800679 }
680
681 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800682 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800683};
684
685/**
686 * Renumber the rowIndex property of the given range of rows.
687 *
688 * The start and end indicies are relative to the screen, not the scrollback.
689 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800690 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800691 * no need to renumber scrollback rows.
692 */
693hterm.Terminal.prototype.renumberRows_ = function(start, end) {
694 var offset = this.scrollbackRows_.length;
695 for (var i = start; i < end; i++) {
696 this.screen_.rowsArray[i].rowIndex = offset + i;
697 }
698};
699
700/**
701 * Print a string to the terminal.
702 *
703 * This respects the current insert and wraparound modes. It will add new lines
704 * to the end of the terminal, scrolling off the top into the scrollback buffer
705 * if necessary.
706 *
707 * The string is *not* parsed for escape codes. Use the interpret() method if
708 * that's what you're after.
709 *
710 * @param{string} str The string to print.
711 */
712hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800713 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
714 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800715
rgindaa19afe22012-01-25 15:40:22 -0800716 if (this.options_.insertMode) {
717 this.screen_.insertString(str);
718 } else {
719 this.screen_.overwriteString(str);
720 }
721
722 var overflow = this.screen_.maybeClipCurrentRow();
723
724 if (this.options_.wraparound && overflow) {
725 var lastColumn;
726
727 do {
728 if (this.screen_.cursorPosition.overflow)
729 this.newLine();
730
731 if (!this.options_.insertMode)
732 this.screen_.deleteChars(overflow.characterLength);
733
734 this.screen_.prependNodes(overflow);
735 lastColumn = overflow.characterCount;
736
737 overflow = this.screen_.maybeClipCurrentRow();
738 } while (overflow);
739
740 this.setCursorColumn(lastColumn);
741 }
rginda8ba33642011-12-14 12:31:31 -0800742
743 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800744
745 if (this.scrollOnOutput)
746 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800747};
748
749/**
rginda87b86462011-12-14 13:48:03 -0800750 * Set the VT scroll region.
751 *
rginda87b86462011-12-14 13:48:03 -0800752 * This also resets the cursor position to the absolute (0, 0) position, since
753 * that's what xterm appears to do.
754 *
755 * @param {integer} scrollTop The zero-based top of the scroll region.
756 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
757 * inclusive.
758 */
759hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
760 this.vtScrollTop_ = scrollTop;
761 this.vtScrollBottom_ = scrollBottom;
762 this.setAbsoluteCursorPosition(0, 0);
763};
764
765/**
rginda8ba33642011-12-14 12:31:31 -0800766 * Return the top row index according to the VT.
767 *
768 * This will return 0 unless the terminal has been told to restrict scrolling
769 * to some lower row. It is used for some VT cursor positioning and scrolling
770 * commands.
771 *
772 * @return {integer} The topmost row in the terminal's scroll region.
773 */
774hterm.Terminal.prototype.getVTScrollTop = function() {
775 if (this.vtScrollTop_ != null)
776 return this.vtScrollTop_;
777
778 return 0;
rginda87b86462011-12-14 13:48:03 -0800779};
rginda8ba33642011-12-14 12:31:31 -0800780
781/**
782 * Return the bottom row index according to the VT.
783 *
784 * This will return the height of the terminal unless the it has been told to
785 * restrict scrolling to some higher row. It is used for some VT cursor
786 * positioning and scrolling commands.
787 *
788 * @return {integer} The bottommost row in the terminal's scroll region.
789 */
790hterm.Terminal.prototype.getVTScrollBottom = function() {
791 if (this.vtScrollBottom_ != null)
792 return this.vtScrollBottom_;
793
rginda87b86462011-12-14 13:48:03 -0800794 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800795}
796
797/**
798 * Process a '\n' character.
799 *
800 * If the cursor is on the final row of the terminal this will append a new
801 * blank row to the screen and scroll the topmost row into the scrollback
802 * buffer.
803 *
804 * Otherwise, this moves the cursor to column zero of the next row.
805 */
806hterm.Terminal.prototype.newLine = function() {
807 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800808 // If we're at the end of the screen we need to append a new line and
809 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800810 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800811 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
812 // End of the scroll region does not affect the scrollback buffer.
813 this.vtScrollUp(1);
814 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800815 } else {
rginda87b86462011-12-14 13:48:03 -0800816 // Anywhere else in the screen just moves the cursor.
817 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800818 }
819};
820
821/**
822 * Like newLine(), except maintain the cursor column.
823 */
824hterm.Terminal.prototype.lineFeed = function() {
825 var column = this.screen_.cursorPosition.column;
826 this.newLine();
827 this.setCursorColumn(column);
828};
829
830/**
rginda87b86462011-12-14 13:48:03 -0800831 * If autoCarriageReturn is set then newLine(), else lineFeed().
832 */
833hterm.Terminal.prototype.formFeed = function() {
834 if (this.options_.autoCarriageReturn) {
835 this.newLine();
836 } else {
837 this.lineFeed();
838 }
839};
840
841/**
842 * Move the cursor up one row, possibly inserting a blank line.
843 *
844 * The cursor column is not changed.
845 */
846hterm.Terminal.prototype.reverseLineFeed = function() {
847 var scrollTop = this.getVTScrollTop();
848 var currentRow = this.screen_.cursorPosition.row;
849
850 if (currentRow == scrollTop) {
851 this.insertLines(1);
852 } else {
853 this.setAbsoluteCursorRow(currentRow - 1);
854 }
855};
856
857/**
rginda8ba33642011-12-14 12:31:31 -0800858 * Replace all characters to the left of the current cursor with the space
859 * character.
860 *
861 * TODO(rginda): This should probably *remove* the characters (not just replace
862 * with a space) if there are no characters at or beyond the current cursor
863 * position. Once it does that, it'll have the same text-attribute related
864 * issues as hterm.Screen.prototype.clearCursorRow :/
865 */
866hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800867 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800868 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800869 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
870 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800871};
872
873/**
874 * Erase a given number of characters to the right of the cursor, shifting
875 * remaining characters to the left.
876 *
877 * The cursor position is unchanged.
878 *
879 * TODO(rginda): Test that this works even when the cursor is positioned beyond
880 * the end of the text.
881 *
882 * TODO(rginda): This likely has text-attribute related troubles similar to the
883 * todo on hterm.Screen.prototype.clearCursorRow.
884 */
885hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800886 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800887
rginda87b86462011-12-14 13:48:03 -0800888 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800889 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
890 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800891 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800892};
893
894/**
895 * Erase the current line.
896 *
897 * The cursor position is unchanged.
898 *
899 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
900 * has a text-attribute related TODO.
901 */
902hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800903 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800904 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800905 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800906};
907
908/**
909 * Erase all characters from the start of the scroll region to the current
910 * cursor position.
911 *
912 * The cursor position is unchanged.
913 *
914 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
915 * has a text-attribute related TODO.
916 */
917hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800918 var cursor = this.saveCursor();
919
920 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800921
922 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800923 for (var i = top; i < cursor.row; i++) {
924 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800925 this.screen_.clearCursorRow();
926 }
927
rginda87b86462011-12-14 13:48:03 -0800928 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800929};
930
931/**
932 * Erase all characters from the current cursor position to the end of the
933 * scroll region.
934 *
935 * The cursor position is unchanged.
936 *
937 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
938 * has a text-attribute related TODO.
939 */
940hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800941 var cursor = this.saveCursor();
942
943 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800944
945 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800946 for (var i = cursor.row + 1; i <= bottom; i++) {
947 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800948 this.screen_.clearCursorRow();
949 }
950
rginda87b86462011-12-14 13:48:03 -0800951 this.restoreCursor(cursor);
952};
953
954/**
955 * Fill the terminal with a given character.
956 *
957 * This methods does not respect the VT scroll region.
958 *
959 * @param {string} ch The character to use for the fill.
960 */
961hterm.Terminal.prototype.fill = function(ch) {
962 var cursor = this.saveCursor();
963
964 this.setAbsoluteCursorPosition(0, 0);
965 for (var row = 0; row < this.screenSize.height; row++) {
966 for (var col = 0; col < this.screenSize.width; col++) {
967 this.setAbsoluteCursorPosition(row, col);
968 this.screen_.overwriteString(ch);
969 }
970 }
971
972 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800973};
974
975/**
rgindae4d29232012-01-19 10:47:13 -0800976 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -0800977 *
rgindae4d29232012-01-19 10:47:13 -0800978 * The cursor position is unchanged. This does not respect the scroll
979 * region.
rginda8ba33642011-12-14 12:31:31 -0800980 *
981 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
982 * has a text-attribute related TODO.
983 */
984hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800985 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800986
rgindae4d29232012-01-19 10:47:13 -0800987 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -0800988
rgindae4d29232012-01-19 10:47:13 -0800989 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800990 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800991 this.screen_.clearCursorRow();
992 }
993
rginda87b86462011-12-14 13:48:03 -0800994 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800995};
996
997/**
998 * VT command to insert lines at the current cursor row.
999 *
1000 * This respects the current scroll region. Rows pushed off the bottom are
1001 * lost (they won't show up in the scrollback buffer).
1002 *
1003 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1004 * has a text-attribute related TODO.
1005 *
1006 * @param {integer} count The number of lines to insert.
1007 */
1008hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001009 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001010
1011 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001012 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001013
rgindae4d29232012-01-19 10:47:13 -08001014 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001015 if (start != cursor.row)
1016 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001017
1018 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001019 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001020 this.screen_.clearCursorRow();
1021 }
1022
rginda87b86462011-12-14 13:48:03 -08001023 cursor.column = 0;
1024 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001025};
1026
1027/**
1028 * VT command to delete lines at the current cursor row.
1029 *
1030 * New rows are added to the bottom of scroll region to take their place. New
1031 * rows are strictly there to take up space and have no content or style.
1032 */
1033hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001034 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001035
rginda87b86462011-12-14 13:48:03 -08001036 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001037 var bottom = this.getVTScrollBottom();
1038
rginda87b86462011-12-14 13:48:03 -08001039 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001040 count = Math.min(count, maxCount);
1041
rginda87b86462011-12-14 13:48:03 -08001042 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001043 if (count != maxCount)
1044 this.moveRows_(top, count, moveStart);
1045
1046 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001047 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001048 this.screen_.clearCursorRow();
1049 }
1050
rginda87b86462011-12-14 13:48:03 -08001051 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001052};
1053
1054/**
1055 * Inserts the given number of spaces at the current cursor position.
1056 *
rginda87b86462011-12-14 13:48:03 -08001057 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001058 */
1059hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001060 var cursor = this.saveCursor();
1061
rginda0f5c0292012-01-13 11:00:13 -08001062 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001063 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001064 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001065
1066 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001067};
1068
1069/**
1070 * Forward-delete the specified number of characters starting at the cursor
1071 * position.
1072 *
1073 * @param {integer} count The number of characters to delete.
1074 */
1075hterm.Terminal.prototype.deleteChars = function(count) {
1076 this.screen_.deleteChars(count);
1077};
1078
1079/**
1080 * Shift rows in the scroll region upwards by a given number of lines.
1081 *
1082 * New rows are inserted at the bottom of the scroll region to fill the
1083 * vacated rows. The new rows not filled out with the current text attributes.
1084 *
1085 * This function does not affect the scrollback rows at all. Rows shifted
1086 * off the top are lost.
1087 *
rginda87b86462011-12-14 13:48:03 -08001088 * The cursor position is not altered.
1089 *
rginda8ba33642011-12-14 12:31:31 -08001090 * @param {integer} count The number of rows to scroll.
1091 */
1092hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001093 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001094
rginda87b86462011-12-14 13:48:03 -08001095 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001096 this.deleteLines(count);
1097
rginda87b86462011-12-14 13:48:03 -08001098 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001099};
1100
1101/**
1102 * Shift rows below the cursor down by a given number of lines.
1103 *
1104 * This function respects the current scroll region.
1105 *
1106 * New rows are inserted at the top of the scroll region to fill the
1107 * vacated rows. The new rows not filled out with the current text attributes.
1108 *
1109 * This function does not affect the scrollback rows at all. Rows shifted
1110 * off the bottom are lost.
1111 *
1112 * @param {integer} count The number of rows to scroll.
1113 */
1114hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001115 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001116
rginda87b86462011-12-14 13:48:03 -08001117 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001118 this.insertLines(opt_count);
1119
rginda87b86462011-12-14 13:48:03 -08001120 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001121};
1122
rginda87b86462011-12-14 13:48:03 -08001123
rginda8ba33642011-12-14 12:31:31 -08001124/**
1125 * Set the cursor position.
1126 *
1127 * The cursor row is relative to the scroll region if the terminal has
1128 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1129 *
1130 * @param {integer} row The new zero-based cursor row.
1131 * @param {integer} row The new zero-based cursor column.
1132 */
1133hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1134 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001135 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001136 } else {
rginda87b86462011-12-14 13:48:03 -08001137 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001138 }
rginda87b86462011-12-14 13:48:03 -08001139};
rginda8ba33642011-12-14 12:31:31 -08001140
rginda87b86462011-12-14 13:48:03 -08001141hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1142 var scrollTop = this.getVTScrollTop();
1143 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001144 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001145 this.screen_.setCursorPosition(row, column);
1146};
1147
1148hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001149 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1150 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001151 this.screen_.setCursorPosition(row, column);
1152};
1153
1154/**
1155 * Set the cursor column.
1156 *
1157 * @param {integer} column The new zero-based cursor column.
1158 */
1159hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001160 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001161};
1162
1163/**
1164 * Return the cursor column.
1165 *
1166 * @return {integer} The zero-based cursor column.
1167 */
1168hterm.Terminal.prototype.getCursorColumn = function() {
1169 return this.screen_.cursorPosition.column;
1170};
1171
1172/**
1173 * Set the cursor row.
1174 *
1175 * The cursor row is relative to the scroll region if the terminal has
1176 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1177 *
1178 * @param {integer} row The new cursor row.
1179 */
rginda87b86462011-12-14 13:48:03 -08001180hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1181 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001182};
1183
1184/**
1185 * Return the cursor row.
1186 *
1187 * @return {integer} The zero-based cursor row.
1188 */
1189hterm.Terminal.prototype.getCursorRow = function(row) {
1190 return this.screen_.cursorPosition.row;
1191};
1192
1193/**
1194 * Request that the ScrollPort redraw itself soon.
1195 *
1196 * The redraw will happen asynchronously, soon after the call stack winds down.
1197 * Multiple calls will be coalesced into a single redraw.
1198 */
1199hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001200 if (this.timeouts_.redraw)
1201 return;
rginda8ba33642011-12-14 12:31:31 -08001202
1203 var self = this;
rginda87b86462011-12-14 13:48:03 -08001204 this.timeouts_.redraw = setTimeout(function() {
1205 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001206 self.scrollPort_.redraw_();
1207 }, 0);
1208};
1209
1210/**
1211 * Request that the ScrollPort be scrolled to the bottom.
1212 *
1213 * The scroll will happen asynchronously, soon after the call stack winds down.
1214 * Multiple calls will be coalesced into a single scroll.
1215 *
1216 * This affects the scrollbar position of the ScrollPort, and has nothing to
1217 * do with the VT scroll commands.
1218 */
1219hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1220 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001221 return;
rginda8ba33642011-12-14 12:31:31 -08001222
1223 var self = this;
1224 this.timeouts_.scrollDown = setTimeout(function() {
1225 delete self.timeouts_.scrollDown;
1226 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1227 }, 10);
1228};
1229
1230/**
1231 * Move the cursor up a specified number of rows.
1232 *
1233 * @param {integer} count The number of rows to move the cursor.
1234 */
1235hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001236 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001237};
1238
1239/**
1240 * Move the cursor down a specified number of rows.
1241 *
1242 * @param {integer} count The number of rows to move the cursor.
1243 */
1244hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001245 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001246 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1247 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1248 this.screenSize.height - 1);
1249
1250 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1251 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001252 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001253};
1254
1255/**
1256 * Move the cursor left a specified number of columns.
1257 *
1258 * @param {integer} count The number of columns to move the cursor.
1259 */
1260hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001261 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001262};
1263
1264/**
1265 * Move the cursor right a specified number of columns.
1266 *
1267 * @param {integer} count The number of columns to move the cursor.
1268 */
1269hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001270 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001271 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001272 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001273 this.setCursorColumn(column);
1274};
1275
1276/**
1277 * Reverse the foreground and background colors of the terminal.
1278 *
1279 * This only affects text that was drawn with no attributes.
1280 *
1281 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1282 * been drawn with attributes that happen to coincide with the default
1283 * 'no-attribute' colors. My guess is probably not.
1284 */
1285hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001286 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001287 if (state) {
1288 this.scrollPort_.setForegroundColor(this.backgroundColor);
1289 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1290 } else {
1291 this.scrollPort_.setForegroundColor(this.foregroundColor);
1292 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1293 }
1294};
1295
1296/**
rginda87b86462011-12-14 13:48:03 -08001297 * Ring the terminal bell.
1298 *
1299 * We only have a visual bell, which quickly toggles inverse video in the
1300 * terminal.
1301 */
1302hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001303 this.cursorNode_.style.backgroundColor =
1304 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001305
1306 var self = this;
1307 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001308 self.cursorNode_.style.backgroundColor = self.cursorColor;
1309 }, 200);
rginda87b86462011-12-14 13:48:03 -08001310};
1311
1312/**
rginda8ba33642011-12-14 12:31:31 -08001313 * Set the origin mode bit.
1314 *
1315 * If origin mode is on, certain VT cursor and scrolling commands measure their
1316 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1317 * to the top of the addressable screen.
1318 *
1319 * Defaults to off.
1320 *
1321 * @param {boolean} state True to set origin mode, false to unset.
1322 */
1323hterm.Terminal.prototype.setOriginMode = function(state) {
1324 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001325 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001326};
1327
1328/**
1329 * Set the insert mode bit.
1330 *
1331 * If insert mode is on, existing text beyond the cursor position will be
1332 * shifted right to make room for new text. Otherwise, new text overwrites
1333 * any existing text.
1334 *
1335 * Defaults to off.
1336 *
1337 * @param {boolean} state True to set insert mode, false to unset.
1338 */
1339hterm.Terminal.prototype.setInsertMode = function(state) {
1340 this.options_.insertMode = state;
1341};
1342
1343/**
rginda87b86462011-12-14 13:48:03 -08001344 * Set the auto carriage return bit.
1345 *
1346 * If auto carriage return is on then a formfeed character is interpreted
1347 * as a newline, otherwise it's the same as a linefeed. The difference boils
1348 * down to whether or not the cursor column is reset.
1349 */
1350hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1351 this.options_.autoCarriageReturn = state;
1352};
1353
1354/**
rginda8ba33642011-12-14 12:31:31 -08001355 * Set the wraparound mode bit.
1356 *
1357 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1358 * to the start of the following row. Otherwise, the cursor is clamped to the
1359 * end of the screen and attempts to write past it are ignored.
1360 *
1361 * Defaults to on.
1362 *
1363 * @param {boolean} state True to set wraparound mode, false to unset.
1364 */
1365hterm.Terminal.prototype.setWraparound = function(state) {
1366 this.options_.wraparound = state;
1367};
1368
1369/**
1370 * Set the reverse-wraparound mode bit.
1371 *
1372 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1373 * to the end of the previous row. Otherwise, the cursor is clamped to column
1374 * 0.
1375 *
1376 * Defaults to off.
1377 *
1378 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1379 */
1380hterm.Terminal.prototype.setReverseWraparound = function(state) {
1381 this.options_.reverseWraparound = state;
1382};
1383
1384/**
1385 * Selects between the primary and alternate screens.
1386 *
1387 * If alternate mode is on, the alternate screen is active. Otherwise the
1388 * primary screen is active.
1389 *
1390 * Swapping screens has no effect on the scrollback buffer.
1391 *
1392 * Each screen maintains its own cursor position.
1393 *
1394 * Defaults to off.
1395 *
1396 * @param {boolean} state True to set alternate mode, false to unset.
1397 */
1398hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001399 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001400 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1401
1402 this.screen_.setColumnCount(this.screenSize.width);
1403
1404 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1405 if (rowDelta > 0)
1406 this.appendRows_(rowDelta);
1407
rginda6d397402012-01-17 10:58:29 -08001408 this.restoreCursor(cursor);
1409
rginda2312fff2012-01-05 16:20:52 -08001410 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001411 this.syncCursorPosition_();
1412};
1413
1414/**
1415 * Set the cursor-blink mode bit.
1416 *
1417 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1418 * a visible cursor does not blink.
1419 *
1420 * You should make sure to turn blinking off if you're going to dispose of a
1421 * terminal, otherwise you'll leak a timeout.
1422 *
1423 * Defaults to on.
1424 *
1425 * @param {boolean} state True to set cursor-blink mode, false to unset.
1426 */
1427hterm.Terminal.prototype.setCursorBlink = function(state) {
1428 this.options_.cursorBlink = state;
1429
1430 if (!state && this.timeouts_.cursorBlink) {
1431 clearTimeout(this.timeouts_.cursorBlink);
1432 delete this.timeouts_.cursorBlink;
1433 }
1434
1435 if (this.options_.cursorVisible)
1436 this.setCursorVisible(true);
1437};
1438
1439/**
1440 * Set the cursor-visible mode bit.
1441 *
1442 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1443 *
1444 * Defaults to on.
1445 *
1446 * @param {boolean} state True to set cursor-visible mode, false to unset.
1447 */
1448hterm.Terminal.prototype.setCursorVisible = function(state) {
1449 this.options_.cursorVisible = state;
1450
1451 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001452 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001453 return;
1454 }
1455
rginda87b86462011-12-14 13:48:03 -08001456 this.syncCursorPosition_();
1457
1458 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001459
1460 if (this.options_.cursorBlink) {
1461 if (this.timeouts_.cursorBlink)
1462 return;
1463
1464 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1465 500);
1466 } else {
1467 if (this.timeouts_.cursorBlink) {
1468 clearTimeout(this.timeouts_.cursorBlink);
1469 delete this.timeouts_.cursorBlink;
1470 }
1471 }
1472};
1473
1474/**
rginda87b86462011-12-14 13:48:03 -08001475 * Synchronizes the visible cursor and document selection with the current
1476 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001477 */
1478hterm.Terminal.prototype.syncCursorPosition_ = function() {
1479 var topRowIndex = this.scrollPort_.getTopRowIndex();
1480 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1481 var cursorRowIndex = this.scrollbackRows_.length +
1482 this.screen_.cursorPosition.row;
1483
1484 if (cursorRowIndex > bottomRowIndex) {
1485 // Cursor is scrolled off screen, move it outside of the visible area.
1486 this.cursorNode_.style.top = -this.characterSize_.height;
1487 return;
1488 }
1489
1490 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1491 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1492 this.cursorNode_.style.left = this.characterSize_.width *
1493 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001494
1495 this.cursorNode_.setAttribute('title',
1496 '(' + this.screen_.cursorPosition.row +
1497 ', ' + this.screen_.cursorPosition.column +
1498 ')');
1499
1500 // Update the caret for a11y purposes.
1501 var selection = this.document_.getSelection();
1502 if (selection && selection.isCollapsed)
1503 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001504};
1505
1506/**
1507 * Synchronizes the visible cursor with the current cursor coordinates.
1508 *
1509 * The sync will happen asynchronously, soon after the call stack winds down.
1510 * Multiple calls will be coalesced into a single sync.
1511 */
1512hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1513 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001514 return;
rginda8ba33642011-12-14 12:31:31 -08001515
1516 var self = this;
1517 this.timeouts_.syncCursor = setTimeout(function() {
1518 self.syncCursorPosition_();
1519 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001520 }, 0);
1521};
1522
1523/**
1524 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1525 *
1526 * @param {string} string The VT string representing the keystroke.
1527 */
1528hterm.Terminal.prototype.onVTKeystroke = function(string) {
1529 if (this.scrollOnKeystroke)
1530 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1531
1532 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001533};
1534
1535/**
1536 * React when the ScrollPort is scrolled.
1537 */
1538hterm.Terminal.prototype.onScroll_ = function() {
1539 this.scheduleSyncCursorPosition_();
1540};
1541
1542/**
rginda9846e2f2012-01-27 13:53:33 -08001543 * React when text is pasted into the scrollPort.
1544 */
1545hterm.Terminal.prototype.onPaste_ = function(e) {
1546 this.io.onVTKeystroke(e.text);
1547};
1548
1549/**
rginda8ba33642011-12-14 12:31:31 -08001550 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001551 *
1552 * Note: This function should not directly contain code that alters the internal
1553 * state of the terminal. That kind of code belongs in realizeWidth or
1554 * realizeHeight, so that it can be executed synchronously in the case of a
1555 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001556 */
1557hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001558 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
1559 this.characterSize_.width);
rgindac9bc5502012-01-18 11:48:44 -08001560 var rowCount = this.scrollPort_.visibleRowCount;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001561 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001562 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001563};
1564
1565/**
1566 * Service the cursor blink timeout.
1567 */
1568hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001569 if (this.cursorNode_.style.opacity == '0') {
1570 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001571 } else {
rginda87b86462011-12-14 13:48:03 -08001572 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001573 }
1574};