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