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