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