blob: 6b16501fe251101ed9ecb1579e9700afdeb2643a [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) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700109 this.screenBorderSize_ = 0;
110
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.scrollOnOutput_ = null;
112 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400113 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800114
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700115 // True if we should override mouse event reporting to allow local selection.
116 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800117
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400118 // Whether to auto hide the mouse cursor when typing.
119 this.setAutomaticMouseHiding();
120 // Timer to keep mouse visible while it's being used.
121 this.mouseHideDelay_ = null;
122
rgindaf0090c92012-02-10 14:58:52 -0800123 // Terminal bell sound.
124 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400125 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800126 this.bellAudio_.setAttribute('preload', 'auto');
127
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000128 // The AccessibilityReader object for announcing command output.
129 this.accessibilityReader_ = null;
130
Mike Frysingercc114512017-09-11 21:39:17 -0400131 // The context menu object.
132 this.contextMenu = new hterm.ContextMenu();
133
Michael Kelly485ecd12014-06-09 11:41:56 -0400134 // All terminal bell notifications that have been generated (not necessarily
135 // shown).
136 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700137 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
139 // Whether we have permission to display notifications.
140 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
rginda6d397402012-01-17 10:58:29 -0800142 // Cursor position and attributes saved with DECSC.
143 this.savedOptions_ = {};
144
rginda8ba33642011-12-14 12:31:31 -0800145 // The current mode bits for the terminal.
146 this.options_ = new hterm.Options();
147
148 // Timeouts we might need to clear.
149 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800150
151 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800152 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800153
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800154 this.saveCursorAndState(true);
155
Zhu Qunying30d40712017-03-14 16:27:00 -0700156 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800157 this.keyboard = new hterm.Keyboard(this);
158
rginda87b86462011-12-14 13:48:03 -0800159 // General IO interface that can be given to third parties without exposing
160 // the entire terminal object.
161 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800162
rgindad5613292012-06-19 15:40:37 -0700163 // True if mouse-click-drag should scroll the terminal.
164 this.enableMouseDragScroll = true;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400167 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700168 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700169
Zhu Qunying30d40712017-03-14 16:27:00 -0700170 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700171 this.useDefaultWindowCopy = false;
172
173 this.clearSelectionAfterCopy = true;
174
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400175 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800176 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700177
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400178 // Whether we allow images to be shown.
179 this.allowImagesInline = null;
180
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400181 this.reportFocus = false;
182
Jason Linf129f3c2020-03-23 11:52:08 +1100183 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
184 this.alwaysUseLegacyPasting = false;
185
Joel Hockey3a44a442019-10-14 16:22:56 -0700186 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500187 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800188};
189
190/**
Robert Ginda830583c2013-08-07 13:20:46 -0700191 * Possible cursor shapes.
192 */
193hterm.Terminal.cursorShape = {
194 BLOCK: 'BLOCK',
195 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400196 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700197};
198
199/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700200 * Clients should override this to be notified when the terminal is ready
201 * for use.
202 *
203 * The terminal initialization is asynchronous, and shouldn't be used before
204 * this method is called.
205 */
206hterm.Terminal.prototype.onTerminalReady = function() { };
207
208/**
rginda35c456b2012-02-09 17:29:05 -0800209 * Default tab with of 8 to match xterm.
210 */
211hterm.Terminal.prototype.tabWidth = 8;
212
213/**
rginda9f5222b2012-03-05 11:53:28 -0800214 * Select a preference profile.
215 *
216 * This will load the terminal preferences for the given profile name and
217 * associate subsequent preference changes with the new preference profile.
218 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500219 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800220 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400221 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700222 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800223 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400224hterm.Terminal.prototype.setProfile = function(
225 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800227
Mike Frysingerdc727792020-04-10 01:41:13 -0400228 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800229
Mike Frysingerbdb34802020-04-07 03:47:32 -0400230 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700231 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400232 }
rginda9f5222b2012-03-05 11:53:28 -0800233
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700235
236 /**
237 * Clears and reloads key bindings. Used by preferences
238 * 'keybindings' and 'keybindings-os-defaults'.
239 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400240 * @param {*?=} bindings
241 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700242 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700244 terminal.keyboard.bindings.clear();
245
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 // Default to an empty object so we still handle OS defaults.
247 if (bindings === null) {
248 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700249 }
250
251 if (!(bindings instanceof Object)) {
252 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400253 bindings = {};
254 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700255 }
256
257 try {
258 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
259 } catch (ex) {
260 console.error('Error in keybindings preference: ' + ex);
261 }
262 }
263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800265 'alt-gr-mode': function(v) {
266 if (v == null) {
267 if (navigator.language.toLowerCase() == 'en-us') {
268 v = 'none';
269 } else {
270 v = 'right-alt';
271 }
272 } else if (typeof v == 'string') {
273 v = v.toLowerCase();
274 } else {
275 v = 'none';
276 }
277
Mike Frysingerbdb34802020-04-07 03:47:32 -0400278 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800279 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400280 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800281
282 terminal.keyboard.altGrMode = v;
283 },
284
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700285 'alt-backspace-is-meta-backspace': function(v) {
286 terminal.keyboard.altBackspaceIsMetaBackspace = v;
287 },
288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 'alt-is-meta': function(v) {
290 terminal.keyboard.altIsMeta = v;
291 },
292
293 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400294 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400296 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700297
298 terminal.keyboard.altSendsWhat = v;
299 },
300
301 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400302 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800303 if (ary) {
304 terminal.bellAudio_.setAttribute('src',
305 lib.resource.getDataUrl(ary[1]));
306 } else {
307 terminal.bellAudio_.setAttribute('src', v);
308 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 },
310
Michael Kelly485ecd12014-06-09 11:41:56 -0400311 'desktop-notification-bell': function(v) {
312 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700313 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400314 Notification.permission === 'granted';
315 if (!terminal.desktopNotificationBell_) {
316 // Note: We don't call Notification.requestPermission here because
317 // Chrome requires the call be the result of a user action (such as an
318 // onclick handler), and pref listeners are run asynchronously.
319 //
320 // A way of working around this would be to display a dialog in the
321 // terminal with a "click-to-request-permission" button.
322 console.warn('desktop-notification-bell is true but we do not have ' +
323 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400324 }
325 } else {
326 terminal.desktopNotificationBell_ = false;
327 }
328 },
329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 'background-color': function(v) {
331 terminal.setBackgroundColor(v);
332 },
333
334 'background-image': function(v) {
335 terminal.scrollPort_.setBackgroundImage(v);
336 },
337
338 'background-size': function(v) {
339 terminal.scrollPort_.setBackgroundSize(v);
340 },
341
342 'background-position': function(v) {
343 terminal.scrollPort_.setBackgroundPosition(v);
344 },
345
346 'backspace-sends-backspace': function(v) {
347 terminal.keyboard.backspaceSendsBackspace = v;
348 },
349
Brad Town18654b62015-03-12 00:27:45 -0700350 'character-map-overrides': function(v) {
351 if (!(v == null || v instanceof Object)) {
352 console.warn('Preference character-map-modifications is not an ' +
353 'object: ' + v);
354 return;
355 }
356
Mike Frysinger095d4062017-06-14 00:29:48 -0700357 terminal.vt.characterMaps.reset();
358 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700359 },
360
Robert Ginda57f03b42012-09-13 11:02:48 -0700361 'cursor-blink': function(v) {
362 terminal.setCursorBlink(!!v);
363 },
364
Joel Hockey9d10ba12019-05-28 01:25:02 -0700365 'cursor-shape': function(v) {
366 terminal.setCursorShape(v);
367 },
368
Robert Gindaea2183e2014-07-17 09:51:51 -0700369 'cursor-blink-cycle': function(v) {
370 if (v instanceof Array &&
371 typeof v[0] == 'number' &&
372 typeof v[1] == 'number') {
373 terminal.cursorBlinkCycle_ = v;
374 } else if (typeof v == 'number') {
375 terminal.cursorBlinkCycle_ = [v, v];
376 } else {
377 // Fast blink indicates an error.
378 terminal.cursorBlinkCycle_ = [100, 100];
379 }
380 },
381
Robert Ginda57f03b42012-09-13 11:02:48 -0700382 'cursor-color': function(v) {
383 terminal.setCursorColor(v);
384 },
385
386 'color-palette-overrides': function(v) {
387 if (!(v == null || v instanceof Object || v instanceof Array)) {
388 console.warn('Preference color-palette-overrides is not an array or ' +
389 'object: ' + v);
390 return;
rginda9f5222b2012-03-05 11:53:28 -0800391 }
rginda9f5222b2012-03-05 11:53:28 -0800392
Joel Hockey42dba8f2020-03-26 16:21:11 -0700393 // Call terminal.setColorPalette here and below with the new default
394 // value before changing it in lib.colors.colorPalette to ensure that
395 // CSS vars are updated.
396 lib.colors.stockColorPalette.forEach(
397 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700398 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700399
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400401 for (const key in v) {
402 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 if (isNaN(i) || i < 0 || i > 255) {
404 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
405 continue;
406 }
407
408 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400409 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700410 if (rgb) {
411 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700413 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 }
415 }
rginda30f20f62012-04-05 16:36:19 -0700416 }
rginda30f20f62012-04-05 16:36:19 -0700417
Joel Hockey42dba8f2020-03-26 16:21:11 -0700418 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
419 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 },
rginda30f20f62012-04-05 16:36:19 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'copy-on-select': function(v) {
423 terminal.copyOnSelect = !!v;
424 },
rginda9f5222b2012-03-05 11:53:28 -0800425
Rob Spies0bec09b2014-06-06 15:58:09 -0700426 'use-default-window-copy': function(v) {
427 terminal.useDefaultWindowCopy = !!v;
428 },
429
430 'clear-selection-after-copy': function(v) {
431 terminal.clearSelectionAfterCopy = !!v;
432 },
433
Robert Ginda7e5e9522014-03-14 12:23:58 -0700434 'ctrl-plus-minus-zero-zoom': function(v) {
435 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
436 },
437
Robert Gindafb5a3f92014-05-13 14:12:00 -0700438 'ctrl-c-copy': function(v) {
439 terminal.keyboard.ctrlCCopy = v;
440 },
441
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100442 'ctrl-v-paste': function(v) {
443 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700444 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100445 },
446
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700447 'paste-on-drop': function(v) {
448 terminal.scrollPort_.setPasteOnDrop(v);
449 },
450
Masaya Suzuki273aa982014-05-31 07:25:55 +0900451 'east-asian-ambiguous-as-two-column': function(v) {
452 lib.wc.regardCjkAmbiguous = v;
453 },
454
Robert Ginda57f03b42012-09-13 11:02:48 -0700455 'enable-8-bit-control': function(v) {
456 terminal.vt.enable8BitControl = !!v;
457 },
rginda30f20f62012-04-05 16:36:19 -0700458
Robert Ginda57f03b42012-09-13 11:02:48 -0700459 'enable-bold': function(v) {
460 terminal.syncBoldSafeState();
461 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400462
Robert Ginda3e278d72014-03-25 13:18:51 -0700463 'enable-bold-as-bright': function(v) {
464 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
465 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
466 },
467
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400468 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500469 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400470 },
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 'enable-clipboard-write': function(v) {
473 terminal.vt.enableClipboardWrite = !!v;
474 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400475
Robert Ginda3755e752013-05-31 13:34:09 -0700476 'enable-dec12': function(v) {
477 terminal.vt.enableDec12 = !!v;
478 },
479
Mike Frysinger38f267d2018-09-07 02:50:59 -0400480 'enable-csi-j-3': function(v) {
481 terminal.vt.enableCsiJ3 = !!v;
482 },
483
Robert Ginda57f03b42012-09-13 11:02:48 -0700484 'font-family': function(v) {
485 terminal.syncFontFamily();
486 },
rginda30f20f62012-04-05 16:36:19 -0700487
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700489 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700490 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500491 console.error(`Invalid font size: ${v}`);
492 return;
493 }
494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 terminal.setFontSize(v);
496 },
rginda9875d902012-08-20 16:21:57 -0700497
Robert Ginda57f03b42012-09-13 11:02:48 -0700498 'font-smoothing': function(v) {
499 terminal.syncFontFamily();
500 },
rgindade84e382012-04-20 15:39:31 -0700501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 'foreground-color': function(v) {
503 terminal.setForegroundColor(v);
504 },
rginda30f20f62012-04-05 16:36:19 -0700505
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400506 'hide-mouse-while-typing': function(v) {
507 terminal.setAutomaticMouseHiding(v);
508 },
509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'home-keys-scroll': function(v) {
511 terminal.keyboard.homeKeysScroll = v;
512 },
rginda4bba5e12012-06-20 16:15:30 -0700513
Robert Gindaa8165692015-06-15 14:46:31 -0700514 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700515 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
516 },
Robert Gindaa8165692015-06-15 14:46:31 -0700517
Joel Hockey95a9e272020-03-16 21:19:53 -0700518 'keybindings-os-defaults': function(v) {
519 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700520 },
521
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700522 'media-keys-are-fkeys': function(v) {
523 terminal.keyboard.mediaKeysAreFKeys = v;
524 },
525
Robert Ginda57f03b42012-09-13 11:02:48 -0700526 'meta-sends-escape': function(v) {
527 terminal.keyboard.metaSendsEscape = v;
528 },
rginda30f20f62012-04-05 16:36:19 -0700529
Mike Frysinger847577f2017-05-23 23:25:57 -0400530 'mouse-right-click-paste': function(v) {
531 terminal.mouseRightClickPaste = v;
532 },
533
Robert Ginda57f03b42012-09-13 11:02:48 -0700534 'mouse-paste-button': function(v) {
535 terminal.syncMousePasteButton();
536 },
rgindaa8ba17d2012-08-15 14:41:10 -0700537
Robert Gindae76aa9f2014-03-14 12:29:12 -0700538 'page-keys-scroll': function(v) {
539 terminal.keyboard.pageKeysScroll = v;
540 },
541
Robert Ginda40932892012-12-10 17:26:40 -0800542 'pass-alt-number': function(v) {
543 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700544 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800545 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500546 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800547 }
548
549 terminal.passAltNumber = v;
550 },
551
552 'pass-ctrl-number': function(v) {
553 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700554 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800555 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500556 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800557 }
558
559 terminal.passCtrlNumber = v;
560 },
561
Joel Hockey0e052042020-02-19 05:37:19 -0800562 'pass-ctrl-n': function(v) {
563 terminal.passCtrlN = v;
564 },
565
566 'pass-ctrl-t': function(v) {
567 terminal.passCtrlT = v;
568 },
569
570 'pass-ctrl-tab': function(v) {
571 terminal.passCtrlTab = v;
572 },
573
574 'pass-ctrl-w': function(v) {
575 terminal.passCtrlW = v;
576 },
577
Robert Ginda40932892012-12-10 17:26:40 -0800578 'pass-meta-number': function(v) {
579 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700580 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800581 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500582 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800583 }
584
585 terminal.passMetaNumber = v;
586 },
587
Marius Schilder77857b32014-05-14 16:21:26 -0700588 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700589 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700590 },
591
Robert Ginda8cb7d902013-06-20 14:37:18 -0700592 'receive-encoding': function(v) {
593 if (!(/^(utf-8|raw)$/).test(v)) {
594 console.warn('Invalid value for "receive-encoding": ' + v);
595 v = 'utf-8';
596 }
597
598 terminal.vt.characterEncoding = v;
599 },
600
Joel Hockey139d82d2020-04-07 23:04:29 -0700601 'screen-padding-size': function(v) {
602 v = parseInt(v, 10);
603 if (isNaN(v) || v < 0) {
604 console.error(`Invalid screen padding size: ${v}`);
605 return;
606 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700607 terminal.setScreenPaddingSize(v);
608 },
609
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700610 'screen-border-size': function(v) {
611 v = parseInt(v, 10);
612 if (isNaN(v) || v < 0) {
613 console.error(`Invalid screen border size: ${v}`);
614 return;
615 }
616 terminal.setScreenBorderSize(v);
617 },
618
619 'screen-border-color': function(v) {
620 terminal.div_.style.borderColor = v;
621 },
622
Robert Ginda57f03b42012-09-13 11:02:48 -0700623 'scroll-on-keystroke': function(v) {
624 terminal.scrollOnKeystroke_ = v;
625 },
rginda9f5222b2012-03-05 11:53:28 -0800626
Robert Ginda57f03b42012-09-13 11:02:48 -0700627 'scroll-on-output': function(v) {
628 terminal.scrollOnOutput_ = v;
629 },
rginda30f20f62012-04-05 16:36:19 -0700630
Robert Ginda57f03b42012-09-13 11:02:48 -0700631 'scrollbar-visible': function(v) {
632 terminal.setScrollbarVisible(v);
633 },
rginda9f5222b2012-03-05 11:53:28 -0800634
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400635 'scroll-wheel-may-send-arrow-keys': function(v) {
636 terminal.scrollWheelArrowKeys_ = v;
637 },
638
Rob Spies49039e52014-12-17 13:40:04 -0800639 'scroll-wheel-move-multiplier': function(v) {
640 terminal.setScrollWheelMoveMultipler(v);
641 },
642
Robert Ginda57f03b42012-09-13 11:02:48 -0700643 'shift-insert-paste': function(v) {
644 terminal.keyboard.shiftInsertPaste = v;
645 },
rginda9f5222b2012-03-05 11:53:28 -0800646
Mike Frysingera7768922017-07-28 15:00:12 -0400647 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400648 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400649 },
650
Robert Gindae76aa9f2014-03-14 12:29:12 -0700651 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400652 terminal.scrollPort_.setUserCssUrl(v);
653 },
654
655 'user-css-text': function(v) {
656 terminal.scrollPort_.setUserCssText(v);
657 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400658
659 'word-break-match-left': function(v) {
660 terminal.primaryScreen_.wordBreakMatchLeft = v;
661 terminal.alternateScreen_.wordBreakMatchLeft = v;
662 },
663
664 'word-break-match-right': function(v) {
665 terminal.primaryScreen_.wordBreakMatchRight = v;
666 terminal.alternateScreen_.wordBreakMatchRight = v;
667 },
668
669 'word-break-match-middle': function(v) {
670 terminal.primaryScreen_.wordBreakMatchMiddle = v;
671 terminal.alternateScreen_.wordBreakMatchMiddle = v;
672 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400673
674 'allow-images-inline': function(v) {
675 terminal.allowImagesInline = v;
676 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700677 });
rginda30f20f62012-04-05 16:36:19 -0700678
Robert Ginda57f03b42012-09-13 11:02:48 -0700679 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800680 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700681
Mike Frysingerec4225d2020-04-07 05:00:01 -0400682 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700683 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400684 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400685 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700686 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800687};
688
Rob Spies56953412014-04-28 14:09:47 -0700689/**
690 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500691 *
Joel Hockey0f933582019-08-27 18:01:51 -0700692 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700693 */
694hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700695 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700696};
697
Robert Gindaa063b202014-07-21 11:08:25 -0700698/**
699 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500700 *
701 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700702 */
703hterm.Terminal.prototype.setBracketedPaste = function(state) {
704 this.options_.bracketedPaste = state;
705};
Rob Spies56953412014-04-28 14:09:47 -0700706
rginda8e92a692012-05-20 19:37:20 -0700707/**
708 * Set the color for the cursor.
709 *
710 * If you want this setting to persist, set it through prefs_, rather than
711 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500712 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500713 * @param {string=} color The color to set. If not defined, we reset to the
714 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700715 */
716hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400717 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700718 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400719 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500720
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400721 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700722};
723
724/**
725 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400726 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500727 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700728 */
729hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400730 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700731};
732
733/**
rgindad5613292012-06-19 15:40:37 -0700734 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700737 */
738hterm.Terminal.prototype.setSelectionEnabled = function(state) {
739 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700740};
741
742/**
rginda8e92a692012-05-20 19:37:20 -0700743 * Set the background color.
744 *
745 * If you want this setting to persist, set it through prefs_, rather than
746 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500747 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500748 * @param {string=} color The color to set. If not defined, we reset to the
749 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700750 */
751hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400752 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700753 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400754 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500755
Joel Hockey42dba8f2020-03-26 16:21:11 -0700756 this.backgroundColor_ = lib.colors.normalizeCSS(color);
757 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700758};
759
rginda9f5222b2012-03-05 11:53:28 -0800760/**
761 * Return the current terminal background color.
762 *
763 * Intended for use by other classes, so we don't have to expose the entire
764 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500765 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700766 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800767 */
768hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700769 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700770};
771
772/**
773 * Set the foreground color.
774 *
775 * If you want this setting to persist, set it through prefs_, rather than
776 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500777 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500778 * @param {string=} color The color to set. If not defined, we reset to the
779 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700780 */
781hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400782 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700783 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400784 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500785
Joel Hockey42dba8f2020-03-26 16:21:11 -0700786 this.foregroundColor_ = lib.colors.normalizeCSS(color);
787 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800788};
789
790/**
791 * Return the current terminal foreground color.
792 *
793 * Intended for use by other classes, so we don't have to expose the entire
794 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500795 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700796 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800797 */
798hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700799 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800800};
801
802/**
rginda87b86462011-12-14 13:48:03 -0800803 * Create a new instance of a terminal command and run it with a given
804 * argument string.
805 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700806 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700807 * @param {string} commandName The command to run for this terminal.
808 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800809 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700810hterm.Terminal.prototype.runCommandClass = function(
811 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400812 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400813 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700814 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400815 }
rgindaf522ce02012-04-17 17:49:17 -0700816
rginda87b86462011-12-14 13:48:03 -0800817 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700818 {
819 commandName: commandName,
820 args: args,
rginda87b86462011-12-14 13:48:03 -0800821 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700822 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400823 onExit: (code) => {
824 this.io.pop();
825 this.uninstallKeyboard();
826 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
827 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400828 window.close();
829 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400830 },
rginda87b86462011-12-14 13:48:03 -0800831 });
832
rgindafeaf3142012-01-31 15:14:20 -0800833 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800834 this.command.run();
835};
836
837/**
rgindafeaf3142012-01-31 15:14:20 -0800838 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500839 *
840 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800841 */
842hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700843 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800844};
845
846/**
847 * Install the keyboard handler for this terminal.
848 *
849 * This will prevent the browser from seeing any keystrokes sent to the
850 * terminal.
851 */
852hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700853 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400854};
rgindafeaf3142012-01-31 15:14:20 -0800855
856/**
857 * Uninstall the keyboard handler for this terminal.
858 */
859hterm.Terminal.prototype.uninstallKeyboard = function() {
860 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400861};
rgindafeaf3142012-01-31 15:14:20 -0800862
863/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400864 * Set a CSS variable.
865 *
866 * Normally this is used to set variables in the hterm namespace.
867 *
868 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700869 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400870 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400871 */
872hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400873 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400874 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400875 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400876};
877
878/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700879 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
880 * input is valid.
881 *
882 * @param {string} name The variable to set.
883 * @param {?string} rgb The rgb value to assign to the variable.
884 */
885hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
886 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
887 if (ary) {
888 this.setCssVar(name, ary.slice(0, 3).join(','));
889 }
890};
891
892/**
893 * Sets the specified color for the active screen.
894 *
895 * @param {number} i The index into the 256 color palette to set.
896 * @param {?string} rgb The rgb value to assign to the variable.
897 */
898hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
899 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
900 this.setRgbColorCssVar(`color-${i}`, rgb);
901 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
902 }
903};
904
905/**
906 * Returns the current value in the active screen of the specified color.
907 *
908 * @param {number} i Color palette index.
909 * @return {string} rgb color.
910 */
911hterm.Terminal.prototype.getColorPalette = function(i) {
912 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
913 lib.colors.colorPalette[i];
914};
915
916/**
917 * Reset the specified color in the active screen to its default value.
918 *
919 * @param {number} i Color to reset
920 */
921hterm.Terminal.prototype.resetColor = function(i) {
922 this.setColorPalette(i, lib.colors.colorPalette[i]);
923 delete this.screen_.textAttributes.colorPaletteOverrides[i];
924};
925
926/**
927 * Reset the current screen color palette to the default state.
928 */
929hterm.Terminal.prototype.resetColorPalette = function() {
930 this.screen_.textAttributes.colorPaletteOverrides.forEach(
931 (c, i) => this.resetColor(i));
932};
933
934/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500935 * Get a CSS variable.
936 *
937 * Normally this is used to get variables in the hterm namespace.
938 *
939 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400940 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500941 * @return {string} The current setting for this variable.
942 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400943hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500944 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400945 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500946};
947
948/**
Jason Linbbbdb752020-03-06 16:26:59 +1100949 * Update CSS character size variables to match the scrollport.
950 */
951hterm.Terminal.prototype.updateCssCharsize_ = function() {
952 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
953 this.setCssVar('charsize-height',
954 this.scrollPort_.characterSize.height + 'px');
955};
956
957/**
rginda35c456b2012-02-09 17:29:05 -0800958 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800959 *
960 * Call setFontSize(0) to reset to the default font size.
961 *
962 * This function does not modify the font-size preference.
963 *
964 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800965 */
966hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400967 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700968 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400969 }
rginda9f5222b2012-03-05 11:53:28 -0800970
rginda35c456b2012-02-09 17:29:05 -0800971 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -0700972 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +1100973 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800974};
975
976/**
977 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500978 *
979 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800980 */
981hterm.Terminal.prototype.getFontSize = function() {
982 return this.scrollPort_.getFontSize();
983};
984
985/**
rginda8e92a692012-05-20 19:37:20 -0700986 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500987 *
988 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700989 */
990hterm.Terminal.prototype.getFontFamily = function() {
991 return this.scrollPort_.getFontFamily();
992};
993
994/**
rginda35c456b2012-02-09 17:29:05 -0800995 * Set the CSS "font-family" for this terminal.
996 */
rginda9f5222b2012-03-05 11:53:28 -0800997hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700998 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
999 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001000 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001001 this.syncBoldSafeState();
1002};
1003
rginda4bba5e12012-06-20 16:15:30 -07001004/**
1005 * Set this.mousePasteButton based on the mouse-paste-button pref,
1006 * autodetecting if necessary.
1007 */
1008hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001009 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001010 if (typeof button == 'number') {
1011 this.mousePasteButton = button;
1012 return;
1013 }
1014
Mike Frysingeree81a002017-12-12 16:14:53 -05001015 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001016 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001017 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001018 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001019 }
1020};
1021
1022/**
1023 * Enable or disable bold based on the enable-bold pref, autodetecting if
1024 * necessary.
1025 */
rginda9f5222b2012-03-05 11:53:28 -08001026hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001027 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001028 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001029 this.primaryScreen_.textAttributes.enableBold = enableBold;
1030 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001031 return;
1032 }
1033
Mike Frysingerdc727792020-04-10 01:41:13 -04001034 const normalSize = this.scrollPort_.measureCharacterSize();
1035 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001036
Mike Frysingerdc727792020-04-10 01:41:13 -04001037 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001038 if (!isBoldSafe) {
1039 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001040 'from normal. Font family is: ' +
1041 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001042 }
rginda9f5222b2012-03-05 11:53:28 -08001043
Robert Gindaed016262012-10-26 16:27:09 -07001044 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1045 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001046};
1047
1048/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001049 * Control text blinking behavior.
1050 *
1051 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001052 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001053hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001054 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001055 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001056 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001057 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001058};
1059
1060/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001061 * Set the mouse cursor style based on the current terminal mode.
1062 */
1063hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001064 this.setCssVar('mouse-cursor-style',
1065 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1066 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001067 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001068};
1069
1070/**
rginda87b86462011-12-14 13:48:03 -08001071 * Return a copy of the current cursor position.
1072 *
Joel Hockey0f933582019-08-27 18:01:51 -07001073 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001074 */
1075hterm.Terminal.prototype.saveCursor = function() {
1076 return this.screen_.cursorPosition.clone();
1077};
1078
Evan Jones2600d4f2016-12-06 09:29:36 -05001079/**
1080 * Return the current text attributes.
1081 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001082 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001083 */
rgindaa19afe22012-01-25 15:40:22 -08001084hterm.Terminal.prototype.getTextAttributes = function() {
1085 return this.screen_.textAttributes;
1086};
1087
Evan Jones2600d4f2016-12-06 09:29:36 -05001088/**
1089 * Set the text attributes.
1090 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001091 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001092 */
rginda1a09aa02012-06-18 21:11:25 -07001093hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1094 this.screen_.textAttributes = textAttributes;
1095};
1096
rginda87b86462011-12-14 13:48:03 -08001097/**
rgindaf522ce02012-04-17 17:49:17 -07001098 * Return the current browser zoom factor applied to the terminal.
1099 *
1100 * @return {number} The current browser zoom factor.
1101 */
1102hterm.Terminal.prototype.getZoomFactor = function() {
1103 return this.scrollPort_.characterSize.zoomFactor;
1104};
1105
1106/**
rginda9846e2f2012-01-27 13:53:33 -08001107 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001108 *
1109 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001110 */
1111hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001112 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001113};
1114
1115/**
rginda87b86462011-12-14 13:48:03 -08001116 * Restore a previously saved cursor position.
1117 *
Joel Hockey0f933582019-08-27 18:01:51 -07001118 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001119 */
1120hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001121 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1122 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001123 this.screen_.setCursorPosition(row, column);
1124 if (cursor.column > column ||
1125 cursor.column == column && cursor.overflow) {
1126 this.screen_.cursorPosition.overflow = true;
1127 }
rginda87b86462011-12-14 13:48:03 -08001128};
1129
1130/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001131 * Clear the cursor's overflow flag.
1132 */
1133hterm.Terminal.prototype.clearCursorOverflow = function() {
1134 this.screen_.cursorPosition.overflow = false;
1135};
1136
1137/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001138 * Save the current cursor state to the corresponding screens.
1139 *
1140 * See the hterm.Screen.CursorState class for more details.
1141 *
1142 * @param {boolean=} both If true, update both screens, else only update the
1143 * current screen.
1144 */
1145hterm.Terminal.prototype.saveCursorAndState = function(both) {
1146 if (both) {
1147 this.primaryScreen_.saveCursorAndState(this.vt);
1148 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001149 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001150 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001151 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001152};
1153
1154/**
1155 * Restore the saved cursor state in the corresponding screens.
1156 *
1157 * See the hterm.Screen.CursorState class for more details.
1158 *
1159 * @param {boolean=} both If true, update both screens, else only update the
1160 * current screen.
1161 */
1162hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1163 if (both) {
1164 this.primaryScreen_.restoreCursorAndState(this.vt);
1165 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001166 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001167 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001168 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001169};
1170
1171/**
Robert Ginda830583c2013-08-07 13:20:46 -07001172 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001173 *
1174 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001175 */
1176hterm.Terminal.prototype.setCursorShape = function(shape) {
1177 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001178 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001179};
Robert Ginda830583c2013-08-07 13:20:46 -07001180
1181/**
1182 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001183 *
1184 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001185 */
1186hterm.Terminal.prototype.getCursorShape = function() {
1187 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001188};
Robert Ginda830583c2013-08-07 13:20:46 -07001189
1190/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001191 * Set the screen padding size in pixels.
1192 *
1193 * @param {number} size
1194 */
1195hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001196 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001197 this.scrollPort_.setScreenPaddingSize(size);
1198};
1199
1200/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001201 * Set the screen border size in pixels.
1202 *
1203 * @param {number} size
1204 */
1205hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1206 this.div_.style.borderWidth = `${size}px`;
1207 this.screenBorderSize_ = size;
1208 this.scrollPort_.resize();
1209};
1210
1211/**
rginda87b86462011-12-14 13:48:03 -08001212 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001213 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001214 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001215 */
1216hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001217 if (columnCount == null) {
1218 this.div_.style.width = '100%';
1219 return;
1220 }
1221
Joel Hockey139d82d2020-04-07 23:04:29 -07001222 const rightPadding = Math.max(
1223 this.scrollPort_.screenPaddingSize,
1224 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001225 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001226 (this.scrollPort_.characterSize.width * columnCount) +
1227 this.scrollPort_.screenPaddingSize + rightPadding +
1228 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001229 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001230 this.scheduleSyncCursorPosition_();
1231};
rginda87b86462011-12-14 13:48:03 -08001232
rgindac9bc5502012-01-18 11:48:44 -08001233/**
rginda35c456b2012-02-09 17:29:05 -08001234 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001235 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001236 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001237 */
1238hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001239 if (rowCount == null) {
1240 this.div_.style.height = '100%';
1241 return;
1242 }
1243
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001244 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1245 (2 * this.scrollPort_.screenPaddingSize) +
1246 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001247 this.realizeSize_(this.screenSize.width, rowCount);
1248 this.scheduleSyncCursorPosition_();
1249};
1250
1251/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001252 * Deal with terminal size changes.
1253 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001254 * @param {number} columnCount The number of columns.
1255 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001256 */
1257hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001258 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001259
Mike Frysinger0206e262019-06-13 10:18:19 -04001260 if (columnCount != this.screenSize.width) {
1261 notify = true;
1262 this.realizeWidth_(columnCount);
1263 }
1264
1265 if (rowCount != this.screenSize.height) {
1266 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001267 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001268 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001269
1270 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001271 if (notify) {
1272 this.io.onTerminalResize_(columnCount, rowCount);
1273 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001274};
1275
1276/**
rgindac9bc5502012-01-18 11:48:44 -08001277 * Deal with terminal width changes.
1278 *
1279 * This function does what needs to be done when the terminal width changes
1280 * out from under us. It happens here rather than in onResize_() because this
1281 * code may need to run synchronously to handle programmatic changes of
1282 * terminal width.
1283 *
1284 * Relying on the browser to send us an async resize event means we may not be
1285 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001286 *
1287 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001288 */
1289hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001290 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001291 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001292 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001293
Mike Frysingerdc727792020-04-10 01:41:13 -04001294 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001295 if (deltaColumns == 0) {
1296 // No change, so don't bother recalculating things.
1297 return;
1298 }
rgindac9bc5502012-01-18 11:48:44 -08001299
rginda87b86462011-12-14 13:48:03 -08001300 this.screenSize.width = columnCount;
1301 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001302
1303 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001304 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001305 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001306 }
rgindac9bc5502012-01-18 11:48:44 -08001307 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001308 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001309 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001310 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001311 }
rgindac9bc5502012-01-18 11:48:44 -08001312
1313 this.tabStops_.pop();
1314 }
1315 }
1316
1317 this.screen_.setColumnCount(this.screenSize.width);
1318};
1319
1320/**
1321 * Deal with terminal height changes.
1322 *
1323 * This function does what needs to be done when the terminal height changes
1324 * out from under us. It happens here rather than in onResize_() because this
1325 * code may need to run synchronously to handle programmatic changes of
1326 * terminal height.
1327 *
1328 * Relying on the browser to send us an async resize event means we may not be
1329 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001330 *
1331 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001332 */
1333hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001334 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001335 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001336 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001337
Mike Frysingerdc727792020-04-10 01:41:13 -04001338 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001339 if (deltaRows == 0) {
1340 // No change, so don't bother recalculating things.
1341 return;
1342 }
rgindac9bc5502012-01-18 11:48:44 -08001343
1344 this.screenSize.height = rowCount;
1345
Mike Frysingerdc727792020-04-10 01:41:13 -04001346 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001347
1348 if (deltaRows < 0) {
1349 // Screen got smaller.
1350 deltaRows *= -1;
1351 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001352 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001353 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001354 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001355 }
rgindac9bc5502012-01-18 11:48:44 -08001356
Mike Frysingerbdb34802020-04-07 03:47:32 -04001357 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001358 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001359 }
rgindac9bc5502012-01-18 11:48:44 -08001360
1361 this.screen_.popRow();
1362 deltaRows--;
1363 }
1364
Mike Frysingerdc727792020-04-10 01:41:13 -04001365 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001366 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1367
1368 // We just removed rows from the top of the screen, we need to update
1369 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001370 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001371 } else if (deltaRows > 0) {
1372 // Screen got larger.
1373
1374 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001375 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1376 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001377 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1378 this.screen_.unshiftRows(rows);
1379 deltaRows -= scrollbackCount;
1380 cursor.row += scrollbackCount;
1381 }
1382
Mike Frysingerbdb34802020-04-07 03:47:32 -04001383 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001384 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001385 }
rgindac9bc5502012-01-18 11:48:44 -08001386 }
1387
rginda35c456b2012-02-09 17:29:05 -08001388 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001389 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001390};
1391
1392/**
1393 * Scroll the terminal to the top of the scrollback buffer.
1394 */
1395hterm.Terminal.prototype.scrollHome = function() {
1396 this.scrollPort_.scrollRowToTop(0);
1397};
1398
1399/**
1400 * Scroll the terminal to the end.
1401 */
1402hterm.Terminal.prototype.scrollEnd = function() {
1403 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1404};
1405
1406/**
1407 * Scroll the terminal one page up (minus one line) relative to the current
1408 * position.
1409 */
1410hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001411 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001412};
1413
1414/**
1415 * Scroll the terminal one page down (minus one line) relative to the current
1416 * position.
1417 */
1418hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001419 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001420};
1421
rgindac9bc5502012-01-18 11:48:44 -08001422/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001423 * Scroll the terminal one line up relative to the current position.
1424 */
1425hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001426 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001427 this.scrollPort_.scrollRowToTop(i - 1);
1428};
1429
1430/**
1431 * Scroll the terminal one line down relative to the current position.
1432 */
1433hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001434 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001435 this.scrollPort_.scrollRowToTop(i + 1);
1436};
1437
1438/**
Robert Ginda40932892012-12-10 17:26:40 -08001439 * Clear primary screen, secondary screen, and the scrollback buffer.
1440 */
1441hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001442 this.clearHome(this.primaryScreen_);
1443 this.clearHome(this.alternateScreen_);
1444
1445 this.clearScrollback();
1446};
1447
1448/**
1449 * Clear scrollback buffer.
1450 */
1451hterm.Terminal.prototype.clearScrollback = function() {
1452 // Move to the end of the buffer in case the screen was scrolled back.
1453 // We're going to throw it away which would leave the display invalid.
1454 this.scrollEnd();
1455
Robert Ginda40932892012-12-10 17:26:40 -08001456 this.scrollbackRows_.length = 0;
1457 this.scrollPort_.resetCache();
1458
Mike Frysinger9c482b82018-09-07 02:49:36 -04001459 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1460 const bottom = screen.getHeight();
1461 this.renumberRows_(0, bottom, screen);
1462 });
Robert Ginda40932892012-12-10 17:26:40 -08001463
1464 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001465 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001466};
1467
1468/**
rgindac9bc5502012-01-18 11:48:44 -08001469 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001470 *
1471 * Perform a full reset to the default values listed in
1472 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001473 */
rginda87b86462011-12-14 13:48:03 -08001474hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001475 this.vt.reset();
1476
rgindac9bc5502012-01-18 11:48:44 -08001477 this.clearAllTabStops();
1478 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001479
Joel Hockey42dba8f2020-03-26 16:21:11 -07001480 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001481 const resetScreen = (screen) => {
1482 // We want to make sure to reset the attributes before we clear the screen.
1483 // The attributes might be used to initialize default/empty rows.
1484 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001485 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001486 this.clearHome(screen);
1487 screen.saveCursorAndState(this.vt);
1488 };
1489 resetScreen(this.primaryScreen_);
1490 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001491
Mike Frysinger84301d02017-11-29 13:28:46 -08001492 // Reset terminal options to their default values.
1493 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001494 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1495
Mike Frysinger84301d02017-11-29 13:28:46 -08001496 this.setVTScrollRegion(null, null);
1497
1498 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001499};
1500
rgindac9bc5502012-01-18 11:48:44 -08001501/**
1502 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001503 *
1504 * Perform a soft reset to the default values listed in
1505 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001506 */
rginda0f5c0292012-01-13 11:00:13 -08001507hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001508 this.vt.reset();
1509
rgindab8bc8932012-04-27 12:45:03 -07001510 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001511 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001512
Brad Townb62dfdc2015-03-16 19:07:15 -07001513 // We show the cursor on soft reset but do not alter the blink state.
1514 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1515
Joel Hockey42dba8f2020-03-26 16:21:11 -07001516 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001517 const resetScreen = (screen) => {
1518 // Xterm also resets the color palette on soft reset, even though it doesn't
1519 // seem to be documented anywhere.
1520 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001521 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001522 screen.saveCursorAndState(this.vt);
1523 };
1524 resetScreen(this.primaryScreen_);
1525 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001526
rgindab8bc8932012-04-27 12:45:03 -07001527 // The xterm man page explicitly says this will happen on soft reset.
1528 this.setVTScrollRegion(null, null);
1529
1530 // Xterm also shows the cursor on soft reset, but does not alter the blink
1531 // state.
rgindaa19afe22012-01-25 15:40:22 -08001532 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001533};
1534
rgindac9bc5502012-01-18 11:48:44 -08001535/**
1536 * Move the cursor forward to the next tab stop, or to the last column
1537 * if no more tab stops are set.
1538 */
1539hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001540 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001541
Mike Frysingerdc727792020-04-10 01:41:13 -04001542 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001543 if (this.tabStops_[i] > column) {
1544 this.setCursorColumn(this.tabStops_[i]);
1545 return;
1546 }
1547 }
1548
David Benjamin66e954d2012-05-05 21:08:12 -04001549 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001550 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001551 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001552 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001553};
1554
rgindac9bc5502012-01-18 11:48:44 -08001555/**
1556 * Move the cursor backward to the previous tab stop, or to the first column
1557 * if no previous tab stops are set.
1558 */
1559hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001560 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001561
Mike Frysingerdc727792020-04-10 01:41:13 -04001562 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001563 if (this.tabStops_[i] < column) {
1564 this.setCursorColumn(this.tabStops_[i]);
1565 return;
1566 }
1567 }
1568
1569 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001570};
1571
rgindac9bc5502012-01-18 11:48:44 -08001572/**
1573 * Set a tab stop at the given column.
1574 *
Joel Hockey0f933582019-08-27 18:01:51 -07001575 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001576 */
1577hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001578 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001579 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001580 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001581 }
rgindac9bc5502012-01-18 11:48:44 -08001582
1583 if (this.tabStops_[i] < column) {
1584 this.tabStops_.splice(i + 1, 0, column);
1585 return;
1586 }
1587 }
1588
1589 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001590};
1591
rgindac9bc5502012-01-18 11:48:44 -08001592/**
1593 * Clear the tab stop at the current cursor position.
1594 *
1595 * No effect if there is no tab stop at the current cursor position.
1596 */
1597hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001598 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001599
Mike Frysingerdc727792020-04-10 01:41:13 -04001600 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001601 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001602 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001603 }
rgindac9bc5502012-01-18 11:48:44 -08001604
1605 this.tabStops_.splice(i, 1);
1606};
1607
1608/**
1609 * Clear all tab stops.
1610 */
1611hterm.Terminal.prototype.clearAllTabStops = function() {
1612 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001613 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001614};
1615
1616/**
1617 * Set up the default tab stops, starting from a given column.
1618 *
1619 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001620 * from the specified column, or 0 if no column is provided. It also flags
1621 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001622 *
1623 * This does not clear the existing tab stops first, use clearAllTabStops
1624 * for that.
1625 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001626 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001627 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001628 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001629hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001630 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001631 // Round start up to a default tab stop.
1632 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001633 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001634 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001635 }
David Benjamin66e954d2012-05-05 21:08:12 -04001636
1637 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001638};
1639
rginda6d397402012-01-17 10:58:29 -08001640/**
rginda8ba33642011-12-14 12:31:31 -08001641 * Interpret a sequence of characters.
1642 *
1643 * Incomplete escape sequences are buffered until the next call.
1644 *
1645 * @param {string} str Sequence of characters to interpret or pass through.
1646 */
1647hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001648 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001649 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001650};
1651
1652/**
1653 * Take over the given DIV for use as the terminal display.
1654 *
Joel Hockey0f933582019-08-27 18:01:51 -07001655 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001656 */
1657hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001658 const charset = div.ownerDocument.characterSet.toLowerCase();
1659 if (charset != 'utf-8') {
1660 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1661 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1662 }
1663
rginda87b86462011-12-14 13:48:03 -08001664 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001665 this.div_.style.borderStyle = 'solid';
1666 this.div_.style.borderWidth = 0;
1667 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001668
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001669 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1670
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001671 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1672};
1673
1674/**
1675 * Initialisation of ScrollPort properties which need to be set after its DOM
1676 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001677 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001678 * @private
1679 */
1680hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001681 this.scrollPort_.setBackgroundImage(
1682 this.prefs_.getString('background-image'));
1683 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001684 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001685 this.prefs_.getString('background-position'));
1686 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1687 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1688 this.scrollPort_.setAccessibilityReader(
1689 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001690
rginda0918b652012-04-04 11:26:24 -07001691 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001692
Joel Hockeyd4fca732019-09-20 16:57:03 -07001693 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001694 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001695
Joel Hockeyd4fca732019-09-20 16:57:03 -07001696 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001697 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001698 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001699
rginda8ba33642011-12-14 12:31:31 -08001700 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001701 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001702
Evan Jones5f9df812016-12-06 09:38:58 -05001703 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001704 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001705
Mike Frysingerdc727792020-04-10 01:41:13 -04001706 const onMouse = this.onMouse_.bind(this);
1707 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001708 screenNode.addEventListener(
1709 'mousedown', /** @type {!EventListener} */ (onMouse));
1710 screenNode.addEventListener(
1711 'mouseup', /** @type {!EventListener} */ (onMouse));
1712 screenNode.addEventListener(
1713 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001714 this.scrollPort_.onScrollWheel = onMouse;
1715
Joel Hockeyd4fca732019-09-20 16:57:03 -07001716 screenNode.addEventListener(
1717 'keydown',
1718 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001719
Toni Barzic0bfa8922013-11-22 11:18:35 -08001720 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001721 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001722 // Listen for mousedown events on the screenNode as in FF the focus
1723 // events don't bubble.
1724 screenNode.addEventListener('mousedown', function() {
1725 setTimeout(this.onFocusChange_.bind(this, true));
1726 }.bind(this));
1727
Toni Barzic0bfa8922013-11-22 11:18:35 -08001728 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001729 'blur', this.onFocusChange_.bind(this, false));
1730
Mike Frysingerdc727792020-04-10 01:41:13 -04001731 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001732 style.textContent = `
1733.cursor-node[focus="false"] {
1734 box-sizing: border-box;
1735 background-color: transparent !important;
1736 border-width: 2px;
1737 border-style: solid;
1738}
1739menu {
1740 margin: 0;
1741 padding: 0;
1742 cursor: var(--hterm-mouse-cursor-pointer);
1743}
1744menuitem {
1745 white-space: nowrap;
1746 border-bottom: 1px dashed;
1747 display: block;
1748 padding: 0.3em 0.3em 0 0.3em;
1749}
1750menuitem.separator {
1751 border-bottom: none;
1752 height: 0.5em;
1753 padding: 0;
1754}
1755menuitem:hover {
1756 color: var(--hterm-cursor-color);
1757}
1758.wc-node {
1759 display: inline-block;
1760 text-align: center;
1761 width: calc(var(--hterm-charsize-width) * 2);
1762 line-height: var(--hterm-charsize-height);
1763}
1764:root {
1765 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1766 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001767 --hterm-blink-node-duration: 0.7s;
1768 --hterm-mouse-cursor-default: default;
1769 --hterm-mouse-cursor-text: text;
1770 --hterm-mouse-cursor-pointer: pointer;
1771 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001772 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001773
Joel Hockey42dba8f2020-03-26 16:21:11 -07001774${lib.colors.stockColorPalette.map((c, i) => `
1775 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1776`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001777}
1778.uri-node:hover {
1779 text-decoration: underline;
1780 cursor: var(--hterm-mouse-cursor-pointer);
1781}
1782@keyframes blink {
1783 from { opacity: 1.0; }
1784 to { opacity: 0.0; }
1785}
1786.blink-node {
1787 animation-name: blink;
1788 animation-duration: var(--hterm-blink-node-duration);
1789 animation-iteration-count: infinite;
1790 animation-timing-function: ease-in-out;
1791 animation-direction: alternate;
1792}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001793 // Insert this stock style as the first node so that any user styles will
1794 // override w/out having to use !important everywhere. The rules above mix
1795 // runtime variables with default ones designed to be overridden by the user,
1796 // but we can wait for a concrete case from the users to determine the best
1797 // way to split the sheet up to before & after the user-css settings.
1798 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001799
rginda8ba33642011-12-14 12:31:31 -08001800 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001801 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001802 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001803 this.cursorNode_.style.cssText = `
1804position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001805left: calc(var(--hterm-screen-padding-size) +
1806 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1807top: calc(var(--hterm-screen-padding-size) +
1808 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001809display: ${this.options_.cursorVisible ? '' : 'none'};
1810width: var(--hterm-charsize-width);
1811height: var(--hterm-charsize-height);
1812background-color: var(--hterm-cursor-color);
1813border-color: var(--hterm-cursor-color);
1814-webkit-transition: opacity, background-color 100ms linear;
1815-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001816
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001817 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001818 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1819 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001820
rginda8ba33642011-12-14 12:31:31 -08001821 this.document_.body.appendChild(this.cursorNode_);
1822
rgindad5613292012-06-19 15:40:37 -07001823 // When 'enableMouseDragScroll' is off we reposition this element directly
1824 // under the mouse cursor after a click. This makes Chrome associate
1825 // subsequent mousemove events with the scroll-blocker. Since the
1826 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1827 // events do not cause the scrollport to scroll.
1828 //
1829 // It's a hack, but it's the cleanest way I could find.
1830 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001831 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001832 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001833 this.scrollBlockerNode_.style.cssText =
1834 ('position: absolute;' +
1835 'top: -99px;' +
1836 'display: block;' +
1837 'width: 10px;' +
1838 'height: 10px;');
1839 this.document_.body.appendChild(this.scrollBlockerNode_);
1840
rgindad5613292012-06-19 15:40:37 -07001841 this.scrollPort_.onScrollWheel = onMouse;
1842 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1843 ].forEach(function(event) {
1844 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001845 this.cursorNode_.addEventListener(
1846 event, /** @type {!EventListener} */ (onMouse));
1847 this.document_.addEventListener(
1848 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001849 }.bind(this));
1850
1851 this.cursorNode_.addEventListener('mousedown', function() {
1852 setTimeout(this.focus.bind(this));
1853 }.bind(this));
1854
rginda8ba33642011-12-14 12:31:31 -08001855 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001856
rginda87b86462011-12-14 13:48:03 -08001857 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001858 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001859};
1860
rginda0918b652012-04-04 11:26:24 -07001861/**
1862 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001863 *
Joel Hockey0f933582019-08-27 18:01:51 -07001864 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001865 */
rginda87b86462011-12-14 13:48:03 -08001866hterm.Terminal.prototype.getDocument = function() {
1867 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001868};
1869
1870/**
rginda0918b652012-04-04 11:26:24 -07001871 * Focus the terminal.
1872 */
1873hterm.Terminal.prototype.focus = function() {
1874 this.scrollPort_.focus();
1875};
1876
1877/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001878 * Unfocus the terminal.
1879 */
1880hterm.Terminal.prototype.blur = function() {
1881 this.scrollPort_.blur();
1882};
1883
1884/**
rginda8ba33642011-12-14 12:31:31 -08001885 * Return the HTML Element for a given row index.
1886 *
1887 * This is a method from the RowProvider interface. The ScrollPort uses
1888 * it to fetch rows on demand as they are scrolled into view.
1889 *
1890 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1891 * pairs to conserve memory.
1892 *
Joel Hockey0f933582019-08-27 18:01:51 -07001893 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001894 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001895 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001896 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001897 * @override
rginda8ba33642011-12-14 12:31:31 -08001898 */
1899hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001900 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001901 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001902 }
rginda8ba33642011-12-14 12:31:31 -08001903
Mike Frysingerdc727792020-04-10 01:41:13 -04001904 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001905 return this.screen_.rowsArray[screenIndex];
1906};
1907
1908/**
1909 * Return the text content for a given range of rows.
1910 *
1911 * This is a method from the RowProvider interface. The ScrollPort uses
1912 * it to fetch text content on demand when the user attempts to copy their
1913 * selection to the clipboard.
1914 *
Joel Hockey0f933582019-08-27 18:01:51 -07001915 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001916 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001917 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001918 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001919 * relative to the start of the scrollback buffer.
1920 * @return {string} A single string containing the text value of the range of
1921 * rows. Lines will be newline delimited, with no trailing newline.
1922 */
1923hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001924 const ary = [];
1925 for (let i = start; i < end; i++) {
1926 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001927 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001928 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001929 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001930 }
rginda8ba33642011-12-14 12:31:31 -08001931 }
1932
rgindaa09e7332012-08-17 12:49:51 -07001933 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001934};
1935
1936/**
1937 * Return the text content for a given row.
1938 *
1939 * This is a method from the RowProvider interface. The ScrollPort uses
1940 * it to fetch text content on demand when the user attempts to copy their
1941 * selection to the clipboard.
1942 *
Joel Hockey0f933582019-08-27 18:01:51 -07001943 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001944 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001945 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001946 * @return {string} A string containing the text value of the selected row.
1947 */
1948hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001949 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001950 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001951};
1952
1953/**
1954 * Return the total number of rows in the addressable screen and in the
1955 * scrollback buffer of this terminal.
1956 *
1957 * This is a method from the RowProvider interface. The ScrollPort uses
1958 * it to compute the size of the scrollbar.
1959 *
Joel Hockey0f933582019-08-27 18:01:51 -07001960 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001961 * @override
rginda8ba33642011-12-14 12:31:31 -08001962 */
1963hterm.Terminal.prototype.getRowCount = function() {
1964 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1965};
1966
1967/**
1968 * Create DOM nodes for new rows and append them to the end of the terminal.
1969 *
1970 * This is the only correct way to add a new DOM node for a row. Notice that
1971 * the new row is appended to the bottom of the list of rows, and does not
1972 * require renumbering (of the rowIndex property) of previous rows.
1973 *
1974 * If you think you want a new blank row somewhere in the middle of the
1975 * terminal, look into moveRows_().
1976 *
1977 * This method does not pay attention to vtScrollTop/Bottom, since you should
1978 * be using moveRows() in cases where they would matter.
1979 *
1980 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001981 *
1982 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001983 */
1984hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001985 let cursorRow = this.screen_.rowsArray.length;
1986 const offset = this.scrollbackRows_.length + cursorRow;
1987 for (let i = 0; i < count; i++) {
1988 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08001989 row.appendChild(this.document_.createTextNode(''));
1990 row.rowIndex = offset + i;
1991 this.screen_.pushRow(row);
1992 }
1993
Mike Frysingerdc727792020-04-10 01:41:13 -04001994 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001995 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001996 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08001997 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001998 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07001999 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002000 }
rginda8ba33642011-12-14 12:31:31 -08002001 }
2002
Mike Frysingerbdb34802020-04-07 03:47:32 -04002003 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002004 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002005 }
rginda8ba33642011-12-14 12:31:31 -08002006
rginda87b86462011-12-14 13:48:03 -08002007 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002008};
2009
2010/**
2011 * Relocate rows from one part of the addressable screen to another.
2012 *
2013 * This is used to recycle rows during VT scrolls (those which are driven
2014 * by VT commands, rather than by the user manipulating the scrollbar.)
2015 *
2016 * In this case, the blank lines scrolled into the scroll region are made of
2017 * the nodes we scrolled off. These have their rowIndex properties carefully
2018 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002019 *
2020 * @param {number} fromIndex The start index.
2021 * @param {number} count The number of rows to move.
2022 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002023 */
2024hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002025 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002026 this.screen_.insertRows(toIndex, ary);
2027
Mike Frysingerdc727792020-04-10 01:41:13 -04002028 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002029 if (fromIndex < toIndex) {
2030 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002031 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002032 } else {
2033 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002034 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002035 }
2036
2037 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002038 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002039};
2040
2041/**
2042 * Renumber the rowIndex property of the given range of rows.
2043 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002044 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002045 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002046 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002047 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002048 *
2049 * @param {number} start The start index.
2050 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002051 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002052 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002053hterm.Terminal.prototype.renumberRows_ = function(
2054 start, end, screen = undefined) {
2055 if (!screen) {
2056 screen = this.screen_;
2057 }
Robert Ginda40932892012-12-10 17:26:40 -08002058
Mike Frysingerdc727792020-04-10 01:41:13 -04002059 const offset = this.scrollbackRows_.length;
2060 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002061 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002062 }
2063};
2064
2065/**
2066 * Print a string to the terminal.
2067 *
2068 * This respects the current insert and wraparound modes. It will add new lines
2069 * to the end of the terminal, scrolling off the top into the scrollback buffer
2070 * if necessary.
2071 *
2072 * The string is *not* parsed for escape codes. Use the interpret() method if
2073 * that's what you're after.
2074 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002075 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002076 */
2077hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002078 this.scheduleSyncCursorPosition_();
2079
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002080 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002081 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002082
Mike Frysingerdc727792020-04-10 01:41:13 -04002083 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002084
Mike Frysingerdc727792020-04-10 01:41:13 -04002085 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002086 // Fun edge case: If the string only contains zero width codepoints (like
2087 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002088 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002089 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002090 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002091
2092 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002093 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2094 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002095 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002096 }
rgindaa19afe22012-01-25 15:40:22 -08002097
Mike Frysingerdc727792020-04-10 01:41:13 -04002098 let count = strWidth - startOffset;
2099 let didOverflow = false;
2100 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002101
rgindaa9abdd82012-08-06 18:05:09 -07002102 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2103 didOverflow = true;
2104 count = this.screenSize.width - this.screen_.cursorPosition.column;
2105 }
rgindaa19afe22012-01-25 15:40:22 -08002106
rgindaa9abdd82012-08-06 18:05:09 -07002107 if (didOverflow && !this.options_.wraparound) {
2108 // If the string overflowed the line but wraparound is off, then the
2109 // last printed character should be the last of the string.
2110 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002111 substr = lib.wc.substr(str, startOffset, count - 1) +
2112 lib.wc.substr(str, strWidth - 1);
2113 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002114 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002115 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002116 }
rgindaa19afe22012-01-25 15:40:22 -08002117
Mike Frysingerdc727792020-04-10 01:41:13 -04002118 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2119 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002120 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2121 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002122
2123 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002124 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002125 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002126 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002127 }
2128 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002129 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002130 }
2131
2132 this.screen_.maybeClipCurrentRow();
2133 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002134 }
rginda8ba33642011-12-14 12:31:31 -08002135
Mike Frysingerbdb34802020-04-07 03:47:32 -04002136 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002137 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002138 }
rginda8ba33642011-12-14 12:31:31 -08002139};
2140
2141/**
rginda87b86462011-12-14 13:48:03 -08002142 * Set the VT scroll region.
2143 *
rginda87b86462011-12-14 13:48:03 -08002144 * This also resets the cursor position to the absolute (0, 0) position, since
2145 * that's what xterm appears to do.
2146 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002147 * Setting the scroll region to the full height of the terminal will clear
2148 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2149 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2150 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2151 * continue to work as most users would expect.
2152 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002153 * @param {?number} scrollTop The zero-based top of the scroll region.
2154 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002155 * inclusive.
2156 */
2157hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002158 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002159 this.vtScrollTop_ = null;
2160 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002161 } else {
2162 this.vtScrollTop_ = scrollTop;
2163 this.vtScrollBottom_ = scrollBottom;
2164 }
rginda87b86462011-12-14 13:48:03 -08002165};
2166
2167/**
rginda8ba33642011-12-14 12:31:31 -08002168 * Return the top row index according to the VT.
2169 *
2170 * This will return 0 unless the terminal has been told to restrict scrolling
2171 * to some lower row. It is used for some VT cursor positioning and scrolling
2172 * commands.
2173 *
Joel Hockey0f933582019-08-27 18:01:51 -07002174 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002175 */
2176hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002177 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002178 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002179 }
rginda8ba33642011-12-14 12:31:31 -08002180
2181 return 0;
rginda87b86462011-12-14 13:48:03 -08002182};
rginda8ba33642011-12-14 12:31:31 -08002183
2184/**
2185 * Return the bottom row index according to the VT.
2186 *
2187 * This will return the height of the terminal unless the it has been told to
2188 * restrict scrolling to some higher row. It is used for some VT cursor
2189 * positioning and scrolling commands.
2190 *
Joel Hockey0f933582019-08-27 18:01:51 -07002191 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002192 */
2193hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002194 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002195 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002196 }
rginda8ba33642011-12-14 12:31:31 -08002197
rginda87b86462011-12-14 13:48:03 -08002198 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002199};
rginda8ba33642011-12-14 12:31:31 -08002200
2201/**
2202 * Process a '\n' character.
2203 *
2204 * If the cursor is on the final row of the terminal this will append a new
2205 * blank row to the screen and scroll the topmost row into the scrollback
2206 * buffer.
2207 *
2208 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002209 *
2210 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2211 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002212 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002213hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002214 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002215 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002216 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002217
Mike Frysingerdc727792020-04-10 01:41:13 -04002218 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2219 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002220
2221 if (this.vtScrollBottom_ != null) {
2222 // A VT Scroll region is active, we never append new rows.
2223 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2224 // We're at the end of the VT Scroll Region, perform a VT scroll.
2225 this.vtScrollUp(1);
2226 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2227 } else if (cursorAtEndOfScreen) {
2228 // We're at the end of the screen, the only thing to do is put the
2229 // cursor to column 0.
2230 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2231 } else {
2232 // Anywhere else, advance the cursor row, and reset the column.
2233 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2234 }
2235 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002236 // We're at the end of the screen. Append a new row to the terminal,
2237 // shifting the top row into the scrollback.
2238 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002239 } else {
rginda87b86462011-12-14 13:48:03 -08002240 // Anywhere else in the screen just moves the cursor.
2241 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002242 }
2243};
2244
2245/**
2246 * Like newLine(), except maintain the cursor column.
2247 */
2248hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002249 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002250 this.newLine();
2251 this.setCursorColumn(column);
2252};
2253
2254/**
rginda87b86462011-12-14 13:48:03 -08002255 * If autoCarriageReturn is set then newLine(), else lineFeed().
2256 */
2257hterm.Terminal.prototype.formFeed = function() {
2258 if (this.options_.autoCarriageReturn) {
2259 this.newLine();
2260 } else {
2261 this.lineFeed();
2262 }
2263};
2264
2265/**
2266 * Move the cursor up one row, possibly inserting a blank line.
2267 *
2268 * The cursor column is not changed.
2269 */
2270hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002271 const scrollTop = this.getVTScrollTop();
2272 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002273
2274 if (currentRow == scrollTop) {
2275 this.insertLines(1);
2276 } else {
2277 this.setAbsoluteCursorRow(currentRow - 1);
2278 }
2279};
2280
2281/**
rginda8ba33642011-12-14 12:31:31 -08002282 * Replace all characters to the left of the current cursor with the space
2283 * character.
2284 *
2285 * TODO(rginda): This should probably *remove* the characters (not just replace
2286 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002287 * position.
rginda8ba33642011-12-14 12:31:31 -08002288 */
2289hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002290 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002291 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002292 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002293 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002294 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002295};
2296
2297/**
David Benjamin684a9b72012-05-01 17:19:58 -04002298 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002299 *
2300 * The cursor position is unchanged.
2301 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002302 * If the current background color is not the default background color this
2303 * will insert spaces rather than delete. This is unfortunate because the
2304 * trailing space will affect text selection, but it's difficult to come up
2305 * with a way to style empty space that wouldn't trip up the hterm.Screen
2306 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002307 *
2308 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2309 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2310 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002311 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002312 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002313 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002314hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002315 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002316 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002317 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002318
Mike Frysingerdc727792020-04-10 01:41:13 -04002319 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002320 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002321
2322 if (this.screen_.textAttributes.background ===
2323 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002324 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002325 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002326 this.screen_.cursorPosition.column + count) {
2327 this.screen_.deleteChars(count);
2328 this.clearCursorOverflow();
2329 return;
2330 }
2331 }
2332
Mike Frysingerdc727792020-04-10 01:41:13 -04002333 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002334 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002335 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002336 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002337};
2338
2339/**
2340 * Erase the current line.
2341 *
2342 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002343 */
2344hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002345 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002346 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002347 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002348 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002349};
2350
2351/**
David Benjamina08d78f2012-05-05 00:28:49 -04002352 * Erase all characters from the start of the screen to the current cursor
2353 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002354 *
2355 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002356 */
2357hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002358 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002359
2360 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002361
Mike Frysingerdc727792020-04-10 01:41:13 -04002362 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002363 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002364 this.screen_.clearCursorRow();
2365 }
2366
rginda87b86462011-12-14 13:48:03 -08002367 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002368 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002369};
2370
2371/**
2372 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002373 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002374 *
2375 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002376 */
2377hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002378 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002379
2380 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002381
Mike Frysingerdc727792020-04-10 01:41:13 -04002382 const bottom = this.screenSize.height - 1;
2383 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002384 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002385 this.screen_.clearCursorRow();
2386 }
2387
rginda87b86462011-12-14 13:48:03 -08002388 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002389 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002390};
2391
2392/**
2393 * Fill the terminal with a given character.
2394 *
2395 * This methods does not respect the VT scroll region.
2396 *
2397 * @param {string} ch The character to use for the fill.
2398 */
2399hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002400 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002401
2402 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002403 for (let row = 0; row < this.screenSize.height; row++) {
2404 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002405 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002406 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002407 }
2408 }
2409
2410 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002411};
2412
2413/**
rginda9ea433c2012-03-16 11:57:00 -07002414 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002415 *
rginda9ea433c2012-03-16 11:57:00 -07002416 * This does not respect the scroll region.
2417 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002418 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002419 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002420 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002421hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2422 if (!screen) {
2423 screen = this.screen_;
2424 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002425 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002426
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002427 this.accessibilityReader_.clear();
2428
rginda11057d52012-04-25 12:29:56 -07002429 if (bottom == 0) {
2430 // Empty screen, nothing to do.
2431 return;
2432 }
2433
Mike Frysingerdc727792020-04-10 01:41:13 -04002434 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002435 screen.setCursorPosition(i, 0);
2436 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002437 }
2438
rginda9ea433c2012-03-16 11:57:00 -07002439 screen.setCursorPosition(0, 0);
2440};
2441
2442/**
2443 * Erase the entire display without changing the cursor position.
2444 *
2445 * The cursor position is unchanged. This does not respect the scroll
2446 * region.
2447 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002448 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002449 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002450 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002451hterm.Terminal.prototype.clear = function(screen = undefined) {
2452 if (!screen) {
2453 screen = this.screen_;
2454 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002455 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002456 this.clearHome(screen);
2457 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002458};
2459
2460/**
2461 * VT command to insert lines at the current cursor row.
2462 *
2463 * This respects the current scroll region. Rows pushed off the bottom are
2464 * lost (they won't show up in the scrollback buffer).
2465 *
Joel Hockey0f933582019-08-27 18:01:51 -07002466 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002467 */
2468hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002469 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002470
Mike Frysingerdc727792020-04-10 01:41:13 -04002471 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002472 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002473
Robert Ginda579186b2012-09-26 11:40:04 -07002474 // The moveCount is the number of rows we need to relocate to make room for
2475 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002476 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002477 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002478 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002479 }
rginda8ba33642011-12-14 12:31:31 -08002480
Mike Frysingerdc727792020-04-10 01:41:13 -04002481 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002482 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002483 this.screen_.clearCursorRow();
2484 }
rginda8ba33642011-12-14 12:31:31 -08002485};
2486
2487/**
2488 * VT command to delete lines at the current cursor row.
2489 *
2490 * New rows are added to the bottom of scroll region to take their place. New
2491 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002492 *
2493 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002494 */
2495hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002496 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002497
Mike Frysingerdc727792020-04-10 01:41:13 -04002498 const top = cursor.row;
2499 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002500
Mike Frysingerdc727792020-04-10 01:41:13 -04002501 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002502 count = Math.min(count, maxCount);
2503
Mike Frysingerdc727792020-04-10 01:41:13 -04002504 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002505 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002506 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002507 }
rginda8ba33642011-12-14 12:31:31 -08002508
Mike Frysingerdc727792020-04-10 01:41:13 -04002509 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002510 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002511 this.screen_.clearCursorRow();
2512 }
2513
rginda87b86462011-12-14 13:48:03 -08002514 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002515 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002516};
2517
2518/**
2519 * Inserts the given number of spaces at the current cursor position.
2520 *
rginda87b86462011-12-14 13:48:03 -08002521 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002522 *
2523 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002524 */
2525hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002526 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002527
Mike Frysinger73e56462019-07-17 00:23:46 -05002528 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002529 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002530 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002531
2532 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002533 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002534};
2535
2536/**
2537 * Forward-delete the specified number of characters starting at the cursor
2538 * position.
2539 *
Joel Hockey0f933582019-08-27 18:01:51 -07002540 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002541 */
2542hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002543 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002544 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002545 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002546 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002547 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002548 this.restoreCursor(cursor);
2549 }
2550
David Benjamin54e8bf62012-06-01 22:31:40 -04002551 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002552};
2553
2554/**
2555 * Shift rows in the scroll region upwards by a given number of lines.
2556 *
2557 * New rows are inserted at the bottom of the scroll region to fill the
2558 * vacated rows. The new rows not filled out with the current text attributes.
2559 *
2560 * This function does not affect the scrollback rows at all. Rows shifted
2561 * off the top are lost.
2562 *
rginda87b86462011-12-14 13:48:03 -08002563 * The cursor position is not altered.
2564 *
Joel Hockey0f933582019-08-27 18:01:51 -07002565 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002566 */
2567hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002568 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002569
rginda87b86462011-12-14 13:48:03 -08002570 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002571 this.deleteLines(count);
2572
rginda87b86462011-12-14 13:48:03 -08002573 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002574};
2575
2576/**
2577 * Shift rows below the cursor down by a given number of lines.
2578 *
2579 * This function respects the current scroll region.
2580 *
2581 * New rows are inserted at the top of the scroll region to fill the
2582 * vacated rows. The new rows not filled out with the current text attributes.
2583 *
2584 * This function does not affect the scrollback rows at all. Rows shifted
2585 * off the bottom are lost.
2586 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002587 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002588 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002589hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002590 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002591
rginda87b86462011-12-14 13:48:03 -08002592 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002593 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002594
rginda87b86462011-12-14 13:48:03 -08002595 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002596};
2597
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002598/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002599 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002600 *
2601 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002602 * cause Assitive Technology to announce the output of the terminal. It also
2603 * enables other features that aid assistive technology. All the features gated
2604 * behind this flag have a performance impact on the terminal which is why they
2605 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002606 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002607 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002608 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002609hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002610 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002611};
rginda87b86462011-12-14 13:48:03 -08002612
rginda8ba33642011-12-14 12:31:31 -08002613/**
2614 * Set the cursor position.
2615 *
2616 * The cursor row is relative to the scroll region if the terminal has
2617 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2618 *
Joel Hockey0f933582019-08-27 18:01:51 -07002619 * @param {number} row The new zero-based cursor row.
2620 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002621 */
2622hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2623 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002624 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002625 } else {
rginda87b86462011-12-14 13:48:03 -08002626 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002627 }
rginda87b86462011-12-14 13:48:03 -08002628};
rginda8ba33642011-12-14 12:31:31 -08002629
Evan Jones2600d4f2016-12-06 09:29:36 -05002630/**
2631 * Move the cursor relative to its current position.
2632 *
2633 * @param {number} row
2634 * @param {number} column
2635 */
rginda87b86462011-12-14 13:48:03 -08002636hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002637 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002638 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2639 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002640 this.screen_.setCursorPosition(row, column);
2641};
2642
Evan Jones2600d4f2016-12-06 09:29:36 -05002643/**
2644 * Move the cursor to the specified position.
2645 *
2646 * @param {number} row
2647 * @param {number} column
2648 */
rginda87b86462011-12-14 13:48:03 -08002649hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002650 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2651 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002652 this.screen_.setCursorPosition(row, column);
2653};
2654
2655/**
2656 * Set the cursor column.
2657 *
Joel Hockey0f933582019-08-27 18:01:51 -07002658 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002659 */
2660hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002661 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002662};
2663
2664/**
2665 * Return the cursor column.
2666 *
Joel Hockey0f933582019-08-27 18:01:51 -07002667 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002668 */
2669hterm.Terminal.prototype.getCursorColumn = function() {
2670 return this.screen_.cursorPosition.column;
2671};
2672
2673/**
2674 * Set the cursor row.
2675 *
2676 * The cursor row is relative to the scroll region if the terminal has
2677 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2678 *
Joel Hockey0f933582019-08-27 18:01:51 -07002679 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002680 */
rginda87b86462011-12-14 13:48:03 -08002681hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2682 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002683};
2684
2685/**
2686 * Return the cursor row.
2687 *
Joel Hockey0f933582019-08-27 18:01:51 -07002688 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002689 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002690hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002691 return this.screen_.cursorPosition.row;
2692};
2693
2694/**
2695 * Request that the ScrollPort redraw itself soon.
2696 *
2697 * The redraw will happen asynchronously, soon after the call stack winds down.
2698 * Multiple calls will be coalesced into a single redraw.
2699 */
2700hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002701 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002702 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002703 }
rginda8ba33642011-12-14 12:31:31 -08002704
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002705 this.timeouts_.redraw = setTimeout(() => {
2706 delete this.timeouts_.redraw;
2707 this.scrollPort_.redraw_();
2708 });
rginda8ba33642011-12-14 12:31:31 -08002709};
2710
2711/**
2712 * Request that the ScrollPort be scrolled to the bottom.
2713 *
2714 * The scroll will happen asynchronously, soon after the call stack winds down.
2715 * Multiple calls will be coalesced into a single scroll.
2716 *
2717 * This affects the scrollbar position of the ScrollPort, and has nothing to
2718 * do with the VT scroll commands.
2719 */
2720hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002721 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002722 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002723 }
rginda8ba33642011-12-14 12:31:31 -08002724
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002725 this.timeouts_.scrollDown = setTimeout(() => {
2726 delete this.timeouts_.scrollDown;
2727 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2728 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002729};
2730
2731/**
2732 * Move the cursor up a specified number of rows.
2733 *
Joel Hockey0f933582019-08-27 18:01:51 -07002734 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002735 */
2736hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002737 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002738};
2739
2740/**
2741 * Move the cursor down a specified number of rows.
2742 *
Joel Hockey0f933582019-08-27 18:01:51 -07002743 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002744 */
2745hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002746 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002747 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2748 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2749 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002750
Mike Frysingerdc727792020-04-10 01:41:13 -04002751 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2752 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002753 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002754};
2755
2756/**
2757 * Move the cursor left a specified number of columns.
2758 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002759 * If reverse wraparound mode is enabled and the previous row wrapped into
2760 * the current row then we back up through the wraparound as well.
2761 *
Joel Hockey0f933582019-08-27 18:01:51 -07002762 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002763 */
2764hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002765 count = count || 1;
2766
Mike Frysingerbdb34802020-04-07 03:47:32 -04002767 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002768 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002769 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002770
Mike Frysingerdc727792020-04-10 01:41:13 -04002771 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002772 if (this.options_.reverseWraparound) {
2773 if (this.screen_.cursorPosition.overflow) {
2774 // If this cursor is in the right margin, consume one count to get it
2775 // back to the last column. This only applies when we're in reverse
2776 // wraparound mode.
2777 count--;
2778 this.clearCursorOverflow();
2779
Mike Frysingerbdb34802020-04-07 03:47:32 -04002780 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002781 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002782 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002783 }
2784
Mike Frysingerdc727792020-04-10 01:41:13 -04002785 let newRow = this.screen_.cursorPosition.row;
2786 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002787 if (newColumn < 0) {
2788 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2789 if (newRow < 0) {
2790 // xterm also wraps from row 0 to the last row.
2791 newRow = this.screenSize.height + newRow % this.screenSize.height;
2792 }
2793 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2794 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002795
Robert Gindabfb32622014-07-17 13:20:27 -07002796 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2797
2798 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002799 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002800 this.setCursorColumn(newColumn);
2801 }
rginda8ba33642011-12-14 12:31:31 -08002802};
2803
2804/**
2805 * Move the cursor right a specified number of columns.
2806 *
Joel Hockey0f933582019-08-27 18:01:51 -07002807 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002808 */
2809hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002810 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002811
Mike Frysingerbdb34802020-04-07 03:47:32 -04002812 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002813 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002814 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002815
Mike Frysingerdc727792020-04-10 01:41:13 -04002816 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2817 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002818 this.setCursorColumn(column);
2819};
2820
2821/**
2822 * Reverse the foreground and background colors of the terminal.
2823 *
2824 * This only affects text that was drawn with no attributes.
2825 *
2826 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2827 * been drawn with attributes that happen to coincide with the default
2828 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002829 *
2830 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002831 */
2832hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002833 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002834 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002835 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2836 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002837 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002838 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2839 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002840 }
2841};
2842
2843/**
rginda87b86462011-12-14 13:48:03 -08002844 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002845 *
2846 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002847 */
2848hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002849 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002850
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002851 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002852
Michael Kelly485ecd12014-06-09 11:41:56 -04002853 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002854 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002855 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002856 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002857
Robert Ginda92e18102013-03-14 13:56:37 -07002858 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002859 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002860 this.bellSequelchTimeout_ = setTimeout(() => {
2861 this.bellSquelchTimeout_ = null;
2862 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002863 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002864 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002865 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002866
2867 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002868 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002869 this.bellNotificationList_.push(n);
2870 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002871 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002872 }
rginda87b86462011-12-14 13:48:03 -08002873};
2874
2875/**
rginda8ba33642011-12-14 12:31:31 -08002876 * Set the origin mode bit.
2877 *
2878 * If origin mode is on, certain VT cursor and scrolling commands measure their
2879 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2880 * to the top of the addressable screen.
2881 *
2882 * Defaults to off.
2883 *
2884 * @param {boolean} state True to set origin mode, false to unset.
2885 */
2886hterm.Terminal.prototype.setOriginMode = function(state) {
2887 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002888 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002889};
2890
2891/**
2892 * Set the insert mode bit.
2893 *
2894 * If insert mode is on, existing text beyond the cursor position will be
2895 * shifted right to make room for new text. Otherwise, new text overwrites
2896 * any existing text.
2897 *
2898 * Defaults to off.
2899 *
2900 * @param {boolean} state True to set insert mode, false to unset.
2901 */
2902hterm.Terminal.prototype.setInsertMode = function(state) {
2903 this.options_.insertMode = state;
2904};
2905
2906/**
rginda87b86462011-12-14 13:48:03 -08002907 * Set the auto carriage return bit.
2908 *
2909 * If auto carriage return is on then a formfeed character is interpreted
2910 * as a newline, otherwise it's the same as a linefeed. The difference boils
2911 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002912 *
2913 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002914 */
2915hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2916 this.options_.autoCarriageReturn = state;
2917};
2918
2919/**
rginda8ba33642011-12-14 12:31:31 -08002920 * Set the wraparound mode bit.
2921 *
2922 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2923 * to the start of the following row. Otherwise, the cursor is clamped to the
2924 * end of the screen and attempts to write past it are ignored.
2925 *
2926 * Defaults to on.
2927 *
2928 * @param {boolean} state True to set wraparound mode, false to unset.
2929 */
2930hterm.Terminal.prototype.setWraparound = function(state) {
2931 this.options_.wraparound = state;
2932};
2933
2934/**
2935 * Set the reverse-wraparound mode bit.
2936 *
2937 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2938 * to the end of the previous row. Otherwise, the cursor is clamped to column
2939 * 0.
2940 *
2941 * Defaults to off.
2942 *
2943 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2944 */
2945hterm.Terminal.prototype.setReverseWraparound = function(state) {
2946 this.options_.reverseWraparound = state;
2947};
2948
2949/**
2950 * Selects between the primary and alternate screens.
2951 *
2952 * If alternate mode is on, the alternate screen is active. Otherwise the
2953 * primary screen is active.
2954 *
2955 * Swapping screens has no effect on the scrollback buffer.
2956 *
2957 * Each screen maintains its own cursor position.
2958 *
2959 * Defaults to off.
2960 *
2961 * @param {boolean} state True to set alternate mode, false to unset.
2962 */
2963hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002964 if (state == (this.screen_ == this.alternateScreen_)) {
2965 return;
2966 }
2967 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2968 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002969 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2970
Joel Hockey42dba8f2020-03-26 16:21:11 -07002971 // Swap color overrides.
2972 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2973 oldOverrides.forEach((c, i) => {
2974 if (!newOverrides.hasOwnProperty(i)) {
2975 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2976 }
2977 });
2978 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2979
rginda35c456b2012-02-09 17:29:05 -08002980 if (this.screen_.rowsArray.length &&
2981 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2982 // If the screen changed sizes while we were away, our rowIndexes may
2983 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002984 const offset = this.scrollbackRows_.length;
2985 const ary = this.screen_.rowsArray;
2986 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002987 ary[i].rowIndex = offset + i;
2988 }
2989 }
rginda8ba33642011-12-14 12:31:31 -08002990
rginda35c456b2012-02-09 17:29:05 -08002991 this.realizeWidth_(this.screenSize.width);
2992 this.realizeHeight_(this.screenSize.height);
2993 this.scrollPort_.syncScrollHeight();
2994 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002995
rginda6d397402012-01-17 10:58:29 -08002996 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002997 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002998};
2999
3000/**
3001 * Set the cursor-blink mode bit.
3002 *
3003 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3004 * a visible cursor does not blink.
3005 *
3006 * You should make sure to turn blinking off if you're going to dispose of a
3007 * terminal, otherwise you'll leak a timeout.
3008 *
3009 * Defaults to on.
3010 *
3011 * @param {boolean} state True to set cursor-blink mode, false to unset.
3012 */
3013hterm.Terminal.prototype.setCursorBlink = function(state) {
3014 this.options_.cursorBlink = state;
3015
3016 if (!state && this.timeouts_.cursorBlink) {
3017 clearTimeout(this.timeouts_.cursorBlink);
3018 delete this.timeouts_.cursorBlink;
3019 }
3020
Mike Frysingerbdb34802020-04-07 03:47:32 -04003021 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003022 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003023 }
rginda8ba33642011-12-14 12:31:31 -08003024};
3025
3026/**
3027 * Set the cursor-visible mode bit.
3028 *
3029 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3030 *
3031 * Defaults to on.
3032 *
3033 * @param {boolean} state True to set cursor-visible mode, false to unset.
3034 */
3035hterm.Terminal.prototype.setCursorVisible = function(state) {
3036 this.options_.cursorVisible = state;
3037
3038 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003039 if (this.timeouts_.cursorBlink) {
3040 clearTimeout(this.timeouts_.cursorBlink);
3041 delete this.timeouts_.cursorBlink;
3042 }
rginda87b86462011-12-14 13:48:03 -08003043 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003044 return;
3045 }
3046
rginda87b86462011-12-14 13:48:03 -08003047 this.syncCursorPosition_();
3048
3049 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003050
3051 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003052 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003053 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003054 }
rginda8ba33642011-12-14 12:31:31 -08003055
Robert Gindaea2183e2014-07-17 09:51:51 -07003056 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003057 } else {
3058 if (this.timeouts_.cursorBlink) {
3059 clearTimeout(this.timeouts_.cursorBlink);
3060 delete this.timeouts_.cursorBlink;
3061 }
3062 }
3063};
3064
3065/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003066 * Pause blinking temporarily.
3067 *
3068 * When the cursor moves around, it can be helpful to momentarily pause the
3069 * blinking. This could be when the user is typing in things, or when they're
3070 * moving around with the arrow keys.
3071 */
3072hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3073 if (!this.options_.cursorBlink) {
3074 return;
3075 }
3076
3077 this.cursorBlinkPause_ = true;
3078
3079 // If a timeout is already pending, reset the clock due to the new input.
3080 if (this.timeouts_.cursorBlinkPause) {
3081 clearTimeout(this.timeouts_.cursorBlinkPause);
3082 }
3083 // After 500ms, resume blinking. That seems like a good balance between user
3084 // input timings & responsiveness to resume.
3085 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3086 delete this.timeouts_.cursorBlinkPause;
3087 this.cursorBlinkPause_ = false;
3088 }, 500);
3089};
3090
3091/**
rginda87b86462011-12-14 13:48:03 -08003092 * Synchronizes the visible cursor and document selection with the current
3093 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003094 *
3095 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003096 */
3097hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003098 const topRowIndex = this.scrollPort_.getTopRowIndex();
3099 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3100 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003101 this.screen_.cursorPosition.row;
3102
Raymes Khoury15697f42018-07-17 11:37:18 +10003103 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003104 if (this.accessibilityReader_.accessibilityEnabled) {
3105 // Report the new position of the cursor for accessibility purposes.
3106 const cursorColumnIndex = this.screen_.cursorPosition.column;
3107 const cursorLineText =
3108 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003109 // This will force the selection to be sync'd to the cursor position if the
3110 // user has pressed a key. Generally we would only sync the cursor position
3111 // when selection is collapsed so that if the user has selected something
3112 // we don't clear the selection by moving the selection. However when a
3113 // screen reader is used, it's intuitive for entering a key to move the
3114 // selection to the cursor.
3115 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003116 this.accessibilityReader_.afterCursorChange(
3117 cursorLineText, cursorRowIndex, cursorColumnIndex);
3118 }
3119
rginda8ba33642011-12-14 12:31:31 -08003120 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003121 // Cursor is scrolled off screen, hide it.
3122 this.cursorOffScreen_ = true;
3123 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003124 return false;
rginda8ba33642011-12-14 12:31:31 -08003125 }
3126
Joel Hockey3babf302020-04-22 15:00:06 -07003127 if (this.cursorNode_.style.display == 'none') {
3128 // Re-display the terminal cursor if it was hidden.
3129 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003130 this.cursorNode_.style.display = '';
3131 }
3132
Mike Frysinger44c32202017-08-05 01:13:09 -04003133 // Position the cursor using CSS variable math. If we do the math in JS,
3134 // the float math will end up being more precise than the CSS which will
3135 // cause the cursor tracking to be off.
3136 this.setCssVar(
3137 'cursor-offset-row',
3138 `${cursorRowIndex - topRowIndex} + ` +
3139 `${this.scrollPort_.visibleRowTopMargin}px`);
3140 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003141
3142 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003143 '(' + this.screen_.cursorPosition.column +
3144 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003145 ')');
3146
3147 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003148 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003149 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003150 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003151 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003152 return true;
rginda8ba33642011-12-14 12:31:31 -08003153};
3154
Robert Gindafb1be6a2013-12-11 11:56:22 -08003155/**
3156 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3157 * and character cell dimensions.
3158 */
Robert Ginda830583c2013-08-07 13:20:46 -07003159hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003160 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003161
3162 if (this.cursorNode_.getAttribute('focus') == 'false') {
3163 // Always show a block cursor when unfocused.
3164 shape = hterm.Terminal.cursorShape.BLOCK;
3165 }
3166
Mike Frysingerdc727792020-04-10 01:41:13 -04003167 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003168
3169 switch (shape) {
3170 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003171 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003172 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003173 style.borderLeftStyle = 'solid';
3174 break;
3175
3176 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003177 style.backgroundColor = 'transparent';
3178 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003179 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003180 break;
3181
3182 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003183 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003184 style.borderBottomStyle = '';
3185 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003186 break;
3187 }
3188};
3189
rginda8ba33642011-12-14 12:31:31 -08003190/**
3191 * Synchronizes the visible cursor with the current cursor coordinates.
3192 *
3193 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003194 * Multiple calls will be coalesced into a single sync. This should be called
3195 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003196 */
3197hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003198 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003199 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003200 }
rginda8ba33642011-12-14 12:31:31 -08003201
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003202 if (this.accessibilityReader_.accessibilityEnabled) {
3203 // Report the previous position of the cursor for accessibility purposes.
3204 const cursorRowIndex = this.scrollbackRows_.length +
3205 this.screen_.cursorPosition.row;
3206 const cursorColumnIndex = this.screen_.cursorPosition.column;
3207 const cursorLineText =
3208 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3209 this.accessibilityReader_.beforeCursorChange(
3210 cursorLineText, cursorRowIndex, cursorColumnIndex);
3211 }
3212
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003213 this.timeouts_.syncCursor = setTimeout(() => {
3214 this.syncCursorPosition_();
3215 delete this.timeouts_.syncCursor;
3216 });
rginda87b86462011-12-14 13:48:03 -08003217};
3218
rgindacc2996c2012-02-24 14:59:31 -08003219/**
rgindaf522ce02012-04-17 17:49:17 -07003220 * Show or hide the zoom warning.
3221 *
3222 * The zoom warning is a message warning the user that their browser zoom must
3223 * be set to 100% in order for hterm to function properly.
3224 *
3225 * @param {boolean} state True to show the message, false to hide it.
3226 */
3227hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3228 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003229 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003230 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003231 }
rgindaf522ce02012-04-17 17:49:17 -07003232
3233 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003234 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003235 this.zoomWarningNode_.style.cssText = (
3236 'color: black;' +
3237 'background-color: #ff2222;' +
3238 'font-size: large;' +
3239 'border-radius: 8px;' +
3240 'opacity: 0.75;' +
3241 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3242 'top: 0.5em;' +
3243 'right: 1.2em;' +
3244 'position: absolute;' +
3245 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003246 '-webkit-user-select: none;' +
3247 '-moz-text-size-adjust: none;' +
3248 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003249
3250 this.zoomWarningNode_.addEventListener('click', function(e) {
3251 this.parentNode.removeChild(this);
3252 });
rgindaf522ce02012-04-17 17:49:17 -07003253 }
3254
Mike Frysingerb7289952019-03-23 16:05:38 -07003255 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003256 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003257 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003258
rgindaf522ce02012-04-17 17:49:17 -07003259 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3260
3261 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003262 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003263 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003264 }
rgindaf522ce02012-04-17 17:49:17 -07003265 } else if (this.zoomWarningNode_.parentNode) {
3266 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3267 }
3268};
3269
3270/**
rgindacc2996c2012-02-24 14:59:31 -08003271 * Show the terminal overlay for a given amount of time.
3272 *
Jason Lin3d825782020-05-12 11:02:48 +10003273 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003274 *
3275 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003276 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003277 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3278 * stay up forever (or until the next overlay).
3279 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003280hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003281 this.showOverlayWithNode(new Text(msg), timeout);
3282};
3283
3284/**
3285 * Show the terminal overlay for a given amount of time.
3286 *
3287 * The terminal overlay appears in inverse video, centered over the terminal.
3288 *
3289 * @param {!Node} node The node to display in the overlay.
3290 * @param {?number=} timeout The amount of time to wait before fading out
3291 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3292 * stay up forever (or until the next overlay).
3293 */
3294hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003295 if (!this.ready_ || !this.div_) {
3296 return;
3297 }
rgindaf0090c92012-02-10 14:58:52 -08003298
Joel Hockeyedac0e72020-05-14 20:16:20 -07003299 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003300 this.overlayNode_ = this.document_.createElement('div');
3301 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003302 'color: rgb(var(--hterm-background-color));' +
3303 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003304 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003305 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003306 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003307 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003308 'position: absolute;' +
3309 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003310 '-webkit-transition: opacity 180ms ease-in;' +
3311 '-moz-user-select: none;' +
3312 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003313
3314 this.overlayNode_.addEventListener('mousedown', function(e) {
3315 e.preventDefault();
3316 e.stopPropagation();
3317 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003318 }
3319
Jason Lin34567412020-05-14 10:32:09 +10003320 this.overlayNode_.textContent = ''; // Remove all children first.
3321 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003322
Mike Frysingerbdb34802020-04-07 03:47:32 -04003323 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003324 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003325 }
rgindaf0090c92012-02-10 14:58:52 -08003326
Mike Frysingerdc727792020-04-10 01:41:13 -04003327 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3328 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003329
Robert Ginda8a59f762014-07-23 11:29:55 -07003330 this.overlayNode_.style.top =
3331 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003332 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003333 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003334
Mike Frysingerbdb34802020-04-07 03:47:32 -04003335 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003336 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003337 }
rgindaf0090c92012-02-10 14:58:52 -08003338
Jason Lin34567412020-05-14 10:32:09 +10003339 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003340
Mike Frysingerec4225d2020-04-07 05:00:01 -04003341 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003342 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003343 }
rgindacc2996c2012-02-24 14:59:31 -08003344
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003345 this.overlayTimeout_ = setTimeout(() => {
3346 this.overlayNode_.style.opacity = '0';
3347 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003348 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003349};
3350
3351/**
3352 * Hide the terminal overlay immediately.
3353 *
3354 * Useful when we show an overlay for an event with an unknown end time.
3355 */
3356hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003357 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003358 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003359 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003360 this.overlayTimeout_ = null;
3361
Mike Frysingerbdb34802020-04-07 03:47:32 -04003362 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003363 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003364 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003365 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003366};
3367
rginda4bba5e12012-06-20 16:15:30 -07003368/**
3369 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003370 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003371 * Note: In Chrome, this should work unless the user has rejected the permission
3372 * request. In Firefox extension environment, you'll need the "clipboardRead"
3373 * permission. In other environments, this might always fail as the browser
3374 * frequently blocks access for security reasons.
3375 *
3376 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3377 * value is always null. Otherwise, this function uses legacy pasting and
3378 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003379 */
3380hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003381 if (!this.alwaysUseLegacyPasting &&
3382 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003383 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3384 return null;
3385 } else {
3386 // Legacy pasting.
3387 try {
3388 return this.document_.execCommand('paste');
3389 } catch (firefoxException) {
3390 // Ignore this. FF 40 and older would incorrectly throw an exception if
3391 // there was an error instead of returning false.
3392 return false;
3393 }
3394 }
rginda4bba5e12012-06-20 16:15:30 -07003395};
3396
3397/**
3398 * Copy a string to the system clipboard.
3399 *
3400 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003401 *
3402 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003403 */
3404hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003405 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003406 if (!this.clipboardNotice_) {
3407 this.clipboardNotice_ = this.document_.createElement('div');
3408 this.clipboardNotice_.style.textAlign = 'center';
3409 const copyImage = lib.resource.getData('hterm/images/copy');
3410 this.clipboardNotice_.innerHTML =
3411 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3412 }
3413 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003414 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003415
Mike Frysinger96eacae2019-01-02 18:13:56 -05003416 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003417};
3418
Evan Jones2600d4f2016-12-06 09:29:36 -05003419/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003420 * Display an image.
3421 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003422 * Either URI or buffer or blob fields must be specified.
3423 *
Joel Hockey0f933582019-08-27 18:01:51 -07003424 * @param {{
3425 * name: (string|undefined),
3426 * size: (string|number|undefined),
3427 * preserveAspectRation: (boolean|undefined),
3428 * inline: (boolean|undefined),
3429 * width: (string|number|undefined),
3430 * height: (string|number|undefined),
3431 * align: (string|undefined),
3432 * url: (string|undefined),
3433 * buffer: (!ArrayBuffer|undefined),
3434 * blob: (!Blob|undefined),
3435 * type: (string|undefined),
3436 * }} options The image to display.
3437 * name A human readable string for the image
3438 * size The size (in bytes).
3439 * preserveAspectRatio Whether to preserve aspect.
3440 * inline Whether to display the image inline.
3441 * width The width of the image.
3442 * height The height of the image.
3443 * align Direction to align the image.
3444 * uri The source URI for the image.
3445 * buffer The ArrayBuffer image data.
3446 * blob The Blob image data.
3447 * type The MIME type of the image data.
3448 * @param {function()=} onLoad Callback when loading finishes.
3449 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003450 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003451hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003452 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003453 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003454 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003455 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003456 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003457
3458 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003459 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003460 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003461 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003462
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003463 // See if the mime type is available. If not, guess from the filename.
3464 // We don't list all possible mime types because the browser can usually
3465 // guess it correctly. So list the ones that need a bit more help.
3466 if (!options.type) {
3467 const ary = options.name.split('.');
3468 const ext = ary[ary.length - 1].trim();
3469 switch (ext) {
3470 case 'svg':
3471 case 'svgz':
3472 options.type = 'image/svg+xml';
3473 break;
3474 }
3475 }
3476
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003477 // Has the user approved image display yet?
3478 if (this.allowImagesInline !== true) {
3479 this.newLine();
3480 const row = this.getRowNode(this.scrollbackRows_.length +
3481 this.getCursorRow() - 1);
3482
3483 if (this.allowImagesInline === false) {
3484 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3485 'Inline Images Disabled');
3486 return;
3487 }
3488
3489 // Show a prompt.
3490 let button;
3491 const span = this.document_.createElement('span');
3492 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3493 span.style.fontWeight = 'bold';
3494 span.style.borderWidth = '1px';
3495 span.style.borderStyle = 'dashed';
3496 button = this.document_.createElement('span');
3497 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3498 button.style.marginLeft = '1em';
3499 button.style.borderWidth = '1px';
3500 button.style.borderStyle = 'solid';
3501 button.addEventListener('click', () => {
3502 this.prefs_.set('allow-images-inline', false);
3503 });
3504 span.appendChild(button);
3505 button = this.document_.createElement('span');
3506 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3507 'allow this session');
3508 button.style.marginLeft = '1em';
3509 button.style.borderWidth = '1px';
3510 button.style.borderStyle = 'solid';
3511 button.addEventListener('click', () => {
3512 this.allowImagesInline = true;
3513 });
3514 span.appendChild(button);
3515 button = this.document_.createElement('span');
3516 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3517 button.style.marginLeft = '1em';
3518 button.style.borderWidth = '1px';
3519 button.style.borderStyle = 'solid';
3520 button.addEventListener('click', () => {
3521 this.prefs_.set('allow-images-inline', true);
3522 });
3523 span.appendChild(button);
3524
3525 row.appendChild(span);
3526 return;
3527 }
3528
3529 // See if we should show this object directly, or download it.
3530 if (options.inline) {
3531 const io = this.io.push();
3532 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003533 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003534
3535 // While we're loading the image, eat all the user's input.
3536 io.onVTKeystroke = io.sendString = () => {};
3537
3538 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003539 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003540 if (options.uri !== undefined) {
3541 img.src = options.uri;
3542 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003543 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003544 img.src = URL.createObjectURL(blob);
3545 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003546 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003547 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003548 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003549 img.title = img.alt = options.name;
3550
3551 // Attach the image to the page to let it load/render. It won't stay here.
3552 // This is needed so it's visible and the DOM can calculate the height. If
3553 // the image is hidden or not in the DOM, the height is always 0.
3554 this.document_.body.appendChild(img);
3555
3556 // Wait for the image to finish loading before we try moving it to the
3557 // right place in the terminal.
3558 img.onload = () => {
3559 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003560 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003561 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003562 img.style.maxWidth = `${screenSize.width}px`;
3563 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003564
3565 // Parse a width/height specification.
3566 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003567 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003568 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003569 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003570
3571 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3572 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003573 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003574 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003575 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003576 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003577 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003578 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003579 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003580 }
3581
3582 return '';
3583 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003584 img.style.width = parseDim(
3585 options.width, screenSize.width, '--hterm-charsize-width');
3586 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003587 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003588
3589 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003590 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003591 const padRows = Math.ceil(img.clientHeight /
3592 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003593 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003594 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003595 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003596
3597 // Update the max height in case the user shrinks the character size.
3598 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3599
3600 // Move the image to the last row. This way when we scroll up, it doesn't
3601 // disappear when the first row gets clipped. It will disappear when we
3602 // scroll down and the last row is clipped ...
3603 this.document_.body.removeChild(img);
3604 // Create a wrapper node so we can do an absolute in a relative position.
3605 // This helps with rounding errors between JS & CSS counts.
3606 const div = this.document_.createElement('div');
3607 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003608 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003609 img.style.position = 'absolute';
3610 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3611 div.appendChild(img);
3612 const row = this.getRowNode(this.scrollbackRows_.length +
3613 this.getCursorRow() - 1);
3614 row.appendChild(div);
3615
Mike Frysinger2558ed52019-01-14 01:03:41 -05003616 // Now that the image has been read, we can revoke the source.
3617 if (options.uri === undefined) {
3618 URL.revokeObjectURL(img.src);
3619 }
3620
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003621 io.hideOverlay();
3622 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003623
Mike Frysingerbdb34802020-04-07 03:47:32 -04003624 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003625 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003626 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003627 };
3628
3629 // If we got a malformed image, give up.
3630 img.onerror = (e) => {
3631 this.document_.body.removeChild(img);
3632 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003633 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003634 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003635
Mike Frysingerbdb34802020-04-07 03:47:32 -04003636 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003637 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003638 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003639 };
3640 } else {
3641 // We can't use chrome.downloads.download as that requires "downloads"
3642 // permissions, and that works only in extensions, not apps.
3643 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003644 if (options.uri !== undefined) {
3645 a.href = options.uri;
3646 } else if (options.buffer !== undefined) {
3647 const blob = new Blob([options.buffer]);
3648 a.href = URL.createObjectURL(blob);
3649 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003650 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003651 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003652 a.download = options.name;
3653 this.document_.body.appendChild(a);
3654 a.click();
3655 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003656 if (options.uri === undefined) {
3657 URL.revokeObjectURL(a.href);
3658 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003659 }
3660};
3661
3662/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003663 * Returns the selected text, or null if no text is selected.
3664 *
3665 * @return {string|null}
3666 */
rgindaa09e7332012-08-17 12:49:51 -07003667hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003668 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003669 selection.sync();
3670
Mike Frysingerbdb34802020-04-07 03:47:32 -04003671 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003672 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003673 }
rgindaa09e7332012-08-17 12:49:51 -07003674
rgindaa09e7332012-08-17 12:49:51 -07003675 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003676 let startOffset = selection.startOffset;
3677 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003678
Raymes Khoury334625a2018-06-25 10:29:40 +10003679 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003681 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003682 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003683
Robert Gindafdbb3f22012-09-06 20:23:06 -07003684 if (node.nodeName != 'X-ROW') {
3685 // If the selection doesn't start on an x-row node, then it must be
3686 // somewhere inside the x-row. Add any characters from previous siblings
3687 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003688
3689 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3690 // If node is the text node in a styled span, move up to the span node.
3691 node = node.parentNode;
3692 }
3693
Robert Gindafdbb3f22012-09-06 20:23:06 -07003694 while (node.previousSibling) {
3695 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003696 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003697 }
rgindaa09e7332012-08-17 12:49:51 -07003698 }
3699
3700 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003701 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003702 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003703 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003704
Robert Gindafdbb3f22012-09-06 20:23:06 -07003705 if (node.nodeName != 'X-ROW') {
3706 // If the selection doesn't end on an x-row node, then it must be
3707 // somewhere inside the x-row. Add any characters from following siblings
3708 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003709
3710 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3711 // If node is the text node in a styled span, move up to the span node.
3712 node = node.parentNode;
3713 }
3714
Robert Gindafdbb3f22012-09-06 20:23:06 -07003715 while (node.nextSibling) {
3716 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003717 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003718 }
rgindaa09e7332012-08-17 12:49:51 -07003719 }
3720
Mike Frysingerdc727792020-04-10 01:41:13 -04003721 const rv = this.getRowsText(selection.startRow.rowIndex,
3722 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003723 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003724};
3725
rginda4bba5e12012-06-20 16:15:30 -07003726/**
3727 * Copy the current selection to the system clipboard, then clear it after a
3728 * short delay.
3729 */
3730hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003731 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003732 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003733 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003734 }
rginda4bba5e12012-06-20 16:15:30 -07003735};
3736
Joel Hockey0f933582019-08-27 18:01:51 -07003737/**
3738 * Show overlay with current terminal size.
3739 */
rgindaf0090c92012-02-10 14:58:52 -08003740hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003741 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003742 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003743 }
rgindaf0090c92012-02-10 14:58:52 -08003744};
3745
rginda87b86462011-12-14 13:48:03 -08003746/**
3747 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3748 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003749 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003750 */
3751hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003752 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003753 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003754 }
rginda87b86462011-12-14 13:48:03 -08003755
Mike Frysinger225c99d2019-10-20 14:02:37 -06003756 this.pauseCursorBlink_();
3757
Mike Frysinger79669762018-12-30 20:51:10 -05003758 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003759};
3760
3761/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003762 * Open the selected url.
3763 */
3764hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003765 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003766
3767 // If there is no selection, try and expand wherever they clicked.
3768 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003769 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003770 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003771
3772 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003773 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003774 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003775 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003776 }
3777
3778 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003779 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003780 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003781 }
Mike Frysinger43472622017-06-26 18:11:07 -04003782
3783 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003784 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003785 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3786 // We have to whitelist a few protocols that lack authorities and thus
3787 // never use the //. Like mailto.
3788 switch (str.split(':', 1)[0]) {
3789 case 'mailto':
3790 break;
3791 default:
3792 str = 'http://' + str;
3793 break;
3794 }
3795 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003796
Mike Frysinger720fa832017-10-23 01:15:52 -04003797 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003798};
Mike Frysinger70b94692017-01-26 18:57:50 -10003799
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003800/**
3801 * Manage the automatic mouse hiding behavior while typing.
3802 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003803 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003804 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003805hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003806 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3807 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003808 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003809 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003810 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003811
3812 this.mouseHideWhileTyping_ = !!v;
3813};
3814
3815/**
3816 * Handler for monitoring user keyboard activity.
3817 *
3818 * This isn't for processing the keystrokes directly, but for updating any
3819 * state that might toggle based on the user using the keyboard at all.
3820 *
Joel Hockey0f933582019-08-27 18:01:51 -07003821 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003822 */
3823hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3824 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003825 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003826 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003827 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003828};
Mike Frysinger70b94692017-01-26 18:57:50 -10003829
3830/**
rgindad5613292012-06-19 15:40:37 -07003831 * Add the terminalRow and terminalColumn properties to mouse events and
3832 * then forward on to onMouse().
3833 *
3834 * The terminalRow and terminalColumn properties contain the (row, column)
3835 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003836 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003837 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003838 */
3839hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003840 if (e.processedByTerminalHandler_) {
3841 // We register our event handlers on the document, as well as the cursor
3842 // and the scroll blocker. Mouse events that occur on the cursor or
3843 // scroll blocker will also appear on the document, but we don't want to
3844 // process them twice.
3845 //
3846 // We can't just prevent bubbling because that has other side effects, so
3847 // we decorate the event object with this property instead.
3848 return;
3849 }
3850
Mike Frysinger468966c2018-08-28 13:48:51 -04003851 // Consume navigation events. Button 3 is usually "browser back" and
3852 // button 4 is "browser forward" which we don't want to happen.
3853 if (e.button > 2) {
3854 e.preventDefault();
3855 // We don't return so click events can be passed to the remote below.
3856 }
3857
Mike Frysingerdc727792020-04-10 01:41:13 -04003858 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003859 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3860
rgindafaa74742012-08-21 13:34:03 -07003861 e.processedByTerminalHandler_ = true;
3862
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003863 // Handle auto hiding of mouse cursor while typing.
3864 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3865 // Make sure the mouse cursor is visible.
3866 this.syncMouseStyle();
3867 // This debounce isn't perfect, but should work well enough for such a
3868 // simple implementation. If the user moved the mouse, we enabled this
3869 // debounce, and then moved the mouse just before the timeout, we wouldn't
3870 // debounce that later movement.
3871 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3872 }
3873
Robert Gindaeda48db2014-07-17 09:25:30 -07003874 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003875 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003876 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003877 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003878 this.scrollPort_.characterSize.height) + 1;
3879 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003880 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003881
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003882 // Clamp row and column.
3883 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3884 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3885
3886 // Ignore mousedown in the scrollbar area.
3887 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003888 return;
3889 }
3890
Joel Hockey3babf302020-04-22 15:00:06 -07003891 if (this.options_.cursorVisible && !reportMouseEvents &&
3892 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003893 // If the cursor is visible and we're not sending mouse events to the
3894 // host app, then we want to hide the terminal cursor when the mouse
3895 // cursor is over top. This keeps the terminal cursor from interfering
3896 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003897 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3898 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3899 this.cursorNode_.style.display = 'none';
3900 } else if (this.cursorNode_.style.display == 'none') {
3901 this.cursorNode_.style.display = '';
3902 }
3903 }
rgindad5613292012-06-19 15:40:37 -07003904
Robert Ginda928cf632014-03-05 15:07:41 -08003905 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003906 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003907
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003908 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003909 // If VT mouse reporting is disabled, or has been defeated with
3910 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003911 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003912 this.setSelectionEnabled(true);
3913 } else {
3914 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003915 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003916 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003917 this.setSelectionEnabled(false);
3918 e.preventDefault();
3919 }
3920 }
3921
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003922 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003923 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003924 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003925 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003926 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003927 }
rgindad5613292012-06-19 15:40:37 -07003928 }
3929
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003930 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003931 // Debounce this event with the dblclick event. If you try to doubleclick
3932 // a URL to open it, Chrome will fire click then dblclick, but we won't
3933 // have expanded the selection text at the first click event.
3934 clearTimeout(this.timeouts_.openUrl);
3935 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3936 500);
3937 return;
3938 }
3939
Mike Frysinger847577f2017-05-23 23:25:57 -04003940 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003941 if (e.ctrlKey && e.button == 2 /* right button */) {
3942 e.preventDefault();
3943 this.contextMenu.show(e, this);
3944 } else if (e.button == this.mousePasteButton ||
3945 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003946 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003947 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003948 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003949 }
3950 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003951
Mike Frysinger2edd3612017-05-24 00:54:39 -04003952 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003953 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003954 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003955 }
3956
3957 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3958 this.scrollBlockerNode_.engaged) {
3959 // Disengage the scroll-blocker after one of these events.
3960 this.scrollBlockerNode_.engaged = false;
3961 this.scrollBlockerNode_.style.top = '-99px';
3962 }
3963
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003964 // Emulate arrow key presses via scroll wheel events.
3965 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3966 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003967 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003968 const delta =
3969 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003970
Mike Frysinger321063c2018-08-29 15:33:14 -04003971 // Helper to turn a wheel event delta into a series of key presses.
3972 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3973 if (distance == 0) {
3974 return '';
3975 }
3976
3977 // Convert the scroll distance into a number of rows/cols.
3978 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3979 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3980 return data.repeat(cells);
3981 };
3982
3983 // The order between up/down and left/right doesn't really matter.
3984 this.io.sendString(
3985 // Up/down arrow keys.
3986 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3987 'A', 'B') +
3988 // Left/right arrow keys.
3989 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10003990 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04003991 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003992
3993 e.preventDefault();
3994 }
3995 }
Robert Ginda928cf632014-03-05 15:07:41 -08003996 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003997 if (!this.scrollBlockerNode_.engaged) {
3998 if (e.type == 'mousedown') {
3999 // Move the scroll-blocker into place if we want to keep the scrollport
4000 // from scrolling.
4001 this.scrollBlockerNode_.engaged = true;
4002 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4003 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4004 } else if (e.type == 'mousemove') {
4005 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4006 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004007 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004008 e.preventDefault();
4009 }
4010 }
Robert Ginda928cf632014-03-05 15:07:41 -08004011
4012 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004013 }
4014
Robert Ginda928cf632014-03-05 15:07:41 -08004015 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4016 // Restore this on mouseup in case it was temporarily defeated with a
4017 // alt-mousedown. Only do this when the selection is empty so that
4018 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004019 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004020 }
rgindad5613292012-06-19 15:40:37 -07004021};
4022
4023/**
4024 * Clients should override this if they care to know about mouse events.
4025 *
4026 * The event parameter will be a normal DOM mouse click event with additional
4027 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004028 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004029 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004030 */
4031hterm.Terminal.prototype.onMouse = function(e) { };
4032
4033/**
rginda8e92a692012-05-20 19:37:20 -07004034 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004035 *
4036 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004037 */
Rob Spies06533ba2014-04-24 11:20:37 -07004038hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4039 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004040 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004041
Mike Frysingerbdb34802020-04-07 03:47:32 -04004042 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004043 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004044 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004045
Mike Frysingerbdb34802020-04-07 03:47:32 -04004046 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004047 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004048 }
rginda8e92a692012-05-20 19:37:20 -07004049};
4050
4051/**
rginda8ba33642011-12-14 12:31:31 -08004052 * React when the ScrollPort is scrolled.
4053 */
4054hterm.Terminal.prototype.onScroll_ = function() {
4055 this.scheduleSyncCursorPosition_();
4056};
4057
4058/**
rginda9846e2f2012-01-27 13:53:33 -08004059 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004060 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004061 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004062 */
4063hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004064 this.onPasteData_(e.text);
4065};
4066
4067/**
4068 * Handle pasted data.
4069 *
4070 * @param {string} data The pasted data.
4071 */
4072hterm.Terminal.prototype.onPasteData_ = function(data) {
4073 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004074 if (this.options_.bracketedPaste) {
4075 // We strip out most escape sequences as they can cause issues (like
4076 // inserting an \x1b[201~ midstream). We pass through whitespace
4077 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4078 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004079 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004080 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4081 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4082 }
Robert Gindaa063b202014-07-21 11:08:25 -07004083
4084 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004085};
4086
4087/**
rgindaa09e7332012-08-17 12:49:51 -07004088 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004089 *
Joel Hockey0f933582019-08-27 18:01:51 -07004090 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004091 */
4092hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004093 if (!this.useDefaultWindowCopy) {
4094 e.preventDefault();
4095 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4096 }
rgindaa09e7332012-08-17 12:49:51 -07004097};
4098
4099/**
rginda8ba33642011-12-14 12:31:31 -08004100 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004101 *
4102 * Note: This function should not directly contain code that alters the internal
4103 * state of the terminal. That kind of code belongs in realizeWidth or
4104 * realizeHeight, so that it can be executed synchronously in the case of a
4105 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004106 */
4107hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004108 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4109 this.scrollPort_.characterSize.width) || 0;
4110 const rowCount = lib.f.smartFloorDivide(
4111 this.scrollPort_.getScreenHeight(),
4112 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004113
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004114 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004115 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004116 // gets removed from the document or during the initial load, and we can't
4117 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004118 // This can also happen if called before the scrollPort calculates the
4119 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004120 return;
4121 }
4122
Mike Frysingerdc727792020-04-10 01:41:13 -04004123 const isNewSize = (columnCount != this.screenSize.width ||
4124 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004125 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004126
4127 // We do this even if the size didn't change, just to be sure everything is
4128 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004129 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004130 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004131
Mike Frysingerbdb34802020-04-07 03:47:32 -04004132 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004133 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004134 }
rgindaa8ba17d2012-08-15 14:41:10 -07004135
Robert Gindafb1be6a2013-12-11 11:56:22 -08004136 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004137 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004138
4139 if (wasScrolledEnd) {
4140 this.scrollEnd();
4141 }
rginda8ba33642011-12-14 12:31:31 -08004142};
4143
4144/**
4145 * Service the cursor blink timeout.
4146 */
4147hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004148 if (!this.options_.cursorBlink) {
4149 delete this.timeouts_.cursorBlink;
4150 return;
4151 }
4152
Robert Ginda830583c2013-08-07 13:20:46 -07004153 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004154 this.cursorNode_.style.opacity == '0' ||
4155 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004156 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004157 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4158 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004159 } else {
rginda87b86462011-12-14 13:48:03 -08004160 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004161 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4162 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004163 }
4164};
David Reveman8f552492012-03-28 12:18:41 -04004165
4166/**
4167 * Set the scrollbar-visible mode bit.
4168 *
4169 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4170 * Otherwise it will not.
4171 *
4172 * Defaults to on.
4173 *
4174 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4175 */
4176hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4177 this.scrollPort_.setScrollbarVisible(state);
4178};
Michael Kelly485ecd12014-06-09 11:41:56 -04004179
4180/**
Rob Spies49039e52014-12-17 13:40:04 -08004181 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004182 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004183 *
4184 * Defaults to 1.
4185 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004186 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004187 */
4188hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4189 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4190};
4191
4192/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004193 * Close all web notifications created by terminal bells.
4194 */
4195hterm.Terminal.prototype.closeBellNotifications_ = function() {
4196 this.bellNotificationList_.forEach(function(n) {
4197 n.close();
4198 });
4199 this.bellNotificationList_.length = 0;
4200};
Raymes Khourye5d48982018-08-02 09:08:32 +10004201
4202/**
4203 * Syncs the cursor position when the scrollport gains focus.
4204 */
4205hterm.Terminal.prototype.onScrollportFocus_ = function() {
4206 // If the cursor is offscreen we set selection to the last row on the screen.
4207 const topRowIndex = this.scrollPort_.getTopRowIndex();
4208 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4209 const selection = this.document_.getSelection();
4210 if (!this.syncCursorPosition_() && selection) {
4211 selection.collapse(this.getRowNode(bottomRowIndex));
4212 }
4213};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004214
4215/**
4216 * Clients can override this if they want to provide an options page.
4217 */
4218hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4219
4220
4221/**
4222 * Called when user selects to open the options page.
4223 */
4224hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4225 this.onOpenOptionsPage();
4226};