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