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