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