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