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