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