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