blob: 58184f0fe2b50554f5a2aba3913bc450ea701091 [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
47 // The document that contains the scrollPort. Set in decorate().
48 this.document_ = null;
49
rginda8ba33642011-12-14 12:31:31 -080050 // The rows that have scrolled off screen and are no longer addressable.
51 this.scrollbackRows_ = [];
52
53 // The VT's notion of the top and bottom rows. Used during some VT
54 // cursor positioning and scrolling commands.
55 this.vtScrollTop_ = null;
56 this.vtScrollBottom_ = null;
57
58 // The DIV element for the visible cursor.
59 this.cursorNode_ = null;
60
61 // The default colors for text with no other color attributes.
62 this.backgroundColor = 'black';
63 this.foregroundColor = 'white';
64
65 // The color of the cursor.
66 this.cursorColor = 'rgba(255,0,0,0.5)';
67
rginda87b86462011-12-14 13:48:03 -080068 // If true, scroll to the bottom on any keystroke.
69 this.scrollOnKeystroke = true;
70
rginda8ba33642011-12-14 12:31:31 -080071 // The current mode bits for the terminal.
72 this.options_ = new hterm.Options();
73
74 // Timeouts we might need to clear.
75 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080076
77 // The VT escape sequence interpreter.
78 this.vt = new hterm.VT100(this);
79
80 // General IO interface that can be given to third parties without exposing
81 // the entire terminal object.
82 this.io = new hterm.Terminal.IO(this);
83};
84
85/**
86 * Create a new instance of a terminal command and run it with a given
87 * argument string.
88 *
89 * @param {function} commandClass The constructor for a terminal command.
90 * @param {string} argString The argument string to pass to the command.
91 */
92hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
93 var self = this;
94 this.command = new commandClass(
95 { argString: argString || '',
96 io: this.io.push(),
97 onExit: function(code) {
98 self.io.pop();
99 self.io.println(hterm.msg('COMMAND_COMPLETE',
100 [self.command.commandName, code]));
101 }
102 });
103
104 this.command.run();
105};
106
107/**
108 * Return a copy of the current cursor position.
109 *
110 * @return {hterm.RowCol} The RowCol object representing the current position.
111 */
112hterm.Terminal.prototype.saveCursor = function() {
113 return this.screen_.cursorPosition.clone();
114};
115
116/**
117 * Restore a previously saved cursor position.
118 *
119 * @param {hterm.RowCol} cursor The position to restore.
120 */
121hterm.Terminal.prototype.restoreCursor = function(cursor) {
122 this.screen_.setCursorPosition(cursor.row, cursor.column);
123};
124
125/**
126 * Set the width of the terminal, resizing the UI to match.
127 */
128hterm.Terminal.prototype.setWidth = function(columnCount) {
129 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px'
130
131 // The resizing of the UI will happen asynchronously, so we need to take
132 // care of this bookeeping here instead of letting the resize handlers deal
133 // with it.
134 this.screenSize.width = columnCount;
135 this.screen_.setColumnCount(columnCount);
136};
137
138/**
139 * Scroll the terminal to the top of the scrollback buffer.
140 */
141hterm.Terminal.prototype.scrollHome = function() {
142 this.scrollPort_.scrollRowToTop(0);
143};
144
145/**
146 * Scroll the terminal to the end.
147 */
148hterm.Terminal.prototype.scrollEnd = function() {
149 this.scrollPort_.scrollRowToBottom(this.getRowCount());
150};
151
152/**
153 * Scroll the terminal one page up (minus one line) relative to the current
154 * position.
155 */
156hterm.Terminal.prototype.scrollPageUp = function() {
157 var i = this.scrollPort_.getTopRowIndex();
158 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
159};
160
161/**
162 * Scroll the terminal one page down (minus one line) relative to the current
163 * position.
164 */
165hterm.Terminal.prototype.scrollPageDown = function() {
166 var i = this.scrollPort_.getTopRowIndex();
167 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800168};
169
170/**
171 * Methods called by Cory's vt100 interpreter which we haven't implemented yet.
172 */
rginda87b86462011-12-14 13:48:03 -0800173hterm.Terminal.prototype.reset = function() {
174 console.log('reset');
175};
176
177hterm.Terminal.prototype.clearColorAndAttributes = function() {
178 //console.log('clearColorAndAttributes');
179};
180
181hterm.Terminal.prototype.setForegroundColor256 = function() {
182 console.log('setForegroundColor256');
183};
184
185hterm.Terminal.prototype.setBackgroundColor256 = function() {
186 console.log('setBackgroundColor256');
187};
188
189hterm.Terminal.prototype.setForegroundColor = function() {
190 //console.log('setForegroundColor');
191};
192
193hterm.Terminal.prototype.setBackgroundColor = function() {
194 //console.log('setBackgroundColor');
195};
196
197hterm.Terminal.prototype.setAttributes = function() {
198 //console.log('setAttributes');
199};
200
201hterm.Terminal.prototype.resize = function() {
202 console.log('resize');
203};
204
205hterm.Terminal.prototype.setSpecialCharsEnabled = function() {
206 //console.log('setSpecialCharactersEnabled');
207};
208
209hterm.Terminal.prototype.setTabStopAtCursor = function() {
210 console.log('setTabStopAtCursor');
211};
212
213hterm.Terminal.prototype.clearTabStops = function() {
214 console.log('clearTabStops');
215};
216
217hterm.Terminal.prototype.saveOptions = function() {
218 console.log('saveOptions');
219};
220
221hterm.Terminal.prototype.restoreOptions = function() {
222 console.log('restoreOptions');
rginda8ba33642011-12-14 12:31:31 -0800223};
224
225/**
226 * Interpret a sequence of characters.
227 *
228 * Incomplete escape sequences are buffered until the next call.
229 *
230 * @param {string} str Sequence of characters to interpret or pass through.
231 */
232hterm.Terminal.prototype.interpret = function(str) {
rginda87b86462011-12-14 13:48:03 -0800233 this.vt.interpretString(str);
rginda8ba33642011-12-14 12:31:31 -0800234 this.scheduleSyncCursorPosition_();
235};
236
237/**
238 * Take over the given DIV for use as the terminal display.
239 *
240 * @param {HTMLDivElement} div The div to use as the terminal display.
241 */
242hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800243 this.div_ = div;
244
rginda8ba33642011-12-14 12:31:31 -0800245 this.scrollPort_.decorate(div);
246 this.document_ = this.scrollPort_.getDocument();
247
248 // Get character dimensions from the scrollPort.
249 this.characterSize_.height = this.scrollPort_.getRowHeight();
250 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
251
252 this.cursorNode_ = this.document_.createElement('div');
253 this.cursorNode_.style.cssText =
254 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800255 'top: -99px;' +
256 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800257 'width: ' + this.characterSize_.width + 'px;' +
258 'height: ' + this.characterSize_.height + 'px;' +
rginda87b86462011-12-14 13:48:03 -0800259 '-webkit-transition: opacity 100ms ease-in;' +
rginda8ba33642011-12-14 12:31:31 -0800260 'background-color: ' + this.cursorColor);
261 this.document_.body.appendChild(this.cursorNode_);
262
263 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800264
265 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
266
267 var link = this.document_.createElement('link');
268 link.setAttribute('href', '../css/dialogs.css');
269 link.setAttribute('rel', 'stylesheet');
270 this.document_.head.appendChild(link);
271
272 this.alertDialog = new AlertDialog(this.document_.body);
273 this.promptDialog = new PromptDialog(this.document_.body);
274 this.confirmDialog = new ConfirmDialog(this.document_.body);
275
276 this.scrollPort_.focus();
277};
278
279hterm.Terminal.prototype.getDocument = function() {
280 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800281};
282
283/**
284 * Return the HTML Element for a given row index.
285 *
286 * This is a method from the RowProvider interface. The ScrollPort uses
287 * it to fetch rows on demand as they are scrolled into view.
288 *
289 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
290 * pairs to conserve memory.
291 *
292 * @param {integer} index The zero-based row index, measured relative to the
293 * start of the scrollback buffer. On-screen rows will always have the
294 * largest indicies.
295 * @return {HTMLElement} The 'x-row' element containing for the requested row.
296 */
297hterm.Terminal.prototype.getRowNode = function(index) {
298 if (index < this.scrollbackRows_.length)
299 return this.scrollbackRows_[index];
300
301 var screenIndex = index - this.scrollbackRows_.length;
302 return this.screen_.rowsArray[screenIndex];
303};
304
305/**
306 * Return the text content for a given range of rows.
307 *
308 * This is a method from the RowProvider interface. The ScrollPort uses
309 * it to fetch text content on demand when the user attempts to copy their
310 * selection to the clipboard.
311 *
312 * @param {integer} start The zero-based row index to start from, measured
313 * relative to the start of the scrollback buffer. On-screen rows will
314 * always have the largest indicies.
315 * @param {integer} end The zero-based row index to end on, measured
316 * relative to the start of the scrollback buffer.
317 * @return {string} A single string containing the text value of the range of
318 * rows. Lines will be newline delimited, with no trailing newline.
319 */
320hterm.Terminal.prototype.getRowsText = function(start, end) {
321 var ary = [];
322 for (var i = start; i < end; i++) {
323 var node = this.getRowNode(i);
324 ary.push(node.textContent);
325 }
326
327 return ary.join('\n');
328};
329
330/**
331 * Return the text content for a given row.
332 *
333 * This is a method from the RowProvider interface. The ScrollPort uses
334 * it to fetch text content on demand when the user attempts to copy their
335 * selection to the clipboard.
336 *
337 * @param {integer} index The zero-based row index to return, measured
338 * relative to the start of the scrollback buffer. On-screen rows will
339 * always have the largest indicies.
340 * @return {string} A string containing the text value of the selected row.
341 */
342hterm.Terminal.prototype.getRowText = function(index) {
343 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800344 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800345};
346
347/**
348 * Return the total number of rows in the addressable screen and in the
349 * scrollback buffer of this terminal.
350 *
351 * This is a method from the RowProvider interface. The ScrollPort uses
352 * it to compute the size of the scrollbar.
353 *
354 * @return {integer} The number of rows in this terminal.
355 */
356hterm.Terminal.prototype.getRowCount = function() {
357 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
358};
359
360/**
361 * Create DOM nodes for new rows and append them to the end of the terminal.
362 *
363 * This is the only correct way to add a new DOM node for a row. Notice that
364 * the new row is appended to the bottom of the list of rows, and does not
365 * require renumbering (of the rowIndex property) of previous rows.
366 *
367 * If you think you want a new blank row somewhere in the middle of the
368 * terminal, look into moveRows_().
369 *
370 * This method does not pay attention to vtScrollTop/Bottom, since you should
371 * be using moveRows() in cases where they would matter.
372 *
373 * The cursor will be positioned at column 0 of the first inserted line.
374 */
375hterm.Terminal.prototype.appendRows_ = function(count) {
376 var cursorRow = this.screen_.rowsArray.length;
377 var offset = this.scrollbackRows_.length + cursorRow;
378 for (var i = 0; i < count; i++) {
379 var row = this.document_.createElement('x-row');
380 row.appendChild(this.document_.createTextNode(''));
381 row.rowIndex = offset + i;
382 this.screen_.pushRow(row);
383 }
384
385 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
386 if (extraRows > 0) {
387 var ary = this.screen_.shiftRows(extraRows);
388 Array.prototype.push.apply(this.scrollbackRows_, ary);
389 this.scheduleScrollDown_();
390 }
391
392 if (cursorRow >= this.screen_.rowsArray.length)
393 cursorRow = this.screen_.rowsArray.length - 1;
394
rginda87b86462011-12-14 13:48:03 -0800395 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800396};
397
398/**
399 * Relocate rows from one part of the addressable screen to another.
400 *
401 * This is used to recycle rows during VT scrolls (those which are driven
402 * by VT commands, rather than by the user manipulating the scrollbar.)
403 *
404 * In this case, the blank lines scrolled into the scroll region are made of
405 * the nodes we scrolled off. These have their rowIndex properties carefully
406 * renumbered so as not to confuse the ScrollPort.
407 *
408 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
409 * It may just be luck. I wouldn't be surprised if we actually needed to call
410 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
411 * adding it.
412 */
413hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
414 var ary = this.screen_.removeRows(fromIndex, count);
415 this.screen_.insertRows(toIndex, ary);
416
417 var start, end;
418 if (fromIndex < toIndex) {
419 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800420 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800421 } else {
422 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800423 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800424 }
425
426 this.renumberRows_(start, end);
rginda87b86462011-12-14 13:48:03 -0800427 this.scrollPort_.scheduleRedraw();
rginda8ba33642011-12-14 12:31:31 -0800428};
429
430/**
431 * Renumber the rowIndex property of the given range of rows.
432 *
433 * The start and end indicies are relative to the screen, not the scrollback.
434 * Rows in the scrollback buffer cannot be renumbered. Since they are not
435 * addressable (you cant delete them, scroll them, etc), you should have
436 * no need to renumber scrollback rows.
437 */
438hterm.Terminal.prototype.renumberRows_ = function(start, end) {
439 var offset = this.scrollbackRows_.length;
440 for (var i = start; i < end; i++) {
441 this.screen_.rowsArray[i].rowIndex = offset + i;
442 }
443};
444
445/**
446 * Print a string to the terminal.
447 *
448 * This respects the current insert and wraparound modes. It will add new lines
449 * to the end of the terminal, scrolling off the top into the scrollback buffer
450 * if necessary.
451 *
452 * The string is *not* parsed for escape codes. Use the interpret() method if
453 * that's what you're after.
454 *
455 * @param{string} str The string to print.
456 */
457hterm.Terminal.prototype.print = function(str) {
458 do {
459 if (this.options_.insertMode) {
460 str = this.screen_.insertString(str);
461 } else {
462 str = this.screen_.overwriteString(str);
463 }
464
rginda87b86462011-12-14 13:48:03 -0800465 if (this.options_.wraparound && str != null) {
rginda8ba33642011-12-14 12:31:31 -0800466 this.newLine();
467 } else {
468 break;
469 }
470 } while (str);
471
472 this.scheduleSyncCursorPosition_();
473};
474
475/**
rginda87b86462011-12-14 13:48:03 -0800476 * Set the VT scroll region.
477 *
478 *
479 * This also resets the cursor position to the absolute (0, 0) position, since
480 * that's what xterm appears to do.
481 *
482 * @param {integer} scrollTop The zero-based top of the scroll region.
483 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
484 * inclusive.
485 */
486hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
487 this.vtScrollTop_ = scrollTop;
488 this.vtScrollBottom_ = scrollBottom;
489 this.setAbsoluteCursorPosition(0, 0);
490};
491
492/**
rginda8ba33642011-12-14 12:31:31 -0800493 * Return the top row index according to the VT.
494 *
495 * This will return 0 unless the terminal has been told to restrict scrolling
496 * to some lower row. It is used for some VT cursor positioning and scrolling
497 * commands.
498 *
499 * @return {integer} The topmost row in the terminal's scroll region.
500 */
501hterm.Terminal.prototype.getVTScrollTop = function() {
502 if (this.vtScrollTop_ != null)
503 return this.vtScrollTop_;
504
505 return 0;
rginda87b86462011-12-14 13:48:03 -0800506};
rginda8ba33642011-12-14 12:31:31 -0800507
508/**
509 * Return the bottom row index according to the VT.
510 *
511 * This will return the height of the terminal unless the it has been told to
512 * restrict scrolling to some higher row. It is used for some VT cursor
513 * positioning and scrolling commands.
514 *
515 * @return {integer} The bottommost row in the terminal's scroll region.
516 */
517hterm.Terminal.prototype.getVTScrollBottom = function() {
518 if (this.vtScrollBottom_ != null)
519 return this.vtScrollBottom_;
520
rginda87b86462011-12-14 13:48:03 -0800521 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800522}
523
524/**
525 * Process a '\n' character.
526 *
527 * If the cursor is on the final row of the terminal this will append a new
528 * blank row to the screen and scroll the topmost row into the scrollback
529 * buffer.
530 *
531 * Otherwise, this moves the cursor to column zero of the next row.
532 */
533hterm.Terminal.prototype.newLine = function() {
534 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800535 // If we're at the end of the screen we need to append a new line and
536 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800537 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800538 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
539 // End of the scroll region does not affect the scrollback buffer.
540 this.vtScrollUp(1);
541 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800542 } else {
rginda87b86462011-12-14 13:48:03 -0800543 // Anywhere else in the screen just moves the cursor.
544 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800545 }
546};
547
548/**
549 * Like newLine(), except maintain the cursor column.
550 */
551hterm.Terminal.prototype.lineFeed = function() {
552 var column = this.screen_.cursorPosition.column;
553 this.newLine();
554 this.setCursorColumn(column);
555};
556
557/**
rginda87b86462011-12-14 13:48:03 -0800558 * If autoCarriageReturn is set then newLine(), else lineFeed().
559 */
560hterm.Terminal.prototype.formFeed = function() {
561 if (this.options_.autoCarriageReturn) {
562 this.newLine();
563 } else {
564 this.lineFeed();
565 }
566};
567
568/**
569 * Move the cursor up one row, possibly inserting a blank line.
570 *
571 * The cursor column is not changed.
572 */
573hterm.Terminal.prototype.reverseLineFeed = function() {
574 var scrollTop = this.getVTScrollTop();
575 var currentRow = this.screen_.cursorPosition.row;
576
577 if (currentRow == scrollTop) {
578 this.insertLines(1);
579 } else {
580 this.setAbsoluteCursorRow(currentRow - 1);
581 }
582};
583
584/**
rginda8ba33642011-12-14 12:31:31 -0800585 * Replace all characters to the left of the current cursor with the space
586 * character.
587 *
588 * TODO(rginda): This should probably *remove* the characters (not just replace
589 * with a space) if there are no characters at or beyond the current cursor
590 * position. Once it does that, it'll have the same text-attribute related
591 * issues as hterm.Screen.prototype.clearCursorRow :/
592 */
593hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800594 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800595 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800596 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
597 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800598};
599
600/**
601 * Erase a given number of characters to the right of the cursor, shifting
602 * remaining characters to the left.
603 *
604 * The cursor position is unchanged.
605 *
606 * TODO(rginda): Test that this works even when the cursor is positioned beyond
607 * the end of the text.
608 *
609 * TODO(rginda): This likely has text-attribute related troubles similar to the
610 * todo on hterm.Screen.prototype.clearCursorRow.
611 */
612hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800613 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800614
rginda87b86462011-12-14 13:48:03 -0800615 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800616 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
617 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800618 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800619};
620
621/**
622 * Erase the current line.
623 *
624 * The cursor position is unchanged.
625 *
626 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
627 * has a text-attribute related TODO.
628 */
629hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800630 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800631 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800632 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800633};
634
635/**
636 * Erase all characters from the start of the scroll region to the current
637 * cursor position.
638 *
639 * The cursor position is unchanged.
640 *
641 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
642 * has a text-attribute related TODO.
643 */
644hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800645 var cursor = this.saveCursor();
646
647 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800648
649 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800650 for (var i = top; i < cursor.row; i++) {
651 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800652 this.screen_.clearCursorRow();
653 }
654
rginda87b86462011-12-14 13:48:03 -0800655 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800656};
657
658/**
659 * Erase all characters from the current cursor position to the end of the
660 * scroll region.
661 *
662 * The cursor position is unchanged.
663 *
664 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
665 * has a text-attribute related TODO.
666 */
667hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800668 var cursor = this.saveCursor();
669
670 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800671
672 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800673 for (var i = cursor.row + 1; i <= bottom; i++) {
674 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800675 this.screen_.clearCursorRow();
676 }
677
rginda87b86462011-12-14 13:48:03 -0800678 this.restoreCursor(cursor);
679};
680
681/**
682 * Fill the terminal with a given character.
683 *
684 * This methods does not respect the VT scroll region.
685 *
686 * @param {string} ch The character to use for the fill.
687 */
688hterm.Terminal.prototype.fill = function(ch) {
689 var cursor = this.saveCursor();
690
691 this.setAbsoluteCursorPosition(0, 0);
692 for (var row = 0; row < this.screenSize.height; row++) {
693 for (var col = 0; col < this.screenSize.width; col++) {
694 this.setAbsoluteCursorPosition(row, col);
695 this.screen_.overwriteString(ch);
696 }
697 }
698
699 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800700};
701
702/**
703 * Erase the entire scroll region.
704 *
705 * The cursor position is unchanged.
706 *
707 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
708 * has a text-attribute related TODO.
709 */
710hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800711 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800712
713 var top = this.getVTScrollTop();
714 var bottom = this.getVTScrollBottom();
715
716 for (var i = top; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800717 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800718 this.screen_.clearCursorRow();
719 }
720
rginda87b86462011-12-14 13:48:03 -0800721 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800722};
723
724/**
725 * VT command to insert lines at the current cursor row.
726 *
727 * This respects the current scroll region. Rows pushed off the bottom are
728 * lost (they won't show up in the scrollback buffer).
729 *
730 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
731 * has a text-attribute related TODO.
732 *
733 * @param {integer} count The number of lines to insert.
734 */
735hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800736 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800737
738 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800739 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800740
741 var start = bottom - count;
rginda87b86462011-12-14 13:48:03 -0800742 if (start != cursor.row)
743 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800744
745 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800746 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800747 this.screen_.clearCursorRow();
748 }
749
rginda87b86462011-12-14 13:48:03 -0800750 cursor.column = 0;
751 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800752};
753
754/**
755 * VT command to delete lines at the current cursor row.
756 *
757 * New rows are added to the bottom of scroll region to take their place. New
758 * rows are strictly there to take up space and have no content or style.
759 */
760hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800761 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800762
rginda87b86462011-12-14 13:48:03 -0800763 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -0800764 var bottom = this.getVTScrollBottom();
765
rginda87b86462011-12-14 13:48:03 -0800766 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -0800767 count = Math.min(count, maxCount);
768
rginda87b86462011-12-14 13:48:03 -0800769 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -0800770 if (count != maxCount)
771 this.moveRows_(top, count, moveStart);
772
773 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800774 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800775 this.screen_.clearCursorRow();
776 }
777
rginda87b86462011-12-14 13:48:03 -0800778 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800779};
780
781/**
782 * Inserts the given number of spaces at the current cursor position.
783 *
rginda87b86462011-12-14 13:48:03 -0800784 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -0800785 */
786hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -0800787 var cursor = this.saveCursor();
788
rginda8ba33642011-12-14 12:31:31 -0800789 var ws = hterm.getWhitespace(count);
790 this.screen_.insertString(ws);
rginda87b86462011-12-14 13:48:03 -0800791
792 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800793};
794
795/**
796 * Forward-delete the specified number of characters starting at the cursor
797 * position.
798 *
799 * @param {integer} count The number of characters to delete.
800 */
801hterm.Terminal.prototype.deleteChars = function(count) {
802 this.screen_.deleteChars(count);
803};
804
805/**
806 * Shift rows in the scroll region upwards by a given number of lines.
807 *
808 * New rows are inserted at the bottom of the scroll region to fill the
809 * vacated rows. The new rows not filled out with the current text attributes.
810 *
811 * This function does not affect the scrollback rows at all. Rows shifted
812 * off the top are lost.
813 *
rginda87b86462011-12-14 13:48:03 -0800814 * The cursor position is not altered.
815 *
rginda8ba33642011-12-14 12:31:31 -0800816 * @param {integer} count The number of rows to scroll.
817 */
818hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -0800819 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800820
rginda87b86462011-12-14 13:48:03 -0800821 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -0800822 this.deleteLines(count);
823
rginda87b86462011-12-14 13:48:03 -0800824 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800825};
826
827/**
828 * Shift rows below the cursor down by a given number of lines.
829 *
830 * This function respects the current scroll region.
831 *
832 * New rows are inserted at the top of the scroll region to fill the
833 * vacated rows. The new rows not filled out with the current text attributes.
834 *
835 * This function does not affect the scrollback rows at all. Rows shifted
836 * off the bottom are lost.
837 *
838 * @param {integer} count The number of rows to scroll.
839 */
840hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800841 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800842
rginda87b86462011-12-14 13:48:03 -0800843 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -0800844 this.insertLines(opt_count);
845
rginda87b86462011-12-14 13:48:03 -0800846 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800847};
848
rginda87b86462011-12-14 13:48:03 -0800849
rginda8ba33642011-12-14 12:31:31 -0800850/**
851 * Set the cursor position.
852 *
853 * The cursor row is relative to the scroll region if the terminal has
854 * 'origin mode' enabled, or relative to the addressable screen otherwise.
855 *
856 * @param {integer} row The new zero-based cursor row.
857 * @param {integer} row The new zero-based cursor column.
858 */
859hterm.Terminal.prototype.setCursorPosition = function(row, column) {
860 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -0800861 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800862 } else {
rginda87b86462011-12-14 13:48:03 -0800863 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800864 }
rginda87b86462011-12-14 13:48:03 -0800865};
rginda8ba33642011-12-14 12:31:31 -0800866
rginda87b86462011-12-14 13:48:03 -0800867hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
868 var scrollTop = this.getVTScrollTop();
869 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
870 this.screen_.setCursorPosition(row, column);
871};
872
873hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda8ba33642011-12-14 12:31:31 -0800874 this.screen_.setCursorPosition(row, column);
875};
876
877/**
878 * Set the cursor column.
879 *
880 * @param {integer} column The new zero-based cursor column.
881 */
882hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -0800883 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -0800884};
885
886/**
887 * Return the cursor column.
888 *
889 * @return {integer} The zero-based cursor column.
890 */
891hterm.Terminal.prototype.getCursorColumn = function() {
892 return this.screen_.cursorPosition.column;
893};
894
895/**
896 * Set the cursor row.
897 *
898 * The cursor row is relative to the scroll region if the terminal has
899 * 'origin mode' enabled, or relative to the addressable screen otherwise.
900 *
901 * @param {integer} row The new cursor row.
902 */
rginda87b86462011-12-14 13:48:03 -0800903hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
904 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -0800905};
906
907/**
908 * Return the cursor row.
909 *
910 * @return {integer} The zero-based cursor row.
911 */
912hterm.Terminal.prototype.getCursorRow = function(row) {
913 return this.screen_.cursorPosition.row;
914};
915
916/**
917 * Request that the ScrollPort redraw itself soon.
918 *
919 * The redraw will happen asynchronously, soon after the call stack winds down.
920 * Multiple calls will be coalesced into a single redraw.
921 */
922hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -0800923 if (this.timeouts_.redraw)
924 return;
rginda8ba33642011-12-14 12:31:31 -0800925
926 var self = this;
rginda87b86462011-12-14 13:48:03 -0800927 this.timeouts_.redraw = setTimeout(function() {
928 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -0800929 self.scrollPort_.redraw_();
930 }, 0);
931};
932
933/**
934 * Request that the ScrollPort be scrolled to the bottom.
935 *
936 * The scroll will happen asynchronously, soon after the call stack winds down.
937 * Multiple calls will be coalesced into a single scroll.
938 *
939 * This affects the scrollbar position of the ScrollPort, and has nothing to
940 * do with the VT scroll commands.
941 */
942hterm.Terminal.prototype.scheduleScrollDown_ = function() {
943 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -0800944 return;
rginda8ba33642011-12-14 12:31:31 -0800945
946 var self = this;
947 this.timeouts_.scrollDown = setTimeout(function() {
948 delete self.timeouts_.scrollDown;
949 self.scrollPort_.scrollRowToBottom(self.getRowCount());
950 }, 10);
951};
952
953/**
954 * Move the cursor up a specified number of rows.
955 *
956 * @param {integer} count The number of rows to move the cursor.
957 */
958hterm.Terminal.prototype.cursorUp = function(count) {
959 return this.cursorDown(-count);
960};
961
962/**
963 * Move the cursor down a specified number of rows.
964 *
965 * @param {integer} count The number of rows to move the cursor.
966 */
967hterm.Terminal.prototype.cursorDown = function(count) {
968 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
969 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
970 this.screenSize.height - 1);
971
972 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
973 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -0800974 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -0800975};
976
977/**
978 * Move the cursor left a specified number of columns.
979 *
980 * @param {integer} count The number of columns to move the cursor.
981 */
982hterm.Terminal.prototype.cursorLeft = function(count) {
983 return this.cursorRight(-count);
984};
985
986/**
987 * Move the cursor right a specified number of columns.
988 *
989 * @param {integer} count The number of columns to move the cursor.
990 */
991hterm.Terminal.prototype.cursorRight = function(count) {
992 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -0800993 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -0800994 this.setCursorColumn(column);
995};
996
997/**
998 * Reverse the foreground and background colors of the terminal.
999 *
1000 * This only affects text that was drawn with no attributes.
1001 *
1002 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1003 * been drawn with attributes that happen to coincide with the default
1004 * 'no-attribute' colors. My guess is probably not.
1005 */
1006hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001007 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001008 if (state) {
1009 this.scrollPort_.setForegroundColor(this.backgroundColor);
1010 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1011 } else {
1012 this.scrollPort_.setForegroundColor(this.foregroundColor);
1013 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1014 }
1015};
1016
1017/**
rginda87b86462011-12-14 13:48:03 -08001018 * Ring the terminal bell.
1019 *
1020 * We only have a visual bell, which quickly toggles inverse video in the
1021 * terminal.
1022 */
1023hterm.Terminal.prototype.ringBell = function() {
1024 // We can't toggle using only setReverseVideo, since there's a chance we'll
1025 // get a request to toggle reverse video before our visual bell is over.
1026 var fg = this.scrollPort_.getForegroundColor();
1027 this.scrollPort_.setForegroundColor(this.scrollPort_.getBackgroundColor());
1028 this.scrollPort_.setBackgroundColor(fg);
1029
1030 var self = this;
1031 setTimeout(function() {
1032 self.setReverseVideo(self.options_.reverseVideo);
1033 }, 100);
1034};
1035
1036/**
rginda8ba33642011-12-14 12:31:31 -08001037 * Set the origin mode bit.
1038 *
1039 * If origin mode is on, certain VT cursor and scrolling commands measure their
1040 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1041 * to the top of the addressable screen.
1042 *
1043 * Defaults to off.
1044 *
1045 * @param {boolean} state True to set origin mode, false to unset.
1046 */
1047hterm.Terminal.prototype.setOriginMode = function(state) {
1048 this.options_.originMode = state;
1049};
1050
1051/**
1052 * Set the insert mode bit.
1053 *
1054 * If insert mode is on, existing text beyond the cursor position will be
1055 * shifted right to make room for new text. Otherwise, new text overwrites
1056 * any existing text.
1057 *
1058 * Defaults to off.
1059 *
1060 * @param {boolean} state True to set insert mode, false to unset.
1061 */
1062hterm.Terminal.prototype.setInsertMode = function(state) {
1063 this.options_.insertMode = state;
1064};
1065
1066/**
rginda87b86462011-12-14 13:48:03 -08001067 * Set the auto carriage return bit.
1068 *
1069 * If auto carriage return is on then a formfeed character is interpreted
1070 * as a newline, otherwise it's the same as a linefeed. The difference boils
1071 * down to whether or not the cursor column is reset.
1072 */
1073hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1074 this.options_.autoCarriageReturn = state;
1075};
1076
1077/**
rginda8ba33642011-12-14 12:31:31 -08001078 * Set the wraparound mode bit.
1079 *
1080 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1081 * to the start of the following row. Otherwise, the cursor is clamped to the
1082 * end of the screen and attempts to write past it are ignored.
1083 *
1084 * Defaults to on.
1085 *
1086 * @param {boolean} state True to set wraparound mode, false to unset.
1087 */
1088hterm.Terminal.prototype.setWraparound = function(state) {
1089 this.options_.wraparound = state;
1090};
1091
1092/**
1093 * Set the reverse-wraparound mode bit.
1094 *
1095 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1096 * to the end of the previous row. Otherwise, the cursor is clamped to column
1097 * 0.
1098 *
1099 * Defaults to off.
1100 *
1101 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1102 */
1103hterm.Terminal.prototype.setReverseWraparound = function(state) {
1104 this.options_.reverseWraparound = state;
1105};
1106
1107/**
1108 * Selects between the primary and alternate screens.
1109 *
1110 * If alternate mode is on, the alternate screen is active. Otherwise the
1111 * primary screen is active.
1112 *
1113 * Swapping screens has no effect on the scrollback buffer.
1114 *
1115 * Each screen maintains its own cursor position.
1116 *
1117 * Defaults to off.
1118 *
1119 * @param {boolean} state True to set alternate mode, false to unset.
1120 */
1121hterm.Terminal.prototype.setAlternateMode = function(state) {
1122 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1123
1124 this.screen_.setColumnCount(this.screenSize.width);
1125
1126 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1127 if (rowDelta > 0)
1128 this.appendRows_(rowDelta);
1129
1130 this.scrollPort_.invalidateRowRange(
1131 this.scrollbackRows_.length,
1132 this.scrollbackRows_.length + this.screenSize.height);
1133
rginda8ba33642011-12-14 12:31:31 -08001134 this.syncCursorPosition_();
1135};
1136
1137/**
1138 * Set the cursor-blink mode bit.
1139 *
1140 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1141 * a visible cursor does not blink.
1142 *
1143 * You should make sure to turn blinking off if you're going to dispose of a
1144 * terminal, otherwise you'll leak a timeout.
1145 *
1146 * Defaults to on.
1147 *
1148 * @param {boolean} state True to set cursor-blink mode, false to unset.
1149 */
1150hterm.Terminal.prototype.setCursorBlink = function(state) {
1151 this.options_.cursorBlink = state;
1152
1153 if (!state && this.timeouts_.cursorBlink) {
1154 clearTimeout(this.timeouts_.cursorBlink);
1155 delete this.timeouts_.cursorBlink;
1156 }
1157
1158 if (this.options_.cursorVisible)
1159 this.setCursorVisible(true);
1160};
1161
1162/**
1163 * Set the cursor-visible mode bit.
1164 *
1165 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1166 *
1167 * Defaults to on.
1168 *
1169 * @param {boolean} state True to set cursor-visible mode, false to unset.
1170 */
1171hterm.Terminal.prototype.setCursorVisible = function(state) {
1172 this.options_.cursorVisible = state;
1173
1174 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001175 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001176 return;
1177 }
1178
rginda87b86462011-12-14 13:48:03 -08001179 this.syncCursorPosition_();
1180
1181 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001182
1183 if (this.options_.cursorBlink) {
1184 if (this.timeouts_.cursorBlink)
1185 return;
1186
1187 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1188 500);
1189 } else {
1190 if (this.timeouts_.cursorBlink) {
1191 clearTimeout(this.timeouts_.cursorBlink);
1192 delete this.timeouts_.cursorBlink;
1193 }
1194 }
1195};
1196
1197/**
rginda87b86462011-12-14 13:48:03 -08001198 * Synchronizes the visible cursor and document selection with the current
1199 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001200 */
1201hterm.Terminal.prototype.syncCursorPosition_ = function() {
1202 var topRowIndex = this.scrollPort_.getTopRowIndex();
1203 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1204 var cursorRowIndex = this.scrollbackRows_.length +
1205 this.screen_.cursorPosition.row;
1206
1207 if (cursorRowIndex > bottomRowIndex) {
1208 // Cursor is scrolled off screen, move it outside of the visible area.
1209 this.cursorNode_.style.top = -this.characterSize_.height;
1210 return;
1211 }
1212
1213 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1214 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1215 this.cursorNode_.style.left = this.characterSize_.width *
1216 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001217
1218 this.cursorNode_.setAttribute('title',
1219 '(' + this.screen_.cursorPosition.row +
1220 ', ' + this.screen_.cursorPosition.column +
1221 ')');
1222
1223 // Update the caret for a11y purposes.
1224 var selection = this.document_.getSelection();
1225 if (selection && selection.isCollapsed)
1226 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001227};
1228
1229/**
1230 * Synchronizes the visible cursor with the current cursor coordinates.
1231 *
1232 * The sync will happen asynchronously, soon after the call stack winds down.
1233 * Multiple calls will be coalesced into a single sync.
1234 */
1235hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1236 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001237 return;
rginda8ba33642011-12-14 12:31:31 -08001238
1239 var self = this;
1240 this.timeouts_.syncCursor = setTimeout(function() {
1241 self.syncCursorPosition_();
1242 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001243 }, 0);
1244};
1245
1246/**
1247 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1248 *
1249 * @param {string} string The VT string representing the keystroke.
1250 */
1251hterm.Terminal.prototype.onVTKeystroke = function(string) {
1252 if (this.scrollOnKeystroke)
1253 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1254
1255 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001256};
1257
1258/**
1259 * React when the ScrollPort is scrolled.
1260 */
1261hterm.Terminal.prototype.onScroll_ = function() {
1262 this.scheduleSyncCursorPosition_();
1263};
1264
1265/**
1266 * React when the ScrollPort is resized.
1267 */
1268hterm.Terminal.prototype.onResize_ = function() {
1269 var width = Math.floor(this.scrollPort_.getScreenWidth() /
1270 this.characterSize_.width);
1271 var height = this.scrollPort_.visibleRowCount;
1272
rginda87b86462011-12-14 13:48:03 -08001273 if (width == this.screenSize.width && height == this.screenSize.height) {
1274 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001275 return;
rginda87b86462011-12-14 13:48:03 -08001276 }
rginda8ba33642011-12-14 12:31:31 -08001277
1278 this.screenSize.resize(width, height);
1279
1280 var screenHeight = this.screen_.getHeight();
1281
1282 var deltaRows = this.screenSize.height - screenHeight;
1283
rginda87b86462011-12-14 13:48:03 -08001284 var cursor = this.saveCursor();
1285
rginda8ba33642011-12-14 12:31:31 -08001286 if (deltaRows < 0) {
1287 // Screen got smaller.
rginda87b86462011-12-14 13:48:03 -08001288 deltaRows *= -1;
1289 while (deltaRows) {
1290 var lastRow = this.getRowCount() - 1;
1291 if (lastRow - this.scrollbackRows_.length == cursor.row)
1292 break;
1293
1294 if (this.getRowText(lastRow))
1295 break;
1296
1297 this.screen_.popRow();
1298 deltaRows--;
1299 }
1300
1301 var ary = this.screen_.shiftRows(deltaRows);
rginda8ba33642011-12-14 12:31:31 -08001302 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
rginda87b86462011-12-14 13:48:03 -08001303
1304 // We just removed rows from the top of the screen, we need to update
1305 // the cursor to match.
1306 cursor.row -= deltaRows;
1307
rginda8ba33642011-12-14 12:31:31 -08001308 } else if (deltaRows > 0) {
1309 // Screen got larger.
1310
1311 if (deltaRows <= this.scrollbackRows_.length) {
1312 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1313 var rows = this.scrollbackRows_.splice(
rginda87b86462011-12-14 13:48:03 -08001314 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
rginda8ba33642011-12-14 12:31:31 -08001315 this.screen_.unshiftRows(rows);
1316 deltaRows -= scrollbackCount;
rginda87b86462011-12-14 13:48:03 -08001317 cursor.row += scrollbackCount;
rginda8ba33642011-12-14 12:31:31 -08001318 }
1319
1320 if (deltaRows)
1321 this.appendRows_(deltaRows);
1322 }
1323
1324 this.screen_.setColumnCount(this.screenSize.width);
rginda87b86462011-12-14 13:48:03 -08001325 this.restoreCursor(cursor);
1326 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001327};
1328
1329/**
1330 * Service the cursor blink timeout.
1331 */
1332hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001333 if (this.cursorNode_.style.opacity == '0') {
1334 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001335 } else {
rginda87b86462011-12-14 13:48:03 -08001336 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001337 }
1338};