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