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