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