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