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