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