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