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 | 1b06b37 | 2013-07-19 15:22:51 -0700 | [diff] [blame^] | 1388 | if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) { |
| 1389 | // We're at the end of the VT Scroll Region. (It's possible we're also at |
| 1390 | // the end of the physical screen.) Perform a VT Scroll rather than create |
| 1391 | // a new terminal row |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1392 | this.vtScrollUp(1); |
| 1393 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
Robert Ginda | 1b06b37 | 2013-07-19 15:22:51 -0700 | [diff] [blame^] | 1394 | } else if ( |
| 1395 | this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) { |
| 1396 | // We're at the end of the screen. Append a new row to the terminal, |
| 1397 | // shifting the top row into the scrollback. |
| 1398 | this.appendRows_(1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1399 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1400 | // Anywhere else in the screen just moves the cursor. |
| 1401 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1402 | } |
| 1403 | }; |
| 1404 | |
| 1405 | /** |
| 1406 | * Like newLine(), except maintain the cursor column. |
| 1407 | */ |
| 1408 | hterm.Terminal.prototype.lineFeed = function() { |
| 1409 | var column = this.screen_.cursorPosition.column; |
| 1410 | this.newLine(); |
| 1411 | this.setCursorColumn(column); |
| 1412 | }; |
| 1413 | |
| 1414 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1415 | * If autoCarriageReturn is set then newLine(), else lineFeed(). |
| 1416 | */ |
| 1417 | hterm.Terminal.prototype.formFeed = function() { |
| 1418 | if (this.options_.autoCarriageReturn) { |
| 1419 | this.newLine(); |
| 1420 | } else { |
| 1421 | this.lineFeed(); |
| 1422 | } |
| 1423 | }; |
| 1424 | |
| 1425 | /** |
| 1426 | * Move the cursor up one row, possibly inserting a blank line. |
| 1427 | * |
| 1428 | * The cursor column is not changed. |
| 1429 | */ |
| 1430 | hterm.Terminal.prototype.reverseLineFeed = function() { |
| 1431 | var scrollTop = this.getVTScrollTop(); |
| 1432 | var currentRow = this.screen_.cursorPosition.row; |
| 1433 | |
| 1434 | if (currentRow == scrollTop) { |
| 1435 | this.insertLines(1); |
| 1436 | } else { |
| 1437 | this.setAbsoluteCursorRow(currentRow - 1); |
| 1438 | } |
| 1439 | }; |
| 1440 | |
| 1441 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1442 | * Replace all characters to the left of the current cursor with the space |
| 1443 | * character. |
| 1444 | * |
| 1445 | * TODO(rginda): This should probably *remove* the characters (not just replace |
| 1446 | * 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] | 1447 | * position. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1448 | */ |
| 1449 | hterm.Terminal.prototype.eraseToLeft = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1450 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1451 | this.setCursorColumn(0); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1452 | this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1453 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1454 | }; |
| 1455 | |
| 1456 | /** |
David Benjamin | 684a9b7 | 2012-05-01 17:19:58 -0400 | [diff] [blame] | 1457 | * Erase a given number of characters to the right of the cursor. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1458 | * |
| 1459 | * The cursor position is unchanged. |
| 1460 | * |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1461 | * If the current background color is not the default background color this |
| 1462 | * will insert spaces rather than delete. This is unfortunate because the |
| 1463 | * trailing space will affect text selection, but it's difficult to come up |
| 1464 | * with a way to style empty space that wouldn't trip up the hterm.Screen |
| 1465 | * code. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1466 | */ |
| 1467 | hterm.Terminal.prototype.eraseToRight = function(opt_count) { |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 1468 | var maxCount = this.screenSize.width - this.screen_.cursorPosition.column; |
| 1469 | var count = opt_count ? Math.min(opt_count, maxCount) : maxCount; |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1470 | |
| 1471 | if (this.screen_.textAttributes.background === |
| 1472 | this.screen_.textAttributes.DEFAULT_COLOR) { |
| 1473 | var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row]; |
| 1474 | if (cursorRow.textContent.length <= |
| 1475 | this.screen_.cursorPosition.column + count) { |
| 1476 | this.screen_.deleteChars(count); |
| 1477 | this.clearCursorOverflow(); |
| 1478 | return; |
| 1479 | } |
| 1480 | } |
| 1481 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1482 | var cursor = this.saveCursor(); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 1483 | this.screen_.overwriteString(lib.f.getWhitespace(count)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1484 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1485 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1486 | }; |
| 1487 | |
| 1488 | /** |
| 1489 | * Erase the current line. |
| 1490 | * |
| 1491 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1492 | */ |
| 1493 | hterm.Terminal.prototype.eraseLine = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1494 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1495 | this.screen_.clearCursorRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1496 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1497 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1498 | }; |
| 1499 | |
| 1500 | /** |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1501 | * Erase all characters from the start of the screen to the current cursor |
| 1502 | * position, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1503 | * |
| 1504 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1505 | */ |
| 1506 | hterm.Terminal.prototype.eraseAbove = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1507 | var cursor = this.saveCursor(); |
| 1508 | |
| 1509 | this.eraseToLeft(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1510 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1511 | for (var i = 0; i < cursor.row; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1512 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1513 | this.screen_.clearCursorRow(); |
| 1514 | } |
| 1515 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1516 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1517 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | /** |
| 1521 | * 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] | 1522 | * screen, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1523 | * |
| 1524 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1525 | */ |
| 1526 | hterm.Terminal.prototype.eraseBelow = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1527 | var cursor = this.saveCursor(); |
| 1528 | |
| 1529 | this.eraseToRight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1530 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1531 | var bottom = this.screenSize.height - 1; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1532 | for (var i = cursor.row + 1; i <= bottom; i++) { |
| 1533 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1534 | this.screen_.clearCursorRow(); |
| 1535 | } |
| 1536 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1537 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1538 | this.clearCursorOverflow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1539 | }; |
| 1540 | |
| 1541 | /** |
| 1542 | * Fill the terminal with a given character. |
| 1543 | * |
| 1544 | * This methods does not respect the VT scroll region. |
| 1545 | * |
| 1546 | * @param {string} ch The character to use for the fill. |
| 1547 | */ |
| 1548 | hterm.Terminal.prototype.fill = function(ch) { |
| 1549 | var cursor = this.saveCursor(); |
| 1550 | |
| 1551 | this.setAbsoluteCursorPosition(0, 0); |
| 1552 | for (var row = 0; row < this.screenSize.height; row++) { |
| 1553 | for (var col = 0; col < this.screenSize.width; col++) { |
| 1554 | this.setAbsoluteCursorPosition(row, col); |
| 1555 | this.screen_.overwriteString(ch); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1560 | }; |
| 1561 | |
| 1562 | /** |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1563 | * Erase the entire display and leave the cursor at (0, 0). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1564 | * |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1565 | * This does not respect the scroll region. |
| 1566 | * |
| 1567 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1568 | * to the current screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1569 | */ |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1570 | hterm.Terminal.prototype.clearHome = function(opt_screen) { |
| 1571 | var screen = opt_screen || this.screen_; |
| 1572 | var bottom = screen.getHeight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1573 | |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 1574 | if (bottom == 0) { |
| 1575 | // Empty screen, nothing to do. |
| 1576 | return; |
| 1577 | } |
| 1578 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1579 | for (var i = 0; i < bottom; i++) { |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1580 | screen.setCursorPosition(i, 0); |
| 1581 | screen.clearCursorRow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1584 | screen.setCursorPosition(0, 0); |
| 1585 | }; |
| 1586 | |
| 1587 | /** |
| 1588 | * Erase the entire display without changing the cursor position. |
| 1589 | * |
| 1590 | * The cursor position is unchanged. This does not respect the scroll |
| 1591 | * region. |
| 1592 | * |
| 1593 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1594 | * to the current screen. |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1595 | */ |
| 1596 | hterm.Terminal.prototype.clear = function(opt_screen) { |
| 1597 | var screen = opt_screen || this.screen_; |
| 1598 | var cursor = screen.cursorPosition.clone(); |
| 1599 | this.clearHome(screen); |
| 1600 | screen.setCursorPosition(cursor.row, cursor.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1601 | }; |
| 1602 | |
| 1603 | /** |
| 1604 | * VT command to insert lines at the current cursor row. |
| 1605 | * |
| 1606 | * This respects the current scroll region. Rows pushed off the bottom are |
| 1607 | * lost (they won't show up in the scrollback buffer). |
| 1608 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1609 | * @param {integer} count The number of lines to insert. |
| 1610 | */ |
| 1611 | hterm.Terminal.prototype.insertLines = function(count) { |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1612 | var cursorRow = this.screen_.cursorPosition.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1613 | |
| 1614 | var bottom = this.getVTScrollBottom(); |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1615 | count = Math.min(count, bottom - cursorRow); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1616 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1617 | // The moveCount is the number of rows we need to relocate to make room for |
| 1618 | // the new row(s). The count is the distance to move them. |
| 1619 | var moveCount = bottom - cursorRow - count + 1; |
| 1620 | if (moveCount) |
| 1621 | this.moveRows_(cursorRow, moveCount, cursorRow + count); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1622 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1623 | for (var i = count - 1; i >= 0; i--) { |
| 1624 | this.setAbsoluteCursorPosition(cursorRow + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1625 | this.screen_.clearCursorRow(); |
| 1626 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1627 | }; |
| 1628 | |
| 1629 | /** |
| 1630 | * VT command to delete lines at the current cursor row. |
| 1631 | * |
| 1632 | * New rows are added to the bottom of scroll region to take their place. New |
| 1633 | * rows are strictly there to take up space and have no content or style. |
| 1634 | */ |
| 1635 | hterm.Terminal.prototype.deleteLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1636 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1637 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1638 | var top = cursor.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1639 | var bottom = this.getVTScrollBottom(); |
| 1640 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1641 | var maxCount = bottom - top + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1642 | count = Math.min(count, maxCount); |
| 1643 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1644 | var moveStart = bottom - count + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1645 | if (count != maxCount) |
| 1646 | this.moveRows_(top, count, moveStart); |
| 1647 | |
| 1648 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1649 | this.setAbsoluteCursorPosition(moveStart + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1650 | this.screen_.clearCursorRow(); |
| 1651 | } |
| 1652 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1653 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1654 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1655 | }; |
| 1656 | |
| 1657 | /** |
| 1658 | * Inserts the given number of spaces at the current cursor position. |
| 1659 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1660 | * The cursor position is not changed. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1661 | */ |
| 1662 | hterm.Terminal.prototype.insertSpace = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1663 | var cursor = this.saveCursor(); |
| 1664 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1665 | var ws = lib.f.getWhitespace(count || 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1666 | this.screen_.insertString(ws); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1667 | this.screen_.maybeClipCurrentRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1668 | |
| 1669 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1670 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1671 | }; |
| 1672 | |
| 1673 | /** |
| 1674 | * Forward-delete the specified number of characters starting at the cursor |
| 1675 | * position. |
| 1676 | * |
| 1677 | * @param {integer} count The number of characters to delete. |
| 1678 | */ |
| 1679 | hterm.Terminal.prototype.deleteChars = function(count) { |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 1680 | var deleted = this.screen_.deleteChars(count); |
| 1681 | if (deleted && !this.screen_.textAttributes.isDefault()) { |
| 1682 | var cursor = this.saveCursor(); |
| 1683 | this.setCursorColumn(this.screenSize.width - deleted); |
| 1684 | this.screen_.insertString(lib.f.getWhitespace(deleted)); |
| 1685 | this.restoreCursor(cursor); |
| 1686 | } |
| 1687 | |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1688 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | /** |
| 1692 | * Shift rows in the scroll region upwards by a given number of lines. |
| 1693 | * |
| 1694 | * New rows are inserted at the bottom of the scroll region to fill the |
| 1695 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1696 | * |
| 1697 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1698 | * off the top are lost. |
| 1699 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1700 | * The cursor position is not altered. |
| 1701 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1702 | * @param {integer} count The number of rows to scroll. |
| 1703 | */ |
| 1704 | hterm.Terminal.prototype.vtScrollUp = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1705 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1706 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1707 | this.setAbsoluteCursorRow(this.getVTScrollTop()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1708 | this.deleteLines(count); |
| 1709 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1710 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1711 | }; |
| 1712 | |
| 1713 | /** |
| 1714 | * Shift rows below the cursor down by a given number of lines. |
| 1715 | * |
| 1716 | * This function respects the current scroll region. |
| 1717 | * |
| 1718 | * New rows are inserted at the top of the scroll region to fill the |
| 1719 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1720 | * |
| 1721 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1722 | * off the bottom are lost. |
| 1723 | * |
| 1724 | * @param {integer} count The number of rows to scroll. |
| 1725 | */ |
| 1726 | hterm.Terminal.prototype.vtScrollDown = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1727 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1728 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1729 | this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1730 | this.insertLines(opt_count); |
| 1731 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1732 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1733 | }; |
| 1734 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1735 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1736 | /** |
| 1737 | * Set the cursor position. |
| 1738 | * |
| 1739 | * The cursor row is relative to the scroll region if the terminal has |
| 1740 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1741 | * |
| 1742 | * @param {integer} row The new zero-based cursor row. |
| 1743 | * @param {integer} row The new zero-based cursor column. |
| 1744 | */ |
| 1745 | hterm.Terminal.prototype.setCursorPosition = function(row, column) { |
| 1746 | if (this.options_.originMode) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1747 | this.setRelativeCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1748 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1749 | this.setAbsoluteCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1750 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1751 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1752 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1753 | hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) { |
| 1754 | var scrollTop = this.getVTScrollTop(); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1755 | row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom()); |
| 1756 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1757 | this.screen_.setCursorPosition(row, column); |
| 1758 | }; |
| 1759 | |
| 1760 | hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1761 | row = lib.f.clamp(row, 0, this.screenSize.height - 1); |
| 1762 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1763 | this.screen_.setCursorPosition(row, column); |
| 1764 | }; |
| 1765 | |
| 1766 | /** |
| 1767 | * Set the cursor column. |
| 1768 | * |
| 1769 | * @param {integer} column The new zero-based cursor column. |
| 1770 | */ |
| 1771 | hterm.Terminal.prototype.setCursorColumn = function(column) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1772 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1773 | }; |
| 1774 | |
| 1775 | /** |
| 1776 | * Return the cursor column. |
| 1777 | * |
| 1778 | * @return {integer} The zero-based cursor column. |
| 1779 | */ |
| 1780 | hterm.Terminal.prototype.getCursorColumn = function() { |
| 1781 | return this.screen_.cursorPosition.column; |
| 1782 | }; |
| 1783 | |
| 1784 | /** |
| 1785 | * Set the cursor row. |
| 1786 | * |
| 1787 | * The cursor row is relative to the scroll region if the terminal has |
| 1788 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1789 | * |
| 1790 | * @param {integer} row The new cursor row. |
| 1791 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1792 | hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) { |
| 1793 | this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1794 | }; |
| 1795 | |
| 1796 | /** |
| 1797 | * Return the cursor row. |
| 1798 | * |
| 1799 | * @return {integer} The zero-based cursor row. |
| 1800 | */ |
| 1801 | hterm.Terminal.prototype.getCursorRow = function(row) { |
| 1802 | return this.screen_.cursorPosition.row; |
| 1803 | }; |
| 1804 | |
| 1805 | /** |
| 1806 | * Request that the ScrollPort redraw itself soon. |
| 1807 | * |
| 1808 | * The redraw will happen asynchronously, soon after the call stack winds down. |
| 1809 | * Multiple calls will be coalesced into a single redraw. |
| 1810 | */ |
| 1811 | hterm.Terminal.prototype.scheduleRedraw_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1812 | if (this.timeouts_.redraw) |
| 1813 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1814 | |
| 1815 | var self = this; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1816 | this.timeouts_.redraw = setTimeout(function() { |
| 1817 | delete self.timeouts_.redraw; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1818 | self.scrollPort_.redraw_(); |
| 1819 | }, 0); |
| 1820 | }; |
| 1821 | |
| 1822 | /** |
| 1823 | * Request that the ScrollPort be scrolled to the bottom. |
| 1824 | * |
| 1825 | * The scroll will happen asynchronously, soon after the call stack winds down. |
| 1826 | * Multiple calls will be coalesced into a single scroll. |
| 1827 | * |
| 1828 | * This affects the scrollbar position of the ScrollPort, and has nothing to |
| 1829 | * do with the VT scroll commands. |
| 1830 | */ |
| 1831 | hterm.Terminal.prototype.scheduleScrollDown_ = function() { |
| 1832 | if (this.timeouts_.scrollDown) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1833 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1834 | |
| 1835 | var self = this; |
| 1836 | this.timeouts_.scrollDown = setTimeout(function() { |
| 1837 | delete self.timeouts_.scrollDown; |
| 1838 | self.scrollPort_.scrollRowToBottom(self.getRowCount()); |
| 1839 | }, 10); |
| 1840 | }; |
| 1841 | |
| 1842 | /** |
| 1843 | * Move the cursor up a specified number of rows. |
| 1844 | * |
| 1845 | * @param {integer} count The number of rows to move the cursor. |
| 1846 | */ |
| 1847 | hterm.Terminal.prototype.cursorUp = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1848 | return this.cursorDown(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1849 | }; |
| 1850 | |
| 1851 | /** |
| 1852 | * Move the cursor down a specified number of rows. |
| 1853 | * |
| 1854 | * @param {integer} count The number of rows to move the cursor. |
| 1855 | */ |
| 1856 | hterm.Terminal.prototype.cursorDown = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1857 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1858 | var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0); |
| 1859 | var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() : |
| 1860 | this.screenSize.height - 1); |
| 1861 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1862 | var row = lib.f.clamp(this.screen_.cursorPosition.row + count, |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1863 | minHeight, maxHeight); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1864 | this.setAbsoluteCursorRow(row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1865 | }; |
| 1866 | |
| 1867 | /** |
| 1868 | * Move the cursor left a specified number of columns. |
| 1869 | * |
| 1870 | * @param {integer} count The number of columns to move the cursor. |
| 1871 | */ |
| 1872 | hterm.Terminal.prototype.cursorLeft = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1873 | return this.cursorRight(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1874 | }; |
| 1875 | |
| 1876 | /** |
| 1877 | * Move the cursor right a specified number of columns. |
| 1878 | * |
| 1879 | * @param {integer} count The number of columns to move the cursor. |
| 1880 | */ |
| 1881 | hterm.Terminal.prototype.cursorRight = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1882 | count = count || 1; |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1883 | var column = lib.f.clamp(this.screen_.cursorPosition.column + count, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1884 | 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1885 | this.setCursorColumn(column); |
| 1886 | }; |
| 1887 | |
| 1888 | /** |
| 1889 | * Reverse the foreground and background colors of the terminal. |
| 1890 | * |
| 1891 | * This only affects text that was drawn with no attributes. |
| 1892 | * |
| 1893 | * TODO(rginda): Test xterm to see if reverse is respected for text that has |
| 1894 | * been drawn with attributes that happen to coincide with the default |
| 1895 | * 'no-attribute' colors. My guess is probably not. |
| 1896 | */ |
| 1897 | hterm.Terminal.prototype.setReverseVideo = function(state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1898 | this.options_.reverseVideo = state; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1899 | if (state) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1900 | this.scrollPort_.setForegroundColor(this.prefs_.get('background-color')); |
| 1901 | this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1902 | } else { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1903 | this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color')); |
| 1904 | this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1905 | } |
| 1906 | }; |
| 1907 | |
| 1908 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1909 | * Ring the terminal bell. |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 1910 | * |
| 1911 | * This will not play the bell audio more than once per second. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1912 | */ |
| 1913 | hterm.Terminal.prototype.ringBell = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1914 | this.cursorNode_.style.backgroundColor = |
| 1915 | this.scrollPort_.getForegroundColor(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1916 | |
| 1917 | var self = this; |
| 1918 | setTimeout(function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1919 | self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color'); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1920 | }, 200); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (this.bellAudio_.getAttribute('src')) { |
Robert Ginda | a633137 | 2013-03-19 10:35:39 -0700 | [diff] [blame] | 1923 | if (this.bellSquelchTimeout_) |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 1924 | return; |
| 1925 | |
| 1926 | this.bellAudio_.play(); |
| 1927 | |
| 1928 | this.bellSequelchTimeout_ = setTimeout(function() { |
| 1929 | delete this.bellSquelchTimeout_; |
Robert Ginda | a633137 | 2013-03-19 10:35:39 -0700 | [diff] [blame] | 1930 | }.bind(this), 500); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 1931 | } else { |
| 1932 | delete this.bellSquelchTimeout_; |
| 1933 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1934 | }; |
| 1935 | |
| 1936 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1937 | * Set the origin mode bit. |
| 1938 | * |
| 1939 | * If origin mode is on, certain VT cursor and scrolling commands measure their |
| 1940 | * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds |
| 1941 | * to the top of the addressable screen. |
| 1942 | * |
| 1943 | * Defaults to off. |
| 1944 | * |
| 1945 | * @param {boolean} state True to set origin mode, false to unset. |
| 1946 | */ |
| 1947 | hterm.Terminal.prototype.setOriginMode = function(state) { |
| 1948 | this.options_.originMode = state; |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1949 | this.setCursorPosition(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1950 | }; |
| 1951 | |
| 1952 | /** |
| 1953 | * Set the insert mode bit. |
| 1954 | * |
| 1955 | * If insert mode is on, existing text beyond the cursor position will be |
| 1956 | * shifted right to make room for new text. Otherwise, new text overwrites |
| 1957 | * any existing text. |
| 1958 | * |
| 1959 | * Defaults to off. |
| 1960 | * |
| 1961 | * @param {boolean} state True to set insert mode, false to unset. |
| 1962 | */ |
| 1963 | hterm.Terminal.prototype.setInsertMode = function(state) { |
| 1964 | this.options_.insertMode = state; |
| 1965 | }; |
| 1966 | |
| 1967 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1968 | * Set the auto carriage return bit. |
| 1969 | * |
| 1970 | * If auto carriage return is on then a formfeed character is interpreted |
| 1971 | * as a newline, otherwise it's the same as a linefeed. The difference boils |
| 1972 | * down to whether or not the cursor column is reset. |
| 1973 | */ |
| 1974 | hterm.Terminal.prototype.setAutoCarriageReturn = function(state) { |
| 1975 | this.options_.autoCarriageReturn = state; |
| 1976 | }; |
| 1977 | |
| 1978 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1979 | * Set the wraparound mode bit. |
| 1980 | * |
| 1981 | * If wraparound mode is on, certain VT commands will allow the cursor to wrap |
| 1982 | * to the start of the following row. Otherwise, the cursor is clamped to the |
| 1983 | * end of the screen and attempts to write past it are ignored. |
| 1984 | * |
| 1985 | * Defaults to on. |
| 1986 | * |
| 1987 | * @param {boolean} state True to set wraparound mode, false to unset. |
| 1988 | */ |
| 1989 | hterm.Terminal.prototype.setWraparound = function(state) { |
| 1990 | this.options_.wraparound = state; |
| 1991 | }; |
| 1992 | |
| 1993 | /** |
| 1994 | * Set the reverse-wraparound mode bit. |
| 1995 | * |
| 1996 | * If wraparound mode is off, certain VT commands will allow the cursor to wrap |
| 1997 | * to the end of the previous row. Otherwise, the cursor is clamped to column |
| 1998 | * 0. |
| 1999 | * |
| 2000 | * Defaults to off. |
| 2001 | * |
| 2002 | * @param {boolean} state True to set reverse-wraparound mode, false to unset. |
| 2003 | */ |
| 2004 | hterm.Terminal.prototype.setReverseWraparound = function(state) { |
| 2005 | this.options_.reverseWraparound = state; |
| 2006 | }; |
| 2007 | |
| 2008 | /** |
| 2009 | * Selects between the primary and alternate screens. |
| 2010 | * |
| 2011 | * If alternate mode is on, the alternate screen is active. Otherwise the |
| 2012 | * primary screen is active. |
| 2013 | * |
| 2014 | * Swapping screens has no effect on the scrollback buffer. |
| 2015 | * |
| 2016 | * Each screen maintains its own cursor position. |
| 2017 | * |
| 2018 | * Defaults to off. |
| 2019 | * |
| 2020 | * @param {boolean} state True to set alternate mode, false to unset. |
| 2021 | */ |
| 2022 | hterm.Terminal.prototype.setAlternateMode = function(state) { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2023 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2024 | this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_; |
| 2025 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2026 | if (this.screen_.rowsArray.length && |
| 2027 | this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) { |
| 2028 | // If the screen changed sizes while we were away, our rowIndexes may |
| 2029 | // be incorrect. |
| 2030 | var offset = this.scrollbackRows_.length; |
| 2031 | var ary = this.screen_.rowsArray; |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2032 | for (var i = 0; i < ary.length; i++) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2033 | ary[i].rowIndex = offset + i; |
| 2034 | } |
| 2035 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2036 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2037 | this.realizeWidth_(this.screenSize.width); |
| 2038 | this.realizeHeight_(this.screenSize.height); |
| 2039 | this.scrollPort_.syncScrollHeight(); |
| 2040 | this.scrollPort_.invalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2041 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2042 | this.restoreCursor(cursor); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2043 | this.scrollPort_.resize(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2044 | }; |
| 2045 | |
| 2046 | /** |
| 2047 | * Set the cursor-blink mode bit. |
| 2048 | * |
| 2049 | * If cursor-blink is on, the cursor will blink when it is visible. Otherwise |
| 2050 | * a visible cursor does not blink. |
| 2051 | * |
| 2052 | * You should make sure to turn blinking off if you're going to dispose of a |
| 2053 | * terminal, otherwise you'll leak a timeout. |
| 2054 | * |
| 2055 | * Defaults to on. |
| 2056 | * |
| 2057 | * @param {boolean} state True to set cursor-blink mode, false to unset. |
| 2058 | */ |
| 2059 | hterm.Terminal.prototype.setCursorBlink = function(state) { |
| 2060 | this.options_.cursorBlink = state; |
| 2061 | |
| 2062 | if (!state && this.timeouts_.cursorBlink) { |
| 2063 | clearTimeout(this.timeouts_.cursorBlink); |
| 2064 | delete this.timeouts_.cursorBlink; |
| 2065 | } |
| 2066 | |
| 2067 | if (this.options_.cursorVisible) |
| 2068 | this.setCursorVisible(true); |
| 2069 | }; |
| 2070 | |
| 2071 | /** |
| 2072 | * Set the cursor-visible mode bit. |
| 2073 | * |
| 2074 | * If cursor-visible is on, the cursor will be visible. Otherwise it will not. |
| 2075 | * |
| 2076 | * Defaults to on. |
| 2077 | * |
| 2078 | * @param {boolean} state True to set cursor-visible mode, false to unset. |
| 2079 | */ |
| 2080 | hterm.Terminal.prototype.setCursorVisible = function(state) { |
| 2081 | this.options_.cursorVisible = state; |
| 2082 | |
| 2083 | if (!state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2084 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2085 | return; |
| 2086 | } |
| 2087 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2088 | this.syncCursorPosition_(); |
| 2089 | |
| 2090 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2091 | |
| 2092 | if (this.options_.cursorBlink) { |
| 2093 | if (this.timeouts_.cursorBlink) |
| 2094 | return; |
| 2095 | |
| 2096 | this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this), |
| 2097 | 500); |
| 2098 | } else { |
| 2099 | if (this.timeouts_.cursorBlink) { |
| 2100 | clearTimeout(this.timeouts_.cursorBlink); |
| 2101 | delete this.timeouts_.cursorBlink; |
| 2102 | } |
| 2103 | } |
| 2104 | }; |
| 2105 | |
| 2106 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2107 | * Synchronizes the visible cursor and document selection with the current |
| 2108 | * cursor coordinates. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2109 | */ |
| 2110 | hterm.Terminal.prototype.syncCursorPosition_ = function() { |
| 2111 | var topRowIndex = this.scrollPort_.getTopRowIndex(); |
| 2112 | var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex); |
| 2113 | var cursorRowIndex = this.scrollbackRows_.length + |
| 2114 | this.screen_.cursorPosition.row; |
| 2115 | |
| 2116 | if (cursorRowIndex > bottomRowIndex) { |
| 2117 | // Cursor is scrolled off screen, move it outside of the visible area. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2118 | this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2119 | return; |
| 2120 | } |
| 2121 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2122 | this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px'; |
| 2123 | this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px'; |
| 2124 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2125 | this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin + |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2126 | this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) + |
| 2127 | 'px'; |
| 2128 | this.cursorNode_.style.left = this.scrollPort_.characterSize.width * |
| 2129 | this.screen_.cursorPosition.column + 'px'; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2130 | |
| 2131 | this.cursorNode_.setAttribute('title', |
| 2132 | '(' + this.screen_.cursorPosition.row + |
| 2133 | ', ' + this.screen_.cursorPosition.column + |
| 2134 | ')'); |
| 2135 | |
| 2136 | // Update the caret for a11y purposes. |
| 2137 | var selection = this.document_.getSelection(); |
| 2138 | if (selection && selection.isCollapsed) |
| 2139 | this.screen_.syncSelectionCaret(selection); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2140 | }; |
| 2141 | |
| 2142 | /** |
| 2143 | * Synchronizes the visible cursor with the current cursor coordinates. |
| 2144 | * |
| 2145 | * The sync will happen asynchronously, soon after the call stack winds down. |
| 2146 | * Multiple calls will be coalesced into a single sync. |
| 2147 | */ |
| 2148 | hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() { |
| 2149 | if (this.timeouts_.syncCursor) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2150 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2151 | |
| 2152 | var self = this; |
| 2153 | this.timeouts_.syncCursor = setTimeout(function() { |
| 2154 | self.syncCursorPosition_(); |
| 2155 | delete self.timeouts_.syncCursor; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2156 | }, 0); |
| 2157 | }; |
| 2158 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2159 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2160 | * Show or hide the zoom warning. |
| 2161 | * |
| 2162 | * The zoom warning is a message warning the user that their browser zoom must |
| 2163 | * be set to 100% in order for hterm to function properly. |
| 2164 | * |
| 2165 | * @param {boolean} state True to show the message, false to hide it. |
| 2166 | */ |
| 2167 | hterm.Terminal.prototype.showZoomWarning_ = function(state) { |
| 2168 | if (!this.zoomWarningNode_) { |
| 2169 | if (!state) |
| 2170 | return; |
| 2171 | |
| 2172 | this.zoomWarningNode_ = this.document_.createElement('div'); |
| 2173 | this.zoomWarningNode_.style.cssText = ( |
| 2174 | 'color: black;' + |
| 2175 | 'background-color: #ff2222;' + |
| 2176 | 'font-size: large;' + |
| 2177 | 'border-radius: 8px;' + |
| 2178 | 'opacity: 0.75;' + |
| 2179 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2180 | 'top: 0.5em;' + |
| 2181 | 'right: 1.2em;' + |
| 2182 | 'position: absolute;' + |
| 2183 | '-webkit-text-size-adjust: none;' + |
| 2184 | '-webkit-user-select: none;'); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 2187 | this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences( |
| 2188 | hterm.zoomWarningMessage, |
| 2189 | [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]); |
| 2190 | |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2191 | this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2192 | |
| 2193 | if (state) { |
| 2194 | if (!this.zoomWarningNode_.parentNode) |
| 2195 | this.div_.parentNode.appendChild(this.zoomWarningNode_); |
| 2196 | } else if (this.zoomWarningNode_.parentNode) { |
| 2197 | this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_); |
| 2198 | } |
| 2199 | }; |
| 2200 | |
| 2201 | /** |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2202 | * Show the terminal overlay for a given amount of time. |
| 2203 | * |
| 2204 | * The terminal overlay appears in inverse video in a large font, centered |
| 2205 | * over the terminal. You should probably keep the overlay message brief, |
| 2206 | * since it's in a large font and you probably aren't going to check the size |
| 2207 | * of the terminal first. |
| 2208 | * |
| 2209 | * @param {string} msg The text (not HTML) message to display in the overlay. |
| 2210 | * @param {number} opt_timeout The amount of time to wait before fading out |
| 2211 | * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay |
| 2212 | * stay up forever (or until the next overlay). |
| 2213 | */ |
| 2214 | hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2215 | if (!this.overlayNode_) { |
| 2216 | if (!this.div_) |
| 2217 | return; |
| 2218 | |
| 2219 | this.overlayNode_ = this.document_.createElement('div'); |
| 2220 | this.overlayNode_.style.cssText = ( |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2221 | 'border-radius: 15px;' + |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2222 | 'font-size: xx-large;' + |
| 2223 | 'opacity: 0.75;' + |
| 2224 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2225 | 'position: absolute;' + |
| 2226 | '-webkit-user-select: none;' + |
| 2227 | '-webkit-transition: opacity 180ms ease-in;'); |
| 2228 | } |
| 2229 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2230 | this.overlayNode_.style.color = this.prefs_.get('background-color'); |
| 2231 | this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color'); |
| 2232 | this.overlayNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2233 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2234 | this.overlayNode_.textContent = msg; |
| 2235 | this.overlayNode_.style.opacity = '0.75'; |
| 2236 | |
| 2237 | if (!this.overlayNode_.parentNode) |
| 2238 | this.div_.appendChild(this.overlayNode_); |
| 2239 | |
Robert Ginda | 9776928 | 2013-02-01 15:30:30 -0800 | [diff] [blame] | 2240 | var divSize = hterm.getClientSize(this.div_); |
| 2241 | var overlaySize = hterm.getClientSize(this.overlayNode_); |
| 2242 | |
| 2243 | this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2; |
| 2244 | this.overlayNode_.style.left = (divSize.width - overlaySize.width - |
| 2245 | this.scrollPort_.currentScrollbarWidthPx) / 2; |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2246 | |
| 2247 | var self = this; |
| 2248 | |
| 2249 | if (this.overlayTimeout_) |
| 2250 | clearTimeout(this.overlayTimeout_); |
| 2251 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2252 | if (opt_timeout === null) |
| 2253 | return; |
| 2254 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2255 | this.overlayTimeout_ = setTimeout(function() { |
| 2256 | self.overlayNode_.style.opacity = '0'; |
| 2257 | setTimeout(function() { |
rginda | 259dcca | 2012-03-14 16:37:11 -0700 | [diff] [blame] | 2258 | if (self.overlayNode_.parentNode) |
| 2259 | self.overlayNode_.parentNode.removeChild(self.overlayNode_); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2260 | self.overlayTimeout_ = null; |
| 2261 | self.overlayNode_.style.opacity = '0.75'; |
| 2262 | }, 200); |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2263 | }, opt_timeout || 1500); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2264 | }; |
| 2265 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2266 | /** |
| 2267 | * Paste from the system clipboard to the terminal. |
| 2268 | */ |
| 2269 | hterm.Terminal.prototype.paste = function() { |
| 2270 | hterm.pasteFromClipboard(this.document_); |
| 2271 | }; |
| 2272 | |
| 2273 | /** |
| 2274 | * Copy a string to the system clipboard. |
| 2275 | * |
| 2276 | * Note: If there is a selected range in the terminal, it'll be cleared. |
| 2277 | */ |
| 2278 | hterm.Terminal.prototype.copyStringToClipboard = function(str) { |
Robert Ginda | 9fb3822 | 2012-09-11 14:19:12 -0700 | [diff] [blame] | 2279 | if (this.prefs_.get('enable-clipboard-notice')) |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 2280 | setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2281 | |
| 2282 | var copySource = this.document_.createElement('pre'); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2283 | copySource.textContent = str; |
| 2284 | copySource.style.cssText = ( |
| 2285 | '-webkit-user-select: text;' + |
| 2286 | 'position: absolute;' + |
| 2287 | 'top: -99px'); |
| 2288 | |
| 2289 | this.document_.body.appendChild(copySource); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2290 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2291 | var selection = this.document_.getSelection(); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2292 | var anchorNode = selection.anchorNode; |
| 2293 | var anchorOffset = selection.anchorOffset; |
| 2294 | var focusNode = selection.focusNode; |
| 2295 | var focusOffset = selection.focusOffset; |
| 2296 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2297 | selection.selectAllChildren(copySource); |
| 2298 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2299 | hterm.copySelectionToClipboard(this.document_); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2300 | |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2301 | selection.collapse(anchorNode, anchorOffset); |
| 2302 | selection.extend(focusNode, focusOffset); |
| 2303 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2304 | copySource.parentNode.removeChild(copySource); |
| 2305 | }; |
| 2306 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2307 | hterm.Terminal.prototype.getSelectionText = function() { |
| 2308 | var selection = this.scrollPort_.selection; |
| 2309 | selection.sync(); |
| 2310 | |
| 2311 | if (selection.isCollapsed) |
| 2312 | return null; |
| 2313 | |
| 2314 | |
| 2315 | // Start offset measures from the beginning of the line. |
| 2316 | var startOffset = selection.startOffset; |
| 2317 | var node = selection.startNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2318 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2319 | if (node.nodeName != 'X-ROW') { |
| 2320 | // If the selection doesn't start on an x-row node, then it must be |
| 2321 | // somewhere inside the x-row. Add any characters from previous siblings |
| 2322 | // into the start offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2323 | |
| 2324 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 2325 | // If node is the text node in a styled span, move up to the span node. |
| 2326 | node = node.parentNode; |
| 2327 | } |
| 2328 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2329 | while (node.previousSibling) { |
| 2330 | node = node.previousSibling; |
| 2331 | startOffset += node.textContent.length; |
| 2332 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | // End offset measures from the end of the line. |
| 2336 | var endOffset = selection.endNode.textContent.length - selection.endOffset; |
| 2337 | var node = selection.endNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2338 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2339 | if (node.nodeName != 'X-ROW') { |
| 2340 | // If the selection doesn't end on an x-row node, then it must be |
| 2341 | // somewhere inside the x-row. Add any characters from following siblings |
| 2342 | // into the end offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2343 | |
| 2344 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 2345 | // If node is the text node in a styled span, move up to the span node. |
| 2346 | node = node.parentNode; |
| 2347 | } |
| 2348 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2349 | while (node.nextSibling) { |
| 2350 | node = node.nextSibling; |
| 2351 | endOffset += node.textContent.length; |
| 2352 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | var rv = this.getRowsText(selection.startRow.rowIndex, |
| 2356 | selection.endRow.rowIndex + 1); |
| 2357 | return rv.substring(startOffset, rv.length - endOffset); |
| 2358 | }; |
| 2359 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2360 | /** |
| 2361 | * Copy the current selection to the system clipboard, then clear it after a |
| 2362 | * short delay. |
| 2363 | */ |
| 2364 | hterm.Terminal.prototype.copySelectionToClipboard = function() { |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2365 | var text = this.getSelectionText(); |
| 2366 | if (text != null) |
| 2367 | this.copyStringToClipboard(text); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2368 | }; |
| 2369 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2370 | hterm.Terminal.prototype.overlaySize = function() { |
| 2371 | this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height); |
| 2372 | }; |
| 2373 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2374 | /** |
| 2375 | * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected. |
| 2376 | * |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 2377 | * @param {string} string The VT string representing the keystroke, in UTF-16. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2378 | */ |
| 2379 | hterm.Terminal.prototype.onVTKeystroke = function(string) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2380 | if (this.scrollOnKeystroke_) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2381 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 2382 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 2383 | this.io.onVTKeystroke(this.keyboard.encode(string)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2384 | }; |
| 2385 | |
| 2386 | /** |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2387 | * Add the terminalRow and terminalColumn properties to mouse events and |
| 2388 | * then forward on to onMouse(). |
| 2389 | * |
| 2390 | * The terminalRow and terminalColumn properties contain the (row, column) |
| 2391 | * coordinates for the mouse event. |
| 2392 | */ |
| 2393 | hterm.Terminal.prototype.onMouse_ = function(e) { |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2394 | if (e.processedByTerminalHandler_) { |
| 2395 | // We register our event handlers on the document, as well as the cursor |
| 2396 | // and the scroll blocker. Mouse events that occur on the cursor or |
| 2397 | // scroll blocker will also appear on the document, but we don't want to |
| 2398 | // process them twice. |
| 2399 | // |
| 2400 | // We can't just prevent bubbling because that has other side effects, so |
| 2401 | // we decorate the event object with this property instead. |
| 2402 | return; |
| 2403 | } |
| 2404 | |
| 2405 | e.processedByTerminalHandler_ = true; |
| 2406 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2407 | if (e.type == 'mousedown' && e.which == this.mousePasteButton) { |
| 2408 | this.paste(); |
| 2409 | return; |
| 2410 | } |
| 2411 | |
| 2412 | if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect && |
| 2413 | !this.document_.getSelection().isCollapsed) { |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2414 | hterm.copySelectionToClipboard(this.document_); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2415 | return; |
| 2416 | } |
| 2417 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2418 | e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) / |
| 2419 | this.scrollPort_.characterSize.height) + 1; |
| 2420 | e.terminalColumn = parseInt(e.clientX / |
| 2421 | this.scrollPort_.characterSize.width) + 1; |
| 2422 | |
| 2423 | if (e.type == 'mousedown') { |
| 2424 | if (e.terminalColumn > this.screenSize.width) { |
| 2425 | // Mousedown in the scrollbar area. |
| 2426 | return; |
| 2427 | } |
| 2428 | |
| 2429 | if (!this.enableMouseDragScroll) { |
| 2430 | // Move the scroll-blocker into place if we want to keep the scrollport |
| 2431 | // from scrolling. |
| 2432 | this.scrollBlockerNode_.engaged = true; |
| 2433 | this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px'; |
| 2434 | this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px'; |
| 2435 | } |
| 2436 | } else if (this.scrollBlockerNode_.engaged && |
| 2437 | (e.type == 'mousemove' || e.type == 'mouseup')) { |
| 2438 | // Disengage the scroll-blocker after one of these events. |
| 2439 | this.scrollBlockerNode_.engaged = false; |
| 2440 | this.scrollBlockerNode_.style.top = '-99px'; |
| 2441 | } |
| 2442 | |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2443 | this.onMouse(e); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2444 | }; |
| 2445 | |
| 2446 | /** |
| 2447 | * Clients should override this if they care to know about mouse events. |
| 2448 | * |
| 2449 | * The event parameter will be a normal DOM mouse click event with additional |
| 2450 | * 'terminalRow' and 'terminalColumn' properties. |
| 2451 | */ |
| 2452 | hterm.Terminal.prototype.onMouse = function(e) { }; |
| 2453 | |
| 2454 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 2455 | * React when focus changes. |
| 2456 | */ |
| 2457 | hterm.Terminal.prototype.onFocusChange_ = function(state) { |
| 2458 | this.cursorNode_.setAttribute('focus', state ? 'true' : 'false'); |
| 2459 | }; |
| 2460 | |
| 2461 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2462 | * React when the ScrollPort is scrolled. |
| 2463 | */ |
| 2464 | hterm.Terminal.prototype.onScroll_ = function() { |
| 2465 | this.scheduleSyncCursorPosition_(); |
| 2466 | }; |
| 2467 | |
| 2468 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 2469 | * React when text is pasted into the scrollPort. |
| 2470 | */ |
| 2471 | hterm.Terminal.prototype.onPaste_ = function(e) { |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 2472 | this.onVTKeystroke(e.text.replace(/\n/mg, '\r')); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 2473 | }; |
| 2474 | |
| 2475 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2476 | * React when the user tries to copy from the scrollPort. |
| 2477 | */ |
| 2478 | hterm.Terminal.prototype.onCopy_ = function(e) { |
| 2479 | e.preventDefault(); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2480 | this.copySelectionToClipboard(); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2481 | }; |
| 2482 | |
| 2483 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2484 | * React when the ScrollPort is resized. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 2485 | * |
| 2486 | * Note: This function should not directly contain code that alters the internal |
| 2487 | * state of the terminal. That kind of code belongs in realizeWidth or |
| 2488 | * realizeHeight, so that it can be executed synchronously in the case of a |
| 2489 | * programmatic width change. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2490 | */ |
| 2491 | hterm.Terminal.prototype.onResize_ = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 2492 | var columnCount = Math.floor(this.scrollPort_.getScreenWidth() / |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2493 | this.scrollPort_.characterSize.width); |
| 2494 | var rowCount = Math.floor(this.scrollPort_.getScreenHeight() / |
| 2495 | this.scrollPort_.characterSize.height); |
| 2496 | |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 2497 | if (columnCount <= 0 || rowCount <= 0) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2498 | // We avoid these situations since they happen sometimes when the terminal |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 2499 | // gets removed from the document or during the initial load, and we can't |
| 2500 | // deal with that. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2501 | return; |
| 2502 | } |
| 2503 | |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 2504 | var isNewSize = (columnCount != this.screenSize.width || |
| 2505 | rowCount != this.screenSize.height); |
| 2506 | |
| 2507 | // We do this even if the size didn't change, just to be sure everything is |
| 2508 | // in sync. |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 2509 | this.realizeSize_(columnCount, rowCount); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2510 | this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1); |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 2511 | |
| 2512 | if (isNewSize) |
| 2513 | this.overlaySize(); |
| 2514 | |
| 2515 | this.scheduleSyncCursorPosition_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2516 | }; |
| 2517 | |
| 2518 | /** |
| 2519 | * Service the cursor blink timeout. |
| 2520 | */ |
| 2521 | hterm.Terminal.prototype.onCursorBlink_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2522 | if (this.cursorNode_.style.opacity == '0') { |
| 2523 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2524 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2525 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2526 | } |
| 2527 | }; |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 2528 | |
| 2529 | /** |
| 2530 | * Set the scrollbar-visible mode bit. |
| 2531 | * |
| 2532 | * If scrollbar-visible is on, the vertical scrollbar will be visible. |
| 2533 | * Otherwise it will not. |
| 2534 | * |
| 2535 | * Defaults to on. |
| 2536 | * |
| 2537 | * @param {boolean} state True to set scrollbar-visible mode, false to unset. |
| 2538 | */ |
| 2539 | hterm.Terminal.prototype.setScrollbarVisible = function(state) { |
| 2540 | this.scrollPort_.setScrollbarVisible(state); |
| 2541 | }; |