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