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