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