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 | |
Masaya Suzuki | 273aa98 | 2014-05-31 07:25:55 +0900 | [diff] [blame] | 7 | lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc', |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 8 | 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard', |
| 9 | 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen', |
| 10 | 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT'); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 11 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 12 | /** |
| 13 | * Constructor for the Terminal class. |
| 14 | * |
| 15 | * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100 |
| 16 | * classes to provide the complete terminal functionality. |
| 17 | * |
| 18 | * There are a number of lower-level Terminal methods that can be called |
| 19 | * directly to manipulate the cursor, text, scroll region, and other terminal |
| 20 | * attributes. However, the primary method is interpret(), which parses VT |
| 21 | * escape sequences and invokes the appropriate Terminal methods. |
| 22 | * |
| 23 | * This class was heavily influenced by Cory Maccarrone's Framebuffer class. |
| 24 | * |
| 25 | * TODO(rginda): Eventually we're going to need to support characters which are |
| 26 | * displayed twice as wide as standard latin characters. This is to support |
| 27 | * CJK (and possibly other character sets). |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 28 | * |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 29 | * @param {string} opt_profileId Optional preference profile name. If not |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 30 | * provided, defaults to 'default'. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 31 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 32 | hterm.Terminal = function(opt_profileId) { |
| 33 | this.profileId_ = null; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 34 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 35 | // Two screen instances. |
| 36 | this.primaryScreen_ = new hterm.Screen(); |
| 37 | this.alternateScreen_ = new hterm.Screen(); |
| 38 | |
| 39 | // The "current" screen. |
| 40 | this.screen_ = this.primaryScreen_; |
| 41 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 42 | // The local notion of the screen size. ScreenBuffers also have a size which |
| 43 | // indicates their present size. During size changes, the two may disagree. |
| 44 | // Also, the inactive screen's size is not altered until it is made the active |
| 45 | // screen. |
| 46 | this.screenSize = new hterm.Size(0, 0); |
| 47 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 48 | // The scroll port we'll be using to display the visible rows. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 49 | this.scrollPort_ = new hterm.ScrollPort(this); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 50 | this.scrollPort_.subscribe('resize', this.onResize_.bind(this)); |
| 51 | this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this)); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 52 | this.scrollPort_.subscribe('paste', this.onPaste_.bind(this)); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 53 | this.scrollPort_.onCopy = this.onCopy_.bind(this); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 54 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 55 | // The div that contains this terminal. |
| 56 | this.div_ = null; |
| 57 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 58 | // The document that contains the scrollPort. Defaulted to the global |
| 59 | // document here so that the terminal is functional even if it hasn't been |
| 60 | // inserted into a document yet, but re-set in decorate(). |
| 61 | this.document_ = window.document; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 62 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 63 | // The rows that have scrolled off screen and are no longer addressable. |
| 64 | this.scrollbackRows_ = []; |
| 65 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 66 | // Saved tab stops. |
| 67 | this.tabStops_ = []; |
| 68 | |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 69 | // Keep track of whether default tab stops have been erased; after a TBC |
| 70 | // clears all tab stops, defaults aren't restored on resize until a reset. |
| 71 | this.defaultTabStops = true; |
| 72 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 73 | // The VT's notion of the top and bottom rows. Used during some VT |
| 74 | // cursor positioning and scrolling commands. |
| 75 | this.vtScrollTop_ = null; |
| 76 | this.vtScrollBottom_ = null; |
| 77 | |
| 78 | // The DIV element for the visible cursor. |
| 79 | this.cursorNode_ = null; |
| 80 | |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 81 | // The current cursor shape of the terminal. |
| 82 | this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK; |
| 83 | |
| 84 | // The current color of the cursor. |
| 85 | this.cursorColor_ = null; |
| 86 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 87 | // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded. |
| 88 | this.cursorBlinkCycle_ = [100, 100]; |
| 89 | |
| 90 | // Pre-bound onCursorBlink_ handler, so we don't have to do this for each |
| 91 | // cursor on/off servicing. |
| 92 | this.myOnCursorBlink_ = this.onCursorBlink_.bind(this); |
| 93 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 94 | // 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] | 95 | // each output and keystroke. They are initialized by the preference manager. |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 96 | this.backgroundColor_ = null; |
| 97 | this.foregroundColor_ = null; |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 98 | this.scrollOnOutput_ = null; |
| 99 | this.scrollOnKeystroke_ = null; |
Mike Frysinger | 3c9fa07 | 2017-07-13 10:21:13 -0400 | [diff] [blame] | 100 | this.scrollWheelArrowKeys_ = null; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 101 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 102 | // True if we should override mouse event reporting to allow local selection. |
| 103 | this.defeatMouseReports_ = false; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 104 | |
Mike Frysinger | 02ded6d | 2018-06-21 14:25:20 -0400 | [diff] [blame] | 105 | // Whether to auto hide the mouse cursor when typing. |
| 106 | this.setAutomaticMouseHiding(); |
| 107 | // Timer to keep mouse visible while it's being used. |
| 108 | this.mouseHideDelay_ = null; |
| 109 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 110 | // Terminal bell sound. |
| 111 | this.bellAudio_ = this.document_.createElement('audio'); |
Mike Frysinger | d826f1a | 2017-07-06 16:20:06 -0400 | [diff] [blame] | 112 | this.bellAudio_.id = 'hterm:bell-audio'; |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 113 | this.bellAudio_.setAttribute('preload', 'auto'); |
| 114 | |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 115 | // The AccessibilityReader object for announcing command output. |
| 116 | this.accessibilityReader_ = null; |
| 117 | |
| 118 | // Whether command output should be rendered for Assistive Technology. |
| 119 | // This isn't always enabled because it has an impact on performance. |
| 120 | this.accessibilityEnabled_ = false; |
| 121 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 122 | // All terminal bell notifications that have been generated (not necessarily |
| 123 | // shown). |
| 124 | this.bellNotificationList_ = []; |
| 125 | |
| 126 | // Whether we have permission to display notifications. |
| 127 | this.desktopNotificationBell_ = false; |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 128 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 129 | // Cursor position and attributes saved with DECSC. |
| 130 | this.savedOptions_ = {}; |
| 131 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 132 | // The current mode bits for the terminal. |
| 133 | this.options_ = new hterm.Options(); |
| 134 | |
| 135 | // Timeouts we might need to clear. |
| 136 | this.timeouts_ = {}; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 137 | |
| 138 | // The VT escape sequence interpreter. |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 139 | this.vt = new hterm.VT(this); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 140 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 141 | this.saveCursorAndState(true); |
| 142 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 143 | // The keyboard handler. |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 144 | this.keyboard = new hterm.Keyboard(this); |
| 145 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 146 | // General IO interface that can be given to third parties without exposing |
| 147 | // the entire terminal object. |
| 148 | this.io = new hterm.Terminal.IO(this); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 149 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 150 | // True if mouse-click-drag should scroll the terminal. |
| 151 | this.enableMouseDragScroll = true; |
| 152 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 153 | this.copyOnSelect = null; |
Mike Frysinger | 847577f | 2017-05-23 23:25:57 -0400 | [diff] [blame] | 154 | this.mouseRightClickPaste = null; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 155 | this.mousePasteButton = null; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 156 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 157 | // Whether to use the default window copy behavior. |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 158 | this.useDefaultWindowCopy = false; |
| 159 | |
| 160 | this.clearSelectionAfterCopy = true; |
| 161 | |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 162 | this.realizeSize_(80, 24); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 163 | this.setDefaultTabStops(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 164 | |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 165 | // Whether we allow images to be shown. |
| 166 | this.allowImagesInline = null; |
| 167 | |
Gabriel Holodak | e8a09be | 2017-10-10 01:07:11 -0400 | [diff] [blame] | 168 | this.reportFocus = false; |
| 169 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 170 | this.setProfile(opt_profileId || 'default', |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 171 | function() { this.onTerminalReady(); }.bind(this)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /** |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 175 | * Possible cursor shapes. |
| 176 | */ |
| 177 | hterm.Terminal.cursorShape = { |
| 178 | BLOCK: 'BLOCK', |
| 179 | BEAM: 'BEAM', |
| 180 | UNDERLINE: 'UNDERLINE' |
| 181 | }; |
| 182 | |
| 183 | /** |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 184 | * Clients should override this to be notified when the terminal is ready |
| 185 | * for use. |
| 186 | * |
| 187 | * The terminal initialization is asynchronous, and shouldn't be used before |
| 188 | * this method is called. |
| 189 | */ |
| 190 | hterm.Terminal.prototype.onTerminalReady = function() { }; |
| 191 | |
| 192 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 193 | * Default tab with of 8 to match xterm. |
| 194 | */ |
| 195 | hterm.Terminal.prototype.tabWidth = 8; |
| 196 | |
| 197 | /** |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 198 | * Select a preference profile. |
| 199 | * |
| 200 | * This will load the terminal preferences for the given profile name and |
| 201 | * associate subsequent preference changes with the new preference profile. |
| 202 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 203 | * @param {string} profileId The name of the preference profile. Forward slash |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 204 | * characters will be removed from the name. |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 205 | * @param {function} opt_callback Optional callback to invoke when the profile |
| 206 | * transition is complete. |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 207 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 208 | hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) { |
| 209 | this.profileId_ = profileId.replace(/\//g, ''); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 210 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 211 | var terminal = this; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 212 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 213 | if (this.prefs_) |
| 214 | this.prefs_.deactivate(); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 215 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 216 | this.prefs_ = new hterm.PreferenceManager(this.profileId_); |
| 217 | this.prefs_.addObservers(null, { |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 218 | 'alt-gr-mode': function(v) { |
| 219 | if (v == null) { |
| 220 | if (navigator.language.toLowerCase() == 'en-us') { |
| 221 | v = 'none'; |
| 222 | } else { |
| 223 | v = 'right-alt'; |
| 224 | } |
| 225 | } else if (typeof v == 'string') { |
| 226 | v = v.toLowerCase(); |
| 227 | } else { |
| 228 | v = 'none'; |
| 229 | } |
| 230 | |
| 231 | if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) |
| 232 | v = 'none'; |
| 233 | |
| 234 | terminal.keyboard.altGrMode = v; |
| 235 | }, |
| 236 | |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 237 | 'alt-backspace-is-meta-backspace': function(v) { |
| 238 | terminal.keyboard.altBackspaceIsMetaBackspace = v; |
| 239 | }, |
| 240 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 241 | 'alt-is-meta': function(v) { |
| 242 | terminal.keyboard.altIsMeta = v; |
| 243 | }, |
| 244 | |
| 245 | 'alt-sends-what': function(v) { |
| 246 | if (!/^(escape|8-bit|browser-key)$/.test(v)) |
| 247 | v = 'escape'; |
| 248 | |
| 249 | terminal.keyboard.altSendsWhat = v; |
| 250 | }, |
| 251 | |
| 252 | 'audible-bell-sound': function(v) { |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 253 | var ary = v.match(/^lib-resource:(\S+)/); |
| 254 | if (ary) { |
| 255 | terminal.bellAudio_.setAttribute('src', |
| 256 | lib.resource.getDataUrl(ary[1])); |
| 257 | } else { |
| 258 | terminal.bellAudio_.setAttribute('src', v); |
| 259 | } |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 260 | }, |
| 261 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 262 | 'desktop-notification-bell': function(v) { |
| 263 | if (v && Notification) { |
Robert Ginda | 348dc2b | 2014-06-24 14:42:23 -0700 | [diff] [blame] | 264 | terminal.desktopNotificationBell_ = |
Michael Kelly | b806786 | 2014-06-26 12:59:47 -0400 | [diff] [blame] | 265 | Notification.permission === 'granted'; |
| 266 | if (!terminal.desktopNotificationBell_) { |
| 267 | // Note: We don't call Notification.requestPermission here because |
| 268 | // Chrome requires the call be the result of a user action (such as an |
| 269 | // onclick handler), and pref listeners are run asynchronously. |
| 270 | // |
| 271 | // A way of working around this would be to display a dialog in the |
| 272 | // terminal with a "click-to-request-permission" button. |
| 273 | console.warn('desktop-notification-bell is true but we do not have ' + |
| 274 | 'permission to display notifications.'); |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 275 | } |
| 276 | } else { |
| 277 | terminal.desktopNotificationBell_ = false; |
| 278 | } |
| 279 | }, |
| 280 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 281 | 'background-color': function(v) { |
| 282 | terminal.setBackgroundColor(v); |
| 283 | }, |
| 284 | |
| 285 | 'background-image': function(v) { |
| 286 | terminal.scrollPort_.setBackgroundImage(v); |
| 287 | }, |
| 288 | |
| 289 | 'background-size': function(v) { |
| 290 | terminal.scrollPort_.setBackgroundSize(v); |
| 291 | }, |
| 292 | |
| 293 | 'background-position': function(v) { |
| 294 | terminal.scrollPort_.setBackgroundPosition(v); |
| 295 | }, |
| 296 | |
| 297 | 'backspace-sends-backspace': function(v) { |
| 298 | terminal.keyboard.backspaceSendsBackspace = v; |
| 299 | }, |
| 300 | |
Brad Town | 18654b6 | 2015-03-12 00:27:45 -0700 | [diff] [blame] | 301 | 'character-map-overrides': function(v) { |
| 302 | if (!(v == null || v instanceof Object)) { |
| 303 | console.warn('Preference character-map-modifications is not an ' + |
| 304 | 'object: ' + v); |
| 305 | return; |
| 306 | } |
| 307 | |
Mike Frysinger | 095d406 | 2017-06-14 00:29:48 -0700 | [diff] [blame] | 308 | terminal.vt.characterMaps.reset(); |
| 309 | terminal.vt.characterMaps.setOverrides(v); |
Brad Town | 18654b6 | 2015-03-12 00:27:45 -0700 | [diff] [blame] | 310 | }, |
| 311 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 312 | 'cursor-blink': function(v) { |
| 313 | terminal.setCursorBlink(!!v); |
| 314 | }, |
| 315 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 316 | 'cursor-blink-cycle': function(v) { |
| 317 | if (v instanceof Array && |
| 318 | typeof v[0] == 'number' && |
| 319 | typeof v[1] == 'number') { |
| 320 | terminal.cursorBlinkCycle_ = v; |
| 321 | } else if (typeof v == 'number') { |
| 322 | terminal.cursorBlinkCycle_ = [v, v]; |
| 323 | } else { |
| 324 | // Fast blink indicates an error. |
| 325 | terminal.cursorBlinkCycle_ = [100, 100]; |
| 326 | } |
| 327 | }, |
| 328 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 329 | 'cursor-color': function(v) { |
| 330 | terminal.setCursorColor(v); |
| 331 | }, |
| 332 | |
| 333 | 'color-palette-overrides': function(v) { |
| 334 | if (!(v == null || v instanceof Object || v instanceof Array)) { |
| 335 | console.warn('Preference color-palette-overrides is not an array or ' + |
| 336 | 'object: ' + v); |
| 337 | return; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 338 | } |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 339 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 340 | lib.colors.colorPalette = lib.colors.stockColorPalette.concat(); |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 341 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 342 | if (v) { |
| 343 | for (var key in v) { |
| 344 | var i = parseInt(key); |
| 345 | if (isNaN(i) || i < 0 || i > 255) { |
| 346 | console.log('Invalid value in palette: ' + key + ': ' + v[key]); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | if (v[i]) { |
| 351 | var rgb = lib.colors.normalizeCSS(v[i]); |
| 352 | if (rgb) |
| 353 | lib.colors.colorPalette[i] = rgb; |
| 354 | } |
| 355 | } |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 356 | } |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 357 | |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 358 | terminal.primaryScreen_.textAttributes.resetColorPalette(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 359 | terminal.alternateScreen_.textAttributes.resetColorPalette(); |
| 360 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 361 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 362 | 'copy-on-select': function(v) { |
| 363 | terminal.copyOnSelect = !!v; |
| 364 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 365 | |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 366 | 'use-default-window-copy': function(v) { |
| 367 | terminal.useDefaultWindowCopy = !!v; |
| 368 | }, |
| 369 | |
| 370 | 'clear-selection-after-copy': function(v) { |
| 371 | terminal.clearSelectionAfterCopy = !!v; |
| 372 | }, |
| 373 | |
Robert Ginda | 7e5e952 | 2014-03-14 12:23:58 -0700 | [diff] [blame] | 374 | 'ctrl-plus-minus-zero-zoom': function(v) { |
| 375 | terminal.keyboard.ctrlPlusMinusZeroZoom = v; |
| 376 | }, |
| 377 | |
Robert Ginda | fb5a3f9 | 2014-05-13 14:12:00 -0700 | [diff] [blame] | 378 | 'ctrl-c-copy': function(v) { |
| 379 | terminal.keyboard.ctrlCCopy = v; |
| 380 | }, |
| 381 | |
Leonardo Mesquita | 61e7c31 | 2014-01-04 12:53:12 +0100 | [diff] [blame] | 382 | 'ctrl-v-paste': function(v) { |
| 383 | terminal.keyboard.ctrlVPaste = v; |
Rob Spies | e52e184 | 2014-07-10 15:32:51 -0700 | [diff] [blame] | 384 | terminal.scrollPort_.setCtrlVPaste(v); |
Leonardo Mesquita | 61e7c31 | 2014-01-04 12:53:12 +0100 | [diff] [blame] | 385 | }, |
| 386 | |
Masaya Suzuki | 273aa98 | 2014-05-31 07:25:55 +0900 | [diff] [blame] | 387 | 'east-asian-ambiguous-as-two-column': function(v) { |
| 388 | lib.wc.regardCjkAmbiguous = v; |
| 389 | }, |
| 390 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 391 | 'enable-8-bit-control': function(v) { |
| 392 | terminal.vt.enable8BitControl = !!v; |
| 393 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 394 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 395 | 'enable-bold': function(v) { |
| 396 | terminal.syncBoldSafeState(); |
| 397 | }, |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 398 | |
Robert Ginda | 3e278d7 | 2014-03-25 13:18:51 -0700 | [diff] [blame] | 399 | 'enable-bold-as-bright': function(v) { |
| 400 | terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v; |
| 401 | terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v; |
| 402 | }, |
| 403 | |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 404 | 'enable-blink': function(v) { |
Mike Frysinger | 261597c | 2017-12-28 01:14:21 -0500 | [diff] [blame] | 405 | terminal.setTextBlink(!!v); |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 406 | }, |
| 407 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 408 | 'enable-clipboard-write': function(v) { |
| 409 | terminal.vt.enableClipboardWrite = !!v; |
| 410 | }, |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 411 | |
Robert Ginda | 3755e75 | 2013-05-31 13:34:09 -0700 | [diff] [blame] | 412 | 'enable-dec12': function(v) { |
| 413 | terminal.vt.enableDec12 = !!v; |
| 414 | }, |
| 415 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 416 | 'font-family': function(v) { |
| 417 | terminal.syncFontFamily(); |
| 418 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 419 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 420 | 'font-size': function(v) { |
Mike Frysinger | 47853ac | 2017-12-14 00:44:10 -0500 | [diff] [blame] | 421 | v = parseInt(v); |
| 422 | if (v <= 0) { |
| 423 | console.error(`Invalid font size: ${v}`); |
| 424 | return; |
| 425 | } |
| 426 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 427 | terminal.setFontSize(v); |
| 428 | }, |
rginda | 9875d90 | 2012-08-20 16:21:57 -0700 | [diff] [blame] | 429 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 430 | 'font-smoothing': function(v) { |
| 431 | terminal.syncFontFamily(); |
| 432 | }, |
rginda | de84e38 | 2012-04-20 15:39:31 -0700 | [diff] [blame] | 433 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 434 | 'foreground-color': function(v) { |
| 435 | terminal.setForegroundColor(v); |
| 436 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 437 | |
Mike Frysinger | 02ded6d | 2018-06-21 14:25:20 -0400 | [diff] [blame] | 438 | 'hide-mouse-while-typing': function(v) { |
| 439 | terminal.setAutomaticMouseHiding(v); |
| 440 | }, |
| 441 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 442 | 'home-keys-scroll': function(v) { |
| 443 | terminal.keyboard.homeKeysScroll = v; |
| 444 | }, |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 445 | |
Robert Ginda | a816569 | 2015-06-15 14:46:31 -0700 | [diff] [blame] | 446 | 'keybindings': function(v) { |
| 447 | terminal.keyboard.bindings.clear(); |
| 448 | |
| 449 | if (!v) |
| 450 | return; |
| 451 | |
| 452 | if (!(v instanceof Object)) { |
| 453 | console.error('Error in keybindings preference: Expected object'); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | try { |
| 458 | terminal.keyboard.bindings.addBindings(v); |
| 459 | } catch (ex) { |
| 460 | console.error('Error in keybindings preference: ' + ex); |
| 461 | } |
| 462 | }, |
| 463 | |
Andrew de los Reyes | 6af23ae | 2013-04-04 14:17:50 -0700 | [diff] [blame] | 464 | 'media-keys-are-fkeys': function(v) { |
| 465 | terminal.keyboard.mediaKeysAreFKeys = v; |
| 466 | }, |
| 467 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 468 | 'meta-sends-escape': function(v) { |
| 469 | terminal.keyboard.metaSendsEscape = v; |
| 470 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 471 | |
Mike Frysinger | 847577f | 2017-05-23 23:25:57 -0400 | [diff] [blame] | 472 | 'mouse-right-click-paste': function(v) { |
| 473 | terminal.mouseRightClickPaste = v; |
| 474 | }, |
| 475 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 476 | 'mouse-paste-button': function(v) { |
| 477 | terminal.syncMousePasteButton(); |
| 478 | }, |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 479 | |
Robert Ginda | e76aa9f | 2014-03-14 12:29:12 -0700 | [diff] [blame] | 480 | 'page-keys-scroll': function(v) { |
| 481 | terminal.keyboard.pageKeysScroll = v; |
| 482 | }, |
| 483 | |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 484 | 'pass-alt-number': function(v) { |
| 485 | if (v == null) { |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 486 | // Let Alt-1..9 pass to the browser (to control tab switching) on |
| 487 | // non-OS X systems, or if hterm is not opened in an app window. |
Mike Frysinger | ee81a00 | 2017-12-12 16:14:53 -0500 | [diff] [blame] | 488 | v = (hterm.os != 'mac' && hterm.windowType != 'popup'); |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | terminal.passAltNumber = v; |
| 492 | }, |
| 493 | |
| 494 | 'pass-ctrl-number': function(v) { |
| 495 | if (v == null) { |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 496 | // Let Ctrl-1..9 pass to the browser (to control tab switching) on |
| 497 | // non-OS X systems, or if hterm is not opened in an app window. |
Mike Frysinger | ee81a00 | 2017-12-12 16:14:53 -0500 | [diff] [blame] | 498 | v = (hterm.os != 'mac' && hterm.windowType != 'popup'); |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | terminal.passCtrlNumber = v; |
| 502 | }, |
| 503 | |
| 504 | 'pass-meta-number': function(v) { |
| 505 | if (v == null) { |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 506 | // Let Meta-1..9 pass to the browser (to control tab switching) on |
| 507 | // OS X systems, or if hterm is not opened in an app window. |
Mike Frysinger | ee81a00 | 2017-12-12 16:14:53 -0500 | [diff] [blame] | 508 | v = (hterm.os == 'mac' && hterm.windowType != 'popup'); |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | terminal.passMetaNumber = v; |
| 512 | }, |
| 513 | |
Marius Schilder | 77857b3 | 2014-05-14 16:21:26 -0700 | [diff] [blame] | 514 | 'pass-meta-v': function(v) { |
Marius Schilder | 1a56781 | 2014-05-15 20:30:02 -0700 | [diff] [blame] | 515 | terminal.keyboard.passMetaV = v; |
Marius Schilder | 77857b3 | 2014-05-14 16:21:26 -0700 | [diff] [blame] | 516 | }, |
| 517 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 518 | 'receive-encoding': function(v) { |
| 519 | if (!(/^(utf-8|raw)$/).test(v)) { |
| 520 | console.warn('Invalid value for "receive-encoding": ' + v); |
| 521 | v = 'utf-8'; |
| 522 | } |
| 523 | |
| 524 | terminal.vt.characterEncoding = v; |
| 525 | }, |
| 526 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 527 | 'scroll-on-keystroke': function(v) { |
| 528 | terminal.scrollOnKeystroke_ = v; |
| 529 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 530 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 531 | 'scroll-on-output': function(v) { |
| 532 | terminal.scrollOnOutput_ = v; |
| 533 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 534 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 535 | 'scrollbar-visible': function(v) { |
| 536 | terminal.setScrollbarVisible(v); |
| 537 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 538 | |
Mike Frysinger | 3c9fa07 | 2017-07-13 10:21:13 -0400 | [diff] [blame] | 539 | 'scroll-wheel-may-send-arrow-keys': function(v) { |
| 540 | terminal.scrollWheelArrowKeys_ = v; |
| 541 | }, |
| 542 | |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 543 | 'scroll-wheel-move-multiplier': function(v) { |
| 544 | terminal.setScrollWheelMoveMultipler(v); |
| 545 | }, |
| 546 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 547 | 'send-encoding': function(v) { |
| 548 | if (!(/^(utf-8|raw)$/).test(v)) { |
| 549 | console.warn('Invalid value for "send-encoding": ' + v); |
| 550 | v = 'utf-8'; |
| 551 | } |
| 552 | |
| 553 | terminal.keyboard.characterEncoding = v; |
| 554 | }, |
| 555 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 556 | 'shift-insert-paste': function(v) { |
| 557 | terminal.keyboard.shiftInsertPaste = v; |
| 558 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 559 | |
Mike Frysinger | a776892 | 2017-07-28 15:00:12 -0400 | [diff] [blame] | 560 | 'terminal-encoding': function(v) { |
Mike Frysinger | a1371e1 | 2017-08-17 01:37:17 -0400 | [diff] [blame] | 561 | terminal.vt.setEncoding(v); |
Mike Frysinger | a776892 | 2017-07-28 15:00:12 -0400 | [diff] [blame] | 562 | }, |
| 563 | |
Robert Ginda | e76aa9f | 2014-03-14 12:29:12 -0700 | [diff] [blame] | 564 | 'user-css': function(v) { |
Mike Frysinger | 08bad43 | 2017-04-24 00:50:54 -0400 | [diff] [blame] | 565 | terminal.scrollPort_.setUserCssUrl(v); |
| 566 | }, |
| 567 | |
| 568 | 'user-css-text': function(v) { |
| 569 | terminal.scrollPort_.setUserCssText(v); |
| 570 | }, |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 571 | |
| 572 | 'word-break-match-left': function(v) { |
| 573 | terminal.primaryScreen_.wordBreakMatchLeft = v; |
| 574 | terminal.alternateScreen_.wordBreakMatchLeft = v; |
| 575 | }, |
| 576 | |
| 577 | 'word-break-match-right': function(v) { |
| 578 | terminal.primaryScreen_.wordBreakMatchRight = v; |
| 579 | terminal.alternateScreen_.wordBreakMatchRight = v; |
| 580 | }, |
| 581 | |
| 582 | 'word-break-match-middle': function(v) { |
| 583 | terminal.primaryScreen_.wordBreakMatchMiddle = v; |
| 584 | terminal.alternateScreen_.wordBreakMatchMiddle = v; |
| 585 | }, |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 586 | |
| 587 | 'allow-images-inline': function(v) { |
| 588 | terminal.allowImagesInline = v; |
| 589 | }, |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 590 | }); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 591 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 592 | this.prefs_.readStorage(function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 593 | this.prefs_.notifyAll(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 594 | |
| 595 | if (opt_callback) |
| 596 | opt_callback(); |
| 597 | }.bind(this)); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 598 | }; |
| 599 | |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * Returns the preferences manager used for configuring this terminal. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 603 | * |
| 604 | * @return {hterm.PreferenceManager} |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 605 | */ |
| 606 | hterm.Terminal.prototype.getPrefs = function() { |
| 607 | return this.prefs_; |
| 608 | }; |
| 609 | |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 610 | /** |
| 611 | * Enable or disable bracketed paste mode. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 612 | * |
| 613 | * @param {boolean} state The value to set. |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 614 | */ |
| 615 | hterm.Terminal.prototype.setBracketedPaste = function(state) { |
| 616 | this.options_.bracketedPaste = state; |
| 617 | }; |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 618 | |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 619 | /** |
| 620 | * Set the color for the cursor. |
| 621 | * |
| 622 | * If you want this setting to persist, set it through prefs_, rather than |
| 623 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 624 | * |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 625 | * @param {string=} color The color to set. If not defined, we reset to the |
| 626 | * saved user preference. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 627 | */ |
| 628 | hterm.Terminal.prototype.setCursorColor = function(color) { |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 629 | if (color === undefined) |
| 630 | color = this.prefs_.get('cursor-color'); |
| 631 | |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 632 | this.cursorColor_ = color; |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 633 | this.cursorNode_.style.backgroundColor = color; |
| 634 | this.cursorNode_.style.borderColor = color; |
| 635 | }; |
| 636 | |
| 637 | /** |
| 638 | * Return the current cursor color as a string. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 639 | * @return {string} |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 640 | */ |
| 641 | hterm.Terminal.prototype.getCursorColor = function() { |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 642 | return this.cursorColor_; |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 643 | }; |
| 644 | |
| 645 | /** |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 646 | * Enable or disable mouse based text selection in the terminal. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 647 | * |
| 648 | * @param {boolean} state The value to set. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 649 | */ |
| 650 | hterm.Terminal.prototype.setSelectionEnabled = function(state) { |
| 651 | this.enableMouseDragScroll = state; |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 655 | * Set the background color. |
| 656 | * |
| 657 | * If you want this setting to persist, set it through prefs_, rather than |
| 658 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 659 | * |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 660 | * @param {string=} color The color to set. If not defined, we reset to the |
| 661 | * saved user preference. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 662 | */ |
| 663 | hterm.Terminal.prototype.setBackgroundColor = function(color) { |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 664 | if (color === undefined) |
| 665 | color = this.prefs_.get('background-color'); |
| 666 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 667 | this.backgroundColor_ = lib.colors.normalizeCSS(color); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 668 | this.primaryScreen_.textAttributes.setDefaults( |
| 669 | this.foregroundColor_, this.backgroundColor_); |
| 670 | this.alternateScreen_.textAttributes.setDefaults( |
| 671 | this.foregroundColor_, this.backgroundColor_); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 672 | this.scrollPort_.setBackgroundColor(color); |
| 673 | }; |
| 674 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 675 | /** |
| 676 | * Return the current terminal background color. |
| 677 | * |
| 678 | * Intended for use by other classes, so we don't have to expose the entire |
| 679 | * prefs_ object. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 680 | * |
| 681 | * @return {string} |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 682 | */ |
| 683 | hterm.Terminal.prototype.getBackgroundColor = function() { |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 684 | return this.backgroundColor_; |
| 685 | }; |
| 686 | |
| 687 | /** |
| 688 | * Set the foreground color. |
| 689 | * |
| 690 | * If you want this setting to persist, set it through prefs_, rather than |
| 691 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 692 | * |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 693 | * @param {string=} color The color to set. If not defined, we reset to the |
| 694 | * saved user preference. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 695 | */ |
| 696 | hterm.Terminal.prototype.setForegroundColor = function(color) { |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 697 | if (color === undefined) |
| 698 | color = this.prefs_.get('foreground-color'); |
| 699 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 700 | this.foregroundColor_ = lib.colors.normalizeCSS(color); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 701 | this.primaryScreen_.textAttributes.setDefaults( |
| 702 | this.foregroundColor_, this.backgroundColor_); |
| 703 | this.alternateScreen_.textAttributes.setDefaults( |
| 704 | this.foregroundColor_, this.backgroundColor_); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 705 | this.scrollPort_.setForegroundColor(color); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | /** |
| 709 | * Return the current terminal foreground color. |
| 710 | * |
| 711 | * Intended for use by other classes, so we don't have to expose the entire |
| 712 | * prefs_ object. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 713 | * |
| 714 | * @return {string} |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 715 | */ |
| 716 | hterm.Terminal.prototype.getForegroundColor = function() { |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 717 | return this.foregroundColor_; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 721 | * Create a new instance of a terminal command and run it with a given |
| 722 | * argument string. |
| 723 | * |
| 724 | * @param {function} commandClass The constructor for a terminal command. |
| 725 | * @param {string} argString The argument string to pass to the command. |
| 726 | */ |
| 727 | hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) { |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 728 | var environment = this.prefs_.get('environment'); |
| 729 | if (typeof environment != 'object' || environment == null) |
| 730 | environment = {}; |
| 731 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 732 | var self = this; |
| 733 | this.command = new commandClass( |
| 734 | { argString: argString || '', |
| 735 | io: this.io.push(), |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 736 | environment: environment, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 737 | onExit: function(code) { |
| 738 | self.io.pop(); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 739 | self.uninstallKeyboard(); |
rginda | 9875d90 | 2012-08-20 16:21:57 -0700 | [diff] [blame] | 740 | if (self.prefs_.get('close-on-exit')) |
| 741 | window.close(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 742 | } |
| 743 | }); |
| 744 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 745 | this.installKeyboard(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 746 | this.command.run(); |
| 747 | }; |
| 748 | |
| 749 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 750 | * Returns true if the current screen is the primary screen, false otherwise. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 751 | * |
| 752 | * @return {boolean} |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 753 | */ |
| 754 | hterm.Terminal.prototype.isPrimaryScreen = function() { |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 755 | return this.screen_ == this.primaryScreen_; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 756 | }; |
| 757 | |
| 758 | /** |
| 759 | * Install the keyboard handler for this terminal. |
| 760 | * |
| 761 | * This will prevent the browser from seeing any keystrokes sent to the |
| 762 | * terminal. |
| 763 | */ |
| 764 | hterm.Terminal.prototype.installKeyboard = function() { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 765 | this.keyboard.installKeyboard(this.scrollPort_.getDocument().body); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 766 | }; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 767 | |
| 768 | /** |
| 769 | * Uninstall the keyboard handler for this terminal. |
| 770 | */ |
| 771 | hterm.Terminal.prototype.uninstallKeyboard = function() { |
| 772 | this.keyboard.installKeyboard(null); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 773 | }; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 774 | |
| 775 | /** |
Mike Frysinger | cce97c4 | 2017-08-05 01:11:22 -0400 | [diff] [blame] | 776 | * Set a CSS variable. |
| 777 | * |
| 778 | * Normally this is used to set variables in the hterm namespace. |
| 779 | * |
| 780 | * @param {string} name The variable to set. |
| 781 | * @param {string} value The value to assign to the variable. |
| 782 | * @param {string?} opt_prefix The variable namespace/prefix to use. |
| 783 | */ |
| 784 | hterm.Terminal.prototype.setCssVar = function(name, value, |
| 785 | opt_prefix='--hterm-') { |
| 786 | this.document_.documentElement.style.setProperty( |
| 787 | `${opt_prefix}${name}`, value); |
| 788 | }; |
| 789 | |
| 790 | /** |
Mike Frysinger | 261597c | 2017-12-28 01:14:21 -0500 | [diff] [blame] | 791 | * Get a CSS variable. |
| 792 | * |
| 793 | * Normally this is used to get variables in the hterm namespace. |
| 794 | * |
| 795 | * @param {string} name The variable to read. |
| 796 | * @param {string?} opt_prefix The variable namespace/prefix to use. |
| 797 | * @return {string} The current setting for this variable. |
| 798 | */ |
| 799 | hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') { |
| 800 | return this.document_.documentElement.style.getPropertyValue( |
| 801 | `${opt_prefix}${name}`); |
| 802 | }; |
| 803 | |
| 804 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 805 | * Set the font size for this terminal. |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 806 | * |
| 807 | * Call setFontSize(0) to reset to the default font size. |
| 808 | * |
| 809 | * This function does not modify the font-size preference. |
| 810 | * |
| 811 | * @param {number} px The desired font size, in pixels. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 812 | */ |
| 813 | hterm.Terminal.prototype.setFontSize = function(px) { |
Mike Frysinger | 47853ac | 2017-12-14 00:44:10 -0500 | [diff] [blame] | 814 | if (px <= 0) |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 815 | px = this.prefs_.get('font-size'); |
| 816 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 817 | this.scrollPort_.setFontSize(px); |
Mike Frysinger | cce97c4 | 2017-08-05 01:11:22 -0400 | [diff] [blame] | 818 | this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px'); |
| 819 | this.setCssVar('charsize-height', |
| 820 | this.scrollPort_.characterSize.height + 'px'); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 821 | }; |
| 822 | |
| 823 | /** |
| 824 | * Get the current font size. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 825 | * |
| 826 | * @return {number} |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 827 | */ |
| 828 | hterm.Terminal.prototype.getFontSize = function() { |
| 829 | return this.scrollPort_.getFontSize(); |
| 830 | }; |
| 831 | |
| 832 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 833 | * Get the current font family. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 834 | * |
| 835 | * @return {string} |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 836 | */ |
| 837 | hterm.Terminal.prototype.getFontFamily = function() { |
| 838 | return this.scrollPort_.getFontFamily(); |
| 839 | }; |
| 840 | |
| 841 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 842 | * Set the CSS "font-family" for this terminal. |
| 843 | */ |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 844 | hterm.Terminal.prototype.syncFontFamily = function() { |
| 845 | this.scrollPort_.setFontFamily(this.prefs_.get('font-family'), |
| 846 | this.prefs_.get('font-smoothing')); |
| 847 | this.syncBoldSafeState(); |
| 848 | }; |
| 849 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 850 | /** |
| 851 | * Set this.mousePasteButton based on the mouse-paste-button pref, |
| 852 | * autodetecting if necessary. |
| 853 | */ |
| 854 | hterm.Terminal.prototype.syncMousePasteButton = function() { |
| 855 | var button = this.prefs_.get('mouse-paste-button'); |
| 856 | if (typeof button == 'number') { |
| 857 | this.mousePasteButton = button; |
| 858 | return; |
| 859 | } |
| 860 | |
Mike Frysinger | ee81a00 | 2017-12-12 16:14:53 -0500 | [diff] [blame] | 861 | if (hterm.os != 'linux') { |
Mike Frysinger | 2edd361 | 2017-05-24 00:54:39 -0400 | [diff] [blame] | 862 | this.mousePasteButton = 1; // Middle mouse button. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 863 | } else { |
Mike Frysinger | 2edd361 | 2017-05-24 00:54:39 -0400 | [diff] [blame] | 864 | this.mousePasteButton = 2; // Right mouse button. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 865 | } |
| 866 | }; |
| 867 | |
| 868 | /** |
| 869 | * Enable or disable bold based on the enable-bold pref, autodetecting if |
| 870 | * necessary. |
| 871 | */ |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 872 | hterm.Terminal.prototype.syncBoldSafeState = function() { |
| 873 | var enableBold = this.prefs_.get('enable-bold'); |
| 874 | if (enableBold !== null) { |
Robert Ginda | ed01626 | 2012-10-26 16:27:09 -0700 | [diff] [blame] | 875 | this.primaryScreen_.textAttributes.enableBold = enableBold; |
| 876 | this.alternateScreen_.textAttributes.enableBold = enableBold; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 880 | var normalSize = this.scrollPort_.measureCharacterSize(); |
| 881 | var boldSize = this.scrollPort_.measureCharacterSize('bold'); |
| 882 | |
| 883 | var isBoldSafe = normalSize.equals(boldSize); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 884 | if (!isBoldSafe) { |
| 885 | console.warn('Bold characters disabled: Size of bold weight differs ' + |
rginda | c9759de | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 886 | 'from normal. Font family is: ' + |
| 887 | this.scrollPort_.getFontFamily()); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 888 | } |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 889 | |
Robert Ginda | ed01626 | 2012-10-26 16:27:09 -0700 | [diff] [blame] | 890 | this.primaryScreen_.textAttributes.enableBold = isBoldSafe; |
| 891 | this.alternateScreen_.textAttributes.enableBold = isBoldSafe; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 892 | }; |
| 893 | |
| 894 | /** |
Mike Frysinger | 261597c | 2017-12-28 01:14:21 -0500 | [diff] [blame] | 895 | * Control text blinking behavior. |
| 896 | * |
| 897 | * @param {boolean=} state Whether to enable support for blinking text. |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 898 | */ |
Mike Frysinger | 261597c | 2017-12-28 01:14:21 -0500 | [diff] [blame] | 899 | hterm.Terminal.prototype.setTextBlink = function(state) { |
| 900 | if (state === undefined) |
| 901 | state = this.prefs_.get('enable-blink'); |
| 902 | this.setCssVar('blink-node-duration', state ? '0.7s' : '0'); |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | /** |
Mike Frysinger | 6ab275c | 2017-05-28 12:48:44 -0400 | [diff] [blame] | 906 | * Set the mouse cursor style based on the current terminal mode. |
| 907 | */ |
| 908 | hterm.Terminal.prototype.syncMouseStyle = function() { |
Mike Frysinger | cce97c4 | 2017-08-05 01:11:22 -0400 | [diff] [blame] | 909 | this.setCssVar('mouse-cursor-style', |
| 910 | this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ? |
| 911 | 'var(--hterm-mouse-cursor-text)' : |
| 912 | 'var(--hterm-mouse-cursor-pointer)'); |
Mike Frysinger | 6ab275c | 2017-05-28 12:48:44 -0400 | [diff] [blame] | 913 | }; |
| 914 | |
| 915 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 916 | * Return a copy of the current cursor position. |
| 917 | * |
| 918 | * @return {hterm.RowCol} The RowCol object representing the current position. |
| 919 | */ |
| 920 | hterm.Terminal.prototype.saveCursor = function() { |
| 921 | return this.screen_.cursorPosition.clone(); |
| 922 | }; |
| 923 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 924 | /** |
| 925 | * Return the current text attributes. |
| 926 | * |
| 927 | * @return {string} |
| 928 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 929 | hterm.Terminal.prototype.getTextAttributes = function() { |
| 930 | return this.screen_.textAttributes; |
| 931 | }; |
| 932 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 933 | /** |
| 934 | * Set the text attributes. |
| 935 | * |
| 936 | * @param {string} textAttributes The attributes to set. |
| 937 | */ |
rginda | 1a09aa0 | 2012-06-18 21:11:25 -0700 | [diff] [blame] | 938 | hterm.Terminal.prototype.setTextAttributes = function(textAttributes) { |
| 939 | this.screen_.textAttributes = textAttributes; |
| 940 | }; |
| 941 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 942 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 943 | * Return the current browser zoom factor applied to the terminal. |
| 944 | * |
| 945 | * @return {number} The current browser zoom factor. |
| 946 | */ |
| 947 | hterm.Terminal.prototype.getZoomFactor = function() { |
| 948 | return this.scrollPort_.characterSize.zoomFactor; |
| 949 | }; |
| 950 | |
| 951 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 952 | * Change the title of this terminal's window. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 953 | * |
| 954 | * @param {string} title The title to set. |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 955 | */ |
| 956 | hterm.Terminal.prototype.setWindowTitle = function(title) { |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 957 | window.document.title = title; |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 958 | }; |
| 959 | |
| 960 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 961 | * Restore a previously saved cursor position. |
| 962 | * |
| 963 | * @param {hterm.RowCol} cursor The position to restore. |
| 964 | */ |
| 965 | hterm.Terminal.prototype.restoreCursor = function(cursor) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 966 | var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1); |
| 967 | var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 968 | this.screen_.setCursorPosition(row, column); |
| 969 | if (cursor.column > column || |
| 970 | cursor.column == column && cursor.overflow) { |
| 971 | this.screen_.cursorPosition.overflow = true; |
| 972 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 973 | }; |
| 974 | |
| 975 | /** |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 976 | * Clear the cursor's overflow flag. |
| 977 | */ |
| 978 | hterm.Terminal.prototype.clearCursorOverflow = function() { |
| 979 | this.screen_.cursorPosition.overflow = false; |
| 980 | }; |
| 981 | |
| 982 | /** |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 983 | * Save the current cursor state to the corresponding screens. |
| 984 | * |
| 985 | * See the hterm.Screen.CursorState class for more details. |
| 986 | * |
| 987 | * @param {boolean=} both If true, update both screens, else only update the |
| 988 | * current screen. |
| 989 | */ |
| 990 | hterm.Terminal.prototype.saveCursorAndState = function(both) { |
| 991 | if (both) { |
| 992 | this.primaryScreen_.saveCursorAndState(this.vt); |
| 993 | this.alternateScreen_.saveCursorAndState(this.vt); |
| 994 | } else |
| 995 | this.screen_.saveCursorAndState(this.vt); |
| 996 | }; |
| 997 | |
| 998 | /** |
| 999 | * Restore the saved cursor state in the corresponding screens. |
| 1000 | * |
| 1001 | * See the hterm.Screen.CursorState class for more details. |
| 1002 | * |
| 1003 | * @param {boolean=} both If true, update both screens, else only update the |
| 1004 | * current screen. |
| 1005 | */ |
| 1006 | hterm.Terminal.prototype.restoreCursorAndState = function(both) { |
| 1007 | if (both) { |
| 1008 | this.primaryScreen_.restoreCursorAndState(this.vt); |
| 1009 | this.alternateScreen_.restoreCursorAndState(this.vt); |
| 1010 | } else |
| 1011 | this.screen_.restoreCursorAndState(this.vt); |
| 1012 | }; |
| 1013 | |
| 1014 | /** |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 1015 | * Sets the cursor shape |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1016 | * |
| 1017 | * @param {string} shape The shape to set. |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 1018 | */ |
| 1019 | hterm.Terminal.prototype.setCursorShape = function(shape) { |
| 1020 | this.cursorShape_ = shape; |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 1021 | this.restyleCursor_(); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 1022 | }; |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 1023 | |
| 1024 | /** |
| 1025 | * Get the cursor shape |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1026 | * |
| 1027 | * @return {string} |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 1028 | */ |
| 1029 | hterm.Terminal.prototype.getCursorShape = function() { |
| 1030 | return this.cursorShape_; |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 1031 | }; |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 1032 | |
| 1033 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1034 | * Set the width of the terminal, resizing the UI to match. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1035 | * |
| 1036 | * @param {number} columnCount |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1037 | */ |
| 1038 | hterm.Terminal.prototype.setWidth = function(columnCount) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1039 | if (columnCount == null) { |
| 1040 | this.div_.style.width = '100%'; |
| 1041 | return; |
| 1042 | } |
| 1043 | |
Robert Ginda | 26806d1 | 2014-07-24 13:44:07 -0700 | [diff] [blame] | 1044 | this.div_.style.width = Math.ceil( |
| 1045 | this.scrollPort_.characterSize.width * |
| 1046 | columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px'; |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 1047 | this.realizeSize_(columnCount, this.screenSize.height); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1048 | this.scheduleSyncCursorPosition_(); |
| 1049 | }; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1050 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1051 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1052 | * Set the height of the terminal, resizing the UI to match. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1053 | * |
| 1054 | * @param {number} rowCount The height in rows. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1055 | */ |
| 1056 | hterm.Terminal.prototype.setHeight = function(rowCount) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 1057 | if (rowCount == null) { |
| 1058 | this.div_.style.height = '100%'; |
| 1059 | return; |
| 1060 | } |
| 1061 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1062 | this.div_.style.height = |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 1063 | this.scrollPort_.characterSize.height * rowCount + 'px'; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1064 | this.realizeSize_(this.screenSize.width, rowCount); |
| 1065 | this.scheduleSyncCursorPosition_(); |
| 1066 | }; |
| 1067 | |
| 1068 | /** |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 1069 | * Deal with terminal size changes. |
| 1070 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1071 | * @param {number} columnCount The number of columns. |
| 1072 | * @param {number} rowCount The number of rows. |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 1073 | */ |
| 1074 | hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) { |
| 1075 | if (columnCount != this.screenSize.width) |
| 1076 | this.realizeWidth_(columnCount); |
| 1077 | |
| 1078 | if (rowCount != this.screenSize.height) |
| 1079 | this.realizeHeight_(rowCount); |
| 1080 | |
| 1081 | // Send new terminal size to plugin. |
Robert Ginda | e81427f | 2013-05-24 10:34:46 -0700 | [diff] [blame] | 1082 | this.io.onTerminalResize_(columnCount, rowCount); |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 1083 | }; |
| 1084 | |
| 1085 | /** |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1086 | * Deal with terminal width changes. |
| 1087 | * |
| 1088 | * This function does what needs to be done when the terminal width changes |
| 1089 | * out from under us. It happens here rather than in onResize_() because this |
| 1090 | * code may need to run synchronously to handle programmatic changes of |
| 1091 | * terminal width. |
| 1092 | * |
| 1093 | * Relying on the browser to send us an async resize event means we may not be |
| 1094 | * in the correct state yet when the next escape sequence hits. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1095 | * |
| 1096 | * @param {number} columnCount The number of columns. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1097 | */ |
| 1098 | hterm.Terminal.prototype.realizeWidth_ = function(columnCount) { |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 1099 | if (columnCount <= 0) |
| 1100 | throw new Error('Attempt to realize bad width: ' + columnCount); |
| 1101 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1102 | var deltaColumns = columnCount - this.screen_.getWidth(); |
| 1103 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1104 | this.screenSize.width = columnCount; |
| 1105 | this.screen_.setColumnCount(columnCount); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1106 | |
| 1107 | if (deltaColumns > 0) { |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1108 | if (this.defaultTabStops) |
| 1109 | this.setDefaultTabStops(this.screenSize.width - deltaColumns); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1110 | } else { |
| 1111 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1112 | if (this.tabStops_[i] < columnCount) |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1113 | break; |
| 1114 | |
| 1115 | this.tabStops_.pop(); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | this.screen_.setColumnCount(this.screenSize.width); |
| 1120 | }; |
| 1121 | |
| 1122 | /** |
| 1123 | * Deal with terminal height changes. |
| 1124 | * |
| 1125 | * This function does what needs to be done when the terminal height changes |
| 1126 | * out from under us. It happens here rather than in onResize_() because this |
| 1127 | * code may need to run synchronously to handle programmatic changes of |
| 1128 | * terminal height. |
| 1129 | * |
| 1130 | * Relying on the browser to send us an async resize event means we may not be |
| 1131 | * in the correct state yet when the next escape sequence hits. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1132 | * |
| 1133 | * @param {number} rowCount The number of rows. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1134 | */ |
| 1135 | hterm.Terminal.prototype.realizeHeight_ = function(rowCount) { |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 1136 | if (rowCount <= 0) |
| 1137 | throw new Error('Attempt to realize bad height: ' + rowCount); |
| 1138 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1139 | var deltaRows = rowCount - this.screen_.getHeight(); |
| 1140 | |
| 1141 | this.screenSize.height = rowCount; |
| 1142 | |
| 1143 | var cursor = this.saveCursor(); |
| 1144 | |
| 1145 | if (deltaRows < 0) { |
| 1146 | // Screen got smaller. |
| 1147 | deltaRows *= -1; |
| 1148 | while (deltaRows) { |
| 1149 | var lastRow = this.getRowCount() - 1; |
| 1150 | if (lastRow - this.scrollbackRows_.length == cursor.row) |
| 1151 | break; |
| 1152 | |
| 1153 | if (this.getRowText(lastRow)) |
| 1154 | break; |
| 1155 | |
| 1156 | this.screen_.popRow(); |
| 1157 | deltaRows--; |
| 1158 | } |
| 1159 | |
| 1160 | var ary = this.screen_.shiftRows(deltaRows); |
| 1161 | this.scrollbackRows_.push.apply(this.scrollbackRows_, ary); |
| 1162 | |
| 1163 | // We just removed rows from the top of the screen, we need to update |
| 1164 | // the cursor to match. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1165 | cursor.row = Math.max(cursor.row - deltaRows, 0); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1166 | } else if (deltaRows > 0) { |
| 1167 | // Screen got larger. |
| 1168 | |
| 1169 | if (deltaRows <= this.scrollbackRows_.length) { |
| 1170 | var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length); |
| 1171 | var rows = this.scrollbackRows_.splice( |
| 1172 | this.scrollbackRows_.length - scrollbackCount, scrollbackCount); |
| 1173 | this.screen_.unshiftRows(rows); |
| 1174 | deltaRows -= scrollbackCount; |
| 1175 | cursor.row += scrollbackCount; |
| 1176 | } |
| 1177 | |
| 1178 | if (deltaRows) |
| 1179 | this.appendRows_(deltaRows); |
| 1180 | } |
| 1181 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1182 | this.setVTScrollRegion(null, null); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1183 | this.restoreCursor(cursor); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1184 | }; |
| 1185 | |
| 1186 | /** |
| 1187 | * Scroll the terminal to the top of the scrollback buffer. |
| 1188 | */ |
| 1189 | hterm.Terminal.prototype.scrollHome = function() { |
| 1190 | this.scrollPort_.scrollRowToTop(0); |
| 1191 | }; |
| 1192 | |
| 1193 | /** |
| 1194 | * Scroll the terminal to the end. |
| 1195 | */ |
| 1196 | hterm.Terminal.prototype.scrollEnd = function() { |
| 1197 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 1198 | }; |
| 1199 | |
| 1200 | /** |
| 1201 | * Scroll the terminal one page up (minus one line) relative to the current |
| 1202 | * position. |
| 1203 | */ |
| 1204 | hterm.Terminal.prototype.scrollPageUp = function() { |
| 1205 | var i = this.scrollPort_.getTopRowIndex(); |
| 1206 | this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1); |
| 1207 | }; |
| 1208 | |
| 1209 | /** |
| 1210 | * Scroll the terminal one page down (minus one line) relative to the current |
| 1211 | * position. |
| 1212 | */ |
| 1213 | hterm.Terminal.prototype.scrollPageDown = function() { |
| 1214 | var i = this.scrollPort_.getTopRowIndex(); |
| 1215 | this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1216 | }; |
| 1217 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1218 | /** |
Mike Frysinger | cd56a63 | 2017-05-10 14:45:28 -0400 | [diff] [blame] | 1219 | * Scroll the terminal one line up relative to the current position. |
| 1220 | */ |
| 1221 | hterm.Terminal.prototype.scrollLineUp = function() { |
| 1222 | var i = this.scrollPort_.getTopRowIndex(); |
| 1223 | this.scrollPort_.scrollRowToTop(i - 1); |
| 1224 | }; |
| 1225 | |
| 1226 | /** |
| 1227 | * Scroll the terminal one line down relative to the current position. |
| 1228 | */ |
| 1229 | hterm.Terminal.prototype.scrollLineDown = function() { |
| 1230 | var i = this.scrollPort_.getTopRowIndex(); |
| 1231 | this.scrollPort_.scrollRowToTop(i + 1); |
| 1232 | }; |
| 1233 | |
| 1234 | /** |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1235 | * Clear primary screen, secondary screen, and the scrollback buffer. |
| 1236 | */ |
| 1237 | hterm.Terminal.prototype.wipeContents = function() { |
| 1238 | this.scrollbackRows_.length = 0; |
| 1239 | this.scrollPort_.resetCache(); |
| 1240 | |
| 1241 | [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) { |
| 1242 | var bottom = screen.getHeight(); |
| 1243 | if (bottom > 0) { |
| 1244 | this.renumberRows_(0, bottom); |
| 1245 | this.clearHome(screen); |
| 1246 | } |
| 1247 | }.bind(this)); |
| 1248 | |
| 1249 | this.syncCursorPosition_(); |
Andrew de los Reyes | 68e0780 | 2013-04-04 15:38:55 -0700 | [diff] [blame] | 1250 | this.scrollPort_.invalidate(); |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1251 | }; |
| 1252 | |
| 1253 | /** |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1254 | * Full terminal reset. |
Mike Frysinger | 84301d0 | 2017-11-29 13:28:46 -0800 | [diff] [blame] | 1255 | * |
| 1256 | * Perform a full reset to the default values listed in |
| 1257 | * https://vt100.net/docs/vt510-rm/RIS.html |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1258 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1259 | hterm.Terminal.prototype.reset = function() { |
Mike Frysinger | 7e42f63 | 2017-11-29 13:42:09 -0800 | [diff] [blame] | 1260 | this.vt.reset(); |
| 1261 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1262 | this.clearAllTabStops(); |
| 1263 | this.setDefaultTabStops(); |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1264 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1265 | const resetScreen = (screen) => { |
| 1266 | // We want to make sure to reset the attributes before we clear the screen. |
| 1267 | // The attributes might be used to initialize default/empty rows. |
| 1268 | screen.textAttributes.reset(); |
| 1269 | screen.textAttributes.resetColorPalette(); |
| 1270 | this.clearHome(screen); |
| 1271 | screen.saveCursorAndState(this.vt); |
| 1272 | }; |
| 1273 | resetScreen(this.primaryScreen_); |
| 1274 | resetScreen(this.alternateScreen_); |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1275 | |
Mike Frysinger | 84301d0 | 2017-11-29 13:28:46 -0800 | [diff] [blame] | 1276 | // Reset terminal options to their default values. |
| 1277 | this.options_ = new hterm.Options(); |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1278 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
| 1279 | |
Mike Frysinger | 84301d0 | 2017-11-29 13:28:46 -0800 | [diff] [blame] | 1280 | this.setVTScrollRegion(null, null); |
| 1281 | |
| 1282 | this.setCursorVisible(true); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1283 | }; |
| 1284 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1285 | /** |
| 1286 | * Soft terminal reset. |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1287 | * |
| 1288 | * Perform a soft reset to the default values listed in |
| 1289 | * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9 |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1290 | */ |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1291 | hterm.Terminal.prototype.softReset = function() { |
Mike Frysinger | 7e42f63 | 2017-11-29 13:42:09 -0800 | [diff] [blame] | 1292 | this.vt.reset(); |
| 1293 | |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1294 | // Reset terminal options to their default values. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1295 | this.options_ = new hterm.Options(); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1296 | |
Brad Town | b62dfdc | 2015-03-16 19:07:15 -0700 | [diff] [blame] | 1297 | // We show the cursor on soft reset but do not alter the blink state. |
| 1298 | this.options_.cursorBlink = !!this.timeouts_.cursorBlink; |
| 1299 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1300 | const resetScreen = (screen) => { |
| 1301 | // Xterm also resets the color palette on soft reset, even though it doesn't |
| 1302 | // seem to be documented anywhere. |
| 1303 | screen.textAttributes.reset(); |
| 1304 | screen.textAttributes.resetColorPalette(); |
| 1305 | screen.saveCursorAndState(this.vt); |
| 1306 | }; |
| 1307 | resetScreen(this.primaryScreen_); |
| 1308 | resetScreen(this.alternateScreen_); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1309 | |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1310 | // The xterm man page explicitly says this will happen on soft reset. |
| 1311 | this.setVTScrollRegion(null, null); |
| 1312 | |
| 1313 | // Xterm also shows the cursor on soft reset, but does not alter the blink |
| 1314 | // state. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1315 | this.setCursorVisible(true); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1316 | }; |
| 1317 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1318 | /** |
| 1319 | * Move the cursor forward to the next tab stop, or to the last column |
| 1320 | * if no more tab stops are set. |
| 1321 | */ |
| 1322 | hterm.Terminal.prototype.forwardTabStop = function() { |
| 1323 | var column = this.screen_.cursorPosition.column; |
| 1324 | |
| 1325 | for (var i = 0; i < this.tabStops_.length; i++) { |
| 1326 | if (this.tabStops_[i] > column) { |
| 1327 | this.setCursorColumn(this.tabStops_[i]); |
| 1328 | return; |
| 1329 | } |
| 1330 | } |
| 1331 | |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1332 | // xterm does not clear the overflow flag on HT or CHT. |
| 1333 | var overflow = this.screen_.cursorPosition.overflow; |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1334 | this.setCursorColumn(this.screenSize.width - 1); |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1335 | this.screen_.cursorPosition.overflow = overflow; |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1336 | }; |
| 1337 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1338 | /** |
| 1339 | * Move the cursor backward to the previous tab stop, or to the first column |
| 1340 | * if no previous tab stops are set. |
| 1341 | */ |
| 1342 | hterm.Terminal.prototype.backwardTabStop = function() { |
| 1343 | var column = this.screen_.cursorPosition.column; |
| 1344 | |
| 1345 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 1346 | if (this.tabStops_[i] < column) { |
| 1347 | this.setCursorColumn(this.tabStops_[i]); |
| 1348 | return; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | this.setCursorColumn(1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1353 | }; |
| 1354 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1355 | /** |
| 1356 | * Set a tab stop at the given column. |
| 1357 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1358 | * @param {integer} column Zero based column. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1359 | */ |
| 1360 | hterm.Terminal.prototype.setTabStop = function(column) { |
| 1361 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 1362 | if (this.tabStops_[i] == column) |
| 1363 | return; |
| 1364 | |
| 1365 | if (this.tabStops_[i] < column) { |
| 1366 | this.tabStops_.splice(i + 1, 0, column); |
| 1367 | return; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | this.tabStops_.splice(0, 0, column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1372 | }; |
| 1373 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1374 | /** |
| 1375 | * Clear the tab stop at the current cursor position. |
| 1376 | * |
| 1377 | * No effect if there is no tab stop at the current cursor position. |
| 1378 | */ |
| 1379 | hterm.Terminal.prototype.clearTabStopAtCursor = function() { |
| 1380 | var column = this.screen_.cursorPosition.column; |
| 1381 | |
| 1382 | var i = this.tabStops_.indexOf(column); |
| 1383 | if (i == -1) |
| 1384 | return; |
| 1385 | |
| 1386 | this.tabStops_.splice(i, 1); |
| 1387 | }; |
| 1388 | |
| 1389 | /** |
| 1390 | * Clear all tab stops. |
| 1391 | */ |
| 1392 | hterm.Terminal.prototype.clearAllTabStops = function() { |
| 1393 | this.tabStops_.length = 0; |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1394 | this.defaultTabStops = false; |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1395 | }; |
| 1396 | |
| 1397 | /** |
| 1398 | * Set up the default tab stops, starting from a given column. |
| 1399 | * |
| 1400 | * This sets a tabstop every (column % this.tabWidth) column, starting |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1401 | * from the specified column, or 0 if no column is provided. It also flags |
| 1402 | * future resizes to set them up. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1403 | * |
| 1404 | * This does not clear the existing tab stops first, use clearAllTabStops |
| 1405 | * for that. |
| 1406 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1407 | * @param {integer} opt_start Optional starting zero based starting column, useful |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1408 | * for filling out missing tab stops when the terminal is resized. |
| 1409 | */ |
| 1410 | hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) { |
| 1411 | var start = opt_start || 0; |
| 1412 | var w = this.tabWidth; |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1413 | // Round start up to a default tab stop. |
| 1414 | start = start - 1 - ((start - 1) % w) + w; |
| 1415 | for (var i = start; i < this.screenSize.width; i += w) { |
| 1416 | this.setTabStop(i); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1417 | } |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1418 | |
| 1419 | this.defaultTabStops = true; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1420 | }; |
| 1421 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1422 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1423 | * Interpret a sequence of characters. |
| 1424 | * |
| 1425 | * Incomplete escape sequences are buffered until the next call. |
| 1426 | * |
| 1427 | * @param {string} str Sequence of characters to interpret or pass through. |
| 1428 | */ |
| 1429 | hterm.Terminal.prototype.interpret = function(str) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1430 | this.vt.interpret(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1431 | this.scheduleSyncCursorPosition_(); |
| 1432 | }; |
| 1433 | |
| 1434 | /** |
| 1435 | * Take over the given DIV for use as the terminal display. |
| 1436 | * |
| 1437 | * @param {HTMLDivElement} div The div to use as the terminal display. |
| 1438 | */ |
| 1439 | hterm.Terminal.prototype.decorate = function(div) { |
Mike Frysinger | 5768a9d | 2017-12-26 12:57:44 -0500 | [diff] [blame] | 1440 | const charset = div.ownerDocument.characterSet.toLowerCase(); |
| 1441 | if (charset != 'utf-8') { |
| 1442 | console.warn(`Document encoding should be set to utf-8, not "${charset}";` + |
| 1443 | ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`); |
| 1444 | } |
| 1445 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1446 | this.div_ = div; |
| 1447 | |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 1448 | this.accessibilityReader_ = new hterm.AccessibilityReader(div); |
| 1449 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1450 | this.scrollPort_.decorate(div); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 1451 | this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image')); |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 1452 | this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size')); |
| 1453 | this.scrollPort_.setBackgroundPosition( |
| 1454 | this.prefs_.get('background-position')); |
Mike Frysinger | 08bad43 | 2017-04-24 00:50:54 -0400 | [diff] [blame] | 1455 | this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css')); |
| 1456 | this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text')); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 1457 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1458 | this.div_.focus = this.focus.bind(this); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 1459 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1460 | this.setFontSize(this.prefs_.get('font-size')); |
| 1461 | this.syncFontFamily(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1462 | |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 1463 | this.setScrollbarVisible(this.prefs_.get('scrollbar-visible')); |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 1464 | this.setScrollWheelMoveMultipler( |
| 1465 | this.prefs_.get('scroll-wheel-move-multiplier')); |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 1466 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1467 | this.document_ = this.scrollPort_.getDocument(); |
| 1468 | |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 1469 | this.document_.body.oncontextmenu = function() { return false; }; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 1470 | |
| 1471 | var onMouse = this.onMouse_.bind(this); |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1472 | var screenNode = this.scrollPort_.getScreenNode(); |
| 1473 | screenNode.addEventListener('mousedown', onMouse); |
| 1474 | screenNode.addEventListener('mouseup', onMouse); |
| 1475 | screenNode.addEventListener('mousemove', onMouse); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 1476 | this.scrollPort_.onScrollWheel = onMouse; |
| 1477 | |
Mike Frysinger | 02ded6d | 2018-06-21 14:25:20 -0400 | [diff] [blame] | 1478 | screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this)); |
| 1479 | |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1480 | screenNode.addEventListener( |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1481 | 'focus', this.onFocusChange_.bind(this, true)); |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 1482 | // Listen for mousedown events on the screenNode as in FF the focus |
| 1483 | // events don't bubble. |
| 1484 | screenNode.addEventListener('mousedown', function() { |
| 1485 | setTimeout(this.onFocusChange_.bind(this, true)); |
| 1486 | }.bind(this)); |
| 1487 | |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1488 | screenNode.addEventListener( |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1489 | 'blur', this.onFocusChange_.bind(this, false)); |
| 1490 | |
| 1491 | var style = this.document_.createElement('style'); |
| 1492 | style.textContent = |
| 1493 | ('.cursor-node[focus="false"] {' + |
| 1494 | ' box-sizing: border-box;' + |
| 1495 | ' background-color: transparent !important;' + |
| 1496 | ' border-width: 2px;' + |
| 1497 | ' border-style: solid;' + |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1498 | '}' + |
| 1499 | '.wc-node {' + |
| 1500 | ' display: inline-block;' + |
| 1501 | ' text-align: center;' + |
Mike Frysinger | 66beb0b | 2017-05-30 19:44:51 -0400 | [diff] [blame] | 1502 | ' width: calc(var(--hterm-charsize-width) * 2);' + |
Mike Frysinger | 4036f6e | 2017-05-31 14:02:55 -0400 | [diff] [blame] | 1503 | ' line-height: var(--hterm-charsize-height);' + |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 1504 | '}' + |
| 1505 | ':root {' + |
Mike Frysinger | 66beb0b | 2017-05-30 19:44:51 -0400 | [diff] [blame] | 1506 | ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' + |
| 1507 | ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' + |
Mike Frysinger | a27c050 | 2017-08-23 22:37:10 -0400 | [diff] [blame] | 1508 | // Default position hides the cursor for when the window is initializing. |
| 1509 | ' --hterm-cursor-offset-col: -1;' + |
| 1510 | ' --hterm-cursor-offset-row: -1;' + |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 1511 | ' --hterm-blink-node-duration: 0.7s;' + |
Mike Frysinger | 6ab275c | 2017-05-28 12:48:44 -0400 | [diff] [blame] | 1512 | ' --hterm-mouse-cursor-text: text;' + |
| 1513 | ' --hterm-mouse-cursor-pointer: default;' + |
| 1514 | ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' + |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 1515 | '}' + |
Mike Frysinger | d390720 | 2017-10-23 01:23:50 -0400 | [diff] [blame] | 1516 | '.uri-node:hover {' + |
| 1517 | ' text-decoration: underline;' + |
Mike Frysinger | b74a647 | 2018-06-22 13:37:08 -0400 | [diff] [blame] | 1518 | ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' + |
Mike Frysinger | d390720 | 2017-10-23 01:23:50 -0400 | [diff] [blame] | 1519 | '}' + |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 1520 | '@keyframes blink {' + |
| 1521 | ' from { opacity: 1.0; }' + |
| 1522 | ' to { opacity: 0.0; }' + |
| 1523 | '}' + |
| 1524 | '.blink-node {' + |
| 1525 | ' animation-name: blink;' + |
| 1526 | ' animation-duration: var(--hterm-blink-node-duration);' + |
| 1527 | ' animation-iteration-count: infinite;' + |
| 1528 | ' animation-timing-function: ease-in-out;' + |
| 1529 | ' animation-direction: alternate;' + |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1530 | '}'); |
Mike Frysinger | b74a647 | 2018-06-22 13:37:08 -0400 | [diff] [blame] | 1531 | // Insert this stock style as the first node so that any user styles will |
| 1532 | // override w/out having to use !important everywhere. The rules above mix |
| 1533 | // runtime variables with default ones designed to be overridden by the user, |
| 1534 | // but we can wait for a concrete case from the users to determine the best |
| 1535 | // way to split the sheet up to before & after the user-css settings. |
| 1536 | this.document_.head.insertBefore(style, this.document_.head.firstChild); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1537 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1538 | this.cursorNode_ = this.document_.createElement('div'); |
Mike Frysinger | d826f1a | 2017-07-06 16:20:06 -0400 | [diff] [blame] | 1539 | this.cursorNode_.id = 'hterm:terminal-cursor'; |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1540 | this.cursorNode_.className = 'cursor-node'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1541 | this.cursorNode_.style.cssText = |
| 1542 | ('position: absolute;' + |
Mike Frysinger | 44c3220 | 2017-08-05 01:13:09 -0400 | [diff] [blame] | 1543 | 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' + |
| 1544 | 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' + |
Mike Frysinger | d60f4c2 | 2018-03-15 21:25:30 -0700 | [diff] [blame] | 1545 | 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' + |
Mike Frysinger | 66beb0b | 2017-05-30 19:44:51 -0400 | [diff] [blame] | 1546 | 'width: var(--hterm-charsize-width);' + |
| 1547 | 'height: var(--hterm-charsize-height);' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 1548 | '-webkit-transition: opacity, background-color 100ms linear;' + |
| 1549 | '-moz-transition: opacity, background-color 100ms linear;'); |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 1550 | |
Mike Frysinger | f02a2cb | 2017-12-21 00:34:03 -0500 | [diff] [blame] | 1551 | this.setCursorColor(); |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 1552 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
| 1553 | this.restyleCursor_(); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1554 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1555 | this.document_.body.appendChild(this.cursorNode_); |
| 1556 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1557 | // When 'enableMouseDragScroll' is off we reposition this element directly |
| 1558 | // under the mouse cursor after a click. This makes Chrome associate |
| 1559 | // subsequent mousemove events with the scroll-blocker. Since the |
| 1560 | // scroll-blocker is a peer (not a child) of the scrollport, the mousemove |
| 1561 | // events do not cause the scrollport to scroll. |
| 1562 | // |
| 1563 | // It's a hack, but it's the cleanest way I could find. |
| 1564 | this.scrollBlockerNode_ = this.document_.createElement('div'); |
Mike Frysinger | d826f1a | 2017-07-06 16:20:06 -0400 | [diff] [blame] | 1565 | this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker'; |
Raymes Khoury | 6dce2f8 | 2018-04-12 15:38:58 +1000 | [diff] [blame] | 1566 | this.scrollBlockerNode_.setAttribute('aria-hidden', 'true'); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1567 | this.scrollBlockerNode_.style.cssText = |
| 1568 | ('position: absolute;' + |
| 1569 | 'top: -99px;' + |
| 1570 | 'display: block;' + |
| 1571 | 'width: 10px;' + |
| 1572 | 'height: 10px;'); |
| 1573 | this.document_.body.appendChild(this.scrollBlockerNode_); |
| 1574 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1575 | this.scrollPort_.onScrollWheel = onMouse; |
| 1576 | ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick', |
| 1577 | ].forEach(function(event) { |
| 1578 | this.scrollBlockerNode_.addEventListener(event, onMouse); |
| 1579 | this.cursorNode_.addEventListener(event, onMouse); |
| 1580 | this.document_.addEventListener(event, onMouse); |
| 1581 | }.bind(this)); |
| 1582 | |
| 1583 | this.cursorNode_.addEventListener('mousedown', function() { |
| 1584 | setTimeout(this.focus.bind(this)); |
| 1585 | }.bind(this)); |
| 1586 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1587 | this.setReverseVideo(false); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1588 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1589 | this.scrollPort_.focus(); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1590 | this.scrollPort_.scheduleRedraw(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1591 | }; |
| 1592 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1593 | /** |
| 1594 | * Return the HTML document that contains the terminal DOM nodes. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1595 | * |
| 1596 | * @return {HTMLDocument} |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1597 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1598 | hterm.Terminal.prototype.getDocument = function() { |
| 1599 | return this.document_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1600 | }; |
| 1601 | |
| 1602 | /** |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1603 | * Focus the terminal. |
| 1604 | */ |
| 1605 | hterm.Terminal.prototype.focus = function() { |
| 1606 | this.scrollPort_.focus(); |
| 1607 | }; |
| 1608 | |
| 1609 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1610 | * Return the HTML Element for a given row index. |
| 1611 | * |
| 1612 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1613 | * it to fetch rows on demand as they are scrolled into view. |
| 1614 | * |
| 1615 | * TODO(rginda): Consider saving scrollback rows as (HTML source, text content) |
| 1616 | * pairs to conserve memory. |
| 1617 | * |
| 1618 | * @param {integer} index The zero-based row index, measured relative to the |
| 1619 | * start of the scrollback buffer. On-screen rows will always have the |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1620 | * largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1621 | * @return {HTMLElement} The 'x-row' element containing for the requested row. |
| 1622 | */ |
| 1623 | hterm.Terminal.prototype.getRowNode = function(index) { |
| 1624 | if (index < this.scrollbackRows_.length) |
| 1625 | return this.scrollbackRows_[index]; |
| 1626 | |
| 1627 | var screenIndex = index - this.scrollbackRows_.length; |
| 1628 | return this.screen_.rowsArray[screenIndex]; |
| 1629 | }; |
| 1630 | |
| 1631 | /** |
| 1632 | * Return the text content for a given range of rows. |
| 1633 | * |
| 1634 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1635 | * it to fetch text content on demand when the user attempts to copy their |
| 1636 | * selection to the clipboard. |
| 1637 | * |
| 1638 | * @param {integer} start The zero-based row index to start from, measured |
| 1639 | * relative to the start of the scrollback buffer. On-screen rows will |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1640 | * always have the largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1641 | * @param {integer} end The zero-based row index to end on, measured |
| 1642 | * relative to the start of the scrollback buffer. |
| 1643 | * @return {string} A single string containing the text value of the range of |
| 1644 | * rows. Lines will be newline delimited, with no trailing newline. |
| 1645 | */ |
| 1646 | hterm.Terminal.prototype.getRowsText = function(start, end) { |
| 1647 | var ary = []; |
| 1648 | for (var i = start; i < end; i++) { |
| 1649 | var node = this.getRowNode(i); |
| 1650 | ary.push(node.textContent); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1651 | if (i < end - 1 && !node.getAttribute('line-overflow')) |
| 1652 | ary.push('\n'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1655 | return ary.join(''); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1656 | }; |
| 1657 | |
| 1658 | /** |
| 1659 | * Return the text content for a given row. |
| 1660 | * |
| 1661 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1662 | * it to fetch text content on demand when the user attempts to copy their |
| 1663 | * selection to the clipboard. |
| 1664 | * |
| 1665 | * @param {integer} index The zero-based row index to return, measured |
| 1666 | * relative to the start of the scrollback buffer. On-screen rows will |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1667 | * always have the largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1668 | * @return {string} A string containing the text value of the selected row. |
| 1669 | */ |
| 1670 | hterm.Terminal.prototype.getRowText = function(index) { |
| 1671 | var node = this.getRowNode(index); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1672 | return node.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1673 | }; |
| 1674 | |
| 1675 | /** |
| 1676 | * Return the total number of rows in the addressable screen and in the |
| 1677 | * scrollback buffer of this terminal. |
| 1678 | * |
| 1679 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1680 | * it to compute the size of the scrollbar. |
| 1681 | * |
| 1682 | * @return {integer} The number of rows in this terminal. |
| 1683 | */ |
| 1684 | hterm.Terminal.prototype.getRowCount = function() { |
| 1685 | return this.scrollbackRows_.length + this.screen_.rowsArray.length; |
| 1686 | }; |
| 1687 | |
| 1688 | /** |
| 1689 | * Create DOM nodes for new rows and append them to the end of the terminal. |
| 1690 | * |
| 1691 | * This is the only correct way to add a new DOM node for a row. Notice that |
| 1692 | * the new row is appended to the bottom of the list of rows, and does not |
| 1693 | * require renumbering (of the rowIndex property) of previous rows. |
| 1694 | * |
| 1695 | * If you think you want a new blank row somewhere in the middle of the |
| 1696 | * terminal, look into moveRows_(). |
| 1697 | * |
| 1698 | * This method does not pay attention to vtScrollTop/Bottom, since you should |
| 1699 | * be using moveRows() in cases where they would matter. |
| 1700 | * |
| 1701 | * The cursor will be positioned at column 0 of the first inserted line. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1702 | * |
| 1703 | * @param {number} count The number of rows to created. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1704 | */ |
| 1705 | hterm.Terminal.prototype.appendRows_ = function(count) { |
| 1706 | var cursorRow = this.screen_.rowsArray.length; |
| 1707 | var offset = this.scrollbackRows_.length + cursorRow; |
| 1708 | for (var i = 0; i < count; i++) { |
| 1709 | var row = this.document_.createElement('x-row'); |
| 1710 | row.appendChild(this.document_.createTextNode('')); |
| 1711 | row.rowIndex = offset + i; |
| 1712 | this.screen_.pushRow(row); |
| 1713 | } |
| 1714 | |
| 1715 | var extraRows = this.screen_.rowsArray.length - this.screenSize.height; |
| 1716 | if (extraRows > 0) { |
| 1717 | var ary = this.screen_.shiftRows(extraRows); |
| 1718 | Array.prototype.push.apply(this.scrollbackRows_, ary); |
Robert Ginda | 36c5aa6 | 2012-10-15 11:17:47 -0700 | [diff] [blame] | 1719 | if (this.scrollPort_.isScrolledEnd) |
| 1720 | this.scheduleScrollDown_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | if (cursorRow >= this.screen_.rowsArray.length) |
| 1724 | cursorRow = this.screen_.rowsArray.length - 1; |
| 1725 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1726 | this.setAbsoluteCursorPosition(cursorRow, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1727 | }; |
| 1728 | |
| 1729 | /** |
| 1730 | * Relocate rows from one part of the addressable screen to another. |
| 1731 | * |
| 1732 | * This is used to recycle rows during VT scrolls (those which are driven |
| 1733 | * by VT commands, rather than by the user manipulating the scrollbar.) |
| 1734 | * |
| 1735 | * In this case, the blank lines scrolled into the scroll region are made of |
| 1736 | * the nodes we scrolled off. These have their rowIndex properties carefully |
| 1737 | * renumbered so as not to confuse the ScrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1738 | * |
| 1739 | * @param {number} fromIndex The start index. |
| 1740 | * @param {number} count The number of rows to move. |
| 1741 | * @param {number} toIndex The destination index. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1742 | */ |
| 1743 | hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) { |
| 1744 | var ary = this.screen_.removeRows(fromIndex, count); |
| 1745 | this.screen_.insertRows(toIndex, ary); |
| 1746 | |
| 1747 | var start, end; |
| 1748 | if (fromIndex < toIndex) { |
| 1749 | start = fromIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1750 | end = toIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1751 | } else { |
| 1752 | start = toIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1753 | end = fromIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | this.renumberRows_(start, end); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1757 | this.scrollPort_.scheduleInvalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1758 | }; |
| 1759 | |
| 1760 | /** |
| 1761 | * Renumber the rowIndex property of the given range of rows. |
| 1762 | * |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1763 | * The start and end indices are relative to the screen, not the scrollback. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1764 | * Rows in the scrollback buffer cannot be renumbered. Since they are not |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1765 | * addressable (you can't delete them, scroll them, etc), you should have |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1766 | * no need to renumber scrollback rows. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1767 | * |
| 1768 | * @param {number} start The start index. |
| 1769 | * @param {number} end The end index. |
| 1770 | * @param {hterm.Screen} opt_screen The screen to renumber. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1771 | */ |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1772 | hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) { |
| 1773 | var screen = opt_screen || this.screen_; |
| 1774 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1775 | var offset = this.scrollbackRows_.length; |
| 1776 | for (var i = start; i < end; i++) { |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1777 | screen.rowsArray[i].rowIndex = offset + i; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1778 | } |
| 1779 | }; |
| 1780 | |
| 1781 | /** |
| 1782 | * Print a string to the terminal. |
| 1783 | * |
| 1784 | * This respects the current insert and wraparound modes. It will add new lines |
| 1785 | * to the end of the terminal, scrolling off the top into the scrollback buffer |
| 1786 | * if necessary. |
| 1787 | * |
| 1788 | * The string is *not* parsed for escape codes. Use the interpret() method if |
| 1789 | * that's what you're after. |
| 1790 | * |
| 1791 | * @param{string} str The string to print. |
| 1792 | */ |
| 1793 | hterm.Terminal.prototype.print = function(str) { |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 1794 | // Basic accessibility output for the screen reader. |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame] | 1795 | if (this.accessibilityEnabled_) { |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 1796 | this.accessibilityReader_.announce(str); |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame] | 1797 | } |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 1798 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1799 | var startOffset = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1800 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1801 | var strWidth = lib.wc.strWidth(str); |
Mike Frysinger | 67fc8ef | 2017-08-21 16:03:16 -0400 | [diff] [blame] | 1802 | // Fun edge case: If the string only contains zero width codepoints (like |
| 1803 | // combining characters), we make sure to iterate at least once below. |
| 1804 | if (strWidth == 0 && str) |
| 1805 | strWidth = 1; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1806 | |
| 1807 | while (startOffset < strWidth) { |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1808 | if (this.options_.wraparound && this.screen_.cursorPosition.overflow) { |
| 1809 | this.screen_.commitLineOverflow(); |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame] | 1810 | this.newLine(true); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1811 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1812 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1813 | var count = strWidth - startOffset; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1814 | var didOverflow = false; |
| 1815 | var substr; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1816 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1817 | if (this.screen_.cursorPosition.column + count >= this.screenSize.width) { |
| 1818 | didOverflow = true; |
| 1819 | count = this.screenSize.width - this.screen_.cursorPosition.column; |
| 1820 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1821 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1822 | if (didOverflow && !this.options_.wraparound) { |
| 1823 | // If the string overflowed the line but wraparound is off, then the |
| 1824 | // last printed character should be the last of the string. |
| 1825 | // TODO: This will add to our problems with multibyte UTF-16 characters. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1826 | substr = lib.wc.substr(str, startOffset, count - 1) + |
| 1827 | lib.wc.substr(str, strWidth - 1); |
| 1828 | count = strWidth; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1829 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1830 | substr = lib.wc.substr(str, startOffset, count); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1831 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1832 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1833 | var tokens = hterm.TextAttributes.splitWidecharString(substr); |
| 1834 | for (var i = 0; i < tokens.length; i++) { |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 1835 | this.screen_.textAttributes.wcNode = tokens[i].wcNode; |
| 1836 | this.screen_.textAttributes.asciiNode = tokens[i].asciiNode; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1837 | |
| 1838 | if (this.options_.insertMode) { |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 1839 | this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1840 | } else { |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 1841 | this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1842 | } |
| 1843 | this.screen_.textAttributes.wcNode = false; |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 1844 | this.screen_.textAttributes.asciiNode = true; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | this.screen_.maybeClipCurrentRow(); |
| 1848 | startOffset += count; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1849 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1850 | |
| 1851 | this.scheduleSyncCursorPosition_(); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1852 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1853 | if (this.scrollOnOutput_) |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1854 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1855 | }; |
| 1856 | |
| 1857 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1858 | * Set the VT scroll region. |
| 1859 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1860 | * This also resets the cursor position to the absolute (0, 0) position, since |
| 1861 | * that's what xterm appears to do. |
| 1862 | * |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1863 | * Setting the scroll region to the full height of the terminal will clear |
| 1864 | * the scroll region. This is *NOT* what most terminals do. We're explicitly |
| 1865 | * going "off-spec" here because it makes `screen` and `tmux` overflow into the |
| 1866 | * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn |
| 1867 | * continue to work as most users would expect. |
| 1868 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1869 | * @param {integer} scrollTop The zero-based top of the scroll region. |
| 1870 | * @param {integer} scrollBottom The zero-based bottom of the scroll region, |
| 1871 | * inclusive. |
| 1872 | */ |
| 1873 | hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) { |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1874 | if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) { |
Robert Ginda | 43684e2 | 2013-11-25 14:18:52 -0800 | [diff] [blame] | 1875 | this.vtScrollTop_ = null; |
| 1876 | this.vtScrollBottom_ = null; |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1877 | } else { |
| 1878 | this.vtScrollTop_ = scrollTop; |
| 1879 | this.vtScrollBottom_ = scrollBottom; |
| 1880 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1881 | }; |
| 1882 | |
| 1883 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1884 | * Return the top row index according to the VT. |
| 1885 | * |
| 1886 | * This will return 0 unless the terminal has been told to restrict scrolling |
| 1887 | * to some lower row. It is used for some VT cursor positioning and scrolling |
| 1888 | * commands. |
| 1889 | * |
| 1890 | * @return {integer} The topmost row in the terminal's scroll region. |
| 1891 | */ |
| 1892 | hterm.Terminal.prototype.getVTScrollTop = function() { |
| 1893 | if (this.vtScrollTop_ != null) |
| 1894 | return this.vtScrollTop_; |
| 1895 | |
| 1896 | return 0; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1897 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1898 | |
| 1899 | /** |
| 1900 | * Return the bottom row index according to the VT. |
| 1901 | * |
| 1902 | * This will return the height of the terminal unless the it has been told to |
| 1903 | * restrict scrolling to some higher row. It is used for some VT cursor |
| 1904 | * positioning and scrolling commands. |
| 1905 | * |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1906 | * @return {integer} The bottom most row in the terminal's scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1907 | */ |
| 1908 | hterm.Terminal.prototype.getVTScrollBottom = function() { |
| 1909 | if (this.vtScrollBottom_ != null) |
| 1910 | return this.vtScrollBottom_; |
| 1911 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1912 | return this.screenSize.height - 1; |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 1913 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1914 | |
| 1915 | /** |
| 1916 | * Process a '\n' character. |
| 1917 | * |
| 1918 | * If the cursor is on the final row of the terminal this will append a new |
| 1919 | * blank row to the screen and scroll the topmost row into the scrollback |
| 1920 | * buffer. |
| 1921 | * |
| 1922 | * Otherwise, this moves the cursor to column zero of the next row. |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame] | 1923 | * |
| 1924 | * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of |
| 1925 | * the terminal. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1926 | */ |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame] | 1927 | hterm.Terminal.prototype.newLine = function(dueToOverflow = false) { |
| 1928 | if (!dueToOverflow) |
| 1929 | this.accessibilityReader_.newLine(); |
| 1930 | |
Robert Ginda | 9937abc | 2013-07-25 16:09:23 -0700 | [diff] [blame] | 1931 | var cursorAtEndOfScreen = (this.screen_.cursorPosition.row == |
| 1932 | this.screen_.rowsArray.length - 1); |
| 1933 | |
| 1934 | if (this.vtScrollBottom_ != null) { |
| 1935 | // A VT Scroll region is active, we never append new rows. |
| 1936 | if (this.screen_.cursorPosition.row == this.vtScrollBottom_) { |
| 1937 | // We're at the end of the VT Scroll Region, perform a VT scroll. |
| 1938 | this.vtScrollUp(1); |
| 1939 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
| 1940 | } else if (cursorAtEndOfScreen) { |
| 1941 | // We're at the end of the screen, the only thing to do is put the |
| 1942 | // cursor to column 0. |
| 1943 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
| 1944 | } else { |
| 1945 | // Anywhere else, advance the cursor row, and reset the column. |
| 1946 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
| 1947 | } |
| 1948 | } else if (cursorAtEndOfScreen) { |
Robert Ginda | 1b06b37 | 2013-07-19 15:22:51 -0700 | [diff] [blame] | 1949 | // We're at the end of the screen. Append a new row to the terminal, |
| 1950 | // shifting the top row into the scrollback. |
| 1951 | this.appendRows_(1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1952 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1953 | // Anywhere else in the screen just moves the cursor. |
| 1954 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1955 | } |
| 1956 | }; |
| 1957 | |
| 1958 | /** |
| 1959 | * Like newLine(), except maintain the cursor column. |
| 1960 | */ |
| 1961 | hterm.Terminal.prototype.lineFeed = function() { |
| 1962 | var column = this.screen_.cursorPosition.column; |
| 1963 | this.newLine(); |
| 1964 | this.setCursorColumn(column); |
| 1965 | }; |
| 1966 | |
| 1967 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1968 | * If autoCarriageReturn is set then newLine(), else lineFeed(). |
| 1969 | */ |
| 1970 | hterm.Terminal.prototype.formFeed = function() { |
| 1971 | if (this.options_.autoCarriageReturn) { |
| 1972 | this.newLine(); |
| 1973 | } else { |
| 1974 | this.lineFeed(); |
| 1975 | } |
| 1976 | }; |
| 1977 | |
| 1978 | /** |
| 1979 | * Move the cursor up one row, possibly inserting a blank line. |
| 1980 | * |
| 1981 | * The cursor column is not changed. |
| 1982 | */ |
| 1983 | hterm.Terminal.prototype.reverseLineFeed = function() { |
| 1984 | var scrollTop = this.getVTScrollTop(); |
| 1985 | var currentRow = this.screen_.cursorPosition.row; |
| 1986 | |
| 1987 | if (currentRow == scrollTop) { |
| 1988 | this.insertLines(1); |
| 1989 | } else { |
| 1990 | this.setAbsoluteCursorRow(currentRow - 1); |
| 1991 | } |
| 1992 | }; |
| 1993 | |
| 1994 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1995 | * Replace all characters to the left of the current cursor with the space |
| 1996 | * character. |
| 1997 | * |
| 1998 | * TODO(rginda): This should probably *remove* the characters (not just replace |
| 1999 | * 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] | 2000 | * position. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2001 | */ |
| 2002 | hterm.Terminal.prototype.eraseToLeft = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2003 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2004 | this.setCursorColumn(0); |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 2005 | const count = cursor.column + 1; |
| 2006 | this.screen_.overwriteString(lib.f.getWhitespace(count), count); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2007 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2008 | }; |
| 2009 | |
| 2010 | /** |
David Benjamin | 684a9b7 | 2012-05-01 17:19:58 -0400 | [diff] [blame] | 2011 | * Erase a given number of characters to the right of the cursor. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2012 | * |
| 2013 | * The cursor position is unchanged. |
| 2014 | * |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 2015 | * If the current background color is not the default background color this |
| 2016 | * will insert spaces rather than delete. This is unfortunate because the |
| 2017 | * trailing space will affect text selection, but it's difficult to come up |
| 2018 | * with a way to style empty space that wouldn't trip up the hterm.Screen |
| 2019 | * code. |
Robert Ginda | cd5637d | 2013-10-30 14:59:10 -0700 | [diff] [blame] | 2020 | * |
| 2021 | * eraseToRight is ignored in the presence of a cursor overflow. This deviates |
| 2022 | * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See |
| 2023 | * crbug.com/232390 for details. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2024 | * |
| 2025 | * @param {number} opt_count The number of characters to erase. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2026 | */ |
| 2027 | hterm.Terminal.prototype.eraseToRight = function(opt_count) { |
Robert Ginda | cd5637d | 2013-10-30 14:59:10 -0700 | [diff] [blame] | 2028 | if (this.screen_.cursorPosition.overflow) |
| 2029 | return; |
| 2030 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 2031 | var maxCount = this.screenSize.width - this.screen_.cursorPosition.column; |
| 2032 | var count = opt_count ? Math.min(opt_count, maxCount) : maxCount; |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 2033 | |
| 2034 | if (this.screen_.textAttributes.background === |
| 2035 | this.screen_.textAttributes.DEFAULT_COLOR) { |
| 2036 | var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row]; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 2037 | if (hterm.TextAttributes.nodeWidth(cursorRow) <= |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 2038 | this.screen_.cursorPosition.column + count) { |
| 2039 | this.screen_.deleteChars(count); |
| 2040 | this.clearCursorOverflow(); |
| 2041 | return; |
| 2042 | } |
| 2043 | } |
| 2044 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2045 | var cursor = this.saveCursor(); |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 2046 | this.screen_.overwriteString(lib.f.getWhitespace(count), count); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2047 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2048 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2049 | }; |
| 2050 | |
| 2051 | /** |
| 2052 | * Erase the current line. |
| 2053 | * |
| 2054 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2055 | */ |
| 2056 | hterm.Terminal.prototype.eraseLine = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2057 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2058 | this.screen_.clearCursorRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2059 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2060 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2061 | }; |
| 2062 | |
| 2063 | /** |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 2064 | * Erase all characters from the start of the screen to the current cursor |
| 2065 | * position, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2066 | * |
| 2067 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2068 | */ |
| 2069 | hterm.Terminal.prototype.eraseAbove = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2070 | var cursor = this.saveCursor(); |
| 2071 | |
| 2072 | this.eraseToLeft(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2073 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 2074 | for (var i = 0; i < cursor.row; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2075 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2076 | this.screen_.clearCursorRow(); |
| 2077 | } |
| 2078 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2079 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2080 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2081 | }; |
| 2082 | |
| 2083 | /** |
| 2084 | * 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] | 2085 | * screen, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2086 | * |
| 2087 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2088 | */ |
| 2089 | hterm.Terminal.prototype.eraseBelow = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2090 | var cursor = this.saveCursor(); |
| 2091 | |
| 2092 | this.eraseToRight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2093 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 2094 | var bottom = this.screenSize.height - 1; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2095 | for (var i = cursor.row + 1; i <= bottom; i++) { |
| 2096 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2097 | this.screen_.clearCursorRow(); |
| 2098 | } |
| 2099 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2100 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2101 | this.clearCursorOverflow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2102 | }; |
| 2103 | |
| 2104 | /** |
| 2105 | * Fill the terminal with a given character. |
| 2106 | * |
| 2107 | * This methods does not respect the VT scroll region. |
| 2108 | * |
| 2109 | * @param {string} ch The character to use for the fill. |
| 2110 | */ |
| 2111 | hterm.Terminal.prototype.fill = function(ch) { |
| 2112 | var cursor = this.saveCursor(); |
| 2113 | |
| 2114 | this.setAbsoluteCursorPosition(0, 0); |
| 2115 | for (var row = 0; row < this.screenSize.height; row++) { |
| 2116 | for (var col = 0; col < this.screenSize.width; col++) { |
| 2117 | this.setAbsoluteCursorPosition(row, col); |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 2118 | this.screen_.overwriteString(ch, 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2123 | }; |
| 2124 | |
| 2125 | /** |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2126 | * Erase the entire display and leave the cursor at (0, 0). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2127 | * |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2128 | * This does not respect the scroll region. |
| 2129 | * |
| 2130 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 2131 | * to the current screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2132 | */ |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2133 | hterm.Terminal.prototype.clearHome = function(opt_screen) { |
| 2134 | var screen = opt_screen || this.screen_; |
| 2135 | var bottom = screen.getHeight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2136 | |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2137 | this.accessibilityReader_.clear(); |
| 2138 | |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 2139 | if (bottom == 0) { |
| 2140 | // Empty screen, nothing to do. |
| 2141 | return; |
| 2142 | } |
| 2143 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 2144 | for (var i = 0; i < bottom; i++) { |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2145 | screen.setCursorPosition(i, 0); |
| 2146 | screen.clearCursorRow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2149 | screen.setCursorPosition(0, 0); |
| 2150 | }; |
| 2151 | |
| 2152 | /** |
| 2153 | * Erase the entire display without changing the cursor position. |
| 2154 | * |
| 2155 | * The cursor position is unchanged. This does not respect the scroll |
| 2156 | * region. |
| 2157 | * |
| 2158 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 2159 | * to the current screen. |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 2160 | */ |
| 2161 | hterm.Terminal.prototype.clear = function(opt_screen) { |
| 2162 | var screen = opt_screen || this.screen_; |
| 2163 | var cursor = screen.cursorPosition.clone(); |
| 2164 | this.clearHome(screen); |
| 2165 | screen.setCursorPosition(cursor.row, cursor.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2166 | }; |
| 2167 | |
| 2168 | /** |
| 2169 | * VT command to insert lines at the current cursor row. |
| 2170 | * |
| 2171 | * This respects the current scroll region. Rows pushed off the bottom are |
| 2172 | * lost (they won't show up in the scrollback buffer). |
| 2173 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2174 | * @param {integer} count The number of lines to insert. |
| 2175 | */ |
| 2176 | hterm.Terminal.prototype.insertLines = function(count) { |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2177 | var cursorRow = this.screen_.cursorPosition.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2178 | |
| 2179 | var bottom = this.getVTScrollBottom(); |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2180 | count = Math.min(count, bottom - cursorRow); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2181 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2182 | // The moveCount is the number of rows we need to relocate to make room for |
| 2183 | // the new row(s). The count is the distance to move them. |
| 2184 | var moveCount = bottom - cursorRow - count + 1; |
| 2185 | if (moveCount) |
| 2186 | this.moveRows_(cursorRow, moveCount, cursorRow + count); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2187 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2188 | for (var i = count - 1; i >= 0; i--) { |
| 2189 | this.setAbsoluteCursorPosition(cursorRow + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2190 | this.screen_.clearCursorRow(); |
| 2191 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2192 | }; |
| 2193 | |
| 2194 | /** |
| 2195 | * VT command to delete lines at the current cursor row. |
| 2196 | * |
| 2197 | * New rows are added to the bottom of scroll region to take their place. New |
| 2198 | * rows are strictly there to take up space and have no content or style. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2199 | * |
| 2200 | * @param {number} count The number of lines to delete. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2201 | */ |
| 2202 | hterm.Terminal.prototype.deleteLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2203 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2204 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2205 | var top = cursor.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2206 | var bottom = this.getVTScrollBottom(); |
| 2207 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2208 | var maxCount = bottom - top + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2209 | count = Math.min(count, maxCount); |
| 2210 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2211 | var moveStart = bottom - count + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2212 | if (count != maxCount) |
| 2213 | this.moveRows_(top, count, moveStart); |
| 2214 | |
| 2215 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2216 | this.setAbsoluteCursorPosition(moveStart + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2217 | this.screen_.clearCursorRow(); |
| 2218 | } |
| 2219 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2220 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2221 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2222 | }; |
| 2223 | |
| 2224 | /** |
| 2225 | * Inserts the given number of spaces at the current cursor position. |
| 2226 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2227 | * The cursor position is not changed. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2228 | * |
| 2229 | * @param {number} count The number of spaces to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2230 | */ |
| 2231 | hterm.Terminal.prototype.insertSpace = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2232 | var cursor = this.saveCursor(); |
| 2233 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2234 | var ws = lib.f.getWhitespace(count || 1); |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 2235 | this.screen_.insertString(ws, ws.length); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 2236 | this.screen_.maybeClipCurrentRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2237 | |
| 2238 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2239 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2240 | }; |
| 2241 | |
| 2242 | /** |
| 2243 | * Forward-delete the specified number of characters starting at the cursor |
| 2244 | * position. |
| 2245 | * |
| 2246 | * @param {integer} count The number of characters to delete. |
| 2247 | */ |
| 2248 | hterm.Terminal.prototype.deleteChars = function(count) { |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 2249 | var deleted = this.screen_.deleteChars(count); |
| 2250 | if (deleted && !this.screen_.textAttributes.isDefault()) { |
| 2251 | var cursor = this.saveCursor(); |
| 2252 | this.setCursorColumn(this.screenSize.width - deleted); |
| 2253 | this.screen_.insertString(lib.f.getWhitespace(deleted)); |
| 2254 | this.restoreCursor(cursor); |
| 2255 | } |
| 2256 | |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2257 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2258 | }; |
| 2259 | |
| 2260 | /** |
| 2261 | * Shift rows in the scroll region upwards by a given number of lines. |
| 2262 | * |
| 2263 | * New rows are inserted at the bottom of the scroll region to fill the |
| 2264 | * vacated rows. The new rows not filled out with the current text attributes. |
| 2265 | * |
| 2266 | * This function does not affect the scrollback rows at all. Rows shifted |
| 2267 | * off the top are lost. |
| 2268 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2269 | * The cursor position is not altered. |
| 2270 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2271 | * @param {integer} count The number of rows to scroll. |
| 2272 | */ |
| 2273 | hterm.Terminal.prototype.vtScrollUp = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2274 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2275 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2276 | this.setAbsoluteCursorRow(this.getVTScrollTop()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2277 | this.deleteLines(count); |
| 2278 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2279 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2280 | }; |
| 2281 | |
| 2282 | /** |
| 2283 | * Shift rows below the cursor down by a given number of lines. |
| 2284 | * |
| 2285 | * This function respects the current scroll region. |
| 2286 | * |
| 2287 | * New rows are inserted at the top of the scroll region to fill the |
| 2288 | * vacated rows. The new rows not filled out with the current text attributes. |
| 2289 | * |
| 2290 | * This function does not affect the scrollback rows at all. Rows shifted |
| 2291 | * off the bottom are lost. |
| 2292 | * |
| 2293 | * @param {integer} count The number of rows to scroll. |
| 2294 | */ |
| 2295 | hterm.Terminal.prototype.vtScrollDown = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2296 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2297 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2298 | this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2299 | this.insertLines(opt_count); |
| 2300 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2301 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2302 | }; |
| 2303 | |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2304 | /** |
Raymes Khoury | fa06b1d | 2018-06-06 16:43:39 +1000 | [diff] [blame] | 2305 | * Enable accessibility-friendly features that have a performance impact. |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2306 | * |
| 2307 | * This will generate additional DOM nodes in an aria-live region that will |
Raymes Khoury | fa06b1d | 2018-06-06 16:43:39 +1000 | [diff] [blame] | 2308 | * cause Assitive Technology to announce the output of the terminal. It also |
| 2309 | * enables other features that aid assistive technology. All the features gated |
| 2310 | * behind this flag have a performance impact on the terminal which is why they |
| 2311 | * are made optional. |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2312 | * |
Raymes Khoury | fa06b1d | 2018-06-06 16:43:39 +1000 | [diff] [blame] | 2313 | * @param {boolean} enabled Whether to enable accessibility-friendly features. |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2314 | */ |
Raymes Khoury | fa06b1d | 2018-06-06 16:43:39 +1000 | [diff] [blame] | 2315 | hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) { |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2316 | this.accessibilityEnabled_ = enabled; |
Raymes Khoury | fa06b1d | 2018-06-06 16:43:39 +1000 | [diff] [blame] | 2317 | this.scrollPort_.setAccessibilityEnabled(enabled); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 2318 | }; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2319 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2320 | /** |
| 2321 | * Set the cursor position. |
| 2322 | * |
| 2323 | * The cursor row is relative to the scroll region if the terminal has |
| 2324 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 2325 | * |
| 2326 | * @param {integer} row The new zero-based cursor row. |
| 2327 | * @param {integer} row The new zero-based cursor column. |
| 2328 | */ |
| 2329 | hterm.Terminal.prototype.setCursorPosition = function(row, column) { |
| 2330 | if (this.options_.originMode) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2331 | this.setRelativeCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2332 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2333 | this.setAbsoluteCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2334 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2335 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2336 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2337 | /** |
| 2338 | * Move the cursor relative to its current position. |
| 2339 | * |
| 2340 | * @param {number} row |
| 2341 | * @param {number} column |
| 2342 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2343 | hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) { |
| 2344 | var scrollTop = this.getVTScrollTop(); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2345 | row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom()); |
| 2346 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2347 | this.screen_.setCursorPosition(row, column); |
| 2348 | }; |
| 2349 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2350 | /** |
| 2351 | * Move the cursor to the specified position. |
| 2352 | * |
| 2353 | * @param {number} row |
| 2354 | * @param {number} column |
| 2355 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2356 | hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2357 | row = lib.f.clamp(row, 0, this.screenSize.height - 1); |
| 2358 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2359 | this.screen_.setCursorPosition(row, column); |
| 2360 | }; |
| 2361 | |
| 2362 | /** |
| 2363 | * Set the cursor column. |
| 2364 | * |
| 2365 | * @param {integer} column The new zero-based cursor column. |
| 2366 | */ |
| 2367 | hterm.Terminal.prototype.setCursorColumn = function(column) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2368 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2369 | }; |
| 2370 | |
| 2371 | /** |
| 2372 | * Return the cursor column. |
| 2373 | * |
| 2374 | * @return {integer} The zero-based cursor column. |
| 2375 | */ |
| 2376 | hterm.Terminal.prototype.getCursorColumn = function() { |
| 2377 | return this.screen_.cursorPosition.column; |
| 2378 | }; |
| 2379 | |
| 2380 | /** |
| 2381 | * Set the cursor row. |
| 2382 | * |
| 2383 | * The cursor row is relative to the scroll region if the terminal has |
| 2384 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 2385 | * |
| 2386 | * @param {integer} row The new cursor row. |
| 2387 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2388 | hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) { |
| 2389 | this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2390 | }; |
| 2391 | |
| 2392 | /** |
| 2393 | * Return the cursor row. |
| 2394 | * |
| 2395 | * @return {integer} The zero-based cursor row. |
| 2396 | */ |
Mike Frysinger | cf3c762 | 2017-04-21 11:37:33 -0400 | [diff] [blame] | 2397 | hterm.Terminal.prototype.getCursorRow = function() { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2398 | return this.screen_.cursorPosition.row; |
| 2399 | }; |
| 2400 | |
| 2401 | /** |
| 2402 | * Request that the ScrollPort redraw itself soon. |
| 2403 | * |
| 2404 | * The redraw will happen asynchronously, soon after the call stack winds down. |
| 2405 | * Multiple calls will be coalesced into a single redraw. |
| 2406 | */ |
| 2407 | hterm.Terminal.prototype.scheduleRedraw_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2408 | if (this.timeouts_.redraw) |
| 2409 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2410 | |
| 2411 | var self = this; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2412 | this.timeouts_.redraw = setTimeout(function() { |
| 2413 | delete self.timeouts_.redraw; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2414 | self.scrollPort_.redraw_(); |
| 2415 | }, 0); |
| 2416 | }; |
| 2417 | |
| 2418 | /** |
| 2419 | * Request that the ScrollPort be scrolled to the bottom. |
| 2420 | * |
| 2421 | * The scroll will happen asynchronously, soon after the call stack winds down. |
| 2422 | * Multiple calls will be coalesced into a single scroll. |
| 2423 | * |
| 2424 | * This affects the scrollbar position of the ScrollPort, and has nothing to |
| 2425 | * do with the VT scroll commands. |
| 2426 | */ |
| 2427 | hterm.Terminal.prototype.scheduleScrollDown_ = function() { |
| 2428 | if (this.timeouts_.scrollDown) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2429 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2430 | |
| 2431 | var self = this; |
| 2432 | this.timeouts_.scrollDown = setTimeout(function() { |
| 2433 | delete self.timeouts_.scrollDown; |
| 2434 | self.scrollPort_.scrollRowToBottom(self.getRowCount()); |
| 2435 | }, 10); |
| 2436 | }; |
| 2437 | |
| 2438 | /** |
| 2439 | * Move the cursor up a specified number of rows. |
| 2440 | * |
| 2441 | * @param {integer} count The number of rows to move the cursor. |
| 2442 | */ |
| 2443 | hterm.Terminal.prototype.cursorUp = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2444 | return this.cursorDown(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2445 | }; |
| 2446 | |
| 2447 | /** |
| 2448 | * Move the cursor down a specified number of rows. |
| 2449 | * |
| 2450 | * @param {integer} count The number of rows to move the cursor. |
| 2451 | */ |
| 2452 | hterm.Terminal.prototype.cursorDown = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2453 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2454 | var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0); |
| 2455 | var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() : |
| 2456 | this.screenSize.height - 1); |
| 2457 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2458 | var row = lib.f.clamp(this.screen_.cursorPosition.row + count, |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2459 | minHeight, maxHeight); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2460 | this.setAbsoluteCursorRow(row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2461 | }; |
| 2462 | |
| 2463 | /** |
| 2464 | * Move the cursor left a specified number of columns. |
| 2465 | * |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2466 | * If reverse wraparound mode is enabled and the previous row wrapped into |
| 2467 | * the current row then we back up through the wraparound as well. |
| 2468 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2469 | * @param {integer} count The number of columns to move the cursor. |
| 2470 | */ |
| 2471 | hterm.Terminal.prototype.cursorLeft = function(count) { |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2472 | count = count || 1; |
| 2473 | |
| 2474 | if (count < 1) |
| 2475 | return; |
| 2476 | |
| 2477 | var currentColumn = this.screen_.cursorPosition.column; |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2478 | if (this.options_.reverseWraparound) { |
| 2479 | if (this.screen_.cursorPosition.overflow) { |
| 2480 | // If this cursor is in the right margin, consume one count to get it |
| 2481 | // back to the last column. This only applies when we're in reverse |
| 2482 | // wraparound mode. |
| 2483 | count--; |
| 2484 | this.clearCursorOverflow(); |
| 2485 | |
| 2486 | if (!count) |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2487 | return; |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2490 | var newRow = this.screen_.cursorPosition.row; |
| 2491 | var newColumn = currentColumn - count; |
| 2492 | if (newColumn < 0) { |
| 2493 | newRow = newRow - Math.floor(count / this.screenSize.width) - 1; |
| 2494 | if (newRow < 0) { |
| 2495 | // xterm also wraps from row 0 to the last row. |
| 2496 | newRow = this.screenSize.height + newRow % this.screenSize.height; |
| 2497 | } |
| 2498 | newColumn = this.screenSize.width + newColumn % this.screenSize.width; |
| 2499 | } |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2500 | |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2501 | this.setCursorPosition(Math.max(newRow, 0), newColumn); |
| 2502 | |
| 2503 | } else { |
| 2504 | var newColumn = Math.max(currentColumn - count, 0); |
| 2505 | this.setCursorColumn(newColumn); |
| 2506 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2507 | }; |
| 2508 | |
| 2509 | /** |
| 2510 | * Move the cursor right a specified number of columns. |
| 2511 | * |
| 2512 | * @param {integer} count The number of columns to move the cursor. |
| 2513 | */ |
| 2514 | hterm.Terminal.prototype.cursorRight = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2515 | count = count || 1; |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2516 | |
| 2517 | if (count < 1) |
| 2518 | return; |
| 2519 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2520 | var column = lib.f.clamp(this.screen_.cursorPosition.column + count, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2521 | 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2522 | this.setCursorColumn(column); |
| 2523 | }; |
| 2524 | |
| 2525 | /** |
| 2526 | * Reverse the foreground and background colors of the terminal. |
| 2527 | * |
| 2528 | * This only affects text that was drawn with no attributes. |
| 2529 | * |
| 2530 | * TODO(rginda): Test xterm to see if reverse is respected for text that has |
| 2531 | * been drawn with attributes that happen to coincide with the default |
| 2532 | * 'no-attribute' colors. My guess is probably not. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2533 | * |
| 2534 | * @param {boolean} state The state to set. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2535 | */ |
| 2536 | hterm.Terminal.prototype.setReverseVideo = function(state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2537 | this.options_.reverseVideo = state; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2538 | if (state) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2539 | this.scrollPort_.setForegroundColor(this.prefs_.get('background-color')); |
| 2540 | this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2541 | } else { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2542 | this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color')); |
| 2543 | this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2544 | } |
| 2545 | }; |
| 2546 | |
| 2547 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2548 | * Ring the terminal bell. |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2549 | * |
| 2550 | * This will not play the bell audio more than once per second. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2551 | */ |
| 2552 | hterm.Terminal.prototype.ringBell = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2553 | this.cursorNode_.style.backgroundColor = |
| 2554 | this.scrollPort_.getForegroundColor(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2555 | |
| 2556 | var self = this; |
| 2557 | setTimeout(function() { |
Matheus Fernandes | 2d73308 | 2017-09-11 06:43:01 -0400 | [diff] [blame] | 2558 | self.restyleCursor_(); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2559 | }, 200); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2560 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2561 | // bellSquelchTimeout_ affects both audio and notification bells. |
| 2562 | if (this.bellSquelchTimeout_) |
| 2563 | return; |
| 2564 | |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2565 | if (this.bellAudio_.getAttribute('src')) { |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2566 | this.bellAudio_.play(); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2567 | this.bellSequelchTimeout_ = setTimeout(function() { |
| 2568 | delete this.bellSquelchTimeout_; |
Robert Ginda | a633137 | 2013-03-19 10:35:39 -0700 | [diff] [blame] | 2569 | }.bind(this), 500); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2570 | } else { |
| 2571 | delete this.bellSquelchTimeout_; |
| 2572 | } |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2573 | |
| 2574 | if (this.desktopNotificationBell_ && !this.document_.hasFocus()) { |
Mike Frysinger | a5fb83c | 2017-06-22 14:48:35 -0700 | [diff] [blame] | 2575 | var n = hterm.notify(); |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2576 | this.bellNotificationList_.push(n); |
| 2577 | // TODO: Should we try to raise the window here? |
| 2578 | n.onclick = function() { self.closeBellNotifications_(); }; |
| 2579 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2580 | }; |
| 2581 | |
| 2582 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2583 | * Set the origin mode bit. |
| 2584 | * |
| 2585 | * If origin mode is on, certain VT cursor and scrolling commands measure their |
| 2586 | * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds |
| 2587 | * to the top of the addressable screen. |
| 2588 | * |
| 2589 | * Defaults to off. |
| 2590 | * |
| 2591 | * @param {boolean} state True to set origin mode, false to unset. |
| 2592 | */ |
| 2593 | hterm.Terminal.prototype.setOriginMode = function(state) { |
| 2594 | this.options_.originMode = state; |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 2595 | this.setCursorPosition(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2596 | }; |
| 2597 | |
| 2598 | /** |
| 2599 | * Set the insert mode bit. |
| 2600 | * |
| 2601 | * If insert mode is on, existing text beyond the cursor position will be |
| 2602 | * shifted right to make room for new text. Otherwise, new text overwrites |
| 2603 | * any existing text. |
| 2604 | * |
| 2605 | * Defaults to off. |
| 2606 | * |
| 2607 | * @param {boolean} state True to set insert mode, false to unset. |
| 2608 | */ |
| 2609 | hterm.Terminal.prototype.setInsertMode = function(state) { |
| 2610 | this.options_.insertMode = state; |
| 2611 | }; |
| 2612 | |
| 2613 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2614 | * Set the auto carriage return bit. |
| 2615 | * |
| 2616 | * If auto carriage return is on then a formfeed character is interpreted |
| 2617 | * as a newline, otherwise it's the same as a linefeed. The difference boils |
| 2618 | * down to whether or not the cursor column is reset. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2619 | * |
| 2620 | * @param {boolean} state The state to set. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2621 | */ |
| 2622 | hterm.Terminal.prototype.setAutoCarriageReturn = function(state) { |
| 2623 | this.options_.autoCarriageReturn = state; |
| 2624 | }; |
| 2625 | |
| 2626 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2627 | * Set the wraparound mode bit. |
| 2628 | * |
| 2629 | * If wraparound mode is on, certain VT commands will allow the cursor to wrap |
| 2630 | * to the start of the following row. Otherwise, the cursor is clamped to the |
| 2631 | * end of the screen and attempts to write past it are ignored. |
| 2632 | * |
| 2633 | * Defaults to on. |
| 2634 | * |
| 2635 | * @param {boolean} state True to set wraparound mode, false to unset. |
| 2636 | */ |
| 2637 | hterm.Terminal.prototype.setWraparound = function(state) { |
| 2638 | this.options_.wraparound = state; |
| 2639 | }; |
| 2640 | |
| 2641 | /** |
| 2642 | * Set the reverse-wraparound mode bit. |
| 2643 | * |
| 2644 | * If wraparound mode is off, certain VT commands will allow the cursor to wrap |
| 2645 | * to the end of the previous row. Otherwise, the cursor is clamped to column |
| 2646 | * 0. |
| 2647 | * |
| 2648 | * Defaults to off. |
| 2649 | * |
| 2650 | * @param {boolean} state True to set reverse-wraparound mode, false to unset. |
| 2651 | */ |
| 2652 | hterm.Terminal.prototype.setReverseWraparound = function(state) { |
| 2653 | this.options_.reverseWraparound = state; |
| 2654 | }; |
| 2655 | |
| 2656 | /** |
| 2657 | * Selects between the primary and alternate screens. |
| 2658 | * |
| 2659 | * If alternate mode is on, the alternate screen is active. Otherwise the |
| 2660 | * primary screen is active. |
| 2661 | * |
| 2662 | * Swapping screens has no effect on the scrollback buffer. |
| 2663 | * |
| 2664 | * Each screen maintains its own cursor position. |
| 2665 | * |
| 2666 | * Defaults to off. |
| 2667 | * |
| 2668 | * @param {boolean} state True to set alternate mode, false to unset. |
| 2669 | */ |
| 2670 | hterm.Terminal.prototype.setAlternateMode = function(state) { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2671 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2672 | this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_; |
| 2673 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2674 | if (this.screen_.rowsArray.length && |
| 2675 | this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) { |
| 2676 | // If the screen changed sizes while we were away, our rowIndexes may |
| 2677 | // be incorrect. |
| 2678 | var offset = this.scrollbackRows_.length; |
| 2679 | var ary = this.screen_.rowsArray; |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2680 | for (var i = 0; i < ary.length; i++) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2681 | ary[i].rowIndex = offset + i; |
| 2682 | } |
| 2683 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2684 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2685 | this.realizeWidth_(this.screenSize.width); |
| 2686 | this.realizeHeight_(this.screenSize.height); |
| 2687 | this.scrollPort_.syncScrollHeight(); |
| 2688 | this.scrollPort_.invalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2689 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2690 | this.restoreCursor(cursor); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2691 | this.scrollPort_.resize(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2692 | }; |
| 2693 | |
| 2694 | /** |
| 2695 | * Set the cursor-blink mode bit. |
| 2696 | * |
| 2697 | * If cursor-blink is on, the cursor will blink when it is visible. Otherwise |
| 2698 | * a visible cursor does not blink. |
| 2699 | * |
| 2700 | * You should make sure to turn blinking off if you're going to dispose of a |
| 2701 | * terminal, otherwise you'll leak a timeout. |
| 2702 | * |
| 2703 | * Defaults to on. |
| 2704 | * |
| 2705 | * @param {boolean} state True to set cursor-blink mode, false to unset. |
| 2706 | */ |
| 2707 | hterm.Terminal.prototype.setCursorBlink = function(state) { |
| 2708 | this.options_.cursorBlink = state; |
| 2709 | |
| 2710 | if (!state && this.timeouts_.cursorBlink) { |
| 2711 | clearTimeout(this.timeouts_.cursorBlink); |
| 2712 | delete this.timeouts_.cursorBlink; |
| 2713 | } |
| 2714 | |
| 2715 | if (this.options_.cursorVisible) |
| 2716 | this.setCursorVisible(true); |
| 2717 | }; |
| 2718 | |
| 2719 | /** |
| 2720 | * Set the cursor-visible mode bit. |
| 2721 | * |
| 2722 | * If cursor-visible is on, the cursor will be visible. Otherwise it will not. |
| 2723 | * |
| 2724 | * Defaults to on. |
| 2725 | * |
| 2726 | * @param {boolean} state True to set cursor-visible mode, false to unset. |
| 2727 | */ |
| 2728 | hterm.Terminal.prototype.setCursorVisible = function(state) { |
| 2729 | this.options_.cursorVisible = state; |
| 2730 | |
| 2731 | if (!state) { |
Brad Town | 1c2afa8 | 2015-03-11 21:36:58 -0700 | [diff] [blame] | 2732 | if (this.timeouts_.cursorBlink) { |
| 2733 | clearTimeout(this.timeouts_.cursorBlink); |
| 2734 | delete this.timeouts_.cursorBlink; |
| 2735 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2736 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2737 | return; |
| 2738 | } |
| 2739 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2740 | this.syncCursorPosition_(); |
| 2741 | |
| 2742 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2743 | |
| 2744 | if (this.options_.cursorBlink) { |
| 2745 | if (this.timeouts_.cursorBlink) |
| 2746 | return; |
| 2747 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 2748 | this.onCursorBlink_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2749 | } else { |
| 2750 | if (this.timeouts_.cursorBlink) { |
| 2751 | clearTimeout(this.timeouts_.cursorBlink); |
| 2752 | delete this.timeouts_.cursorBlink; |
| 2753 | } |
| 2754 | } |
| 2755 | }; |
| 2756 | |
| 2757 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2758 | * Synchronizes the visible cursor and document selection with the current |
| 2759 | * cursor coordinates. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2760 | */ |
| 2761 | hterm.Terminal.prototype.syncCursorPosition_ = function() { |
| 2762 | var topRowIndex = this.scrollPort_.getTopRowIndex(); |
| 2763 | var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex); |
| 2764 | var cursorRowIndex = this.scrollbackRows_.length + |
| 2765 | this.screen_.cursorPosition.row; |
| 2766 | |
| 2767 | if (cursorRowIndex > bottomRowIndex) { |
| 2768 | // Cursor is scrolled off screen, move it outside of the visible area. |
Mike Frysinger | 44c3220 | 2017-08-05 01:13:09 -0400 | [diff] [blame] | 2769 | this.setCssVar('cursor-offset-row', '-1'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2770 | return; |
| 2771 | } |
| 2772 | |
Robert Ginda | b837c05 | 2014-08-11 11:17:51 -0700 | [diff] [blame] | 2773 | if (this.options_.cursorVisible && |
| 2774 | this.cursorNode_.style.display == 'none') { |
| 2775 | // Re-display the terminal cursor if it was hidden by the mouse cursor. |
| 2776 | this.cursorNode_.style.display = ''; |
| 2777 | } |
| 2778 | |
Mike Frysinger | 44c3220 | 2017-08-05 01:13:09 -0400 | [diff] [blame] | 2779 | // Position the cursor using CSS variable math. If we do the math in JS, |
| 2780 | // the float math will end up being more precise than the CSS which will |
| 2781 | // cause the cursor tracking to be off. |
| 2782 | this.setCssVar( |
| 2783 | 'cursor-offset-row', |
| 2784 | `${cursorRowIndex - topRowIndex} + ` + |
| 2785 | `${this.scrollPort_.visibleRowTopMargin}px`); |
| 2786 | this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2787 | |
| 2788 | this.cursorNode_.setAttribute('title', |
Mike Frysinger | 44c3220 | 2017-08-05 01:13:09 -0400 | [diff] [blame] | 2789 | '(' + this.screen_.cursorPosition.column + |
| 2790 | ', ' + this.screen_.cursorPosition.row + |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2791 | ')'); |
| 2792 | |
| 2793 | // Update the caret for a11y purposes. |
| 2794 | var selection = this.document_.getSelection(); |
| 2795 | if (selection && selection.isCollapsed) |
| 2796 | this.screen_.syncSelectionCaret(selection); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2797 | }; |
| 2798 | |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 2799 | /** |
| 2800 | * Adjusts the style of this.cursorNode_ according to the current cursor shape |
| 2801 | * and character cell dimensions. |
| 2802 | */ |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 2803 | hterm.Terminal.prototype.restyleCursor_ = function() { |
| 2804 | var shape = this.cursorShape_; |
| 2805 | |
| 2806 | if (this.cursorNode_.getAttribute('focus') == 'false') { |
| 2807 | // Always show a block cursor when unfocused. |
| 2808 | shape = hterm.Terminal.cursorShape.BLOCK; |
| 2809 | } |
| 2810 | |
| 2811 | var style = this.cursorNode_.style; |
| 2812 | |
| 2813 | switch (shape) { |
| 2814 | case hterm.Terminal.cursorShape.BEAM: |
Mike Frysinger | 66beb0b | 2017-05-30 19:44:51 -0400 | [diff] [blame] | 2815 | style.height = 'var(--hterm-charsize-height)'; |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 2816 | style.backgroundColor = 'transparent'; |
| 2817 | style.borderBottomStyle = null; |
| 2818 | style.borderLeftStyle = 'solid'; |
| 2819 | break; |
| 2820 | |
| 2821 | case hterm.Terminal.cursorShape.UNDERLINE: |
| 2822 | style.height = this.scrollPort_.characterSize.baseline + 'px'; |
| 2823 | style.backgroundColor = 'transparent'; |
| 2824 | style.borderBottomStyle = 'solid'; |
| 2825 | // correct the size to put it exactly at the baseline |
| 2826 | style.borderLeftStyle = null; |
| 2827 | break; |
| 2828 | |
| 2829 | default: |
Mike Frysinger | 66beb0b | 2017-05-30 19:44:51 -0400 | [diff] [blame] | 2830 | style.height = 'var(--hterm-charsize-height)'; |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 2831 | style.backgroundColor = this.cursorColor_; |
| 2832 | style.borderBottomStyle = null; |
| 2833 | style.borderLeftStyle = null; |
| 2834 | break; |
| 2835 | } |
| 2836 | }; |
| 2837 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2838 | /** |
| 2839 | * Synchronizes the visible cursor with the current cursor coordinates. |
| 2840 | * |
| 2841 | * The sync will happen asynchronously, soon after the call stack winds down. |
| 2842 | * Multiple calls will be coalesced into a single sync. |
| 2843 | */ |
| 2844 | hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() { |
| 2845 | if (this.timeouts_.syncCursor) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2846 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2847 | |
| 2848 | var self = this; |
| 2849 | this.timeouts_.syncCursor = setTimeout(function() { |
| 2850 | self.syncCursorPosition_(); |
| 2851 | delete self.timeouts_.syncCursor; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2852 | }, 0); |
| 2853 | }; |
| 2854 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2855 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2856 | * Show or hide the zoom warning. |
| 2857 | * |
| 2858 | * The zoom warning is a message warning the user that their browser zoom must |
| 2859 | * be set to 100% in order for hterm to function properly. |
| 2860 | * |
| 2861 | * @param {boolean} state True to show the message, false to hide it. |
| 2862 | */ |
| 2863 | hterm.Terminal.prototype.showZoomWarning_ = function(state) { |
| 2864 | if (!this.zoomWarningNode_) { |
| 2865 | if (!state) |
| 2866 | return; |
| 2867 | |
| 2868 | this.zoomWarningNode_ = this.document_.createElement('div'); |
Mike Frysinger | d826f1a | 2017-07-06 16:20:06 -0400 | [diff] [blame] | 2869 | this.zoomWarningNode_.id = 'hterm:zoom-warning'; |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2870 | this.zoomWarningNode_.style.cssText = ( |
| 2871 | 'color: black;' + |
| 2872 | 'background-color: #ff2222;' + |
| 2873 | 'font-size: large;' + |
| 2874 | 'border-radius: 8px;' + |
| 2875 | 'opacity: 0.75;' + |
| 2876 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2877 | 'top: 0.5em;' + |
| 2878 | 'right: 1.2em;' + |
| 2879 | 'position: absolute;' + |
| 2880 | '-webkit-text-size-adjust: none;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 2881 | '-webkit-user-select: none;' + |
| 2882 | '-moz-text-size-adjust: none;' + |
| 2883 | '-moz-user-select: none;'); |
Mike Frysinger | 4c0c5e0 | 2016-03-12 23:11:25 -0500 | [diff] [blame] | 2884 | |
| 2885 | this.zoomWarningNode_.addEventListener('click', function(e) { |
| 2886 | this.parentNode.removeChild(this); |
| 2887 | }); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 2890 | this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences( |
| 2891 | hterm.zoomWarningMessage, |
| 2892 | [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]); |
| 2893 | |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2894 | this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2895 | |
| 2896 | if (state) { |
| 2897 | if (!this.zoomWarningNode_.parentNode) |
| 2898 | this.div_.parentNode.appendChild(this.zoomWarningNode_); |
| 2899 | } else if (this.zoomWarningNode_.parentNode) { |
| 2900 | this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_); |
| 2901 | } |
| 2902 | }; |
| 2903 | |
| 2904 | /** |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2905 | * Show the terminal overlay for a given amount of time. |
| 2906 | * |
| 2907 | * The terminal overlay appears in inverse video in a large font, centered |
| 2908 | * over the terminal. You should probably keep the overlay message brief, |
| 2909 | * since it's in a large font and you probably aren't going to check the size |
| 2910 | * of the terminal first. |
| 2911 | * |
| 2912 | * @param {string} msg The text (not HTML) message to display in the overlay. |
| 2913 | * @param {number} opt_timeout The amount of time to wait before fading out |
| 2914 | * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay |
| 2915 | * stay up forever (or until the next overlay). |
| 2916 | */ |
| 2917 | hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2918 | if (!this.overlayNode_) { |
| 2919 | if (!this.div_) |
| 2920 | return; |
| 2921 | |
| 2922 | this.overlayNode_ = this.document_.createElement('div'); |
| 2923 | this.overlayNode_.style.cssText = ( |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2924 | 'border-radius: 15px;' + |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2925 | 'font-size: xx-large;' + |
| 2926 | 'opacity: 0.75;' + |
| 2927 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2928 | 'position: absolute;' + |
| 2929 | '-webkit-user-select: none;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 2930 | '-webkit-transition: opacity 180ms ease-in;' + |
| 2931 | '-moz-user-select: none;' + |
| 2932 | '-moz-transition: opacity 180ms ease-in;'); |
Robert Ginda | 70926e4 | 2013-11-25 14:56:36 -0800 | [diff] [blame] | 2933 | |
| 2934 | this.overlayNode_.addEventListener('mousedown', function(e) { |
| 2935 | e.preventDefault(); |
| 2936 | e.stopPropagation(); |
| 2937 | }, true); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2938 | } |
| 2939 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2940 | this.overlayNode_.style.color = this.prefs_.get('background-color'); |
| 2941 | this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color'); |
| 2942 | this.overlayNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2943 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2944 | this.overlayNode_.textContent = msg; |
| 2945 | this.overlayNode_.style.opacity = '0.75'; |
| 2946 | |
| 2947 | if (!this.overlayNode_.parentNode) |
| 2948 | this.div_.appendChild(this.overlayNode_); |
| 2949 | |
Robert Ginda | 9776928 | 2013-02-01 15:30:30 -0800 | [diff] [blame] | 2950 | var divSize = hterm.getClientSize(this.div_); |
| 2951 | var overlaySize = hterm.getClientSize(this.overlayNode_); |
| 2952 | |
Robert Ginda | 8a59f76 | 2014-07-23 11:29:55 -0700 | [diff] [blame] | 2953 | this.overlayNode_.style.top = |
| 2954 | (divSize.height - overlaySize.height) / 2 + 'px'; |
Robert Ginda | 9776928 | 2013-02-01 15:30:30 -0800 | [diff] [blame] | 2955 | this.overlayNode_.style.left = (divSize.width - overlaySize.width - |
Robert Ginda | 8a59f76 | 2014-07-23 11:29:55 -0700 | [diff] [blame] | 2956 | this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px'; |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2957 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2958 | if (this.overlayTimeout_) |
| 2959 | clearTimeout(this.overlayTimeout_); |
| 2960 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2961 | if (opt_timeout === null) |
| 2962 | return; |
| 2963 | |
Mike Frysinger | b6cfded | 2017-09-18 00:39:31 -0400 | [diff] [blame] | 2964 | this.overlayTimeout_ = setTimeout(() => { |
| 2965 | this.overlayNode_.style.opacity = '0'; |
| 2966 | this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200); |
| 2967 | }, opt_timeout || 1500); |
| 2968 | }; |
| 2969 | |
| 2970 | /** |
| 2971 | * Hide the terminal overlay immediately. |
| 2972 | * |
| 2973 | * Useful when we show an overlay for an event with an unknown end time. |
| 2974 | */ |
| 2975 | hterm.Terminal.prototype.hideOverlay = function() { |
| 2976 | if (this.overlayTimeout_) |
| 2977 | clearTimeout(this.overlayTimeout_); |
| 2978 | this.overlayTimeout_ = null; |
| 2979 | |
| 2980 | if (this.overlayNode_.parentNode) |
| 2981 | this.overlayNode_.parentNode.removeChild(this.overlayNode_); |
| 2982 | this.overlayNode_.style.opacity = '0.75'; |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2983 | }; |
| 2984 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2985 | /** |
| 2986 | * Paste from the system clipboard to the terminal. |
| 2987 | */ |
| 2988 | hterm.Terminal.prototype.paste = function() { |
Mike Frysinger | 4628ad2 | 2017-07-20 02:44:20 -0400 | [diff] [blame] | 2989 | return hterm.pasteFromClipboard(this.document_); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2990 | }; |
| 2991 | |
| 2992 | /** |
| 2993 | * Copy a string to the system clipboard. |
| 2994 | * |
| 2995 | * Note: If there is a selected range in the terminal, it'll be cleared. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2996 | * |
| 2997 | * @param {string} str The string to copy. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2998 | */ |
| 2999 | hterm.Terminal.prototype.copyStringToClipboard = function(str) { |
Robert Ginda | 4e4d42c | 2014-03-04 14:07:23 -0800 | [diff] [blame] | 3000 | if (this.prefs_.get('enable-clipboard-notice')) |
| 3001 | setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200); |
| 3002 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3003 | var copySource = this.document_.createElement('pre'); |
Mike Frysinger | d826f1a | 2017-07-06 16:20:06 -0400 | [diff] [blame] | 3004 | copySource.id = 'hterm:copy-to-clipboard-source'; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3005 | copySource.textContent = str; |
| 3006 | copySource.style.cssText = ( |
| 3007 | '-webkit-user-select: text;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 3008 | '-moz-user-select: text;' + |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3009 | 'position: absolute;' + |
| 3010 | 'top: -99px'); |
| 3011 | |
| 3012 | this.document_.body.appendChild(copySource); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 3013 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3014 | var selection = this.document_.getSelection(); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 3015 | var anchorNode = selection.anchorNode; |
| 3016 | var anchorOffset = selection.anchorOffset; |
| 3017 | var focusNode = selection.focusNode; |
| 3018 | var focusOffset = selection.focusOffset; |
| 3019 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3020 | selection.selectAllChildren(copySource); |
| 3021 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3022 | hterm.copySelectionToClipboard(this.document_); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3023 | |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 3024 | // IE doesn't support selection.extend. This means that the selection |
| 3025 | // won't return on IE. |
Robert Ginda | 6f4f0aa | 2014-07-28 10:21:39 -0700 | [diff] [blame] | 3026 | if (selection.extend) { |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 3027 | selection.collapse(anchorNode, anchorOffset); |
| 3028 | selection.extend(focusNode, focusOffset); |
| 3029 | } |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 3030 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3031 | copySource.parentNode.removeChild(copySource); |
| 3032 | }; |
| 3033 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3034 | /** |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3035 | * Display an image. |
| 3036 | * |
| 3037 | * @param {Object} options The image to display. |
| 3038 | * @param {string=} options.name A human readable string for the image. |
| 3039 | * @param {string|number=} options.size The size (in bytes). |
| 3040 | * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect. |
| 3041 | * @param {boolean=} options.inline Whether to display the image inline. |
| 3042 | * @param {string|number=} options.width The width of the image. |
| 3043 | * @param {string|number=} options.height The height of the image. |
| 3044 | * @param {string=} options.align Direction to align the image. |
| 3045 | * @param {string} options.uri The source URI for the image. |
Mike Frysinger | 3a62a2f | 2018-03-14 21:11:45 -0700 | [diff] [blame] | 3046 | * @param {function=} onLoad Callback when loading finishes. |
| 3047 | * @param {function(Event)=} onError Callback when loading fails. |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3048 | */ |
Mike Frysinger | 3a62a2f | 2018-03-14 21:11:45 -0700 | [diff] [blame] | 3049 | hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) { |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3050 | // Make sure we're actually given a resource to display. |
| 3051 | if (options.uri === undefined) |
| 3052 | return; |
| 3053 | |
| 3054 | // Set up the defaults to simplify code below. |
| 3055 | if (!options.name) |
| 3056 | options.name = ''; |
| 3057 | |
| 3058 | // Has the user approved image display yet? |
| 3059 | if (this.allowImagesInline !== true) { |
| 3060 | this.newLine(); |
| 3061 | const row = this.getRowNode(this.scrollbackRows_.length + |
| 3062 | this.getCursorRow() - 1); |
| 3063 | |
| 3064 | if (this.allowImagesInline === false) { |
| 3065 | row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [], |
| 3066 | 'Inline Images Disabled'); |
| 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | // Show a prompt. |
| 3071 | let button; |
| 3072 | const span = this.document_.createElement('span'); |
| 3073 | span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images'); |
| 3074 | span.style.fontWeight = 'bold'; |
| 3075 | span.style.borderWidth = '1px'; |
| 3076 | span.style.borderStyle = 'dashed'; |
| 3077 | button = this.document_.createElement('span'); |
| 3078 | button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block'); |
| 3079 | button.style.marginLeft = '1em'; |
| 3080 | button.style.borderWidth = '1px'; |
| 3081 | button.style.borderStyle = 'solid'; |
| 3082 | button.addEventListener('click', () => { |
| 3083 | this.prefs_.set('allow-images-inline', false); |
| 3084 | }); |
| 3085 | span.appendChild(button); |
| 3086 | button = this.document_.createElement('span'); |
| 3087 | button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [], |
| 3088 | 'allow this session'); |
| 3089 | button.style.marginLeft = '1em'; |
| 3090 | button.style.borderWidth = '1px'; |
| 3091 | button.style.borderStyle = 'solid'; |
| 3092 | button.addEventListener('click', () => { |
| 3093 | this.allowImagesInline = true; |
| 3094 | }); |
| 3095 | span.appendChild(button); |
| 3096 | button = this.document_.createElement('span'); |
| 3097 | button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow'); |
| 3098 | button.style.marginLeft = '1em'; |
| 3099 | button.style.borderWidth = '1px'; |
| 3100 | button.style.borderStyle = 'solid'; |
| 3101 | button.addEventListener('click', () => { |
| 3102 | this.prefs_.set('allow-images-inline', true); |
| 3103 | }); |
| 3104 | span.appendChild(button); |
| 3105 | |
| 3106 | row.appendChild(span); |
| 3107 | return; |
| 3108 | } |
| 3109 | |
| 3110 | // See if we should show this object directly, or download it. |
| 3111 | if (options.inline) { |
| 3112 | const io = this.io.push(); |
| 3113 | io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name], |
| 3114 | 'Loading $1 ...'), null); |
| 3115 | |
| 3116 | // While we're loading the image, eat all the user's input. |
| 3117 | io.onVTKeystroke = io.sendString = () => {}; |
| 3118 | |
| 3119 | // Initialize this new image. |
| 3120 | const img = this.document_.createElement('img'); |
| 3121 | img.src = options.uri; |
| 3122 | img.title = img.alt = options.name; |
| 3123 | |
| 3124 | // Attach the image to the page to let it load/render. It won't stay here. |
| 3125 | // This is needed so it's visible and the DOM can calculate the height. If |
| 3126 | // the image is hidden or not in the DOM, the height is always 0. |
| 3127 | this.document_.body.appendChild(img); |
| 3128 | |
| 3129 | // Wait for the image to finish loading before we try moving it to the |
| 3130 | // right place in the terminal. |
| 3131 | img.onload = () => { |
| 3132 | // Now that we have the image dimensions, figure out how to show it. |
| 3133 | img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill'; |
| 3134 | img.style.maxWidth = `${this.document_.body.clientWidth}px`; |
| 3135 | img.style.maxHeight = `${this.document_.body.clientHeight}px`; |
| 3136 | |
| 3137 | // Parse a width/height specification. |
| 3138 | const parseDim = (dim, maxDim, cssVar) => { |
| 3139 | if (!dim || dim == 'auto') |
| 3140 | return ''; |
| 3141 | |
| 3142 | const ary = dim.match(/^([0-9]+)(px|%)?$/); |
| 3143 | if (ary) { |
| 3144 | if (ary[2] == '%') |
| 3145 | return maxDim * parseInt(ary[1]) / 100 + 'px'; |
| 3146 | else if (ary[2] == 'px') |
| 3147 | return dim; |
| 3148 | else |
| 3149 | return `calc(${dim} * var(${cssVar}))`; |
| 3150 | } |
| 3151 | |
| 3152 | return ''; |
| 3153 | }; |
| 3154 | img.style.width = |
| 3155 | parseDim(options.width, this.document_.body.clientWidth, |
| 3156 | '--hterm-charsize-width'); |
| 3157 | img.style.height = |
| 3158 | parseDim(options.height, this.document_.body.clientHeight, |
| 3159 | '--hterm-charsize-height'); |
| 3160 | |
| 3161 | // Figure out how many rows the image occupies, then add that many. |
| 3162 | // XXX: This count will be inaccurate if the font size changes on us. |
| 3163 | const padRows = Math.ceil(img.clientHeight / |
| 3164 | this.scrollPort_.characterSize.height); |
| 3165 | for (let i = 0; i < padRows; ++i) |
| 3166 | this.newLine(); |
| 3167 | |
| 3168 | // Update the max height in case the user shrinks the character size. |
| 3169 | img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`; |
| 3170 | |
| 3171 | // Move the image to the last row. This way when we scroll up, it doesn't |
| 3172 | // disappear when the first row gets clipped. It will disappear when we |
| 3173 | // scroll down and the last row is clipped ... |
| 3174 | this.document_.body.removeChild(img); |
| 3175 | // Create a wrapper node so we can do an absolute in a relative position. |
| 3176 | // This helps with rounding errors between JS & CSS counts. |
| 3177 | const div = this.document_.createElement('div'); |
| 3178 | div.style.position = 'relative'; |
| 3179 | div.style.textAlign = options.align; |
| 3180 | img.style.position = 'absolute'; |
| 3181 | img.style.bottom = 'calc(0px - var(--hterm-charsize-height))'; |
| 3182 | div.appendChild(img); |
| 3183 | const row = this.getRowNode(this.scrollbackRows_.length + |
| 3184 | this.getCursorRow() - 1); |
| 3185 | row.appendChild(div); |
| 3186 | |
| 3187 | io.hideOverlay(); |
| 3188 | io.pop(); |
Mike Frysinger | 3a62a2f | 2018-03-14 21:11:45 -0700 | [diff] [blame] | 3189 | |
| 3190 | if (onLoad) |
| 3191 | onLoad(); |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3192 | }; |
| 3193 | |
| 3194 | // If we got a malformed image, give up. |
| 3195 | img.onerror = (e) => { |
| 3196 | this.document_.body.removeChild(img); |
| 3197 | io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name], |
Mike Frysinger | e14a8c4 | 2018-03-10 00:17:30 -0800 | [diff] [blame] | 3198 | 'Loading $1 failed')); |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3199 | io.pop(); |
Mike Frysinger | 3a62a2f | 2018-03-14 21:11:45 -0700 | [diff] [blame] | 3200 | |
| 3201 | if (onError) |
| 3202 | onError(e); |
Mike Frysinger | 8c5a0a4 | 2017-04-21 11:38:27 -0400 | [diff] [blame] | 3203 | }; |
| 3204 | } else { |
| 3205 | // We can't use chrome.downloads.download as that requires "downloads" |
| 3206 | // permissions, and that works only in extensions, not apps. |
| 3207 | const a = this.document_.createElement('a'); |
| 3208 | a.href = options.uri; |
| 3209 | a.download = options.name; |
| 3210 | this.document_.body.appendChild(a); |
| 3211 | a.click(); |
| 3212 | a.remove(); |
| 3213 | } |
| 3214 | }; |
| 3215 | |
| 3216 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3217 | * Returns the selected text, or null if no text is selected. |
| 3218 | * |
| 3219 | * @return {string|null} |
| 3220 | */ |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3221 | hterm.Terminal.prototype.getSelectionText = function() { |
| 3222 | var selection = this.scrollPort_.selection; |
| 3223 | selection.sync(); |
| 3224 | |
| 3225 | if (selection.isCollapsed) |
| 3226 | return null; |
| 3227 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3228 | // Start offset measures from the beginning of the line. |
| 3229 | var startOffset = selection.startOffset; |
| 3230 | var node = selection.startNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 3231 | |
Raymes Khoury | 334625a | 2018-06-25 10:29:40 +1000 | [diff] [blame^] | 3232 | // If an x-row isn't selected, |node| will be null. |
| 3233 | if (!node) |
| 3234 | return null; |
| 3235 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3236 | if (node.nodeName != 'X-ROW') { |
| 3237 | // If the selection doesn't start on an x-row node, then it must be |
| 3238 | // somewhere inside the x-row. Add any characters from previous siblings |
| 3239 | // into the start offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 3240 | |
| 3241 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 3242 | // If node is the text node in a styled span, move up to the span node. |
| 3243 | node = node.parentNode; |
| 3244 | } |
| 3245 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3246 | while (node.previousSibling) { |
| 3247 | node = node.previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 3248 | startOffset += hterm.TextAttributes.nodeWidth(node); |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3249 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | // End offset measures from the end of the line. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 3253 | var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) - |
| 3254 | selection.endOffset); |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 3255 | node = selection.endNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 3256 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3257 | if (node.nodeName != 'X-ROW') { |
| 3258 | // If the selection doesn't end on an x-row node, then it must be |
| 3259 | // somewhere inside the x-row. Add any characters from following siblings |
| 3260 | // into the end offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 3261 | |
| 3262 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 3263 | // If node is the text node in a styled span, move up to the span node. |
| 3264 | node = node.parentNode; |
| 3265 | } |
| 3266 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3267 | while (node.nextSibling) { |
| 3268 | node = node.nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 3269 | endOffset += hterm.TextAttributes.nodeWidth(node); |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 3270 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | var rv = this.getRowsText(selection.startRow.rowIndex, |
| 3274 | selection.endRow.rowIndex + 1); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 3275 | return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3276 | }; |
| 3277 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3278 | /** |
| 3279 | * Copy the current selection to the system clipboard, then clear it after a |
| 3280 | * short delay. |
| 3281 | */ |
| 3282 | hterm.Terminal.prototype.copySelectionToClipboard = function() { |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3283 | var text = this.getSelectionText(); |
| 3284 | if (text != null) |
| 3285 | this.copyStringToClipboard(text); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3286 | }; |
| 3287 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 3288 | hterm.Terminal.prototype.overlaySize = function() { |
| 3289 | this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height); |
| 3290 | }; |
| 3291 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3292 | /** |
| 3293 | * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected. |
| 3294 | * |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 3295 | * @param {string} string The VT string representing the keystroke, in UTF-16. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3296 | */ |
| 3297 | hterm.Terminal.prototype.onVTKeystroke = function(string) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 3298 | if (this.scrollOnKeystroke_) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3299 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 3300 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 3301 | this.io.onVTKeystroke(this.keyboard.encode(string)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3302 | }; |
| 3303 | |
| 3304 | /** |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3305 | * Open the selected url. |
| 3306 | */ |
| 3307 | hterm.Terminal.prototype.openSelectedUrl_ = function() { |
| 3308 | var str = this.getSelectionText(); |
| 3309 | |
| 3310 | // If there is no selection, try and expand wherever they clicked. |
| 3311 | if (str == null) { |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 3312 | this.screen_.expandSelectionForUrl(this.document_.getSelection()); |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3313 | str = this.getSelectionText(); |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 3314 | |
| 3315 | // If clicking in empty space, return. |
| 3316 | if (str == null) |
| 3317 | return; |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | // Make sure URL is valid before opening. |
| 3321 | if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0) |
| 3322 | return; |
Mike Frysinger | 4347262 | 2017-06-26 18:11:07 -0400 | [diff] [blame] | 3323 | |
| 3324 | // If the URI isn't anchored, it'll open relative to the extension. |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3325 | // We have no way of knowing the correct schema, so assume http. |
Mike Frysinger | 4347262 | 2017-06-26 18:11:07 -0400 | [diff] [blame] | 3326 | if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) { |
| 3327 | // We have to whitelist a few protocols that lack authorities and thus |
| 3328 | // never use the //. Like mailto. |
| 3329 | switch (str.split(':', 1)[0]) { |
| 3330 | case 'mailto': |
| 3331 | break; |
| 3332 | default: |
| 3333 | str = 'http://' + str; |
| 3334 | break; |
| 3335 | } |
| 3336 | } |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3337 | |
Mike Frysinger | 720fa83 | 2017-10-23 01:15:52 -0400 | [diff] [blame] | 3338 | hterm.openUrl(str); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 3339 | }; |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3340 | |
Mike Frysinger | 02ded6d | 2018-06-21 14:25:20 -0400 | [diff] [blame] | 3341 | /** |
| 3342 | * Manage the automatic mouse hiding behavior while typing. |
| 3343 | * |
| 3344 | * @param {boolean=} v Whether to enable automatic hiding. |
| 3345 | */ |
| 3346 | hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) { |
| 3347 | // Since Chrome OS & macOS do this by default everywhere, we don't need to. |
| 3348 | // Linux & Windows seem to leave this to specific applications to manage. |
| 3349 | if (v === null) |
| 3350 | v = (hterm.os != 'cros' && hterm.os != 'mac'); |
| 3351 | |
| 3352 | this.mouseHideWhileTyping_ = !!v; |
| 3353 | }; |
| 3354 | |
| 3355 | /** |
| 3356 | * Handler for monitoring user keyboard activity. |
| 3357 | * |
| 3358 | * This isn't for processing the keystrokes directly, but for updating any |
| 3359 | * state that might toggle based on the user using the keyboard at all. |
| 3360 | * |
| 3361 | * @param {KeyboardEvent} e The keyboard event that triggered us. |
| 3362 | */ |
| 3363 | hterm.Terminal.prototype.onKeyboardActivity_ = function(e) { |
| 3364 | // When the user starts typing, hide the mouse cursor. |
| 3365 | if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) |
| 3366 | this.setCssVar('mouse-cursor-style', 'none'); |
| 3367 | }; |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3368 | |
| 3369 | /** |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3370 | * Add the terminalRow and terminalColumn properties to mouse events and |
| 3371 | * then forward on to onMouse(). |
| 3372 | * |
| 3373 | * The terminalRow and terminalColumn properties contain the (row, column) |
| 3374 | * coordinates for the mouse event. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3375 | * |
| 3376 | * @param {Event} e The mouse event to handle. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3377 | */ |
| 3378 | hterm.Terminal.prototype.onMouse_ = function(e) { |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 3379 | if (e.processedByTerminalHandler_) { |
| 3380 | // We register our event handlers on the document, as well as the cursor |
| 3381 | // and the scroll blocker. Mouse events that occur on the cursor or |
| 3382 | // scroll blocker will also appear on the document, but we don't want to |
| 3383 | // process them twice. |
| 3384 | // |
| 3385 | // We can't just prevent bubbling because that has other side effects, so |
| 3386 | // we decorate the event object with this property instead. |
| 3387 | return; |
| 3388 | } |
| 3389 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3390 | var reportMouseEvents = (!this.defeatMouseReports_ && |
| 3391 | this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED); |
| 3392 | |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 3393 | e.processedByTerminalHandler_ = true; |
| 3394 | |
Mike Frysinger | 02ded6d | 2018-06-21 14:25:20 -0400 | [diff] [blame] | 3395 | // Handle auto hiding of mouse cursor while typing. |
| 3396 | if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) { |
| 3397 | // Make sure the mouse cursor is visible. |
| 3398 | this.syncMouseStyle(); |
| 3399 | // This debounce isn't perfect, but should work well enough for such a |
| 3400 | // simple implementation. If the user moved the mouse, we enabled this |
| 3401 | // debounce, and then moved the mouse just before the timeout, we wouldn't |
| 3402 | // debounce that later movement. |
| 3403 | this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000); |
| 3404 | } |
| 3405 | |
Robert Ginda | eda48db | 2014-07-17 09:25:30 -0700 | [diff] [blame] | 3406 | // One based row/column stored on the mouse event. |
| 3407 | e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) / |
| 3408 | this.scrollPort_.characterSize.height) + 1; |
| 3409 | e.terminalColumn = parseInt(e.clientX / |
| 3410 | this.scrollPort_.characterSize.width) + 1; |
| 3411 | |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3412 | if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) { |
| 3413 | // Mousedown in the scrollbar area. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 3414 | return; |
| 3415 | } |
| 3416 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3417 | if (this.options_.cursorVisible && !reportMouseEvents) { |
Robert Ginda | b837c05 | 2014-08-11 11:17:51 -0700 | [diff] [blame] | 3418 | // If the cursor is visible and we're not sending mouse events to the |
| 3419 | // host app, then we want to hide the terminal cursor when the mouse |
| 3420 | // cursor is over top. This keeps the terminal cursor from interfering |
| 3421 | // with local text selection. |
Robert Ginda | eda48db | 2014-07-17 09:25:30 -0700 | [diff] [blame] | 3422 | if (e.terminalRow - 1 == this.screen_.cursorPosition.row && |
| 3423 | e.terminalColumn - 1 == this.screen_.cursorPosition.column) { |
| 3424 | this.cursorNode_.style.display = 'none'; |
| 3425 | } else if (this.cursorNode_.style.display == 'none') { |
| 3426 | this.cursorNode_.style.display = ''; |
| 3427 | } |
| 3428 | } |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3429 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3430 | if (e.type == 'mousedown') { |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3431 | if (e.altKey || !reportMouseEvents) { |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3432 | // If VT mouse reporting is disabled, or has been defeated with |
| 3433 | // alt-mousedown, then the mouse will act on the local selection. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3434 | this.defeatMouseReports_ = true; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3435 | this.setSelectionEnabled(true); |
| 3436 | } else { |
| 3437 | // Otherwise we defer ownership of the mouse to the VT. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3438 | this.defeatMouseReports_ = false; |
Robert Ginda | 3ae3782 | 2014-05-15 13:05:35 -0700 | [diff] [blame] | 3439 | this.document_.getSelection().collapseToEnd(); |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3440 | this.setSelectionEnabled(false); |
| 3441 | e.preventDefault(); |
| 3442 | } |
| 3443 | } |
| 3444 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3445 | if (!reportMouseEvents) { |
John Lin | 2aad22e | 2018-03-16 13:58:11 +0800 | [diff] [blame] | 3446 | if (e.type == 'dblclick') { |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3447 | this.screen_.expandSelection(this.document_.getSelection()); |
John Lin | 2aad22e | 2018-03-16 13:58:11 +0800 | [diff] [blame] | 3448 | if (this.copyOnSelect) |
| 3449 | this.copySelectionToClipboard(this.document_); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
Mihir Nimbalkar | 467fd8b | 2017-07-12 12:14:16 -0700 | [diff] [blame] | 3452 | if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) { |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3453 | // Debounce this event with the dblclick event. If you try to doubleclick |
| 3454 | // a URL to open it, Chrome will fire click then dblclick, but we won't |
| 3455 | // have expanded the selection text at the first click event. |
| 3456 | clearTimeout(this.timeouts_.openUrl); |
| 3457 | this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this), |
| 3458 | 500); |
| 3459 | return; |
| 3460 | } |
| 3461 | |
Mike Frysinger | 847577f | 2017-05-23 23:25:57 -0400 | [diff] [blame] | 3462 | if (e.type == 'mousedown') { |
| 3463 | if ((this.mouseRightClickPaste && e.button == 2 /* right button */) || |
Mike Frysinger | 2edd361 | 2017-05-24 00:54:39 -0400 | [diff] [blame] | 3464 | e.button == this.mousePasteButton) { |
Mike Frysinger | 4628ad2 | 2017-07-20 02:44:20 -0400 | [diff] [blame] | 3465 | if (!this.paste()) |
Mike Frysinger | 05a57f0 | 2017-08-27 17:48:55 -0400 | [diff] [blame] | 3466 | console.warn('Could not paste manually due to web restrictions'); |
Mike Frysinger | 847577f | 2017-05-23 23:25:57 -0400 | [diff] [blame] | 3467 | } |
| 3468 | } |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3469 | |
Mike Frysinger | 2edd361 | 2017-05-24 00:54:39 -0400 | [diff] [blame] | 3470 | if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect && |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3471 | !this.document_.getSelection().isCollapsed) { |
Robert Ginda | 15ed490 | 2016-07-12 10:43:22 -0700 | [diff] [blame] | 3472 | this.copySelectionToClipboard(this.document_); |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | if ((e.type == 'mousemove' || e.type == 'mouseup') && |
| 3476 | this.scrollBlockerNode_.engaged) { |
| 3477 | // Disengage the scroll-blocker after one of these events. |
| 3478 | this.scrollBlockerNode_.engaged = false; |
| 3479 | this.scrollBlockerNode_.style.top = '-99px'; |
| 3480 | } |
| 3481 | |
Mike Frysinger | 3c9fa07 | 2017-07-13 10:21:13 -0400 | [diff] [blame] | 3482 | // Emulate arrow key presses via scroll wheel events. |
| 3483 | if (this.scrollWheelArrowKeys_ && !e.shiftKey && |
| 3484 | this.keyboard.applicationCursor && !this.isPrimaryScreen()) { |
Mike Frysinger | c3030a8 | 2017-05-29 14:16:11 -0400 | [diff] [blame] | 3485 | if (e.type == 'wheel') { |
| 3486 | var delta = this.scrollPort_.scrollWheelDelta(e); |
| 3487 | var lines = lib.f.smartFloorDivide( |
| 3488 | Math.abs(delta), this.scrollPort_.characterSize.height); |
| 3489 | |
| 3490 | var data = '\x1bO' + (delta < 0 ? 'B' : 'A'); |
| 3491 | this.io.sendString(data.repeat(lines)); |
| 3492 | |
| 3493 | e.preventDefault(); |
| 3494 | } |
| 3495 | } |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3496 | } else /* if (this.reportMouseEvents) */ { |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3497 | if (!this.scrollBlockerNode_.engaged) { |
| 3498 | if (e.type == 'mousedown') { |
| 3499 | // Move the scroll-blocker into place if we want to keep the scrollport |
| 3500 | // from scrolling. |
| 3501 | this.scrollBlockerNode_.engaged = true; |
| 3502 | this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px'; |
| 3503 | this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px'; |
| 3504 | } else if (e.type == 'mousemove') { |
| 3505 | // Oh. This means that drag-scroll was disabled AFTER the mouse down, |
| 3506 | // in which case it's too late to engage the scroll-blocker. |
Robert Ginda | 3ae3782 | 2014-05-15 13:05:35 -0700 | [diff] [blame] | 3507 | this.document_.getSelection().collapseToEnd(); |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3508 | e.preventDefault(); |
| 3509 | } |
| 3510 | } |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3511 | |
| 3512 | this.onMouse(e); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3515 | if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) { |
| 3516 | // Restore this on mouseup in case it was temporarily defeated with a |
| 3517 | // alt-mousedown. Only do this when the selection is empty so that |
| 3518 | // we don't immediately kill the users selection. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3519 | this.defeatMouseReports_ = false; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3520 | } |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3521 | }; |
| 3522 | |
| 3523 | /** |
| 3524 | * Clients should override this if they care to know about mouse events. |
| 3525 | * |
| 3526 | * The event parameter will be a normal DOM mouse click event with additional |
| 3527 | * 'terminalRow' and 'terminalColumn' properties. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3528 | * |
| 3529 | * @param {Event} e The mouse event to handle. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3530 | */ |
| 3531 | hterm.Terminal.prototype.onMouse = function(e) { }; |
| 3532 | |
| 3533 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3534 | * React when focus changes. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3535 | * |
| 3536 | * @param {boolean} focused True if focused, false otherwise. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3537 | */ |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 3538 | hterm.Terminal.prototype.onFocusChange_ = function(focused) { |
| 3539 | this.cursorNode_.setAttribute('focus', focused); |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 3540 | this.restyleCursor_(); |
Gabriel Holodak | e8a09be | 2017-10-10 01:07:11 -0400 | [diff] [blame] | 3541 | |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 3542 | if (this.reportFocus) |
| 3543 | this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O'); |
Gabriel Holodak | e8a09be | 2017-10-10 01:07:11 -0400 | [diff] [blame] | 3544 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3545 | if (focused === true) |
| 3546 | this.closeBellNotifications_(); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3547 | }; |
| 3548 | |
| 3549 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3550 | * React when the ScrollPort is scrolled. |
| 3551 | */ |
| 3552 | hterm.Terminal.prototype.onScroll_ = function() { |
| 3553 | this.scheduleSyncCursorPosition_(); |
| 3554 | }; |
| 3555 | |
| 3556 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3557 | * React when text is pasted into the scrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3558 | * |
| 3559 | * @param {Event} e The DOM paste event to handle. |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3560 | */ |
| 3561 | hterm.Terminal.prototype.onPaste_ = function(e) { |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 3562 | var data = e.text.replace(/\n/mg, '\r'); |
Connor Hegarty | f525ceb | 2014-09-03 13:36:03 -0700 | [diff] [blame] | 3563 | data = this.keyboard.encode(data); |
Mike Frysinger | e8c32c8 | 2018-03-11 14:57:28 -0700 | [diff] [blame] | 3564 | if (this.options_.bracketedPaste) { |
| 3565 | // We strip out most escape sequences as they can cause issues (like |
| 3566 | // inserting an \x1b[201~ midstream). We pass through whitespace |
| 3567 | // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r. |
| 3568 | // This matches xterm behavior. |
| 3569 | const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, ''); |
| 3570 | data = '\x1b[200~' + filter(data) + '\x1b[201~'; |
| 3571 | } |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 3572 | |
| 3573 | this.io.sendString(data); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3574 | }; |
| 3575 | |
| 3576 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3577 | * React when the user tries to copy from the scrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3578 | * |
| 3579 | * @param {Event} e The DOM copy event. |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3580 | */ |
| 3581 | hterm.Terminal.prototype.onCopy_ = function(e) { |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 3582 | if (!this.useDefaultWindowCopy) { |
| 3583 | e.preventDefault(); |
| 3584 | setTimeout(this.copySelectionToClipboard.bind(this), 0); |
| 3585 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3586 | }; |
| 3587 | |
| 3588 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3589 | * React when the ScrollPort is resized. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 3590 | * |
| 3591 | * Note: This function should not directly contain code that alters the internal |
| 3592 | * state of the terminal. That kind of code belongs in realizeWidth or |
| 3593 | * realizeHeight, so that it can be executed synchronously in the case of a |
| 3594 | * programmatic width change. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3595 | */ |
| 3596 | hterm.Terminal.prototype.onResize_ = function() { |
Robert Ginda | 19f6129 | 2014-03-04 14:07:57 -0800 | [diff] [blame] | 3597 | var columnCount = Math.floor(this.scrollPort_.getScreenWidth() / |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3598 | this.scrollPort_.characterSize.width) || 0; |
Rob Spies | f4e90e8 | 2015-01-28 12:10:13 -0800 | [diff] [blame] | 3599 | var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(), |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3600 | this.scrollPort_.characterSize.height) || 0; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3601 | |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 3602 | if (columnCount <= 0 || rowCount <= 0) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3603 | // We avoid these situations since they happen sometimes when the terminal |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 3604 | // gets removed from the document or during the initial load, and we can't |
| 3605 | // deal with that. |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3606 | // This can also happen if called before the scrollPort calculates the |
| 3607 | // character size, meaning we dived by 0 above and default to 0 values. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3608 | return; |
| 3609 | } |
| 3610 | |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3611 | var isNewSize = (columnCount != this.screenSize.width || |
| 3612 | rowCount != this.screenSize.height); |
| 3613 | |
| 3614 | // We do this even if the size didn't change, just to be sure everything is |
| 3615 | // in sync. |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 3616 | this.realizeSize_(columnCount, rowCount); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 3617 | this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1); |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3618 | |
| 3619 | if (isNewSize) |
| 3620 | this.overlaySize(); |
| 3621 | |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 3622 | this.restyleCursor_(); |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3623 | this.scheduleSyncCursorPosition_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3624 | }; |
| 3625 | |
| 3626 | /** |
| 3627 | * Service the cursor blink timeout. |
| 3628 | */ |
| 3629 | hterm.Terminal.prototype.onCursorBlink_ = function() { |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3630 | if (!this.options_.cursorBlink) { |
| 3631 | delete this.timeouts_.cursorBlink; |
| 3632 | return; |
| 3633 | } |
| 3634 | |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 3635 | if (this.cursorNode_.getAttribute('focus') == 'false' || |
| 3636 | this.cursorNode_.style.opacity == '0') { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3637 | this.cursorNode_.style.opacity = '1'; |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3638 | this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_, |
| 3639 | this.cursorBlinkCycle_[0]); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3640 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3641 | this.cursorNode_.style.opacity = '0'; |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3642 | this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_, |
| 3643 | this.cursorBlinkCycle_[1]); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3644 | } |
| 3645 | }; |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 3646 | |
| 3647 | /** |
| 3648 | * Set the scrollbar-visible mode bit. |
| 3649 | * |
| 3650 | * If scrollbar-visible is on, the vertical scrollbar will be visible. |
| 3651 | * Otherwise it will not. |
| 3652 | * |
| 3653 | * Defaults to on. |
| 3654 | * |
| 3655 | * @param {boolean} state True to set scrollbar-visible mode, false to unset. |
| 3656 | */ |
| 3657 | hterm.Terminal.prototype.setScrollbarVisible = function(state) { |
| 3658 | this.scrollPort_.setScrollbarVisible(state); |
| 3659 | }; |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3660 | |
| 3661 | /** |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3662 | * Set the scroll wheel move multiplier. This will affect how fast the page |
Mike Frysinger | 9975de6 | 2017-04-28 00:01:14 -0400 | [diff] [blame] | 3663 | * scrolls on wheel events. |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3664 | * |
| 3665 | * Defaults to 1. |
| 3666 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3667 | * @param {number} multiplier The multiplier to set. |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3668 | */ |
| 3669 | hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) { |
| 3670 | this.scrollPort_.setScrollWheelMoveMultipler(multiplier); |
| 3671 | }; |
| 3672 | |
| 3673 | /** |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3674 | * Close all web notifications created by terminal bells. |
| 3675 | */ |
| 3676 | hterm.Terminal.prototype.closeBellNotifications_ = function() { |
| 3677 | this.bellNotificationList_.forEach(function(n) { |
| 3678 | n.close(); |
| 3679 | }); |
| 3680 | this.bellNotificationList_.length = 0; |
| 3681 | }; |