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