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