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