blob: 8236a77ede31141f229853d353cd2c446ea6d3fe [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
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700106 this.screenBorderSize_ = 0;
107
Robert Ginda57f03b42012-09-13 11:02:48 -0700108 this.scrollOnOutput_ = null;
109 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400110 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800111
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700112 // True if we should override mouse event reporting to allow local selection.
113 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800114
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400115 // Whether to auto hide the mouse cursor when typing.
116 this.setAutomaticMouseHiding();
117 // Timer to keep mouse visible while it's being used.
118 this.mouseHideDelay_ = null;
119
rgindaf0090c92012-02-10 14:58:52 -0800120 // Terminal bell sound.
121 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400122 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800123 this.bellAudio_.setAttribute('preload', 'auto');
124
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000125 // The AccessibilityReader object for announcing command output.
126 this.accessibilityReader_ = null;
127
Mike Frysingercc114512017-09-11 21:39:17 -0400128 // The context menu object.
129 this.contextMenu = new hterm.ContextMenu();
130
Michael Kelly485ecd12014-06-09 11:41:56 -0400131 // All terminal bell notifications that have been generated (not necessarily
132 // shown).
133 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700134 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400135
136 // Whether we have permission to display notifications.
137 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
rginda6d397402012-01-17 10:58:29 -0800139 // Cursor position and attributes saved with DECSC.
140 this.savedOptions_ = {};
141
rginda8ba33642011-12-14 12:31:31 -0800142 // The current mode bits for the terminal.
143 this.options_ = new hterm.Options();
144
145 // Timeouts we might need to clear.
146 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800147
148 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800149 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800150
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800151 this.saveCursorAndState(true);
152
Zhu Qunying30d40712017-03-14 16:27:00 -0700153 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800154 this.keyboard = new hterm.Keyboard(this);
155
rginda87b86462011-12-14 13:48:03 -0800156 // General IO interface that can be given to third parties without exposing
157 // the entire terminal object.
158 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800159
rgindad5613292012-06-19 15:40:37 -0700160 // True if mouse-click-drag should scroll the terminal.
161 this.enableMouseDragScroll = true;
162
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400164 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700165 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700166
Zhu Qunying30d40712017-03-14 16:27:00 -0700167 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700168 this.useDefaultWindowCopy = false;
169
170 this.clearSelectionAfterCopy = true;
171
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400172 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800173 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700174
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400175 // Whether we allow images to be shown.
176 this.allowImagesInline = null;
177
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400178 this.reportFocus = false;
179
Jason Linf129f3c2020-03-23 11:52:08 +1100180 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
181 this.alwaysUseLegacyPasting = false;
182
Joel Hockey3a44a442019-10-14 16:22:56 -0700183 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500184 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800185};
186
187/**
Robert Ginda830583c2013-08-07 13:20:46 -0700188 * Possible cursor shapes.
189 */
190hterm.Terminal.cursorShape = {
191 BLOCK: 'BLOCK',
192 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400193 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700194};
195
196/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700197 * Clients should override this to be notified when the terminal is ready
198 * for use.
199 *
200 * The terminal initialization is asynchronous, and shouldn't be used before
201 * this method is called.
202 */
203hterm.Terminal.prototype.onTerminalReady = function() { };
204
205/**
rginda35c456b2012-02-09 17:29:05 -0800206 * Default tab with of 8 to match xterm.
207 */
208hterm.Terminal.prototype.tabWidth = 8;
209
210/**
rginda9f5222b2012-03-05 11:53:28 -0800211 * Select a preference profile.
212 *
213 * This will load the terminal preferences for the given profile name and
214 * associate subsequent preference changes with the new preference profile.
215 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500216 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800217 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400218 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700219 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800220 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400221hterm.Terminal.prototype.setProfile = function(
222 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700223 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800224
Mike Frysingerdc727792020-04-10 01:41:13 -0400225 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800226
Mike Frysingerbdb34802020-04-07 03:47:32 -0400227 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700228 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400229 }
rginda9f5222b2012-03-05 11:53:28 -0800230
Robert Ginda57f03b42012-09-13 11:02:48 -0700231 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700232
233 /**
234 * Clears and reloads key bindings. Used by preferences
235 * 'keybindings' and 'keybindings-os-defaults'.
236 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400237 * @param {*?=} bindings
238 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700239 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400240 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700241 terminal.keyboard.bindings.clear();
242
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 // Default to an empty object so we still handle OS defaults.
244 if (bindings === null) {
245 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700246 }
247
248 if (!(bindings instanceof Object)) {
249 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400250 bindings = {};
251 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700252 }
253
254 try {
255 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
256 } catch (ex) {
257 console.error('Error in keybindings preference: ' + ex);
258 }
259 }
260
Robert Ginda57f03b42012-09-13 11:02:48 -0700261 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800262 'alt-gr-mode': function(v) {
263 if (v == null) {
264 if (navigator.language.toLowerCase() == 'en-us') {
265 v = 'none';
266 } else {
267 v = 'right-alt';
268 }
269 } else if (typeof v == 'string') {
270 v = v.toLowerCase();
271 } else {
272 v = 'none';
273 }
274
Mike Frysingerbdb34802020-04-07 03:47:32 -0400275 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800276 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400277 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800278
279 terminal.keyboard.altGrMode = v;
280 },
281
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700282 'alt-backspace-is-meta-backspace': function(v) {
283 terminal.keyboard.altBackspaceIsMetaBackspace = v;
284 },
285
Robert Ginda57f03b42012-09-13 11:02:48 -0700286 'alt-is-meta': function(v) {
287 terminal.keyboard.altIsMeta = v;
288 },
289
290 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400291 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400293 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700294
295 terminal.keyboard.altSendsWhat = v;
296 },
297
298 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400299 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800300 if (ary) {
301 terminal.bellAudio_.setAttribute('src',
302 lib.resource.getDataUrl(ary[1]));
303 } else {
304 terminal.bellAudio_.setAttribute('src', v);
305 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700306 },
307
Michael Kelly485ecd12014-06-09 11:41:56 -0400308 'desktop-notification-bell': function(v) {
309 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700310 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400311 Notification.permission === 'granted';
312 if (!terminal.desktopNotificationBell_) {
313 // Note: We don't call Notification.requestPermission here because
314 // Chrome requires the call be the result of a user action (such as an
315 // onclick handler), and pref listeners are run asynchronously.
316 //
317 // A way of working around this would be to display a dialog in the
318 // terminal with a "click-to-request-permission" button.
319 console.warn('desktop-notification-bell is true but we do not have ' +
320 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400321 }
322 } else {
323 terminal.desktopNotificationBell_ = false;
324 }
325 },
326
Robert Ginda57f03b42012-09-13 11:02:48 -0700327 'background-color': function(v) {
328 terminal.setBackgroundColor(v);
329 },
330
331 'background-image': function(v) {
332 terminal.scrollPort_.setBackgroundImage(v);
333 },
334
335 'background-size': function(v) {
336 terminal.scrollPort_.setBackgroundSize(v);
337 },
338
339 'background-position': function(v) {
340 terminal.scrollPort_.setBackgroundPosition(v);
341 },
342
343 'backspace-sends-backspace': function(v) {
344 terminal.keyboard.backspaceSendsBackspace = v;
345 },
346
Brad Town18654b62015-03-12 00:27:45 -0700347 'character-map-overrides': function(v) {
348 if (!(v == null || v instanceof Object)) {
349 console.warn('Preference character-map-modifications is not an ' +
350 'object: ' + v);
351 return;
352 }
353
Mike Frysinger095d4062017-06-14 00:29:48 -0700354 terminal.vt.characterMaps.reset();
355 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700356 },
357
Robert Ginda57f03b42012-09-13 11:02:48 -0700358 'cursor-blink': function(v) {
359 terminal.setCursorBlink(!!v);
360 },
361
Joel Hockey9d10ba12019-05-28 01:25:02 -0700362 'cursor-shape': function(v) {
363 terminal.setCursorShape(v);
364 },
365
Robert Gindaea2183e2014-07-17 09:51:51 -0700366 'cursor-blink-cycle': function(v) {
367 if (v instanceof Array &&
368 typeof v[0] == 'number' &&
369 typeof v[1] == 'number') {
370 terminal.cursorBlinkCycle_ = v;
371 } else if (typeof v == 'number') {
372 terminal.cursorBlinkCycle_ = [v, v];
373 } else {
374 // Fast blink indicates an error.
375 terminal.cursorBlinkCycle_ = [100, 100];
376 }
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'cursor-color': function(v) {
380 terminal.setCursorColor(v);
381 },
382
383 'color-palette-overrides': function(v) {
384 if (!(v == null || v instanceof Object || v instanceof Array)) {
385 console.warn('Preference color-palette-overrides is not an array or ' +
386 'object: ' + v);
387 return;
rginda9f5222b2012-03-05 11:53:28 -0800388 }
rginda9f5222b2012-03-05 11:53:28 -0800389
Joel Hockey42dba8f2020-03-26 16:21:11 -0700390 // Call terminal.setColorPalette here and below with the new default
391 // value before changing it in lib.colors.colorPalette to ensure that
392 // CSS vars are updated.
393 lib.colors.stockColorPalette.forEach(
394 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700396
Robert Ginda57f03b42012-09-13 11:02:48 -0700397 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400398 for (const key in v) {
399 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 if (isNaN(i) || i < 0 || i > 255) {
401 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
402 continue;
403 }
404
405 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400406 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700407 if (rgb) {
408 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700410 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 }
412 }
rginda30f20f62012-04-05 16:36:19 -0700413 }
rginda30f20f62012-04-05 16:36:19 -0700414
Joel Hockey42dba8f2020-03-26 16:21:11 -0700415 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
416 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 },
rginda30f20f62012-04-05 16:36:19 -0700418
Robert Ginda57f03b42012-09-13 11:02:48 -0700419 'copy-on-select': function(v) {
420 terminal.copyOnSelect = !!v;
421 },
rginda9f5222b2012-03-05 11:53:28 -0800422
Rob Spies0bec09b2014-06-06 15:58:09 -0700423 'use-default-window-copy': function(v) {
424 terminal.useDefaultWindowCopy = !!v;
425 },
426
427 'clear-selection-after-copy': function(v) {
428 terminal.clearSelectionAfterCopy = !!v;
429 },
430
Robert Ginda7e5e9522014-03-14 12:23:58 -0700431 'ctrl-plus-minus-zero-zoom': function(v) {
432 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
433 },
434
Robert Gindafb5a3f92014-05-13 14:12:00 -0700435 'ctrl-c-copy': function(v) {
436 terminal.keyboard.ctrlCCopy = v;
437 },
438
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100439 'ctrl-v-paste': function(v) {
440 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700441 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100442 },
443
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700444 'paste-on-drop': function(v) {
445 terminal.scrollPort_.setPasteOnDrop(v);
446 },
447
Masaya Suzuki273aa982014-05-31 07:25:55 +0900448 'east-asian-ambiguous-as-two-column': function(v) {
449 lib.wc.regardCjkAmbiguous = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'enable-8-bit-control': function(v) {
453 terminal.vt.enable8BitControl = !!v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Robert Ginda57f03b42012-09-13 11:02:48 -0700456 'enable-bold': function(v) {
457 terminal.syncBoldSafeState();
458 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400459
Robert Ginda3e278d72014-03-25 13:18:51 -0700460 'enable-bold-as-bright': function(v) {
461 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
462 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
463 },
464
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400465 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500466 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400467 },
468
Robert Ginda57f03b42012-09-13 11:02:48 -0700469 'enable-clipboard-write': function(v) {
470 terminal.vt.enableClipboardWrite = !!v;
471 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400472
Robert Ginda3755e752013-05-31 13:34:09 -0700473 'enable-dec12': function(v) {
474 terminal.vt.enableDec12 = !!v;
475 },
476
Mike Frysinger38f267d2018-09-07 02:50:59 -0400477 'enable-csi-j-3': function(v) {
478 terminal.vt.enableCsiJ3 = !!v;
479 },
480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'font-family': function(v) {
482 terminal.syncFontFamily();
483 },
rginda30f20f62012-04-05 16:36:19 -0700484
Robert Ginda57f03b42012-09-13 11:02:48 -0700485 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700486 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700487 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500488 console.error(`Invalid font size: ${v}`);
489 return;
490 }
491
Robert Ginda57f03b42012-09-13 11:02:48 -0700492 terminal.setFontSize(v);
493 },
rginda9875d902012-08-20 16:21:57 -0700494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 'font-smoothing': function(v) {
496 terminal.syncFontFamily();
497 },
rgindade84e382012-04-20 15:39:31 -0700498
Robert Ginda57f03b42012-09-13 11:02:48 -0700499 'foreground-color': function(v) {
500 terminal.setForegroundColor(v);
501 },
rginda30f20f62012-04-05 16:36:19 -0700502
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400503 'hide-mouse-while-typing': function(v) {
504 terminal.setAutomaticMouseHiding(v);
505 },
506
Robert Ginda57f03b42012-09-13 11:02:48 -0700507 'home-keys-scroll': function(v) {
508 terminal.keyboard.homeKeysScroll = v;
509 },
rginda4bba5e12012-06-20 16:15:30 -0700510
Robert Gindaa8165692015-06-15 14:46:31 -0700511 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700512 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
513 },
Robert Gindaa8165692015-06-15 14:46:31 -0700514
Joel Hockey95a9e272020-03-16 21:19:53 -0700515 'keybindings-os-defaults': function(v) {
516 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700517 },
518
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700519 'media-keys-are-fkeys': function(v) {
520 terminal.keyboard.mediaKeysAreFKeys = v;
521 },
522
Robert Ginda57f03b42012-09-13 11:02:48 -0700523 'meta-sends-escape': function(v) {
524 terminal.keyboard.metaSendsEscape = v;
525 },
rginda30f20f62012-04-05 16:36:19 -0700526
Mike Frysinger847577f2017-05-23 23:25:57 -0400527 'mouse-right-click-paste': function(v) {
528 terminal.mouseRightClickPaste = v;
529 },
530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'mouse-paste-button': function(v) {
532 terminal.syncMousePasteButton();
533 },
rgindaa8ba17d2012-08-15 14:41:10 -0700534
Robert Gindae76aa9f2014-03-14 12:29:12 -0700535 'page-keys-scroll': function(v) {
536 terminal.keyboard.pageKeysScroll = v;
537 },
538
Robert Ginda40932892012-12-10 17:26:40 -0800539 'pass-alt-number': function(v) {
540 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700541 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800542 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500543 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800544 }
545
546 terminal.passAltNumber = v;
547 },
548
549 'pass-ctrl-number': function(v) {
550 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700551 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800552 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500553 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800554 }
555
556 terminal.passCtrlNumber = v;
557 },
558
Joel Hockey0e052042020-02-19 05:37:19 -0800559 'pass-ctrl-n': function(v) {
560 terminal.passCtrlN = v;
561 },
562
563 'pass-ctrl-t': function(v) {
564 terminal.passCtrlT = v;
565 },
566
567 'pass-ctrl-tab': function(v) {
568 terminal.passCtrlTab = v;
569 },
570
571 'pass-ctrl-w': function(v) {
572 terminal.passCtrlW = v;
573 },
574
Robert Ginda40932892012-12-10 17:26:40 -0800575 'pass-meta-number': function(v) {
576 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700577 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800578 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500579 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800580 }
581
582 terminal.passMetaNumber = v;
583 },
584
Marius Schilder77857b32014-05-14 16:21:26 -0700585 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700586 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700587 },
588
Robert Ginda8cb7d902013-06-20 14:37:18 -0700589 'receive-encoding': function(v) {
590 if (!(/^(utf-8|raw)$/).test(v)) {
591 console.warn('Invalid value for "receive-encoding": ' + v);
592 v = 'utf-8';
593 }
594
595 terminal.vt.characterEncoding = v;
596 },
597
Joel Hockey139d82d2020-04-07 23:04:29 -0700598 'screen-padding-size': function(v) {
599 v = parseInt(v, 10);
600 if (isNaN(v) || v < 0) {
601 console.error(`Invalid screen padding size: ${v}`);
602 return;
603 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700604 terminal.setScreenPaddingSize(v);
605 },
606
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700607 'screen-border-size': function(v) {
608 v = parseInt(v, 10);
609 if (isNaN(v) || v < 0) {
610 console.error(`Invalid screen border size: ${v}`);
611 return;
612 }
613 terminal.setScreenBorderSize(v);
614 },
615
616 'screen-border-color': function(v) {
617 terminal.div_.style.borderColor = v;
618 },
619
Robert Ginda57f03b42012-09-13 11:02:48 -0700620 'scroll-on-keystroke': function(v) {
621 terminal.scrollOnKeystroke_ = v;
622 },
rginda9f5222b2012-03-05 11:53:28 -0800623
Robert Ginda57f03b42012-09-13 11:02:48 -0700624 'scroll-on-output': function(v) {
625 terminal.scrollOnOutput_ = v;
626 },
rginda30f20f62012-04-05 16:36:19 -0700627
Robert Ginda57f03b42012-09-13 11:02:48 -0700628 'scrollbar-visible': function(v) {
629 terminal.setScrollbarVisible(v);
630 },
rginda9f5222b2012-03-05 11:53:28 -0800631
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400632 'scroll-wheel-may-send-arrow-keys': function(v) {
633 terminal.scrollWheelArrowKeys_ = v;
634 },
635
Rob Spies49039e52014-12-17 13:40:04 -0800636 'scroll-wheel-move-multiplier': function(v) {
637 terminal.setScrollWheelMoveMultipler(v);
638 },
639
Robert Ginda57f03b42012-09-13 11:02:48 -0700640 'shift-insert-paste': function(v) {
641 terminal.keyboard.shiftInsertPaste = v;
642 },
rginda9f5222b2012-03-05 11:53:28 -0800643
Mike Frysingera7768922017-07-28 15:00:12 -0400644 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400645 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400646 },
647
Robert Gindae76aa9f2014-03-14 12:29:12 -0700648 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400649 terminal.scrollPort_.setUserCssUrl(v);
650 },
651
652 'user-css-text': function(v) {
653 terminal.scrollPort_.setUserCssText(v);
654 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400655
656 'word-break-match-left': function(v) {
657 terminal.primaryScreen_.wordBreakMatchLeft = v;
658 terminal.alternateScreen_.wordBreakMatchLeft = v;
659 },
660
661 'word-break-match-right': function(v) {
662 terminal.primaryScreen_.wordBreakMatchRight = v;
663 terminal.alternateScreen_.wordBreakMatchRight = v;
664 },
665
666 'word-break-match-middle': function(v) {
667 terminal.primaryScreen_.wordBreakMatchMiddle = v;
668 terminal.alternateScreen_.wordBreakMatchMiddle = v;
669 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400670
671 'allow-images-inline': function(v) {
672 terminal.allowImagesInline = v;
673 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700674 });
rginda30f20f62012-04-05 16:36:19 -0700675
Robert Ginda57f03b42012-09-13 11:02:48 -0700676 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800677 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700678
Mike Frysingerec4225d2020-04-07 05:00:01 -0400679 if (callback) {
680 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400681 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700682 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800683};
684
Rob Spies56953412014-04-28 14:09:47 -0700685/**
686 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500687 *
Joel Hockey0f933582019-08-27 18:01:51 -0700688 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700689 */
690hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700691 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700692};
693
Robert Gindaa063b202014-07-21 11:08:25 -0700694/**
695 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500696 *
697 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700698 */
699hterm.Terminal.prototype.setBracketedPaste = function(state) {
700 this.options_.bracketedPaste = state;
701};
Rob Spies56953412014-04-28 14:09:47 -0700702
rginda8e92a692012-05-20 19:37:20 -0700703/**
704 * Set the color for the cursor.
705 *
706 * If you want this setting to persist, set it through prefs_, rather than
707 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500708 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500709 * @param {string=} color The color to set. If not defined, we reset to the
710 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700711 */
712hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400713 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700714 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400715 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500716
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400717 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700718};
719
720/**
721 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400722 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500723 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700724 */
725hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400726 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700727};
728
729/**
rgindad5613292012-06-19 15:40:37 -0700730 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500731 *
732 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700733 */
734hterm.Terminal.prototype.setSelectionEnabled = function(state) {
735 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700736};
737
738/**
rginda8e92a692012-05-20 19:37:20 -0700739 * Set the background color.
740 *
741 * If you want this setting to persist, set it through prefs_, rather than
742 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500743 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500744 * @param {string=} color The color to set. If not defined, we reset to the
745 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700746 */
747hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400748 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700749 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400750 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500751
Joel Hockey42dba8f2020-03-26 16:21:11 -0700752 this.backgroundColor_ = lib.colors.normalizeCSS(color);
753 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700754};
755
rginda9f5222b2012-03-05 11:53:28 -0800756/**
757 * Return the current terminal background color.
758 *
759 * Intended for use by other classes, so we don't have to expose the entire
760 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500761 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700762 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800763 */
764hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700765 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700766};
767
768/**
769 * Set the foreground color.
770 *
771 * If you want this setting to persist, set it through prefs_, rather than
772 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500773 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500774 * @param {string=} color The color to set. If not defined, we reset to the
775 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700776 */
777hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400778 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700779 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400780 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500781
Joel Hockey42dba8f2020-03-26 16:21:11 -0700782 this.foregroundColor_ = lib.colors.normalizeCSS(color);
783 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800784};
785
786/**
787 * Return the current terminal foreground color.
788 *
789 * Intended for use by other classes, so we don't have to expose the entire
790 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500791 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700792 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800793 */
794hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700795 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800796};
797
798/**
rginda87b86462011-12-14 13:48:03 -0800799 * Create a new instance of a terminal command and run it with a given
800 * argument string.
801 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700802 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700803 * @param {string} commandName The command to run for this terminal.
804 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800805 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700806hterm.Terminal.prototype.runCommandClass = function(
807 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400808 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400809 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700810 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400811 }
rgindaf522ce02012-04-17 17:49:17 -0700812
rginda87b86462011-12-14 13:48:03 -0800813 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700814 {
815 commandName: commandName,
816 args: args,
rginda87b86462011-12-14 13:48:03 -0800817 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700818 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400819 onExit: (code) => {
820 this.io.pop();
821 this.uninstallKeyboard();
822 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
823 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400824 window.close();
825 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400826 },
rginda87b86462011-12-14 13:48:03 -0800827 });
828
rgindafeaf3142012-01-31 15:14:20 -0800829 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800830 this.command.run();
831};
832
833/**
rgindafeaf3142012-01-31 15:14:20 -0800834 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500835 *
836 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800837 */
838hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700839 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800840};
841
842/**
843 * Install the keyboard handler for this terminal.
844 *
845 * This will prevent the browser from seeing any keystrokes sent to the
846 * terminal.
847 */
848hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700849 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400850};
rgindafeaf3142012-01-31 15:14:20 -0800851
852/**
853 * Uninstall the keyboard handler for this terminal.
854 */
855hterm.Terminal.prototype.uninstallKeyboard = function() {
856 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400857};
rgindafeaf3142012-01-31 15:14:20 -0800858
859/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400860 * Set a CSS variable.
861 *
862 * Normally this is used to set variables in the hterm namespace.
863 *
864 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700865 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400866 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400867 */
868hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400869 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400870 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400871 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400872};
873
874/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700875 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
876 * input is valid.
877 *
878 * @param {string} name The variable to set.
879 * @param {?string} rgb The rgb value to assign to the variable.
880 */
881hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
882 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
883 if (ary) {
884 this.setCssVar(name, ary.slice(0, 3).join(','));
885 }
886};
887
888/**
889 * Sets the specified color for the active screen.
890 *
891 * @param {number} i The index into the 256 color palette to set.
892 * @param {?string} rgb The rgb value to assign to the variable.
893 */
894hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
895 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
896 this.setRgbColorCssVar(`color-${i}`, rgb);
897 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
898 }
899};
900
901/**
902 * Returns the current value in the active screen of the specified color.
903 *
904 * @param {number} i Color palette index.
905 * @return {string} rgb color.
906 */
907hterm.Terminal.prototype.getColorPalette = function(i) {
908 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
909 lib.colors.colorPalette[i];
910};
911
912/**
913 * Reset the specified color in the active screen to its default value.
914 *
915 * @param {number} i Color to reset
916 */
917hterm.Terminal.prototype.resetColor = function(i) {
918 this.setColorPalette(i, lib.colors.colorPalette[i]);
919 delete this.screen_.textAttributes.colorPaletteOverrides[i];
920};
921
922/**
923 * Reset the current screen color palette to the default state.
924 */
925hterm.Terminal.prototype.resetColorPalette = function() {
926 this.screen_.textAttributes.colorPaletteOverrides.forEach(
927 (c, i) => this.resetColor(i));
928};
929
930/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500931 * Get a CSS variable.
932 *
933 * Normally this is used to get variables in the hterm namespace.
934 *
935 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400936 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500937 * @return {string} The current setting for this variable.
938 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400939hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500940 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400941 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500942};
943
944/**
Jason Linbbbdb752020-03-06 16:26:59 +1100945 * Update CSS character size variables to match the scrollport.
946 */
947hterm.Terminal.prototype.updateCssCharsize_ = function() {
948 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
949 this.setCssVar('charsize-height',
950 this.scrollPort_.characterSize.height + 'px');
951};
952
953/**
rginda35c456b2012-02-09 17:29:05 -0800954 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800955 *
956 * Call setFontSize(0) to reset to the default font size.
957 *
958 * This function does not modify the font-size preference.
959 *
960 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800961 */
962hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400963 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700964 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400965 }
rginda9f5222b2012-03-05 11:53:28 -0800966
rginda35c456b2012-02-09 17:29:05 -0800967 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100968 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800969};
970
971/**
972 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500973 *
974 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800975 */
976hterm.Terminal.prototype.getFontSize = function() {
977 return this.scrollPort_.getFontSize();
978};
979
980/**
rginda8e92a692012-05-20 19:37:20 -0700981 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500982 *
983 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700984 */
985hterm.Terminal.prototype.getFontFamily = function() {
986 return this.scrollPort_.getFontFamily();
987};
988
989/**
rginda35c456b2012-02-09 17:29:05 -0800990 * Set the CSS "font-family" for this terminal.
991 */
rginda9f5222b2012-03-05 11:53:28 -0800992hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700993 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
994 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100995 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800996 this.syncBoldSafeState();
997};
998
rginda4bba5e12012-06-20 16:15:30 -0700999/**
1000 * Set this.mousePasteButton based on the mouse-paste-button pref,
1001 * autodetecting if necessary.
1002 */
1003hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001004 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001005 if (typeof button == 'number') {
1006 this.mousePasteButton = button;
1007 return;
1008 }
1009
Mike Frysingeree81a002017-12-12 16:14:53 -05001010 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001011 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001012 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001013 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001014 }
1015};
1016
1017/**
1018 * Enable or disable bold based on the enable-bold pref, autodetecting if
1019 * necessary.
1020 */
rginda9f5222b2012-03-05 11:53:28 -08001021hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001022 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001023 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001024 this.primaryScreen_.textAttributes.enableBold = enableBold;
1025 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001026 return;
1027 }
1028
Mike Frysingerdc727792020-04-10 01:41:13 -04001029 const normalSize = this.scrollPort_.measureCharacterSize();
1030 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001031
Mike Frysingerdc727792020-04-10 01:41:13 -04001032 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001033 if (!isBoldSafe) {
1034 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001035 'from normal. Font family is: ' +
1036 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001037 }
rginda9f5222b2012-03-05 11:53:28 -08001038
Robert Gindaed016262012-10-26 16:27:09 -07001039 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1040 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001041};
1042
1043/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001044 * Control text blinking behavior.
1045 *
1046 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001047 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001048hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001049 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001050 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001051 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001052 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001053};
1054
1055/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001056 * Set the mouse cursor style based on the current terminal mode.
1057 */
1058hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001059 this.setCssVar('mouse-cursor-style',
1060 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1061 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001062 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001063};
1064
1065/**
rginda87b86462011-12-14 13:48:03 -08001066 * Return a copy of the current cursor position.
1067 *
Joel Hockey0f933582019-08-27 18:01:51 -07001068 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001069 */
1070hterm.Terminal.prototype.saveCursor = function() {
1071 return this.screen_.cursorPosition.clone();
1072};
1073
Evan Jones2600d4f2016-12-06 09:29:36 -05001074/**
1075 * Return the current text attributes.
1076 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001077 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001078 */
rgindaa19afe22012-01-25 15:40:22 -08001079hterm.Terminal.prototype.getTextAttributes = function() {
1080 return this.screen_.textAttributes;
1081};
1082
Evan Jones2600d4f2016-12-06 09:29:36 -05001083/**
1084 * Set the text attributes.
1085 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001086 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001087 */
rginda1a09aa02012-06-18 21:11:25 -07001088hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1089 this.screen_.textAttributes = textAttributes;
1090};
1091
rginda87b86462011-12-14 13:48:03 -08001092/**
rgindaf522ce02012-04-17 17:49:17 -07001093 * Return the current browser zoom factor applied to the terminal.
1094 *
1095 * @return {number} The current browser zoom factor.
1096 */
1097hterm.Terminal.prototype.getZoomFactor = function() {
1098 return this.scrollPort_.characterSize.zoomFactor;
1099};
1100
1101/**
rginda9846e2f2012-01-27 13:53:33 -08001102 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001103 *
1104 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001105 */
1106hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001107 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001108};
1109
1110/**
rginda87b86462011-12-14 13:48:03 -08001111 * Restore a previously saved cursor position.
1112 *
Joel Hockey0f933582019-08-27 18:01:51 -07001113 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001114 */
1115hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001116 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1117 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001118 this.screen_.setCursorPosition(row, column);
1119 if (cursor.column > column ||
1120 cursor.column == column && cursor.overflow) {
1121 this.screen_.cursorPosition.overflow = true;
1122 }
rginda87b86462011-12-14 13:48:03 -08001123};
1124
1125/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001126 * Clear the cursor's overflow flag.
1127 */
1128hterm.Terminal.prototype.clearCursorOverflow = function() {
1129 this.screen_.cursorPosition.overflow = false;
1130};
1131
1132/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001133 * Save the current cursor state to the corresponding screens.
1134 *
1135 * See the hterm.Screen.CursorState class for more details.
1136 *
1137 * @param {boolean=} both If true, update both screens, else only update the
1138 * current screen.
1139 */
1140hterm.Terminal.prototype.saveCursorAndState = function(both) {
1141 if (both) {
1142 this.primaryScreen_.saveCursorAndState(this.vt);
1143 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001144 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001145 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001146 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001147};
1148
1149/**
1150 * Restore the saved cursor state in the corresponding screens.
1151 *
1152 * See the hterm.Screen.CursorState class for more details.
1153 *
1154 * @param {boolean=} both If true, update both screens, else only update the
1155 * current screen.
1156 */
1157hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1158 if (both) {
1159 this.primaryScreen_.restoreCursorAndState(this.vt);
1160 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001161 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001162 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001163 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001164};
1165
1166/**
Robert Ginda830583c2013-08-07 13:20:46 -07001167 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001168 *
1169 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001170 */
1171hterm.Terminal.prototype.setCursorShape = function(shape) {
1172 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001173 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001174};
Robert Ginda830583c2013-08-07 13:20:46 -07001175
1176/**
1177 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001178 *
1179 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001180 */
1181hterm.Terminal.prototype.getCursorShape = function() {
1182 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001183};
Robert Ginda830583c2013-08-07 13:20:46 -07001184
1185/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001186 * Set the screen padding size in pixels.
1187 *
1188 * @param {number} size
1189 */
1190hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001191 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001192 this.scrollPort_.setScreenPaddingSize(size);
1193};
1194
1195/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001196 * Set the screen border size in pixels.
1197 *
1198 * @param {number} size
1199 */
1200hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1201 this.div_.style.borderWidth = `${size}px`;
1202 this.screenBorderSize_ = size;
1203 this.scrollPort_.resize();
1204};
1205
1206/**
rginda87b86462011-12-14 13:48:03 -08001207 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001208 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001209 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001210 */
1211hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001212 if (columnCount == null) {
1213 this.div_.style.width = '100%';
1214 return;
1215 }
1216
Joel Hockey139d82d2020-04-07 23:04:29 -07001217 const rightPadding = Math.max(
1218 this.scrollPort_.screenPaddingSize,
1219 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001220 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001221 (this.scrollPort_.characterSize.width * columnCount) +
1222 this.scrollPort_.screenPaddingSize + rightPadding +
1223 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001224 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001225 this.scheduleSyncCursorPosition_();
1226};
rginda87b86462011-12-14 13:48:03 -08001227
rgindac9bc5502012-01-18 11:48:44 -08001228/**
rginda35c456b2012-02-09 17:29:05 -08001229 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001230 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001231 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001232 */
1233hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001234 if (rowCount == null) {
1235 this.div_.style.height = '100%';
1236 return;
1237 }
1238
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001239 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1240 (2 * this.scrollPort_.screenPaddingSize) +
1241 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001242 this.realizeSize_(this.screenSize.width, rowCount);
1243 this.scheduleSyncCursorPosition_();
1244};
1245
1246/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001247 * Deal with terminal size changes.
1248 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001249 * @param {number} columnCount The number of columns.
1250 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001251 */
1252hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001253 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001254
Mike Frysinger0206e262019-06-13 10:18:19 -04001255 if (columnCount != this.screenSize.width) {
1256 notify = true;
1257 this.realizeWidth_(columnCount);
1258 }
1259
1260 if (rowCount != this.screenSize.height) {
1261 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001262 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001263 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001264
1265 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001266 if (notify) {
1267 this.io.onTerminalResize_(columnCount, rowCount);
1268 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001269};
1270
1271/**
rgindac9bc5502012-01-18 11:48:44 -08001272 * Deal with terminal width changes.
1273 *
1274 * This function does what needs to be done when the terminal width changes
1275 * out from under us. It happens here rather than in onResize_() because this
1276 * code may need to run synchronously to handle programmatic changes of
1277 * terminal width.
1278 *
1279 * Relying on the browser to send us an async resize event means we may not be
1280 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001281 *
1282 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001283 */
1284hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001285 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001286 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001287 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001288
Mike Frysingerdc727792020-04-10 01:41:13 -04001289 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001290 if (deltaColumns == 0) {
1291 // No change, so don't bother recalculating things.
1292 return;
1293 }
rgindac9bc5502012-01-18 11:48:44 -08001294
rginda87b86462011-12-14 13:48:03 -08001295 this.screenSize.width = columnCount;
1296 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001297
1298 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001299 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001300 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001301 }
rgindac9bc5502012-01-18 11:48:44 -08001302 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001303 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001304 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001305 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001306 }
rgindac9bc5502012-01-18 11:48:44 -08001307
1308 this.tabStops_.pop();
1309 }
1310 }
1311
1312 this.screen_.setColumnCount(this.screenSize.width);
1313};
1314
1315/**
1316 * Deal with terminal height changes.
1317 *
1318 * This function does what needs to be done when the terminal height changes
1319 * out from under us. It happens here rather than in onResize_() because this
1320 * code may need to run synchronously to handle programmatic changes of
1321 * terminal height.
1322 *
1323 * Relying on the browser to send us an async resize event means we may not be
1324 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001325 *
1326 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001327 */
1328hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001329 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001330 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001331 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001332
Mike Frysingerdc727792020-04-10 01:41:13 -04001333 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001334 if (deltaRows == 0) {
1335 // No change, so don't bother recalculating things.
1336 return;
1337 }
rgindac9bc5502012-01-18 11:48:44 -08001338
1339 this.screenSize.height = rowCount;
1340
Mike Frysingerdc727792020-04-10 01:41:13 -04001341 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001342
1343 if (deltaRows < 0) {
1344 // Screen got smaller.
1345 deltaRows *= -1;
1346 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001347 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001348 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001349 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001350 }
rgindac9bc5502012-01-18 11:48:44 -08001351
Mike Frysingerbdb34802020-04-07 03:47:32 -04001352 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001353 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001354 }
rgindac9bc5502012-01-18 11:48:44 -08001355
1356 this.screen_.popRow();
1357 deltaRows--;
1358 }
1359
Mike Frysingerdc727792020-04-10 01:41:13 -04001360 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001361 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1362
1363 // We just removed rows from the top of the screen, we need to update
1364 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001365 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001366 } else if (deltaRows > 0) {
1367 // Screen got larger.
1368
1369 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001370 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1371 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001372 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1373 this.screen_.unshiftRows(rows);
1374 deltaRows -= scrollbackCount;
1375 cursor.row += scrollbackCount;
1376 }
1377
Mike Frysingerbdb34802020-04-07 03:47:32 -04001378 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001379 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001380 }
rgindac9bc5502012-01-18 11:48:44 -08001381 }
1382
rginda35c456b2012-02-09 17:29:05 -08001383 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001384 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001385};
1386
1387/**
1388 * Scroll the terminal to the top of the scrollback buffer.
1389 */
1390hterm.Terminal.prototype.scrollHome = function() {
1391 this.scrollPort_.scrollRowToTop(0);
1392};
1393
1394/**
1395 * Scroll the terminal to the end.
1396 */
1397hterm.Terminal.prototype.scrollEnd = function() {
1398 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1399};
1400
1401/**
1402 * Scroll the terminal one page up (minus one line) relative to the current
1403 * position.
1404 */
1405hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001406 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001407};
1408
1409/**
1410 * Scroll the terminal one page down (minus one line) relative to the current
1411 * position.
1412 */
1413hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001414 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001415};
1416
rgindac9bc5502012-01-18 11:48:44 -08001417/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001418 * Scroll the terminal one line up relative to the current position.
1419 */
1420hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001421 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001422 this.scrollPort_.scrollRowToTop(i - 1);
1423};
1424
1425/**
1426 * Scroll the terminal one line down relative to the current position.
1427 */
1428hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001429 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001430 this.scrollPort_.scrollRowToTop(i + 1);
1431};
1432
1433/**
Robert Ginda40932892012-12-10 17:26:40 -08001434 * Clear primary screen, secondary screen, and the scrollback buffer.
1435 */
1436hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001437 this.clearHome(this.primaryScreen_);
1438 this.clearHome(this.alternateScreen_);
1439
1440 this.clearScrollback();
1441};
1442
1443/**
1444 * Clear scrollback buffer.
1445 */
1446hterm.Terminal.prototype.clearScrollback = function() {
1447 // Move to the end of the buffer in case the screen was scrolled back.
1448 // We're going to throw it away which would leave the display invalid.
1449 this.scrollEnd();
1450
Robert Ginda40932892012-12-10 17:26:40 -08001451 this.scrollbackRows_.length = 0;
1452 this.scrollPort_.resetCache();
1453
Mike Frysinger9c482b82018-09-07 02:49:36 -04001454 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1455 const bottom = screen.getHeight();
1456 this.renumberRows_(0, bottom, screen);
1457 });
Robert Ginda40932892012-12-10 17:26:40 -08001458
1459 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001460 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001461};
1462
1463/**
rgindac9bc5502012-01-18 11:48:44 -08001464 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001465 *
1466 * Perform a full reset to the default values listed in
1467 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001468 */
rginda87b86462011-12-14 13:48:03 -08001469hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001470 this.vt.reset();
1471
rgindac9bc5502012-01-18 11:48:44 -08001472 this.clearAllTabStops();
1473 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001474
Joel Hockey42dba8f2020-03-26 16:21:11 -07001475 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001476 const resetScreen = (screen) => {
1477 // We want to make sure to reset the attributes before we clear the screen.
1478 // The attributes might be used to initialize default/empty rows.
1479 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001480 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001481 this.clearHome(screen);
1482 screen.saveCursorAndState(this.vt);
1483 };
1484 resetScreen(this.primaryScreen_);
1485 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001486
Mike Frysinger84301d02017-11-29 13:28:46 -08001487 // Reset terminal options to their default values.
1488 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001489 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1490
Mike Frysinger84301d02017-11-29 13:28:46 -08001491 this.setVTScrollRegion(null, null);
1492
1493 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001494};
1495
rgindac9bc5502012-01-18 11:48:44 -08001496/**
1497 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001498 *
1499 * Perform a soft reset to the default values listed in
1500 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001501 */
rginda0f5c0292012-01-13 11:00:13 -08001502hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001503 this.vt.reset();
1504
rgindab8bc8932012-04-27 12:45:03 -07001505 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001506 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001507
Brad Townb62dfdc2015-03-16 19:07:15 -07001508 // We show the cursor on soft reset but do not alter the blink state.
1509 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1510
Joel Hockey42dba8f2020-03-26 16:21:11 -07001511 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001512 const resetScreen = (screen) => {
1513 // Xterm also resets the color palette on soft reset, even though it doesn't
1514 // seem to be documented anywhere.
1515 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001516 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001517 screen.saveCursorAndState(this.vt);
1518 };
1519 resetScreen(this.primaryScreen_);
1520 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001521
rgindab8bc8932012-04-27 12:45:03 -07001522 // The xterm man page explicitly says this will happen on soft reset.
1523 this.setVTScrollRegion(null, null);
1524
1525 // Xterm also shows the cursor on soft reset, but does not alter the blink
1526 // state.
rgindaa19afe22012-01-25 15:40:22 -08001527 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001528};
1529
rgindac9bc5502012-01-18 11:48:44 -08001530/**
1531 * Move the cursor forward to the next tab stop, or to the last column
1532 * if no more tab stops are set.
1533 */
1534hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001535 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001536
Mike Frysingerdc727792020-04-10 01:41:13 -04001537 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001538 if (this.tabStops_[i] > column) {
1539 this.setCursorColumn(this.tabStops_[i]);
1540 return;
1541 }
1542 }
1543
David Benjamin66e954d2012-05-05 21:08:12 -04001544 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001545 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001546 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001547 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001548};
1549
rgindac9bc5502012-01-18 11:48:44 -08001550/**
1551 * Move the cursor backward to the previous tab stop, or to the first column
1552 * if no previous tab stops are set.
1553 */
1554hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001555 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001556
Mike Frysingerdc727792020-04-10 01:41:13 -04001557 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001558 if (this.tabStops_[i] < column) {
1559 this.setCursorColumn(this.tabStops_[i]);
1560 return;
1561 }
1562 }
1563
1564 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001565};
1566
rgindac9bc5502012-01-18 11:48:44 -08001567/**
1568 * Set a tab stop at the given column.
1569 *
Joel Hockey0f933582019-08-27 18:01:51 -07001570 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001571 */
1572hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001573 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001574 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001575 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001576 }
rgindac9bc5502012-01-18 11:48:44 -08001577
1578 if (this.tabStops_[i] < column) {
1579 this.tabStops_.splice(i + 1, 0, column);
1580 return;
1581 }
1582 }
1583
1584 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001585};
1586
rgindac9bc5502012-01-18 11:48:44 -08001587/**
1588 * Clear the tab stop at the current cursor position.
1589 *
1590 * No effect if there is no tab stop at the current cursor position.
1591 */
1592hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001593 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001594
Mike Frysingerdc727792020-04-10 01:41:13 -04001595 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001596 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001597 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001598 }
rgindac9bc5502012-01-18 11:48:44 -08001599
1600 this.tabStops_.splice(i, 1);
1601};
1602
1603/**
1604 * Clear all tab stops.
1605 */
1606hterm.Terminal.prototype.clearAllTabStops = function() {
1607 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001608 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001609};
1610
1611/**
1612 * Set up the default tab stops, starting from a given column.
1613 *
1614 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001615 * from the specified column, or 0 if no column is provided. It also flags
1616 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001617 *
1618 * This does not clear the existing tab stops first, use clearAllTabStops
1619 * for that.
1620 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001621 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001622 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001623 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001624hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001625 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001626 // Round start up to a default tab stop.
1627 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001628 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001629 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001630 }
David Benjamin66e954d2012-05-05 21:08:12 -04001631
1632 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001633};
1634
rginda6d397402012-01-17 10:58:29 -08001635/**
rginda8ba33642011-12-14 12:31:31 -08001636 * Interpret a sequence of characters.
1637 *
1638 * Incomplete escape sequences are buffered until the next call.
1639 *
1640 * @param {string} str Sequence of characters to interpret or pass through.
1641 */
1642hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001643 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001644 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001645};
1646
1647/**
1648 * Take over the given DIV for use as the terminal display.
1649 *
Joel Hockey0f933582019-08-27 18:01:51 -07001650 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001651 */
1652hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001653 const charset = div.ownerDocument.characterSet.toLowerCase();
1654 if (charset != 'utf-8') {
1655 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1656 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1657 }
1658
rginda87b86462011-12-14 13:48:03 -08001659 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001660 this.div_.style.borderStyle = 'solid';
1661 this.div_.style.borderWidth = 0;
1662 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001663
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001664 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1665
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001666 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1667};
1668
1669/**
1670 * Initialisation of ScrollPort properties which need to be set after its DOM
1671 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001672 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001673 * @private
1674 */
1675hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001676 this.scrollPort_.setBackgroundImage(
1677 this.prefs_.getString('background-image'));
1678 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001679 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001680 this.prefs_.getString('background-position'));
1681 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1682 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1683 this.scrollPort_.setAccessibilityReader(
1684 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001685
rginda0918b652012-04-04 11:26:24 -07001686 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001687
Joel Hockeyd4fca732019-09-20 16:57:03 -07001688 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001689 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001690
Joel Hockeyd4fca732019-09-20 16:57:03 -07001691 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001692 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001693 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001694
rginda8ba33642011-12-14 12:31:31 -08001695 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001696 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001697
Evan Jones5f9df812016-12-06 09:38:58 -05001698 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001699 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001700
Mike Frysingerdc727792020-04-10 01:41:13 -04001701 const onMouse = this.onMouse_.bind(this);
1702 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001703 screenNode.addEventListener(
1704 'mousedown', /** @type {!EventListener} */ (onMouse));
1705 screenNode.addEventListener(
1706 'mouseup', /** @type {!EventListener} */ (onMouse));
1707 screenNode.addEventListener(
1708 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001709 this.scrollPort_.onScrollWheel = onMouse;
1710
Joel Hockeyd4fca732019-09-20 16:57:03 -07001711 screenNode.addEventListener(
1712 'keydown',
1713 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001714
Toni Barzic0bfa8922013-11-22 11:18:35 -08001715 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001716 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001717 // Listen for mousedown events on the screenNode as in FF the focus
1718 // events don't bubble.
1719 screenNode.addEventListener('mousedown', function() {
1720 setTimeout(this.onFocusChange_.bind(this, true));
1721 }.bind(this));
1722
Toni Barzic0bfa8922013-11-22 11:18:35 -08001723 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001724 'blur', this.onFocusChange_.bind(this, false));
1725
Mike Frysingerdc727792020-04-10 01:41:13 -04001726 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001727 style.textContent = `
1728.cursor-node[focus="false"] {
1729 box-sizing: border-box;
1730 background-color: transparent !important;
1731 border-width: 2px;
1732 border-style: solid;
1733}
1734menu {
1735 margin: 0;
1736 padding: 0;
1737 cursor: var(--hterm-mouse-cursor-pointer);
1738}
1739menuitem {
1740 white-space: nowrap;
1741 border-bottom: 1px dashed;
1742 display: block;
1743 padding: 0.3em 0.3em 0 0.3em;
1744}
1745menuitem.separator {
1746 border-bottom: none;
1747 height: 0.5em;
1748 padding: 0;
1749}
1750menuitem:hover {
1751 color: var(--hterm-cursor-color);
1752}
1753.wc-node {
1754 display: inline-block;
1755 text-align: center;
1756 width: calc(var(--hterm-charsize-width) * 2);
1757 line-height: var(--hterm-charsize-height);
1758}
1759:root {
1760 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1761 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001762 --hterm-blink-node-duration: 0.7s;
1763 --hterm-mouse-cursor-default: default;
1764 --hterm-mouse-cursor-text: text;
1765 --hterm-mouse-cursor-pointer: pointer;
1766 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001767 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001768
Joel Hockey42dba8f2020-03-26 16:21:11 -07001769${lib.colors.stockColorPalette.map((c, i) => `
1770 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1771`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001772}
1773.uri-node:hover {
1774 text-decoration: underline;
1775 cursor: var(--hterm-mouse-cursor-pointer);
1776}
1777@keyframes blink {
1778 from { opacity: 1.0; }
1779 to { opacity: 0.0; }
1780}
1781.blink-node {
1782 animation-name: blink;
1783 animation-duration: var(--hterm-blink-node-duration);
1784 animation-iteration-count: infinite;
1785 animation-timing-function: ease-in-out;
1786 animation-direction: alternate;
1787}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001788 // Insert this stock style as the first node so that any user styles will
1789 // override w/out having to use !important everywhere. The rules above mix
1790 // runtime variables with default ones designed to be overridden by the user,
1791 // but we can wait for a concrete case from the users to determine the best
1792 // way to split the sheet up to before & after the user-css settings.
1793 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001794
rginda8ba33642011-12-14 12:31:31 -08001795 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001796 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001797 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001798 this.cursorNode_.style.cssText = `
1799position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001800left: calc(var(--hterm-screen-padding-size) +
1801 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1802top: calc(var(--hterm-screen-padding-size) +
1803 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001804display: ${this.options_.cursorVisible ? '' : 'none'};
1805width: var(--hterm-charsize-width);
1806height: var(--hterm-charsize-height);
1807background-color: var(--hterm-cursor-color);
1808border-color: var(--hterm-cursor-color);
1809-webkit-transition: opacity, background-color 100ms linear;
1810-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001811
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001812 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001813 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1814 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001815
rginda8ba33642011-12-14 12:31:31 -08001816 this.document_.body.appendChild(this.cursorNode_);
1817
rgindad5613292012-06-19 15:40:37 -07001818 // When 'enableMouseDragScroll' is off we reposition this element directly
1819 // under the mouse cursor after a click. This makes Chrome associate
1820 // subsequent mousemove events with the scroll-blocker. Since the
1821 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1822 // events do not cause the scrollport to scroll.
1823 //
1824 // It's a hack, but it's the cleanest way I could find.
1825 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001826 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001827 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001828 this.scrollBlockerNode_.style.cssText =
1829 ('position: absolute;' +
1830 'top: -99px;' +
1831 'display: block;' +
1832 'width: 10px;' +
1833 'height: 10px;');
1834 this.document_.body.appendChild(this.scrollBlockerNode_);
1835
rgindad5613292012-06-19 15:40:37 -07001836 this.scrollPort_.onScrollWheel = onMouse;
1837 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1838 ].forEach(function(event) {
1839 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001840 this.cursorNode_.addEventListener(
1841 event, /** @type {!EventListener} */ (onMouse));
1842 this.document_.addEventListener(
1843 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001844 }.bind(this));
1845
1846 this.cursorNode_.addEventListener('mousedown', function() {
1847 setTimeout(this.focus.bind(this));
1848 }.bind(this));
1849
rginda8ba33642011-12-14 12:31:31 -08001850 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001851
rginda87b86462011-12-14 13:48:03 -08001852 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001853 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001854};
1855
rginda0918b652012-04-04 11:26:24 -07001856/**
1857 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001858 *
Joel Hockey0f933582019-08-27 18:01:51 -07001859 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001860 */
rginda87b86462011-12-14 13:48:03 -08001861hterm.Terminal.prototype.getDocument = function() {
1862 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001863};
1864
1865/**
rginda0918b652012-04-04 11:26:24 -07001866 * Focus the terminal.
1867 */
1868hterm.Terminal.prototype.focus = function() {
1869 this.scrollPort_.focus();
1870};
1871
1872/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001873 * Unfocus the terminal.
1874 */
1875hterm.Terminal.prototype.blur = function() {
1876 this.scrollPort_.blur();
1877};
1878
1879/**
rginda8ba33642011-12-14 12:31:31 -08001880 * Return the HTML Element for a given row index.
1881 *
1882 * This is a method from the RowProvider interface. The ScrollPort uses
1883 * it to fetch rows on demand as they are scrolled into view.
1884 *
1885 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1886 * pairs to conserve memory.
1887 *
Joel Hockey0f933582019-08-27 18:01:51 -07001888 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001889 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001890 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001891 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001892 * @override
rginda8ba33642011-12-14 12:31:31 -08001893 */
1894hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001895 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001896 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001897 }
rginda8ba33642011-12-14 12:31:31 -08001898
Mike Frysingerdc727792020-04-10 01:41:13 -04001899 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001900 return this.screen_.rowsArray[screenIndex];
1901};
1902
1903/**
1904 * Return the text content for a given range of rows.
1905 *
1906 * This is a method from the RowProvider interface. The ScrollPort uses
1907 * it to fetch text content on demand when the user attempts to copy their
1908 * selection to the clipboard.
1909 *
Joel Hockey0f933582019-08-27 18:01:51 -07001910 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001911 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001912 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001913 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001914 * relative to the start of the scrollback buffer.
1915 * @return {string} A single string containing the text value of the range of
1916 * rows. Lines will be newline delimited, with no trailing newline.
1917 */
1918hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001919 const ary = [];
1920 for (let i = start; i < end; i++) {
1921 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001922 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001923 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001924 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001925 }
rginda8ba33642011-12-14 12:31:31 -08001926 }
1927
rgindaa09e7332012-08-17 12:49:51 -07001928 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001929};
1930
1931/**
1932 * Return the text content for a given row.
1933 *
1934 * This is a method from the RowProvider interface. The ScrollPort uses
1935 * it to fetch text content on demand when the user attempts to copy their
1936 * selection to the clipboard.
1937 *
Joel Hockey0f933582019-08-27 18:01:51 -07001938 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001939 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001940 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001941 * @return {string} A string containing the text value of the selected row.
1942 */
1943hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001944 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001945 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001946};
1947
1948/**
1949 * Return the total number of rows in the addressable screen and in the
1950 * scrollback buffer of this terminal.
1951 *
1952 * This is a method from the RowProvider interface. The ScrollPort uses
1953 * it to compute the size of the scrollbar.
1954 *
Joel Hockey0f933582019-08-27 18:01:51 -07001955 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001956 * @override
rginda8ba33642011-12-14 12:31:31 -08001957 */
1958hterm.Terminal.prototype.getRowCount = function() {
1959 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1960};
1961
1962/**
1963 * Create DOM nodes for new rows and append them to the end of the terminal.
1964 *
1965 * This is the only correct way to add a new DOM node for a row. Notice that
1966 * the new row is appended to the bottom of the list of rows, and does not
1967 * require renumbering (of the rowIndex property) of previous rows.
1968 *
1969 * If you think you want a new blank row somewhere in the middle of the
1970 * terminal, look into moveRows_().
1971 *
1972 * This method does not pay attention to vtScrollTop/Bottom, since you should
1973 * be using moveRows() in cases where they would matter.
1974 *
1975 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001976 *
1977 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001978 */
1979hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001980 let cursorRow = this.screen_.rowsArray.length;
1981 const offset = this.scrollbackRows_.length + cursorRow;
1982 for (let i = 0; i < count; i++) {
1983 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08001984 row.appendChild(this.document_.createTextNode(''));
1985 row.rowIndex = offset + i;
1986 this.screen_.pushRow(row);
1987 }
1988
Mike Frysingerdc727792020-04-10 01:41:13 -04001989 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001990 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001991 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08001992 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001993 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07001994 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04001995 }
rginda8ba33642011-12-14 12:31:31 -08001996 }
1997
Mike Frysingerbdb34802020-04-07 03:47:32 -04001998 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08001999 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002000 }
rginda8ba33642011-12-14 12:31:31 -08002001
rginda87b86462011-12-14 13:48:03 -08002002 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
2006 * Relocate rows from one part of the addressable screen to another.
2007 *
2008 * This is used to recycle rows during VT scrolls (those which are driven
2009 * by VT commands, rather than by the user manipulating the scrollbar.)
2010 *
2011 * In this case, the blank lines scrolled into the scroll region are made of
2012 * the nodes we scrolled off. These have their rowIndex properties carefully
2013 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002014 *
2015 * @param {number} fromIndex The start index.
2016 * @param {number} count The number of rows to move.
2017 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002018 */
2019hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002020 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002021 this.screen_.insertRows(toIndex, ary);
2022
Mike Frysingerdc727792020-04-10 01:41:13 -04002023 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002024 if (fromIndex < toIndex) {
2025 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002026 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002027 } else {
2028 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002029 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002030 }
2031
2032 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002033 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002034};
2035
2036/**
2037 * Renumber the rowIndex property of the given range of rows.
2038 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002039 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002040 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002041 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002042 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002043 *
2044 * @param {number} start The start index.
2045 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002046 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002047 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002048hterm.Terminal.prototype.renumberRows_ = function(
2049 start, end, screen = undefined) {
2050 if (!screen) {
2051 screen = this.screen_;
2052 }
Robert Ginda40932892012-12-10 17:26:40 -08002053
Mike Frysingerdc727792020-04-10 01:41:13 -04002054 const offset = this.scrollbackRows_.length;
2055 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002056 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002057 }
2058};
2059
2060/**
2061 * Print a string to the terminal.
2062 *
2063 * This respects the current insert and wraparound modes. It will add new lines
2064 * to the end of the terminal, scrolling off the top into the scrollback buffer
2065 * if necessary.
2066 *
2067 * The string is *not* parsed for escape codes. Use the interpret() method if
2068 * that's what you're after.
2069 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002070 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002071 */
2072hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002073 this.scheduleSyncCursorPosition_();
2074
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002075 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002076 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002077
Mike Frysingerdc727792020-04-10 01:41:13 -04002078 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002079
Mike Frysingerdc727792020-04-10 01:41:13 -04002080 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002081 // Fun edge case: If the string only contains zero width codepoints (like
2082 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002083 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002084 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002085 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002086
2087 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002088 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2089 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002090 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002091 }
rgindaa19afe22012-01-25 15:40:22 -08002092
Mike Frysingerdc727792020-04-10 01:41:13 -04002093 let count = strWidth - startOffset;
2094 let didOverflow = false;
2095 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002096
rgindaa9abdd82012-08-06 18:05:09 -07002097 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2098 didOverflow = true;
2099 count = this.screenSize.width - this.screen_.cursorPosition.column;
2100 }
rgindaa19afe22012-01-25 15:40:22 -08002101
rgindaa9abdd82012-08-06 18:05:09 -07002102 if (didOverflow && !this.options_.wraparound) {
2103 // If the string overflowed the line but wraparound is off, then the
2104 // last printed character should be the last of the string.
2105 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002106 substr = lib.wc.substr(str, startOffset, count - 1) +
2107 lib.wc.substr(str, strWidth - 1);
2108 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002109 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002110 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002111 }
rgindaa19afe22012-01-25 15:40:22 -08002112
Mike Frysingerdc727792020-04-10 01:41:13 -04002113 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2114 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002115 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2116 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002117
2118 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002119 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002120 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002121 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002122 }
2123 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002124 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002125 }
2126
2127 this.screen_.maybeClipCurrentRow();
2128 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002129 }
rginda8ba33642011-12-14 12:31:31 -08002130
Mike Frysingerbdb34802020-04-07 03:47:32 -04002131 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002132 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002133 }
rginda8ba33642011-12-14 12:31:31 -08002134};
2135
2136/**
rginda87b86462011-12-14 13:48:03 -08002137 * Set the VT scroll region.
2138 *
rginda87b86462011-12-14 13:48:03 -08002139 * This also resets the cursor position to the absolute (0, 0) position, since
2140 * that's what xterm appears to do.
2141 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002142 * Setting the scroll region to the full height of the terminal will clear
2143 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2144 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2145 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2146 * continue to work as most users would expect.
2147 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002148 * @param {?number} scrollTop The zero-based top of the scroll region.
2149 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002150 * inclusive.
2151 */
2152hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002153 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002154 this.vtScrollTop_ = null;
2155 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002156 } else {
2157 this.vtScrollTop_ = scrollTop;
2158 this.vtScrollBottom_ = scrollBottom;
2159 }
rginda87b86462011-12-14 13:48:03 -08002160};
2161
2162/**
rginda8ba33642011-12-14 12:31:31 -08002163 * Return the top row index according to the VT.
2164 *
2165 * This will return 0 unless the terminal has been told to restrict scrolling
2166 * to some lower row. It is used for some VT cursor positioning and scrolling
2167 * commands.
2168 *
Joel Hockey0f933582019-08-27 18:01:51 -07002169 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002170 */
2171hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002172 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002173 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002174 }
rginda8ba33642011-12-14 12:31:31 -08002175
2176 return 0;
rginda87b86462011-12-14 13:48:03 -08002177};
rginda8ba33642011-12-14 12:31:31 -08002178
2179/**
2180 * Return the bottom row index according to the VT.
2181 *
2182 * This will return the height of the terminal unless the it has been told to
2183 * restrict scrolling to some higher row. It is used for some VT cursor
2184 * positioning and scrolling commands.
2185 *
Joel Hockey0f933582019-08-27 18:01:51 -07002186 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002187 */
2188hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002189 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002190 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002191 }
rginda8ba33642011-12-14 12:31:31 -08002192
rginda87b86462011-12-14 13:48:03 -08002193 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002194};
rginda8ba33642011-12-14 12:31:31 -08002195
2196/**
2197 * Process a '\n' character.
2198 *
2199 * If the cursor is on the final row of the terminal this will append a new
2200 * blank row to the screen and scroll the topmost row into the scrollback
2201 * buffer.
2202 *
2203 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002204 *
2205 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2206 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002207 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002208hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002209 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002210 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002211 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002212
Mike Frysingerdc727792020-04-10 01:41:13 -04002213 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2214 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002215
2216 if (this.vtScrollBottom_ != null) {
2217 // A VT Scroll region is active, we never append new rows.
2218 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2219 // We're at the end of the VT Scroll Region, perform a VT scroll.
2220 this.vtScrollUp(1);
2221 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2222 } else if (cursorAtEndOfScreen) {
2223 // We're at the end of the screen, the only thing to do is put the
2224 // cursor to column 0.
2225 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2226 } else {
2227 // Anywhere else, advance the cursor row, and reset the column.
2228 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2229 }
2230 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002231 // We're at the end of the screen. Append a new row to the terminal,
2232 // shifting the top row into the scrollback.
2233 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002234 } else {
rginda87b86462011-12-14 13:48:03 -08002235 // Anywhere else in the screen just moves the cursor.
2236 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002237 }
2238};
2239
2240/**
2241 * Like newLine(), except maintain the cursor column.
2242 */
2243hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002244 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002245 this.newLine();
2246 this.setCursorColumn(column);
2247};
2248
2249/**
rginda87b86462011-12-14 13:48:03 -08002250 * If autoCarriageReturn is set then newLine(), else lineFeed().
2251 */
2252hterm.Terminal.prototype.formFeed = function() {
2253 if (this.options_.autoCarriageReturn) {
2254 this.newLine();
2255 } else {
2256 this.lineFeed();
2257 }
2258};
2259
2260/**
2261 * Move the cursor up one row, possibly inserting a blank line.
2262 *
2263 * The cursor column is not changed.
2264 */
2265hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002266 const scrollTop = this.getVTScrollTop();
2267 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002268
2269 if (currentRow == scrollTop) {
2270 this.insertLines(1);
2271 } else {
2272 this.setAbsoluteCursorRow(currentRow - 1);
2273 }
2274};
2275
2276/**
rginda8ba33642011-12-14 12:31:31 -08002277 * Replace all characters to the left of the current cursor with the space
2278 * character.
2279 *
2280 * TODO(rginda): This should probably *remove* the characters (not just replace
2281 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002282 * position.
rginda8ba33642011-12-14 12:31:31 -08002283 */
2284hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002285 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002286 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002287 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002288 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002289 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002290};
2291
2292/**
David Benjamin684a9b72012-05-01 17:19:58 -04002293 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002294 *
2295 * The cursor position is unchanged.
2296 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002297 * If the current background color is not the default background color this
2298 * will insert spaces rather than delete. This is unfortunate because the
2299 * trailing space will affect text selection, but it's difficult to come up
2300 * with a way to style empty space that wouldn't trip up the hterm.Screen
2301 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002302 *
2303 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2304 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2305 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002306 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002307 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002308 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002309hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002310 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002311 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002312 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002313
Mike Frysingerdc727792020-04-10 01:41:13 -04002314 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002315 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002316
2317 if (this.screen_.textAttributes.background ===
2318 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002319 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002320 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002321 this.screen_.cursorPosition.column + count) {
2322 this.screen_.deleteChars(count);
2323 this.clearCursorOverflow();
2324 return;
2325 }
2326 }
2327
Mike Frysingerdc727792020-04-10 01:41:13 -04002328 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002329 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002330 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002331 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002332};
2333
2334/**
2335 * Erase the current line.
2336 *
2337 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002338 */
2339hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002340 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002341 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002342 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002343 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002344};
2345
2346/**
David Benjamina08d78f2012-05-05 00:28:49 -04002347 * Erase all characters from the start of the screen to the current cursor
2348 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002349 *
2350 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002351 */
2352hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002353 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002354
2355 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002356
Mike Frysingerdc727792020-04-10 01:41:13 -04002357 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002358 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002359 this.screen_.clearCursorRow();
2360 }
2361
rginda87b86462011-12-14 13:48:03 -08002362 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002363 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
2367 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002368 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002369 *
2370 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002371 */
2372hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002373 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002374
2375 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002376
Mike Frysingerdc727792020-04-10 01:41:13 -04002377 const bottom = this.screenSize.height - 1;
2378 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002379 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002380 this.screen_.clearCursorRow();
2381 }
2382
rginda87b86462011-12-14 13:48:03 -08002383 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002384 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002385};
2386
2387/**
2388 * Fill the terminal with a given character.
2389 *
2390 * This methods does not respect the VT scroll region.
2391 *
2392 * @param {string} ch The character to use for the fill.
2393 */
2394hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002395 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002396
2397 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002398 for (let row = 0; row < this.screenSize.height; row++) {
2399 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002400 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002401 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002402 }
2403 }
2404
2405 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002406};
2407
2408/**
rginda9ea433c2012-03-16 11:57:00 -07002409 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002410 *
rginda9ea433c2012-03-16 11:57:00 -07002411 * This does not respect the scroll region.
2412 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002413 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002414 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002415 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002416hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2417 if (!screen) {
2418 screen = this.screen_;
2419 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002420 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002421
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002422 this.accessibilityReader_.clear();
2423
rginda11057d52012-04-25 12:29:56 -07002424 if (bottom == 0) {
2425 // Empty screen, nothing to do.
2426 return;
2427 }
2428
Mike Frysingerdc727792020-04-10 01:41:13 -04002429 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002430 screen.setCursorPosition(i, 0);
2431 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002432 }
2433
rginda9ea433c2012-03-16 11:57:00 -07002434 screen.setCursorPosition(0, 0);
2435};
2436
2437/**
2438 * Erase the entire display without changing the cursor position.
2439 *
2440 * The cursor position is unchanged. This does not respect the scroll
2441 * region.
2442 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002443 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002444 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002445 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002446hterm.Terminal.prototype.clear = function(screen = undefined) {
2447 if (!screen) {
2448 screen = this.screen_;
2449 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002450 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002451 this.clearHome(screen);
2452 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002453};
2454
2455/**
2456 * VT command to insert lines at the current cursor row.
2457 *
2458 * This respects the current scroll region. Rows pushed off the bottom are
2459 * lost (they won't show up in the scrollback buffer).
2460 *
Joel Hockey0f933582019-08-27 18:01:51 -07002461 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002462 */
2463hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002464 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002465
Mike Frysingerdc727792020-04-10 01:41:13 -04002466 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002467 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002468
Robert Ginda579186b2012-09-26 11:40:04 -07002469 // The moveCount is the number of rows we need to relocate to make room for
2470 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002471 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002472 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002473 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002474 }
rginda8ba33642011-12-14 12:31:31 -08002475
Mike Frysingerdc727792020-04-10 01:41:13 -04002476 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002477 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002478 this.screen_.clearCursorRow();
2479 }
rginda8ba33642011-12-14 12:31:31 -08002480};
2481
2482/**
2483 * VT command to delete lines at the current cursor row.
2484 *
2485 * New rows are added to the bottom of scroll region to take their place. New
2486 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002487 *
2488 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002489 */
2490hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002491 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002492
Mike Frysingerdc727792020-04-10 01:41:13 -04002493 const top = cursor.row;
2494 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002495
Mike Frysingerdc727792020-04-10 01:41:13 -04002496 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002497 count = Math.min(count, maxCount);
2498
Mike Frysingerdc727792020-04-10 01:41:13 -04002499 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002500 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002501 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002502 }
rginda8ba33642011-12-14 12:31:31 -08002503
Mike Frysingerdc727792020-04-10 01:41:13 -04002504 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002505 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002506 this.screen_.clearCursorRow();
2507 }
2508
rginda87b86462011-12-14 13:48:03 -08002509 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002510 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002511};
2512
2513/**
2514 * Inserts the given number of spaces at the current cursor position.
2515 *
rginda87b86462011-12-14 13:48:03 -08002516 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002517 *
2518 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002519 */
2520hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002521 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002522
Mike Frysinger73e56462019-07-17 00:23:46 -05002523 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002524 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002525 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002526
2527 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002528 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002529};
2530
2531/**
2532 * Forward-delete the specified number of characters starting at the cursor
2533 * position.
2534 *
Joel Hockey0f933582019-08-27 18:01:51 -07002535 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002536 */
2537hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002538 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002539 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002540 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002541 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002542 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002543 this.restoreCursor(cursor);
2544 }
2545
David Benjamin54e8bf62012-06-01 22:31:40 -04002546 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002547};
2548
2549/**
2550 * Shift rows in the scroll region upwards by a given number of lines.
2551 *
2552 * New rows are inserted at the bottom of the scroll region to fill the
2553 * vacated rows. The new rows not filled out with the current text attributes.
2554 *
2555 * This function does not affect the scrollback rows at all. Rows shifted
2556 * off the top are lost.
2557 *
rginda87b86462011-12-14 13:48:03 -08002558 * The cursor position is not altered.
2559 *
Joel Hockey0f933582019-08-27 18:01:51 -07002560 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002561 */
2562hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002563 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002564
rginda87b86462011-12-14 13:48:03 -08002565 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002566 this.deleteLines(count);
2567
rginda87b86462011-12-14 13:48:03 -08002568 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002569};
2570
2571/**
2572 * Shift rows below the cursor down by a given number of lines.
2573 *
2574 * This function respects the current scroll region.
2575 *
2576 * New rows are inserted at the top of the scroll region to fill the
2577 * vacated rows. The new rows not filled out with the current text attributes.
2578 *
2579 * This function does not affect the scrollback rows at all. Rows shifted
2580 * off the bottom are lost.
2581 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002582 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002583 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002584hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002585 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002586
rginda87b86462011-12-14 13:48:03 -08002587 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002588 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002589
rginda87b86462011-12-14 13:48:03 -08002590 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002591};
2592
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002593/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002594 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002595 *
2596 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002597 * cause Assitive Technology to announce the output of the terminal. It also
2598 * enables other features that aid assistive technology. All the features gated
2599 * behind this flag have a performance impact on the terminal which is why they
2600 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002601 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002602 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002603 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002604hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002605 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002606};
rginda87b86462011-12-14 13:48:03 -08002607
rginda8ba33642011-12-14 12:31:31 -08002608/**
2609 * Set the cursor position.
2610 *
2611 * The cursor row is relative to the scroll region if the terminal has
2612 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2613 *
Joel Hockey0f933582019-08-27 18:01:51 -07002614 * @param {number} row The new zero-based cursor row.
2615 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002616 */
2617hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2618 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002619 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002620 } else {
rginda87b86462011-12-14 13:48:03 -08002621 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002622 }
rginda87b86462011-12-14 13:48:03 -08002623};
rginda8ba33642011-12-14 12:31:31 -08002624
Evan Jones2600d4f2016-12-06 09:29:36 -05002625/**
2626 * Move the cursor relative to its current position.
2627 *
2628 * @param {number} row
2629 * @param {number} column
2630 */
rginda87b86462011-12-14 13:48:03 -08002631hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002632 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002633 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2634 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002635 this.screen_.setCursorPosition(row, column);
2636};
2637
Evan Jones2600d4f2016-12-06 09:29:36 -05002638/**
2639 * Move the cursor to the specified position.
2640 *
2641 * @param {number} row
2642 * @param {number} column
2643 */
rginda87b86462011-12-14 13:48:03 -08002644hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002645 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2646 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002647 this.screen_.setCursorPosition(row, column);
2648};
2649
2650/**
2651 * Set the cursor column.
2652 *
Joel Hockey0f933582019-08-27 18:01:51 -07002653 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002654 */
2655hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002656 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002657};
2658
2659/**
2660 * Return the cursor column.
2661 *
Joel Hockey0f933582019-08-27 18:01:51 -07002662 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002663 */
2664hterm.Terminal.prototype.getCursorColumn = function() {
2665 return this.screen_.cursorPosition.column;
2666};
2667
2668/**
2669 * Set the cursor row.
2670 *
2671 * The cursor row is relative to the scroll region if the terminal has
2672 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2673 *
Joel Hockey0f933582019-08-27 18:01:51 -07002674 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002675 */
rginda87b86462011-12-14 13:48:03 -08002676hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2677 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002678};
2679
2680/**
2681 * Return the cursor row.
2682 *
Joel Hockey0f933582019-08-27 18:01:51 -07002683 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002684 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002685hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002686 return this.screen_.cursorPosition.row;
2687};
2688
2689/**
2690 * Request that the ScrollPort redraw itself soon.
2691 *
2692 * The redraw will happen asynchronously, soon after the call stack winds down.
2693 * Multiple calls will be coalesced into a single redraw.
2694 */
2695hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002696 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002697 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002698 }
rginda8ba33642011-12-14 12:31:31 -08002699
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002700 this.timeouts_.redraw = setTimeout(() => {
2701 delete this.timeouts_.redraw;
2702 this.scrollPort_.redraw_();
2703 });
rginda8ba33642011-12-14 12:31:31 -08002704};
2705
2706/**
2707 * Request that the ScrollPort be scrolled to the bottom.
2708 *
2709 * The scroll will happen asynchronously, soon after the call stack winds down.
2710 * Multiple calls will be coalesced into a single scroll.
2711 *
2712 * This affects the scrollbar position of the ScrollPort, and has nothing to
2713 * do with the VT scroll commands.
2714 */
2715hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002716 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002717 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002718 }
rginda8ba33642011-12-14 12:31:31 -08002719
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002720 this.timeouts_.scrollDown = setTimeout(() => {
2721 delete this.timeouts_.scrollDown;
2722 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2723 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002724};
2725
2726/**
2727 * Move the cursor up a specified number of rows.
2728 *
Joel Hockey0f933582019-08-27 18:01:51 -07002729 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002730 */
2731hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002732 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002733};
2734
2735/**
2736 * Move the cursor down a specified number of rows.
2737 *
Joel Hockey0f933582019-08-27 18:01:51 -07002738 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002739 */
2740hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002741 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002742 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2743 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2744 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002745
Mike Frysingerdc727792020-04-10 01:41:13 -04002746 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2747 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002748 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002749};
2750
2751/**
2752 * Move the cursor left a specified number of columns.
2753 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002754 * If reverse wraparound mode is enabled and the previous row wrapped into
2755 * the current row then we back up through the wraparound as well.
2756 *
Joel Hockey0f933582019-08-27 18:01:51 -07002757 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002758 */
2759hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002760 count = count || 1;
2761
Mike Frysingerbdb34802020-04-07 03:47:32 -04002762 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002763 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002764 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002765
Mike Frysingerdc727792020-04-10 01:41:13 -04002766 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002767 if (this.options_.reverseWraparound) {
2768 if (this.screen_.cursorPosition.overflow) {
2769 // If this cursor is in the right margin, consume one count to get it
2770 // back to the last column. This only applies when we're in reverse
2771 // wraparound mode.
2772 count--;
2773 this.clearCursorOverflow();
2774
Mike Frysingerbdb34802020-04-07 03:47:32 -04002775 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002776 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002777 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002778 }
2779
Mike Frysingerdc727792020-04-10 01:41:13 -04002780 let newRow = this.screen_.cursorPosition.row;
2781 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002782 if (newColumn < 0) {
2783 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2784 if (newRow < 0) {
2785 // xterm also wraps from row 0 to the last row.
2786 newRow = this.screenSize.height + newRow % this.screenSize.height;
2787 }
2788 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2789 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002790
Robert Gindabfb32622014-07-17 13:20:27 -07002791 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2792
2793 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002794 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002795 this.setCursorColumn(newColumn);
2796 }
rginda8ba33642011-12-14 12:31:31 -08002797};
2798
2799/**
2800 * Move the cursor right a specified number of columns.
2801 *
Joel Hockey0f933582019-08-27 18:01:51 -07002802 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002803 */
2804hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002805 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002806
Mike Frysingerbdb34802020-04-07 03:47:32 -04002807 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002808 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002809 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002810
Mike Frysingerdc727792020-04-10 01:41:13 -04002811 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2812 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002813 this.setCursorColumn(column);
2814};
2815
2816/**
2817 * Reverse the foreground and background colors of the terminal.
2818 *
2819 * This only affects text that was drawn with no attributes.
2820 *
2821 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2822 * been drawn with attributes that happen to coincide with the default
2823 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002824 *
2825 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002826 */
2827hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002828 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002829 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002830 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2831 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002832 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002833 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2834 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002835 }
2836};
2837
2838/**
rginda87b86462011-12-14 13:48:03 -08002839 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002840 *
2841 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002842 */
2843hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002844 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002845
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002846 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002847
Michael Kelly485ecd12014-06-09 11:41:56 -04002848 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002849 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002850 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002851 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002852
Robert Ginda92e18102013-03-14 13:56:37 -07002853 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002854 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002855 this.bellSequelchTimeout_ = setTimeout(() => {
2856 this.bellSquelchTimeout_ = null;
2857 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002858 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002859 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002860 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002861
2862 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002863 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002864 this.bellNotificationList_.push(n);
2865 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002866 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002867 }
rginda87b86462011-12-14 13:48:03 -08002868};
2869
2870/**
rginda8ba33642011-12-14 12:31:31 -08002871 * Set the origin mode bit.
2872 *
2873 * If origin mode is on, certain VT cursor and scrolling commands measure their
2874 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2875 * to the top of the addressable screen.
2876 *
2877 * Defaults to off.
2878 *
2879 * @param {boolean} state True to set origin mode, false to unset.
2880 */
2881hterm.Terminal.prototype.setOriginMode = function(state) {
2882 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002883 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002884};
2885
2886/**
2887 * Set the insert mode bit.
2888 *
2889 * If insert mode is on, existing text beyond the cursor position will be
2890 * shifted right to make room for new text. Otherwise, new text overwrites
2891 * any existing text.
2892 *
2893 * Defaults to off.
2894 *
2895 * @param {boolean} state True to set insert mode, false to unset.
2896 */
2897hterm.Terminal.prototype.setInsertMode = function(state) {
2898 this.options_.insertMode = state;
2899};
2900
2901/**
rginda87b86462011-12-14 13:48:03 -08002902 * Set the auto carriage return bit.
2903 *
2904 * If auto carriage return is on then a formfeed character is interpreted
2905 * as a newline, otherwise it's the same as a linefeed. The difference boils
2906 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002907 *
2908 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002909 */
2910hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2911 this.options_.autoCarriageReturn = state;
2912};
2913
2914/**
rginda8ba33642011-12-14 12:31:31 -08002915 * Set the wraparound mode bit.
2916 *
2917 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2918 * to the start of the following row. Otherwise, the cursor is clamped to the
2919 * end of the screen and attempts to write past it are ignored.
2920 *
2921 * Defaults to on.
2922 *
2923 * @param {boolean} state True to set wraparound mode, false to unset.
2924 */
2925hterm.Terminal.prototype.setWraparound = function(state) {
2926 this.options_.wraparound = state;
2927};
2928
2929/**
2930 * Set the reverse-wraparound mode bit.
2931 *
2932 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2933 * to the end of the previous row. Otherwise, the cursor is clamped to column
2934 * 0.
2935 *
2936 * Defaults to off.
2937 *
2938 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2939 */
2940hterm.Terminal.prototype.setReverseWraparound = function(state) {
2941 this.options_.reverseWraparound = state;
2942};
2943
2944/**
2945 * Selects between the primary and alternate screens.
2946 *
2947 * If alternate mode is on, the alternate screen is active. Otherwise the
2948 * primary screen is active.
2949 *
2950 * Swapping screens has no effect on the scrollback buffer.
2951 *
2952 * Each screen maintains its own cursor position.
2953 *
2954 * Defaults to off.
2955 *
2956 * @param {boolean} state True to set alternate mode, false to unset.
2957 */
2958hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002959 if (state == (this.screen_ == this.alternateScreen_)) {
2960 return;
2961 }
2962 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2963 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002964 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2965
Joel Hockey42dba8f2020-03-26 16:21:11 -07002966 // Swap color overrides.
2967 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2968 oldOverrides.forEach((c, i) => {
2969 if (!newOverrides.hasOwnProperty(i)) {
2970 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2971 }
2972 });
2973 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2974
rginda35c456b2012-02-09 17:29:05 -08002975 if (this.screen_.rowsArray.length &&
2976 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2977 // If the screen changed sizes while we were away, our rowIndexes may
2978 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002979 const offset = this.scrollbackRows_.length;
2980 const ary = this.screen_.rowsArray;
2981 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002982 ary[i].rowIndex = offset + i;
2983 }
2984 }
rginda8ba33642011-12-14 12:31:31 -08002985
rginda35c456b2012-02-09 17:29:05 -08002986 this.realizeWidth_(this.screenSize.width);
2987 this.realizeHeight_(this.screenSize.height);
2988 this.scrollPort_.syncScrollHeight();
2989 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002990
rginda6d397402012-01-17 10:58:29 -08002991 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002992 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002993};
2994
2995/**
2996 * Set the cursor-blink mode bit.
2997 *
2998 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2999 * a visible cursor does not blink.
3000 *
3001 * You should make sure to turn blinking off if you're going to dispose of a
3002 * terminal, otherwise you'll leak a timeout.
3003 *
3004 * Defaults to on.
3005 *
3006 * @param {boolean} state True to set cursor-blink mode, false to unset.
3007 */
3008hterm.Terminal.prototype.setCursorBlink = function(state) {
3009 this.options_.cursorBlink = state;
3010
3011 if (!state && this.timeouts_.cursorBlink) {
3012 clearTimeout(this.timeouts_.cursorBlink);
3013 delete this.timeouts_.cursorBlink;
3014 }
3015
Mike Frysingerbdb34802020-04-07 03:47:32 -04003016 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003017 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003018 }
rginda8ba33642011-12-14 12:31:31 -08003019};
3020
3021/**
3022 * Set the cursor-visible mode bit.
3023 *
3024 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3025 *
3026 * Defaults to on.
3027 *
3028 * @param {boolean} state True to set cursor-visible mode, false to unset.
3029 */
3030hterm.Terminal.prototype.setCursorVisible = function(state) {
3031 this.options_.cursorVisible = state;
3032
3033 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003034 if (this.timeouts_.cursorBlink) {
3035 clearTimeout(this.timeouts_.cursorBlink);
3036 delete this.timeouts_.cursorBlink;
3037 }
rginda87b86462011-12-14 13:48:03 -08003038 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003039 return;
3040 }
3041
rginda87b86462011-12-14 13:48:03 -08003042 this.syncCursorPosition_();
3043
3044 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003045
3046 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003047 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003048 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003049 }
rginda8ba33642011-12-14 12:31:31 -08003050
Robert Gindaea2183e2014-07-17 09:51:51 -07003051 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003052 } else {
3053 if (this.timeouts_.cursorBlink) {
3054 clearTimeout(this.timeouts_.cursorBlink);
3055 delete this.timeouts_.cursorBlink;
3056 }
3057 }
3058};
3059
3060/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003061 * Pause blinking temporarily.
3062 *
3063 * When the cursor moves around, it can be helpful to momentarily pause the
3064 * blinking. This could be when the user is typing in things, or when they're
3065 * moving around with the arrow keys.
3066 */
3067hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3068 if (!this.options_.cursorBlink) {
3069 return;
3070 }
3071
3072 this.cursorBlinkPause_ = true;
3073
3074 // If a timeout is already pending, reset the clock due to the new input.
3075 if (this.timeouts_.cursorBlinkPause) {
3076 clearTimeout(this.timeouts_.cursorBlinkPause);
3077 }
3078 // After 500ms, resume blinking. That seems like a good balance between user
3079 // input timings & responsiveness to resume.
3080 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3081 delete this.timeouts_.cursorBlinkPause;
3082 this.cursorBlinkPause_ = false;
3083 }, 500);
3084};
3085
3086/**
rginda87b86462011-12-14 13:48:03 -08003087 * Synchronizes the visible cursor and document selection with the current
3088 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003089 *
3090 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003091 */
3092hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003093 const topRowIndex = this.scrollPort_.getTopRowIndex();
3094 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3095 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003096 this.screen_.cursorPosition.row;
3097
Raymes Khoury15697f42018-07-17 11:37:18 +10003098 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003099 if (this.accessibilityReader_.accessibilityEnabled) {
3100 // Report the new position of the cursor for accessibility purposes.
3101 const cursorColumnIndex = this.screen_.cursorPosition.column;
3102 const cursorLineText =
3103 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003104 // This will force the selection to be sync'd to the cursor position if the
3105 // user has pressed a key. Generally we would only sync the cursor position
3106 // when selection is collapsed so that if the user has selected something
3107 // we don't clear the selection by moving the selection. However when a
3108 // screen reader is used, it's intuitive for entering a key to move the
3109 // selection to the cursor.
3110 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003111 this.accessibilityReader_.afterCursorChange(
3112 cursorLineText, cursorRowIndex, cursorColumnIndex);
3113 }
3114
rginda8ba33642011-12-14 12:31:31 -08003115 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003116 // Cursor is scrolled off screen, hide it.
3117 this.cursorOffScreen_ = true;
3118 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003119 return false;
rginda8ba33642011-12-14 12:31:31 -08003120 }
3121
Joel Hockey3babf302020-04-22 15:00:06 -07003122 if (this.cursorNode_.style.display == 'none') {
3123 // Re-display the terminal cursor if it was hidden.
3124 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003125 this.cursorNode_.style.display = '';
3126 }
3127
Mike Frysinger44c32202017-08-05 01:13:09 -04003128 // Position the cursor using CSS variable math. If we do the math in JS,
3129 // the float math will end up being more precise than the CSS which will
3130 // cause the cursor tracking to be off.
3131 this.setCssVar(
3132 'cursor-offset-row',
3133 `${cursorRowIndex - topRowIndex} + ` +
3134 `${this.scrollPort_.visibleRowTopMargin}px`);
3135 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003136
3137 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003138 '(' + this.screen_.cursorPosition.column +
3139 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003140 ')');
3141
3142 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003143 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003144 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003145 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003146 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003147 return true;
rginda8ba33642011-12-14 12:31:31 -08003148};
3149
Robert Gindafb1be6a2013-12-11 11:56:22 -08003150/**
3151 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3152 * and character cell dimensions.
3153 */
Robert Ginda830583c2013-08-07 13:20:46 -07003154hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003155 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003156
3157 if (this.cursorNode_.getAttribute('focus') == 'false') {
3158 // Always show a block cursor when unfocused.
3159 shape = hterm.Terminal.cursorShape.BLOCK;
3160 }
3161
Mike Frysingerdc727792020-04-10 01:41:13 -04003162 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003163
3164 switch (shape) {
3165 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003166 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003167 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003168 style.borderLeftStyle = 'solid';
3169 break;
3170
3171 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003172 style.backgroundColor = 'transparent';
3173 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003174 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003175 break;
3176
3177 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003178 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003179 style.borderBottomStyle = '';
3180 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003181 break;
3182 }
3183};
3184
rginda8ba33642011-12-14 12:31:31 -08003185/**
3186 * Synchronizes the visible cursor with the current cursor coordinates.
3187 *
3188 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003189 * Multiple calls will be coalesced into a single sync. This should be called
3190 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003191 */
3192hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003193 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003194 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003195 }
rginda8ba33642011-12-14 12:31:31 -08003196
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003197 if (this.accessibilityReader_.accessibilityEnabled) {
3198 // Report the previous position of the cursor for accessibility purposes.
3199 const cursorRowIndex = this.scrollbackRows_.length +
3200 this.screen_.cursorPosition.row;
3201 const cursorColumnIndex = this.screen_.cursorPosition.column;
3202 const cursorLineText =
3203 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3204 this.accessibilityReader_.beforeCursorChange(
3205 cursorLineText, cursorRowIndex, cursorColumnIndex);
3206 }
3207
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003208 this.timeouts_.syncCursor = setTimeout(() => {
3209 this.syncCursorPosition_();
3210 delete this.timeouts_.syncCursor;
3211 });
rginda87b86462011-12-14 13:48:03 -08003212};
3213
rgindacc2996c2012-02-24 14:59:31 -08003214/**
rgindaf522ce02012-04-17 17:49:17 -07003215 * Show or hide the zoom warning.
3216 *
3217 * The zoom warning is a message warning the user that their browser zoom must
3218 * be set to 100% in order for hterm to function properly.
3219 *
3220 * @param {boolean} state True to show the message, false to hide it.
3221 */
3222hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3223 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003224 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003225 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003226 }
rgindaf522ce02012-04-17 17:49:17 -07003227
3228 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003229 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003230 this.zoomWarningNode_.style.cssText = (
3231 'color: black;' +
3232 'background-color: #ff2222;' +
3233 'font-size: large;' +
3234 'border-radius: 8px;' +
3235 'opacity: 0.75;' +
3236 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3237 'top: 0.5em;' +
3238 'right: 1.2em;' +
3239 'position: absolute;' +
3240 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003241 '-webkit-user-select: none;' +
3242 '-moz-text-size-adjust: none;' +
3243 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003244
3245 this.zoomWarningNode_.addEventListener('click', function(e) {
3246 this.parentNode.removeChild(this);
3247 });
rgindaf522ce02012-04-17 17:49:17 -07003248 }
3249
Mike Frysingerb7289952019-03-23 16:05:38 -07003250 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003251 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003252 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003253
rgindaf522ce02012-04-17 17:49:17 -07003254 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3255
3256 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003257 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003258 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003259 }
rgindaf522ce02012-04-17 17:49:17 -07003260 } else if (this.zoomWarningNode_.parentNode) {
3261 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3262 }
3263};
3264
3265/**
rgindacc2996c2012-02-24 14:59:31 -08003266 * Show the terminal overlay for a given amount of time.
3267 *
Jason Lin3d825782020-05-12 11:02:48 +10003268 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003269 *
3270 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003271 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003272 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3273 * stay up forever (or until the next overlay).
3274 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003275hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003276 this.showOverlayWithNode(new Text(msg), timeout);
3277};
3278
3279/**
3280 * Show the terminal overlay for a given amount of time.
3281 *
3282 * The terminal overlay appears in inverse video, centered over the terminal.
3283 *
3284 * @param {!Node} node The node to display in the overlay.
3285 * @param {?number=} timeout The amount of time to wait before fading out
3286 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3287 * stay up forever (or until the next overlay).
3288 */
3289hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
rgindaf0090c92012-02-10 14:58:52 -08003290 if (!this.overlayNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003291 if (!this.div_) {
rgindaf0090c92012-02-10 14:58:52 -08003292 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003293 }
rgindaf0090c92012-02-10 14:58:52 -08003294
3295 this.overlayNode_ = this.document_.createElement('div');
3296 this.overlayNode_.style.cssText = (
Mike Frysingeraee93052017-07-31 23:44:18 -04003297 'color: var(--hterm-background-color);' +
3298 'background-color: var(--hterm-foreground-color);' +
Jason Lin3d825782020-05-12 11:02:48 +10003299 'border-radius: 12px;' +
3300 'font: 500 1em "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003301 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003302 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003303 'position: absolute;' +
3304 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003305 '-webkit-transition: opacity 180ms ease-in;' +
3306 '-moz-user-select: none;' +
3307 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003308
3309 this.overlayNode_.addEventListener('mousedown', function(e) {
3310 e.preventDefault();
3311 e.stopPropagation();
3312 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003313 }
3314
Jason Lin3d825782020-05-12 11:02:48 +10003315 this.overlayNode_.style.fontSize = this.prefs_.get('font-size');
rginda9f5222b2012-03-05 11:53:28 -08003316
Jason Lin34567412020-05-14 10:32:09 +10003317 this.overlayNode_.textContent = ''; // Remove all children first.
3318 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003319 this.overlayNode_.style.opacity = '0.75';
3320
Mike Frysingerbdb34802020-04-07 03:47:32 -04003321 if (!this.overlayNode_.parentNode) {
rgindaf0090c92012-02-10 14:58:52 -08003322 this.div_.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003323 }
rgindaf0090c92012-02-10 14:58:52 -08003324
Mike Frysingerdc727792020-04-10 01:41:13 -04003325 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3326 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003327
Robert Ginda8a59f762014-07-23 11:29:55 -07003328 this.overlayNode_.style.top =
3329 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003330 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003331 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003332
Mike Frysingerbdb34802020-04-07 03:47:32 -04003333 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003334 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003335 }
rgindaf0090c92012-02-10 14:58:52 -08003336
Jason Lin34567412020-05-14 10:32:09 +10003337 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003338
Mike Frysingerec4225d2020-04-07 05:00:01 -04003339 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003340 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003341 }
rgindacc2996c2012-02-24 14:59:31 -08003342
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003343 this.overlayTimeout_ = setTimeout(() => {
3344 this.overlayNode_.style.opacity = '0';
3345 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003346 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003347};
3348
3349/**
3350 * Hide the terminal overlay immediately.
3351 *
3352 * Useful when we show an overlay for an event with an unknown end time.
3353 */
3354hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003355 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003356 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003357 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003358 this.overlayTimeout_ = null;
3359
Mike Frysingerbdb34802020-04-07 03:47:32 -04003360 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003361 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003362 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003363 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003364};
3365
rginda4bba5e12012-06-20 16:15:30 -07003366/**
3367 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003368 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003369 * Note: In Chrome, this should work unless the user has rejected the permission
3370 * request. In Firefox extension environment, you'll need the "clipboardRead"
3371 * permission. In other environments, this might always fail as the browser
3372 * frequently blocks access for security reasons.
3373 *
3374 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3375 * value is always null. Otherwise, this function uses legacy pasting and
3376 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003377 */
3378hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003379 if (!this.alwaysUseLegacyPasting &&
3380 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003381 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3382 return null;
3383 } else {
3384 // Legacy pasting.
3385 try {
3386 return this.document_.execCommand('paste');
3387 } catch (firefoxException) {
3388 // Ignore this. FF 40 and older would incorrectly throw an exception if
3389 // there was an error instead of returning false.
3390 return false;
3391 }
3392 }
rginda4bba5e12012-06-20 16:15:30 -07003393};
3394
3395/**
3396 * Copy a string to the system clipboard.
3397 *
3398 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003399 *
3400 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003401 */
3402hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003403 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003404 if (!this.clipboardNotice_) {
3405 this.clipboardNotice_ = this.document_.createElement('div');
3406 this.clipboardNotice_.style.textAlign = 'center';
3407 const copyImage = lib.resource.getData('hterm/images/copy');
3408 this.clipboardNotice_.innerHTML =
3409 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3410 }
3411 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003412 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003413
Mike Frysinger96eacae2019-01-02 18:13:56 -05003414 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003415};
3416
Evan Jones2600d4f2016-12-06 09:29:36 -05003417/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003418 * Display an image.
3419 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003420 * Either URI or buffer or blob fields must be specified.
3421 *
Joel Hockey0f933582019-08-27 18:01:51 -07003422 * @param {{
3423 * name: (string|undefined),
3424 * size: (string|number|undefined),
3425 * preserveAspectRation: (boolean|undefined),
3426 * inline: (boolean|undefined),
3427 * width: (string|number|undefined),
3428 * height: (string|number|undefined),
3429 * align: (string|undefined),
3430 * url: (string|undefined),
3431 * buffer: (!ArrayBuffer|undefined),
3432 * blob: (!Blob|undefined),
3433 * type: (string|undefined),
3434 * }} options The image to display.
3435 * name A human readable string for the image
3436 * size The size (in bytes).
3437 * preserveAspectRatio Whether to preserve aspect.
3438 * inline Whether to display the image inline.
3439 * width The width of the image.
3440 * height The height of the image.
3441 * align Direction to align the image.
3442 * uri The source URI for the image.
3443 * buffer The ArrayBuffer image data.
3444 * blob The Blob image data.
3445 * type The MIME type of the image data.
3446 * @param {function()=} onLoad Callback when loading finishes.
3447 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003448 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003449hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003450 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003451 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003452 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003453 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003454 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003455
3456 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003457 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003458 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003459 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003460
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003461 // See if the mime type is available. If not, guess from the filename.
3462 // We don't list all possible mime types because the browser can usually
3463 // guess it correctly. So list the ones that need a bit more help.
3464 if (!options.type) {
3465 const ary = options.name.split('.');
3466 const ext = ary[ary.length - 1].trim();
3467 switch (ext) {
3468 case 'svg':
3469 case 'svgz':
3470 options.type = 'image/svg+xml';
3471 break;
3472 }
3473 }
3474
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003475 // Has the user approved image display yet?
3476 if (this.allowImagesInline !== true) {
3477 this.newLine();
3478 const row = this.getRowNode(this.scrollbackRows_.length +
3479 this.getCursorRow() - 1);
3480
3481 if (this.allowImagesInline === false) {
3482 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3483 'Inline Images Disabled');
3484 return;
3485 }
3486
3487 // Show a prompt.
3488 let button;
3489 const span = this.document_.createElement('span');
3490 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3491 span.style.fontWeight = 'bold';
3492 span.style.borderWidth = '1px';
3493 span.style.borderStyle = 'dashed';
3494 button = this.document_.createElement('span');
3495 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3496 button.style.marginLeft = '1em';
3497 button.style.borderWidth = '1px';
3498 button.style.borderStyle = 'solid';
3499 button.addEventListener('click', () => {
3500 this.prefs_.set('allow-images-inline', false);
3501 });
3502 span.appendChild(button);
3503 button = this.document_.createElement('span');
3504 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3505 'allow this session');
3506 button.style.marginLeft = '1em';
3507 button.style.borderWidth = '1px';
3508 button.style.borderStyle = 'solid';
3509 button.addEventListener('click', () => {
3510 this.allowImagesInline = true;
3511 });
3512 span.appendChild(button);
3513 button = this.document_.createElement('span');
3514 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3515 button.style.marginLeft = '1em';
3516 button.style.borderWidth = '1px';
3517 button.style.borderStyle = 'solid';
3518 button.addEventListener('click', () => {
3519 this.prefs_.set('allow-images-inline', true);
3520 });
3521 span.appendChild(button);
3522
3523 row.appendChild(span);
3524 return;
3525 }
3526
3527 // See if we should show this object directly, or download it.
3528 if (options.inline) {
3529 const io = this.io.push();
3530 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003531 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003532
3533 // While we're loading the image, eat all the user's input.
3534 io.onVTKeystroke = io.sendString = () => {};
3535
3536 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003537 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003538 if (options.uri !== undefined) {
3539 img.src = options.uri;
3540 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003541 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003542 img.src = URL.createObjectURL(blob);
3543 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003544 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003545 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003546 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003547 img.title = img.alt = options.name;
3548
3549 // Attach the image to the page to let it load/render. It won't stay here.
3550 // This is needed so it's visible and the DOM can calculate the height. If
3551 // the image is hidden or not in the DOM, the height is always 0.
3552 this.document_.body.appendChild(img);
3553
3554 // Wait for the image to finish loading before we try moving it to the
3555 // right place in the terminal.
3556 img.onload = () => {
3557 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003558 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003559 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003560 img.style.maxWidth = `${screenSize.width}px`;
3561 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003562
3563 // Parse a width/height specification.
3564 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003565 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003566 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003567 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003568
3569 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3570 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003571 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003572 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003573 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003574 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003575 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003576 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003577 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003578 }
3579
3580 return '';
3581 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003582 img.style.width = parseDim(
3583 options.width, screenSize.width, '--hterm-charsize-width');
3584 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003585 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003586
3587 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003588 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003589 const padRows = Math.ceil(img.clientHeight /
3590 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003591 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003592 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003593 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003594
3595 // Update the max height in case the user shrinks the character size.
3596 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3597
3598 // Move the image to the last row. This way when we scroll up, it doesn't
3599 // disappear when the first row gets clipped. It will disappear when we
3600 // scroll down and the last row is clipped ...
3601 this.document_.body.removeChild(img);
3602 // Create a wrapper node so we can do an absolute in a relative position.
3603 // This helps with rounding errors between JS & CSS counts.
3604 const div = this.document_.createElement('div');
3605 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003606 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003607 img.style.position = 'absolute';
3608 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3609 div.appendChild(img);
3610 const row = this.getRowNode(this.scrollbackRows_.length +
3611 this.getCursorRow() - 1);
3612 row.appendChild(div);
3613
Mike Frysinger2558ed52019-01-14 01:03:41 -05003614 // Now that the image has been read, we can revoke the source.
3615 if (options.uri === undefined) {
3616 URL.revokeObjectURL(img.src);
3617 }
3618
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003619 io.hideOverlay();
3620 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003621
Mike Frysingerbdb34802020-04-07 03:47:32 -04003622 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003623 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003624 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003625 };
3626
3627 // If we got a malformed image, give up.
3628 img.onerror = (e) => {
3629 this.document_.body.removeChild(img);
3630 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003631 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003632 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003633
Mike Frysingerbdb34802020-04-07 03:47:32 -04003634 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003635 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003636 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003637 };
3638 } else {
3639 // We can't use chrome.downloads.download as that requires "downloads"
3640 // permissions, and that works only in extensions, not apps.
3641 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003642 if (options.uri !== undefined) {
3643 a.href = options.uri;
3644 } else if (options.buffer !== undefined) {
3645 const blob = new Blob([options.buffer]);
3646 a.href = URL.createObjectURL(blob);
3647 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003648 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003649 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003650 a.download = options.name;
3651 this.document_.body.appendChild(a);
3652 a.click();
3653 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003654 if (options.uri === undefined) {
3655 URL.revokeObjectURL(a.href);
3656 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003657 }
3658};
3659
3660/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003661 * Returns the selected text, or null if no text is selected.
3662 *
3663 * @return {string|null}
3664 */
rgindaa09e7332012-08-17 12:49:51 -07003665hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003666 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003667 selection.sync();
3668
Mike Frysingerbdb34802020-04-07 03:47:32 -04003669 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003670 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003671 }
rgindaa09e7332012-08-17 12:49:51 -07003672
rgindaa09e7332012-08-17 12:49:51 -07003673 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003674 let startOffset = selection.startOffset;
3675 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003676
Raymes Khoury334625a2018-06-25 10:29:40 +10003677 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003678 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003679 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003681
Robert Gindafdbb3f22012-09-06 20:23:06 -07003682 if (node.nodeName != 'X-ROW') {
3683 // If the selection doesn't start on an x-row node, then it must be
3684 // somewhere inside the x-row. Add any characters from previous siblings
3685 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003686
3687 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3688 // If node is the text node in a styled span, move up to the span node.
3689 node = node.parentNode;
3690 }
3691
Robert Gindafdbb3f22012-09-06 20:23:06 -07003692 while (node.previousSibling) {
3693 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003694 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003695 }
rgindaa09e7332012-08-17 12:49:51 -07003696 }
3697
3698 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003699 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003700 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003701 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003702
Robert Gindafdbb3f22012-09-06 20:23:06 -07003703 if (node.nodeName != 'X-ROW') {
3704 // If the selection doesn't end on an x-row node, then it must be
3705 // somewhere inside the x-row. Add any characters from following siblings
3706 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003707
3708 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3709 // If node is the text node in a styled span, move up to the span node.
3710 node = node.parentNode;
3711 }
3712
Robert Gindafdbb3f22012-09-06 20:23:06 -07003713 while (node.nextSibling) {
3714 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003715 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003716 }
rgindaa09e7332012-08-17 12:49:51 -07003717 }
3718
Mike Frysingerdc727792020-04-10 01:41:13 -04003719 const rv = this.getRowsText(selection.startRow.rowIndex,
3720 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003721 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003722};
3723
rginda4bba5e12012-06-20 16:15:30 -07003724/**
3725 * Copy the current selection to the system clipboard, then clear it after a
3726 * short delay.
3727 */
3728hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003729 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003730 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003731 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003732 }
rginda4bba5e12012-06-20 16:15:30 -07003733};
3734
Joel Hockey0f933582019-08-27 18:01:51 -07003735/**
3736 * Show overlay with current terminal size.
3737 */
rgindaf0090c92012-02-10 14:58:52 -08003738hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003739 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003740 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003741 }
rgindaf0090c92012-02-10 14:58:52 -08003742};
3743
rginda87b86462011-12-14 13:48:03 -08003744/**
3745 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3746 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003747 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003748 */
3749hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003750 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003751 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003752 }
rginda87b86462011-12-14 13:48:03 -08003753
Mike Frysinger225c99d2019-10-20 14:02:37 -06003754 this.pauseCursorBlink_();
3755
Mike Frysinger79669762018-12-30 20:51:10 -05003756 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003757};
3758
3759/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003760 * Open the selected url.
3761 */
3762hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003763 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003764
3765 // If there is no selection, try and expand wherever they clicked.
3766 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003767 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003768 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003769
3770 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003771 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003772 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003773 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003774 }
3775
3776 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003777 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003778 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003779 }
Mike Frysinger43472622017-06-26 18:11:07 -04003780
3781 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003782 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003783 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3784 // We have to whitelist a few protocols that lack authorities and thus
3785 // never use the //. Like mailto.
3786 switch (str.split(':', 1)[0]) {
3787 case 'mailto':
3788 break;
3789 default:
3790 str = 'http://' + str;
3791 break;
3792 }
3793 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003794
Mike Frysinger720fa832017-10-23 01:15:52 -04003795 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003796};
Mike Frysinger70b94692017-01-26 18:57:50 -10003797
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003798/**
3799 * Manage the automatic mouse hiding behavior while typing.
3800 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003801 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003802 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003803hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003804 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3805 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003806 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003807 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003808 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003809
3810 this.mouseHideWhileTyping_ = !!v;
3811};
3812
3813/**
3814 * Handler for monitoring user keyboard activity.
3815 *
3816 * This isn't for processing the keystrokes directly, but for updating any
3817 * state that might toggle based on the user using the keyboard at all.
3818 *
Joel Hockey0f933582019-08-27 18:01:51 -07003819 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003820 */
3821hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3822 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003823 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003824 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003825 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003826};
Mike Frysinger70b94692017-01-26 18:57:50 -10003827
3828/**
rgindad5613292012-06-19 15:40:37 -07003829 * Add the terminalRow and terminalColumn properties to mouse events and
3830 * then forward on to onMouse().
3831 *
3832 * The terminalRow and terminalColumn properties contain the (row, column)
3833 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003834 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003835 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003836 */
3837hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003838 if (e.processedByTerminalHandler_) {
3839 // We register our event handlers on the document, as well as the cursor
3840 // and the scroll blocker. Mouse events that occur on the cursor or
3841 // scroll blocker will also appear on the document, but we don't want to
3842 // process them twice.
3843 //
3844 // We can't just prevent bubbling because that has other side effects, so
3845 // we decorate the event object with this property instead.
3846 return;
3847 }
3848
Mike Frysinger468966c2018-08-28 13:48:51 -04003849 // Consume navigation events. Button 3 is usually "browser back" and
3850 // button 4 is "browser forward" which we don't want to happen.
3851 if (e.button > 2) {
3852 e.preventDefault();
3853 // We don't return so click events can be passed to the remote below.
3854 }
3855
Mike Frysingerdc727792020-04-10 01:41:13 -04003856 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003857 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3858
rgindafaa74742012-08-21 13:34:03 -07003859 e.processedByTerminalHandler_ = true;
3860
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003861 // Handle auto hiding of mouse cursor while typing.
3862 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3863 // Make sure the mouse cursor is visible.
3864 this.syncMouseStyle();
3865 // This debounce isn't perfect, but should work well enough for such a
3866 // simple implementation. If the user moved the mouse, we enabled this
3867 // debounce, and then moved the mouse just before the timeout, we wouldn't
3868 // debounce that later movement.
3869 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3870 }
3871
Robert Gindaeda48db2014-07-17 09:25:30 -07003872 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003873 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003874 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003875 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003876 this.scrollPort_.characterSize.height) + 1;
3877 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003878 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003879
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003880 // Clamp row and column.
3881 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3882 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3883
3884 // Ignore mousedown in the scrollbar area.
3885 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003886 return;
3887 }
3888
Joel Hockey3babf302020-04-22 15:00:06 -07003889 if (this.options_.cursorVisible && !reportMouseEvents &&
3890 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003891 // If the cursor is visible and we're not sending mouse events to the
3892 // host app, then we want to hide the terminal cursor when the mouse
3893 // cursor is over top. This keeps the terminal cursor from interfering
3894 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003895 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3896 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3897 this.cursorNode_.style.display = 'none';
3898 } else if (this.cursorNode_.style.display == 'none') {
3899 this.cursorNode_.style.display = '';
3900 }
3901 }
rgindad5613292012-06-19 15:40:37 -07003902
Robert Ginda928cf632014-03-05 15:07:41 -08003903 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003904 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003905
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003906 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003907 // If VT mouse reporting is disabled, or has been defeated with
3908 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003909 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003910 this.setSelectionEnabled(true);
3911 } else {
3912 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003913 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003914 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003915 this.setSelectionEnabled(false);
3916 e.preventDefault();
3917 }
3918 }
3919
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003920 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003921 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003922 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003923 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003924 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003925 }
rgindad5613292012-06-19 15:40:37 -07003926 }
3927
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003928 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003929 // Debounce this event with the dblclick event. If you try to doubleclick
3930 // a URL to open it, Chrome will fire click then dblclick, but we won't
3931 // have expanded the selection text at the first click event.
3932 clearTimeout(this.timeouts_.openUrl);
3933 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3934 500);
3935 return;
3936 }
3937
Mike Frysinger847577f2017-05-23 23:25:57 -04003938 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003939 if (e.ctrlKey && e.button == 2 /* right button */) {
3940 e.preventDefault();
3941 this.contextMenu.show(e, this);
3942 } else if (e.button == this.mousePasteButton ||
3943 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003944 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003945 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003946 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003947 }
3948 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003949
Mike Frysinger2edd3612017-05-24 00:54:39 -04003950 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003951 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003952 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003953 }
3954
3955 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3956 this.scrollBlockerNode_.engaged) {
3957 // Disengage the scroll-blocker after one of these events.
3958 this.scrollBlockerNode_.engaged = false;
3959 this.scrollBlockerNode_.style.top = '-99px';
3960 }
3961
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003962 // Emulate arrow key presses via scroll wheel events.
3963 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3964 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003965 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003966 const delta =
3967 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003968
Mike Frysinger321063c2018-08-29 15:33:14 -04003969 // Helper to turn a wheel event delta into a series of key presses.
3970 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3971 if (distance == 0) {
3972 return '';
3973 }
3974
3975 // Convert the scroll distance into a number of rows/cols.
3976 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3977 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3978 return data.repeat(cells);
3979 };
3980
3981 // The order between up/down and left/right doesn't really matter.
3982 this.io.sendString(
3983 // Up/down arrow keys.
3984 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3985 'A', 'B') +
3986 // Left/right arrow keys.
3987 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10003988 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04003989 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003990
3991 e.preventDefault();
3992 }
3993 }
Robert Ginda928cf632014-03-05 15:07:41 -08003994 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003995 if (!this.scrollBlockerNode_.engaged) {
3996 if (e.type == 'mousedown') {
3997 // Move the scroll-blocker into place if we want to keep the scrollport
3998 // from scrolling.
3999 this.scrollBlockerNode_.engaged = true;
4000 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4001 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4002 } else if (e.type == 'mousemove') {
4003 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4004 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004005 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004006 e.preventDefault();
4007 }
4008 }
Robert Ginda928cf632014-03-05 15:07:41 -08004009
4010 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004011 }
4012
Robert Ginda928cf632014-03-05 15:07:41 -08004013 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4014 // Restore this on mouseup in case it was temporarily defeated with a
4015 // alt-mousedown. Only do this when the selection is empty so that
4016 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004017 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004018 }
rgindad5613292012-06-19 15:40:37 -07004019};
4020
4021/**
4022 * Clients should override this if they care to know about mouse events.
4023 *
4024 * The event parameter will be a normal DOM mouse click event with additional
4025 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004026 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004027 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004028 */
4029hterm.Terminal.prototype.onMouse = function(e) { };
4030
4031/**
rginda8e92a692012-05-20 19:37:20 -07004032 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004033 *
4034 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004035 */
Rob Spies06533ba2014-04-24 11:20:37 -07004036hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4037 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004038 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004039
Mike Frysingerbdb34802020-04-07 03:47:32 -04004040 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004041 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004042 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004043
Mike Frysingerbdb34802020-04-07 03:47:32 -04004044 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004045 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004046 }
rginda8e92a692012-05-20 19:37:20 -07004047};
4048
4049/**
rginda8ba33642011-12-14 12:31:31 -08004050 * React when the ScrollPort is scrolled.
4051 */
4052hterm.Terminal.prototype.onScroll_ = function() {
4053 this.scheduleSyncCursorPosition_();
4054};
4055
4056/**
rginda9846e2f2012-01-27 13:53:33 -08004057 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004058 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004059 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004060 */
4061hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004062 this.onPasteData_(e.text);
4063};
4064
4065/**
4066 * Handle pasted data.
4067 *
4068 * @param {string} data The pasted data.
4069 */
4070hterm.Terminal.prototype.onPasteData_ = function(data) {
4071 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004072 if (this.options_.bracketedPaste) {
4073 // We strip out most escape sequences as they can cause issues (like
4074 // inserting an \x1b[201~ midstream). We pass through whitespace
4075 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4076 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004077 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004078 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4079 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4080 }
Robert Gindaa063b202014-07-21 11:08:25 -07004081
4082 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004083};
4084
4085/**
rgindaa09e7332012-08-17 12:49:51 -07004086 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004087 *
Joel Hockey0f933582019-08-27 18:01:51 -07004088 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004089 */
4090hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004091 if (!this.useDefaultWindowCopy) {
4092 e.preventDefault();
4093 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4094 }
rgindaa09e7332012-08-17 12:49:51 -07004095};
4096
4097/**
rginda8ba33642011-12-14 12:31:31 -08004098 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004099 *
4100 * Note: This function should not directly contain code that alters the internal
4101 * state of the terminal. That kind of code belongs in realizeWidth or
4102 * realizeHeight, so that it can be executed synchronously in the case of a
4103 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004104 */
4105hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004106 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4107 this.scrollPort_.characterSize.width) || 0;
4108 const rowCount = lib.f.smartFloorDivide(
4109 this.scrollPort_.getScreenHeight(),
4110 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004111
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004112 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004113 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004114 // gets removed from the document or during the initial load, and we can't
4115 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004116 // This can also happen if called before the scrollPort calculates the
4117 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004118 return;
4119 }
4120
Mike Frysingerdc727792020-04-10 01:41:13 -04004121 const isNewSize = (columnCount != this.screenSize.width ||
4122 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004123 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004124
4125 // We do this even if the size didn't change, just to be sure everything is
4126 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004127 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004128 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004129
Mike Frysingerbdb34802020-04-07 03:47:32 -04004130 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004131 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004132 }
rgindaa8ba17d2012-08-15 14:41:10 -07004133
Robert Gindafb1be6a2013-12-11 11:56:22 -08004134 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004135 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004136
4137 if (wasScrolledEnd) {
4138 this.scrollEnd();
4139 }
rginda8ba33642011-12-14 12:31:31 -08004140};
4141
4142/**
4143 * Service the cursor blink timeout.
4144 */
4145hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004146 if (!this.options_.cursorBlink) {
4147 delete this.timeouts_.cursorBlink;
4148 return;
4149 }
4150
Robert Ginda830583c2013-08-07 13:20:46 -07004151 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004152 this.cursorNode_.style.opacity == '0' ||
4153 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004154 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004155 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4156 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004157 } else {
rginda87b86462011-12-14 13:48:03 -08004158 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004159 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4160 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004161 }
4162};
David Reveman8f552492012-03-28 12:18:41 -04004163
4164/**
4165 * Set the scrollbar-visible mode bit.
4166 *
4167 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4168 * Otherwise it will not.
4169 *
4170 * Defaults to on.
4171 *
4172 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4173 */
4174hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4175 this.scrollPort_.setScrollbarVisible(state);
4176};
Michael Kelly485ecd12014-06-09 11:41:56 -04004177
4178/**
Rob Spies49039e52014-12-17 13:40:04 -08004179 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004180 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004181 *
4182 * Defaults to 1.
4183 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004184 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004185 */
4186hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4187 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4188};
4189
4190/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004191 * Close all web notifications created by terminal bells.
4192 */
4193hterm.Terminal.prototype.closeBellNotifications_ = function() {
4194 this.bellNotificationList_.forEach(function(n) {
4195 n.close();
4196 });
4197 this.bellNotificationList_.length = 0;
4198};
Raymes Khourye5d48982018-08-02 09:08:32 +10004199
4200/**
4201 * Syncs the cursor position when the scrollport gains focus.
4202 */
4203hterm.Terminal.prototype.onScrollportFocus_ = function() {
4204 // If the cursor is offscreen we set selection to the last row on the screen.
4205 const topRowIndex = this.scrollPort_.getTopRowIndex();
4206 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4207 const selection = this.document_.getSelection();
4208 if (!this.syncCursorPosition_() && selection) {
4209 selection.collapse(this.getRowNode(bottomRowIndex));
4210 }
4211};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004212
4213/**
4214 * Clients can override this if they want to provide an options page.
4215 */
4216hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4217
4218
4219/**
4220 * Called when user selects to open the options page.
4221 */
4222hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4223 this.onOpenOptionsPage();
4224};