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