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