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