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