blob: 3e36eb0c91cb41c3c3efa88cdd2835e0a478f76e [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
rginda6d397402012-01-17 10:58:29 -080074 // Cursor position and attributes saved with DECSC.
75 this.savedOptions_ = {};
76
rginda8ba33642011-12-14 12:31:31 -080077 // The current mode bits for the terminal.
78 this.options_ = new hterm.Options();
79
80 // Timeouts we might need to clear.
81 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080082
83 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080084 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080085
86 // General IO interface that can be given to third parties without exposing
87 // the entire terminal object.
88 this.io = new hterm.Terminal.IO(this);
89};
90
91/**
92 * Create a new instance of a terminal command and run it with a given
93 * argument string.
94 *
95 * @param {function} commandClass The constructor for a terminal command.
96 * @param {string} argString The argument string to pass to the command.
97 */
98hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
99 var self = this;
100 this.command = new commandClass(
101 { argString: argString || '',
102 io: this.io.push(),
103 onExit: function(code) {
104 self.io.pop();
105 self.io.println(hterm.msg('COMMAND_COMPLETE',
106 [self.command.commandName, code]));
107 }
108 });
109
110 this.command.run();
111};
112
113/**
114 * Return a copy of the current cursor position.
115 *
116 * @return {hterm.RowCol} The RowCol object representing the current position.
117 */
118hterm.Terminal.prototype.saveCursor = function() {
119 return this.screen_.cursorPosition.clone();
120};
121
122/**
123 * Restore a previously saved cursor position.
124 *
125 * @param {hterm.RowCol} cursor The position to restore.
126 */
127hterm.Terminal.prototype.restoreCursor = function(cursor) {
128 this.screen_.setCursorPosition(cursor.row, cursor.column);
rginda2312fff2012-01-05 16:20:52 -0800129 this.screen_.cursorPosition.overflow = cursor.overflow;
rginda87b86462011-12-14 13:48:03 -0800130};
131
132/**
133 * Set the width of the terminal, resizing the UI to match.
134 */
135hterm.Terminal.prototype.setWidth = function(columnCount) {
136 this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px'
137
138 // The resizing of the UI will happen asynchronously, so we need to take
139 // care of this bookeeping here instead of letting the resize handlers deal
140 // with it.
141 this.screenSize.width = columnCount;
142 this.screen_.setColumnCount(columnCount);
143};
144
145/**
146 * Scroll the terminal to the top of the scrollback buffer.
147 */
148hterm.Terminal.prototype.scrollHome = function() {
149 this.scrollPort_.scrollRowToTop(0);
150};
151
152/**
153 * Scroll the terminal to the end.
154 */
155hterm.Terminal.prototype.scrollEnd = function() {
156 this.scrollPort_.scrollRowToBottom(this.getRowCount());
157};
158
159/**
160 * Scroll the terminal one page up (minus one line) relative to the current
161 * position.
162 */
163hterm.Terminal.prototype.scrollPageUp = function() {
164 var i = this.scrollPort_.getTopRowIndex();
165 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
166};
167
168/**
169 * Scroll the terminal one page down (minus one line) relative to the current
170 * position.
171 */
172hterm.Terminal.prototype.scrollPageDown = function() {
173 var i = this.scrollPort_.getTopRowIndex();
174 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800175};
176
rginda87b86462011-12-14 13:48:03 -0800177hterm.Terminal.prototype.reset = function() {
178 console.log('reset');
179};
180
rginda0f5c0292012-01-13 11:00:13 -0800181hterm.Terminal.prototype.softReset = function() {
182 console.log('softReset');
183};
184
rginda87b86462011-12-14 13:48:03 -0800185hterm.Terminal.prototype.clearColorAndAttributes = function() {
186 //console.log('clearColorAndAttributes');
187};
188
189hterm.Terminal.prototype.setForegroundColor256 = function() {
190 console.log('setForegroundColor256');
191};
192
193hterm.Terminal.prototype.setBackgroundColor256 = function() {
194 console.log('setBackgroundColor256');
195};
196
197hterm.Terminal.prototype.setForegroundColor = function() {
198 //console.log('setForegroundColor');
199};
200
201hterm.Terminal.prototype.setBackgroundColor = function() {
202 //console.log('setBackgroundColor');
203};
204
205hterm.Terminal.prototype.setAttributes = function() {
206 //console.log('setAttributes');
207};
208
209hterm.Terminal.prototype.resize = function() {
210 console.log('resize');
211};
212
213hterm.Terminal.prototype.setSpecialCharsEnabled = function() {
214 //console.log('setSpecialCharactersEnabled');
215};
216
rginda0f5c0292012-01-13 11:00:13 -0800217hterm.Terminal.prototype.forwardTabStop = function(count) {
218 this.cursorRight(4);
219};
220
221hterm.Terminal.prototype.backwardTabStop = function(count) {
222 console.error('Not implemented: backwardTabStop');
223};
224
rginda87b86462011-12-14 13:48:03 -0800225hterm.Terminal.prototype.setTabStopAtCursor = function() {
226 console.log('setTabStopAtCursor');
227};
228
229hterm.Terminal.prototype.clearTabStops = function() {
230 console.log('clearTabStops');
231};
232
rginda6d397402012-01-17 10:58:29 -0800233/**
234 * Save cursor position and attributes.
235 *
236 * TODO(rginda): Save attributes once we support them.
237 */
rginda87b86462011-12-14 13:48:03 -0800238hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800239 this.savedOptions_.cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -0800240};
241
rginda6d397402012-01-17 10:58:29 -0800242/**
243 * Restore cursor position and attributes.
244 *
245 * TODO(rginda): Restore attributes once we support them.
246 */
rginda87b86462011-12-14 13:48:03 -0800247hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800248 if (this.savedOptions_.cursor)
249 this.restoreCursor(this.savedOptions_.cursor);
rginda8ba33642011-12-14 12:31:31 -0800250};
251
252/**
253 * Interpret a sequence of characters.
254 *
255 * Incomplete escape sequences are buffered until the next call.
256 *
257 * @param {string} str Sequence of characters to interpret or pass through.
258 */
259hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800260 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800261 this.scheduleSyncCursorPosition_();
262};
263
264/**
265 * Take over the given DIV for use as the terminal display.
266 *
267 * @param {HTMLDivElement} div The div to use as the terminal display.
268 */
269hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800270 this.div_ = div;
271
rginda8ba33642011-12-14 12:31:31 -0800272 this.scrollPort_.decorate(div);
273 this.document_ = this.scrollPort_.getDocument();
274
275 // Get character dimensions from the scrollPort.
276 this.characterSize_.height = this.scrollPort_.getRowHeight();
277 this.characterSize_.width = this.scrollPort_.getCharacterWidth();
278
279 this.cursorNode_ = this.document_.createElement('div');
280 this.cursorNode_.style.cssText =
281 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800282 'top: -99px;' +
283 'display: block;' +
rginda8ba33642011-12-14 12:31:31 -0800284 'width: ' + this.characterSize_.width + 'px;' +
285 'height: ' + this.characterSize_.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800286 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800287 'background-color: ' + this.cursorColor);
288 this.document_.body.appendChild(this.cursorNode_);
289
290 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800291
292 this.vt.keyboard.installKeyboard(this.document_.body.firstChild);
293
294 var link = this.document_.createElement('link');
295 link.setAttribute('href', '../css/dialogs.css');
296 link.setAttribute('rel', 'stylesheet');
297 this.document_.head.appendChild(link);
298
299 this.alertDialog = new AlertDialog(this.document_.body);
300 this.promptDialog = new PromptDialog(this.document_.body);
301 this.confirmDialog = new ConfirmDialog(this.document_.body);
302
303 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800304 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800305};
306
307hterm.Terminal.prototype.getDocument = function() {
308 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800309};
310
311/**
312 * Return the HTML Element for a given row index.
313 *
314 * This is a method from the RowProvider interface. The ScrollPort uses
315 * it to fetch rows on demand as they are scrolled into view.
316 *
317 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
318 * pairs to conserve memory.
319 *
320 * @param {integer} index The zero-based row index, measured relative to the
321 * start of the scrollback buffer. On-screen rows will always have the
322 * largest indicies.
323 * @return {HTMLElement} The 'x-row' element containing for the requested row.
324 */
325hterm.Terminal.prototype.getRowNode = function(index) {
326 if (index < this.scrollbackRows_.length)
327 return this.scrollbackRows_[index];
328
329 var screenIndex = index - this.scrollbackRows_.length;
330 return this.screen_.rowsArray[screenIndex];
331};
332
333/**
334 * Return the text content for a given range of rows.
335 *
336 * This is a method from the RowProvider interface. The ScrollPort uses
337 * it to fetch text content on demand when the user attempts to copy their
338 * selection to the clipboard.
339 *
340 * @param {integer} start The zero-based row index to start from, measured
341 * relative to the start of the scrollback buffer. On-screen rows will
342 * always have the largest indicies.
343 * @param {integer} end The zero-based row index to end on, measured
344 * relative to the start of the scrollback buffer.
345 * @return {string} A single string containing the text value of the range of
346 * rows. Lines will be newline delimited, with no trailing newline.
347 */
348hterm.Terminal.prototype.getRowsText = function(start, end) {
349 var ary = [];
350 for (var i = start; i < end; i++) {
351 var node = this.getRowNode(i);
352 ary.push(node.textContent);
353 }
354
355 return ary.join('\n');
356};
357
358/**
359 * Return the text content for a given row.
360 *
361 * This is a method from the RowProvider interface. The ScrollPort uses
362 * it to fetch text content on demand when the user attempts to copy their
363 * selection to the clipboard.
364 *
365 * @param {integer} index The zero-based row index to return, measured
366 * relative to the start of the scrollback buffer. On-screen rows will
367 * always have the largest indicies.
368 * @return {string} A string containing the text value of the selected row.
369 */
370hterm.Terminal.prototype.getRowText = function(index) {
371 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800372 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800373};
374
375/**
376 * Return the total number of rows in the addressable screen and in the
377 * scrollback buffer of this terminal.
378 *
379 * This is a method from the RowProvider interface. The ScrollPort uses
380 * it to compute the size of the scrollbar.
381 *
382 * @return {integer} The number of rows in this terminal.
383 */
384hterm.Terminal.prototype.getRowCount = function() {
385 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
386};
387
388/**
389 * Create DOM nodes for new rows and append them to the end of the terminal.
390 *
391 * This is the only correct way to add a new DOM node for a row. Notice that
392 * the new row is appended to the bottom of the list of rows, and does not
393 * require renumbering (of the rowIndex property) of previous rows.
394 *
395 * If you think you want a new blank row somewhere in the middle of the
396 * terminal, look into moveRows_().
397 *
398 * This method does not pay attention to vtScrollTop/Bottom, since you should
399 * be using moveRows() in cases where they would matter.
400 *
401 * The cursor will be positioned at column 0 of the first inserted line.
402 */
403hterm.Terminal.prototype.appendRows_ = function(count) {
404 var cursorRow = this.screen_.rowsArray.length;
405 var offset = this.scrollbackRows_.length + cursorRow;
406 for (var i = 0; i < count; i++) {
407 var row = this.document_.createElement('x-row');
408 row.appendChild(this.document_.createTextNode(''));
409 row.rowIndex = offset + i;
410 this.screen_.pushRow(row);
411 }
412
413 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
414 if (extraRows > 0) {
415 var ary = this.screen_.shiftRows(extraRows);
416 Array.prototype.push.apply(this.scrollbackRows_, ary);
417 this.scheduleScrollDown_();
418 }
419
420 if (cursorRow >= this.screen_.rowsArray.length)
421 cursorRow = this.screen_.rowsArray.length - 1;
422
rginda87b86462011-12-14 13:48:03 -0800423 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800424};
425
426/**
427 * Relocate rows from one part of the addressable screen to another.
428 *
429 * This is used to recycle rows during VT scrolls (those which are driven
430 * by VT commands, rather than by the user manipulating the scrollbar.)
431 *
432 * In this case, the blank lines scrolled into the scroll region are made of
433 * the nodes we scrolled off. These have their rowIndex properties carefully
434 * renumbered so as not to confuse the ScrollPort.
435 *
436 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
437 * It may just be luck. I wouldn't be surprised if we actually needed to call
438 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
439 * adding it.
440 */
441hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
442 var ary = this.screen_.removeRows(fromIndex, count);
443 this.screen_.insertRows(toIndex, ary);
444
445 var start, end;
446 if (fromIndex < toIndex) {
447 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800448 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800449 } else {
450 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800451 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800452 }
453
454 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800455 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800456};
457
458/**
459 * Renumber the rowIndex property of the given range of rows.
460 *
461 * The start and end indicies are relative to the screen, not the scrollback.
462 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800463 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800464 * no need to renumber scrollback rows.
465 */
466hterm.Terminal.prototype.renumberRows_ = function(start, end) {
467 var offset = this.scrollbackRows_.length;
468 for (var i = start; i < end; i++) {
469 this.screen_.rowsArray[i].rowIndex = offset + i;
470 }
471};
472
473/**
474 * Print a string to the terminal.
475 *
476 * This respects the current insert and wraparound modes. It will add new lines
477 * to the end of the terminal, scrolling off the top into the scrollback buffer
478 * if necessary.
479 *
480 * The string is *not* parsed for escape codes. Use the interpret() method if
481 * that's what you're after.
482 *
483 * @param{string} str The string to print.
484 */
485hterm.Terminal.prototype.print = function(str) {
486 do {
rginda2312fff2012-01-05 16:20:52 -0800487 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
488 this.newLine();
489
rginda8ba33642011-12-14 12:31:31 -0800490 if (this.options_.insertMode) {
491 str = this.screen_.insertString(str);
492 } else {
493 str = this.screen_.overwriteString(str);
494 }
rginda2312fff2012-01-05 16:20:52 -0800495 } while (this.options_.wraparound && str);
rginda8ba33642011-12-14 12:31:31 -0800496
497 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800498
499 if (this.scrollOnOutput)
500 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800501};
502
503/**
rginda87b86462011-12-14 13:48:03 -0800504 * Set the VT scroll region.
505 *
rginda87b86462011-12-14 13:48:03 -0800506 * This also resets the cursor position to the absolute (0, 0) position, since
507 * that's what xterm appears to do.
508 *
509 * @param {integer} scrollTop The zero-based top of the scroll region.
510 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
511 * inclusive.
512 */
513hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
514 this.vtScrollTop_ = scrollTop;
515 this.vtScrollBottom_ = scrollBottom;
516 this.setAbsoluteCursorPosition(0, 0);
517};
518
519/**
rginda8ba33642011-12-14 12:31:31 -0800520 * Return the top row index according to the VT.
521 *
522 * This will return 0 unless the terminal has been told to restrict scrolling
523 * to some lower row. It is used for some VT cursor positioning and scrolling
524 * commands.
525 *
526 * @return {integer} The topmost row in the terminal's scroll region.
527 */
528hterm.Terminal.prototype.getVTScrollTop = function() {
529 if (this.vtScrollTop_ != null)
530 return this.vtScrollTop_;
531
532 return 0;
rginda87b86462011-12-14 13:48:03 -0800533};
rginda8ba33642011-12-14 12:31:31 -0800534
535/**
536 * Return the bottom row index according to the VT.
537 *
538 * This will return the height of the terminal unless the it has been told to
539 * restrict scrolling to some higher row. It is used for some VT cursor
540 * positioning and scrolling commands.
541 *
542 * @return {integer} The bottommost row in the terminal's scroll region.
543 */
544hterm.Terminal.prototype.getVTScrollBottom = function() {
545 if (this.vtScrollBottom_ != null)
546 return this.vtScrollBottom_;
547
rginda87b86462011-12-14 13:48:03 -0800548 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800549}
550
551/**
552 * Process a '\n' character.
553 *
554 * If the cursor is on the final row of the terminal this will append a new
555 * blank row to the screen and scroll the topmost row into the scrollback
556 * buffer.
557 *
558 * Otherwise, this moves the cursor to column zero of the next row.
559 */
560hterm.Terminal.prototype.newLine = function() {
561 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800562 // If we're at the end of the screen we need to append a new line and
563 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800564 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800565 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
566 // End of the scroll region does not affect the scrollback buffer.
567 this.vtScrollUp(1);
568 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800569 } else {
rginda87b86462011-12-14 13:48:03 -0800570 // Anywhere else in the screen just moves the cursor.
571 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800572 }
573};
574
575/**
576 * Like newLine(), except maintain the cursor column.
577 */
578hterm.Terminal.prototype.lineFeed = function() {
579 var column = this.screen_.cursorPosition.column;
580 this.newLine();
581 this.setCursorColumn(column);
582};
583
584/**
rginda87b86462011-12-14 13:48:03 -0800585 * If autoCarriageReturn is set then newLine(), else lineFeed().
586 */
587hterm.Terminal.prototype.formFeed = function() {
588 if (this.options_.autoCarriageReturn) {
589 this.newLine();
590 } else {
591 this.lineFeed();
592 }
593};
594
595/**
596 * Move the cursor up one row, possibly inserting a blank line.
597 *
598 * The cursor column is not changed.
599 */
600hterm.Terminal.prototype.reverseLineFeed = function() {
601 var scrollTop = this.getVTScrollTop();
602 var currentRow = this.screen_.cursorPosition.row;
603
604 if (currentRow == scrollTop) {
605 this.insertLines(1);
606 } else {
607 this.setAbsoluteCursorRow(currentRow - 1);
608 }
609};
610
611/**
rginda8ba33642011-12-14 12:31:31 -0800612 * Replace all characters to the left of the current cursor with the space
613 * character.
614 *
615 * TODO(rginda): This should probably *remove* the characters (not just replace
616 * with a space) if there are no characters at or beyond the current cursor
617 * position. Once it does that, it'll have the same text-attribute related
618 * issues as hterm.Screen.prototype.clearCursorRow :/
619 */
620hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800621 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800622 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800623 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
624 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800625};
626
627/**
628 * Erase a given number of characters to the right of the cursor, shifting
629 * remaining characters to the left.
630 *
631 * The cursor position is unchanged.
632 *
633 * TODO(rginda): Test that this works even when the cursor is positioned beyond
634 * the end of the text.
635 *
636 * TODO(rginda): This likely has text-attribute related troubles similar to the
637 * todo on hterm.Screen.prototype.clearCursorRow.
638 */
639hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800640 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800641
rginda87b86462011-12-14 13:48:03 -0800642 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800643 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
644 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800645 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800646};
647
648/**
649 * Erase the current line.
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.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800657 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800658 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800659 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800660};
661
662/**
663 * Erase all characters from the start of the scroll region to the current
664 * cursor position.
665 *
666 * The cursor position is unchanged.
667 *
668 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
669 * has a text-attribute related TODO.
670 */
671hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800672 var cursor = this.saveCursor();
673
674 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -0800675
676 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -0800677 for (var i = top; i < cursor.row; i++) {
678 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800679 this.screen_.clearCursorRow();
680 }
681
rginda87b86462011-12-14 13:48:03 -0800682 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800683};
684
685/**
686 * Erase all characters from the current cursor position to the end of the
687 * scroll region.
688 *
689 * The cursor position is unchanged.
690 *
691 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
692 * has a text-attribute related TODO.
693 */
694hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -0800695 var cursor = this.saveCursor();
696
697 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -0800698
699 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800700 for (var i = cursor.row + 1; i <= bottom; i++) {
701 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800702 this.screen_.clearCursorRow();
703 }
704
rginda87b86462011-12-14 13:48:03 -0800705 this.restoreCursor(cursor);
706};
707
708/**
709 * Fill the terminal with a given character.
710 *
711 * This methods does not respect the VT scroll region.
712 *
713 * @param {string} ch The character to use for the fill.
714 */
715hterm.Terminal.prototype.fill = function(ch) {
716 var cursor = this.saveCursor();
717
718 this.setAbsoluteCursorPosition(0, 0);
719 for (var row = 0; row < this.screenSize.height; row++) {
720 for (var col = 0; col < this.screenSize.width; col++) {
721 this.setAbsoluteCursorPosition(row, col);
722 this.screen_.overwriteString(ch);
723 }
724 }
725
726 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800727};
728
729/**
730 * Erase the entire scroll region.
731 *
732 * The cursor position is unchanged.
733 *
734 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
735 * has a text-attribute related TODO.
736 */
737hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -0800738 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800739
740 var top = this.getVTScrollTop();
741 var bottom = this.getVTScrollBottom();
742
743 for (var i = top; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -0800744 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -0800745 this.screen_.clearCursorRow();
746 }
747
rginda87b86462011-12-14 13:48:03 -0800748 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800749};
750
751/**
752 * VT command to insert lines at the current cursor row.
753 *
754 * This respects the current scroll region. Rows pushed off the bottom are
755 * lost (they won't show up in the scrollback buffer).
756 *
757 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
758 * has a text-attribute related TODO.
759 *
760 * @param {integer} count The number of lines to insert.
761 */
762hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800763 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800764
765 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -0800766 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800767
768 var start = bottom - count;
rginda87b86462011-12-14 13:48:03 -0800769 if (start != cursor.row)
770 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -0800771
772 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800773 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800774 this.screen_.clearCursorRow();
775 }
776
rginda87b86462011-12-14 13:48:03 -0800777 cursor.column = 0;
778 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800779};
780
781/**
782 * VT command to delete lines at the current cursor row.
783 *
784 * New rows are added to the bottom of scroll region to take their place. New
785 * rows are strictly there to take up space and have no content or style.
786 */
787hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -0800788 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800789
rginda87b86462011-12-14 13:48:03 -0800790 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -0800791 var bottom = this.getVTScrollBottom();
792
rginda87b86462011-12-14 13:48:03 -0800793 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -0800794 count = Math.min(count, maxCount);
795
rginda87b86462011-12-14 13:48:03 -0800796 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -0800797 if (count != maxCount)
798 this.moveRows_(top, count, moveStart);
799
800 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -0800801 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -0800802 this.screen_.clearCursorRow();
803 }
804
rginda87b86462011-12-14 13:48:03 -0800805 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800806};
807
808/**
809 * Inserts the given number of spaces at the current cursor position.
810 *
rginda87b86462011-12-14 13:48:03 -0800811 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -0800812 */
813hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -0800814 var cursor = this.saveCursor();
815
rginda0f5c0292012-01-13 11:00:13 -0800816 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -0800817 this.screen_.insertString(ws);
rginda87b86462011-12-14 13:48:03 -0800818
819 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800820};
821
822/**
823 * Forward-delete the specified number of characters starting at the cursor
824 * position.
825 *
826 * @param {integer} count The number of characters to delete.
827 */
828hterm.Terminal.prototype.deleteChars = function(count) {
829 this.screen_.deleteChars(count);
830};
831
832/**
833 * Shift rows in the scroll region upwards by a given number of lines.
834 *
835 * New rows are inserted at the bottom of the scroll region to fill the
836 * vacated rows. The new rows not filled out with the current text attributes.
837 *
838 * This function does not affect the scrollback rows at all. Rows shifted
839 * off the top are lost.
840 *
rginda87b86462011-12-14 13:48:03 -0800841 * The cursor position is not altered.
842 *
rginda8ba33642011-12-14 12:31:31 -0800843 * @param {integer} count The number of rows to scroll.
844 */
845hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -0800846 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800847
rginda87b86462011-12-14 13:48:03 -0800848 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -0800849 this.deleteLines(count);
850
rginda87b86462011-12-14 13:48:03 -0800851 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800852};
853
854/**
855 * Shift rows below the cursor down by a given number of lines.
856 *
857 * This function respects the current scroll region.
858 *
859 * New rows are inserted at the top of the scroll region to fill the
860 * vacated rows. The new rows not filled out with the current text attributes.
861 *
862 * This function does not affect the scrollback rows at all. Rows shifted
863 * off the bottom are lost.
864 *
865 * @param {integer} count The number of rows to scroll.
866 */
867hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800868 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800869
rginda87b86462011-12-14 13:48:03 -0800870 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -0800871 this.insertLines(opt_count);
872
rginda87b86462011-12-14 13:48:03 -0800873 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800874};
875
rginda87b86462011-12-14 13:48:03 -0800876
rginda8ba33642011-12-14 12:31:31 -0800877/**
878 * Set the cursor position.
879 *
880 * The cursor row is relative to the scroll region if the terminal has
881 * 'origin mode' enabled, or relative to the addressable screen otherwise.
882 *
883 * @param {integer} row The new zero-based cursor row.
884 * @param {integer} row The new zero-based cursor column.
885 */
886hterm.Terminal.prototype.setCursorPosition = function(row, column) {
887 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -0800888 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800889 } else {
rginda87b86462011-12-14 13:48:03 -0800890 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -0800891 }
rginda87b86462011-12-14 13:48:03 -0800892};
rginda8ba33642011-12-14 12:31:31 -0800893
rginda87b86462011-12-14 13:48:03 -0800894hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
895 var scrollTop = this.getVTScrollTop();
896 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -0800897 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -0800898 this.screen_.setCursorPosition(row, column);
899};
900
901hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -0800902 row = hterm.clamp(row, 0, this.screenSize.height - 1);
903 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -0800904 this.screen_.setCursorPosition(row, column);
905};
906
907/**
908 * Set the cursor column.
909 *
910 * @param {integer} column The new zero-based cursor column.
911 */
912hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -0800913 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -0800914};
915
916/**
917 * Return the cursor column.
918 *
919 * @return {integer} The zero-based cursor column.
920 */
921hterm.Terminal.prototype.getCursorColumn = function() {
922 return this.screen_.cursorPosition.column;
923};
924
925/**
926 * Set the cursor row.
927 *
928 * The cursor row is relative to the scroll region if the terminal has
929 * 'origin mode' enabled, or relative to the addressable screen otherwise.
930 *
931 * @param {integer} row The new cursor row.
932 */
rginda87b86462011-12-14 13:48:03 -0800933hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
934 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -0800935};
936
937/**
938 * Return the cursor row.
939 *
940 * @return {integer} The zero-based cursor row.
941 */
942hterm.Terminal.prototype.getCursorRow = function(row) {
943 return this.screen_.cursorPosition.row;
944};
945
946/**
947 * Request that the ScrollPort redraw itself soon.
948 *
949 * The redraw will happen asynchronously, soon after the call stack winds down.
950 * Multiple calls will be coalesced into a single redraw.
951 */
952hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -0800953 if (this.timeouts_.redraw)
954 return;
rginda8ba33642011-12-14 12:31:31 -0800955
956 var self = this;
rginda87b86462011-12-14 13:48:03 -0800957 this.timeouts_.redraw = setTimeout(function() {
958 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -0800959 self.scrollPort_.redraw_();
960 }, 0);
961};
962
963/**
964 * Request that the ScrollPort be scrolled to the bottom.
965 *
966 * The scroll will happen asynchronously, soon after the call stack winds down.
967 * Multiple calls will be coalesced into a single scroll.
968 *
969 * This affects the scrollbar position of the ScrollPort, and has nothing to
970 * do with the VT scroll commands.
971 */
972hterm.Terminal.prototype.scheduleScrollDown_ = function() {
973 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -0800974 return;
rginda8ba33642011-12-14 12:31:31 -0800975
976 var self = this;
977 this.timeouts_.scrollDown = setTimeout(function() {
978 delete self.timeouts_.scrollDown;
979 self.scrollPort_.scrollRowToBottom(self.getRowCount());
980 }, 10);
981};
982
983/**
984 * Move the cursor up a specified number of rows.
985 *
986 * @param {integer} count The number of rows to move the cursor.
987 */
988hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -0800989 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -0800990};
991
992/**
993 * Move the cursor down a specified number of rows.
994 *
995 * @param {integer} count The number of rows to move the cursor.
996 */
997hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -0800998 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -0800999 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1000 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1001 this.screenSize.height - 1);
1002
1003 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1004 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001005 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001006};
1007
1008/**
1009 * Move the cursor left a specified number of columns.
1010 *
1011 * @param {integer} count The number of columns to move the cursor.
1012 */
1013hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001014 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001015};
1016
1017/**
1018 * Move the cursor right a specified number of columns.
1019 *
1020 * @param {integer} count The number of columns to move the cursor.
1021 */
1022hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001023 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001024 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001025 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001026 this.setCursorColumn(column);
1027};
1028
1029/**
1030 * Reverse the foreground and background colors of the terminal.
1031 *
1032 * This only affects text that was drawn with no attributes.
1033 *
1034 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1035 * been drawn with attributes that happen to coincide with the default
1036 * 'no-attribute' colors. My guess is probably not.
1037 */
1038hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001039 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001040 if (state) {
1041 this.scrollPort_.setForegroundColor(this.backgroundColor);
1042 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1043 } else {
1044 this.scrollPort_.setForegroundColor(this.foregroundColor);
1045 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1046 }
1047};
1048
1049/**
rginda87b86462011-12-14 13:48:03 -08001050 * Ring the terminal bell.
1051 *
1052 * We only have a visual bell, which quickly toggles inverse video in the
1053 * terminal.
1054 */
1055hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001056 this.cursorNode_.style.backgroundColor =
1057 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001058
1059 var self = this;
1060 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001061 self.cursorNode_.style.backgroundColor = self.cursorColor;
1062 }, 200);
rginda87b86462011-12-14 13:48:03 -08001063};
1064
1065/**
rginda8ba33642011-12-14 12:31:31 -08001066 * Set the origin mode bit.
1067 *
1068 * If origin mode is on, certain VT cursor and scrolling commands measure their
1069 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1070 * to the top of the addressable screen.
1071 *
1072 * Defaults to off.
1073 *
1074 * @param {boolean} state True to set origin mode, false to unset.
1075 */
1076hterm.Terminal.prototype.setOriginMode = function(state) {
1077 this.options_.originMode = state;
1078};
1079
1080/**
1081 * Set the insert mode bit.
1082 *
1083 * If insert mode is on, existing text beyond the cursor position will be
1084 * shifted right to make room for new text. Otherwise, new text overwrites
1085 * any existing text.
1086 *
1087 * Defaults to off.
1088 *
1089 * @param {boolean} state True to set insert mode, false to unset.
1090 */
1091hterm.Terminal.prototype.setInsertMode = function(state) {
1092 this.options_.insertMode = state;
1093};
1094
1095/**
rginda87b86462011-12-14 13:48:03 -08001096 * Set the auto carriage return bit.
1097 *
1098 * If auto carriage return is on then a formfeed character is interpreted
1099 * as a newline, otherwise it's the same as a linefeed. The difference boils
1100 * down to whether or not the cursor column is reset.
1101 */
1102hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1103 this.options_.autoCarriageReturn = state;
1104};
1105
1106/**
rginda8ba33642011-12-14 12:31:31 -08001107 * Set the wraparound mode bit.
1108 *
1109 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1110 * to the start of the following row. Otherwise, the cursor is clamped to the
1111 * end of the screen and attempts to write past it are ignored.
1112 *
1113 * Defaults to on.
1114 *
1115 * @param {boolean} state True to set wraparound mode, false to unset.
1116 */
1117hterm.Terminal.prototype.setWraparound = function(state) {
1118 this.options_.wraparound = state;
1119};
1120
1121/**
1122 * Set the reverse-wraparound mode bit.
1123 *
1124 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1125 * to the end of the previous row. Otherwise, the cursor is clamped to column
1126 * 0.
1127 *
1128 * Defaults to off.
1129 *
1130 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1131 */
1132hterm.Terminal.prototype.setReverseWraparound = function(state) {
1133 this.options_.reverseWraparound = state;
1134};
1135
1136/**
1137 * Selects between the primary and alternate screens.
1138 *
1139 * If alternate mode is on, the alternate screen is active. Otherwise the
1140 * primary screen is active.
1141 *
1142 * Swapping screens has no effect on the scrollback buffer.
1143 *
1144 * Each screen maintains its own cursor position.
1145 *
1146 * Defaults to off.
1147 *
1148 * @param {boolean} state True to set alternate mode, false to unset.
1149 */
1150hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001151 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001152 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1153
1154 this.screen_.setColumnCount(this.screenSize.width);
1155
1156 var rowDelta = this.screenSize.height - this.screen_.getHeight();
1157 if (rowDelta > 0)
1158 this.appendRows_(rowDelta);
1159
rginda6d397402012-01-17 10:58:29 -08001160 this.restoreCursor(cursor);
1161
rginda2312fff2012-01-05 16:20:52 -08001162 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001163 this.syncCursorPosition_();
1164};
1165
1166/**
1167 * Set the cursor-blink mode bit.
1168 *
1169 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1170 * a visible cursor does not blink.
1171 *
1172 * You should make sure to turn blinking off if you're going to dispose of a
1173 * terminal, otherwise you'll leak a timeout.
1174 *
1175 * Defaults to on.
1176 *
1177 * @param {boolean} state True to set cursor-blink mode, false to unset.
1178 */
1179hterm.Terminal.prototype.setCursorBlink = function(state) {
1180 this.options_.cursorBlink = state;
1181
1182 if (!state && this.timeouts_.cursorBlink) {
1183 clearTimeout(this.timeouts_.cursorBlink);
1184 delete this.timeouts_.cursorBlink;
1185 }
1186
1187 if (this.options_.cursorVisible)
1188 this.setCursorVisible(true);
1189};
1190
1191/**
1192 * Set the cursor-visible mode bit.
1193 *
1194 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1195 *
1196 * Defaults to on.
1197 *
1198 * @param {boolean} state True to set cursor-visible mode, false to unset.
1199 */
1200hterm.Terminal.prototype.setCursorVisible = function(state) {
1201 this.options_.cursorVisible = state;
1202
1203 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001204 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001205 return;
1206 }
1207
rginda87b86462011-12-14 13:48:03 -08001208 this.syncCursorPosition_();
1209
1210 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001211
1212 if (this.options_.cursorBlink) {
1213 if (this.timeouts_.cursorBlink)
1214 return;
1215
1216 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1217 500);
1218 } else {
1219 if (this.timeouts_.cursorBlink) {
1220 clearTimeout(this.timeouts_.cursorBlink);
1221 delete this.timeouts_.cursorBlink;
1222 }
1223 }
1224};
1225
1226/**
rginda87b86462011-12-14 13:48:03 -08001227 * Synchronizes the visible cursor and document selection with the current
1228 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001229 */
1230hterm.Terminal.prototype.syncCursorPosition_ = function() {
1231 var topRowIndex = this.scrollPort_.getTopRowIndex();
1232 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1233 var cursorRowIndex = this.scrollbackRows_.length +
1234 this.screen_.cursorPosition.row;
1235
1236 if (cursorRowIndex > bottomRowIndex) {
1237 // Cursor is scrolled off screen, move it outside of the visible area.
1238 this.cursorNode_.style.top = -this.characterSize_.height;
1239 return;
1240 }
1241
1242 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
1243 this.characterSize_.height * (cursorRowIndex - topRowIndex);
1244 this.cursorNode_.style.left = this.characterSize_.width *
1245 this.screen_.cursorPosition.column;
rginda87b86462011-12-14 13:48:03 -08001246
1247 this.cursorNode_.setAttribute('title',
1248 '(' + this.screen_.cursorPosition.row +
1249 ', ' + this.screen_.cursorPosition.column +
1250 ')');
1251
1252 // Update the caret for a11y purposes.
1253 var selection = this.document_.getSelection();
1254 if (selection && selection.isCollapsed)
1255 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001256};
1257
1258/**
1259 * Synchronizes the visible cursor with the current cursor coordinates.
1260 *
1261 * The sync will happen asynchronously, soon after the call stack winds down.
1262 * Multiple calls will be coalesced into a single sync.
1263 */
1264hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1265 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001266 return;
rginda8ba33642011-12-14 12:31:31 -08001267
1268 var self = this;
1269 this.timeouts_.syncCursor = setTimeout(function() {
1270 self.syncCursorPosition_();
1271 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001272 }, 0);
1273};
1274
1275/**
1276 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1277 *
1278 * @param {string} string The VT string representing the keystroke.
1279 */
1280hterm.Terminal.prototype.onVTKeystroke = function(string) {
1281 if (this.scrollOnKeystroke)
1282 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1283
1284 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001285};
1286
1287/**
1288 * React when the ScrollPort is scrolled.
1289 */
1290hterm.Terminal.prototype.onScroll_ = function() {
1291 this.scheduleSyncCursorPosition_();
1292};
1293
1294/**
1295 * React when the ScrollPort is resized.
1296 */
1297hterm.Terminal.prototype.onResize_ = function() {
1298 var width = Math.floor(this.scrollPort_.getScreenWidth() /
1299 this.characterSize_.width);
1300 var height = this.scrollPort_.visibleRowCount;
1301
rginda87b86462011-12-14 13:48:03 -08001302 if (width == this.screenSize.width && height == this.screenSize.height) {
1303 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001304 return;
rginda87b86462011-12-14 13:48:03 -08001305 }
rginda8ba33642011-12-14 12:31:31 -08001306
1307 this.screenSize.resize(width, height);
1308
1309 var screenHeight = this.screen_.getHeight();
1310
1311 var deltaRows = this.screenSize.height - screenHeight;
1312
rginda87b86462011-12-14 13:48:03 -08001313 var cursor = this.saveCursor();
1314
rginda8ba33642011-12-14 12:31:31 -08001315 if (deltaRows < 0) {
1316 // Screen got smaller.
rginda87b86462011-12-14 13:48:03 -08001317 deltaRows *= -1;
1318 while (deltaRows) {
1319 var lastRow = this.getRowCount() - 1;
1320 if (lastRow - this.scrollbackRows_.length == cursor.row)
1321 break;
1322
1323 if (this.getRowText(lastRow))
1324 break;
1325
1326 this.screen_.popRow();
1327 deltaRows--;
1328 }
1329
1330 var ary = this.screen_.shiftRows(deltaRows);
rginda8ba33642011-12-14 12:31:31 -08001331 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
rginda87b86462011-12-14 13:48:03 -08001332
1333 // We just removed rows from the top of the screen, we need to update
1334 // the cursor to match.
1335 cursor.row -= deltaRows;
1336
rginda8ba33642011-12-14 12:31:31 -08001337 } else if (deltaRows > 0) {
1338 // Screen got larger.
1339
1340 if (deltaRows <= this.scrollbackRows_.length) {
1341 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1342 var rows = this.scrollbackRows_.splice(
rginda87b86462011-12-14 13:48:03 -08001343 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
rginda8ba33642011-12-14 12:31:31 -08001344 this.screen_.unshiftRows(rows);
1345 deltaRows -= scrollbackCount;
rginda87b86462011-12-14 13:48:03 -08001346 cursor.row += scrollbackCount;
rginda8ba33642011-12-14 12:31:31 -08001347 }
1348
1349 if (deltaRows)
1350 this.appendRows_(deltaRows);
1351 }
1352
1353 this.screen_.setColumnCount(this.screenSize.width);
rginda87b86462011-12-14 13:48:03 -08001354 this.restoreCursor(cursor);
1355 this.syncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001356};
1357
1358/**
1359 * Service the cursor blink timeout.
1360 */
1361hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001362 if (this.cursorNode_.style.opacity == '0') {
1363 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001364 } else {
rginda87b86462011-12-14 13:48:03 -08001365 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001366 }
1367};