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