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