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