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