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