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