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