blob: 090db34460551dd33391555a9f9b3171de29c693 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Robert Ginda57f03b42012-09-13 11:02:48 -070030 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080031
Joel Hockeyd4fca732019-09-20 16:57:03 -070032 /** @type {?hterm.PreferenceManager} */
33 this.prefs_ = null;
34
rginda8ba33642011-12-14 12:31:31 -080035 // 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
rginda8ba33642011-12-14 12:31:31 -080042 // 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
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070054 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070055 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080056
rginda87b86462011-12-14 13:48:03 -080057 // The div that contains this terminal.
58 this.div_ = null;
59
rgindac9bc5502012-01-18 11:48:44 -080060 // The document that contains the scrollPort. Defaulted to the global
61 // document here so that the terminal is functional even if it hasn't been
62 // inserted into a document yet, but re-set in decorate().
63 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080064
rginda8ba33642011-12-14 12:31:31 -080065 // The rows that have scrolled off screen and are no longer addressable.
66 this.scrollbackRows_ = [];
67
rgindac9bc5502012-01-18 11:48:44 -080068 // Saved tab stops.
69 this.tabStops_ = [];
70
David Benjamin66e954d2012-05-05 21:08:12 -040071 // Keep track of whether default tab stops have been erased; after a TBC
72 // clears all tab stops, defaults aren't restored on resize until a reset.
73 this.defaultTabStops = true;
74
rginda8ba33642011-12-14 12:31:31 -080075 // The VT's notion of the top and bottom rows. Used during some VT
76 // cursor positioning and scrolling commands.
77 this.vtScrollTop_ = null;
78 this.vtScrollBottom_ = null;
79
80 // The DIV element for the visible cursor.
81 this.cursorNode_ = null;
82
Robert Ginda830583c2013-08-07 13:20:46 -070083 // The current cursor shape of the terminal.
84 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
85
Robert Gindaea2183e2014-07-17 09:51:51 -070086 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
87 this.cursorBlinkCycle_ = [100, 100];
88
Mike Frysinger225c99d2019-10-20 14:02:37 -060089 // Whether to temporarily disable blinking.
90 this.cursorBlinkPause_ = false;
91
Joel Hockey3babf302020-04-22 15:00:06 -070092 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
93 this.cursorOffScreen_ = false;
94
Robert Gindaea2183e2014-07-17 09:51:51 -070095 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
96 // cursor on/off servicing.
97 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
98
rginda9f5222b2012-03-05 11:53:28 -080099 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700100 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700101 /** @type {?string} */
102 this.backgroundColor_ = null;
103 /** @type {?string} */
104 this.foregroundColor_ = null;
105
Robert Ginda57f03b42012-09-13 11:02:48 -0700106 this.scrollOnOutput_ = null;
107 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400108 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800109
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700110 // True if we should override mouse event reporting to allow local selection.
111 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800112
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400113 // Whether to auto hide the mouse cursor when typing.
114 this.setAutomaticMouseHiding();
115 // Timer to keep mouse visible while it's being used.
116 this.mouseHideDelay_ = null;
117
rgindaf0090c92012-02-10 14:58:52 -0800118 // Terminal bell sound.
119 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400120 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800121 this.bellAudio_.setAttribute('preload', 'auto');
122
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000123 // The AccessibilityReader object for announcing command output.
124 this.accessibilityReader_ = null;
125
Mike Frysingercc114512017-09-11 21:39:17 -0400126 // The context menu object.
127 this.contextMenu = new hterm.ContextMenu();
128
Michael Kelly485ecd12014-06-09 11:41:56 -0400129 // All terminal bell notifications that have been generated (not necessarily
130 // shown).
131 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700132 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400133
134 // Whether we have permission to display notifications.
135 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400136
rginda6d397402012-01-17 10:58:29 -0800137 // Cursor position and attributes saved with DECSC.
138 this.savedOptions_ = {};
139
rginda8ba33642011-12-14 12:31:31 -0800140 // The current mode bits for the terminal.
141 this.options_ = new hterm.Options();
142
143 // Timeouts we might need to clear.
144 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800145
146 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800147 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800148
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800149 this.saveCursorAndState(true);
150
Zhu Qunying30d40712017-03-14 16:27:00 -0700151 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800152 this.keyboard = new hterm.Keyboard(this);
153
rginda87b86462011-12-14 13:48:03 -0800154 // General IO interface that can be given to third parties without exposing
155 // the entire terminal object.
156 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800157
rgindad5613292012-06-19 15:40:37 -0700158 // True if mouse-click-drag should scroll the terminal.
159 this.enableMouseDragScroll = true;
160
Robert Ginda57f03b42012-09-13 11:02:48 -0700161 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400162 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700163 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700164
Zhu Qunying30d40712017-03-14 16:27:00 -0700165 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700166 this.useDefaultWindowCopy = false;
167
168 this.clearSelectionAfterCopy = true;
169
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400170 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800171 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700172
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400173 // Whether we allow images to be shown.
174 this.allowImagesInline = null;
175
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400176 this.reportFocus = false;
177
Jason Linf129f3c2020-03-23 11:52:08 +1100178 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
179 this.alwaysUseLegacyPasting = false;
180
Joel Hockey3a44a442019-10-14 16:22:56 -0700181 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500182 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800183};
184
185/**
Robert Ginda830583c2013-08-07 13:20:46 -0700186 * Possible cursor shapes.
187 */
188hterm.Terminal.cursorShape = {
189 BLOCK: 'BLOCK',
190 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400191 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700192};
193
194/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 * Clients should override this to be notified when the terminal is ready
196 * for use.
197 *
198 * The terminal initialization is asynchronous, and shouldn't be used before
199 * this method is called.
200 */
201hterm.Terminal.prototype.onTerminalReady = function() { };
202
203/**
rginda35c456b2012-02-09 17:29:05 -0800204 * Default tab with of 8 to match xterm.
205 */
206hterm.Terminal.prototype.tabWidth = 8;
207
208/**
rginda9f5222b2012-03-05 11:53:28 -0800209 * Select a preference profile.
210 *
211 * This will load the terminal preferences for the given profile name and
212 * associate subsequent preference changes with the new preference profile.
213 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500214 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800215 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400216 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700217 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800218 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400219hterm.Terminal.prototype.setProfile = function(
220 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700221 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800222
Mike Frysingerdc727792020-04-10 01:41:13 -0400223 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800224
Mike Frysingerbdb34802020-04-07 03:47:32 -0400225 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400227 }
rginda9f5222b2012-03-05 11:53:28 -0800228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700230
231 /**
232 * Clears and reloads key bindings. Used by preferences
233 * 'keybindings' and 'keybindings-os-defaults'.
234 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400235 * @param {*?=} bindings
236 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700237 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400238 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700239 terminal.keyboard.bindings.clear();
240
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400241 // Default to an empty object so we still handle OS defaults.
242 if (bindings === null) {
243 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700244 }
245
246 if (!(bindings instanceof Object)) {
247 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400248 bindings = {};
249 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700250 }
251
252 try {
253 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
254 } catch (ex) {
255 console.error('Error in keybindings preference: ' + ex);
256 }
257 }
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800260 'alt-gr-mode': function(v) {
261 if (v == null) {
262 if (navigator.language.toLowerCase() == 'en-us') {
263 v = 'none';
264 } else {
265 v = 'right-alt';
266 }
267 } else if (typeof v == 'string') {
268 v = v.toLowerCase();
269 } else {
270 v = 'none';
271 }
272
Mike Frysingerbdb34802020-04-07 03:47:32 -0400273 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800274 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400275 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800276
277 terminal.keyboard.altGrMode = v;
278 },
279
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700280 'alt-backspace-is-meta-backspace': function(v) {
281 terminal.keyboard.altBackspaceIsMetaBackspace = v;
282 },
283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'alt-is-meta': function(v) {
285 terminal.keyboard.altIsMeta = v;
286 },
287
288 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400289 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700290 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400291 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700292
293 terminal.keyboard.altSendsWhat = v;
294 },
295
296 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400297 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800298 if (ary) {
299 terminal.bellAudio_.setAttribute('src',
300 lib.resource.getDataUrl(ary[1]));
301 } else {
302 terminal.bellAudio_.setAttribute('src', v);
303 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 },
305
Michael Kelly485ecd12014-06-09 11:41:56 -0400306 'desktop-notification-bell': function(v) {
307 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700308 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400309 Notification.permission === 'granted';
310 if (!terminal.desktopNotificationBell_) {
311 // Note: We don't call Notification.requestPermission here because
312 // Chrome requires the call be the result of a user action (such as an
313 // onclick handler), and pref listeners are run asynchronously.
314 //
315 // A way of working around this would be to display a dialog in the
316 // terminal with a "click-to-request-permission" button.
317 console.warn('desktop-notification-bell is true but we do not have ' +
318 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400319 }
320 } else {
321 terminal.desktopNotificationBell_ = false;
322 }
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'background-color': function(v) {
326 terminal.setBackgroundColor(v);
327 },
328
329 'background-image': function(v) {
330 terminal.scrollPort_.setBackgroundImage(v);
331 },
332
333 'background-size': function(v) {
334 terminal.scrollPort_.setBackgroundSize(v);
335 },
336
337 'background-position': function(v) {
338 terminal.scrollPort_.setBackgroundPosition(v);
339 },
340
341 'backspace-sends-backspace': function(v) {
342 terminal.keyboard.backspaceSendsBackspace = v;
343 },
344
Brad Town18654b62015-03-12 00:27:45 -0700345 'character-map-overrides': function(v) {
346 if (!(v == null || v instanceof Object)) {
347 console.warn('Preference character-map-modifications is not an ' +
348 'object: ' + v);
349 return;
350 }
351
Mike Frysinger095d4062017-06-14 00:29:48 -0700352 terminal.vt.characterMaps.reset();
353 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700354 },
355
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 'cursor-blink': function(v) {
357 terminal.setCursorBlink(!!v);
358 },
359
Joel Hockey9d10ba12019-05-28 01:25:02 -0700360 'cursor-shape': function(v) {
361 terminal.setCursorShape(v);
362 },
363
Robert Gindaea2183e2014-07-17 09:51:51 -0700364 'cursor-blink-cycle': function(v) {
365 if (v instanceof Array &&
366 typeof v[0] == 'number' &&
367 typeof v[1] == 'number') {
368 terminal.cursorBlinkCycle_ = v;
369 } else if (typeof v == 'number') {
370 terminal.cursorBlinkCycle_ = [v, v];
371 } else {
372 // Fast blink indicates an error.
373 terminal.cursorBlinkCycle_ = [100, 100];
374 }
375 },
376
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 'cursor-color': function(v) {
378 terminal.setCursorColor(v);
379 },
380
381 'color-palette-overrides': function(v) {
382 if (!(v == null || v instanceof Object || v instanceof Array)) {
383 console.warn('Preference color-palette-overrides is not an array or ' +
384 'object: ' + v);
385 return;
rginda9f5222b2012-03-05 11:53:28 -0800386 }
rginda9f5222b2012-03-05 11:53:28 -0800387
Joel Hockey42dba8f2020-03-26 16:21:11 -0700388 // Call terminal.setColorPalette here and below with the new default
389 // value before changing it in lib.colors.colorPalette to ensure that
390 // CSS vars are updated.
391 lib.colors.stockColorPalette.forEach(
392 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400396 for (const key in v) {
397 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700398 if (isNaN(i) || i < 0 || i > 255) {
399 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
400 continue;
401 }
402
403 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400404 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700405 if (rgb) {
406 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700408 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 }
410 }
rginda30f20f62012-04-05 16:36:19 -0700411 }
rginda30f20f62012-04-05 16:36:19 -0700412
Joel Hockey42dba8f2020-03-26 16:21:11 -0700413 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
414 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 },
rginda30f20f62012-04-05 16:36:19 -0700416
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 'copy-on-select': function(v) {
418 terminal.copyOnSelect = !!v;
419 },
rginda9f5222b2012-03-05 11:53:28 -0800420
Rob Spies0bec09b2014-06-06 15:58:09 -0700421 'use-default-window-copy': function(v) {
422 terminal.useDefaultWindowCopy = !!v;
423 },
424
425 'clear-selection-after-copy': function(v) {
426 terminal.clearSelectionAfterCopy = !!v;
427 },
428
Robert Ginda7e5e9522014-03-14 12:23:58 -0700429 'ctrl-plus-minus-zero-zoom': function(v) {
430 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
431 },
432
Robert Gindafb5a3f92014-05-13 14:12:00 -0700433 'ctrl-c-copy': function(v) {
434 terminal.keyboard.ctrlCCopy = v;
435 },
436
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100437 'ctrl-v-paste': function(v) {
438 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700439 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100440 },
441
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700442 'paste-on-drop': function(v) {
443 terminal.scrollPort_.setPasteOnDrop(v);
444 },
445
Masaya Suzuki273aa982014-05-31 07:25:55 +0900446 'east-asian-ambiguous-as-two-column': function(v) {
447 lib.wc.regardCjkAmbiguous = v;
448 },
449
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 'enable-8-bit-control': function(v) {
451 terminal.vt.enable8BitControl = !!v;
452 },
rginda30f20f62012-04-05 16:36:19 -0700453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'enable-bold': function(v) {
455 terminal.syncBoldSafeState();
456 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400457
Robert Ginda3e278d72014-03-25 13:18:51 -0700458 'enable-bold-as-bright': function(v) {
459 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
460 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
461 },
462
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400463 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500464 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400465 },
466
Robert Ginda57f03b42012-09-13 11:02:48 -0700467 'enable-clipboard-write': function(v) {
468 terminal.vt.enableClipboardWrite = !!v;
469 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400470
Robert Ginda3755e752013-05-31 13:34:09 -0700471 'enable-dec12': function(v) {
472 terminal.vt.enableDec12 = !!v;
473 },
474
Mike Frysinger38f267d2018-09-07 02:50:59 -0400475 'enable-csi-j-3': function(v) {
476 terminal.vt.enableCsiJ3 = !!v;
477 },
478
Robert Ginda57f03b42012-09-13 11:02:48 -0700479 'font-family': function(v) {
480 terminal.syncFontFamily();
481 },
rginda30f20f62012-04-05 16:36:19 -0700482
Robert Ginda57f03b42012-09-13 11:02:48 -0700483 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700484 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700485 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500486 console.error(`Invalid font size: ${v}`);
487 return;
488 }
489
Robert Ginda57f03b42012-09-13 11:02:48 -0700490 terminal.setFontSize(v);
491 },
rginda9875d902012-08-20 16:21:57 -0700492
Robert Ginda57f03b42012-09-13 11:02:48 -0700493 'font-smoothing': function(v) {
494 terminal.syncFontFamily();
495 },
rgindade84e382012-04-20 15:39:31 -0700496
Robert Ginda57f03b42012-09-13 11:02:48 -0700497 'foreground-color': function(v) {
498 terminal.setForegroundColor(v);
499 },
rginda30f20f62012-04-05 16:36:19 -0700500
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400501 'hide-mouse-while-typing': function(v) {
502 terminal.setAutomaticMouseHiding(v);
503 },
504
Robert Ginda57f03b42012-09-13 11:02:48 -0700505 'home-keys-scroll': function(v) {
506 terminal.keyboard.homeKeysScroll = v;
507 },
rginda4bba5e12012-06-20 16:15:30 -0700508
Robert Gindaa8165692015-06-15 14:46:31 -0700509 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700510 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
511 },
Robert Gindaa8165692015-06-15 14:46:31 -0700512
Joel Hockey95a9e272020-03-16 21:19:53 -0700513 'keybindings-os-defaults': function(v) {
514 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700515 },
516
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700517 'media-keys-are-fkeys': function(v) {
518 terminal.keyboard.mediaKeysAreFKeys = v;
519 },
520
Robert Ginda57f03b42012-09-13 11:02:48 -0700521 'meta-sends-escape': function(v) {
522 terminal.keyboard.metaSendsEscape = v;
523 },
rginda30f20f62012-04-05 16:36:19 -0700524
Mike Frysinger847577f2017-05-23 23:25:57 -0400525 'mouse-right-click-paste': function(v) {
526 terminal.mouseRightClickPaste = v;
527 },
528
Robert Ginda57f03b42012-09-13 11:02:48 -0700529 'mouse-paste-button': function(v) {
530 terminal.syncMousePasteButton();
531 },
rgindaa8ba17d2012-08-15 14:41:10 -0700532
Robert Gindae76aa9f2014-03-14 12:29:12 -0700533 'page-keys-scroll': function(v) {
534 terminal.keyboard.pageKeysScroll = v;
535 },
536
Robert Ginda40932892012-12-10 17:26:40 -0800537 'pass-alt-number': function(v) {
538 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700539 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800540 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500541 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800542 }
543
544 terminal.passAltNumber = v;
545 },
546
547 'pass-ctrl-number': function(v) {
548 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700549 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800550 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500551 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800552 }
553
554 terminal.passCtrlNumber = v;
555 },
556
Joel Hockey0e052042020-02-19 05:37:19 -0800557 'pass-ctrl-n': function(v) {
558 terminal.passCtrlN = v;
559 },
560
561 'pass-ctrl-t': function(v) {
562 terminal.passCtrlT = v;
563 },
564
565 'pass-ctrl-tab': function(v) {
566 terminal.passCtrlTab = v;
567 },
568
569 'pass-ctrl-w': function(v) {
570 terminal.passCtrlW = v;
571 },
572
Robert Ginda40932892012-12-10 17:26:40 -0800573 'pass-meta-number': function(v) {
574 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700575 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800576 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500577 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800578 }
579
580 terminal.passMetaNumber = v;
581 },
582
Marius Schilder77857b32014-05-14 16:21:26 -0700583 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700584 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700585 },
586
Robert Ginda8cb7d902013-06-20 14:37:18 -0700587 'receive-encoding': function(v) {
588 if (!(/^(utf-8|raw)$/).test(v)) {
589 console.warn('Invalid value for "receive-encoding": ' + v);
590 v = 'utf-8';
591 }
592
593 terminal.vt.characterEncoding = v;
594 },
595
Joel Hockey139d82d2020-04-07 23:04:29 -0700596 'screen-padding-size': function(v) {
597 v = parseInt(v, 10);
598 if (isNaN(v) || v < 0) {
599 console.error(`Invalid screen padding size: ${v}`);
600 return;
601 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700602 terminal.setScreenPaddingSize(v);
603 },
604
Robert Ginda57f03b42012-09-13 11:02:48 -0700605 'scroll-on-keystroke': function(v) {
606 terminal.scrollOnKeystroke_ = v;
607 },
rginda9f5222b2012-03-05 11:53:28 -0800608
Robert Ginda57f03b42012-09-13 11:02:48 -0700609 'scroll-on-output': function(v) {
610 terminal.scrollOnOutput_ = v;
611 },
rginda30f20f62012-04-05 16:36:19 -0700612
Robert Ginda57f03b42012-09-13 11:02:48 -0700613 'scrollbar-visible': function(v) {
614 terminal.setScrollbarVisible(v);
615 },
rginda9f5222b2012-03-05 11:53:28 -0800616
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400617 'scroll-wheel-may-send-arrow-keys': function(v) {
618 terminal.scrollWheelArrowKeys_ = v;
619 },
620
Rob Spies49039e52014-12-17 13:40:04 -0800621 'scroll-wheel-move-multiplier': function(v) {
622 terminal.setScrollWheelMoveMultipler(v);
623 },
624
Robert Ginda57f03b42012-09-13 11:02:48 -0700625 'shift-insert-paste': function(v) {
626 terminal.keyboard.shiftInsertPaste = v;
627 },
rginda9f5222b2012-03-05 11:53:28 -0800628
Mike Frysingera7768922017-07-28 15:00:12 -0400629 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400630 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400631 },
632
Robert Gindae76aa9f2014-03-14 12:29:12 -0700633 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400634 terminal.scrollPort_.setUserCssUrl(v);
635 },
636
637 'user-css-text': function(v) {
638 terminal.scrollPort_.setUserCssText(v);
639 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400640
641 'word-break-match-left': function(v) {
642 terminal.primaryScreen_.wordBreakMatchLeft = v;
643 terminal.alternateScreen_.wordBreakMatchLeft = v;
644 },
645
646 'word-break-match-right': function(v) {
647 terminal.primaryScreen_.wordBreakMatchRight = v;
648 terminal.alternateScreen_.wordBreakMatchRight = v;
649 },
650
651 'word-break-match-middle': function(v) {
652 terminal.primaryScreen_.wordBreakMatchMiddle = v;
653 terminal.alternateScreen_.wordBreakMatchMiddle = v;
654 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400655
656 'allow-images-inline': function(v) {
657 terminal.allowImagesInline = v;
658 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700659 });
rginda30f20f62012-04-05 16:36:19 -0700660
Robert Ginda57f03b42012-09-13 11:02:48 -0700661 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800662 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700663
Mike Frysingerec4225d2020-04-07 05:00:01 -0400664 if (callback) {
665 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400666 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700667 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800668};
669
Rob Spies56953412014-04-28 14:09:47 -0700670/**
671 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500672 *
Joel Hockey0f933582019-08-27 18:01:51 -0700673 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700674 */
675hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700676 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700677};
678
Robert Gindaa063b202014-07-21 11:08:25 -0700679/**
680 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500681 *
682 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700683 */
684hterm.Terminal.prototype.setBracketedPaste = function(state) {
685 this.options_.bracketedPaste = state;
686};
Rob Spies56953412014-04-28 14:09:47 -0700687
rginda8e92a692012-05-20 19:37:20 -0700688/**
689 * Set the color for the cursor.
690 *
691 * If you want this setting to persist, set it through prefs_, rather than
692 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500693 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500694 * @param {string=} color The color to set. If not defined, we reset to the
695 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700696 */
697hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400698 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700699 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400700 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500701
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400702 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700703};
704
705/**
706 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400707 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500708 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700709 */
710hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400711 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700712};
713
714/**
rgindad5613292012-06-19 15:40:37 -0700715 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500716 *
717 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700718 */
719hterm.Terminal.prototype.setSelectionEnabled = function(state) {
720 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700721};
722
723/**
rginda8e92a692012-05-20 19:37:20 -0700724 * Set the background color.
725 *
726 * If you want this setting to persist, set it through prefs_, rather than
727 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500728 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500729 * @param {string=} color The color to set. If not defined, we reset to the
730 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700731 */
732hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400733 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700734 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400735 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500736
Joel Hockey42dba8f2020-03-26 16:21:11 -0700737 this.backgroundColor_ = lib.colors.normalizeCSS(color);
738 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700739};
740
rginda9f5222b2012-03-05 11:53:28 -0800741/**
742 * Return the current terminal background color.
743 *
744 * Intended for use by other classes, so we don't have to expose the entire
745 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500746 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700747 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800748 */
749hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700750 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700751};
752
753/**
754 * Set the foreground color.
755 *
756 * If you want this setting to persist, set it through prefs_, rather than
757 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500758 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500759 * @param {string=} color The color to set. If not defined, we reset to the
760 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700761 */
762hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400763 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700764 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400765 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500766
Joel Hockey42dba8f2020-03-26 16:21:11 -0700767 this.foregroundColor_ = lib.colors.normalizeCSS(color);
768 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800769};
770
771/**
772 * Return the current terminal foreground color.
773 *
774 * Intended for use by other classes, so we don't have to expose the entire
775 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500776 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700777 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800778 */
779hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700780 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800781};
782
783/**
rginda87b86462011-12-14 13:48:03 -0800784 * Create a new instance of a terminal command and run it with a given
785 * argument string.
786 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700787 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700788 * @param {string} commandName The command to run for this terminal.
789 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800790 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700791hterm.Terminal.prototype.runCommandClass = function(
792 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400793 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400794 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700795 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400796 }
rgindaf522ce02012-04-17 17:49:17 -0700797
rginda87b86462011-12-14 13:48:03 -0800798 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700799 {
800 commandName: commandName,
801 args: args,
rginda87b86462011-12-14 13:48:03 -0800802 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700803 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400804 onExit: (code) => {
805 this.io.pop();
806 this.uninstallKeyboard();
807 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
808 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400809 window.close();
810 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400811 },
rginda87b86462011-12-14 13:48:03 -0800812 });
813
rgindafeaf3142012-01-31 15:14:20 -0800814 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800815 this.command.run();
816};
817
818/**
rgindafeaf3142012-01-31 15:14:20 -0800819 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500820 *
821 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800822 */
823hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700824 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800825};
826
827/**
828 * Install the keyboard handler for this terminal.
829 *
830 * This will prevent the browser from seeing any keystrokes sent to the
831 * terminal.
832 */
833hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700834 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400835};
rgindafeaf3142012-01-31 15:14:20 -0800836
837/**
838 * Uninstall the keyboard handler for this terminal.
839 */
840hterm.Terminal.prototype.uninstallKeyboard = function() {
841 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400842};
rgindafeaf3142012-01-31 15:14:20 -0800843
844/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400845 * Set a CSS variable.
846 *
847 * Normally this is used to set variables in the hterm namespace.
848 *
849 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700850 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400851 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400852 */
853hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400854 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400855 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400856 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400857};
858
859/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700860 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
861 * input is valid.
862 *
863 * @param {string} name The variable to set.
864 * @param {?string} rgb The rgb value to assign to the variable.
865 */
866hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
867 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
868 if (ary) {
869 this.setCssVar(name, ary.slice(0, 3).join(','));
870 }
871};
872
873/**
874 * Sets the specified color for the active screen.
875 *
876 * @param {number} i The index into the 256 color palette to set.
877 * @param {?string} rgb The rgb value to assign to the variable.
878 */
879hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
880 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
881 this.setRgbColorCssVar(`color-${i}`, rgb);
882 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
883 }
884};
885
886/**
887 * Returns the current value in the active screen of the specified color.
888 *
889 * @param {number} i Color palette index.
890 * @return {string} rgb color.
891 */
892hterm.Terminal.prototype.getColorPalette = function(i) {
893 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
894 lib.colors.colorPalette[i];
895};
896
897/**
898 * Reset the specified color in the active screen to its default value.
899 *
900 * @param {number} i Color to reset
901 */
902hterm.Terminal.prototype.resetColor = function(i) {
903 this.setColorPalette(i, lib.colors.colorPalette[i]);
904 delete this.screen_.textAttributes.colorPaletteOverrides[i];
905};
906
907/**
908 * Reset the current screen color palette to the default state.
909 */
910hterm.Terminal.prototype.resetColorPalette = function() {
911 this.screen_.textAttributes.colorPaletteOverrides.forEach(
912 (c, i) => this.resetColor(i));
913};
914
915/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500916 * Get a CSS variable.
917 *
918 * Normally this is used to get variables in the hterm namespace.
919 *
920 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400921 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500922 * @return {string} The current setting for this variable.
923 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400924hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500925 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400926 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500927};
928
929/**
Jason Linbbbdb752020-03-06 16:26:59 +1100930 * Update CSS character size variables to match the scrollport.
931 */
932hterm.Terminal.prototype.updateCssCharsize_ = function() {
933 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
934 this.setCssVar('charsize-height',
935 this.scrollPort_.characterSize.height + 'px');
936};
937
938/**
rginda35c456b2012-02-09 17:29:05 -0800939 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800940 *
941 * Call setFontSize(0) to reset to the default font size.
942 *
943 * This function does not modify the font-size preference.
944 *
945 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800946 */
947hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400948 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700949 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400950 }
rginda9f5222b2012-03-05 11:53:28 -0800951
rginda35c456b2012-02-09 17:29:05 -0800952 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100953 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800954};
955
956/**
957 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500958 *
959 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800960 */
961hterm.Terminal.prototype.getFontSize = function() {
962 return this.scrollPort_.getFontSize();
963};
964
965/**
rginda8e92a692012-05-20 19:37:20 -0700966 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500967 *
968 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700969 */
970hterm.Terminal.prototype.getFontFamily = function() {
971 return this.scrollPort_.getFontFamily();
972};
973
974/**
rginda35c456b2012-02-09 17:29:05 -0800975 * Set the CSS "font-family" for this terminal.
976 */
rginda9f5222b2012-03-05 11:53:28 -0800977hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700978 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
979 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100980 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800981 this.syncBoldSafeState();
982};
983
rginda4bba5e12012-06-20 16:15:30 -0700984/**
985 * Set this.mousePasteButton based on the mouse-paste-button pref,
986 * autodetecting if necessary.
987 */
988hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -0400989 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -0700990 if (typeof button == 'number') {
991 this.mousePasteButton = button;
992 return;
993 }
994
Mike Frysingeree81a002017-12-12 16:14:53 -0500995 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400996 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700997 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400998 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700999 }
1000};
1001
1002/**
1003 * Enable or disable bold based on the enable-bold pref, autodetecting if
1004 * necessary.
1005 */
rginda9f5222b2012-03-05 11:53:28 -08001006hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001007 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001008 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001009 this.primaryScreen_.textAttributes.enableBold = enableBold;
1010 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001011 return;
1012 }
1013
Mike Frysingerdc727792020-04-10 01:41:13 -04001014 const normalSize = this.scrollPort_.measureCharacterSize();
1015 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001016
Mike Frysingerdc727792020-04-10 01:41:13 -04001017 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001018 if (!isBoldSafe) {
1019 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001020 'from normal. Font family is: ' +
1021 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001022 }
rginda9f5222b2012-03-05 11:53:28 -08001023
Robert Gindaed016262012-10-26 16:27:09 -07001024 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1025 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001026};
1027
1028/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001029 * Control text blinking behavior.
1030 *
1031 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001032 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001033hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001034 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001035 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001036 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001037 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001038};
1039
1040/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001041 * Set the mouse cursor style based on the current terminal mode.
1042 */
1043hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001044 this.setCssVar('mouse-cursor-style',
1045 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1046 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001047 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001048};
1049
1050/**
rginda87b86462011-12-14 13:48:03 -08001051 * Return a copy of the current cursor position.
1052 *
Joel Hockey0f933582019-08-27 18:01:51 -07001053 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001054 */
1055hterm.Terminal.prototype.saveCursor = function() {
1056 return this.screen_.cursorPosition.clone();
1057};
1058
Evan Jones2600d4f2016-12-06 09:29:36 -05001059/**
1060 * Return the current text attributes.
1061 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001062 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001063 */
rgindaa19afe22012-01-25 15:40:22 -08001064hterm.Terminal.prototype.getTextAttributes = function() {
1065 return this.screen_.textAttributes;
1066};
1067
Evan Jones2600d4f2016-12-06 09:29:36 -05001068/**
1069 * Set the text attributes.
1070 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001071 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001072 */
rginda1a09aa02012-06-18 21:11:25 -07001073hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1074 this.screen_.textAttributes = textAttributes;
1075};
1076
rginda87b86462011-12-14 13:48:03 -08001077/**
rgindaf522ce02012-04-17 17:49:17 -07001078 * Return the current browser zoom factor applied to the terminal.
1079 *
1080 * @return {number} The current browser zoom factor.
1081 */
1082hterm.Terminal.prototype.getZoomFactor = function() {
1083 return this.scrollPort_.characterSize.zoomFactor;
1084};
1085
1086/**
rginda9846e2f2012-01-27 13:53:33 -08001087 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001088 *
1089 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001090 */
1091hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001092 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001093};
1094
1095/**
rginda87b86462011-12-14 13:48:03 -08001096 * Restore a previously saved cursor position.
1097 *
Joel Hockey0f933582019-08-27 18:01:51 -07001098 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001099 */
1100hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001101 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1102 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001103 this.screen_.setCursorPosition(row, column);
1104 if (cursor.column > column ||
1105 cursor.column == column && cursor.overflow) {
1106 this.screen_.cursorPosition.overflow = true;
1107 }
rginda87b86462011-12-14 13:48:03 -08001108};
1109
1110/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001111 * Clear the cursor's overflow flag.
1112 */
1113hterm.Terminal.prototype.clearCursorOverflow = function() {
1114 this.screen_.cursorPosition.overflow = false;
1115};
1116
1117/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001118 * Save the current cursor state to the corresponding screens.
1119 *
1120 * See the hterm.Screen.CursorState class for more details.
1121 *
1122 * @param {boolean=} both If true, update both screens, else only update the
1123 * current screen.
1124 */
1125hterm.Terminal.prototype.saveCursorAndState = function(both) {
1126 if (both) {
1127 this.primaryScreen_.saveCursorAndState(this.vt);
1128 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001129 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001130 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001131 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001132};
1133
1134/**
1135 * Restore the saved cursor state in the corresponding screens.
1136 *
1137 * See the hterm.Screen.CursorState class for more details.
1138 *
1139 * @param {boolean=} both If true, update both screens, else only update the
1140 * current screen.
1141 */
1142hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1143 if (both) {
1144 this.primaryScreen_.restoreCursorAndState(this.vt);
1145 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001146 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001147 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001148 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001149};
1150
1151/**
Robert Ginda830583c2013-08-07 13:20:46 -07001152 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001153 *
1154 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001155 */
1156hterm.Terminal.prototype.setCursorShape = function(shape) {
1157 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001158 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001159};
Robert Ginda830583c2013-08-07 13:20:46 -07001160
1161/**
1162 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001163 *
1164 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001165 */
1166hterm.Terminal.prototype.getCursorShape = function() {
1167 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001168};
Robert Ginda830583c2013-08-07 13:20:46 -07001169
1170/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001171 * Set the screen padding size in pixels.
1172 *
1173 * @param {number} size
1174 */
1175hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001176 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001177 this.scrollPort_.setScreenPaddingSize(size);
1178};
1179
1180/**
rginda87b86462011-12-14 13:48:03 -08001181 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001182 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001183 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001184 */
1185hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001186 if (columnCount == null) {
1187 this.div_.style.width = '100%';
1188 return;
1189 }
1190
Joel Hockey139d82d2020-04-07 23:04:29 -07001191 const rightPadding = Math.max(
1192 this.scrollPort_.screenPaddingSize,
1193 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001194 this.div_.style.width = Math.ceil(
Joel Hockey139d82d2020-04-07 23:04:29 -07001195 this.scrollPort_.characterSize.width * columnCount +
1196 this.scrollPort_.screenPaddingSize + rightPadding) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001197 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001198 this.scheduleSyncCursorPosition_();
1199};
rginda87b86462011-12-14 13:48:03 -08001200
rgindac9bc5502012-01-18 11:48:44 -08001201/**
rginda35c456b2012-02-09 17:29:05 -08001202 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001203 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001204 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001205 */
1206hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001207 if (rowCount == null) {
1208 this.div_.style.height = '100%';
1209 return;
1210 }
1211
Joel Hockey139d82d2020-04-07 23:04:29 -07001212 this.div_.style.height = this.scrollPort_.characterSize.height * rowCount +
1213 (2 * this.scrollPort_.screenPaddingSize) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001214 this.realizeSize_(this.screenSize.width, rowCount);
1215 this.scheduleSyncCursorPosition_();
1216};
1217
1218/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001219 * Deal with terminal size changes.
1220 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001221 * @param {number} columnCount The number of columns.
1222 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001223 */
1224hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001225 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001226
Mike Frysinger0206e262019-06-13 10:18:19 -04001227 if (columnCount != this.screenSize.width) {
1228 notify = true;
1229 this.realizeWidth_(columnCount);
1230 }
1231
1232 if (rowCount != this.screenSize.height) {
1233 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001234 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001235 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001236
1237 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001238 if (notify) {
1239 this.io.onTerminalResize_(columnCount, rowCount);
1240 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001241};
1242
1243/**
rgindac9bc5502012-01-18 11:48:44 -08001244 * Deal with terminal width changes.
1245 *
1246 * This function does what needs to be done when the terminal width changes
1247 * out from under us. It happens here rather than in onResize_() because this
1248 * code may need to run synchronously to handle programmatic changes of
1249 * terminal width.
1250 *
1251 * Relying on the browser to send us an async resize event means we may not be
1252 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001253 *
1254 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001255 */
1256hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001257 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001258 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001259 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001260
Mike Frysingerdc727792020-04-10 01:41:13 -04001261 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001262 if (deltaColumns == 0) {
1263 // No change, so don't bother recalculating things.
1264 return;
1265 }
rgindac9bc5502012-01-18 11:48:44 -08001266
rginda87b86462011-12-14 13:48:03 -08001267 this.screenSize.width = columnCount;
1268 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001269
1270 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001271 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001272 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001273 }
rgindac9bc5502012-01-18 11:48:44 -08001274 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001275 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001276 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001277 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001278 }
rgindac9bc5502012-01-18 11:48:44 -08001279
1280 this.tabStops_.pop();
1281 }
1282 }
1283
1284 this.screen_.setColumnCount(this.screenSize.width);
1285};
1286
1287/**
1288 * Deal with terminal height changes.
1289 *
1290 * This function does what needs to be done when the terminal height changes
1291 * out from under us. It happens here rather than in onResize_() because this
1292 * code may need to run synchronously to handle programmatic changes of
1293 * terminal height.
1294 *
1295 * Relying on the browser to send us an async resize event means we may not be
1296 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001297 *
1298 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001299 */
1300hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001301 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001302 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001303 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001304
Mike Frysingerdc727792020-04-10 01:41:13 -04001305 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001306 if (deltaRows == 0) {
1307 // No change, so don't bother recalculating things.
1308 return;
1309 }
rgindac9bc5502012-01-18 11:48:44 -08001310
1311 this.screenSize.height = rowCount;
1312
Mike Frysingerdc727792020-04-10 01:41:13 -04001313 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001314
1315 if (deltaRows < 0) {
1316 // Screen got smaller.
1317 deltaRows *= -1;
1318 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001319 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001320 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001321 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001322 }
rgindac9bc5502012-01-18 11:48:44 -08001323
Mike Frysingerbdb34802020-04-07 03:47:32 -04001324 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001325 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001326 }
rgindac9bc5502012-01-18 11:48:44 -08001327
1328 this.screen_.popRow();
1329 deltaRows--;
1330 }
1331
Mike Frysingerdc727792020-04-10 01:41:13 -04001332 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001333 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1334
1335 // We just removed rows from the top of the screen, we need to update
1336 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001337 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001338 } else if (deltaRows > 0) {
1339 // Screen got larger.
1340
1341 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001342 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1343 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001344 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1345 this.screen_.unshiftRows(rows);
1346 deltaRows -= scrollbackCount;
1347 cursor.row += scrollbackCount;
1348 }
1349
Mike Frysingerbdb34802020-04-07 03:47:32 -04001350 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001351 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001352 }
rgindac9bc5502012-01-18 11:48:44 -08001353 }
1354
rginda35c456b2012-02-09 17:29:05 -08001355 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001356 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001357};
1358
1359/**
1360 * Scroll the terminal to the top of the scrollback buffer.
1361 */
1362hterm.Terminal.prototype.scrollHome = function() {
1363 this.scrollPort_.scrollRowToTop(0);
1364};
1365
1366/**
1367 * Scroll the terminal to the end.
1368 */
1369hterm.Terminal.prototype.scrollEnd = function() {
1370 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1371};
1372
1373/**
1374 * Scroll the terminal one page up (minus one line) relative to the current
1375 * position.
1376 */
1377hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001378 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001379};
1380
1381/**
1382 * Scroll the terminal one page down (minus one line) relative to the current
1383 * position.
1384 */
1385hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001386 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001387};
1388
rgindac9bc5502012-01-18 11:48:44 -08001389/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001390 * Scroll the terminal one line up relative to the current position.
1391 */
1392hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001393 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001394 this.scrollPort_.scrollRowToTop(i - 1);
1395};
1396
1397/**
1398 * Scroll the terminal one line down relative to the current position.
1399 */
1400hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001401 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001402 this.scrollPort_.scrollRowToTop(i + 1);
1403};
1404
1405/**
Robert Ginda40932892012-12-10 17:26:40 -08001406 * Clear primary screen, secondary screen, and the scrollback buffer.
1407 */
1408hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001409 this.clearHome(this.primaryScreen_);
1410 this.clearHome(this.alternateScreen_);
1411
1412 this.clearScrollback();
1413};
1414
1415/**
1416 * Clear scrollback buffer.
1417 */
1418hterm.Terminal.prototype.clearScrollback = function() {
1419 // Move to the end of the buffer in case the screen was scrolled back.
1420 // We're going to throw it away which would leave the display invalid.
1421 this.scrollEnd();
1422
Robert Ginda40932892012-12-10 17:26:40 -08001423 this.scrollbackRows_.length = 0;
1424 this.scrollPort_.resetCache();
1425
Mike Frysinger9c482b82018-09-07 02:49:36 -04001426 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1427 const bottom = screen.getHeight();
1428 this.renumberRows_(0, bottom, screen);
1429 });
Robert Ginda40932892012-12-10 17:26:40 -08001430
1431 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001432 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001433};
1434
1435/**
rgindac9bc5502012-01-18 11:48:44 -08001436 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001437 *
1438 * Perform a full reset to the default values listed in
1439 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001440 */
rginda87b86462011-12-14 13:48:03 -08001441hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001442 this.vt.reset();
1443
rgindac9bc5502012-01-18 11:48:44 -08001444 this.clearAllTabStops();
1445 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001446
Joel Hockey42dba8f2020-03-26 16:21:11 -07001447 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001448 const resetScreen = (screen) => {
1449 // We want to make sure to reset the attributes before we clear the screen.
1450 // The attributes might be used to initialize default/empty rows.
1451 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001452 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001453 this.clearHome(screen);
1454 screen.saveCursorAndState(this.vt);
1455 };
1456 resetScreen(this.primaryScreen_);
1457 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001458
Mike Frysinger84301d02017-11-29 13:28:46 -08001459 // Reset terminal options to their default values.
1460 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001461 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1462
Mike Frysinger84301d02017-11-29 13:28:46 -08001463 this.setVTScrollRegion(null, null);
1464
1465 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001466};
1467
rgindac9bc5502012-01-18 11:48:44 -08001468/**
1469 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001470 *
1471 * Perform a soft reset to the default values listed in
1472 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001473 */
rginda0f5c0292012-01-13 11:00:13 -08001474hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001475 this.vt.reset();
1476
rgindab8bc8932012-04-27 12:45:03 -07001477 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001478 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001479
Brad Townb62dfdc2015-03-16 19:07:15 -07001480 // We show the cursor on soft reset but do not alter the blink state.
1481 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1482
Joel Hockey42dba8f2020-03-26 16:21:11 -07001483 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001484 const resetScreen = (screen) => {
1485 // Xterm also resets the color palette on soft reset, even though it doesn't
1486 // seem to be documented anywhere.
1487 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001488 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001489 screen.saveCursorAndState(this.vt);
1490 };
1491 resetScreen(this.primaryScreen_);
1492 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001493
rgindab8bc8932012-04-27 12:45:03 -07001494 // The xterm man page explicitly says this will happen on soft reset.
1495 this.setVTScrollRegion(null, null);
1496
1497 // Xterm also shows the cursor on soft reset, but does not alter the blink
1498 // state.
rgindaa19afe22012-01-25 15:40:22 -08001499 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001500};
1501
rgindac9bc5502012-01-18 11:48:44 -08001502/**
1503 * Move the cursor forward to the next tab stop, or to the last column
1504 * if no more tab stops are set.
1505 */
1506hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001507 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001508
Mike Frysingerdc727792020-04-10 01:41:13 -04001509 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001510 if (this.tabStops_[i] > column) {
1511 this.setCursorColumn(this.tabStops_[i]);
1512 return;
1513 }
1514 }
1515
David Benjamin66e954d2012-05-05 21:08:12 -04001516 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001517 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001518 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001519 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001520};
1521
rgindac9bc5502012-01-18 11:48:44 -08001522/**
1523 * Move the cursor backward to the previous tab stop, or to the first column
1524 * if no previous tab stops are set.
1525 */
1526hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001527 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001528
Mike Frysingerdc727792020-04-10 01:41:13 -04001529 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001530 if (this.tabStops_[i] < column) {
1531 this.setCursorColumn(this.tabStops_[i]);
1532 return;
1533 }
1534 }
1535
1536 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001537};
1538
rgindac9bc5502012-01-18 11:48:44 -08001539/**
1540 * Set a tab stop at the given column.
1541 *
Joel Hockey0f933582019-08-27 18:01:51 -07001542 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001543 */
1544hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001545 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001546 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001547 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001548 }
rgindac9bc5502012-01-18 11:48:44 -08001549
1550 if (this.tabStops_[i] < column) {
1551 this.tabStops_.splice(i + 1, 0, column);
1552 return;
1553 }
1554 }
1555
1556 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001557};
1558
rgindac9bc5502012-01-18 11:48:44 -08001559/**
1560 * Clear the tab stop at the current cursor position.
1561 *
1562 * No effect if there is no tab stop at the current cursor position.
1563 */
1564hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001565 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001566
Mike Frysingerdc727792020-04-10 01:41:13 -04001567 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001568 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001569 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001570 }
rgindac9bc5502012-01-18 11:48:44 -08001571
1572 this.tabStops_.splice(i, 1);
1573};
1574
1575/**
1576 * Clear all tab stops.
1577 */
1578hterm.Terminal.prototype.clearAllTabStops = function() {
1579 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001580 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001581};
1582
1583/**
1584 * Set up the default tab stops, starting from a given column.
1585 *
1586 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001587 * from the specified column, or 0 if no column is provided. It also flags
1588 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001589 *
1590 * This does not clear the existing tab stops first, use clearAllTabStops
1591 * for that.
1592 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001593 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001594 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001595 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001596hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001597 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001598 // Round start up to a default tab stop.
1599 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001600 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001601 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001602 }
David Benjamin66e954d2012-05-05 21:08:12 -04001603
1604 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001605};
1606
rginda6d397402012-01-17 10:58:29 -08001607/**
rginda8ba33642011-12-14 12:31:31 -08001608 * Interpret a sequence of characters.
1609 *
1610 * Incomplete escape sequences are buffered until the next call.
1611 *
1612 * @param {string} str Sequence of characters to interpret or pass through.
1613 */
1614hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001615 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001616 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001617};
1618
1619/**
1620 * Take over the given DIV for use as the terminal display.
1621 *
Joel Hockey0f933582019-08-27 18:01:51 -07001622 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001623 */
1624hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001625 const charset = div.ownerDocument.characterSet.toLowerCase();
1626 if (charset != 'utf-8') {
1627 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1628 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1629 }
1630
rginda87b86462011-12-14 13:48:03 -08001631 this.div_ = div;
1632
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001633 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1634
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001635 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1636};
1637
1638/**
1639 * Initialisation of ScrollPort properties which need to be set after its DOM
1640 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001641 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001642 * @private
1643 */
1644hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001645 this.scrollPort_.setBackgroundImage(
1646 this.prefs_.getString('background-image'));
1647 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001648 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001649 this.prefs_.getString('background-position'));
1650 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1651 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1652 this.scrollPort_.setAccessibilityReader(
1653 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001654
rginda0918b652012-04-04 11:26:24 -07001655 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001656
Joel Hockeyd4fca732019-09-20 16:57:03 -07001657 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001658 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001659
Joel Hockeyd4fca732019-09-20 16:57:03 -07001660 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001661 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001662 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001663
rginda8ba33642011-12-14 12:31:31 -08001664 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001665 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001666
Evan Jones5f9df812016-12-06 09:38:58 -05001667 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001668 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001669
Mike Frysingerdc727792020-04-10 01:41:13 -04001670 const onMouse = this.onMouse_.bind(this);
1671 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001672 screenNode.addEventListener(
1673 'mousedown', /** @type {!EventListener} */ (onMouse));
1674 screenNode.addEventListener(
1675 'mouseup', /** @type {!EventListener} */ (onMouse));
1676 screenNode.addEventListener(
1677 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001678 this.scrollPort_.onScrollWheel = onMouse;
1679
Joel Hockeyd4fca732019-09-20 16:57:03 -07001680 screenNode.addEventListener(
1681 'keydown',
1682 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001683
Toni Barzic0bfa8922013-11-22 11:18:35 -08001684 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001685 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001686 // Listen for mousedown events on the screenNode as in FF the focus
1687 // events don't bubble.
1688 screenNode.addEventListener('mousedown', function() {
1689 setTimeout(this.onFocusChange_.bind(this, true));
1690 }.bind(this));
1691
Toni Barzic0bfa8922013-11-22 11:18:35 -08001692 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001693 'blur', this.onFocusChange_.bind(this, false));
1694
Mike Frysingerdc727792020-04-10 01:41:13 -04001695 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001696 style.textContent = `
1697.cursor-node[focus="false"] {
1698 box-sizing: border-box;
1699 background-color: transparent !important;
1700 border-width: 2px;
1701 border-style: solid;
1702}
1703menu {
1704 margin: 0;
1705 padding: 0;
1706 cursor: var(--hterm-mouse-cursor-pointer);
1707}
1708menuitem {
1709 white-space: nowrap;
1710 border-bottom: 1px dashed;
1711 display: block;
1712 padding: 0.3em 0.3em 0 0.3em;
1713}
1714menuitem.separator {
1715 border-bottom: none;
1716 height: 0.5em;
1717 padding: 0;
1718}
1719menuitem:hover {
1720 color: var(--hterm-cursor-color);
1721}
1722.wc-node {
1723 display: inline-block;
1724 text-align: center;
1725 width: calc(var(--hterm-charsize-width) * 2);
1726 line-height: var(--hterm-charsize-height);
1727}
1728:root {
1729 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1730 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001731 --hterm-blink-node-duration: 0.7s;
1732 --hterm-mouse-cursor-default: default;
1733 --hterm-mouse-cursor-text: text;
1734 --hterm-mouse-cursor-pointer: pointer;
1735 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001736 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001737
Joel Hockey42dba8f2020-03-26 16:21:11 -07001738${lib.colors.stockColorPalette.map((c, i) => `
1739 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1740`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001741}
1742.uri-node:hover {
1743 text-decoration: underline;
1744 cursor: var(--hterm-mouse-cursor-pointer);
1745}
1746@keyframes blink {
1747 from { opacity: 1.0; }
1748 to { opacity: 0.0; }
1749}
1750.blink-node {
1751 animation-name: blink;
1752 animation-duration: var(--hterm-blink-node-duration);
1753 animation-iteration-count: infinite;
1754 animation-timing-function: ease-in-out;
1755 animation-direction: alternate;
1756}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001757 // Insert this stock style as the first node so that any user styles will
1758 // override w/out having to use !important everywhere. The rules above mix
1759 // runtime variables with default ones designed to be overridden by the user,
1760 // but we can wait for a concrete case from the users to determine the best
1761 // way to split the sheet up to before & after the user-css settings.
1762 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001763
rginda8ba33642011-12-14 12:31:31 -08001764 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001765 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001766 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001767 this.cursorNode_.style.cssText = `
1768position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001769left: calc(var(--hterm-screen-padding-size) +
1770 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1771top: calc(var(--hterm-screen-padding-size) +
1772 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001773display: ${this.options_.cursorVisible ? '' : 'none'};
1774width: var(--hterm-charsize-width);
1775height: var(--hterm-charsize-height);
1776background-color: var(--hterm-cursor-color);
1777border-color: var(--hterm-cursor-color);
1778-webkit-transition: opacity, background-color 100ms linear;
1779-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001780
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001781 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001782 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1783 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001784
rginda8ba33642011-12-14 12:31:31 -08001785 this.document_.body.appendChild(this.cursorNode_);
1786
rgindad5613292012-06-19 15:40:37 -07001787 // When 'enableMouseDragScroll' is off we reposition this element directly
1788 // under the mouse cursor after a click. This makes Chrome associate
1789 // subsequent mousemove events with the scroll-blocker. Since the
1790 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1791 // events do not cause the scrollport to scroll.
1792 //
1793 // It's a hack, but it's the cleanest way I could find.
1794 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001795 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001796 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001797 this.scrollBlockerNode_.style.cssText =
1798 ('position: absolute;' +
1799 'top: -99px;' +
1800 'display: block;' +
1801 'width: 10px;' +
1802 'height: 10px;');
1803 this.document_.body.appendChild(this.scrollBlockerNode_);
1804
rgindad5613292012-06-19 15:40:37 -07001805 this.scrollPort_.onScrollWheel = onMouse;
1806 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1807 ].forEach(function(event) {
1808 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001809 this.cursorNode_.addEventListener(
1810 event, /** @type {!EventListener} */ (onMouse));
1811 this.document_.addEventListener(
1812 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001813 }.bind(this));
1814
1815 this.cursorNode_.addEventListener('mousedown', function() {
1816 setTimeout(this.focus.bind(this));
1817 }.bind(this));
1818
rginda8ba33642011-12-14 12:31:31 -08001819 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001820
rginda87b86462011-12-14 13:48:03 -08001821 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001822 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001823};
1824
rginda0918b652012-04-04 11:26:24 -07001825/**
1826 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001827 *
Joel Hockey0f933582019-08-27 18:01:51 -07001828 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001829 */
rginda87b86462011-12-14 13:48:03 -08001830hterm.Terminal.prototype.getDocument = function() {
1831 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001832};
1833
1834/**
rginda0918b652012-04-04 11:26:24 -07001835 * Focus the terminal.
1836 */
1837hterm.Terminal.prototype.focus = function() {
1838 this.scrollPort_.focus();
1839};
1840
1841/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001842 * Unfocus the terminal.
1843 */
1844hterm.Terminal.prototype.blur = function() {
1845 this.scrollPort_.blur();
1846};
1847
1848/**
rginda8ba33642011-12-14 12:31:31 -08001849 * Return the HTML Element for a given row index.
1850 *
1851 * This is a method from the RowProvider interface. The ScrollPort uses
1852 * it to fetch rows on demand as they are scrolled into view.
1853 *
1854 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1855 * pairs to conserve memory.
1856 *
Joel Hockey0f933582019-08-27 18:01:51 -07001857 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001858 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001859 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001860 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001861 * @override
rginda8ba33642011-12-14 12:31:31 -08001862 */
1863hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001864 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001865 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001866 }
rginda8ba33642011-12-14 12:31:31 -08001867
Mike Frysingerdc727792020-04-10 01:41:13 -04001868 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001869 return this.screen_.rowsArray[screenIndex];
1870};
1871
1872/**
1873 * Return the text content for a given range of rows.
1874 *
1875 * This is a method from the RowProvider interface. The ScrollPort uses
1876 * it to fetch text content on demand when the user attempts to copy their
1877 * selection to the clipboard.
1878 *
Joel Hockey0f933582019-08-27 18:01:51 -07001879 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001880 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001881 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001882 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001883 * relative to the start of the scrollback buffer.
1884 * @return {string} A single string containing the text value of the range of
1885 * rows. Lines will be newline delimited, with no trailing newline.
1886 */
1887hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001888 const ary = [];
1889 for (let i = start; i < end; i++) {
1890 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001891 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001892 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001893 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001894 }
rginda8ba33642011-12-14 12:31:31 -08001895 }
1896
rgindaa09e7332012-08-17 12:49:51 -07001897 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001898};
1899
1900/**
1901 * Return the text content for a given row.
1902 *
1903 * This is a method from the RowProvider interface. The ScrollPort uses
1904 * it to fetch text content on demand when the user attempts to copy their
1905 * selection to the clipboard.
1906 *
Joel Hockey0f933582019-08-27 18:01:51 -07001907 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001908 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001909 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001910 * @return {string} A string containing the text value of the selected row.
1911 */
1912hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001913 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001914 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001915};
1916
1917/**
1918 * Return the total number of rows in the addressable screen and in the
1919 * scrollback buffer of this terminal.
1920 *
1921 * This is a method from the RowProvider interface. The ScrollPort uses
1922 * it to compute the size of the scrollbar.
1923 *
Joel Hockey0f933582019-08-27 18:01:51 -07001924 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001925 * @override
rginda8ba33642011-12-14 12:31:31 -08001926 */
1927hterm.Terminal.prototype.getRowCount = function() {
1928 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1929};
1930
1931/**
1932 * Create DOM nodes for new rows and append them to the end of the terminal.
1933 *
1934 * This is the only correct way to add a new DOM node for a row. Notice that
1935 * the new row is appended to the bottom of the list of rows, and does not
1936 * require renumbering (of the rowIndex property) of previous rows.
1937 *
1938 * If you think you want a new blank row somewhere in the middle of the
1939 * terminal, look into moveRows_().
1940 *
1941 * This method does not pay attention to vtScrollTop/Bottom, since you should
1942 * be using moveRows() in cases where they would matter.
1943 *
1944 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001945 *
1946 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001947 */
1948hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001949 let cursorRow = this.screen_.rowsArray.length;
1950 const offset = this.scrollbackRows_.length + cursorRow;
1951 for (let i = 0; i < count; i++) {
1952 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08001953 row.appendChild(this.document_.createTextNode(''));
1954 row.rowIndex = offset + i;
1955 this.screen_.pushRow(row);
1956 }
1957
Mike Frysingerdc727792020-04-10 01:41:13 -04001958 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001959 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001960 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08001961 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001962 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07001963 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04001964 }
rginda8ba33642011-12-14 12:31:31 -08001965 }
1966
Mike Frysingerbdb34802020-04-07 03:47:32 -04001967 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08001968 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001969 }
rginda8ba33642011-12-14 12:31:31 -08001970
rginda87b86462011-12-14 13:48:03 -08001971 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001972};
1973
1974/**
1975 * Relocate rows from one part of the addressable screen to another.
1976 *
1977 * This is used to recycle rows during VT scrolls (those which are driven
1978 * by VT commands, rather than by the user manipulating the scrollbar.)
1979 *
1980 * In this case, the blank lines scrolled into the scroll region are made of
1981 * the nodes we scrolled off. These have their rowIndex properties carefully
1982 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001983 *
1984 * @param {number} fromIndex The start index.
1985 * @param {number} count The number of rows to move.
1986 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001987 */
1988hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001989 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08001990 this.screen_.insertRows(toIndex, ary);
1991
Mike Frysingerdc727792020-04-10 01:41:13 -04001992 let start, end;
rginda8ba33642011-12-14 12:31:31 -08001993 if (fromIndex < toIndex) {
1994 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001995 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001996 } else {
1997 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001998 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001999 }
2000
2001 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002002 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
2006 * Renumber the rowIndex property of the given range of rows.
2007 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002008 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002009 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002010 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002011 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002012 *
2013 * @param {number} start The start index.
2014 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002015 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002016 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002017hterm.Terminal.prototype.renumberRows_ = function(
2018 start, end, screen = undefined) {
2019 if (!screen) {
2020 screen = this.screen_;
2021 }
Robert Ginda40932892012-12-10 17:26:40 -08002022
Mike Frysingerdc727792020-04-10 01:41:13 -04002023 const offset = this.scrollbackRows_.length;
2024 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002025 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002026 }
2027};
2028
2029/**
2030 * Print a string to the terminal.
2031 *
2032 * This respects the current insert and wraparound modes. It will add new lines
2033 * to the end of the terminal, scrolling off the top into the scrollback buffer
2034 * if necessary.
2035 *
2036 * The string is *not* parsed for escape codes. Use the interpret() method if
2037 * that's what you're after.
2038 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002039 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002040 */
2041hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002042 this.scheduleSyncCursorPosition_();
2043
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002044 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002045 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002046
Mike Frysingerdc727792020-04-10 01:41:13 -04002047 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002048
Mike Frysingerdc727792020-04-10 01:41:13 -04002049 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002050 // Fun edge case: If the string only contains zero width codepoints (like
2051 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002052 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002053 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002054 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002055
2056 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002057 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2058 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002059 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002060 }
rgindaa19afe22012-01-25 15:40:22 -08002061
Mike Frysingerdc727792020-04-10 01:41:13 -04002062 let count = strWidth - startOffset;
2063 let didOverflow = false;
2064 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002065
rgindaa9abdd82012-08-06 18:05:09 -07002066 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2067 didOverflow = true;
2068 count = this.screenSize.width - this.screen_.cursorPosition.column;
2069 }
rgindaa19afe22012-01-25 15:40:22 -08002070
rgindaa9abdd82012-08-06 18:05:09 -07002071 if (didOverflow && !this.options_.wraparound) {
2072 // If the string overflowed the line but wraparound is off, then the
2073 // last printed character should be the last of the string.
2074 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002075 substr = lib.wc.substr(str, startOffset, count - 1) +
2076 lib.wc.substr(str, strWidth - 1);
2077 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002078 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002079 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002080 }
rgindaa19afe22012-01-25 15:40:22 -08002081
Mike Frysingerdc727792020-04-10 01:41:13 -04002082 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2083 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002084 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2085 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002086
2087 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002088 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002089 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002090 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002091 }
2092 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002093 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002094 }
2095
2096 this.screen_.maybeClipCurrentRow();
2097 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002098 }
rginda8ba33642011-12-14 12:31:31 -08002099
Mike Frysingerbdb34802020-04-07 03:47:32 -04002100 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002101 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002102 }
rginda8ba33642011-12-14 12:31:31 -08002103};
2104
2105/**
rginda87b86462011-12-14 13:48:03 -08002106 * Set the VT scroll region.
2107 *
rginda87b86462011-12-14 13:48:03 -08002108 * This also resets the cursor position to the absolute (0, 0) position, since
2109 * that's what xterm appears to do.
2110 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002111 * Setting the scroll region to the full height of the terminal will clear
2112 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2113 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2114 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2115 * continue to work as most users would expect.
2116 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002117 * @param {?number} scrollTop The zero-based top of the scroll region.
2118 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002119 * inclusive.
2120 */
2121hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002122 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002123 this.vtScrollTop_ = null;
2124 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002125 } else {
2126 this.vtScrollTop_ = scrollTop;
2127 this.vtScrollBottom_ = scrollBottom;
2128 }
rginda87b86462011-12-14 13:48:03 -08002129};
2130
2131/**
rginda8ba33642011-12-14 12:31:31 -08002132 * Return the top row index according to the VT.
2133 *
2134 * This will return 0 unless the terminal has been told to restrict scrolling
2135 * to some lower row. It is used for some VT cursor positioning and scrolling
2136 * commands.
2137 *
Joel Hockey0f933582019-08-27 18:01:51 -07002138 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002139 */
2140hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002141 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002142 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002143 }
rginda8ba33642011-12-14 12:31:31 -08002144
2145 return 0;
rginda87b86462011-12-14 13:48:03 -08002146};
rginda8ba33642011-12-14 12:31:31 -08002147
2148/**
2149 * Return the bottom row index according to the VT.
2150 *
2151 * This will return the height of the terminal unless the it has been told to
2152 * restrict scrolling to some higher row. It is used for some VT cursor
2153 * positioning and scrolling commands.
2154 *
Joel Hockey0f933582019-08-27 18:01:51 -07002155 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002156 */
2157hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002158 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002159 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002160 }
rginda8ba33642011-12-14 12:31:31 -08002161
rginda87b86462011-12-14 13:48:03 -08002162 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002163};
rginda8ba33642011-12-14 12:31:31 -08002164
2165/**
2166 * Process a '\n' character.
2167 *
2168 * If the cursor is on the final row of the terminal this will append a new
2169 * blank row to the screen and scroll the topmost row into the scrollback
2170 * buffer.
2171 *
2172 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002173 *
2174 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2175 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002176 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002177hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002178 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002179 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002180 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002181
Mike Frysingerdc727792020-04-10 01:41:13 -04002182 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2183 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002184
2185 if (this.vtScrollBottom_ != null) {
2186 // A VT Scroll region is active, we never append new rows.
2187 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2188 // We're at the end of the VT Scroll Region, perform a VT scroll.
2189 this.vtScrollUp(1);
2190 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2191 } else if (cursorAtEndOfScreen) {
2192 // We're at the end of the screen, the only thing to do is put the
2193 // cursor to column 0.
2194 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2195 } else {
2196 // Anywhere else, advance the cursor row, and reset the column.
2197 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2198 }
2199 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002200 // We're at the end of the screen. Append a new row to the terminal,
2201 // shifting the top row into the scrollback.
2202 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002203 } else {
rginda87b86462011-12-14 13:48:03 -08002204 // Anywhere else in the screen just moves the cursor.
2205 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002206 }
2207};
2208
2209/**
2210 * Like newLine(), except maintain the cursor column.
2211 */
2212hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002213 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002214 this.newLine();
2215 this.setCursorColumn(column);
2216};
2217
2218/**
rginda87b86462011-12-14 13:48:03 -08002219 * If autoCarriageReturn is set then newLine(), else lineFeed().
2220 */
2221hterm.Terminal.prototype.formFeed = function() {
2222 if (this.options_.autoCarriageReturn) {
2223 this.newLine();
2224 } else {
2225 this.lineFeed();
2226 }
2227};
2228
2229/**
2230 * Move the cursor up one row, possibly inserting a blank line.
2231 *
2232 * The cursor column is not changed.
2233 */
2234hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002235 const scrollTop = this.getVTScrollTop();
2236 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002237
2238 if (currentRow == scrollTop) {
2239 this.insertLines(1);
2240 } else {
2241 this.setAbsoluteCursorRow(currentRow - 1);
2242 }
2243};
2244
2245/**
rginda8ba33642011-12-14 12:31:31 -08002246 * Replace all characters to the left of the current cursor with the space
2247 * character.
2248 *
2249 * TODO(rginda): This should probably *remove* the characters (not just replace
2250 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002251 * position.
rginda8ba33642011-12-14 12:31:31 -08002252 */
2253hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002254 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002255 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002256 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002257 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002258 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002259};
2260
2261/**
David Benjamin684a9b72012-05-01 17:19:58 -04002262 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002263 *
2264 * The cursor position is unchanged.
2265 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002266 * If the current background color is not the default background color this
2267 * will insert spaces rather than delete. This is unfortunate because the
2268 * trailing space will affect text selection, but it's difficult to come up
2269 * with a way to style empty space that wouldn't trip up the hterm.Screen
2270 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002271 *
2272 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2273 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2274 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002275 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002276 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002277 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002278hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002279 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002280 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002281 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002282
Mike Frysingerdc727792020-04-10 01:41:13 -04002283 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002284 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002285
2286 if (this.screen_.textAttributes.background ===
2287 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002288 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002289 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002290 this.screen_.cursorPosition.column + count) {
2291 this.screen_.deleteChars(count);
2292 this.clearCursorOverflow();
2293 return;
2294 }
2295 }
2296
Mike Frysingerdc727792020-04-10 01:41:13 -04002297 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002298 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002299 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002300 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002301};
2302
2303/**
2304 * Erase the current line.
2305 *
2306 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002307 */
2308hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002309 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002310 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002311 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002312 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002313};
2314
2315/**
David Benjamina08d78f2012-05-05 00:28:49 -04002316 * Erase all characters from the start of the screen to the current cursor
2317 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002318 *
2319 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002320 */
2321hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002322 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002323
2324 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002325
Mike Frysingerdc727792020-04-10 01:41:13 -04002326 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002327 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002328 this.screen_.clearCursorRow();
2329 }
2330
rginda87b86462011-12-14 13:48:03 -08002331 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002332 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002333};
2334
2335/**
2336 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002337 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002338 *
2339 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002340 */
2341hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002342 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002343
2344 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002345
Mike Frysingerdc727792020-04-10 01:41:13 -04002346 const bottom = this.screenSize.height - 1;
2347 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002348 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002349 this.screen_.clearCursorRow();
2350 }
2351
rginda87b86462011-12-14 13:48:03 -08002352 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002353 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002354};
2355
2356/**
2357 * Fill the terminal with a given character.
2358 *
2359 * This methods does not respect the VT scroll region.
2360 *
2361 * @param {string} ch The character to use for the fill.
2362 */
2363hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002364 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002365
2366 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002367 for (let row = 0; row < this.screenSize.height; row++) {
2368 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002369 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002370 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002371 }
2372 }
2373
2374 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002375};
2376
2377/**
rginda9ea433c2012-03-16 11:57:00 -07002378 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002379 *
rginda9ea433c2012-03-16 11:57:00 -07002380 * This does not respect the scroll region.
2381 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002382 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002383 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002384 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002385hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2386 if (!screen) {
2387 screen = this.screen_;
2388 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002389 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002390
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002391 this.accessibilityReader_.clear();
2392
rginda11057d52012-04-25 12:29:56 -07002393 if (bottom == 0) {
2394 // Empty screen, nothing to do.
2395 return;
2396 }
2397
Mike Frysingerdc727792020-04-10 01:41:13 -04002398 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002399 screen.setCursorPosition(i, 0);
2400 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002401 }
2402
rginda9ea433c2012-03-16 11:57:00 -07002403 screen.setCursorPosition(0, 0);
2404};
2405
2406/**
2407 * Erase the entire display without changing the cursor position.
2408 *
2409 * The cursor position is unchanged. This does not respect the scroll
2410 * region.
2411 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002412 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002413 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002414 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002415hterm.Terminal.prototype.clear = function(screen = undefined) {
2416 if (!screen) {
2417 screen = this.screen_;
2418 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002419 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002420 this.clearHome(screen);
2421 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002422};
2423
2424/**
2425 * VT command to insert lines at the current cursor row.
2426 *
2427 * This respects the current scroll region. Rows pushed off the bottom are
2428 * lost (they won't show up in the scrollback buffer).
2429 *
Joel Hockey0f933582019-08-27 18:01:51 -07002430 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002431 */
2432hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002433 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002434
Mike Frysingerdc727792020-04-10 01:41:13 -04002435 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002436 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002437
Robert Ginda579186b2012-09-26 11:40:04 -07002438 // The moveCount is the number of rows we need to relocate to make room for
2439 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002440 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002441 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002442 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002443 }
rginda8ba33642011-12-14 12:31:31 -08002444
Mike Frysingerdc727792020-04-10 01:41:13 -04002445 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002446 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002447 this.screen_.clearCursorRow();
2448 }
rginda8ba33642011-12-14 12:31:31 -08002449};
2450
2451/**
2452 * VT command to delete lines at the current cursor row.
2453 *
2454 * New rows are added to the bottom of scroll region to take their place. New
2455 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002456 *
2457 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002458 */
2459hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002460 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002461
Mike Frysingerdc727792020-04-10 01:41:13 -04002462 const top = cursor.row;
2463 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002464
Mike Frysingerdc727792020-04-10 01:41:13 -04002465 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002466 count = Math.min(count, maxCount);
2467
Mike Frysingerdc727792020-04-10 01:41:13 -04002468 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002469 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002470 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002471 }
rginda8ba33642011-12-14 12:31:31 -08002472
Mike Frysingerdc727792020-04-10 01:41:13 -04002473 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002474 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002475 this.screen_.clearCursorRow();
2476 }
2477
rginda87b86462011-12-14 13:48:03 -08002478 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002479 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002480};
2481
2482/**
2483 * Inserts the given number of spaces at the current cursor position.
2484 *
rginda87b86462011-12-14 13:48:03 -08002485 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002486 *
2487 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002488 */
2489hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002490 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002491
Mike Frysinger73e56462019-07-17 00:23:46 -05002492 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002493 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002494 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002495
2496 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002497 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002498};
2499
2500/**
2501 * Forward-delete the specified number of characters starting at the cursor
2502 * position.
2503 *
Joel Hockey0f933582019-08-27 18:01:51 -07002504 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002505 */
2506hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002507 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002508 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002509 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002510 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002511 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002512 this.restoreCursor(cursor);
2513 }
2514
David Benjamin54e8bf62012-06-01 22:31:40 -04002515 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002516};
2517
2518/**
2519 * Shift rows in the scroll region upwards by a given number of lines.
2520 *
2521 * New rows are inserted at the bottom of the scroll region to fill the
2522 * vacated rows. The new rows not filled out with the current text attributes.
2523 *
2524 * This function does not affect the scrollback rows at all. Rows shifted
2525 * off the top are lost.
2526 *
rginda87b86462011-12-14 13:48:03 -08002527 * The cursor position is not altered.
2528 *
Joel Hockey0f933582019-08-27 18:01:51 -07002529 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002530 */
2531hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002532 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002533
rginda87b86462011-12-14 13:48:03 -08002534 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002535 this.deleteLines(count);
2536
rginda87b86462011-12-14 13:48:03 -08002537 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002538};
2539
2540/**
2541 * Shift rows below the cursor down by a given number of lines.
2542 *
2543 * This function respects the current scroll region.
2544 *
2545 * New rows are inserted at the top of the scroll region to fill the
2546 * vacated rows. The new rows not filled out with the current text attributes.
2547 *
2548 * This function does not affect the scrollback rows at all. Rows shifted
2549 * off the bottom are lost.
2550 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002551 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002552 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002553hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002554 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002555
rginda87b86462011-12-14 13:48:03 -08002556 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002557 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002558
rginda87b86462011-12-14 13:48:03 -08002559 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002560};
2561
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002562/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002563 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002564 *
2565 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002566 * cause Assitive Technology to announce the output of the terminal. It also
2567 * enables other features that aid assistive technology. All the features gated
2568 * behind this flag have a performance impact on the terminal which is why they
2569 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002570 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002571 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002572 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002573hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002574 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002575};
rginda87b86462011-12-14 13:48:03 -08002576
rginda8ba33642011-12-14 12:31:31 -08002577/**
2578 * Set the cursor position.
2579 *
2580 * The cursor row is relative to the scroll region if the terminal has
2581 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2582 *
Joel Hockey0f933582019-08-27 18:01:51 -07002583 * @param {number} row The new zero-based cursor row.
2584 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002585 */
2586hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2587 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002588 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002589 } else {
rginda87b86462011-12-14 13:48:03 -08002590 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002591 }
rginda87b86462011-12-14 13:48:03 -08002592};
rginda8ba33642011-12-14 12:31:31 -08002593
Evan Jones2600d4f2016-12-06 09:29:36 -05002594/**
2595 * Move the cursor relative to its current position.
2596 *
2597 * @param {number} row
2598 * @param {number} column
2599 */
rginda87b86462011-12-14 13:48:03 -08002600hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002601 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002602 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2603 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002604 this.screen_.setCursorPosition(row, column);
2605};
2606
Evan Jones2600d4f2016-12-06 09:29:36 -05002607/**
2608 * Move the cursor to the specified position.
2609 *
2610 * @param {number} row
2611 * @param {number} column
2612 */
rginda87b86462011-12-14 13:48:03 -08002613hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002614 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2615 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002616 this.screen_.setCursorPosition(row, column);
2617};
2618
2619/**
2620 * Set the cursor column.
2621 *
Joel Hockey0f933582019-08-27 18:01:51 -07002622 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002623 */
2624hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002625 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002626};
2627
2628/**
2629 * Return the cursor column.
2630 *
Joel Hockey0f933582019-08-27 18:01:51 -07002631 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002632 */
2633hterm.Terminal.prototype.getCursorColumn = function() {
2634 return this.screen_.cursorPosition.column;
2635};
2636
2637/**
2638 * Set the cursor row.
2639 *
2640 * The cursor row is relative to the scroll region if the terminal has
2641 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2642 *
Joel Hockey0f933582019-08-27 18:01:51 -07002643 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002644 */
rginda87b86462011-12-14 13:48:03 -08002645hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2646 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002647};
2648
2649/**
2650 * Return the cursor row.
2651 *
Joel Hockey0f933582019-08-27 18:01:51 -07002652 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002653 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002654hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002655 return this.screen_.cursorPosition.row;
2656};
2657
2658/**
2659 * Request that the ScrollPort redraw itself soon.
2660 *
2661 * The redraw will happen asynchronously, soon after the call stack winds down.
2662 * Multiple calls will be coalesced into a single redraw.
2663 */
2664hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002665 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002666 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002667 }
rginda8ba33642011-12-14 12:31:31 -08002668
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002669 this.timeouts_.redraw = setTimeout(() => {
2670 delete this.timeouts_.redraw;
2671 this.scrollPort_.redraw_();
2672 });
rginda8ba33642011-12-14 12:31:31 -08002673};
2674
2675/**
2676 * Request that the ScrollPort be scrolled to the bottom.
2677 *
2678 * The scroll will happen asynchronously, soon after the call stack winds down.
2679 * Multiple calls will be coalesced into a single scroll.
2680 *
2681 * This affects the scrollbar position of the ScrollPort, and has nothing to
2682 * do with the VT scroll commands.
2683 */
2684hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002685 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002686 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002687 }
rginda8ba33642011-12-14 12:31:31 -08002688
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002689 this.timeouts_.scrollDown = setTimeout(() => {
2690 delete this.timeouts_.scrollDown;
2691 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2692 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002693};
2694
2695/**
2696 * Move the cursor up a specified number of rows.
2697 *
Joel Hockey0f933582019-08-27 18:01:51 -07002698 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002699 */
2700hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002701 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002702};
2703
2704/**
2705 * Move the cursor down a specified number of rows.
2706 *
Joel Hockey0f933582019-08-27 18:01:51 -07002707 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002708 */
2709hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002710 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002711 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2712 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2713 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002714
Mike Frysingerdc727792020-04-10 01:41:13 -04002715 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2716 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002717 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002718};
2719
2720/**
2721 * Move the cursor left a specified number of columns.
2722 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002723 * If reverse wraparound mode is enabled and the previous row wrapped into
2724 * the current row then we back up through the wraparound as well.
2725 *
Joel Hockey0f933582019-08-27 18:01:51 -07002726 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002727 */
2728hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002729 count = count || 1;
2730
Mike Frysingerbdb34802020-04-07 03:47:32 -04002731 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002732 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002733 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002734
Mike Frysingerdc727792020-04-10 01:41:13 -04002735 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002736 if (this.options_.reverseWraparound) {
2737 if (this.screen_.cursorPosition.overflow) {
2738 // If this cursor is in the right margin, consume one count to get it
2739 // back to the last column. This only applies when we're in reverse
2740 // wraparound mode.
2741 count--;
2742 this.clearCursorOverflow();
2743
Mike Frysingerbdb34802020-04-07 03:47:32 -04002744 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002745 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002746 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002747 }
2748
Mike Frysingerdc727792020-04-10 01:41:13 -04002749 let newRow = this.screen_.cursorPosition.row;
2750 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002751 if (newColumn < 0) {
2752 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2753 if (newRow < 0) {
2754 // xterm also wraps from row 0 to the last row.
2755 newRow = this.screenSize.height + newRow % this.screenSize.height;
2756 }
2757 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2758 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002759
Robert Gindabfb32622014-07-17 13:20:27 -07002760 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2761
2762 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002763 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002764 this.setCursorColumn(newColumn);
2765 }
rginda8ba33642011-12-14 12:31:31 -08002766};
2767
2768/**
2769 * Move the cursor right a specified number of columns.
2770 *
Joel Hockey0f933582019-08-27 18:01:51 -07002771 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002772 */
2773hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002774 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002775
Mike Frysingerbdb34802020-04-07 03:47:32 -04002776 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002777 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002778 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002779
Mike Frysingerdc727792020-04-10 01:41:13 -04002780 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2781 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002782 this.setCursorColumn(column);
2783};
2784
2785/**
2786 * Reverse the foreground and background colors of the terminal.
2787 *
2788 * This only affects text that was drawn with no attributes.
2789 *
2790 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2791 * been drawn with attributes that happen to coincide with the default
2792 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002793 *
2794 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002795 */
2796hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002797 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002798 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002799 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2800 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002801 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002802 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2803 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002804 }
2805};
2806
2807/**
rginda87b86462011-12-14 13:48:03 -08002808 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002809 *
2810 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002811 */
2812hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002813 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002814
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002815 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002816
Michael Kelly485ecd12014-06-09 11:41:56 -04002817 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002818 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002819 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002820 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002821
Robert Ginda92e18102013-03-14 13:56:37 -07002822 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002823 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002824 this.bellSequelchTimeout_ = setTimeout(() => {
2825 this.bellSquelchTimeout_ = null;
2826 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002827 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002828 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002829 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002830
2831 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002832 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002833 this.bellNotificationList_.push(n);
2834 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002835 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002836 }
rginda87b86462011-12-14 13:48:03 -08002837};
2838
2839/**
rginda8ba33642011-12-14 12:31:31 -08002840 * Set the origin mode bit.
2841 *
2842 * If origin mode is on, certain VT cursor and scrolling commands measure their
2843 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2844 * to the top of the addressable screen.
2845 *
2846 * Defaults to off.
2847 *
2848 * @param {boolean} state True to set origin mode, false to unset.
2849 */
2850hterm.Terminal.prototype.setOriginMode = function(state) {
2851 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002852 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002853};
2854
2855/**
2856 * Set the insert mode bit.
2857 *
2858 * If insert mode is on, existing text beyond the cursor position will be
2859 * shifted right to make room for new text. Otherwise, new text overwrites
2860 * any existing text.
2861 *
2862 * Defaults to off.
2863 *
2864 * @param {boolean} state True to set insert mode, false to unset.
2865 */
2866hterm.Terminal.prototype.setInsertMode = function(state) {
2867 this.options_.insertMode = state;
2868};
2869
2870/**
rginda87b86462011-12-14 13:48:03 -08002871 * Set the auto carriage return bit.
2872 *
2873 * If auto carriage return is on then a formfeed character is interpreted
2874 * as a newline, otherwise it's the same as a linefeed. The difference boils
2875 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002876 *
2877 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002878 */
2879hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2880 this.options_.autoCarriageReturn = state;
2881};
2882
2883/**
rginda8ba33642011-12-14 12:31:31 -08002884 * Set the wraparound mode bit.
2885 *
2886 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2887 * to the start of the following row. Otherwise, the cursor is clamped to the
2888 * end of the screen and attempts to write past it are ignored.
2889 *
2890 * Defaults to on.
2891 *
2892 * @param {boolean} state True to set wraparound mode, false to unset.
2893 */
2894hterm.Terminal.prototype.setWraparound = function(state) {
2895 this.options_.wraparound = state;
2896};
2897
2898/**
2899 * Set the reverse-wraparound mode bit.
2900 *
2901 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2902 * to the end of the previous row. Otherwise, the cursor is clamped to column
2903 * 0.
2904 *
2905 * Defaults to off.
2906 *
2907 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2908 */
2909hterm.Terminal.prototype.setReverseWraparound = function(state) {
2910 this.options_.reverseWraparound = state;
2911};
2912
2913/**
2914 * Selects between the primary and alternate screens.
2915 *
2916 * If alternate mode is on, the alternate screen is active. Otherwise the
2917 * primary screen is active.
2918 *
2919 * Swapping screens has no effect on the scrollback buffer.
2920 *
2921 * Each screen maintains its own cursor position.
2922 *
2923 * Defaults to off.
2924 *
2925 * @param {boolean} state True to set alternate mode, false to unset.
2926 */
2927hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002928 if (state == (this.screen_ == this.alternateScreen_)) {
2929 return;
2930 }
2931 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2932 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002933 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2934
Joel Hockey42dba8f2020-03-26 16:21:11 -07002935 // Swap color overrides.
2936 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2937 oldOverrides.forEach((c, i) => {
2938 if (!newOverrides.hasOwnProperty(i)) {
2939 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2940 }
2941 });
2942 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2943
rginda35c456b2012-02-09 17:29:05 -08002944 if (this.screen_.rowsArray.length &&
2945 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2946 // If the screen changed sizes while we were away, our rowIndexes may
2947 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002948 const offset = this.scrollbackRows_.length;
2949 const ary = this.screen_.rowsArray;
2950 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002951 ary[i].rowIndex = offset + i;
2952 }
2953 }
rginda8ba33642011-12-14 12:31:31 -08002954
rginda35c456b2012-02-09 17:29:05 -08002955 this.realizeWidth_(this.screenSize.width);
2956 this.realizeHeight_(this.screenSize.height);
2957 this.scrollPort_.syncScrollHeight();
2958 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002959
rginda6d397402012-01-17 10:58:29 -08002960 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002961 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002962};
2963
2964/**
2965 * Set the cursor-blink mode bit.
2966 *
2967 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2968 * a visible cursor does not blink.
2969 *
2970 * You should make sure to turn blinking off if you're going to dispose of a
2971 * terminal, otherwise you'll leak a timeout.
2972 *
2973 * Defaults to on.
2974 *
2975 * @param {boolean} state True to set cursor-blink mode, false to unset.
2976 */
2977hterm.Terminal.prototype.setCursorBlink = function(state) {
2978 this.options_.cursorBlink = state;
2979
2980 if (!state && this.timeouts_.cursorBlink) {
2981 clearTimeout(this.timeouts_.cursorBlink);
2982 delete this.timeouts_.cursorBlink;
2983 }
2984
Mike Frysingerbdb34802020-04-07 03:47:32 -04002985 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08002986 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002987 }
rginda8ba33642011-12-14 12:31:31 -08002988};
2989
2990/**
2991 * Set the cursor-visible mode bit.
2992 *
2993 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2994 *
2995 * Defaults to on.
2996 *
2997 * @param {boolean} state True to set cursor-visible mode, false to unset.
2998 */
2999hterm.Terminal.prototype.setCursorVisible = function(state) {
3000 this.options_.cursorVisible = state;
3001
3002 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003003 if (this.timeouts_.cursorBlink) {
3004 clearTimeout(this.timeouts_.cursorBlink);
3005 delete this.timeouts_.cursorBlink;
3006 }
rginda87b86462011-12-14 13:48:03 -08003007 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003008 return;
3009 }
3010
rginda87b86462011-12-14 13:48:03 -08003011 this.syncCursorPosition_();
3012
3013 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003014
3015 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003016 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003017 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003018 }
rginda8ba33642011-12-14 12:31:31 -08003019
Robert Gindaea2183e2014-07-17 09:51:51 -07003020 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003021 } else {
3022 if (this.timeouts_.cursorBlink) {
3023 clearTimeout(this.timeouts_.cursorBlink);
3024 delete this.timeouts_.cursorBlink;
3025 }
3026 }
3027};
3028
3029/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003030 * Pause blinking temporarily.
3031 *
3032 * When the cursor moves around, it can be helpful to momentarily pause the
3033 * blinking. This could be when the user is typing in things, or when they're
3034 * moving around with the arrow keys.
3035 */
3036hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3037 if (!this.options_.cursorBlink) {
3038 return;
3039 }
3040
3041 this.cursorBlinkPause_ = true;
3042
3043 // If a timeout is already pending, reset the clock due to the new input.
3044 if (this.timeouts_.cursorBlinkPause) {
3045 clearTimeout(this.timeouts_.cursorBlinkPause);
3046 }
3047 // After 500ms, resume blinking. That seems like a good balance between user
3048 // input timings & responsiveness to resume.
3049 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3050 delete this.timeouts_.cursorBlinkPause;
3051 this.cursorBlinkPause_ = false;
3052 }, 500);
3053};
3054
3055/**
rginda87b86462011-12-14 13:48:03 -08003056 * Synchronizes the visible cursor and document selection with the current
3057 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003058 *
3059 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003060 */
3061hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003062 const topRowIndex = this.scrollPort_.getTopRowIndex();
3063 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3064 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003065 this.screen_.cursorPosition.row;
3066
Raymes Khoury15697f42018-07-17 11:37:18 +10003067 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003068 if (this.accessibilityReader_.accessibilityEnabled) {
3069 // Report the new position of the cursor for accessibility purposes.
3070 const cursorColumnIndex = this.screen_.cursorPosition.column;
3071 const cursorLineText =
3072 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003073 // This will force the selection to be sync'd to the cursor position if the
3074 // user has pressed a key. Generally we would only sync the cursor position
3075 // when selection is collapsed so that if the user has selected something
3076 // we don't clear the selection by moving the selection. However when a
3077 // screen reader is used, it's intuitive for entering a key to move the
3078 // selection to the cursor.
3079 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003080 this.accessibilityReader_.afterCursorChange(
3081 cursorLineText, cursorRowIndex, cursorColumnIndex);
3082 }
3083
rginda8ba33642011-12-14 12:31:31 -08003084 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003085 // Cursor is scrolled off screen, hide it.
3086 this.cursorOffScreen_ = true;
3087 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003088 return false;
rginda8ba33642011-12-14 12:31:31 -08003089 }
3090
Joel Hockey3babf302020-04-22 15:00:06 -07003091 if (this.cursorNode_.style.display == 'none') {
3092 // Re-display the terminal cursor if it was hidden.
3093 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003094 this.cursorNode_.style.display = '';
3095 }
3096
Mike Frysinger44c32202017-08-05 01:13:09 -04003097 // Position the cursor using CSS variable math. If we do the math in JS,
3098 // the float math will end up being more precise than the CSS which will
3099 // cause the cursor tracking to be off.
3100 this.setCssVar(
3101 'cursor-offset-row',
3102 `${cursorRowIndex - topRowIndex} + ` +
3103 `${this.scrollPort_.visibleRowTopMargin}px`);
3104 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003105
3106 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003107 '(' + this.screen_.cursorPosition.column +
3108 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003109 ')');
3110
3111 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003112 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003113 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003114 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003115 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003116 return true;
rginda8ba33642011-12-14 12:31:31 -08003117};
3118
Robert Gindafb1be6a2013-12-11 11:56:22 -08003119/**
3120 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3121 * and character cell dimensions.
3122 */
Robert Ginda830583c2013-08-07 13:20:46 -07003123hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003124 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003125
3126 if (this.cursorNode_.getAttribute('focus') == 'false') {
3127 // Always show a block cursor when unfocused.
3128 shape = hterm.Terminal.cursorShape.BLOCK;
3129 }
3130
Mike Frysingerdc727792020-04-10 01:41:13 -04003131 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003132
3133 switch (shape) {
3134 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003135 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003136 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003137 style.borderLeftStyle = 'solid';
3138 break;
3139
3140 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003141 style.backgroundColor = 'transparent';
3142 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003143 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003144 break;
3145
3146 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003147 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003148 style.borderBottomStyle = '';
3149 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003150 break;
3151 }
3152};
3153
rginda8ba33642011-12-14 12:31:31 -08003154/**
3155 * Synchronizes the visible cursor with the current cursor coordinates.
3156 *
3157 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003158 * Multiple calls will be coalesced into a single sync. This should be called
3159 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003160 */
3161hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003162 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003163 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003164 }
rginda8ba33642011-12-14 12:31:31 -08003165
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003166 if (this.accessibilityReader_.accessibilityEnabled) {
3167 // Report the previous position of the cursor for accessibility purposes.
3168 const cursorRowIndex = this.scrollbackRows_.length +
3169 this.screen_.cursorPosition.row;
3170 const cursorColumnIndex = this.screen_.cursorPosition.column;
3171 const cursorLineText =
3172 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3173 this.accessibilityReader_.beforeCursorChange(
3174 cursorLineText, cursorRowIndex, cursorColumnIndex);
3175 }
3176
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003177 this.timeouts_.syncCursor = setTimeout(() => {
3178 this.syncCursorPosition_();
3179 delete this.timeouts_.syncCursor;
3180 });
rginda87b86462011-12-14 13:48:03 -08003181};
3182
rgindacc2996c2012-02-24 14:59:31 -08003183/**
rgindaf522ce02012-04-17 17:49:17 -07003184 * Show or hide the zoom warning.
3185 *
3186 * The zoom warning is a message warning the user that their browser zoom must
3187 * be set to 100% in order for hterm to function properly.
3188 *
3189 * @param {boolean} state True to show the message, false to hide it.
3190 */
3191hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3192 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003193 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003194 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003195 }
rgindaf522ce02012-04-17 17:49:17 -07003196
3197 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003198 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003199 this.zoomWarningNode_.style.cssText = (
3200 'color: black;' +
3201 'background-color: #ff2222;' +
3202 'font-size: large;' +
3203 'border-radius: 8px;' +
3204 'opacity: 0.75;' +
3205 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3206 'top: 0.5em;' +
3207 'right: 1.2em;' +
3208 'position: absolute;' +
3209 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003210 '-webkit-user-select: none;' +
3211 '-moz-text-size-adjust: none;' +
3212 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003213
3214 this.zoomWarningNode_.addEventListener('click', function(e) {
3215 this.parentNode.removeChild(this);
3216 });
rgindaf522ce02012-04-17 17:49:17 -07003217 }
3218
Mike Frysingerb7289952019-03-23 16:05:38 -07003219 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003220 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003221 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003222
rgindaf522ce02012-04-17 17:49:17 -07003223 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3224
3225 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003226 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003227 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003228 }
rgindaf522ce02012-04-17 17:49:17 -07003229 } else if (this.zoomWarningNode_.parentNode) {
3230 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3231 }
3232};
3233
3234/**
rgindacc2996c2012-02-24 14:59:31 -08003235 * Show the terminal overlay for a given amount of time.
3236 *
3237 * The terminal overlay appears in inverse video in a large font, centered
3238 * over the terminal. You should probably keep the overlay message brief,
3239 * since it's in a large font and you probably aren't going to check the size
3240 * of the terminal first.
3241 *
3242 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003243 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003244 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3245 * stay up forever (or until the next overlay).
3246 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003247hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
rgindaf0090c92012-02-10 14:58:52 -08003248 if (!this.overlayNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003249 if (!this.div_) {
rgindaf0090c92012-02-10 14:58:52 -08003250 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003251 }
rgindaf0090c92012-02-10 14:58:52 -08003252
3253 this.overlayNode_ = this.document_.createElement('div');
3254 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003255 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003256 'font-size: xx-large;' +
3257 'opacity: 0.75;' +
3258 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3259 'position: absolute;' +
3260 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003261 '-webkit-transition: opacity 180ms ease-in;' +
3262 '-moz-user-select: none;' +
3263 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003264
3265 this.overlayNode_.addEventListener('mousedown', function(e) {
3266 e.preventDefault();
3267 e.stopPropagation();
3268 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003269 }
3270
rginda9f5222b2012-03-05 11:53:28 -08003271 this.overlayNode_.style.color = this.prefs_.get('background-color');
3272 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3273 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3274
rgindaf0090c92012-02-10 14:58:52 -08003275 this.overlayNode_.textContent = msg;
3276 this.overlayNode_.style.opacity = '0.75';
3277
Mike Frysingerbdb34802020-04-07 03:47:32 -04003278 if (!this.overlayNode_.parentNode) {
rgindaf0090c92012-02-10 14:58:52 -08003279 this.div_.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003280 }
rgindaf0090c92012-02-10 14:58:52 -08003281
Mike Frysingerdc727792020-04-10 01:41:13 -04003282 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3283 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003284
Robert Ginda8a59f762014-07-23 11:29:55 -07003285 this.overlayNode_.style.top =
3286 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003287 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003288 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003289
Mike Frysingerbdb34802020-04-07 03:47:32 -04003290 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003291 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003292 }
rgindaf0090c92012-02-10 14:58:52 -08003293
Raymes Khouryc7a06382018-07-04 10:25:45 +10003294 this.accessibilityReader_.assertiveAnnounce(msg);
3295
Mike Frysingerec4225d2020-04-07 05:00:01 -04003296 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003297 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003298 }
rgindacc2996c2012-02-24 14:59:31 -08003299
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003300 this.overlayTimeout_ = setTimeout(() => {
3301 this.overlayNode_.style.opacity = '0';
3302 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003303 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003304};
3305
3306/**
3307 * Hide the terminal overlay immediately.
3308 *
3309 * Useful when we show an overlay for an event with an unknown end time.
3310 */
3311hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003312 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003313 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003314 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003315 this.overlayTimeout_ = null;
3316
Mike Frysingerbdb34802020-04-07 03:47:32 -04003317 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003318 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003319 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003320 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003321};
3322
rginda4bba5e12012-06-20 16:15:30 -07003323/**
3324 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003325 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003326 * Note: In Chrome, this should work unless the user has rejected the permission
3327 * request. In Firefox extension environment, you'll need the "clipboardRead"
3328 * permission. In other environments, this might always fail as the browser
3329 * frequently blocks access for security reasons.
3330 *
3331 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3332 * value is always null. Otherwise, this function uses legacy pasting and
3333 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003334 */
3335hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003336 if (!this.alwaysUseLegacyPasting &&
3337 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003338 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3339 return null;
3340 } else {
3341 // Legacy pasting.
3342 try {
3343 return this.document_.execCommand('paste');
3344 } catch (firefoxException) {
3345 // Ignore this. FF 40 and older would incorrectly throw an exception if
3346 // there was an error instead of returning false.
3347 return false;
3348 }
3349 }
rginda4bba5e12012-06-20 16:15:30 -07003350};
3351
3352/**
3353 * Copy a string to the system clipboard.
3354 *
3355 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003356 *
3357 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003358 */
3359hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003360 if (this.prefs_.get('enable-clipboard-notice')) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003361 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003362 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003363
Mike Frysinger96eacae2019-01-02 18:13:56 -05003364 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003365};
3366
Evan Jones2600d4f2016-12-06 09:29:36 -05003367/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003368 * Display an image.
3369 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003370 * Either URI or buffer or blob fields must be specified.
3371 *
Joel Hockey0f933582019-08-27 18:01:51 -07003372 * @param {{
3373 * name: (string|undefined),
3374 * size: (string|number|undefined),
3375 * preserveAspectRation: (boolean|undefined),
3376 * inline: (boolean|undefined),
3377 * width: (string|number|undefined),
3378 * height: (string|number|undefined),
3379 * align: (string|undefined),
3380 * url: (string|undefined),
3381 * buffer: (!ArrayBuffer|undefined),
3382 * blob: (!Blob|undefined),
3383 * type: (string|undefined),
3384 * }} options The image to display.
3385 * name A human readable string for the image
3386 * size The size (in bytes).
3387 * preserveAspectRatio Whether to preserve aspect.
3388 * inline Whether to display the image inline.
3389 * width The width of the image.
3390 * height The height of the image.
3391 * align Direction to align the image.
3392 * uri The source URI for the image.
3393 * buffer The ArrayBuffer image data.
3394 * blob The Blob image data.
3395 * type The MIME type of the image data.
3396 * @param {function()=} onLoad Callback when loading finishes.
3397 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003398 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003399hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003400 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003401 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003402 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003403 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003404 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003405
3406 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003407 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003408 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003409 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003410
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003411 // See if the mime type is available. If not, guess from the filename.
3412 // We don't list all possible mime types because the browser can usually
3413 // guess it correctly. So list the ones that need a bit more help.
3414 if (!options.type) {
3415 const ary = options.name.split('.');
3416 const ext = ary[ary.length - 1].trim();
3417 switch (ext) {
3418 case 'svg':
3419 case 'svgz':
3420 options.type = 'image/svg+xml';
3421 break;
3422 }
3423 }
3424
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003425 // Has the user approved image display yet?
3426 if (this.allowImagesInline !== true) {
3427 this.newLine();
3428 const row = this.getRowNode(this.scrollbackRows_.length +
3429 this.getCursorRow() - 1);
3430
3431 if (this.allowImagesInline === false) {
3432 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3433 'Inline Images Disabled');
3434 return;
3435 }
3436
3437 // Show a prompt.
3438 let button;
3439 const span = this.document_.createElement('span');
3440 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3441 span.style.fontWeight = 'bold';
3442 span.style.borderWidth = '1px';
3443 span.style.borderStyle = 'dashed';
3444 button = this.document_.createElement('span');
3445 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3446 button.style.marginLeft = '1em';
3447 button.style.borderWidth = '1px';
3448 button.style.borderStyle = 'solid';
3449 button.addEventListener('click', () => {
3450 this.prefs_.set('allow-images-inline', false);
3451 });
3452 span.appendChild(button);
3453 button = this.document_.createElement('span');
3454 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3455 'allow this session');
3456 button.style.marginLeft = '1em';
3457 button.style.borderWidth = '1px';
3458 button.style.borderStyle = 'solid';
3459 button.addEventListener('click', () => {
3460 this.allowImagesInline = true;
3461 });
3462 span.appendChild(button);
3463 button = this.document_.createElement('span');
3464 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3465 button.style.marginLeft = '1em';
3466 button.style.borderWidth = '1px';
3467 button.style.borderStyle = 'solid';
3468 button.addEventListener('click', () => {
3469 this.prefs_.set('allow-images-inline', true);
3470 });
3471 span.appendChild(button);
3472
3473 row.appendChild(span);
3474 return;
3475 }
3476
3477 // See if we should show this object directly, or download it.
3478 if (options.inline) {
3479 const io = this.io.push();
3480 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003481 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003482
3483 // While we're loading the image, eat all the user's input.
3484 io.onVTKeystroke = io.sendString = () => {};
3485
3486 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003487 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003488 if (options.uri !== undefined) {
3489 img.src = options.uri;
3490 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003491 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003492 img.src = URL.createObjectURL(blob);
3493 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003494 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003495 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003496 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003497 img.title = img.alt = options.name;
3498
3499 // Attach the image to the page to let it load/render. It won't stay here.
3500 // This is needed so it's visible and the DOM can calculate the height. If
3501 // the image is hidden or not in the DOM, the height is always 0.
3502 this.document_.body.appendChild(img);
3503
3504 // Wait for the image to finish loading before we try moving it to the
3505 // right place in the terminal.
3506 img.onload = () => {
3507 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003508 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003509 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003510 img.style.maxWidth = `${screenSize.width}px`;
3511 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003512
3513 // Parse a width/height specification.
3514 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003515 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003516 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003517 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003518
3519 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3520 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003521 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003522 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003523 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003524 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003525 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003526 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003527 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003528 }
3529
3530 return '';
3531 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003532 img.style.width = parseDim(
3533 options.width, screenSize.width, '--hterm-charsize-width');
3534 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003535 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003536
3537 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003538 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003539 const padRows = Math.ceil(img.clientHeight /
3540 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003541 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003542 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003543 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003544
3545 // Update the max height in case the user shrinks the character size.
3546 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3547
3548 // Move the image to the last row. This way when we scroll up, it doesn't
3549 // disappear when the first row gets clipped. It will disappear when we
3550 // scroll down and the last row is clipped ...
3551 this.document_.body.removeChild(img);
3552 // Create a wrapper node so we can do an absolute in a relative position.
3553 // This helps with rounding errors between JS & CSS counts.
3554 const div = this.document_.createElement('div');
3555 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003556 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003557 img.style.position = 'absolute';
3558 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3559 div.appendChild(img);
3560 const row = this.getRowNode(this.scrollbackRows_.length +
3561 this.getCursorRow() - 1);
3562 row.appendChild(div);
3563
Mike Frysinger2558ed52019-01-14 01:03:41 -05003564 // Now that the image has been read, we can revoke the source.
3565 if (options.uri === undefined) {
3566 URL.revokeObjectURL(img.src);
3567 }
3568
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003569 io.hideOverlay();
3570 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003571
Mike Frysingerbdb34802020-04-07 03:47:32 -04003572 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003573 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003574 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003575 };
3576
3577 // If we got a malformed image, give up.
3578 img.onerror = (e) => {
3579 this.document_.body.removeChild(img);
3580 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003581 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003582 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003583
Mike Frysingerbdb34802020-04-07 03:47:32 -04003584 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003585 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003586 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 };
3588 } else {
3589 // We can't use chrome.downloads.download as that requires "downloads"
3590 // permissions, and that works only in extensions, not apps.
3591 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003592 if (options.uri !== undefined) {
3593 a.href = options.uri;
3594 } else if (options.buffer !== undefined) {
3595 const blob = new Blob([options.buffer]);
3596 a.href = URL.createObjectURL(blob);
3597 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003598 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003599 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003600 a.download = options.name;
3601 this.document_.body.appendChild(a);
3602 a.click();
3603 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003604 if (options.uri === undefined) {
3605 URL.revokeObjectURL(a.href);
3606 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003607 }
3608};
3609
3610/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003611 * Returns the selected text, or null if no text is selected.
3612 *
3613 * @return {string|null}
3614 */
rgindaa09e7332012-08-17 12:49:51 -07003615hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003616 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003617 selection.sync();
3618
Mike Frysingerbdb34802020-04-07 03:47:32 -04003619 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003620 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003621 }
rgindaa09e7332012-08-17 12:49:51 -07003622
rgindaa09e7332012-08-17 12:49:51 -07003623 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003624 let startOffset = selection.startOffset;
3625 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003626
Raymes Khoury334625a2018-06-25 10:29:40 +10003627 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003628 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003629 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003630 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003631
Robert Gindafdbb3f22012-09-06 20:23:06 -07003632 if (node.nodeName != 'X-ROW') {
3633 // If the selection doesn't start on an x-row node, then it must be
3634 // somewhere inside the x-row. Add any characters from previous siblings
3635 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003636
3637 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3638 // If node is the text node in a styled span, move up to the span node.
3639 node = node.parentNode;
3640 }
3641
Robert Gindafdbb3f22012-09-06 20:23:06 -07003642 while (node.previousSibling) {
3643 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003644 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003645 }
rgindaa09e7332012-08-17 12:49:51 -07003646 }
3647
3648 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003649 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003650 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003651 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003652
Robert Gindafdbb3f22012-09-06 20:23:06 -07003653 if (node.nodeName != 'X-ROW') {
3654 // If the selection doesn't end on an x-row node, then it must be
3655 // somewhere inside the x-row. Add any characters from following siblings
3656 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003657
3658 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3659 // If node is the text node in a styled span, move up to the span node.
3660 node = node.parentNode;
3661 }
3662
Robert Gindafdbb3f22012-09-06 20:23:06 -07003663 while (node.nextSibling) {
3664 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003665 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003666 }
rgindaa09e7332012-08-17 12:49:51 -07003667 }
3668
Mike Frysingerdc727792020-04-10 01:41:13 -04003669 const rv = this.getRowsText(selection.startRow.rowIndex,
3670 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003671 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003672};
3673
rginda4bba5e12012-06-20 16:15:30 -07003674/**
3675 * Copy the current selection to the system clipboard, then clear it after a
3676 * short delay.
3677 */
3678hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003679 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003681 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003682 }
rginda4bba5e12012-06-20 16:15:30 -07003683};
3684
Joel Hockey0f933582019-08-27 18:01:51 -07003685/**
3686 * Show overlay with current terminal size.
3687 */
rgindaf0090c92012-02-10 14:58:52 -08003688hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003689 if (this.prefs_.get('enable-resize-status')) {
3690 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3691 }
rgindaf0090c92012-02-10 14:58:52 -08003692};
3693
rginda87b86462011-12-14 13:48:03 -08003694/**
3695 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3696 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003697 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003698 */
3699hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003700 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003701 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003702 }
rginda87b86462011-12-14 13:48:03 -08003703
Mike Frysinger225c99d2019-10-20 14:02:37 -06003704 this.pauseCursorBlink_();
3705
Mike Frysinger79669762018-12-30 20:51:10 -05003706 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003707};
3708
3709/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003710 * Open the selected url.
3711 */
3712hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003713 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003714
3715 // If there is no selection, try and expand wherever they clicked.
3716 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003717 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003718 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003719
3720 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003721 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003722 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003723 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003724 }
3725
3726 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003727 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003728 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003729 }
Mike Frysinger43472622017-06-26 18:11:07 -04003730
3731 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003732 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003733 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3734 // We have to whitelist a few protocols that lack authorities and thus
3735 // never use the //. Like mailto.
3736 switch (str.split(':', 1)[0]) {
3737 case 'mailto':
3738 break;
3739 default:
3740 str = 'http://' + str;
3741 break;
3742 }
3743 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003744
Mike Frysinger720fa832017-10-23 01:15:52 -04003745 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003746};
Mike Frysinger70b94692017-01-26 18:57:50 -10003747
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003748/**
3749 * Manage the automatic mouse hiding behavior while typing.
3750 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003751 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003752 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003753hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003754 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3755 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003756 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003757 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003758 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003759
3760 this.mouseHideWhileTyping_ = !!v;
3761};
3762
3763/**
3764 * Handler for monitoring user keyboard activity.
3765 *
3766 * This isn't for processing the keystrokes directly, but for updating any
3767 * state that might toggle based on the user using the keyboard at all.
3768 *
Joel Hockey0f933582019-08-27 18:01:51 -07003769 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003770 */
3771hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3772 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003773 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003774 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003775 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003776};
Mike Frysinger70b94692017-01-26 18:57:50 -10003777
3778/**
rgindad5613292012-06-19 15:40:37 -07003779 * Add the terminalRow and terminalColumn properties to mouse events and
3780 * then forward on to onMouse().
3781 *
3782 * The terminalRow and terminalColumn properties contain the (row, column)
3783 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003784 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003785 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003786 */
3787hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003788 if (e.processedByTerminalHandler_) {
3789 // We register our event handlers on the document, as well as the cursor
3790 // and the scroll blocker. Mouse events that occur on the cursor or
3791 // scroll blocker will also appear on the document, but we don't want to
3792 // process them twice.
3793 //
3794 // We can't just prevent bubbling because that has other side effects, so
3795 // we decorate the event object with this property instead.
3796 return;
3797 }
3798
Mike Frysinger468966c2018-08-28 13:48:51 -04003799 // Consume navigation events. Button 3 is usually "browser back" and
3800 // button 4 is "browser forward" which we don't want to happen.
3801 if (e.button > 2) {
3802 e.preventDefault();
3803 // We don't return so click events can be passed to the remote below.
3804 }
3805
Mike Frysingerdc727792020-04-10 01:41:13 -04003806 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003807 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3808
rgindafaa74742012-08-21 13:34:03 -07003809 e.processedByTerminalHandler_ = true;
3810
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003811 // Handle auto hiding of mouse cursor while typing.
3812 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3813 // Make sure the mouse cursor is visible.
3814 this.syncMouseStyle();
3815 // This debounce isn't perfect, but should work well enough for such a
3816 // simple implementation. If the user moved the mouse, we enabled this
3817 // debounce, and then moved the mouse just before the timeout, we wouldn't
3818 // debounce that later movement.
3819 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3820 }
3821
Robert Gindaeda48db2014-07-17 09:25:30 -07003822 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003823 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003824 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003825 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003826 this.scrollPort_.characterSize.height) + 1;
3827 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003828 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003829
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003830 // Clamp row and column.
3831 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3832 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3833
3834 // Ignore mousedown in the scrollbar area.
3835 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003836 return;
3837 }
3838
Joel Hockey3babf302020-04-22 15:00:06 -07003839 if (this.options_.cursorVisible && !reportMouseEvents &&
3840 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003841 // If the cursor is visible and we're not sending mouse events to the
3842 // host app, then we want to hide the terminal cursor when the mouse
3843 // cursor is over top. This keeps the terminal cursor from interfering
3844 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003845 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3846 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3847 this.cursorNode_.style.display = 'none';
3848 } else if (this.cursorNode_.style.display == 'none') {
3849 this.cursorNode_.style.display = '';
3850 }
3851 }
rgindad5613292012-06-19 15:40:37 -07003852
Robert Ginda928cf632014-03-05 15:07:41 -08003853 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003854 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003855
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003856 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003857 // If VT mouse reporting is disabled, or has been defeated with
3858 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003859 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003860 this.setSelectionEnabled(true);
3861 } else {
3862 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003863 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003864 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003865 this.setSelectionEnabled(false);
3866 e.preventDefault();
3867 }
3868 }
3869
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003870 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003871 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003872 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003873 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003874 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003875 }
rgindad5613292012-06-19 15:40:37 -07003876 }
3877
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003878 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003879 // Debounce this event with the dblclick event. If you try to doubleclick
3880 // a URL to open it, Chrome will fire click then dblclick, but we won't
3881 // have expanded the selection text at the first click event.
3882 clearTimeout(this.timeouts_.openUrl);
3883 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3884 500);
3885 return;
3886 }
3887
Mike Frysinger847577f2017-05-23 23:25:57 -04003888 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003889 if (e.ctrlKey && e.button == 2 /* right button */) {
3890 e.preventDefault();
3891 this.contextMenu.show(e, this);
3892 } else if (e.button == this.mousePasteButton ||
3893 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003894 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003895 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003896 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003897 }
3898 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003899
Mike Frysinger2edd3612017-05-24 00:54:39 -04003900 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003901 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003902 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003903 }
3904
3905 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3906 this.scrollBlockerNode_.engaged) {
3907 // Disengage the scroll-blocker after one of these events.
3908 this.scrollBlockerNode_.engaged = false;
3909 this.scrollBlockerNode_.style.top = '-99px';
3910 }
3911
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003912 // Emulate arrow key presses via scroll wheel events.
3913 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3914 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003915 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003916 const delta =
3917 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003918
Mike Frysinger321063c2018-08-29 15:33:14 -04003919 // Helper to turn a wheel event delta into a series of key presses.
3920 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3921 if (distance == 0) {
3922 return '';
3923 }
3924
3925 // Convert the scroll distance into a number of rows/cols.
3926 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3927 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3928 return data.repeat(cells);
3929 };
3930
3931 // The order between up/down and left/right doesn't really matter.
3932 this.io.sendString(
3933 // Up/down arrow keys.
3934 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3935 'A', 'B') +
3936 // Left/right arrow keys.
3937 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10003938 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04003939 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003940
3941 e.preventDefault();
3942 }
3943 }
Robert Ginda928cf632014-03-05 15:07:41 -08003944 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003945 if (!this.scrollBlockerNode_.engaged) {
3946 if (e.type == 'mousedown') {
3947 // Move the scroll-blocker into place if we want to keep the scrollport
3948 // from scrolling.
3949 this.scrollBlockerNode_.engaged = true;
3950 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3951 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3952 } else if (e.type == 'mousemove') {
3953 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3954 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003955 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003956 e.preventDefault();
3957 }
3958 }
Robert Ginda928cf632014-03-05 15:07:41 -08003959
3960 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003961 }
3962
Robert Ginda928cf632014-03-05 15:07:41 -08003963 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3964 // Restore this on mouseup in case it was temporarily defeated with a
3965 // alt-mousedown. Only do this when the selection is empty so that
3966 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003967 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003968 }
rgindad5613292012-06-19 15:40:37 -07003969};
3970
3971/**
3972 * Clients should override this if they care to know about mouse events.
3973 *
3974 * The event parameter will be a normal DOM mouse click event with additional
3975 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003976 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003977 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003978 */
3979hterm.Terminal.prototype.onMouse = function(e) { };
3980
3981/**
rginda8e92a692012-05-20 19:37:20 -07003982 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003983 *
3984 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003985 */
Rob Spies06533ba2014-04-24 11:20:37 -07003986hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3987 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003988 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003989
Mike Frysingerbdb34802020-04-07 03:47:32 -04003990 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003991 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003992 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003993
Mike Frysingerbdb34802020-04-07 03:47:32 -04003994 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04003995 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003996 }
rginda8e92a692012-05-20 19:37:20 -07003997};
3998
3999/**
rginda8ba33642011-12-14 12:31:31 -08004000 * React when the ScrollPort is scrolled.
4001 */
4002hterm.Terminal.prototype.onScroll_ = function() {
4003 this.scheduleSyncCursorPosition_();
4004};
4005
4006/**
rginda9846e2f2012-01-27 13:53:33 -08004007 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004008 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004009 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004010 */
4011hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004012 this.onPasteData_(e.text);
4013};
4014
4015/**
4016 * Handle pasted data.
4017 *
4018 * @param {string} data The pasted data.
4019 */
4020hterm.Terminal.prototype.onPasteData_ = function(data) {
4021 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004022 if (this.options_.bracketedPaste) {
4023 // We strip out most escape sequences as they can cause issues (like
4024 // inserting an \x1b[201~ midstream). We pass through whitespace
4025 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4026 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004027 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004028 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4029 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4030 }
Robert Gindaa063b202014-07-21 11:08:25 -07004031
4032 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004033};
4034
4035/**
rgindaa09e7332012-08-17 12:49:51 -07004036 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004037 *
Joel Hockey0f933582019-08-27 18:01:51 -07004038 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004039 */
4040hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004041 if (!this.useDefaultWindowCopy) {
4042 e.preventDefault();
4043 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4044 }
rgindaa09e7332012-08-17 12:49:51 -07004045};
4046
4047/**
rginda8ba33642011-12-14 12:31:31 -08004048 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004049 *
4050 * Note: This function should not directly contain code that alters the internal
4051 * state of the terminal. That kind of code belongs in realizeWidth or
4052 * realizeHeight, so that it can be executed synchronously in the case of a
4053 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004054 */
4055hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004056 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4057 this.scrollPort_.characterSize.width) || 0;
4058 const rowCount = lib.f.smartFloorDivide(
4059 this.scrollPort_.getScreenHeight(),
4060 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004061
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004062 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004063 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004064 // gets removed from the document or during the initial load, and we can't
4065 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004066 // This can also happen if called before the scrollPort calculates the
4067 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004068 return;
4069 }
4070
Mike Frysingerdc727792020-04-10 01:41:13 -04004071 const isNewSize = (columnCount != this.screenSize.width ||
4072 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004073 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004074
4075 // We do this even if the size didn't change, just to be sure everything is
4076 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004077 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004078 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004079
Mike Frysingerbdb34802020-04-07 03:47:32 -04004080 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004081 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004082 }
rgindaa8ba17d2012-08-15 14:41:10 -07004083
Robert Gindafb1be6a2013-12-11 11:56:22 -08004084 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004085 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004086
4087 if (wasScrolledEnd) {
4088 this.scrollEnd();
4089 }
rginda8ba33642011-12-14 12:31:31 -08004090};
4091
4092/**
4093 * Service the cursor blink timeout.
4094 */
4095hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004096 if (!this.options_.cursorBlink) {
4097 delete this.timeouts_.cursorBlink;
4098 return;
4099 }
4100
Robert Ginda830583c2013-08-07 13:20:46 -07004101 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004102 this.cursorNode_.style.opacity == '0' ||
4103 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004104 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004105 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4106 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004107 } else {
rginda87b86462011-12-14 13:48:03 -08004108 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004109 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4110 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004111 }
4112};
David Reveman8f552492012-03-28 12:18:41 -04004113
4114/**
4115 * Set the scrollbar-visible mode bit.
4116 *
4117 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4118 * Otherwise it will not.
4119 *
4120 * Defaults to on.
4121 *
4122 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4123 */
4124hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4125 this.scrollPort_.setScrollbarVisible(state);
4126};
Michael Kelly485ecd12014-06-09 11:41:56 -04004127
4128/**
Rob Spies49039e52014-12-17 13:40:04 -08004129 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004130 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004131 *
4132 * Defaults to 1.
4133 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004134 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004135 */
4136hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4137 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4138};
4139
4140/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004141 * Close all web notifications created by terminal bells.
4142 */
4143hterm.Terminal.prototype.closeBellNotifications_ = function() {
4144 this.bellNotificationList_.forEach(function(n) {
4145 n.close();
4146 });
4147 this.bellNotificationList_.length = 0;
4148};
Raymes Khourye5d48982018-08-02 09:08:32 +10004149
4150/**
4151 * Syncs the cursor position when the scrollport gains focus.
4152 */
4153hterm.Terminal.prototype.onScrollportFocus_ = function() {
4154 // If the cursor is offscreen we set selection to the last row on the screen.
4155 const topRowIndex = this.scrollPort_.getTopRowIndex();
4156 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4157 const selection = this.document_.getSelection();
4158 if (!this.syncCursorPosition_() && selection) {
4159 selection.collapse(this.getRowNode(bottomRowIndex));
4160 }
4161};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004162
4163/**
4164 * Clients can override this if they want to provide an options page.
4165 */
4166hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4167
4168
4169/**
4170 * Called when user selects to open the options page.
4171 */
4172hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4173 this.onOpenOptionsPage();
4174};