blob: 2aec839cfb64f0057e102b61b5d74edcaf11975d [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
rginda0f5c0292012-01-13 11:00:13 -080071 // If true, scroll to the bottom on terminal output.
72 this.scrollOnOutput = false;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The current mode bits for the terminal.
75 this.options_ = new hterm.Options();
76
77 // Timeouts we might need to clear.
78 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080079
80 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080081 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080082
83 // General IO interface that can be given to third parties without exposing
84 // the entire terminal object.
85 this.io = new hterm.Terminal.IO(this);
86};
87
88/**
89 * Create a new instance of a terminal command and run it with a given
90 * argument string.
91 *
92 * @param {function} commandClass The constructor for a terminal command.
93 * @param {string} argString The argument string to pass to the command.
94 */
95hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
96 var self = this;
97 this.command = new commandClass(
98 { argString: argString || '',
99 io: this.io.push(),
100 onExit: function(code) {
101 self.io.pop();
102 self.io.println(hterm.msg('COMMAND_COMPLETE',
103 [self.command.commandName, code]));
104 }
105 });
106
107 this.command.run();
108};
109
110/**
111 * Return a copy of the current cursor position.
112 *
113 * @return {hterm.RowCol} The RowCol object representing the current position.
114 */
115hterm.Terminal.prototype.saveCursor = function() {
116 return this.screen_.cursorPosition.clone();
117};
118
119/**
120 * Restore a previously saved cursor position.
121 *
122 * @param {hterm.RowCol} cursor The position to restore.
123 */
124hterm.Terminal.prototype.restoreCursor = function(cursor) {
125 this.screen_.setCursorPosition(cursor.row, cursor.column);
rginda2312fff2012-01-05 16:20:52 -0800126 this.screen_.cursorPosition.overflow = cursor.overflow;
rginda87b86462011-12-14 13:48:03 -0800127};
128
129/**
130 * Set the width of the terminal, resizing the UI to match.
131 */
132hterm.Terminal.prototype.setWidth = function(columnCount) {
133 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px'
134
135 // The resizing of the UI will happen asynchronously, so we need to take
136 // care of this bookeeping here instead of letting the resize handlers deal
137 // with it.
138 this.screenSize.width = columnCount;
139 this.screen_.setColumnCount(columnCount);
140};
141
142/**
143 * Scroll the terminal to the top of the scrollback buffer.
144 */
145hterm.Terminal.prototype.scrollHome = function() {
146 this.scrollPort_.scrollRowToTop(0);
147};
148
149/**
150 * Scroll the terminal to the end.
151 */
152hterm.Terminal.prototype.scrollEnd = function() {
153 this.scrollPort_.scrollRowToBottom(this.getRowCount());
154};
155
156/**
157 * Scroll the terminal one page up (minus one line) relative to the current
158 * position.
159 */
160hterm.Terminal.prototype.scrollPageUp = function() {
161 var i = this.scrollPort_.getTopRowIndex();
162 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
163};
164
165/**
166 * Scroll the terminal one page down (minus one line) relative to the current
167 * position.
168 */
169hterm.Terminal.prototype.scrollPageDown = function() {
170 var i = this.scrollPort_.getTopRowIndex();
171 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800172};
173
rginda87b86462011-12-14 13:48:03 -0800174hterm.Terminal.prototype.reset = function() {
175 console.log('reset');
176};
177
rginda0f5c0292012-01-13 11:00:13 -0800178hterm.Terminal.prototype.softReset = function() {
179 console.log('softReset');
180};
181
rginda87b86462011-12-14 13:48:03 -0800182hterm.Terminal.prototype.clearColorAndAttributes = function() {
183 //console.log('clearColorAndAttributes');
184};
185
186hterm.Terminal.prototype.setForegroundColor256 = function() {
187 console.log('setForegroundColor256');
188};
189
190hterm.Terminal.prototype.setBackgroundColor256 = function() {
191 console.log('setBackgroundColor256');
192};
193
194hterm.Terminal.prototype.setForegroundColor = function() {
195 //console.log('setForegroundColor');
196};
197
198hterm.Terminal.prototype.setBackgroundColor = function() {
199 //console.log('setBackgroundColor');
200};
201
202hterm.Terminal.prototype.setAttributes = function() {
203 //console.log('setAttributes');
204};
205
206hterm.Terminal.prototype.resize = function() {
207 console.log('resize');
208};
209
210hterm.Terminal.prototype.setSpecialCharsEnabled = function() {
211 //console.log('setSpecialCharactersEnabled');
212};
213
rginda0f5c0292012-01-13 11:00:13 -0800214hterm.Terminal.prototype.forwardTabStop = function(count) {
215 this.cursorRight(4);
216};
217
218hterm.Terminal.prototype.backwardTabStop = function(count) {
219 console.error('Not implemented: backwardTabStop');
220};
221
rginda87b86462011-12-14 13:48:03 -0800222hterm.Terminal.prototype.setTabStopAtCursor = function() {
223 console.log('setTabStopAtCursor');
224};
225
226hterm.Terminal.prototype.clearTabStops = function() {
227 console.log('clearTabStops');
228};
229
230hterm.Terminal.prototype.saveOptions = function() {
231 console.log('saveOptions');
232};
233
234hterm.Terminal.prototype.restoreOptions = function() {
235 console.log('restoreOptions');
rginda8ba33642011-12-14 12:31:31 -0800236};
237
238/**
239 * Interpret a sequence of characters.
240 *
241 * Incomplete escape sequences are buffered until the next call.
242 *
243 * @param {string} str Sequence of characters to interpret or pass through.
244 */
245hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800246 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800247 this.scheduleSyncCursorPosition_();
248};
249
250/**
251 * Take over the given DIV for use as the terminal display.
252 *
253 * @param {HTMLDivElement} div The div to use as the terminal display.
254 */
255hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800256 this.div_ = div;
257
rginda8ba33642011-12-14 12:31:31 -0800258 this.scrollPort_.decorate(div);
259 this.document_ = this.scrollPort_.getDocument();
260
261 // Get character dimensions from the scrollPort.
262 this.characterSize_.height = this.scrollPort_.getRowHeight();
263 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
264
265 this.cursorNode_ = this.document_.createElement('div');
266 this.cursorNode_.style.cssText =
267 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800268 'top: -99px;' +
269 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800270 'width: ' + this.characterSize_.width + 'px;' +
271 'height: ' + this.characterSize_.height + 'px;' +
rginda87b86462011-12-14 13:48:03 -0800272 '-webkit-transition: opacity 100ms ease-in;' +
rginda8ba33642011-12-14 12:31:31 -0800273 'background-color: ' + this.cursorColor);
274 this.document_.body.appendChild(this.cursorNode_);
275
276 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800277
278 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
279
280 var link = this.document_.createElement('link');
281 link.setAttribute('href', '../css/dialogs.css');
282 link.setAttribute('rel', 'stylesheet');
283 this.document_.head.appendChild(link);
284
285 this.alertDialog = new AlertDialog(this.document_.body);
286 this.promptDialog = new PromptDialog(this.document_.body);
287 this.confirmDialog = new ConfirmDialog(this.document_.body);
288
289 this.scrollPort_.focus();
290};
291
292hterm.Terminal.prototype.getDocument = function() {
293 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800294};
295
296/**
297 * Return the HTML Element for a given row index.
298 *
299 * This is a method from the RowProvider interface. The ScrollPort uses
300 * it to fetch rows on demand as they are scrolled into view.
301 *
302 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
303 * pairs to conserve memory.
304 *
305 * @param {integer} index The zero-based row index, measured relative to the
306 * start of the scrollback buffer. On-screen rows will always have the
307 * largest indicies.
308 * @return {HTMLElement} The 'x-row' element containing for the requested row.
309 */
310hterm.Terminal.prototype.getRowNode = function(index) {
311 if (index < this.scrollbackRows_.length)
312 return this.scrollbackRows_[index];
313
314 var screenIndex = index - this.scrollbackRows_.length;
315 return this.screen_.rowsArray[screenIndex];
316};
317
318/**
319 * Return the text content for a given range of rows.
320 *
321 * This is a method from the RowProvider interface. The ScrollPort uses
322 * it to fetch text content on demand when the user attempts to copy their
323 * selection to the clipboard.
324 *
325 * @param {integer} start The zero-based row index to start from, measured
326 * relative to the start of the scrollback buffer. On-screen rows will
327 * always have the largest indicies.
328 * @param {integer} end The zero-based row index to end on, measured
329 * relative to the start of the scrollback buffer.
330 * @return {string} A single string containing the text value of the range of
331 * rows. Lines will be newline delimited, with no trailing newline.
332 */
333hterm.Terminal.prototype.getRowsText = function(start, end) {
334 var ary = [];
335 for (var i = start; i < end; i++) {
336 var node = this.getRowNode(i);
337 ary.push(node.textContent);
338 }
339
340 return ary.join('\n');
341};
342
343/**
344 * Return the text content for a given row.
345 *
346 * This is a method from the RowProvider interface. The ScrollPort uses
347 * it to fetch text content on demand when the user attempts to copy their
348 * selection to the clipboard.
349 *
350 * @param {integer} index The zero-based row index to return, measured
351 * relative to the start of the scrollback buffer. On-screen rows will
352 * always have the largest indicies.
353 * @return {string} A string containing the text value of the selected row.
354 */
355hterm.Terminal.prototype.getRowText = function(index) {
356 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800357 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800358};
359
360/**
361 * Return the total number of rows in the addressable screen and in the
362 * scrollback buffer of this terminal.
363 *
364 * This is a method from the RowProvider interface. The ScrollPort uses
365 * it to compute the size of the scrollbar.
366 *
367 * @return {integer} The number of rows in this terminal.
368 */
369hterm.Terminal.prototype.getRowCount = function() {
370 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
371};
372
373/**
374 * Create DOM nodes for new rows and append them to the end of the terminal.
375 *
376 * This is the only correct way to add a new DOM node for a row. Notice that
377 * the new row is appended to the bottom of the list of rows, and does not
378 * require renumbering (of the rowIndex property) of previous rows.
379 *
380 * If you think you want a new blank row somewhere in the middle of the
381 * terminal, look into moveRows_().
382 *
383 * This method does not pay attention to vtScrollTop/Bottom, since you should
384 * be using moveRows() in cases where they would matter.
385 *
386 * The cursor will be positioned at column 0 of the first inserted line.
387 */
388hterm.Terminal.prototype.appendRows_ = function(count) {
389 var cursorRow = this.screen_.rowsArray.length;
390 var offset = this.scrollbackRows_.length + cursorRow;
391 for (var i = 0; i < count; i++) {
392 var row = this.document_.createElement('x-row');
393 row.appendChild(this.document_.createTextNode(''));
394 row.rowIndex = offset + i;
395 this.screen_.pushRow(row);
396 }
397
398 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
399 if (extraRows > 0) {
400 var ary = this.screen_.shiftRows(extraRows);
401 Array.prototype.push.apply(this.scrollbackRows_, ary);
402 this.scheduleScrollDown_();
403 }
404
405 if (cursorRow >= this.screen_.rowsArray.length)
406 cursorRow = this.screen_.rowsArray.length - 1;
407
rginda87b86462011-12-14 13:48:03 -0800408 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800409};
410
411/**
412 * Relocate rows from one part of the addressable screen to another.
413 *
414 * This is used to recycle rows during VT scrolls (those which are driven
415 * by VT commands, rather than by the user manipulating the scrollbar.)
416 *
417 * In this case, the blank lines scrolled into the scroll region are made of
418 * the nodes we scrolled off. These have their rowIndex properties carefully
419 * renumbered so as not to confuse the ScrollPort.
420 *
421 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
422 * It may just be luck. I wouldn't be surprised if we actually needed to call
423 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
424 * adding it.
425 */
426hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
427 var ary = this.screen_.removeRows(fromIndex, count);
428 this.screen_.insertRows(toIndex, ary);
429
430 var start, end;
431 if (fromIndex < toIndex) {
432 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800433 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800434 } else {
435 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800436 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800437 }
438
439 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800440 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800441};
442
443/**
444 * Renumber the rowIndex property of the given range of rows.
445 *
446 * The start and end indicies are relative to the screen, not the scrollback.
447 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800448 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800449 * no need to renumber scrollback rows.
450 */
451hterm.Terminal.prototype.renumberRows_ = function(start, end) {
452 var offset = this.scrollbackRows_.length;
453 for (var i = start; i < end; i++) {
454 this.screen_.rowsArray[i].rowIndex = offset + i;
455 }
456};
457
458/**
459 * Print a string to the terminal.
460 *
461 * This respects the current insert and wraparound modes. It will add new lines
462 * to the end of the terminal, scrolling off the top into the scrollback buffer
463 * if necessary.
464 *
465 * The string is *not* parsed for escape codes. Use the interpret() method if
466 * that's what you're after.
467 *
468 * @param{string} str The string to print.
469 */
470hterm.Terminal.prototype.print = function(str) {
471 do {
rginda2312fff2012-01-05 16:20:52 -0800472 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
473 this.newLine();
474
rginda8ba33642011-12-14 12:31:31 -0800475 if (this.options_.insertMode) {
476 str = this.screen_.insertString(str);
477 } else {
478 str = this.screen_.overwriteString(str);
479 }
rginda2312fff2012-01-05 16:20:52 -0800480 } while (this.options_.wraparound && str);
rginda8ba33642011-12-14 12:31:31 -0800481
482 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800483
484 if (this.scrollOnOutput)
485 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800486};
487
488/**
rginda87b86462011-12-14 13:48:03 -0800489 * Set the VT scroll region.
490 *
rginda87b86462011-12-14 13:48:03 -0800491 * This also resets the cursor position to the absolute (0, 0) position, since
492 * that's what xterm appears to do.
493 *
494 * @param {integer} scrollTop The zero-based top of the scroll region.
495 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
496 * inclusive.
497 */
498hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
499 this.vtScrollTop_ = scrollTop;
500 this.vtScrollBottom_ = scrollBottom;
501 this.setAbsoluteCursorPosition(0, 0);
502};
503
504/**
rginda8ba33642011-12-14 12:31:31 -0800505 * Return the top row index according to the VT.
506 *
507 * This will return 0 unless the terminal has been told to restrict scrolling
508 * to some lower row. It is used for some VT cursor positioning and scrolling
509 * commands.
510 *
511 * @return {integer} The topmost row in the terminal's scroll region.
512 */
513hterm.Terminal.prototype.getVTScrollTop = function() {
514 if (this.vtScrollTop_ != null)
515 return this.vtScrollTop_;
516
517 return 0;
rginda87b86462011-12-14 13:48:03 -0800518};
rginda8ba33642011-12-14 12:31:31 -0800519
520/**
521 * Return the bottom row index according to the VT.
522 *
523 * This will return the height of the terminal unless the it has been told to
524 * restrict scrolling to some higher row. It is used for some VT cursor
525 * positioning and scrolling commands.
526 *
527 * @return {integer} The bottommost row in the terminal's scroll region.
528 */
529hterm.Terminal.prototype.getVTScrollBottom = function() {
530 if (this.vtScrollBottom_ != null)
531 return this.vtScrollBottom_;
532
rginda87b86462011-12-14 13:48:03 -0800533 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800534}
535
536/**
537 * Process a '\n' character.
538 *
539 * If the cursor is on the final row of the terminal this will append a new
540 * blank row to the screen and scroll the topmost row into the scrollback
541 * buffer.
542 *
543 * Otherwise, this moves the cursor to column zero of the next row.
544 */
545hterm.Terminal.prototype.newLine = function() {
546 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800547 // If we're at the end of the screen we need to append a new line and
548 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800549 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800550 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
551 // End of the scroll region does not affect the scrollback buffer.
552 this.vtScrollUp(1);
553 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800554 } else {
rginda87b86462011-12-14 13:48:03 -0800555 // Anywhere else in the screen just moves the cursor.
556 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800557 }
558};
559
560/**
561 * Like newLine(), except maintain the cursor column.
562 */
563hterm.Terminal.prototype.lineFeed = function() {
564 var column = this.screen_.cursorPosition.column;
565 this.newLine();
566 this.setCursorColumn(column);
567};
568
569/**
rginda87b86462011-12-14 13:48:03 -0800570 * If autoCarriageReturn is set then newLine(), else lineFeed().
571 */
572hterm.Terminal.prototype.formFeed = function() {
573 if (this.options_.autoCarriageReturn) {
574 this.newLine();
575 } else {
576 this.lineFeed();
577 }
578};
579
580/**
581 * Move the cursor up one row, possibly inserting a blank line.
582 *
583 * The cursor column is not changed.
584 */
585hterm.Terminal.prototype.reverseLineFeed = function() {
586 var scrollTop = this.getVTScrollTop();
587 var currentRow = this.screen_.cursorPosition.row;
588
589 if (currentRow == scrollTop) {
590 this.insertLines(1);
591 } else {
592 this.setAbsoluteCursorRow(currentRow - 1);
593 }
594};
595
596/**
rginda8ba33642011-12-14 12:31:31 -0800597 * Replace all characters to the left of the current cursor with the space
598 * character.
599 *
600 * TODO(rginda): This should probably *remove* the characters (not just replace
601 * with a space) if there are no characters at or beyond the current cursor
602 * position. Once it does that, it'll have the same text-attribute related
603 * issues as hterm.Screen.prototype.clearCursorRow :/
604 */
605hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800606 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800607 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800608 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
609 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800610};
611
612/**
613 * Erase a given number of characters to the right of the cursor, shifting
614 * remaining characters to the left.
615 *
616 * The cursor position is unchanged.
617 *
618 * TODO(rginda): Test that this works even when the cursor is positioned beyond
619 * the end of the text.
620 *
621 * TODO(rginda): This likely has text-attribute related troubles similar to the
622 * todo on hterm.Screen.prototype.clearCursorRow.
623 */
624hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800625 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800626
rginda87b86462011-12-14 13:48:03 -0800627 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800628 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
629 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800630 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800631};
632
633/**
634 * Erase the current line.
635 *
636 * The cursor position is unchanged.
637 *
638 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
639 * has a text-attribute related TODO.
640 */
641hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800642 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800643 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800644 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800645};
646
647/**
648 * Erase all characters from the start of the scroll region to the current
649 * cursor position.
650 *
651 * The cursor position is unchanged.
652 *
653 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
654 * has a text-attribute related TODO.
655 */
656hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800657 var cursor = this.saveCursor();
658
659 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800660
661 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800662 for (var i = top; i < cursor.row; i++) {
663 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800664 this.screen_.clearCursorRow();
665 }
666
rginda87b86462011-12-14 13:48:03 -0800667 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800668};
669
670/**
671 * Erase all characters from the current cursor position to the end of the
672 * scroll region.
673 *
674 * The cursor position is unchanged.
675 *
676 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
677 * has a text-attribute related TODO.
678 */
679hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800680 var cursor = this.saveCursor();
681
682 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800683
684 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800685 for (var i = cursor.row + 1; i <= bottom; i++) {
686 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800687 this.screen_.clearCursorRow();
688 }
689
rginda87b86462011-12-14 13:48:03 -0800690 this.restoreCursor(cursor);
691};
692
693/**
694 * Fill the terminal with a given character.
695 *
696 * This methods does not respect the VT scroll region.
697 *
698 * @param {string} ch The character to use for the fill.
699 */
700hterm.Terminal.prototype.fill = function(ch) {
701 var cursor = this.saveCursor();
702
703 this.setAbsoluteCursorPosition(0, 0);
704 for (var row = 0; row < this.screenSize.height; row++) {
705 for (var col = 0; col < this.screenSize.width; col++) {
706 this.setAbsoluteCursorPosition(row, col);
707 this.screen_.overwriteString(ch);
708 }
709 }
710
711 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800712};
713
714/**
715 * Erase the entire scroll region.
716 *
717 * The cursor position is unchanged.
718 *
719 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
720 * has a text-attribute related TODO.
721 */
722hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800723 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800724
725 var top = this.getVTScrollTop();
726 var bottom = this.getVTScrollBottom();
727
728 for (var i = top; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800729 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800730 this.screen_.clearCursorRow();
731 }
732
rginda87b86462011-12-14 13:48:03 -0800733 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800734};
735
736/**
737 * VT command to insert lines at the current cursor row.
738 *
739 * This respects the current scroll region. Rows pushed off the bottom are
740 * lost (they won't show up in the scrollback buffer).
741 *
742 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
743 * has a text-attribute related TODO.
744 *
745 * @param {integer} count The number of lines to insert.
746 */
747hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800748 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800749
750 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800751 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800752
753 var start = bottom - count;
rginda87b86462011-12-14 13:48:03 -0800754 if (start != cursor.row)
755 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800756
757 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800758 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800759 this.screen_.clearCursorRow();
760 }
761
rginda87b86462011-12-14 13:48:03 -0800762 cursor.column = 0;
763 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800764};
765
766/**
767 * VT command to delete lines at the current cursor row.
768 *
769 * New rows are added to the bottom of scroll region to take their place. New
770 * rows are strictly there to take up space and have no content or style.
771 */
772hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800773 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800774
rginda87b86462011-12-14 13:48:03 -0800775 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -0800776 var bottom = this.getVTScrollBottom();
777
rginda87b86462011-12-14 13:48:03 -0800778 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -0800779 count = Math.min(count, maxCount);
780
rginda87b86462011-12-14 13:48:03 -0800781 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -0800782 if (count != maxCount)
783 this.moveRows_(top, count, moveStart);
784
785 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800786 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800787 this.screen_.clearCursorRow();
788 }
789
rginda87b86462011-12-14 13:48:03 -0800790 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800791};
792
793/**
794 * Inserts the given number of spaces at the current cursor position.
795 *
rginda87b86462011-12-14 13:48:03 -0800796 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -0800797 */
798hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -0800799 var cursor = this.saveCursor();
800
rginda0f5c0292012-01-13 11:00:13 -0800801 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -0800802 this.screen_.insertString(ws);
rginda87b86462011-12-14 13:48:03 -0800803
804 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800805};
806
807/**
808 * Forward-delete the specified number of characters starting at the cursor
809 * position.
810 *
811 * @param {integer} count The number of characters to delete.
812 */
813hterm.Terminal.prototype.deleteChars = function(count) {
814 this.screen_.deleteChars(count);
815};
816
817/**
818 * Shift rows in the scroll region upwards by a given number of lines.
819 *
820 * New rows are inserted at the bottom of the scroll region to fill the
821 * vacated rows. The new rows not filled out with the current text attributes.
822 *
823 * This function does not affect the scrollback rows at all. Rows shifted
824 * off the top are lost.
825 *
rginda87b86462011-12-14 13:48:03 -0800826 * The cursor position is not altered.
827 *
rginda8ba33642011-12-14 12:31:31 -0800828 * @param {integer} count The number of rows to scroll.
829 */
830hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -0800831 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800832
rginda87b86462011-12-14 13:48:03 -0800833 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -0800834 this.deleteLines(count);
835
rginda87b86462011-12-14 13:48:03 -0800836 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800837};
838
839/**
840 * Shift rows below the cursor down by a given number of lines.
841 *
842 * This function respects the current scroll region.
843 *
844 * New rows are inserted at the top of the scroll region to fill the
845 * vacated rows. The new rows not filled out with the current text attributes.
846 *
847 * This function does not affect the scrollback rows at all. Rows shifted
848 * off the bottom are lost.
849 *
850 * @param {integer} count The number of rows to scroll.
851 */
852hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800853 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800854
rginda87b86462011-12-14 13:48:03 -0800855 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -0800856 this.insertLines(opt_count);
857
rginda87b86462011-12-14 13:48:03 -0800858 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800859};
860
rginda87b86462011-12-14 13:48:03 -0800861
rginda8ba33642011-12-14 12:31:31 -0800862/**
863 * Set the cursor position.
864 *
865 * The cursor row is relative to the scroll region if the terminal has
866 * 'origin mode' enabled, or relative to the addressable screen otherwise.
867 *
868 * @param {integer} row The new zero-based cursor row.
869 * @param {integer} row The new zero-based cursor column.
870 */
871hterm.Terminal.prototype.setCursorPosition = function(row, column) {
872 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -0800873 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800874 } else {
rginda87b86462011-12-14 13:48:03 -0800875 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800876 }
rginda87b86462011-12-14 13:48:03 -0800877};
rginda8ba33642011-12-14 12:31:31 -0800878
rginda87b86462011-12-14 13:48:03 -0800879hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
880 var scrollTop = this.getVTScrollTop();
881 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -0800882 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -0800883 this.screen_.setCursorPosition(row, column);
884};
885
886hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -0800887 row = hterm.clamp(row, 0, this.screenSize.height - 1);
888 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -0800889 this.screen_.setCursorPosition(row, column);
890};
891
892/**
893 * Set the cursor column.
894 *
895 * @param {integer} column The new zero-based cursor column.
896 */
897hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -0800898 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -0800899};
900
901/**
902 * Return the cursor column.
903 *
904 * @return {integer} The zero-based cursor column.
905 */
906hterm.Terminal.prototype.getCursorColumn = function() {
907 return this.screen_.cursorPosition.column;
908};
909
910/**
911 * Set the cursor row.
912 *
913 * The cursor row is relative to the scroll region if the terminal has
914 * 'origin mode' enabled, or relative to the addressable screen otherwise.
915 *
916 * @param {integer} row The new cursor row.
917 */
rginda87b86462011-12-14 13:48:03 -0800918hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
919 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -0800920};
921
922/**
923 * Return the cursor row.
924 *
925 * @return {integer} The zero-based cursor row.
926 */
927hterm.Terminal.prototype.getCursorRow = function(row) {
928 return this.screen_.cursorPosition.row;
929};
930
931/**
932 * Request that the ScrollPort redraw itself soon.
933 *
934 * The redraw will happen asynchronously, soon after the call stack winds down.
935 * Multiple calls will be coalesced into a single redraw.
936 */
937hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -0800938 if (this.timeouts_.redraw)
939 return;
rginda8ba33642011-12-14 12:31:31 -0800940
941 var self = this;
rginda87b86462011-12-14 13:48:03 -0800942 this.timeouts_.redraw = setTimeout(function() {
943 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -0800944 self.scrollPort_.redraw_();
945 }, 0);
946};
947
948/**
949 * Request that the ScrollPort be scrolled to the bottom.
950 *
951 * The scroll will happen asynchronously, soon after the call stack winds down.
952 * Multiple calls will be coalesced into a single scroll.
953 *
954 * This affects the scrollbar position of the ScrollPort, and has nothing to
955 * do with the VT scroll commands.
956 */
957hterm.Terminal.prototype.scheduleScrollDown_ = function() {
958 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -0800959 return;
rginda8ba33642011-12-14 12:31:31 -0800960
961 var self = this;
962 this.timeouts_.scrollDown = setTimeout(function() {
963 delete self.timeouts_.scrollDown;
964 self.scrollPort_.scrollRowToBottom(self.getRowCount());
965 }, 10);
966};
967
968/**
969 * Move the cursor up a specified number of rows.
970 *
971 * @param {integer} count The number of rows to move the cursor.
972 */
973hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -0800974 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -0800975};
976
977/**
978 * Move the cursor down a specified number of rows.
979 *
980 * @param {integer} count The number of rows to move the cursor.
981 */
982hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -0800983 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -0800984 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
985 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
986 this.screenSize.height - 1);
987
988 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
989 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -0800990 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -0800991};
992
993/**
994 * Move the cursor left a specified number of columns.
995 *
996 * @param {integer} count The number of columns to move the cursor.
997 */
998hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -0800999 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001000};
1001
1002/**
1003 * Move the cursor right a specified number of columns.
1004 *
1005 * @param {integer} count The number of columns to move the cursor.
1006 */
1007hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001008 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001009 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001010 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001011 this.setCursorColumn(column);
1012};
1013
1014/**
1015 * Reverse the foreground and background colors of the terminal.
1016 *
1017 * This only affects text that was drawn with no attributes.
1018 *
1019 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1020 * been drawn with attributes that happen to coincide with the default
1021 * 'no-attribute' colors. My guess is probably not.
1022 */
1023hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001024 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001025 if (state) {
1026 this.scrollPort_.setForegroundColor(this.backgroundColor);
1027 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1028 } else {
1029 this.scrollPort_.setForegroundColor(this.foregroundColor);
1030 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1031 }
1032};
1033
1034/**
rginda87b86462011-12-14 13:48:03 -08001035 * Ring the terminal bell.
1036 *
1037 * We only have a visual bell, which quickly toggles inverse video in the
1038 * terminal.
1039 */
1040hterm.Terminal.prototype.ringBell = function() {
1041 // We can't toggle using only setReverseVideo, since there's a chance we'll
1042 // get a request to toggle reverse video before our visual bell is over.
1043 var fg = this.scrollPort_.getForegroundColor();
1044 this.scrollPort_.setForegroundColor(this.scrollPort_.getBackgroundColor());
1045 this.scrollPort_.setBackgroundColor(fg);
1046
1047 var self = this;
1048 setTimeout(function() {
1049 self.setReverseVideo(self.options_.reverseVideo);
1050 }, 100);
1051};
1052
1053/**
rginda8ba33642011-12-14 12:31:31 -08001054 * Set the origin mode bit.
1055 *
1056 * If origin mode is on, certain VT cursor and scrolling commands measure their
1057 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1058 * to the top of the addressable screen.
1059 *
1060 * Defaults to off.
1061 *
1062 * @param {boolean} state True to set origin mode, false to unset.
1063 */
1064hterm.Terminal.prototype.setOriginMode = function(state) {
1065 this.options_.originMode = state;
1066};
1067
1068/**
1069 * Set the insert mode bit.
1070 *
1071 * If insert mode is on, existing text beyond the cursor position will be
1072 * shifted right to make room for new text. Otherwise, new text overwrites
1073 * any existing text.
1074 *
1075 * Defaults to off.
1076 *
1077 * @param {boolean} state True to set insert mode, false to unset.
1078 */
1079hterm.Terminal.prototype.setInsertMode = function(state) {
1080 this.options_.insertMode = state;
1081};
1082
1083/**
rginda87b86462011-12-14 13:48:03 -08001084 * Set the auto carriage return bit.
1085 *
1086 * If auto carriage return is on then a formfeed character is interpreted
1087 * as a newline, otherwise it's the same as a linefeed. The difference boils
1088 * down to whether or not the cursor column is reset.
1089 */
1090hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1091 this.options_.autoCarriageReturn = state;
1092};
1093
1094/**
rginda8ba33642011-12-14 12:31:31 -08001095 * Set the wraparound mode bit.
1096 *
1097 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1098 * to the start of the following row. Otherwise, the cursor is clamped to the
1099 * end of the screen and attempts to write past it are ignored.
1100 *
1101 * Defaults to on.
1102 *
1103 * @param {boolean} state True to set wraparound mode, false to unset.
1104 */
1105hterm.Terminal.prototype.setWraparound = function(state) {
1106 this.options_.wraparound = state;
1107};
1108
1109/**
1110 * Set the reverse-wraparound mode bit.
1111 *
1112 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1113 * to the end of the previous row. Otherwise, the cursor is clamped to column
1114 * 0.
1115 *
1116 * Defaults to off.
1117 *
1118 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1119 */
1120hterm.Terminal.prototype.setReverseWraparound = function(state) {
1121 this.options_.reverseWraparound = state;
1122};
1123
1124/**
1125 * Selects between the primary and alternate screens.
1126 *
1127 * If alternate mode is on, the alternate screen is active. Otherwise the
1128 * primary screen is active.
1129 *
1130 * Swapping screens has no effect on the scrollback buffer.
1131 *
1132 * Each screen maintains its own cursor position.
1133 *
1134 * Defaults to off.
1135 *
1136 * @param {boolean} state True to set alternate mode, false to unset.
1137 */
1138hterm.Terminal.prototype.setAlternateMode = function(state) {
1139 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1140
1141 this.screen_.setColumnCount(this.screenSize.width);
1142
1143 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1144 if (rowDelta > 0)
1145 this.appendRows_(rowDelta);
1146
rginda2312fff2012-01-05 16:20:52 -08001147 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001148 this.syncCursorPosition_();
1149};
1150
1151/**
1152 * Set the cursor-blink mode bit.
1153 *
1154 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1155 * a visible cursor does not blink.
1156 *
1157 * You should make sure to turn blinking off if you're going to dispose of a
1158 * terminal, otherwise you'll leak a timeout.
1159 *
1160 * Defaults to on.
1161 *
1162 * @param {boolean} state True to set cursor-blink mode, false to unset.
1163 */
1164hterm.Terminal.prototype.setCursorBlink = function(state) {
1165 this.options_.cursorBlink = state;
1166
1167 if (!state && this.timeouts_.cursorBlink) {
1168 clearTimeout(this.timeouts_.cursorBlink);
1169 delete this.timeouts_.cursorBlink;
1170 }
1171
1172 if (this.options_.cursorVisible)
1173 this.setCursorVisible(true);
1174};
1175
1176/**
1177 * Set the cursor-visible mode bit.
1178 *
1179 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1180 *
1181 * Defaults to on.
1182 *
1183 * @param {boolean} state True to set cursor-visible mode, false to unset.
1184 */
1185hterm.Terminal.prototype.setCursorVisible = function(state) {
1186 this.options_.cursorVisible = state;
1187
1188 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001189 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001190 return;
1191 }
1192
rginda87b86462011-12-14 13:48:03 -08001193 this.syncCursorPosition_();
1194
1195 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001196
1197 if (this.options_.cursorBlink) {
1198 if (this.timeouts_.cursorBlink)
1199 return;
1200
1201 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1202 500);
1203 } else {
1204 if (this.timeouts_.cursorBlink) {
1205 clearTimeout(this.timeouts_.cursorBlink);
1206 delete this.timeouts_.cursorBlink;
1207 }
1208 }
1209};
1210
1211/**
rginda87b86462011-12-14 13:48:03 -08001212 * Synchronizes the visible cursor and document selection with the current
1213 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001214 */
1215hterm.Terminal.prototype.syncCursorPosition_ = function() {
1216 var topRowIndex = this.scrollPort_.getTopRowIndex();
1217 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1218 var cursorRowIndex = this.scrollbackRows_.length +
1219 this.screen_.cursorPosition.row;
1220
1221 if (cursorRowIndex > bottomRowIndex) {
1222 // Cursor is scrolled off screen, move it outside of the visible area.
1223 this.cursorNode_.style.top = -this.characterSize_.height;
1224 return;
1225 }
1226
1227 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1228 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1229 this.cursorNode_.style.left = this.characterSize_.width *
1230 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001231
1232 this.cursorNode_.setAttribute('title',
1233 '(' + this.screen_.cursorPosition.row +
1234 ', ' + this.screen_.cursorPosition.column +
1235 ')');
1236
1237 // Update the caret for a11y purposes.
1238 var selection = this.document_.getSelection();
1239 if (selection && selection.isCollapsed)
1240 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001241};
1242
1243/**
1244 * Synchronizes the visible cursor with the current cursor coordinates.
1245 *
1246 * The sync will happen asynchronously, soon after the call stack winds down.
1247 * Multiple calls will be coalesced into a single sync.
1248 */
1249hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1250 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001251 return;
rginda8ba33642011-12-14 12:31:31 -08001252
1253 var self = this;
1254 this.timeouts_.syncCursor = setTimeout(function() {
1255 self.syncCursorPosition_();
1256 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001257 }, 0);
1258};
1259
1260/**
1261 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1262 *
1263 * @param {string} string The VT string representing the keystroke.
1264 */
1265hterm.Terminal.prototype.onVTKeystroke = function(string) {
1266 if (this.scrollOnKeystroke)
1267 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1268
1269 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001270};
1271
1272/**
1273 * React when the ScrollPort is scrolled.
1274 */
1275hterm.Terminal.prototype.onScroll_ = function() {
1276 this.scheduleSyncCursorPosition_();
1277};
1278
1279/**
1280 * React when the ScrollPort is resized.
1281 */
1282hterm.Terminal.prototype.onResize_ = function() {
1283 var width = Math.floor(this.scrollPort_.getScreenWidth() /
1284 this.characterSize_.width);
1285 var height = this.scrollPort_.visibleRowCount;
1286
rginda87b86462011-12-14 13:48:03 -08001287 if (width == this.screenSize.width && height == this.screenSize.height) {
1288 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001289 return;
rginda87b86462011-12-14 13:48:03 -08001290 }
rginda8ba33642011-12-14 12:31:31 -08001291
1292 this.screenSize.resize(width, height);
1293
1294 var screenHeight = this.screen_.getHeight();
1295
1296 var deltaRows = this.screenSize.height - screenHeight;
1297
rginda87b86462011-12-14 13:48:03 -08001298 var cursor = this.saveCursor();
1299
rginda8ba33642011-12-14 12:31:31 -08001300 if (deltaRows < 0) {
1301 // Screen got smaller.
rginda87b86462011-12-14 13:48:03 -08001302 deltaRows *= -1;
1303 while (deltaRows) {
1304 var lastRow = this.getRowCount() - 1;
1305 if (lastRow - this.scrollbackRows_.length == cursor.row)
1306 break;
1307
1308 if (this.getRowText(lastRow))
1309 break;
1310
1311 this.screen_.popRow();
1312 deltaRows--;
1313 }
1314
1315 var ary = this.screen_.shiftRows(deltaRows);
rginda8ba33642011-12-14 12:31:31 -08001316 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
rginda87b86462011-12-14 13:48:03 -08001317
1318 // We just removed rows from the top of the screen, we need to update
1319 // the cursor to match.
1320 cursor.row -= deltaRows;
1321
rginda8ba33642011-12-14 12:31:31 -08001322 } else if (deltaRows > 0) {
1323 // Screen got larger.
1324
1325 if (deltaRows <= this.scrollbackRows_.length) {
1326 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1327 var rows = this.scrollbackRows_.splice(
rginda87b86462011-12-14 13:48:03 -08001328 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
rginda8ba33642011-12-14 12:31:31 -08001329 this.screen_.unshiftRows(rows);
1330 deltaRows -= scrollbackCount;
rginda87b86462011-12-14 13:48:03 -08001331 cursor.row += scrollbackCount;
rginda8ba33642011-12-14 12:31:31 -08001332 }
1333
1334 if (deltaRows)
1335 this.appendRows_(deltaRows);
1336 }
1337
1338 this.screen_.setColumnCount(this.screenSize.width);
rginda87b86462011-12-14 13:48:03 -08001339 this.restoreCursor(cursor);
1340 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001341};
1342
1343/**
1344 * Service the cursor blink timeout.
1345 */
1346hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001347 if (this.cursorNode_.style.opacity == '0') {
1348 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001349 } else {
rginda87b86462011-12-14 13:48:03 -08001350 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001351 }
1352};