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