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 | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 686 | this.setVTScrollRegion(null, null); |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 687 | |
| 688 | this.clearHome(this.primaryScreen_); |
| 689 | this.primaryScreen_.textAttributes.reset(); |
| 690 | |
| 691 | this.clearHome(this.alternateScreen_); |
| 692 | this.alternateScreen_.textAttributes.reset(); |
| 693 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 694 | this.softReset(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 695 | }; |
| 696 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 697 | /** |
| 698 | * Soft terminal reset. |
| 699 | */ |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 700 | hterm.Terminal.prototype.softReset = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 701 | this.options_ = new hterm.Options(); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 702 | |
| 703 | this.primaryScreen_.textAttributes.resetColorPalette(); |
| 704 | this.alternateScreen_.textAttributes.resetColorPalette(); |
| 705 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 706 | this.setCursorVisible(true); |
rginda | de84e38 | 2012-04-20 15:39:31 -0700 | [diff] [blame] | 707 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 708 | }; |
| 709 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 710 | /** |
| 711 | * Move the cursor forward to the next tab stop, or to the last column |
| 712 | * if no more tab stops are set. |
| 713 | */ |
| 714 | hterm.Terminal.prototype.forwardTabStop = function() { |
| 715 | var column = this.screen_.cursorPosition.column; |
| 716 | |
| 717 | for (var i = 0; i < this.tabStops_.length; i++) { |
| 718 | if (this.tabStops_[i] > column) { |
| 719 | this.setCursorColumn(this.tabStops_[i]); |
| 720 | return; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | this.setCursorColumn(this.screenSize.width - 1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 725 | }; |
| 726 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 727 | /** |
| 728 | * Move the cursor backward to the previous tab stop, or to the first column |
| 729 | * if no previous tab stops are set. |
| 730 | */ |
| 731 | hterm.Terminal.prototype.backwardTabStop = function() { |
| 732 | var column = this.screen_.cursorPosition.column; |
| 733 | |
| 734 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 735 | if (this.tabStops_[i] < column) { |
| 736 | this.setCursorColumn(this.tabStops_[i]); |
| 737 | return; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | this.setCursorColumn(1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 742 | }; |
| 743 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 744 | /** |
| 745 | * Set a tab stop at the given column. |
| 746 | * |
| 747 | * @param {int} column Zero based column. |
| 748 | */ |
| 749 | hterm.Terminal.prototype.setTabStop = function(column) { |
| 750 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 751 | if (this.tabStops_[i] == column) |
| 752 | return; |
| 753 | |
| 754 | if (this.tabStops_[i] < column) { |
| 755 | this.tabStops_.splice(i + 1, 0, column); |
| 756 | return; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | this.tabStops_.splice(0, 0, column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 761 | }; |
| 762 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 763 | /** |
| 764 | * Clear the tab stop at the current cursor position. |
| 765 | * |
| 766 | * No effect if there is no tab stop at the current cursor position. |
| 767 | */ |
| 768 | hterm.Terminal.prototype.clearTabStopAtCursor = function() { |
| 769 | var column = this.screen_.cursorPosition.column; |
| 770 | |
| 771 | var i = this.tabStops_.indexOf(column); |
| 772 | if (i == -1) |
| 773 | return; |
| 774 | |
| 775 | this.tabStops_.splice(i, 1); |
| 776 | }; |
| 777 | |
| 778 | /** |
| 779 | * Clear all tab stops. |
| 780 | */ |
| 781 | hterm.Terminal.prototype.clearAllTabStops = function() { |
| 782 | this.tabStops_.length = 0; |
| 783 | }; |
| 784 | |
| 785 | /** |
| 786 | * Set up the default tab stops, starting from a given column. |
| 787 | * |
| 788 | * This sets a tabstop every (column % this.tabWidth) column, starting |
| 789 | * from the specified column, or 0 if no column is provided. |
| 790 | * |
| 791 | * This does not clear the existing tab stops first, use clearAllTabStops |
| 792 | * for that. |
| 793 | * |
| 794 | * @param {int} opt_start Optional starting zero based starting column, useful |
| 795 | * for filling out missing tab stops when the terminal is resized. |
| 796 | */ |
| 797 | hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) { |
| 798 | var start = opt_start || 0; |
| 799 | var w = this.tabWidth; |
| 800 | var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth) |
| 801 | for (var i = 0; i < stopCount; i++) { |
| 802 | this.setTabStop(Math.floor((start + i * w) / w) * w + w); |
| 803 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 804 | }; |
| 805 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 806 | /** |
| 807 | * Save cursor position and attributes. |
| 808 | * |
| 809 | * TODO(rginda): Save attributes once we support them. |
| 810 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 811 | hterm.Terminal.prototype.saveOptions = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 812 | this.savedOptions_.cursor = this.saveCursor(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 813 | this.savedOptions_.textAttributes = this.screen_.textAttributes.clone(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 814 | }; |
| 815 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 816 | /** |
| 817 | * Restore cursor position and attributes. |
| 818 | * |
| 819 | * TODO(rginda): Restore attributes once we support them. |
| 820 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 821 | hterm.Terminal.prototype.restoreOptions = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 822 | if (this.savedOptions_.cursor) |
| 823 | this.restoreCursor(this.savedOptions_.cursor); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 824 | if (this.savedOptions_.textAttributes) |
| 825 | this.screen_.textAttributes = this.savedOptions_.textAttributes; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | /** |
| 829 | * Interpret a sequence of characters. |
| 830 | * |
| 831 | * Incomplete escape sequences are buffered until the next call. |
| 832 | * |
| 833 | * @param {string} str Sequence of characters to interpret or pass through. |
| 834 | */ |
| 835 | hterm.Terminal.prototype.interpret = function(str) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 836 | this.vt.interpret(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 837 | this.scheduleSyncCursorPosition_(); |
| 838 | }; |
| 839 | |
| 840 | /** |
| 841 | * Take over the given DIV for use as the terminal display. |
| 842 | * |
| 843 | * @param {HTMLDivElement} div The div to use as the terminal display. |
| 844 | */ |
| 845 | hterm.Terminal.prototype.decorate = function(div) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 846 | this.div_ = div; |
| 847 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 848 | this.scrollPort_.decorate(div); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 849 | this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image')); |
| 850 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 851 | this.div_.focus = this.focus.bind(this); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 852 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 853 | this.setFontSize(this.prefs_.get('font-size')); |
| 854 | this.syncFontFamily(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 855 | |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 856 | this.setScrollbarVisible(this.prefs_.get('scrollbar-visible')); |
| 857 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 858 | this.document_ = this.scrollPort_.getDocument(); |
| 859 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 860 | this.cursorNode_ = this.document_.createElement('div'); |
| 861 | this.cursorNode_.style.cssText = |
| 862 | ('position: absolute;' + |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 863 | 'top: -99px;' + |
| 864 | 'display: block;' + |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 865 | 'width: ' + this.scrollPort_.characterSize.width + 'px;' + |
| 866 | 'height: ' + this.scrollPort_.characterSize.height + 'px;' + |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 867 | '-webkit-transition: opacity, background-color 100ms linear;' + |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 868 | 'background-color: ' + this.prefs_.get('cursor-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 869 | this.document_.body.appendChild(this.cursorNode_); |
| 870 | |
rginda | de84e38 | 2012-04-20 15:39:31 -0700 | [diff] [blame] | 871 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 872 | this.setReverseVideo(false); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 873 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 874 | this.scrollPort_.focus(); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 875 | this.scrollPort_.scheduleRedraw(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 876 | }; |
| 877 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 878 | /** |
| 879 | * Return the HTML document that contains the terminal DOM nodes. |
| 880 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 881 | hterm.Terminal.prototype.getDocument = function() { |
| 882 | return this.document_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 883 | }; |
| 884 | |
| 885 | /** |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 886 | * Focus the terminal. |
| 887 | */ |
| 888 | hterm.Terminal.prototype.focus = function() { |
| 889 | this.scrollPort_.focus(); |
| 890 | }; |
| 891 | |
| 892 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 893 | * Return the HTML Element for a given row index. |
| 894 | * |
| 895 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 896 | * it to fetch rows on demand as they are scrolled into view. |
| 897 | * |
| 898 | * TODO(rginda): Consider saving scrollback rows as (HTML source, text content) |
| 899 | * pairs to conserve memory. |
| 900 | * |
| 901 | * @param {integer} index The zero-based row index, measured relative to the |
| 902 | * start of the scrollback buffer. On-screen rows will always have the |
| 903 | * largest indicies. |
| 904 | * @return {HTMLElement} The 'x-row' element containing for the requested row. |
| 905 | */ |
| 906 | hterm.Terminal.prototype.getRowNode = function(index) { |
| 907 | if (index < this.scrollbackRows_.length) |
| 908 | return this.scrollbackRows_[index]; |
| 909 | |
| 910 | var screenIndex = index - this.scrollbackRows_.length; |
| 911 | return this.screen_.rowsArray[screenIndex]; |
| 912 | }; |
| 913 | |
| 914 | /** |
| 915 | * Return the text content for a given range of rows. |
| 916 | * |
| 917 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 918 | * it to fetch text content on demand when the user attempts to copy their |
| 919 | * selection to the clipboard. |
| 920 | * |
| 921 | * @param {integer} start The zero-based row index to start from, measured |
| 922 | * relative to the start of the scrollback buffer. On-screen rows will |
| 923 | * always have the largest indicies. |
| 924 | * @param {integer} end The zero-based row index to end on, measured |
| 925 | * relative to the start of the scrollback buffer. |
| 926 | * @return {string} A single string containing the text value of the range of |
| 927 | * rows. Lines will be newline delimited, with no trailing newline. |
| 928 | */ |
| 929 | hterm.Terminal.prototype.getRowsText = function(start, end) { |
| 930 | var ary = []; |
| 931 | for (var i = start; i < end; i++) { |
| 932 | var node = this.getRowNode(i); |
| 933 | ary.push(node.textContent); |
| 934 | } |
| 935 | |
| 936 | return ary.join('\n'); |
| 937 | }; |
| 938 | |
| 939 | /** |
| 940 | * Return the text content for a given row. |
| 941 | * |
| 942 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 943 | * it to fetch text content on demand when the user attempts to copy their |
| 944 | * selection to the clipboard. |
| 945 | * |
| 946 | * @param {integer} index The zero-based row index to return, measured |
| 947 | * relative to the start of the scrollback buffer. On-screen rows will |
| 948 | * always have the largest indicies. |
| 949 | * @return {string} A string containing the text value of the selected row. |
| 950 | */ |
| 951 | hterm.Terminal.prototype.getRowText = function(index) { |
| 952 | var node = this.getRowNode(index); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 953 | return node.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 954 | }; |
| 955 | |
| 956 | /** |
| 957 | * Return the total number of rows in the addressable screen and in the |
| 958 | * scrollback buffer of this terminal. |
| 959 | * |
| 960 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 961 | * it to compute the size of the scrollbar. |
| 962 | * |
| 963 | * @return {integer} The number of rows in this terminal. |
| 964 | */ |
| 965 | hterm.Terminal.prototype.getRowCount = function() { |
| 966 | return this.scrollbackRows_.length + this.screen_.rowsArray.length; |
| 967 | }; |
| 968 | |
| 969 | /** |
| 970 | * Create DOM nodes for new rows and append them to the end of the terminal. |
| 971 | * |
| 972 | * This is the only correct way to add a new DOM node for a row. Notice that |
| 973 | * the new row is appended to the bottom of the list of rows, and does not |
| 974 | * require renumbering (of the rowIndex property) of previous rows. |
| 975 | * |
| 976 | * If you think you want a new blank row somewhere in the middle of the |
| 977 | * terminal, look into moveRows_(). |
| 978 | * |
| 979 | * This method does not pay attention to vtScrollTop/Bottom, since you should |
| 980 | * be using moveRows() in cases where they would matter. |
| 981 | * |
| 982 | * The cursor will be positioned at column 0 of the first inserted line. |
| 983 | */ |
| 984 | hterm.Terminal.prototype.appendRows_ = function(count) { |
| 985 | var cursorRow = this.screen_.rowsArray.length; |
| 986 | var offset = this.scrollbackRows_.length + cursorRow; |
| 987 | for (var i = 0; i < count; i++) { |
| 988 | var row = this.document_.createElement('x-row'); |
| 989 | row.appendChild(this.document_.createTextNode('')); |
| 990 | row.rowIndex = offset + i; |
| 991 | this.screen_.pushRow(row); |
| 992 | } |
| 993 | |
| 994 | var extraRows = this.screen_.rowsArray.length - this.screenSize.height; |
| 995 | if (extraRows > 0) { |
| 996 | var ary = this.screen_.shiftRows(extraRows); |
| 997 | Array.prototype.push.apply(this.scrollbackRows_, ary); |
| 998 | this.scheduleScrollDown_(); |
| 999 | } |
| 1000 | |
| 1001 | if (cursorRow >= this.screen_.rowsArray.length) |
| 1002 | cursorRow = this.screen_.rowsArray.length - 1; |
| 1003 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1004 | this.setAbsoluteCursorPosition(cursorRow, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1005 | }; |
| 1006 | |
| 1007 | /** |
| 1008 | * Relocate rows from one part of the addressable screen to another. |
| 1009 | * |
| 1010 | * This is used to recycle rows during VT scrolls (those which are driven |
| 1011 | * by VT commands, rather than by the user manipulating the scrollbar.) |
| 1012 | * |
| 1013 | * In this case, the blank lines scrolled into the scroll region are made of |
| 1014 | * the nodes we scrolled off. These have their rowIndex properties carefully |
| 1015 | * renumbered so as not to confuse the ScrollPort. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1016 | */ |
| 1017 | hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) { |
| 1018 | var ary = this.screen_.removeRows(fromIndex, count); |
| 1019 | this.screen_.insertRows(toIndex, ary); |
| 1020 | |
| 1021 | var start, end; |
| 1022 | if (fromIndex < toIndex) { |
| 1023 | start = fromIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1024 | end = toIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1025 | } else { |
| 1026 | start = toIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1027 | end = fromIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | this.renumberRows_(start, end); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1031 | this.scrollPort_.scheduleInvalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1032 | }; |
| 1033 | |
| 1034 | /** |
| 1035 | * Renumber the rowIndex property of the given range of rows. |
| 1036 | * |
| 1037 | * The start and end indicies are relative to the screen, not the scrollback. |
| 1038 | * Rows in the scrollback buffer cannot be renumbered. Since they are not |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1039 | * addressable (you can't delete them, scroll them, etc), you should have |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1040 | * no need to renumber scrollback rows. |
| 1041 | */ |
| 1042 | hterm.Terminal.prototype.renumberRows_ = function(start, end) { |
| 1043 | var offset = this.scrollbackRows_.length; |
| 1044 | for (var i = start; i < end; i++) { |
| 1045 | this.screen_.rowsArray[i].rowIndex = offset + i; |
| 1046 | } |
| 1047 | }; |
| 1048 | |
| 1049 | /** |
| 1050 | * Print a string to the terminal. |
| 1051 | * |
| 1052 | * This respects the current insert and wraparound modes. It will add new lines |
| 1053 | * to the end of the terminal, scrolling off the top into the scrollback buffer |
| 1054 | * if necessary. |
| 1055 | * |
| 1056 | * The string is *not* parsed for escape codes. Use the interpret() method if |
| 1057 | * that's what you're after. |
| 1058 | * |
| 1059 | * @param{string} str The string to print. |
| 1060 | */ |
| 1061 | hterm.Terminal.prototype.print = function(str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1062 | if (this.options_.wraparound && this.screen_.cursorPosition.overflow) |
| 1063 | this.newLine(); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1064 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1065 | if (this.options_.insertMode) { |
| 1066 | this.screen_.insertString(str); |
| 1067 | } else { |
| 1068 | this.screen_.overwriteString(str); |
| 1069 | } |
| 1070 | |
| 1071 | var overflow = this.screen_.maybeClipCurrentRow(); |
| 1072 | |
| 1073 | if (this.options_.wraparound && overflow) { |
| 1074 | var lastColumn; |
| 1075 | |
| 1076 | do { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1077 | this.newLine(); |
| 1078 | lastColumn = overflow.characterLength; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1079 | |
| 1080 | if (!this.options_.insertMode) |
| 1081 | this.screen_.deleteChars(overflow.characterLength); |
| 1082 | |
| 1083 | this.screen_.prependNodes(overflow); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1084 | |
| 1085 | overflow = this.screen_.maybeClipCurrentRow(); |
| 1086 | } while (overflow); |
| 1087 | |
| 1088 | this.setCursorColumn(lastColumn); |
| 1089 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1090 | |
| 1091 | this.scheduleSyncCursorPosition_(); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1092 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1093 | if (this.scrollOnOutput_) |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1094 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1095 | }; |
| 1096 | |
| 1097 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1098 | * Set the VT scroll region. |
| 1099 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1100 | * This also resets the cursor position to the absolute (0, 0) position, since |
| 1101 | * that's what xterm appears to do. |
| 1102 | * |
| 1103 | * @param {integer} scrollTop The zero-based top of the scroll region. |
| 1104 | * @param {integer} scrollBottom The zero-based bottom of the scroll region, |
| 1105 | * inclusive. |
| 1106 | */ |
| 1107 | hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) { |
| 1108 | this.vtScrollTop_ = scrollTop; |
| 1109 | this.vtScrollBottom_ = scrollBottom; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1110 | }; |
| 1111 | |
| 1112 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1113 | * Return the top row index according to the VT. |
| 1114 | * |
| 1115 | * This will return 0 unless the terminal has been told to restrict scrolling |
| 1116 | * to some lower row. It is used for some VT cursor positioning and scrolling |
| 1117 | * commands. |
| 1118 | * |
| 1119 | * @return {integer} The topmost row in the terminal's scroll region. |
| 1120 | */ |
| 1121 | hterm.Terminal.prototype.getVTScrollTop = function() { |
| 1122 | if (this.vtScrollTop_ != null) |
| 1123 | return this.vtScrollTop_; |
| 1124 | |
| 1125 | return 0; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1126 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1127 | |
| 1128 | /** |
| 1129 | * Return the bottom row index according to the VT. |
| 1130 | * |
| 1131 | * This will return the height of the terminal unless the it has been told to |
| 1132 | * restrict scrolling to some higher row. It is used for some VT cursor |
| 1133 | * positioning and scrolling commands. |
| 1134 | * |
| 1135 | * @return {integer} The bottommost row in the terminal's scroll region. |
| 1136 | */ |
| 1137 | hterm.Terminal.prototype.getVTScrollBottom = function() { |
| 1138 | if (this.vtScrollBottom_ != null) |
| 1139 | return this.vtScrollBottom_; |
| 1140 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1141 | return this.screenSize.height - 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * Process a '\n' character. |
| 1146 | * |
| 1147 | * If the cursor is on the final row of the terminal this will append a new |
| 1148 | * blank row to the screen and scroll the topmost row into the scrollback |
| 1149 | * buffer. |
| 1150 | * |
| 1151 | * Otherwise, this moves the cursor to column zero of the next row. |
| 1152 | */ |
| 1153 | hterm.Terminal.prototype.newLine = function() { |
| 1154 | if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1155 | // If we're at the end of the screen we need to append a new line and |
| 1156 | // scroll the top line into the scrollback buffer. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1157 | this.appendRows_(1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1158 | } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) { |
| 1159 | // End of the scroll region does not affect the scrollback buffer. |
| 1160 | this.vtScrollUp(1); |
| 1161 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1162 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1163 | // Anywhere else in the screen just moves the cursor. |
| 1164 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1165 | } |
| 1166 | }; |
| 1167 | |
| 1168 | /** |
| 1169 | * Like newLine(), except maintain the cursor column. |
| 1170 | */ |
| 1171 | hterm.Terminal.prototype.lineFeed = function() { |
| 1172 | var column = this.screen_.cursorPosition.column; |
| 1173 | this.newLine(); |
| 1174 | this.setCursorColumn(column); |
| 1175 | }; |
| 1176 | |
| 1177 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1178 | * If autoCarriageReturn is set then newLine(), else lineFeed(). |
| 1179 | */ |
| 1180 | hterm.Terminal.prototype.formFeed = function() { |
| 1181 | if (this.options_.autoCarriageReturn) { |
| 1182 | this.newLine(); |
| 1183 | } else { |
| 1184 | this.lineFeed(); |
| 1185 | } |
| 1186 | }; |
| 1187 | |
| 1188 | /** |
| 1189 | * Move the cursor up one row, possibly inserting a blank line. |
| 1190 | * |
| 1191 | * The cursor column is not changed. |
| 1192 | */ |
| 1193 | hterm.Terminal.prototype.reverseLineFeed = function() { |
| 1194 | var scrollTop = this.getVTScrollTop(); |
| 1195 | var currentRow = this.screen_.cursorPosition.row; |
| 1196 | |
| 1197 | if (currentRow == scrollTop) { |
| 1198 | this.insertLines(1); |
| 1199 | } else { |
| 1200 | this.setAbsoluteCursorRow(currentRow - 1); |
| 1201 | } |
| 1202 | }; |
| 1203 | |
| 1204 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1205 | * Replace all characters to the left of the current cursor with the space |
| 1206 | * character. |
| 1207 | * |
| 1208 | * TODO(rginda): This should probably *remove* the characters (not just replace |
| 1209 | * with a space) if there are no characters at or beyond the current cursor |
| 1210 | * position. Once it does that, it'll have the same text-attribute related |
| 1211 | * issues as hterm.Screen.prototype.clearCursorRow :/ |
| 1212 | */ |
| 1213 | hterm.Terminal.prototype.eraseToLeft = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1214 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1215 | this.setCursorColumn(0); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1216 | this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1)); |
| 1217 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1218 | }; |
| 1219 | |
| 1220 | /** |
| 1221 | * Erase a given number of characters to the right of the cursor, shifting |
| 1222 | * remaining characters to the left. |
| 1223 | * |
| 1224 | * The cursor position is unchanged. |
| 1225 | * |
| 1226 | * TODO(rginda): Test that this works even when the cursor is positioned beyond |
| 1227 | * the end of the text. |
| 1228 | * |
| 1229 | * TODO(rginda): This likely has text-attribute related troubles similar to the |
| 1230 | * todo on hterm.Screen.prototype.clearCursorRow. |
| 1231 | */ |
| 1232 | hterm.Terminal.prototype.eraseToRight = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1233 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1234 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1235 | var maxCount = this.screenSize.width - cursor.column; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1236 | var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount; |
| 1237 | this.screen_.deleteChars(count); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1238 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1239 | }; |
| 1240 | |
| 1241 | /** |
| 1242 | * Erase the current line. |
| 1243 | * |
| 1244 | * The cursor position is unchanged. |
| 1245 | * |
| 1246 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1247 | * has a text-attribute related TODO. |
| 1248 | */ |
| 1249 | hterm.Terminal.prototype.eraseLine = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1250 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1251 | this.screen_.clearCursorRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1252 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1253 | }; |
| 1254 | |
| 1255 | /** |
| 1256 | * Erase all characters from the start of the scroll region to the current |
| 1257 | * cursor position. |
| 1258 | * |
| 1259 | * The cursor position is unchanged. |
| 1260 | * |
| 1261 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1262 | * has a text-attribute related TODO. |
| 1263 | */ |
| 1264 | hterm.Terminal.prototype.eraseAbove = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1265 | var cursor = this.saveCursor(); |
| 1266 | |
| 1267 | this.eraseToLeft(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1268 | |
| 1269 | var top = this.getVTScrollTop(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1270 | for (var i = top; i < cursor.row; i++) { |
| 1271 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1272 | this.screen_.clearCursorRow(); |
| 1273 | } |
| 1274 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1275 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1276 | }; |
| 1277 | |
| 1278 | /** |
| 1279 | * Erase all characters from the current cursor position to the end of the |
| 1280 | * scroll region. |
| 1281 | * |
| 1282 | * The cursor position is unchanged. |
| 1283 | * |
| 1284 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1285 | * has a text-attribute related TODO. |
| 1286 | */ |
| 1287 | hterm.Terminal.prototype.eraseBelow = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1288 | var cursor = this.saveCursor(); |
| 1289 | |
| 1290 | this.eraseToRight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1291 | |
| 1292 | var bottom = this.getVTScrollBottom(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1293 | for (var i = cursor.row + 1; i <= bottom; i++) { |
| 1294 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1295 | this.screen_.clearCursorRow(); |
| 1296 | } |
| 1297 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1298 | this.restoreCursor(cursor); |
| 1299 | }; |
| 1300 | |
| 1301 | /** |
| 1302 | * Fill the terminal with a given character. |
| 1303 | * |
| 1304 | * This methods does not respect the VT scroll region. |
| 1305 | * |
| 1306 | * @param {string} ch The character to use for the fill. |
| 1307 | */ |
| 1308 | hterm.Terminal.prototype.fill = function(ch) { |
| 1309 | var cursor = this.saveCursor(); |
| 1310 | |
| 1311 | this.setAbsoluteCursorPosition(0, 0); |
| 1312 | for (var row = 0; row < this.screenSize.height; row++) { |
| 1313 | for (var col = 0; col < this.screenSize.width; col++) { |
| 1314 | this.setAbsoluteCursorPosition(row, col); |
| 1315 | this.screen_.overwriteString(ch); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1320 | }; |
| 1321 | |
| 1322 | /** |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1323 | * Erase the entire display and leave the cursor at (0, 0). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1324 | * |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1325 | * This does not respect the scroll region. |
| 1326 | * |
| 1327 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1328 | * to the current screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1329 | * |
| 1330 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1331 | * has a text-attribute related TODO. |
| 1332 | */ |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1333 | hterm.Terminal.prototype.clearHome = function(opt_screen) { |
| 1334 | var screen = opt_screen || this.screen_; |
| 1335 | var bottom = screen.getHeight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1336 | |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame^] | 1337 | if (bottom == 0) { |
| 1338 | // Empty screen, nothing to do. |
| 1339 | return; |
| 1340 | } |
| 1341 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1342 | for (var i = 0; i < bottom; i++) { |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1343 | screen.setCursorPosition(i, 0); |
| 1344 | screen.clearCursorRow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1345 | } |
| 1346 | |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1347 | screen.setCursorPosition(0, 0); |
| 1348 | }; |
| 1349 | |
| 1350 | /** |
| 1351 | * Erase the entire display without changing the cursor position. |
| 1352 | * |
| 1353 | * The cursor position is unchanged. This does not respect the scroll |
| 1354 | * region. |
| 1355 | * |
| 1356 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1357 | * to the current screen. |
| 1358 | * |
| 1359 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1360 | * has a text-attribute related TODO. |
| 1361 | */ |
| 1362 | hterm.Terminal.prototype.clear = function(opt_screen) { |
| 1363 | var screen = opt_screen || this.screen_; |
| 1364 | var cursor = screen.cursorPosition.clone(); |
| 1365 | this.clearHome(screen); |
| 1366 | screen.setCursorPosition(cursor.row, cursor.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1367 | }; |
| 1368 | |
| 1369 | /** |
| 1370 | * VT command to insert lines at the current cursor row. |
| 1371 | * |
| 1372 | * This respects the current scroll region. Rows pushed off the bottom are |
| 1373 | * lost (they won't show up in the scrollback buffer). |
| 1374 | * |
| 1375 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1376 | * has a text-attribute related TODO. |
| 1377 | * |
| 1378 | * @param {integer} count The number of lines to insert. |
| 1379 | */ |
| 1380 | hterm.Terminal.prototype.insertLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1381 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1382 | |
| 1383 | var bottom = this.getVTScrollBottom(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1384 | count = Math.min(count, bottom - cursor.row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1385 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1386 | var start = bottom - count + 1; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1387 | if (start != cursor.row) |
| 1388 | this.moveRows_(start, count, cursor.row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1389 | |
| 1390 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1391 | this.setAbsoluteCursorPosition(cursor.row + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1392 | this.screen_.clearCursorRow(); |
| 1393 | } |
| 1394 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1395 | cursor.column = 0; |
| 1396 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | /** |
| 1400 | * VT command to delete lines at the current cursor row. |
| 1401 | * |
| 1402 | * New rows are added to the bottom of scroll region to take their place. New |
| 1403 | * rows are strictly there to take up space and have no content or style. |
| 1404 | */ |
| 1405 | hterm.Terminal.prototype.deleteLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1406 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1407 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1408 | var top = cursor.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1409 | var bottom = this.getVTScrollBottom(); |
| 1410 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1411 | var maxCount = bottom - top + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1412 | count = Math.min(count, maxCount); |
| 1413 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1414 | var moveStart = bottom - count + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1415 | if (count != maxCount) |
| 1416 | this.moveRows_(top, count, moveStart); |
| 1417 | |
| 1418 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1419 | this.setAbsoluteCursorPosition(moveStart + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1420 | this.screen_.clearCursorRow(); |
| 1421 | } |
| 1422 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1423 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1424 | }; |
| 1425 | |
| 1426 | /** |
| 1427 | * Inserts the given number of spaces at the current cursor position. |
| 1428 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1429 | * The cursor position is not changed. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1430 | */ |
| 1431 | hterm.Terminal.prototype.insertSpace = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1432 | var cursor = this.saveCursor(); |
| 1433 | |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1434 | var ws = hterm.getWhitespace(count || 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1435 | this.screen_.insertString(ws); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1436 | this.screen_.maybeClipCurrentRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1437 | |
| 1438 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1439 | }; |
| 1440 | |
| 1441 | /** |
| 1442 | * Forward-delete the specified number of characters starting at the cursor |
| 1443 | * position. |
| 1444 | * |
| 1445 | * @param {integer} count The number of characters to delete. |
| 1446 | */ |
| 1447 | hterm.Terminal.prototype.deleteChars = function(count) { |
| 1448 | this.screen_.deleteChars(count); |
| 1449 | }; |
| 1450 | |
| 1451 | /** |
| 1452 | * Shift rows in the scroll region upwards by a given number of lines. |
| 1453 | * |
| 1454 | * New rows are inserted at the bottom of the scroll region to fill the |
| 1455 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1456 | * |
| 1457 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1458 | * off the top are lost. |
| 1459 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1460 | * The cursor position is not altered. |
| 1461 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1462 | * @param {integer} count The number of rows to scroll. |
| 1463 | */ |
| 1464 | hterm.Terminal.prototype.vtScrollUp = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1465 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1466 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1467 | this.setAbsoluteCursorRow(this.getVTScrollTop()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1468 | this.deleteLines(count); |
| 1469 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1470 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1471 | }; |
| 1472 | |
| 1473 | /** |
| 1474 | * Shift rows below the cursor down by a given number of lines. |
| 1475 | * |
| 1476 | * This function respects the current scroll region. |
| 1477 | * |
| 1478 | * New rows are inserted at the top of the scroll region to fill the |
| 1479 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1480 | * |
| 1481 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1482 | * off the bottom are lost. |
| 1483 | * |
| 1484 | * @param {integer} count The number of rows to scroll. |
| 1485 | */ |
| 1486 | hterm.Terminal.prototype.vtScrollDown = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1487 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1488 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1489 | this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1490 | this.insertLines(opt_count); |
| 1491 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1492 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1493 | }; |
| 1494 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1495 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1496 | /** |
| 1497 | * Set the cursor position. |
| 1498 | * |
| 1499 | * The cursor row is relative to the scroll region if the terminal has |
| 1500 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1501 | * |
| 1502 | * @param {integer} row The new zero-based cursor row. |
| 1503 | * @param {integer} row The new zero-based cursor column. |
| 1504 | */ |
| 1505 | hterm.Terminal.prototype.setCursorPosition = function(row, column) { |
| 1506 | if (this.options_.originMode) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1507 | this.setRelativeCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1508 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1509 | this.setAbsoluteCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1510 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1511 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1512 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1513 | hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) { |
| 1514 | var scrollTop = this.getVTScrollTop(); |
| 1515 | row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom()); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1516 | column = hterm.clamp(column, 0, this.screenSize.width - 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1517 | this.screen_.setCursorPosition(row, column); |
| 1518 | }; |
| 1519 | |
| 1520 | hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1521 | row = hterm.clamp(row, 0, this.screenSize.height - 1); |
| 1522 | column = hterm.clamp(column, 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1523 | this.screen_.setCursorPosition(row, column); |
| 1524 | }; |
| 1525 | |
| 1526 | /** |
| 1527 | * Set the cursor column. |
| 1528 | * |
| 1529 | * @param {integer} column The new zero-based cursor column. |
| 1530 | */ |
| 1531 | hterm.Terminal.prototype.setCursorColumn = function(column) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1532 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1533 | }; |
| 1534 | |
| 1535 | /** |
| 1536 | * Return the cursor column. |
| 1537 | * |
| 1538 | * @return {integer} The zero-based cursor column. |
| 1539 | */ |
| 1540 | hterm.Terminal.prototype.getCursorColumn = function() { |
| 1541 | return this.screen_.cursorPosition.column; |
| 1542 | }; |
| 1543 | |
| 1544 | /** |
| 1545 | * Set the cursor row. |
| 1546 | * |
| 1547 | * The cursor row is relative to the scroll region if the terminal has |
| 1548 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1549 | * |
| 1550 | * @param {integer} row The new cursor row. |
| 1551 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1552 | hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) { |
| 1553 | this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1554 | }; |
| 1555 | |
| 1556 | /** |
| 1557 | * Return the cursor row. |
| 1558 | * |
| 1559 | * @return {integer} The zero-based cursor row. |
| 1560 | */ |
| 1561 | hterm.Terminal.prototype.getCursorRow = function(row) { |
| 1562 | return this.screen_.cursorPosition.row; |
| 1563 | }; |
| 1564 | |
| 1565 | /** |
| 1566 | * Request that the ScrollPort redraw itself soon. |
| 1567 | * |
| 1568 | * The redraw will happen asynchronously, soon after the call stack winds down. |
| 1569 | * Multiple calls will be coalesced into a single redraw. |
| 1570 | */ |
| 1571 | hterm.Terminal.prototype.scheduleRedraw_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1572 | if (this.timeouts_.redraw) |
| 1573 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1574 | |
| 1575 | var self = this; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1576 | this.timeouts_.redraw = setTimeout(function() { |
| 1577 | delete self.timeouts_.redraw; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1578 | self.scrollPort_.redraw_(); |
| 1579 | }, 0); |
| 1580 | }; |
| 1581 | |
| 1582 | /** |
| 1583 | * Request that the ScrollPort be scrolled to the bottom. |
| 1584 | * |
| 1585 | * The scroll will happen asynchronously, soon after the call stack winds down. |
| 1586 | * Multiple calls will be coalesced into a single scroll. |
| 1587 | * |
| 1588 | * This affects the scrollbar position of the ScrollPort, and has nothing to |
| 1589 | * do with the VT scroll commands. |
| 1590 | */ |
| 1591 | hterm.Terminal.prototype.scheduleScrollDown_ = function() { |
| 1592 | if (this.timeouts_.scrollDown) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1593 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1594 | |
| 1595 | var self = this; |
| 1596 | this.timeouts_.scrollDown = setTimeout(function() { |
| 1597 | delete self.timeouts_.scrollDown; |
| 1598 | self.scrollPort_.scrollRowToBottom(self.getRowCount()); |
| 1599 | }, 10); |
| 1600 | }; |
| 1601 | |
| 1602 | /** |
| 1603 | * Move the cursor up a specified number of rows. |
| 1604 | * |
| 1605 | * @param {integer} count The number of rows to move the cursor. |
| 1606 | */ |
| 1607 | hterm.Terminal.prototype.cursorUp = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1608 | return this.cursorDown(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1609 | }; |
| 1610 | |
| 1611 | /** |
| 1612 | * Move the cursor down a specified number of rows. |
| 1613 | * |
| 1614 | * @param {integer} count The number of rows to move the cursor. |
| 1615 | */ |
| 1616 | hterm.Terminal.prototype.cursorDown = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1617 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1618 | var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0); |
| 1619 | var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() : |
| 1620 | this.screenSize.height - 1); |
| 1621 | |
| 1622 | var row = hterm.clamp(this.screen_.cursorPosition.row + count, |
| 1623 | minHeight, maxHeight); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1624 | this.setAbsoluteCursorRow(row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1625 | }; |
| 1626 | |
| 1627 | /** |
| 1628 | * Move the cursor left a specified number of columns. |
| 1629 | * |
| 1630 | * @param {integer} count The number of columns to move the cursor. |
| 1631 | */ |
| 1632 | hterm.Terminal.prototype.cursorLeft = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1633 | return this.cursorRight(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1634 | }; |
| 1635 | |
| 1636 | /** |
| 1637 | * Move the cursor right a specified number of columns. |
| 1638 | * |
| 1639 | * @param {integer} count The number of columns to move the cursor. |
| 1640 | */ |
| 1641 | hterm.Terminal.prototype.cursorRight = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1642 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1643 | var column = hterm.clamp(this.screen_.cursorPosition.column + count, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1644 | 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1645 | this.setCursorColumn(column); |
| 1646 | }; |
| 1647 | |
| 1648 | /** |
| 1649 | * Reverse the foreground and background colors of the terminal. |
| 1650 | * |
| 1651 | * This only affects text that was drawn with no attributes. |
| 1652 | * |
| 1653 | * TODO(rginda): Test xterm to see if reverse is respected for text that has |
| 1654 | * been drawn with attributes that happen to coincide with the default |
| 1655 | * 'no-attribute' colors. My guess is probably not. |
| 1656 | */ |
| 1657 | hterm.Terminal.prototype.setReverseVideo = function(state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1658 | this.options_.reverseVideo = state; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1659 | if (state) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1660 | this.scrollPort_.setForegroundColor(this.prefs_.get('background-color')); |
| 1661 | this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1662 | } else { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1663 | this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color')); |
| 1664 | this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1665 | } |
| 1666 | }; |
| 1667 | |
| 1668 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1669 | * Ring the terminal bell. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1670 | */ |
| 1671 | hterm.Terminal.prototype.ringBell = function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1672 | if (this.bellAudio_.getAttribute('src')) |
| 1673 | this.bellAudio_.play(); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1674 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1675 | this.cursorNode_.style.backgroundColor = |
| 1676 | this.scrollPort_.getForegroundColor(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1677 | |
| 1678 | var self = this; |
| 1679 | setTimeout(function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1680 | self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color'); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1681 | }, 200); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1682 | }; |
| 1683 | |
| 1684 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1685 | * Set the origin mode bit. |
| 1686 | * |
| 1687 | * If origin mode is on, certain VT cursor and scrolling commands measure their |
| 1688 | * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds |
| 1689 | * to the top of the addressable screen. |
| 1690 | * |
| 1691 | * Defaults to off. |
| 1692 | * |
| 1693 | * @param {boolean} state True to set origin mode, false to unset. |
| 1694 | */ |
| 1695 | hterm.Terminal.prototype.setOriginMode = function(state) { |
| 1696 | this.options_.originMode = state; |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1697 | this.setCursorPosition(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1698 | }; |
| 1699 | |
| 1700 | /** |
| 1701 | * Set the insert mode bit. |
| 1702 | * |
| 1703 | * If insert mode is on, existing text beyond the cursor position will be |
| 1704 | * shifted right to make room for new text. Otherwise, new text overwrites |
| 1705 | * any existing text. |
| 1706 | * |
| 1707 | * Defaults to off. |
| 1708 | * |
| 1709 | * @param {boolean} state True to set insert mode, false to unset. |
| 1710 | */ |
| 1711 | hterm.Terminal.prototype.setInsertMode = function(state) { |
| 1712 | this.options_.insertMode = state; |
| 1713 | }; |
| 1714 | |
| 1715 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1716 | * Set the auto carriage return bit. |
| 1717 | * |
| 1718 | * If auto carriage return is on then a formfeed character is interpreted |
| 1719 | * as a newline, otherwise it's the same as a linefeed. The difference boils |
| 1720 | * down to whether or not the cursor column is reset. |
| 1721 | */ |
| 1722 | hterm.Terminal.prototype.setAutoCarriageReturn = function(state) { |
| 1723 | this.options_.autoCarriageReturn = state; |
| 1724 | }; |
| 1725 | |
| 1726 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1727 | * Set the wraparound mode bit. |
| 1728 | * |
| 1729 | * If wraparound mode is on, certain VT commands will allow the cursor to wrap |
| 1730 | * to the start of the following row. Otherwise, the cursor is clamped to the |
| 1731 | * end of the screen and attempts to write past it are ignored. |
| 1732 | * |
| 1733 | * Defaults to on. |
| 1734 | * |
| 1735 | * @param {boolean} state True to set wraparound mode, false to unset. |
| 1736 | */ |
| 1737 | hterm.Terminal.prototype.setWraparound = function(state) { |
| 1738 | this.options_.wraparound = state; |
| 1739 | }; |
| 1740 | |
| 1741 | /** |
| 1742 | * Set the reverse-wraparound mode bit. |
| 1743 | * |
| 1744 | * If wraparound mode is off, certain VT commands will allow the cursor to wrap |
| 1745 | * to the end of the previous row. Otherwise, the cursor is clamped to column |
| 1746 | * 0. |
| 1747 | * |
| 1748 | * Defaults to off. |
| 1749 | * |
| 1750 | * @param {boolean} state True to set reverse-wraparound mode, false to unset. |
| 1751 | */ |
| 1752 | hterm.Terminal.prototype.setReverseWraparound = function(state) { |
| 1753 | this.options_.reverseWraparound = state; |
| 1754 | }; |
| 1755 | |
| 1756 | /** |
| 1757 | * Selects between the primary and alternate screens. |
| 1758 | * |
| 1759 | * If alternate mode is on, the alternate screen is active. Otherwise the |
| 1760 | * primary screen is active. |
| 1761 | * |
| 1762 | * Swapping screens has no effect on the scrollback buffer. |
| 1763 | * |
| 1764 | * Each screen maintains its own cursor position. |
| 1765 | * |
| 1766 | * Defaults to off. |
| 1767 | * |
| 1768 | * @param {boolean} state True to set alternate mode, false to unset. |
| 1769 | */ |
| 1770 | hterm.Terminal.prototype.setAlternateMode = function(state) { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1771 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1772 | this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_; |
| 1773 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1774 | if (this.screen_.rowsArray.length && |
| 1775 | this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) { |
| 1776 | // If the screen changed sizes while we were away, our rowIndexes may |
| 1777 | // be incorrect. |
| 1778 | var offset = this.scrollbackRows_.length; |
| 1779 | var ary = this.screen_.rowsArray; |
| 1780 | for (i = 0; i < ary.length; i++) { |
| 1781 | ary[i].rowIndex = offset + i; |
| 1782 | } |
| 1783 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1784 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1785 | this.realizeWidth_(this.screenSize.width); |
| 1786 | this.realizeHeight_(this.screenSize.height); |
| 1787 | this.scrollPort_.syncScrollHeight(); |
| 1788 | this.scrollPort_.invalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1789 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1790 | this.restoreCursor(cursor); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1791 | this.scrollPort_.resize(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1792 | }; |
| 1793 | |
| 1794 | /** |
| 1795 | * Set the cursor-blink mode bit. |
| 1796 | * |
| 1797 | * If cursor-blink is on, the cursor will blink when it is visible. Otherwise |
| 1798 | * a visible cursor does not blink. |
| 1799 | * |
| 1800 | * You should make sure to turn blinking off if you're going to dispose of a |
| 1801 | * terminal, otherwise you'll leak a timeout. |
| 1802 | * |
| 1803 | * Defaults to on. |
| 1804 | * |
| 1805 | * @param {boolean} state True to set cursor-blink mode, false to unset. |
| 1806 | */ |
| 1807 | hterm.Terminal.prototype.setCursorBlink = function(state) { |
| 1808 | this.options_.cursorBlink = state; |
| 1809 | |
| 1810 | if (!state && this.timeouts_.cursorBlink) { |
| 1811 | clearTimeout(this.timeouts_.cursorBlink); |
| 1812 | delete this.timeouts_.cursorBlink; |
| 1813 | } |
| 1814 | |
| 1815 | if (this.options_.cursorVisible) |
| 1816 | this.setCursorVisible(true); |
| 1817 | }; |
| 1818 | |
| 1819 | /** |
| 1820 | * Set the cursor-visible mode bit. |
| 1821 | * |
| 1822 | * If cursor-visible is on, the cursor will be visible. Otherwise it will not. |
| 1823 | * |
| 1824 | * Defaults to on. |
| 1825 | * |
| 1826 | * @param {boolean} state True to set cursor-visible mode, false to unset. |
| 1827 | */ |
| 1828 | hterm.Terminal.prototype.setCursorVisible = function(state) { |
| 1829 | this.options_.cursorVisible = state; |
| 1830 | |
| 1831 | if (!state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1832 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1833 | return; |
| 1834 | } |
| 1835 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1836 | this.syncCursorPosition_(); |
| 1837 | |
| 1838 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1839 | |
| 1840 | if (this.options_.cursorBlink) { |
| 1841 | if (this.timeouts_.cursorBlink) |
| 1842 | return; |
| 1843 | |
| 1844 | this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this), |
| 1845 | 500); |
| 1846 | } else { |
| 1847 | if (this.timeouts_.cursorBlink) { |
| 1848 | clearTimeout(this.timeouts_.cursorBlink); |
| 1849 | delete this.timeouts_.cursorBlink; |
| 1850 | } |
| 1851 | } |
| 1852 | }; |
| 1853 | |
| 1854 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1855 | * Synchronizes the visible cursor and document selection with the current |
| 1856 | * cursor coordinates. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1857 | */ |
| 1858 | hterm.Terminal.prototype.syncCursorPosition_ = function() { |
| 1859 | var topRowIndex = this.scrollPort_.getTopRowIndex(); |
| 1860 | var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex); |
| 1861 | var cursorRowIndex = this.scrollbackRows_.length + |
| 1862 | this.screen_.cursorPosition.row; |
| 1863 | |
| 1864 | if (cursorRowIndex > bottomRowIndex) { |
| 1865 | // Cursor is scrolled off screen, move it outside of the visible area. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1866 | this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1867 | return; |
| 1868 | } |
| 1869 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1870 | this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px'; |
| 1871 | this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px'; |
| 1872 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1873 | this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin + |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1874 | this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) + |
| 1875 | 'px'; |
| 1876 | this.cursorNode_.style.left = this.scrollPort_.characterSize.width * |
| 1877 | this.screen_.cursorPosition.column + 'px'; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1878 | |
| 1879 | this.cursorNode_.setAttribute('title', |
| 1880 | '(' + this.screen_.cursorPosition.row + |
| 1881 | ', ' + this.screen_.cursorPosition.column + |
| 1882 | ')'); |
| 1883 | |
| 1884 | // Update the caret for a11y purposes. |
| 1885 | var selection = this.document_.getSelection(); |
| 1886 | if (selection && selection.isCollapsed) |
| 1887 | this.screen_.syncSelectionCaret(selection); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1888 | }; |
| 1889 | |
| 1890 | /** |
| 1891 | * Synchronizes the visible cursor with the current cursor coordinates. |
| 1892 | * |
| 1893 | * The sync will happen asynchronously, soon after the call stack winds down. |
| 1894 | * Multiple calls will be coalesced into a single sync. |
| 1895 | */ |
| 1896 | hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() { |
| 1897 | if (this.timeouts_.syncCursor) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1898 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1899 | |
| 1900 | var self = this; |
| 1901 | this.timeouts_.syncCursor = setTimeout(function() { |
| 1902 | self.syncCursorPosition_(); |
| 1903 | delete self.timeouts_.syncCursor; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1904 | }, 0); |
| 1905 | }; |
| 1906 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 1907 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1908 | * Show or hide the zoom warning. |
| 1909 | * |
| 1910 | * The zoom warning is a message warning the user that their browser zoom must |
| 1911 | * be set to 100% in order for hterm to function properly. |
| 1912 | * |
| 1913 | * @param {boolean} state True to show the message, false to hide it. |
| 1914 | */ |
| 1915 | hterm.Terminal.prototype.showZoomWarning_ = function(state) { |
| 1916 | if (!this.zoomWarningNode_) { |
| 1917 | if (!state) |
| 1918 | return; |
| 1919 | |
| 1920 | this.zoomWarningNode_ = this.document_.createElement('div'); |
| 1921 | this.zoomWarningNode_.style.cssText = ( |
| 1922 | 'color: black;' + |
| 1923 | 'background-color: #ff2222;' + |
| 1924 | 'font-size: large;' + |
| 1925 | 'border-radius: 8px;' + |
| 1926 | 'opacity: 0.75;' + |
| 1927 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 1928 | 'top: 0.5em;' + |
| 1929 | 'right: 1.2em;' + |
| 1930 | 'position: absolute;' + |
| 1931 | '-webkit-text-size-adjust: none;' + |
| 1932 | '-webkit-user-select: none;'); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
rginda | de84e38 | 2012-04-20 15:39:31 -0700 | [diff] [blame] | 1935 | this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') || |
| 1936 | ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) + |
| 1937 | '% !!'); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1938 | this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 1939 | |
| 1940 | if (state) { |
| 1941 | if (!this.zoomWarningNode_.parentNode) |
| 1942 | this.div_.parentNode.appendChild(this.zoomWarningNode_); |
| 1943 | } else if (this.zoomWarningNode_.parentNode) { |
| 1944 | this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_); |
| 1945 | } |
| 1946 | }; |
| 1947 | |
| 1948 | /** |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 1949 | * Show the terminal overlay for a given amount of time. |
| 1950 | * |
| 1951 | * The terminal overlay appears in inverse video in a large font, centered |
| 1952 | * over the terminal. You should probably keep the overlay message brief, |
| 1953 | * since it's in a large font and you probably aren't going to check the size |
| 1954 | * of the terminal first. |
| 1955 | * |
| 1956 | * @param {string} msg The text (not HTML) message to display in the overlay. |
| 1957 | * @param {number} opt_timeout The amount of time to wait before fading out |
| 1958 | * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay |
| 1959 | * stay up forever (or until the next overlay). |
| 1960 | */ |
| 1961 | hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1962 | if (!this.overlayNode_) { |
| 1963 | if (!this.div_) |
| 1964 | return; |
| 1965 | |
| 1966 | this.overlayNode_ = this.document_.createElement('div'); |
| 1967 | this.overlayNode_.style.cssText = ( |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1968 | 'border-radius: 15px;' + |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1969 | 'font-size: xx-large;' + |
| 1970 | 'opacity: 0.75;' + |
| 1971 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 1972 | 'position: absolute;' + |
| 1973 | '-webkit-user-select: none;' + |
| 1974 | '-webkit-transition: opacity 180ms ease-in;'); |
| 1975 | } |
| 1976 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1977 | this.overlayNode_.style.color = this.prefs_.get('background-color'); |
| 1978 | this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color'); |
| 1979 | this.overlayNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 1980 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1981 | this.overlayNode_.textContent = msg; |
| 1982 | this.overlayNode_.style.opacity = '0.75'; |
| 1983 | |
| 1984 | if (!this.overlayNode_.parentNode) |
| 1985 | this.div_.appendChild(this.overlayNode_); |
| 1986 | |
| 1987 | this.overlayNode_.style.top = ( |
| 1988 | this.div_.clientHeight - this.overlayNode_.clientHeight) / 2; |
| 1989 | this.overlayNode_.style.left = ( |
| 1990 | this.div_.clientWidth - this.overlayNode_.clientWidth - |
| 1991 | this.scrollbarWidthPx) / 2; |
| 1992 | |
| 1993 | var self = this; |
| 1994 | |
| 1995 | if (this.overlayTimeout_) |
| 1996 | clearTimeout(this.overlayTimeout_); |
| 1997 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 1998 | if (opt_timeout === null) |
| 1999 | return; |
| 2000 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2001 | this.overlayTimeout_ = setTimeout(function() { |
| 2002 | self.overlayNode_.style.opacity = '0'; |
| 2003 | setTimeout(function() { |
rginda | 259dcca | 2012-03-14 16:37:11 -0700 | [diff] [blame] | 2004 | if (self.overlayNode_.parentNode) |
| 2005 | self.overlayNode_.parentNode.removeChild(self.overlayNode_); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2006 | self.overlayTimeout_ = null; |
| 2007 | self.overlayNode_.style.opacity = '0.75'; |
| 2008 | }, 200); |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2009 | }, opt_timeout || 1500); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2010 | }; |
| 2011 | |
| 2012 | hterm.Terminal.prototype.overlaySize = function() { |
| 2013 | this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height); |
| 2014 | }; |
| 2015 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2016 | /** |
| 2017 | * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected. |
| 2018 | * |
| 2019 | * @param {string} string The VT string representing the keystroke. |
| 2020 | */ |
| 2021 | hterm.Terminal.prototype.onVTKeystroke = function(string) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2022 | if (this.scrollOnKeystroke_) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2023 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 2024 | |
| 2025 | this.io.onVTKeystroke(string); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2026 | }; |
| 2027 | |
| 2028 | /** |
| 2029 | * React when the ScrollPort is scrolled. |
| 2030 | */ |
| 2031 | hterm.Terminal.prototype.onScroll_ = function() { |
| 2032 | this.scheduleSyncCursorPosition_(); |
| 2033 | }; |
| 2034 | |
| 2035 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 2036 | * React when text is pasted into the scrollPort. |
| 2037 | */ |
| 2038 | hterm.Terminal.prototype.onPaste_ = function(e) { |
| 2039 | this.io.onVTKeystroke(e.text); |
| 2040 | }; |
| 2041 | |
| 2042 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2043 | * React when the ScrollPort is resized. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 2044 | * |
| 2045 | * Note: This function should not directly contain code that alters the internal |
| 2046 | * state of the terminal. That kind of code belongs in realizeWidth or |
| 2047 | * realizeHeight, so that it can be executed synchronously in the case of a |
| 2048 | * programmatic width change. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2049 | */ |
| 2050 | hterm.Terminal.prototype.onResize_ = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 2051 | var columnCount = Math.floor(this.scrollPort_.getScreenWidth() / |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2052 | this.scrollPort_.characterSize.width); |
| 2053 | var rowCount = Math.floor(this.scrollPort_.getScreenHeight() / |
| 2054 | this.scrollPort_.characterSize.height); |
| 2055 | |
| 2056 | if (!(columnCount || rowCount)) { |
| 2057 | // We avoid these situations since they happen sometimes when the terminal |
| 2058 | // gets removed from the document, and we can't deal with that. |
| 2059 | return; |
| 2060 | } |
| 2061 | |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 2062 | this.realizeSize_(columnCount, rowCount); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 2063 | this.scheduleSyncCursorPosition_(); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2064 | this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2065 | this.overlaySize(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2066 | }; |
| 2067 | |
| 2068 | /** |
| 2069 | * Service the cursor blink timeout. |
| 2070 | */ |
| 2071 | hterm.Terminal.prototype.onCursorBlink_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2072 | if (this.cursorNode_.style.opacity == '0') { |
| 2073 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2074 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2075 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2076 | } |
| 2077 | }; |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 2078 | |
| 2079 | /** |
| 2080 | * Set the scrollbar-visible mode bit. |
| 2081 | * |
| 2082 | * If scrollbar-visible is on, the vertical scrollbar will be visible. |
| 2083 | * Otherwise it will not. |
| 2084 | * |
| 2085 | * Defaults to on. |
| 2086 | * |
| 2087 | * @param {boolean} state True to set scrollbar-visible mode, false to unset. |
| 2088 | */ |
| 2089 | hterm.Terminal.prototype.setScrollbarVisible = function(state) { |
| 2090 | this.scrollPort_.setScrollbarVisible(state); |
| 2091 | }; |