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