blob: b0dde3336e1bbca451aceee597d48fc5546e20e9 [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 {
Joel Hockey500c6102020-05-14 19:24:02 -07001740 background: #fff;
1741 border-radius: 4px;
1742 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001743 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001744 display: none;
1745 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1746 margin: 0;
1747 padding: 8px 0;
1748 position: absolute;
1749 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001750}
1751menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001752 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001753 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1754 padding: 0.5em 1em;
1755 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001756}
1757menuitem.separator {
1758 border-bottom: none;
1759 height: 0.5em;
1760 padding: 0;
1761}
1762menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001763 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001764}
1765.wc-node {
1766 display: inline-block;
1767 text-align: center;
1768 width: calc(var(--hterm-charsize-width) * 2);
1769 line-height: var(--hterm-charsize-height);
1770}
1771:root {
1772 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1773 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001774 --hterm-blink-node-duration: 0.7s;
1775 --hterm-mouse-cursor-default: default;
1776 --hterm-mouse-cursor-text: text;
1777 --hterm-mouse-cursor-pointer: pointer;
1778 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001779 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001780
Joel Hockey42dba8f2020-03-26 16:21:11 -07001781${lib.colors.stockColorPalette.map((c, i) => `
1782 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1783`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001784}
1785.uri-node:hover {
1786 text-decoration: underline;
1787 cursor: var(--hterm-mouse-cursor-pointer);
1788}
1789@keyframes blink {
1790 from { opacity: 1.0; }
1791 to { opacity: 0.0; }
1792}
1793.blink-node {
1794 animation-name: blink;
1795 animation-duration: var(--hterm-blink-node-duration);
1796 animation-iteration-count: infinite;
1797 animation-timing-function: ease-in-out;
1798 animation-direction: alternate;
1799}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001800 // Insert this stock style as the first node so that any user styles will
1801 // override w/out having to use !important everywhere. The rules above mix
1802 // runtime variables with default ones designed to be overridden by the user,
1803 // but we can wait for a concrete case from the users to determine the best
1804 // way to split the sheet up to before & after the user-css settings.
1805 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001806
rginda8ba33642011-12-14 12:31:31 -08001807 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001808 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001809 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001810 this.cursorNode_.style.cssText = `
1811position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001812left: calc(var(--hterm-screen-padding-size) +
1813 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1814top: calc(var(--hterm-screen-padding-size) +
1815 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001816display: ${this.options_.cursorVisible ? '' : 'none'};
1817width: var(--hterm-charsize-width);
1818height: var(--hterm-charsize-height);
1819background-color: var(--hterm-cursor-color);
1820border-color: var(--hterm-cursor-color);
1821-webkit-transition: opacity, background-color 100ms linear;
1822-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001823
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001824 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001825 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1826 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001827
rginda8ba33642011-12-14 12:31:31 -08001828 this.document_.body.appendChild(this.cursorNode_);
1829
rgindad5613292012-06-19 15:40:37 -07001830 // When 'enableMouseDragScroll' is off we reposition this element directly
1831 // under the mouse cursor after a click. This makes Chrome associate
1832 // subsequent mousemove events with the scroll-blocker. Since the
1833 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1834 // events do not cause the scrollport to scroll.
1835 //
1836 // It's a hack, but it's the cleanest way I could find.
1837 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001838 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001839 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001840 this.scrollBlockerNode_.style.cssText =
1841 ('position: absolute;' +
1842 'top: -99px;' +
1843 'display: block;' +
1844 'width: 10px;' +
1845 'height: 10px;');
1846 this.document_.body.appendChild(this.scrollBlockerNode_);
1847
rgindad5613292012-06-19 15:40:37 -07001848 this.scrollPort_.onScrollWheel = onMouse;
1849 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1850 ].forEach(function(event) {
1851 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001852 this.cursorNode_.addEventListener(
1853 event, /** @type {!EventListener} */ (onMouse));
1854 this.document_.addEventListener(
1855 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001856 }.bind(this));
1857
1858 this.cursorNode_.addEventListener('mousedown', function() {
1859 setTimeout(this.focus.bind(this));
1860 }.bind(this));
1861
rginda8ba33642011-12-14 12:31:31 -08001862 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001863
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001864 // Re-sync fonts whenever a web font loads.
1865 this.document_.fonts.addEventListener(
1866 'loadingdone', () => this.syncFontFamily());
1867
rginda87b86462011-12-14 13:48:03 -08001868 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001869 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001870};
1871
rginda0918b652012-04-04 11:26:24 -07001872/**
1873 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001874 *
Joel Hockey0f933582019-08-27 18:01:51 -07001875 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001876 */
rginda87b86462011-12-14 13:48:03 -08001877hterm.Terminal.prototype.getDocument = function() {
1878 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001879};
1880
1881/**
rginda0918b652012-04-04 11:26:24 -07001882 * Focus the terminal.
1883 */
1884hterm.Terminal.prototype.focus = function() {
1885 this.scrollPort_.focus();
1886};
1887
1888/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001889 * Unfocus the terminal.
1890 */
1891hterm.Terminal.prototype.blur = function() {
1892 this.scrollPort_.blur();
1893};
1894
1895/**
rginda8ba33642011-12-14 12:31:31 -08001896 * Return the HTML Element for a given row index.
1897 *
1898 * This is a method from the RowProvider interface. The ScrollPort uses
1899 * it to fetch rows on demand as they are scrolled into view.
1900 *
1901 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1902 * pairs to conserve memory.
1903 *
Joel Hockey0f933582019-08-27 18:01:51 -07001904 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001905 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001906 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001907 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001908 * @override
rginda8ba33642011-12-14 12:31:31 -08001909 */
1910hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001911 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001912 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001913 }
rginda8ba33642011-12-14 12:31:31 -08001914
Mike Frysingerdc727792020-04-10 01:41:13 -04001915 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001916 return this.screen_.rowsArray[screenIndex];
1917};
1918
1919/**
1920 * Return the text content for a given range of rows.
1921 *
1922 * This is a method from the RowProvider interface. The ScrollPort uses
1923 * it to fetch text content on demand when the user attempts to copy their
1924 * selection to the clipboard.
1925 *
Joel Hockey0f933582019-08-27 18:01:51 -07001926 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001927 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001928 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001929 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001930 * relative to the start of the scrollback buffer.
1931 * @return {string} A single string containing the text value of the range of
1932 * rows. Lines will be newline delimited, with no trailing newline.
1933 */
1934hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001935 const ary = [];
1936 for (let i = start; i < end; i++) {
1937 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001938 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001939 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001940 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001941 }
rginda8ba33642011-12-14 12:31:31 -08001942 }
1943
rgindaa09e7332012-08-17 12:49:51 -07001944 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001945};
1946
1947/**
1948 * Return the text content for a given row.
1949 *
1950 * This is a method from the RowProvider interface. The ScrollPort uses
1951 * it to fetch text content on demand when the user attempts to copy their
1952 * selection to the clipboard.
1953 *
Joel Hockey0f933582019-08-27 18:01:51 -07001954 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001955 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001956 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001957 * @return {string} A string containing the text value of the selected row.
1958 */
1959hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001960 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001961 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001962};
1963
1964/**
1965 * Return the total number of rows in the addressable screen and in the
1966 * scrollback buffer of this terminal.
1967 *
1968 * This is a method from the RowProvider interface. The ScrollPort uses
1969 * it to compute the size of the scrollbar.
1970 *
Joel Hockey0f933582019-08-27 18:01:51 -07001971 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001972 * @override
rginda8ba33642011-12-14 12:31:31 -08001973 */
1974hterm.Terminal.prototype.getRowCount = function() {
1975 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1976};
1977
1978/**
1979 * Create DOM nodes for new rows and append them to the end of the terminal.
1980 *
1981 * This is the only correct way to add a new DOM node for a row. Notice that
1982 * the new row is appended to the bottom of the list of rows, and does not
1983 * require renumbering (of the rowIndex property) of previous rows.
1984 *
1985 * If you think you want a new blank row somewhere in the middle of the
1986 * terminal, look into moveRows_().
1987 *
1988 * This method does not pay attention to vtScrollTop/Bottom, since you should
1989 * be using moveRows() in cases where they would matter.
1990 *
1991 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001992 *
1993 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001994 */
1995hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001996 let cursorRow = this.screen_.rowsArray.length;
1997 const offset = this.scrollbackRows_.length + cursorRow;
1998 for (let i = 0; i < count; i++) {
1999 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002000 row.appendChild(this.document_.createTextNode(''));
2001 row.rowIndex = offset + i;
2002 this.screen_.pushRow(row);
2003 }
2004
Mike Frysingerdc727792020-04-10 01:41:13 -04002005 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002006 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002007 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002008 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002009 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002010 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002011 }
rginda8ba33642011-12-14 12:31:31 -08002012 }
2013
Mike Frysingerbdb34802020-04-07 03:47:32 -04002014 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002015 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002016 }
rginda8ba33642011-12-14 12:31:31 -08002017
rginda87b86462011-12-14 13:48:03 -08002018 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002019};
2020
2021/**
2022 * Relocate rows from one part of the addressable screen to another.
2023 *
2024 * This is used to recycle rows during VT scrolls (those which are driven
2025 * by VT commands, rather than by the user manipulating the scrollbar.)
2026 *
2027 * In this case, the blank lines scrolled into the scroll region are made of
2028 * the nodes we scrolled off. These have their rowIndex properties carefully
2029 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002030 *
2031 * @param {number} fromIndex The start index.
2032 * @param {number} count The number of rows to move.
2033 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002034 */
2035hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002036 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002037 this.screen_.insertRows(toIndex, ary);
2038
Mike Frysingerdc727792020-04-10 01:41:13 -04002039 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002040 if (fromIndex < toIndex) {
2041 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002042 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002043 } else {
2044 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002045 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002046 }
2047
2048 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002049 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002050};
2051
2052/**
2053 * Renumber the rowIndex property of the given range of rows.
2054 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002055 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002056 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002057 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002058 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002059 *
2060 * @param {number} start The start index.
2061 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002062 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002063 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002064hterm.Terminal.prototype.renumberRows_ = function(
2065 start, end, screen = undefined) {
2066 if (!screen) {
2067 screen = this.screen_;
2068 }
Robert Ginda40932892012-12-10 17:26:40 -08002069
Mike Frysingerdc727792020-04-10 01:41:13 -04002070 const offset = this.scrollbackRows_.length;
2071 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002072 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002073 }
2074};
2075
2076/**
2077 * Print a string to the terminal.
2078 *
2079 * This respects the current insert and wraparound modes. It will add new lines
2080 * to the end of the terminal, scrolling off the top into the scrollback buffer
2081 * if necessary.
2082 *
2083 * The string is *not* parsed for escape codes. Use the interpret() method if
2084 * that's what you're after.
2085 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002086 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002087 */
2088hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002089 this.scheduleSyncCursorPosition_();
2090
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002091 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002092 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002093
Mike Frysingerdc727792020-04-10 01:41:13 -04002094 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002095
Mike Frysingerdc727792020-04-10 01:41:13 -04002096 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002097 // Fun edge case: If the string only contains zero width codepoints (like
2098 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002099 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002100 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002101 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002102
2103 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002104 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2105 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002106 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002107 }
rgindaa19afe22012-01-25 15:40:22 -08002108
Mike Frysingerdc727792020-04-10 01:41:13 -04002109 let count = strWidth - startOffset;
2110 let didOverflow = false;
2111 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002112
rgindaa9abdd82012-08-06 18:05:09 -07002113 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2114 didOverflow = true;
2115 count = this.screenSize.width - this.screen_.cursorPosition.column;
2116 }
rgindaa19afe22012-01-25 15:40:22 -08002117
rgindaa9abdd82012-08-06 18:05:09 -07002118 if (didOverflow && !this.options_.wraparound) {
2119 // If the string overflowed the line but wraparound is off, then the
2120 // last printed character should be the last of the string.
2121 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002122 substr = lib.wc.substr(str, startOffset, count - 1) +
2123 lib.wc.substr(str, strWidth - 1);
2124 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002125 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002126 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002127 }
rgindaa19afe22012-01-25 15:40:22 -08002128
Mike Frysingerdc727792020-04-10 01:41:13 -04002129 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2130 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002131 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2132 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002133
2134 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002135 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002136 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002137 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002138 }
2139 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002140 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002141 }
2142
2143 this.screen_.maybeClipCurrentRow();
2144 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002145 }
rginda8ba33642011-12-14 12:31:31 -08002146
Mike Frysingerbdb34802020-04-07 03:47:32 -04002147 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002148 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002149 }
rginda8ba33642011-12-14 12:31:31 -08002150};
2151
2152/**
rginda87b86462011-12-14 13:48:03 -08002153 * Set the VT scroll region.
2154 *
rginda87b86462011-12-14 13:48:03 -08002155 * This also resets the cursor position to the absolute (0, 0) position, since
2156 * that's what xterm appears to do.
2157 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002158 * Setting the scroll region to the full height of the terminal will clear
2159 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2160 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2161 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2162 * continue to work as most users would expect.
2163 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002164 * @param {?number} scrollTop The zero-based top of the scroll region.
2165 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002166 * inclusive.
2167 */
2168hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002169 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002170 this.vtScrollTop_ = null;
2171 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002172 } else {
2173 this.vtScrollTop_ = scrollTop;
2174 this.vtScrollBottom_ = scrollBottom;
2175 }
rginda87b86462011-12-14 13:48:03 -08002176};
2177
2178/**
rginda8ba33642011-12-14 12:31:31 -08002179 * Return the top row index according to the VT.
2180 *
2181 * This will return 0 unless the terminal has been told to restrict scrolling
2182 * to some lower row. It is used for some VT cursor positioning and scrolling
2183 * commands.
2184 *
Joel Hockey0f933582019-08-27 18:01:51 -07002185 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002186 */
2187hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002188 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002189 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002190 }
rginda8ba33642011-12-14 12:31:31 -08002191
2192 return 0;
rginda87b86462011-12-14 13:48:03 -08002193};
rginda8ba33642011-12-14 12:31:31 -08002194
2195/**
2196 * Return the bottom row index according to the VT.
2197 *
2198 * This will return the height of the terminal unless the it has been told to
2199 * restrict scrolling to some higher row. It is used for some VT cursor
2200 * positioning and scrolling commands.
2201 *
Joel Hockey0f933582019-08-27 18:01:51 -07002202 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002203 */
2204hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002205 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002206 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002207 }
rginda8ba33642011-12-14 12:31:31 -08002208
rginda87b86462011-12-14 13:48:03 -08002209 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002210};
rginda8ba33642011-12-14 12:31:31 -08002211
2212/**
2213 * Process a '\n' character.
2214 *
2215 * If the cursor is on the final row of the terminal this will append a new
2216 * blank row to the screen and scroll the topmost row into the scrollback
2217 * buffer.
2218 *
2219 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002220 *
2221 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2222 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002223 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002224hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002225 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002226 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002227 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002228
Mike Frysingerdc727792020-04-10 01:41:13 -04002229 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2230 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002231
2232 if (this.vtScrollBottom_ != null) {
2233 // A VT Scroll region is active, we never append new rows.
2234 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2235 // We're at the end of the VT Scroll Region, perform a VT scroll.
2236 this.vtScrollUp(1);
2237 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2238 } else if (cursorAtEndOfScreen) {
2239 // We're at the end of the screen, the only thing to do is put the
2240 // cursor to column 0.
2241 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2242 } else {
2243 // Anywhere else, advance the cursor row, and reset the column.
2244 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2245 }
2246 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002247 // We're at the end of the screen. Append a new row to the terminal,
2248 // shifting the top row into the scrollback.
2249 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002250 } else {
rginda87b86462011-12-14 13:48:03 -08002251 // Anywhere else in the screen just moves the cursor.
2252 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002253 }
2254};
2255
2256/**
2257 * Like newLine(), except maintain the cursor column.
2258 */
2259hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002260 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002261 this.newLine();
2262 this.setCursorColumn(column);
2263};
2264
2265/**
rginda87b86462011-12-14 13:48:03 -08002266 * If autoCarriageReturn is set then newLine(), else lineFeed().
2267 */
2268hterm.Terminal.prototype.formFeed = function() {
2269 if (this.options_.autoCarriageReturn) {
2270 this.newLine();
2271 } else {
2272 this.lineFeed();
2273 }
2274};
2275
2276/**
2277 * Move the cursor up one row, possibly inserting a blank line.
2278 *
2279 * The cursor column is not changed.
2280 */
2281hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002282 const scrollTop = this.getVTScrollTop();
2283 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002284
2285 if (currentRow == scrollTop) {
2286 this.insertLines(1);
2287 } else {
2288 this.setAbsoluteCursorRow(currentRow - 1);
2289 }
2290};
2291
2292/**
rginda8ba33642011-12-14 12:31:31 -08002293 * Replace all characters to the left of the current cursor with the space
2294 * character.
2295 *
2296 * TODO(rginda): This should probably *remove* the characters (not just replace
2297 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002298 * position.
rginda8ba33642011-12-14 12:31:31 -08002299 */
2300hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002301 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002302 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002303 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002304 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002305 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002306};
2307
2308/**
David Benjamin684a9b72012-05-01 17:19:58 -04002309 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002310 *
2311 * The cursor position is unchanged.
2312 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002313 * If the current background color is not the default background color this
2314 * will insert spaces rather than delete. This is unfortunate because the
2315 * trailing space will affect text selection, but it's difficult to come up
2316 * with a way to style empty space that wouldn't trip up the hterm.Screen
2317 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002318 *
2319 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2320 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2321 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002322 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002323 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002324 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002325hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002326 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002327 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002328 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002329
Mike Frysingerdc727792020-04-10 01:41:13 -04002330 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002331 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002332
2333 if (this.screen_.textAttributes.background ===
2334 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002335 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002336 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002337 this.screen_.cursorPosition.column + count) {
2338 this.screen_.deleteChars(count);
2339 this.clearCursorOverflow();
2340 return;
2341 }
2342 }
2343
Mike Frysingerdc727792020-04-10 01:41:13 -04002344 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002345 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002346 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002347 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002348};
2349
2350/**
2351 * Erase the current line.
2352 *
2353 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002354 */
2355hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002356 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002357 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002358 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002359 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002360};
2361
2362/**
David Benjamina08d78f2012-05-05 00:28:49 -04002363 * Erase all characters from the start of the screen to the current cursor
2364 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002365 *
2366 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002367 */
2368hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002369 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002370
2371 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002372
Mike Frysingerdc727792020-04-10 01:41:13 -04002373 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002374 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002375 this.screen_.clearCursorRow();
2376 }
2377
rginda87b86462011-12-14 13:48:03 -08002378 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002379 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002380};
2381
2382/**
2383 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002384 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002385 *
2386 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002387 */
2388hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002389 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002390
2391 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002392
Mike Frysingerdc727792020-04-10 01:41:13 -04002393 const bottom = this.screenSize.height - 1;
2394 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002395 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002396 this.screen_.clearCursorRow();
2397 }
2398
rginda87b86462011-12-14 13:48:03 -08002399 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002400 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002401};
2402
2403/**
2404 * Fill the terminal with a given character.
2405 *
2406 * This methods does not respect the VT scroll region.
2407 *
2408 * @param {string} ch The character to use for the fill.
2409 */
2410hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002411 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002412
2413 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002414 for (let row = 0; row < this.screenSize.height; row++) {
2415 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002416 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002417 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002418 }
2419 }
2420
2421 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002422};
2423
2424/**
rginda9ea433c2012-03-16 11:57:00 -07002425 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002426 *
rginda9ea433c2012-03-16 11:57:00 -07002427 * This does not respect the scroll region.
2428 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002429 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002430 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002431 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002432hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2433 if (!screen) {
2434 screen = this.screen_;
2435 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002436 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002437
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002438 this.accessibilityReader_.clear();
2439
rginda11057d52012-04-25 12:29:56 -07002440 if (bottom == 0) {
2441 // Empty screen, nothing to do.
2442 return;
2443 }
2444
Mike Frysingerdc727792020-04-10 01:41:13 -04002445 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002446 screen.setCursorPosition(i, 0);
2447 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002448 }
2449
rginda9ea433c2012-03-16 11:57:00 -07002450 screen.setCursorPosition(0, 0);
2451};
2452
2453/**
2454 * Erase the entire display without changing the cursor position.
2455 *
2456 * The cursor position is unchanged. This does not respect the scroll
2457 * region.
2458 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002459 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002460 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002461 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002462hterm.Terminal.prototype.clear = function(screen = undefined) {
2463 if (!screen) {
2464 screen = this.screen_;
2465 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002466 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002467 this.clearHome(screen);
2468 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002469};
2470
2471/**
2472 * VT command to insert lines at the current cursor row.
2473 *
2474 * This respects the current scroll region. Rows pushed off the bottom are
2475 * lost (they won't show up in the scrollback buffer).
2476 *
Joel Hockey0f933582019-08-27 18:01:51 -07002477 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002478 */
2479hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002480 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002481
Mike Frysingerdc727792020-04-10 01:41:13 -04002482 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002483 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002484
Robert Ginda579186b2012-09-26 11:40:04 -07002485 // The moveCount is the number of rows we need to relocate to make room for
2486 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002487 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002488 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002489 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002490 }
rginda8ba33642011-12-14 12:31:31 -08002491
Mike Frysingerdc727792020-04-10 01:41:13 -04002492 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002493 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002494 this.screen_.clearCursorRow();
2495 }
rginda8ba33642011-12-14 12:31:31 -08002496};
2497
2498/**
2499 * VT command to delete lines at the current cursor row.
2500 *
2501 * New rows are added to the bottom of scroll region to take their place. New
2502 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002503 *
2504 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002505 */
2506hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002507 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002508
Mike Frysingerdc727792020-04-10 01:41:13 -04002509 const top = cursor.row;
2510 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002511
Mike Frysingerdc727792020-04-10 01:41:13 -04002512 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002513 count = Math.min(count, maxCount);
2514
Mike Frysingerdc727792020-04-10 01:41:13 -04002515 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002516 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002517 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002518 }
rginda8ba33642011-12-14 12:31:31 -08002519
Mike Frysingerdc727792020-04-10 01:41:13 -04002520 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002521 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002522 this.screen_.clearCursorRow();
2523 }
2524
rginda87b86462011-12-14 13:48:03 -08002525 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002526 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002527};
2528
2529/**
2530 * Inserts the given number of spaces at the current cursor position.
2531 *
rginda87b86462011-12-14 13:48:03 -08002532 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002533 *
2534 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002535 */
2536hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002537 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002538
Mike Frysinger73e56462019-07-17 00:23:46 -05002539 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002540 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002541 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002542
2543 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002544 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002545};
2546
2547/**
2548 * Forward-delete the specified number of characters starting at the cursor
2549 * position.
2550 *
Joel Hockey0f933582019-08-27 18:01:51 -07002551 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002552 */
2553hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002554 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002555 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002556 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002557 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002558 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002559 this.restoreCursor(cursor);
2560 }
2561
David Benjamin54e8bf62012-06-01 22:31:40 -04002562 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002563};
2564
2565/**
2566 * Shift rows in the scroll region upwards by a given number of lines.
2567 *
2568 * New rows are inserted at the bottom of the scroll region to fill the
2569 * vacated rows. The new rows not filled out with the current text attributes.
2570 *
2571 * This function does not affect the scrollback rows at all. Rows shifted
2572 * off the top are lost.
2573 *
rginda87b86462011-12-14 13:48:03 -08002574 * The cursor position is not altered.
2575 *
Joel Hockey0f933582019-08-27 18:01:51 -07002576 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002577 */
2578hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002579 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002580
rginda87b86462011-12-14 13:48:03 -08002581 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002582 this.deleteLines(count);
2583
rginda87b86462011-12-14 13:48:03 -08002584 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002585};
2586
2587/**
2588 * Shift rows below the cursor down by a given number of lines.
2589 *
2590 * This function respects the current scroll region.
2591 *
2592 * New rows are inserted at the top of the scroll region to fill the
2593 * vacated rows. The new rows not filled out with the current text attributes.
2594 *
2595 * This function does not affect the scrollback rows at all. Rows shifted
2596 * off the bottom are lost.
2597 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002598 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002599 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002600hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002601 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002602
rginda87b86462011-12-14 13:48:03 -08002603 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002604 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002605
rginda87b86462011-12-14 13:48:03 -08002606 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002607};
2608
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002609/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002610 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002611 *
2612 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002613 * cause Assitive Technology to announce the output of the terminal. It also
2614 * enables other features that aid assistive technology. All the features gated
2615 * behind this flag have a performance impact on the terminal which is why they
2616 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002617 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002618 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002619 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002620hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002621 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002622};
rginda87b86462011-12-14 13:48:03 -08002623
rginda8ba33642011-12-14 12:31:31 -08002624/**
2625 * Set the cursor position.
2626 *
2627 * The cursor row is relative to the scroll region if the terminal has
2628 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2629 *
Joel Hockey0f933582019-08-27 18:01:51 -07002630 * @param {number} row The new zero-based cursor row.
2631 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002632 */
2633hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2634 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002635 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002636 } else {
rginda87b86462011-12-14 13:48:03 -08002637 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002638 }
rginda87b86462011-12-14 13:48:03 -08002639};
rginda8ba33642011-12-14 12:31:31 -08002640
Evan Jones2600d4f2016-12-06 09:29:36 -05002641/**
2642 * Move the cursor relative to its current position.
2643 *
2644 * @param {number} row
2645 * @param {number} column
2646 */
rginda87b86462011-12-14 13:48:03 -08002647hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002648 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002649 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2650 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002651 this.screen_.setCursorPosition(row, column);
2652};
2653
Evan Jones2600d4f2016-12-06 09:29:36 -05002654/**
2655 * Move the cursor to the specified position.
2656 *
2657 * @param {number} row
2658 * @param {number} column
2659 */
rginda87b86462011-12-14 13:48:03 -08002660hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002661 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2662 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002663 this.screen_.setCursorPosition(row, column);
2664};
2665
2666/**
2667 * Set the cursor column.
2668 *
Joel Hockey0f933582019-08-27 18:01:51 -07002669 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002670 */
2671hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002672 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002673};
2674
2675/**
2676 * Return the cursor column.
2677 *
Joel Hockey0f933582019-08-27 18:01:51 -07002678 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002679 */
2680hterm.Terminal.prototype.getCursorColumn = function() {
2681 return this.screen_.cursorPosition.column;
2682};
2683
2684/**
2685 * Set the cursor row.
2686 *
2687 * The cursor row is relative to the scroll region if the terminal has
2688 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2689 *
Joel Hockey0f933582019-08-27 18:01:51 -07002690 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002691 */
rginda87b86462011-12-14 13:48:03 -08002692hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2693 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002694};
2695
2696/**
2697 * Return the cursor row.
2698 *
Joel Hockey0f933582019-08-27 18:01:51 -07002699 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002700 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002701hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002702 return this.screen_.cursorPosition.row;
2703};
2704
2705/**
2706 * Request that the ScrollPort redraw itself soon.
2707 *
2708 * The redraw will happen asynchronously, soon after the call stack winds down.
2709 * Multiple calls will be coalesced into a single redraw.
2710 */
2711hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002712 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002713 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002714 }
rginda8ba33642011-12-14 12:31:31 -08002715
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002716 this.timeouts_.redraw = setTimeout(() => {
2717 delete this.timeouts_.redraw;
2718 this.scrollPort_.redraw_();
2719 });
rginda8ba33642011-12-14 12:31:31 -08002720};
2721
2722/**
2723 * Request that the ScrollPort be scrolled to the bottom.
2724 *
2725 * The scroll will happen asynchronously, soon after the call stack winds down.
2726 * Multiple calls will be coalesced into a single scroll.
2727 *
2728 * This affects the scrollbar position of the ScrollPort, and has nothing to
2729 * do with the VT scroll commands.
2730 */
2731hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002732 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002733 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002734 }
rginda8ba33642011-12-14 12:31:31 -08002735
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002736 this.timeouts_.scrollDown = setTimeout(() => {
2737 delete this.timeouts_.scrollDown;
2738 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2739 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002740};
2741
2742/**
2743 * Move the cursor up a specified number of rows.
2744 *
Joel Hockey0f933582019-08-27 18:01:51 -07002745 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002746 */
2747hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002748 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002749};
2750
2751/**
2752 * Move the cursor down a specified number of rows.
2753 *
Joel Hockey0f933582019-08-27 18:01:51 -07002754 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002755 */
2756hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002757 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002758 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2759 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2760 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002761
Mike Frysingerdc727792020-04-10 01:41:13 -04002762 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2763 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002764 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002765};
2766
2767/**
2768 * Move the cursor left a specified number of columns.
2769 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002770 * If reverse wraparound mode is enabled and the previous row wrapped into
2771 * the current row then we back up through the wraparound as well.
2772 *
Joel Hockey0f933582019-08-27 18:01:51 -07002773 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002774 */
2775hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002776 count = count || 1;
2777
Mike Frysingerbdb34802020-04-07 03:47:32 -04002778 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002779 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002780 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002781
Mike Frysingerdc727792020-04-10 01:41:13 -04002782 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002783 if (this.options_.reverseWraparound) {
2784 if (this.screen_.cursorPosition.overflow) {
2785 // If this cursor is in the right margin, consume one count to get it
2786 // back to the last column. This only applies when we're in reverse
2787 // wraparound mode.
2788 count--;
2789 this.clearCursorOverflow();
2790
Mike Frysingerbdb34802020-04-07 03:47:32 -04002791 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002792 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002793 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002794 }
2795
Mike Frysingerdc727792020-04-10 01:41:13 -04002796 let newRow = this.screen_.cursorPosition.row;
2797 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002798 if (newColumn < 0) {
2799 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2800 if (newRow < 0) {
2801 // xterm also wraps from row 0 to the last row.
2802 newRow = this.screenSize.height + newRow % this.screenSize.height;
2803 }
2804 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2805 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002806
Robert Gindabfb32622014-07-17 13:20:27 -07002807 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2808
2809 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002810 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002811 this.setCursorColumn(newColumn);
2812 }
rginda8ba33642011-12-14 12:31:31 -08002813};
2814
2815/**
2816 * Move the cursor right a specified number of columns.
2817 *
Joel Hockey0f933582019-08-27 18:01:51 -07002818 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002819 */
2820hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002821 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002822
Mike Frysingerbdb34802020-04-07 03:47:32 -04002823 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002824 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002825 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002826
Mike Frysingerdc727792020-04-10 01:41:13 -04002827 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2828 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002829 this.setCursorColumn(column);
2830};
2831
2832/**
2833 * Reverse the foreground and background colors of the terminal.
2834 *
2835 * This only affects text that was drawn with no attributes.
2836 *
2837 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2838 * been drawn with attributes that happen to coincide with the default
2839 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002840 *
2841 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002842 */
2843hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002844 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002845 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002846 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2847 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002848 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002849 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2850 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002851 }
2852};
2853
2854/**
rginda87b86462011-12-14 13:48:03 -08002855 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002856 *
2857 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002858 */
2859hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002860 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002861
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002862 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002863
Michael Kelly485ecd12014-06-09 11:41:56 -04002864 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002865 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002866 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002867 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002868
Robert Ginda92e18102013-03-14 13:56:37 -07002869 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002870 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002871 this.bellSequelchTimeout_ = setTimeout(() => {
2872 this.bellSquelchTimeout_ = null;
2873 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002874 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002875 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002876 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002877
2878 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002879 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002880 this.bellNotificationList_.push(n);
2881 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002882 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002883 }
rginda87b86462011-12-14 13:48:03 -08002884};
2885
2886/**
rginda8ba33642011-12-14 12:31:31 -08002887 * Set the origin mode bit.
2888 *
2889 * If origin mode is on, certain VT cursor and scrolling commands measure their
2890 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2891 * to the top of the addressable screen.
2892 *
2893 * Defaults to off.
2894 *
2895 * @param {boolean} state True to set origin mode, false to unset.
2896 */
2897hterm.Terminal.prototype.setOriginMode = function(state) {
2898 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002899 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002900};
2901
2902/**
2903 * Set the insert mode bit.
2904 *
2905 * If insert mode is on, existing text beyond the cursor position will be
2906 * shifted right to make room for new text. Otherwise, new text overwrites
2907 * any existing text.
2908 *
2909 * Defaults to off.
2910 *
2911 * @param {boolean} state True to set insert mode, false to unset.
2912 */
2913hterm.Terminal.prototype.setInsertMode = function(state) {
2914 this.options_.insertMode = state;
2915};
2916
2917/**
rginda87b86462011-12-14 13:48:03 -08002918 * Set the auto carriage return bit.
2919 *
2920 * If auto carriage return is on then a formfeed character is interpreted
2921 * as a newline, otherwise it's the same as a linefeed. The difference boils
2922 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002923 *
2924 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002925 */
2926hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2927 this.options_.autoCarriageReturn = state;
2928};
2929
2930/**
rginda8ba33642011-12-14 12:31:31 -08002931 * Set the wraparound mode bit.
2932 *
2933 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2934 * to the start of the following row. Otherwise, the cursor is clamped to the
2935 * end of the screen and attempts to write past it are ignored.
2936 *
2937 * Defaults to on.
2938 *
2939 * @param {boolean} state True to set wraparound mode, false to unset.
2940 */
2941hterm.Terminal.prototype.setWraparound = function(state) {
2942 this.options_.wraparound = state;
2943};
2944
2945/**
2946 * Set the reverse-wraparound mode bit.
2947 *
2948 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2949 * to the end of the previous row. Otherwise, the cursor is clamped to column
2950 * 0.
2951 *
2952 * Defaults to off.
2953 *
2954 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2955 */
2956hterm.Terminal.prototype.setReverseWraparound = function(state) {
2957 this.options_.reverseWraparound = state;
2958};
2959
2960/**
2961 * Selects between the primary and alternate screens.
2962 *
2963 * If alternate mode is on, the alternate screen is active. Otherwise the
2964 * primary screen is active.
2965 *
2966 * Swapping screens has no effect on the scrollback buffer.
2967 *
2968 * Each screen maintains its own cursor position.
2969 *
2970 * Defaults to off.
2971 *
2972 * @param {boolean} state True to set alternate mode, false to unset.
2973 */
2974hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002975 if (state == (this.screen_ == this.alternateScreen_)) {
2976 return;
2977 }
2978 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2979 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002980 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2981
Joel Hockey42dba8f2020-03-26 16:21:11 -07002982 // Swap color overrides.
2983 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2984 oldOverrides.forEach((c, i) => {
2985 if (!newOverrides.hasOwnProperty(i)) {
2986 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2987 }
2988 });
2989 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2990
rginda35c456b2012-02-09 17:29:05 -08002991 if (this.screen_.rowsArray.length &&
2992 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2993 // If the screen changed sizes while we were away, our rowIndexes may
2994 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002995 const offset = this.scrollbackRows_.length;
2996 const ary = this.screen_.rowsArray;
2997 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002998 ary[i].rowIndex = offset + i;
2999 }
3000 }
rginda8ba33642011-12-14 12:31:31 -08003001
rginda35c456b2012-02-09 17:29:05 -08003002 this.realizeWidth_(this.screenSize.width);
3003 this.realizeHeight_(this.screenSize.height);
3004 this.scrollPort_.syncScrollHeight();
3005 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003006
rginda6d397402012-01-17 10:58:29 -08003007 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003008 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003009};
3010
3011/**
3012 * Set the cursor-blink mode bit.
3013 *
3014 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3015 * a visible cursor does not blink.
3016 *
3017 * You should make sure to turn blinking off if you're going to dispose of a
3018 * terminal, otherwise you'll leak a timeout.
3019 *
3020 * Defaults to on.
3021 *
3022 * @param {boolean} state True to set cursor-blink mode, false to unset.
3023 */
3024hterm.Terminal.prototype.setCursorBlink = function(state) {
3025 this.options_.cursorBlink = state;
3026
3027 if (!state && this.timeouts_.cursorBlink) {
3028 clearTimeout(this.timeouts_.cursorBlink);
3029 delete this.timeouts_.cursorBlink;
3030 }
3031
Mike Frysingerbdb34802020-04-07 03:47:32 -04003032 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003033 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003034 }
rginda8ba33642011-12-14 12:31:31 -08003035};
3036
3037/**
3038 * Set the cursor-visible mode bit.
3039 *
3040 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3041 *
3042 * Defaults to on.
3043 *
3044 * @param {boolean} state True to set cursor-visible mode, false to unset.
3045 */
3046hterm.Terminal.prototype.setCursorVisible = function(state) {
3047 this.options_.cursorVisible = state;
3048
3049 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003050 if (this.timeouts_.cursorBlink) {
3051 clearTimeout(this.timeouts_.cursorBlink);
3052 delete this.timeouts_.cursorBlink;
3053 }
rginda87b86462011-12-14 13:48:03 -08003054 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003055 return;
3056 }
3057
rginda87b86462011-12-14 13:48:03 -08003058 this.syncCursorPosition_();
3059
3060 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003061
3062 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003063 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003064 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003065 }
rginda8ba33642011-12-14 12:31:31 -08003066
Robert Gindaea2183e2014-07-17 09:51:51 -07003067 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003068 } else {
3069 if (this.timeouts_.cursorBlink) {
3070 clearTimeout(this.timeouts_.cursorBlink);
3071 delete this.timeouts_.cursorBlink;
3072 }
3073 }
3074};
3075
3076/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003077 * Pause blinking temporarily.
3078 *
3079 * When the cursor moves around, it can be helpful to momentarily pause the
3080 * blinking. This could be when the user is typing in things, or when they're
3081 * moving around with the arrow keys.
3082 */
3083hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3084 if (!this.options_.cursorBlink) {
3085 return;
3086 }
3087
3088 this.cursorBlinkPause_ = true;
3089
3090 // If a timeout is already pending, reset the clock due to the new input.
3091 if (this.timeouts_.cursorBlinkPause) {
3092 clearTimeout(this.timeouts_.cursorBlinkPause);
3093 }
3094 // After 500ms, resume blinking. That seems like a good balance between user
3095 // input timings & responsiveness to resume.
3096 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3097 delete this.timeouts_.cursorBlinkPause;
3098 this.cursorBlinkPause_ = false;
3099 }, 500);
3100};
3101
3102/**
rginda87b86462011-12-14 13:48:03 -08003103 * Synchronizes the visible cursor and document selection with the current
3104 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003105 *
3106 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003107 */
3108hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003109 const topRowIndex = this.scrollPort_.getTopRowIndex();
3110 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3111 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003112 this.screen_.cursorPosition.row;
3113
Raymes Khoury15697f42018-07-17 11:37:18 +10003114 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003115 if (this.accessibilityReader_.accessibilityEnabled) {
3116 // Report the new position of the cursor for accessibility purposes.
3117 const cursorColumnIndex = this.screen_.cursorPosition.column;
3118 const cursorLineText =
3119 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003120 // This will force the selection to be sync'd to the cursor position if the
3121 // user has pressed a key. Generally we would only sync the cursor position
3122 // when selection is collapsed so that if the user has selected something
3123 // we don't clear the selection by moving the selection. However when a
3124 // screen reader is used, it's intuitive for entering a key to move the
3125 // selection to the cursor.
3126 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003127 this.accessibilityReader_.afterCursorChange(
3128 cursorLineText, cursorRowIndex, cursorColumnIndex);
3129 }
3130
rginda8ba33642011-12-14 12:31:31 -08003131 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003132 // Cursor is scrolled off screen, hide it.
3133 this.cursorOffScreen_ = true;
3134 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003135 return false;
rginda8ba33642011-12-14 12:31:31 -08003136 }
3137
Joel Hockey3babf302020-04-22 15:00:06 -07003138 if (this.cursorNode_.style.display == 'none') {
3139 // Re-display the terminal cursor if it was hidden.
3140 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003141 this.cursorNode_.style.display = '';
3142 }
3143
Mike Frysinger44c32202017-08-05 01:13:09 -04003144 // Position the cursor using CSS variable math. If we do the math in JS,
3145 // the float math will end up being more precise than the CSS which will
3146 // cause the cursor tracking to be off.
3147 this.setCssVar(
3148 'cursor-offset-row',
3149 `${cursorRowIndex - topRowIndex} + ` +
3150 `${this.scrollPort_.visibleRowTopMargin}px`);
3151 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003152
3153 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003154 '(' + this.screen_.cursorPosition.column +
3155 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003156 ')');
3157
3158 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003159 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003160 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003161 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003162 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003163 return true;
rginda8ba33642011-12-14 12:31:31 -08003164};
3165
Robert Gindafb1be6a2013-12-11 11:56:22 -08003166/**
3167 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3168 * and character cell dimensions.
3169 */
Robert Ginda830583c2013-08-07 13:20:46 -07003170hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003171 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003172
3173 if (this.cursorNode_.getAttribute('focus') == 'false') {
3174 // Always show a block cursor when unfocused.
3175 shape = hterm.Terminal.cursorShape.BLOCK;
3176 }
3177
Mike Frysingerdc727792020-04-10 01:41:13 -04003178 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003179
3180 switch (shape) {
3181 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003182 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003183 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003184 style.borderLeftStyle = 'solid';
3185 break;
3186
3187 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003188 style.backgroundColor = 'transparent';
3189 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003190 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003191 break;
3192
3193 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003194 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003195 style.borderBottomStyle = '';
3196 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003197 break;
3198 }
3199};
3200
rginda8ba33642011-12-14 12:31:31 -08003201/**
3202 * Synchronizes the visible cursor with the current cursor coordinates.
3203 *
3204 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003205 * Multiple calls will be coalesced into a single sync. This should be called
3206 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003207 */
3208hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003209 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003210 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003211 }
rginda8ba33642011-12-14 12:31:31 -08003212
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003213 if (this.accessibilityReader_.accessibilityEnabled) {
3214 // Report the previous position of the cursor for accessibility purposes.
3215 const cursorRowIndex = this.scrollbackRows_.length +
3216 this.screen_.cursorPosition.row;
3217 const cursorColumnIndex = this.screen_.cursorPosition.column;
3218 const cursorLineText =
3219 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3220 this.accessibilityReader_.beforeCursorChange(
3221 cursorLineText, cursorRowIndex, cursorColumnIndex);
3222 }
3223
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003224 this.timeouts_.syncCursor = setTimeout(() => {
3225 this.syncCursorPosition_();
3226 delete this.timeouts_.syncCursor;
3227 });
rginda87b86462011-12-14 13:48:03 -08003228};
3229
rgindacc2996c2012-02-24 14:59:31 -08003230/**
rgindaf522ce02012-04-17 17:49:17 -07003231 * Show or hide the zoom warning.
3232 *
3233 * The zoom warning is a message warning the user that their browser zoom must
3234 * be set to 100% in order for hterm to function properly.
3235 *
3236 * @param {boolean} state True to show the message, false to hide it.
3237 */
3238hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3239 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003240 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003241 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003242 }
rgindaf522ce02012-04-17 17:49:17 -07003243
3244 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003245 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003246 this.zoomWarningNode_.style.cssText = (
3247 'color: black;' +
3248 'background-color: #ff2222;' +
3249 'font-size: large;' +
3250 'border-radius: 8px;' +
3251 'opacity: 0.75;' +
3252 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3253 'top: 0.5em;' +
3254 'right: 1.2em;' +
3255 'position: absolute;' +
3256 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003257 '-webkit-user-select: none;' +
3258 '-moz-text-size-adjust: none;' +
3259 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003260
3261 this.zoomWarningNode_.addEventListener('click', function(e) {
3262 this.parentNode.removeChild(this);
3263 });
rgindaf522ce02012-04-17 17:49:17 -07003264 }
3265
Mike Frysingerb7289952019-03-23 16:05:38 -07003266 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003267 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003268 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003269
rgindaf522ce02012-04-17 17:49:17 -07003270 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3271
3272 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003273 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003274 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003275 }
rgindaf522ce02012-04-17 17:49:17 -07003276 } else if (this.zoomWarningNode_.parentNode) {
3277 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3278 }
3279};
3280
3281/**
rgindacc2996c2012-02-24 14:59:31 -08003282 * Show the terminal overlay for a given amount of time.
3283 *
Jason Lin3d825782020-05-12 11:02:48 +10003284 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003285 *
3286 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003287 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003288 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3289 * stay up forever (or until the next overlay).
3290 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003291hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003292 this.showOverlayWithNode(new Text(msg), timeout);
3293};
3294
3295/**
3296 * Show the terminal overlay for a given amount of time.
3297 *
3298 * The terminal overlay appears in inverse video, centered over the terminal.
3299 *
3300 * @param {!Node} node The node to display in the overlay.
3301 * @param {?number=} timeout The amount of time to wait before fading out
3302 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3303 * stay up forever (or until the next overlay).
3304 */
3305hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003306 if (!this.ready_ || !this.div_) {
3307 return;
3308 }
rgindaf0090c92012-02-10 14:58:52 -08003309
Joel Hockeyedac0e72020-05-14 20:16:20 -07003310 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003311 this.overlayNode_ = this.document_.createElement('div');
3312 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003313 'color: rgb(var(--hterm-background-color));' +
3314 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003315 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003316 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003317 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003318 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003319 'position: absolute;' +
3320 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003321 '-webkit-transition: opacity 180ms ease-in;' +
3322 '-moz-user-select: none;' +
3323 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003324
3325 this.overlayNode_.addEventListener('mousedown', function(e) {
3326 e.preventDefault();
3327 e.stopPropagation();
3328 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003329 }
3330
Jason Lin34567412020-05-14 10:32:09 +10003331 this.overlayNode_.textContent = ''; // Remove all children first.
3332 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003333
Mike Frysingerbdb34802020-04-07 03:47:32 -04003334 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003335 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003336 }
rgindaf0090c92012-02-10 14:58:52 -08003337
Mike Frysingerdc727792020-04-10 01:41:13 -04003338 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3339 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003340
Robert Ginda8a59f762014-07-23 11:29:55 -07003341 this.overlayNode_.style.top =
3342 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003343 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003344 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003345
Mike Frysingerbdb34802020-04-07 03:47:32 -04003346 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003347 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003348 }
rgindaf0090c92012-02-10 14:58:52 -08003349
Jason Lin34567412020-05-14 10:32:09 +10003350 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003351
Mike Frysingerec4225d2020-04-07 05:00:01 -04003352 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003353 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003354 }
rgindacc2996c2012-02-24 14:59:31 -08003355
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003356 this.overlayTimeout_ = setTimeout(() => {
3357 this.overlayNode_.style.opacity = '0';
3358 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003359 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003360};
3361
3362/**
3363 * Hide the terminal overlay immediately.
3364 *
3365 * Useful when we show an overlay for an event with an unknown end time.
3366 */
3367hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003368 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003369 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003370 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003371 this.overlayTimeout_ = null;
3372
Mike Frysingerbdb34802020-04-07 03:47:32 -04003373 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003374 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003375 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003376 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003377};
3378
rginda4bba5e12012-06-20 16:15:30 -07003379/**
3380 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003381 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003382 * Note: In Chrome, this should work unless the user has rejected the permission
3383 * request. In Firefox extension environment, you'll need the "clipboardRead"
3384 * permission. In other environments, this might always fail as the browser
3385 * frequently blocks access for security reasons.
3386 *
3387 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3388 * value is always null. Otherwise, this function uses legacy pasting and
3389 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003390 */
3391hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003392 if (!this.alwaysUseLegacyPasting &&
3393 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003394 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3395 return null;
3396 } else {
3397 // Legacy pasting.
3398 try {
3399 return this.document_.execCommand('paste');
3400 } catch (firefoxException) {
3401 // Ignore this. FF 40 and older would incorrectly throw an exception if
3402 // there was an error instead of returning false.
3403 return false;
3404 }
3405 }
rginda4bba5e12012-06-20 16:15:30 -07003406};
3407
3408/**
3409 * Copy a string to the system clipboard.
3410 *
3411 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003412 *
3413 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003414 */
3415hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003416 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003417 if (!this.clipboardNotice_) {
3418 this.clipboardNotice_ = this.document_.createElement('div');
3419 this.clipboardNotice_.style.textAlign = 'center';
3420 const copyImage = lib.resource.getData('hterm/images/copy');
3421 this.clipboardNotice_.innerHTML =
3422 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3423 }
3424 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003425 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003426
Mike Frysinger96eacae2019-01-02 18:13:56 -05003427 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003428};
3429
Evan Jones2600d4f2016-12-06 09:29:36 -05003430/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003431 * Display an image.
3432 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003433 * Either URI or buffer or blob fields must be specified.
3434 *
Joel Hockey0f933582019-08-27 18:01:51 -07003435 * @param {{
3436 * name: (string|undefined),
3437 * size: (string|number|undefined),
3438 * preserveAspectRation: (boolean|undefined),
3439 * inline: (boolean|undefined),
3440 * width: (string|number|undefined),
3441 * height: (string|number|undefined),
3442 * align: (string|undefined),
3443 * url: (string|undefined),
3444 * buffer: (!ArrayBuffer|undefined),
3445 * blob: (!Blob|undefined),
3446 * type: (string|undefined),
3447 * }} options The image to display.
3448 * name A human readable string for the image
3449 * size The size (in bytes).
3450 * preserveAspectRatio Whether to preserve aspect.
3451 * inline Whether to display the image inline.
3452 * width The width of the image.
3453 * height The height of the image.
3454 * align Direction to align the image.
3455 * uri The source URI for the image.
3456 * buffer The ArrayBuffer image data.
3457 * blob The Blob image data.
3458 * type The MIME type of the image data.
3459 * @param {function()=} onLoad Callback when loading finishes.
3460 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003461 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003462hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003463 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003464 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003465 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003466 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003467 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003468
3469 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003470 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003471 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003472 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003473
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003474 // See if the mime type is available. If not, guess from the filename.
3475 // We don't list all possible mime types because the browser can usually
3476 // guess it correctly. So list the ones that need a bit more help.
3477 if (!options.type) {
3478 const ary = options.name.split('.');
3479 const ext = ary[ary.length - 1].trim();
3480 switch (ext) {
3481 case 'svg':
3482 case 'svgz':
3483 options.type = 'image/svg+xml';
3484 break;
3485 }
3486 }
3487
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003488 // Has the user approved image display yet?
3489 if (this.allowImagesInline !== true) {
3490 this.newLine();
3491 const row = this.getRowNode(this.scrollbackRows_.length +
3492 this.getCursorRow() - 1);
3493
3494 if (this.allowImagesInline === false) {
3495 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3496 'Inline Images Disabled');
3497 return;
3498 }
3499
3500 // Show a prompt.
3501 let button;
3502 const span = this.document_.createElement('span');
3503 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3504 span.style.fontWeight = 'bold';
3505 span.style.borderWidth = '1px';
3506 span.style.borderStyle = 'dashed';
3507 button = this.document_.createElement('span');
3508 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3509 button.style.marginLeft = '1em';
3510 button.style.borderWidth = '1px';
3511 button.style.borderStyle = 'solid';
3512 button.addEventListener('click', () => {
3513 this.prefs_.set('allow-images-inline', false);
3514 });
3515 span.appendChild(button);
3516 button = this.document_.createElement('span');
3517 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3518 'allow this session');
3519 button.style.marginLeft = '1em';
3520 button.style.borderWidth = '1px';
3521 button.style.borderStyle = 'solid';
3522 button.addEventListener('click', () => {
3523 this.allowImagesInline = true;
3524 });
3525 span.appendChild(button);
3526 button = this.document_.createElement('span');
3527 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3528 button.style.marginLeft = '1em';
3529 button.style.borderWidth = '1px';
3530 button.style.borderStyle = 'solid';
3531 button.addEventListener('click', () => {
3532 this.prefs_.set('allow-images-inline', true);
3533 });
3534 span.appendChild(button);
3535
3536 row.appendChild(span);
3537 return;
3538 }
3539
3540 // See if we should show this object directly, or download it.
3541 if (options.inline) {
3542 const io = this.io.push();
3543 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003544 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003545
3546 // While we're loading the image, eat all the user's input.
3547 io.onVTKeystroke = io.sendString = () => {};
3548
3549 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003550 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003551 if (options.uri !== undefined) {
3552 img.src = options.uri;
3553 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003554 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003555 img.src = URL.createObjectURL(blob);
3556 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003557 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003558 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003559 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003560 img.title = img.alt = options.name;
3561
3562 // Attach the image to the page to let it load/render. It won't stay here.
3563 // This is needed so it's visible and the DOM can calculate the height. If
3564 // the image is hidden or not in the DOM, the height is always 0.
3565 this.document_.body.appendChild(img);
3566
3567 // Wait for the image to finish loading before we try moving it to the
3568 // right place in the terminal.
3569 img.onload = () => {
3570 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003571 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003572 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003573 img.style.maxWidth = `${screenSize.width}px`;
3574 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003575
3576 // Parse a width/height specification.
3577 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003578 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003579 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003580 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003581
3582 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3583 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003584 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003585 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003586 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003588 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003589 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003590 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003591 }
3592
3593 return '';
3594 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003595 img.style.width = parseDim(
3596 options.width, screenSize.width, '--hterm-charsize-width');
3597 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003598 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003599
3600 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003601 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003602 const padRows = Math.ceil(img.clientHeight /
3603 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003604 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003605 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003606 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003607
3608 // Update the max height in case the user shrinks the character size.
3609 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3610
3611 // Move the image to the last row. This way when we scroll up, it doesn't
3612 // disappear when the first row gets clipped. It will disappear when we
3613 // scroll down and the last row is clipped ...
3614 this.document_.body.removeChild(img);
3615 // Create a wrapper node so we can do an absolute in a relative position.
3616 // This helps with rounding errors between JS & CSS counts.
3617 const div = this.document_.createElement('div');
3618 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003619 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003620 img.style.position = 'absolute';
3621 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3622 div.appendChild(img);
3623 const row = this.getRowNode(this.scrollbackRows_.length +
3624 this.getCursorRow() - 1);
3625 row.appendChild(div);
3626
Mike Frysinger2558ed52019-01-14 01:03:41 -05003627 // Now that the image has been read, we can revoke the source.
3628 if (options.uri === undefined) {
3629 URL.revokeObjectURL(img.src);
3630 }
3631
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003632 io.hideOverlay();
3633 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003634
Mike Frysingerbdb34802020-04-07 03:47:32 -04003635 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003636 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003637 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003638 };
3639
3640 // If we got a malformed image, give up.
3641 img.onerror = (e) => {
3642 this.document_.body.removeChild(img);
3643 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003644 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003645 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003646
Mike Frysingerbdb34802020-04-07 03:47:32 -04003647 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003648 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003649 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003650 };
3651 } else {
3652 // We can't use chrome.downloads.download as that requires "downloads"
3653 // permissions, and that works only in extensions, not apps.
3654 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003655 if (options.uri !== undefined) {
3656 a.href = options.uri;
3657 } else if (options.buffer !== undefined) {
3658 const blob = new Blob([options.buffer]);
3659 a.href = URL.createObjectURL(blob);
3660 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003661 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003662 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003663 a.download = options.name;
3664 this.document_.body.appendChild(a);
3665 a.click();
3666 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003667 if (options.uri === undefined) {
3668 URL.revokeObjectURL(a.href);
3669 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003670 }
3671};
3672
3673/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003674 * Returns the selected text, or null if no text is selected.
3675 *
3676 * @return {string|null}
3677 */
rgindaa09e7332012-08-17 12:49:51 -07003678hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003679 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003680 selection.sync();
3681
Mike Frysingerbdb34802020-04-07 03:47:32 -04003682 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003683 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003684 }
rgindaa09e7332012-08-17 12:49:51 -07003685
rgindaa09e7332012-08-17 12:49:51 -07003686 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003687 let startOffset = selection.startOffset;
3688 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003689
Raymes Khoury334625a2018-06-25 10:29:40 +10003690 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003691 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003692 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003693 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003694
Robert Gindafdbb3f22012-09-06 20:23:06 -07003695 if (node.nodeName != 'X-ROW') {
3696 // If the selection doesn't start on an x-row node, then it must be
3697 // somewhere inside the x-row. Add any characters from previous siblings
3698 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003699
3700 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3701 // If node is the text node in a styled span, move up to the span node.
3702 node = node.parentNode;
3703 }
3704
Robert Gindafdbb3f22012-09-06 20:23:06 -07003705 while (node.previousSibling) {
3706 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003707 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003708 }
rgindaa09e7332012-08-17 12:49:51 -07003709 }
3710
3711 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003712 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003713 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003714 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003715
Robert Gindafdbb3f22012-09-06 20:23:06 -07003716 if (node.nodeName != 'X-ROW') {
3717 // If the selection doesn't end on an x-row node, then it must be
3718 // somewhere inside the x-row. Add any characters from following siblings
3719 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003720
3721 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3722 // If node is the text node in a styled span, move up to the span node.
3723 node = node.parentNode;
3724 }
3725
Robert Gindafdbb3f22012-09-06 20:23:06 -07003726 while (node.nextSibling) {
3727 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003728 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003729 }
rgindaa09e7332012-08-17 12:49:51 -07003730 }
3731
Mike Frysingerdc727792020-04-10 01:41:13 -04003732 const rv = this.getRowsText(selection.startRow.rowIndex,
3733 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003734 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003735};
3736
rginda4bba5e12012-06-20 16:15:30 -07003737/**
3738 * Copy the current selection to the system clipboard, then clear it after a
3739 * short delay.
3740 */
3741hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003742 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003743 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003744 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003745 }
rginda4bba5e12012-06-20 16:15:30 -07003746};
3747
Joel Hockey0f933582019-08-27 18:01:51 -07003748/**
3749 * Show overlay with current terminal size.
3750 */
rgindaf0090c92012-02-10 14:58:52 -08003751hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003752 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003753 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003754 }
rgindaf0090c92012-02-10 14:58:52 -08003755};
3756
rginda87b86462011-12-14 13:48:03 -08003757/**
3758 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3759 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003760 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003761 */
3762hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003763 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003764 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003765 }
rginda87b86462011-12-14 13:48:03 -08003766
Mike Frysinger225c99d2019-10-20 14:02:37 -06003767 this.pauseCursorBlink_();
3768
Mike Frysinger79669762018-12-30 20:51:10 -05003769 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003770};
3771
3772/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003773 * Open the selected url.
3774 */
3775hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003776 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003777
3778 // If there is no selection, try and expand wherever they clicked.
3779 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003780 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003781 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003782
3783 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003784 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003785 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003786 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003787 }
3788
3789 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003790 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003791 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003792 }
Mike Frysinger43472622017-06-26 18:11:07 -04003793
3794 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003795 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003796 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3797 // We have to whitelist a few protocols that lack authorities and thus
3798 // never use the //. Like mailto.
3799 switch (str.split(':', 1)[0]) {
3800 case 'mailto':
3801 break;
3802 default:
3803 str = 'http://' + str;
3804 break;
3805 }
3806 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003807
Mike Frysinger720fa832017-10-23 01:15:52 -04003808 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003809};
Mike Frysinger70b94692017-01-26 18:57:50 -10003810
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003811/**
3812 * Manage the automatic mouse hiding behavior while typing.
3813 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003814 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003815 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003816hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003817 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3818 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003819 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003820 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003821 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003822
3823 this.mouseHideWhileTyping_ = !!v;
3824};
3825
3826/**
3827 * Handler for monitoring user keyboard activity.
3828 *
3829 * This isn't for processing the keystrokes directly, but for updating any
3830 * state that might toggle based on the user using the keyboard at all.
3831 *
Joel Hockey0f933582019-08-27 18:01:51 -07003832 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003833 */
3834hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3835 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003836 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003837 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003838 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003839};
Mike Frysinger70b94692017-01-26 18:57:50 -10003840
3841/**
rgindad5613292012-06-19 15:40:37 -07003842 * Add the terminalRow and terminalColumn properties to mouse events and
3843 * then forward on to onMouse().
3844 *
3845 * The terminalRow and terminalColumn properties contain the (row, column)
3846 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003847 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003848 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003849 */
3850hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003851 if (e.processedByTerminalHandler_) {
3852 // We register our event handlers on the document, as well as the cursor
3853 // and the scroll blocker. Mouse events that occur on the cursor or
3854 // scroll blocker will also appear on the document, but we don't want to
3855 // process them twice.
3856 //
3857 // We can't just prevent bubbling because that has other side effects, so
3858 // we decorate the event object with this property instead.
3859 return;
3860 }
3861
Mike Frysinger468966c2018-08-28 13:48:51 -04003862 // Consume navigation events. Button 3 is usually "browser back" and
3863 // button 4 is "browser forward" which we don't want to happen.
3864 if (e.button > 2) {
3865 e.preventDefault();
3866 // We don't return so click events can be passed to the remote below.
3867 }
3868
Mike Frysingerdc727792020-04-10 01:41:13 -04003869 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003870 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3871
rgindafaa74742012-08-21 13:34:03 -07003872 e.processedByTerminalHandler_ = true;
3873
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003874 // Handle auto hiding of mouse cursor while typing.
3875 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3876 // Make sure the mouse cursor is visible.
3877 this.syncMouseStyle();
3878 // This debounce isn't perfect, but should work well enough for such a
3879 // simple implementation. If the user moved the mouse, we enabled this
3880 // debounce, and then moved the mouse just before the timeout, we wouldn't
3881 // debounce that later movement.
3882 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3883 }
3884
Robert Gindaeda48db2014-07-17 09:25:30 -07003885 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003886 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003887 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003888 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003889 this.scrollPort_.characterSize.height) + 1;
3890 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003891 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003892
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003893 // Clamp row and column.
3894 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3895 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3896
3897 // Ignore mousedown in the scrollbar area.
3898 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003899 return;
3900 }
3901
Joel Hockey3babf302020-04-22 15:00:06 -07003902 if (this.options_.cursorVisible && !reportMouseEvents &&
3903 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003904 // If the cursor is visible and we're not sending mouse events to the
3905 // host app, then we want to hide the terminal cursor when the mouse
3906 // cursor is over top. This keeps the terminal cursor from interfering
3907 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003908 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3909 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3910 this.cursorNode_.style.display = 'none';
3911 } else if (this.cursorNode_.style.display == 'none') {
3912 this.cursorNode_.style.display = '';
3913 }
3914 }
rgindad5613292012-06-19 15:40:37 -07003915
Robert Ginda928cf632014-03-05 15:07:41 -08003916 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003917 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003918
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003919 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003920 // If VT mouse reporting is disabled, or has been defeated with
3921 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003922 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003923 this.setSelectionEnabled(true);
3924 } else {
3925 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003926 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003927 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003928 this.setSelectionEnabled(false);
3929 e.preventDefault();
3930 }
3931 }
3932
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003933 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003934 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003935 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003936 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003937 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003938 }
rgindad5613292012-06-19 15:40:37 -07003939 }
3940
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003941 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003942 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003943 // Ignore links created using OSC-8 as those will open by themselves, and
3944 // the visible text is most likely not the URI they want anyways.
3945 if (e.target.className === 'uri-node') {
3946 return;
3947 }
3948
Mike Frysinger70b94692017-01-26 18:57:50 -10003949 // Debounce this event with the dblclick event. If you try to doubleclick
3950 // a URL to open it, Chrome will fire click then dblclick, but we won't
3951 // have expanded the selection text at the first click event.
3952 clearTimeout(this.timeouts_.openUrl);
3953 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3954 500);
3955 return;
3956 }
3957
Mike Frysinger847577f2017-05-23 23:25:57 -04003958 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003959 if (e.ctrlKey && e.button == 2 /* right button */) {
3960 e.preventDefault();
3961 this.contextMenu.show(e, this);
3962 } else if (e.button == this.mousePasteButton ||
3963 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003964 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003965 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003966 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003967 }
3968 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003969
Mike Frysinger2edd3612017-05-24 00:54:39 -04003970 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003971 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003972 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003973 }
3974
3975 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3976 this.scrollBlockerNode_.engaged) {
3977 // Disengage the scroll-blocker after one of these events.
3978 this.scrollBlockerNode_.engaged = false;
3979 this.scrollBlockerNode_.style.top = '-99px';
3980 }
3981
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003982 // Emulate arrow key presses via scroll wheel events.
3983 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3984 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003985 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003986 const delta =
3987 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003988
Mike Frysinger321063c2018-08-29 15:33:14 -04003989 // Helper to turn a wheel event delta into a series of key presses.
3990 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3991 if (distance == 0) {
3992 return '';
3993 }
3994
3995 // Convert the scroll distance into a number of rows/cols.
3996 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3997 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3998 return data.repeat(cells);
3999 };
4000
4001 // The order between up/down and left/right doesn't really matter.
4002 this.io.sendString(
4003 // Up/down arrow keys.
4004 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4005 'A', 'B') +
4006 // Left/right arrow keys.
4007 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004008 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004009 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004010
4011 e.preventDefault();
4012 }
4013 }
Robert Ginda928cf632014-03-05 15:07:41 -08004014 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004015 if (!this.scrollBlockerNode_.engaged) {
4016 if (e.type == 'mousedown') {
4017 // Move the scroll-blocker into place if we want to keep the scrollport
4018 // from scrolling.
4019 this.scrollBlockerNode_.engaged = true;
4020 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4021 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4022 } else if (e.type == 'mousemove') {
4023 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4024 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004025 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004026 e.preventDefault();
4027 }
4028 }
Robert Ginda928cf632014-03-05 15:07:41 -08004029
4030 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004031 }
4032
Robert Ginda928cf632014-03-05 15:07:41 -08004033 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4034 // Restore this on mouseup in case it was temporarily defeated with a
4035 // alt-mousedown. Only do this when the selection is empty so that
4036 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004037 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004038 }
rgindad5613292012-06-19 15:40:37 -07004039};
4040
4041/**
4042 * Clients should override this if they care to know about mouse events.
4043 *
4044 * The event parameter will be a normal DOM mouse click event with additional
4045 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004046 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004047 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004048 */
4049hterm.Terminal.prototype.onMouse = function(e) { };
4050
4051/**
rginda8e92a692012-05-20 19:37:20 -07004052 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004053 *
4054 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004055 */
Rob Spies06533ba2014-04-24 11:20:37 -07004056hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4057 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004058 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004059
Mike Frysingerbdb34802020-04-07 03:47:32 -04004060 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004061 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004062 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004063
Mike Frysingerbdb34802020-04-07 03:47:32 -04004064 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004065 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004066 }
rginda8e92a692012-05-20 19:37:20 -07004067};
4068
4069/**
rginda8ba33642011-12-14 12:31:31 -08004070 * React when the ScrollPort is scrolled.
4071 */
4072hterm.Terminal.prototype.onScroll_ = function() {
4073 this.scheduleSyncCursorPosition_();
4074};
4075
4076/**
rginda9846e2f2012-01-27 13:53:33 -08004077 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004078 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004079 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004080 */
4081hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004082 this.onPasteData_(e.text);
4083};
4084
4085/**
4086 * Handle pasted data.
4087 *
4088 * @param {string} data The pasted data.
4089 */
4090hterm.Terminal.prototype.onPasteData_ = function(data) {
4091 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004092 if (this.options_.bracketedPaste) {
4093 // We strip out most escape sequences as they can cause issues (like
4094 // inserting an \x1b[201~ midstream). We pass through whitespace
4095 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4096 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004097 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004098 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4099 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4100 }
Robert Gindaa063b202014-07-21 11:08:25 -07004101
4102 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004103};
4104
4105/**
rgindaa09e7332012-08-17 12:49:51 -07004106 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004107 *
Joel Hockey0f933582019-08-27 18:01:51 -07004108 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004109 */
4110hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004111 if (!this.useDefaultWindowCopy) {
4112 e.preventDefault();
4113 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4114 }
rgindaa09e7332012-08-17 12:49:51 -07004115};
4116
4117/**
rginda8ba33642011-12-14 12:31:31 -08004118 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004119 *
4120 * Note: This function should not directly contain code that alters the internal
4121 * state of the terminal. That kind of code belongs in realizeWidth or
4122 * realizeHeight, so that it can be executed synchronously in the case of a
4123 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004124 */
4125hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004126 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4127 this.scrollPort_.characterSize.width) || 0;
4128 const rowCount = lib.f.smartFloorDivide(
4129 this.scrollPort_.getScreenHeight(),
4130 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004131
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004132 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004133 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004134 // gets removed from the document or during the initial load, and we can't
4135 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004136 // This can also happen if called before the scrollPort calculates the
4137 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004138 return;
4139 }
4140
Mike Frysingerdc727792020-04-10 01:41:13 -04004141 const isNewSize = (columnCount != this.screenSize.width ||
4142 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004143 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004144
4145 // We do this even if the size didn't change, just to be sure everything is
4146 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004147 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004148 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004149
Mike Frysingerbdb34802020-04-07 03:47:32 -04004150 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004151 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004152 }
rgindaa8ba17d2012-08-15 14:41:10 -07004153
Robert Gindafb1be6a2013-12-11 11:56:22 -08004154 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004155 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004156
4157 if (wasScrolledEnd) {
4158 this.scrollEnd();
4159 }
rginda8ba33642011-12-14 12:31:31 -08004160};
4161
4162/**
4163 * Service the cursor blink timeout.
4164 */
4165hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004166 if (!this.options_.cursorBlink) {
4167 delete this.timeouts_.cursorBlink;
4168 return;
4169 }
4170
Robert Ginda830583c2013-08-07 13:20:46 -07004171 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004172 this.cursorNode_.style.opacity == '0' ||
4173 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004174 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004175 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4176 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004177 } else {
rginda87b86462011-12-14 13:48:03 -08004178 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004179 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4180 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004181 }
4182};
David Reveman8f552492012-03-28 12:18:41 -04004183
4184/**
4185 * Set the scrollbar-visible mode bit.
4186 *
4187 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4188 * Otherwise it will not.
4189 *
4190 * Defaults to on.
4191 *
4192 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4193 */
4194hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4195 this.scrollPort_.setScrollbarVisible(state);
4196};
Michael Kelly485ecd12014-06-09 11:41:56 -04004197
4198/**
Rob Spies49039e52014-12-17 13:40:04 -08004199 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004200 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004201 *
4202 * Defaults to 1.
4203 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004204 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004205 */
4206hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4207 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4208};
4209
4210/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004211 * Close all web notifications created by terminal bells.
4212 */
4213hterm.Terminal.prototype.closeBellNotifications_ = function() {
4214 this.bellNotificationList_.forEach(function(n) {
4215 n.close();
4216 });
4217 this.bellNotificationList_.length = 0;
4218};
Raymes Khourye5d48982018-08-02 09:08:32 +10004219
4220/**
4221 * Syncs the cursor position when the scrollport gains focus.
4222 */
4223hterm.Terminal.prototype.onScrollportFocus_ = function() {
4224 // If the cursor is offscreen we set selection to the last row on the screen.
4225 const topRowIndex = this.scrollPort_.getTopRowIndex();
4226 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4227 const selection = this.document_.getSelection();
4228 if (!this.syncCursorPosition_() && selection) {
4229 selection.collapse(this.getRowNode(bottomRowIndex));
4230 }
4231};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004232
4233/**
4234 * Clients can override this if they want to provide an options page.
4235 */
4236hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4237
4238
4239/**
4240 * Called when user selects to open the options page.
4241 */
4242hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4243 this.onOpenOptionsPage();
4244};