blob: b6ceecd73f1f8596f90655482868d398dc9521fb [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 *
Mike Frysinger1bcf4012020-10-23 01:56:47 -040024 * @param {{
25 * profileId: (?string|undefined),
26 * }=} options Various settings to control behavior.
27 * profileId: The preference profile name. Defaults to "default".
Joel Hockey0f933582019-08-27 18:01:51 -070028 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070029 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080030 */
Mike Frysinger1bcf4012020-10-23 01:56:47 -040031hterm.Terminal = function({profileId} = {}) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070032 // Set to true once terminal is initialized and onTerminalReady() is called.
33 this.ready_ = false;
34
Robert Ginda57f03b42012-09-13 11:02:48 -070035 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080036
Joel Hockeyd4fca732019-09-20 16:57:03 -070037 /** @type {?hterm.PreferenceManager} */
38 this.prefs_ = null;
39
rginda8ba33642011-12-14 12:31:31 -080040 // Two screen instances.
41 this.primaryScreen_ = new hterm.Screen();
42 this.alternateScreen_ = new hterm.Screen();
43
44 // The "current" screen.
45 this.screen_ = this.primaryScreen_;
46
rginda8ba33642011-12-14 12:31:31 -080047 // The local notion of the screen size. ScreenBuffers also have a size which
48 // indicates their present size. During size changes, the two may disagree.
49 // Also, the inactive screen's size is not altered until it is made the active
50 // screen.
51 this.screenSize = new hterm.Size(0, 0);
52
rginda8ba33642011-12-14 12:31:31 -080053 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080054 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080055 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
56 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080057 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100058 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070059 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070060 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080061
rginda87b86462011-12-14 13:48:03 -080062 // The div that contains this terminal.
63 this.div_ = null;
64
rgindac9bc5502012-01-18 11:48:44 -080065 // The document that contains the scrollPort. Defaulted to the global
66 // document here so that the terminal is functional even if it hasn't been
67 // inserted into a document yet, but re-set in decorate().
68 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080069
rginda8ba33642011-12-14 12:31:31 -080070 // The rows that have scrolled off screen and are no longer addressable.
71 this.scrollbackRows_ = [];
72
rgindac9bc5502012-01-18 11:48:44 -080073 // Saved tab stops.
74 this.tabStops_ = [];
75
David Benjamin66e954d2012-05-05 21:08:12 -040076 // Keep track of whether default tab stops have been erased; after a TBC
77 // clears all tab stops, defaults aren't restored on resize until a reset.
78 this.defaultTabStops = true;
79
rginda8ba33642011-12-14 12:31:31 -080080 // The VT's notion of the top and bottom rows. Used during some VT
81 // cursor positioning and scrolling commands.
82 this.vtScrollTop_ = null;
83 this.vtScrollBottom_ = null;
84
85 // The DIV element for the visible cursor.
86 this.cursorNode_ = null;
87
Robert Ginda830583c2013-08-07 13:20:46 -070088 // The current cursor shape of the terminal.
89 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
90
Robert Gindaea2183e2014-07-17 09:51:51 -070091 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
92 this.cursorBlinkCycle_ = [100, 100];
93
Mike Frysinger225c99d2019-10-20 14:02:37 -060094 // Whether to temporarily disable blinking.
95 this.cursorBlinkPause_ = false;
96
Joel Hockey3babf302020-04-22 15:00:06 -070097 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
98 this.cursorOffScreen_ = false;
99
Robert Gindaea2183e2014-07-17 09:51:51 -0700100 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
101 // cursor on/off servicing.
102 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
103
rginda9f5222b2012-03-05 11:53:28 -0800104 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700105 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700106 /** @type {?string} */
107 this.backgroundColor_ = null;
108 /** @type {?string} */
109 this.foregroundColor_ = null;
110
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400111 /** @type {!Map<number, string>} */
112 this.colorPaletteOverrides_ = new Map();
113
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700114 this.screenBorderSize_ = 0;
115
Robert Ginda57f03b42012-09-13 11:02:48 -0700116 this.scrollOnOutput_ = null;
117 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400118 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800119
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700120 // True if we should override mouse event reporting to allow local selection.
121 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800122
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400123 // Whether to auto hide the mouse cursor when typing.
124 this.setAutomaticMouseHiding();
125 // Timer to keep mouse visible while it's being used.
126 this.mouseHideDelay_ = null;
127
rgindaf0090c92012-02-10 14:58:52 -0800128 // Terminal bell sound.
129 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400130 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800131 this.bellAudio_.setAttribute('preload', 'auto');
132
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000133 // The AccessibilityReader object for announcing command output.
134 this.accessibilityReader_ = null;
135
Mike Frysingercc114512017-09-11 21:39:17 -0400136 // The context menu object.
137 this.contextMenu = new hterm.ContextMenu();
138
Michael Kelly485ecd12014-06-09 11:41:56 -0400139 // All terminal bell notifications that have been generated (not necessarily
140 // shown).
141 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700142 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400143
144 // Whether we have permission to display notifications.
145 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400146
rginda6d397402012-01-17 10:58:29 -0800147 // Cursor position and attributes saved with DECSC.
148 this.savedOptions_ = {};
149
rginda8ba33642011-12-14 12:31:31 -0800150 // The current mode bits for the terminal.
151 this.options_ = new hterm.Options();
152
153 // Timeouts we might need to clear.
154 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800155
156 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800157 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800158
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800159 this.saveCursorAndState(true);
160
Zhu Qunying30d40712017-03-14 16:27:00 -0700161 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800162 this.keyboard = new hterm.Keyboard(this);
163
rginda87b86462011-12-14 13:48:03 -0800164 // General IO interface that can be given to third parties without exposing
165 // the entire terminal object.
166 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800167
rgindad5613292012-06-19 15:40:37 -0700168 // True if mouse-click-drag should scroll the terminal.
169 this.enableMouseDragScroll = true;
170
Robert Ginda57f03b42012-09-13 11:02:48 -0700171 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400172 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700173 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700174
Zhu Qunying30d40712017-03-14 16:27:00 -0700175 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700176 this.useDefaultWindowCopy = false;
177
178 this.clearSelectionAfterCopy = true;
179
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400180 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800181 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700182
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400183 // Whether we allow images to be shown.
184 this.allowImagesInline = null;
185
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400186 this.reportFocus = false;
187
Jason Linf129f3c2020-03-23 11:52:08 +1100188 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
189 this.alwaysUseLegacyPasting = false;
190
Joel Hockey3a44a442019-10-14 16:22:56 -0700191 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500192 function() { this.onTerminalReady(); }.bind(this));
shivanggargde5387e2020-06-10 01:31:16 +0530193
194 /** @const */
195 this.findBar = new hterm.FindBar(this);
rginda87b86462011-12-14 13:48:03 -0800196};
197
198/**
Robert Ginda830583c2013-08-07 13:20:46 -0700199 * Possible cursor shapes.
200 */
201hterm.Terminal.cursorShape = {
202 BLOCK: 'BLOCK',
203 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400204 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700205};
206
207/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 * Clients should override this to be notified when the terminal is ready
209 * for use.
210 *
211 * The terminal initialization is asynchronous, and shouldn't be used before
212 * this method is called.
213 */
214hterm.Terminal.prototype.onTerminalReady = function() { };
215
216/**
rginda35c456b2012-02-09 17:29:05 -0800217 * Default tab with of 8 to match xterm.
218 */
219hterm.Terminal.prototype.tabWidth = 8;
220
221/**
rginda9f5222b2012-03-05 11:53:28 -0800222 * Select a preference profile.
223 *
224 * This will load the terminal preferences for the given profile name and
225 * associate subsequent preference changes with the new preference profile.
226 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500227 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800228 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400229 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700230 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800231 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400232hterm.Terminal.prototype.setProfile = function(
233 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800235
Mike Frysingerdc727792020-04-10 01:41:13 -0400236 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800237
Mike Frysingerbdb34802020-04-07 03:47:32 -0400238 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400240 }
rginda9f5222b2012-03-05 11:53:28 -0800241
Robert Ginda57f03b42012-09-13 11:02:48 -0700242 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700243
244 /**
245 * Clears and reloads key bindings. Used by preferences
246 * 'keybindings' and 'keybindings-os-defaults'.
247 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400248 * @param {*?=} bindings
249 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700250 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400251 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700252 terminal.keyboard.bindings.clear();
253
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400254 // Default to an empty object so we still handle OS defaults.
255 if (bindings === null) {
256 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700257 }
258
259 if (!(bindings instanceof Object)) {
260 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400261 bindings = {};
262 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700263 }
264
265 try {
266 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
267 } catch (ex) {
268 console.error('Error in keybindings preference: ' + ex);
269 }
270 }
271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800273 'alt-gr-mode': function(v) {
274 if (v == null) {
275 if (navigator.language.toLowerCase() == 'en-us') {
276 v = 'none';
277 } else {
278 v = 'right-alt';
279 }
280 } else if (typeof v == 'string') {
281 v = v.toLowerCase();
282 } else {
283 v = 'none';
284 }
285
Mike Frysingerbdb34802020-04-07 03:47:32 -0400286 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800287 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400288 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800289
290 terminal.keyboard.altGrMode = v;
291 },
292
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700293 'alt-backspace-is-meta-backspace': function(v) {
294 terminal.keyboard.altBackspaceIsMetaBackspace = v;
295 },
296
Robert Ginda57f03b42012-09-13 11:02:48 -0700297 'alt-is-meta': function(v) {
298 terminal.keyboard.altIsMeta = v;
299 },
300
301 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400302 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700303 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400304 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700305
306 terminal.keyboard.altSendsWhat = v;
307 },
308
309 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400310 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800311 if (ary) {
312 terminal.bellAudio_.setAttribute('src',
313 lib.resource.getDataUrl(ary[1]));
314 } else {
315 terminal.bellAudio_.setAttribute('src', v);
316 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 },
318
Michael Kelly485ecd12014-06-09 11:41:56 -0400319 'desktop-notification-bell': function(v) {
320 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700321 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400322 Notification.permission === 'granted';
323 if (!terminal.desktopNotificationBell_) {
324 // Note: We don't call Notification.requestPermission here because
325 // Chrome requires the call be the result of a user action (such as an
326 // onclick handler), and pref listeners are run asynchronously.
327 //
328 // A way of working around this would be to display a dialog in the
329 // terminal with a "click-to-request-permission" button.
330 console.warn('desktop-notification-bell is true but we do not have ' +
331 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400332 }
333 } else {
334 terminal.desktopNotificationBell_ = false;
335 }
336 },
337
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 'background-color': function(v) {
339 terminal.setBackgroundColor(v);
340 },
341
342 'background-image': function(v) {
343 terminal.scrollPort_.setBackgroundImage(v);
344 },
345
346 'background-size': function(v) {
347 terminal.scrollPort_.setBackgroundSize(v);
348 },
349
350 'background-position': function(v) {
351 terminal.scrollPort_.setBackgroundPosition(v);
352 },
353
354 'backspace-sends-backspace': function(v) {
355 terminal.keyboard.backspaceSendsBackspace = v;
356 },
357
Brad Town18654b62015-03-12 00:27:45 -0700358 'character-map-overrides': function(v) {
359 if (!(v == null || v instanceof Object)) {
360 console.warn('Preference character-map-modifications is not an ' +
361 'object: ' + v);
362 return;
363 }
364
Mike Frysinger095d4062017-06-14 00:29:48 -0700365 terminal.vt.characterMaps.reset();
366 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700367 },
368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'cursor-blink': function(v) {
370 terminal.setCursorBlink(!!v);
371 },
372
Joel Hockey9d10ba12019-05-28 01:25:02 -0700373 'cursor-shape': function(v) {
374 terminal.setCursorShape(v);
375 },
376
Robert Gindaea2183e2014-07-17 09:51:51 -0700377 'cursor-blink-cycle': function(v) {
378 if (v instanceof Array &&
379 typeof v[0] == 'number' &&
380 typeof v[1] == 'number') {
381 terminal.cursorBlinkCycle_ = v;
382 } else if (typeof v == 'number') {
383 terminal.cursorBlinkCycle_ = [v, v];
384 } else {
385 // Fast blink indicates an error.
386 terminal.cursorBlinkCycle_ = [100, 100];
387 }
388 },
389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 'cursor-color': function(v) {
391 terminal.setCursorColor(v);
392 },
393
394 'color-palette-overrides': function(v) {
395 if (!(v == null || v instanceof Object || v instanceof Array)) {
396 console.warn('Preference color-palette-overrides is not an array or ' +
397 'object: ' + v);
398 return;
rginda9f5222b2012-03-05 11:53:28 -0800399 }
rginda9f5222b2012-03-05 11:53:28 -0800400
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400401 // Reset all existing colors first as the new palette override might not
402 // have the same mappings. If the old one set colors the new one doesn't,
403 // those old mappings have to get cleared first.
Mike Frysinger06ce15d2020-10-21 21:53:29 -0400404 lib.colors.stockPalette.forEach((c, i) => terminal.setColorPalette(i, c));
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400405 terminal.colorPaletteOverrides_.clear();
rginda39bdf6f2012-04-10 16:50:55 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400408 for (const key in v) {
409 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700410 if (isNaN(i) || i < 0 || i > 255) {
411 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
412 continue;
413 }
414
415 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400416 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700417 if (rgb) {
418 terminal.setColorPalette(i, rgb);
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400419 terminal.colorPaletteOverrides_.set(i, rgb);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700420 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 }
422 }
rginda30f20f62012-04-05 16:36:19 -0700423 }
rginda30f20f62012-04-05 16:36:19 -0700424
Joel Hockey42dba8f2020-03-26 16:21:11 -0700425 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
426 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 },
rginda30f20f62012-04-05 16:36:19 -0700428
Robert Ginda57f03b42012-09-13 11:02:48 -0700429 'copy-on-select': function(v) {
430 terminal.copyOnSelect = !!v;
431 },
rginda9f5222b2012-03-05 11:53:28 -0800432
Rob Spies0bec09b2014-06-06 15:58:09 -0700433 'use-default-window-copy': function(v) {
434 terminal.useDefaultWindowCopy = !!v;
435 },
436
437 'clear-selection-after-copy': function(v) {
438 terminal.clearSelectionAfterCopy = !!v;
439 },
440
Robert Ginda7e5e9522014-03-14 12:23:58 -0700441 'ctrl-plus-minus-zero-zoom': function(v) {
442 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
443 },
444
Robert Gindafb5a3f92014-05-13 14:12:00 -0700445 'ctrl-c-copy': function(v) {
446 terminal.keyboard.ctrlCCopy = v;
447 },
448
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100449 'ctrl-v-paste': function(v) {
450 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700451 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100452 },
453
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700454 'paste-on-drop': function(v) {
455 terminal.scrollPort_.setPasteOnDrop(v);
456 },
457
Masaya Suzuki273aa982014-05-31 07:25:55 +0900458 'east-asian-ambiguous-as-two-column': function(v) {
459 lib.wc.regardCjkAmbiguous = v;
460 },
461
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 'enable-8-bit-control': function(v) {
463 terminal.vt.enable8BitControl = !!v;
464 },
rginda30f20f62012-04-05 16:36:19 -0700465
Robert Ginda57f03b42012-09-13 11:02:48 -0700466 'enable-bold': function(v) {
467 terminal.syncBoldSafeState();
468 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400469
Robert Ginda3e278d72014-03-25 13:18:51 -0700470 'enable-bold-as-bright': function(v) {
471 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
472 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
473 },
474
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400475 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500476 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400477 },
478
Robert Ginda57f03b42012-09-13 11:02:48 -0700479 'enable-clipboard-write': function(v) {
480 terminal.vt.enableClipboardWrite = !!v;
481 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400482
Robert Ginda3755e752013-05-31 13:34:09 -0700483 'enable-dec12': function(v) {
484 terminal.vt.enableDec12 = !!v;
485 },
486
Mike Frysinger38f267d2018-09-07 02:50:59 -0400487 'enable-csi-j-3': function(v) {
488 terminal.vt.enableCsiJ3 = !!v;
489 },
490
shivanggarg2b7b0d52020-07-10 11:01:34 +0530491 'find-result-color': function(v) {
492 terminal.findBar.setFindResultColor(v);
493 },
494
shivanggarga72e8ba2020-07-16 07:11:17 +0530495 'find-result-selected-color': function(v) {
496 terminal.findBar.setFindResultSelectedColor(v);
497 },
498
Robert Ginda57f03b42012-09-13 11:02:48 -0700499 'font-family': function(v) {
500 terminal.syncFontFamily();
501 },
rginda30f20f62012-04-05 16:36:19 -0700502
Robert Ginda57f03b42012-09-13 11:02:48 -0700503 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700504 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700505 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500506 console.error(`Invalid font size: ${v}`);
507 return;
508 }
509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 terminal.setFontSize(v);
511 },
rginda9875d902012-08-20 16:21:57 -0700512
Robert Ginda57f03b42012-09-13 11:02:48 -0700513 'font-smoothing': function(v) {
514 terminal.syncFontFamily();
515 },
rgindade84e382012-04-20 15:39:31 -0700516
Robert Ginda57f03b42012-09-13 11:02:48 -0700517 'foreground-color': function(v) {
518 terminal.setForegroundColor(v);
519 },
rginda30f20f62012-04-05 16:36:19 -0700520
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400521 'hide-mouse-while-typing': function(v) {
522 terminal.setAutomaticMouseHiding(v);
523 },
524
Robert Ginda57f03b42012-09-13 11:02:48 -0700525 'home-keys-scroll': function(v) {
526 terminal.keyboard.homeKeysScroll = v;
527 },
rginda4bba5e12012-06-20 16:15:30 -0700528
Robert Gindaa8165692015-06-15 14:46:31 -0700529 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700530 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
531 },
Robert Gindaa8165692015-06-15 14:46:31 -0700532
Joel Hockey95a9e272020-03-16 21:19:53 -0700533 'keybindings-os-defaults': function(v) {
534 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700535 },
536
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700537 'media-keys-are-fkeys': function(v) {
538 terminal.keyboard.mediaKeysAreFKeys = v;
539 },
540
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 'meta-sends-escape': function(v) {
542 terminal.keyboard.metaSendsEscape = v;
543 },
rginda30f20f62012-04-05 16:36:19 -0700544
Mike Frysinger847577f2017-05-23 23:25:57 -0400545 'mouse-right-click-paste': function(v) {
546 terminal.mouseRightClickPaste = v;
547 },
548
Robert Ginda57f03b42012-09-13 11:02:48 -0700549 'mouse-paste-button': function(v) {
550 terminal.syncMousePasteButton();
551 },
rgindaa8ba17d2012-08-15 14:41:10 -0700552
Robert Gindae76aa9f2014-03-14 12:29:12 -0700553 'page-keys-scroll': function(v) {
554 terminal.keyboard.pageKeysScroll = v;
555 },
556
Robert Ginda40932892012-12-10 17:26:40 -0800557 'pass-alt-number': function(v) {
558 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700559 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800560 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500561 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800562 }
563
564 terminal.passAltNumber = v;
565 },
566
567 'pass-ctrl-number': function(v) {
568 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700569 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800570 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500571 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800572 }
573
574 terminal.passCtrlNumber = v;
575 },
576
Joel Hockey0e052042020-02-19 05:37:19 -0800577 'pass-ctrl-n': function(v) {
578 terminal.passCtrlN = v;
579 },
580
581 'pass-ctrl-t': function(v) {
582 terminal.passCtrlT = v;
583 },
584
585 'pass-ctrl-tab': function(v) {
586 terminal.passCtrlTab = v;
587 },
588
589 'pass-ctrl-w': function(v) {
590 terminal.passCtrlW = v;
591 },
592
Robert Ginda40932892012-12-10 17:26:40 -0800593 'pass-meta-number': function(v) {
594 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700595 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800596 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500597 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800598 }
599
600 terminal.passMetaNumber = v;
601 },
602
Marius Schilder77857b32014-05-14 16:21:26 -0700603 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700604 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700605 },
606
Robert Ginda8cb7d902013-06-20 14:37:18 -0700607 'receive-encoding': function(v) {
608 if (!(/^(utf-8|raw)$/).test(v)) {
609 console.warn('Invalid value for "receive-encoding": ' + v);
610 v = 'utf-8';
611 }
612
613 terminal.vt.characterEncoding = v;
614 },
615
Joel Hockey139d82d2020-04-07 23:04:29 -0700616 'screen-padding-size': function(v) {
617 v = parseInt(v, 10);
618 if (isNaN(v) || v < 0) {
619 console.error(`Invalid screen padding size: ${v}`);
620 return;
621 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700622 terminal.setScreenPaddingSize(v);
623 },
624
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700625 'screen-border-size': function(v) {
626 v = parseInt(v, 10);
627 if (isNaN(v) || v < 0) {
628 console.error(`Invalid screen border size: ${v}`);
629 return;
630 }
631 terminal.setScreenBorderSize(v);
632 },
633
634 'screen-border-color': function(v) {
635 terminal.div_.style.borderColor = v;
636 },
637
Robert Ginda57f03b42012-09-13 11:02:48 -0700638 'scroll-on-keystroke': function(v) {
639 terminal.scrollOnKeystroke_ = v;
640 },
rginda9f5222b2012-03-05 11:53:28 -0800641
Robert Ginda57f03b42012-09-13 11:02:48 -0700642 'scroll-on-output': function(v) {
643 terminal.scrollOnOutput_ = v;
644 },
rginda30f20f62012-04-05 16:36:19 -0700645
Robert Ginda57f03b42012-09-13 11:02:48 -0700646 'scrollbar-visible': function(v) {
647 terminal.setScrollbarVisible(v);
648 },
rginda9f5222b2012-03-05 11:53:28 -0800649
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400650 'scroll-wheel-may-send-arrow-keys': function(v) {
651 terminal.scrollWheelArrowKeys_ = v;
652 },
653
Rob Spies49039e52014-12-17 13:40:04 -0800654 'scroll-wheel-move-multiplier': function(v) {
655 terminal.setScrollWheelMoveMultipler(v);
656 },
657
Robert Ginda57f03b42012-09-13 11:02:48 -0700658 'shift-insert-paste': function(v) {
659 terminal.keyboard.shiftInsertPaste = v;
660 },
rginda9f5222b2012-03-05 11:53:28 -0800661
Mike Frysingera7768922017-07-28 15:00:12 -0400662 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400663 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400664 },
665
Robert Gindae76aa9f2014-03-14 12:29:12 -0700666 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400667 terminal.scrollPort_.setUserCssUrl(v);
668 },
669
670 'user-css-text': function(v) {
671 terminal.scrollPort_.setUserCssText(v);
672 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400673
674 'word-break-match-left': function(v) {
675 terminal.primaryScreen_.wordBreakMatchLeft = v;
676 terminal.alternateScreen_.wordBreakMatchLeft = v;
677 },
678
679 'word-break-match-right': function(v) {
680 terminal.primaryScreen_.wordBreakMatchRight = v;
681 terminal.alternateScreen_.wordBreakMatchRight = v;
682 },
683
684 'word-break-match-middle': function(v) {
685 terminal.primaryScreen_.wordBreakMatchMiddle = v;
686 terminal.alternateScreen_.wordBreakMatchMiddle = v;
687 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400688
689 'allow-images-inline': function(v) {
690 terminal.allowImagesInline = v;
691 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700692 });
rginda30f20f62012-04-05 16:36:19 -0700693
Robert Ginda57f03b42012-09-13 11:02:48 -0700694 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800695 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700696
Mike Frysingerec4225d2020-04-07 05:00:01 -0400697 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700698 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400699 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400700 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700701 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
Rob Spies56953412014-04-28 14:09:47 -0700704/**
705 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500706 *
Joel Hockey0f933582019-08-27 18:01:51 -0700707 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700708 */
709hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700710 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700711};
712
Robert Gindaa063b202014-07-21 11:08:25 -0700713/**
714 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500715 *
716 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700717 */
718hterm.Terminal.prototype.setBracketedPaste = function(state) {
719 this.options_.bracketedPaste = state;
720};
Rob Spies56953412014-04-28 14:09:47 -0700721
rginda8e92a692012-05-20 19:37:20 -0700722/**
723 * Set the color for the cursor.
724 *
725 * If you want this setting to persist, set it through prefs_, rather than
726 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500727 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500728 * @param {string=} color The color to set. If not defined, we reset to the
729 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700730 */
731hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400732 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700733 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400734 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500735
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400736 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700737};
738
739/**
740 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400741 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500742 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700743 */
744hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400745 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700746};
747
748/**
rgindad5613292012-06-19 15:40:37 -0700749 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500750 *
751 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700752 */
753hterm.Terminal.prototype.setSelectionEnabled = function(state) {
754 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700755};
756
757/**
Joel Hockeyfde20592020-08-02 15:05:27 -0700758 * Set the background image.
759 *
760 * If you want this setting to persist, set it through prefs_, rather than
761 * with this method.
762 *
763 * @param {string=} cssUrl The image to set as a css url. If not defined, we
764 * reset to the saved user preference.
765 */
766hterm.Terminal.prototype.setBackgroundImage = function(cssUrl) {
767 if (cssUrl === undefined) {
768 cssUrl = this.prefs_.getString('background-image');
769 }
770 this.scrollPort_.setBackgroundImage(cssUrl);
771};
772
773/**
rginda8e92a692012-05-20 19:37:20 -0700774 * Set the background color.
775 *
776 * If you want this setting to persist, set it through prefs_, rather than
777 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500778 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500779 * @param {string=} color The color to set. If not defined, we reset to the
780 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700781 */
782hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400783 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700784 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400785 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500786
Joel Hockey42dba8f2020-03-26 16:21:11 -0700787 this.backgroundColor_ = lib.colors.normalizeCSS(color);
788 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700789};
790
rginda9f5222b2012-03-05 11:53:28 -0800791/**
792 * Return the current terminal background color.
793 *
794 * Intended for use by other classes, so we don't have to expose the entire
795 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500796 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700797 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800798 */
799hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700800 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700801};
802
803/**
804 * Set the foreground color.
805 *
806 * If you want this setting to persist, set it through prefs_, rather than
807 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500808 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500809 * @param {string=} color The color to set. If not defined, we reset to the
810 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700811 */
812hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400813 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700814 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400815 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500816
Joel Hockey42dba8f2020-03-26 16:21:11 -0700817 this.foregroundColor_ = lib.colors.normalizeCSS(color);
818 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800819};
820
821/**
822 * Return the current terminal foreground color.
823 *
824 * Intended for use by other classes, so we don't have to expose the entire
825 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500826 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700827 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800828 */
829hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700830 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800831};
832
833/**
rginda87b86462011-12-14 13:48:03 -0800834 * Create a new instance of a terminal command and run it with a given
835 * argument string.
836 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700837 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700838 * @param {string} commandName The command to run for this terminal.
839 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800840 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700841hterm.Terminal.prototype.runCommandClass = function(
842 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400843 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400844 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700845 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400846 }
rgindaf522ce02012-04-17 17:49:17 -0700847
rginda87b86462011-12-14 13:48:03 -0800848 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700849 {
850 commandName: commandName,
851 args: args,
rginda87b86462011-12-14 13:48:03 -0800852 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700853 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400854 onExit: (code) => {
855 this.io.pop();
856 this.uninstallKeyboard();
857 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
858 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400859 window.close();
860 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400861 },
rginda87b86462011-12-14 13:48:03 -0800862 });
863
rgindafeaf3142012-01-31 15:14:20 -0800864 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800865 this.command.run();
866};
867
868/**
rgindafeaf3142012-01-31 15:14:20 -0800869 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500870 *
871 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800872 */
873hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700874 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800875};
876
877/**
878 * Install the keyboard handler for this terminal.
879 *
880 * This will prevent the browser from seeing any keystrokes sent to the
881 * terminal.
882 */
883hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700884 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400885};
rgindafeaf3142012-01-31 15:14:20 -0800886
887/**
888 * Uninstall the keyboard handler for this terminal.
889 */
890hterm.Terminal.prototype.uninstallKeyboard = function() {
891 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400892};
rgindafeaf3142012-01-31 15:14:20 -0800893
894/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400895 * Set a CSS variable.
896 *
897 * Normally this is used to set variables in the hterm namespace.
898 *
899 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700900 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400901 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400902 */
903hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400904 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400905 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400906 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400907};
908
909/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700910 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
911 * input is valid.
912 *
913 * @param {string} name The variable to set.
914 * @param {?string} rgb The rgb value to assign to the variable.
915 */
916hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
917 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
918 if (ary) {
919 this.setCssVar(name, ary.slice(0, 3).join(','));
920 }
921};
922
923/**
924 * Sets the specified color for the active screen.
925 *
926 * @param {number} i The index into the 256 color palette to set.
927 * @param {?string} rgb The rgb value to assign to the variable.
928 */
929hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
930 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
931 this.setRgbColorCssVar(`color-${i}`, rgb);
932 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
933 }
934};
935
936/**
937 * Returns the current value in the active screen of the specified color.
938 *
939 * @param {number} i Color palette index.
940 * @return {string} rgb color.
941 */
942hterm.Terminal.prototype.getColorPalette = function(i) {
943 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400944 this.colorPaletteOverrides_.get(i) ||
945 lib.colors.stockPalette[i];
Joel Hockey42dba8f2020-03-26 16:21:11 -0700946};
947
948/**
949 * Reset the specified color in the active screen to its default value.
950 *
951 * @param {number} i Color to reset
952 */
953hterm.Terminal.prototype.resetColor = function(i) {
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400954 this.setColorPalette(
955 i, this.colorPaletteOverrides_.get(i) || lib.colors.stockPalette[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700956 delete this.screen_.textAttributes.colorPaletteOverrides[i];
957};
958
959/**
960 * Reset the current screen color palette to the default state.
961 */
962hterm.Terminal.prototype.resetColorPalette = function() {
963 this.screen_.textAttributes.colorPaletteOverrides.forEach(
964 (c, i) => this.resetColor(i));
965};
966
967/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500968 * Get a CSS variable.
969 *
970 * Normally this is used to get variables in the hterm namespace.
971 *
972 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400973 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500974 * @return {string} The current setting for this variable.
975 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400976hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500977 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400978 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500979};
980
981/**
shivanggargf3b362a2020-07-10 11:06:35 +0530982 * @return {!hterm.ScrollPort}
983 */
984hterm.Terminal.prototype.getScrollPort = function() {
985 return this.scrollPort_;
986};
987
988/**
Jason Linbbbdb752020-03-06 16:26:59 +1100989 * Update CSS character size variables to match the scrollport.
990 */
991hterm.Terminal.prototype.updateCssCharsize_ = function() {
992 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
993 this.setCssVar('charsize-height',
994 this.scrollPort_.characterSize.height + 'px');
995};
996
997/**
rginda35c456b2012-02-09 17:29:05 -0800998 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800999 *
1000 * Call setFontSize(0) to reset to the default font size.
1001 *
1002 * This function does not modify the font-size preference.
1003 *
1004 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -08001005 */
1006hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001007 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001008 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001009 }
rginda9f5222b2012-03-05 11:53:28 -08001010
rginda35c456b2012-02-09 17:29:05 -08001011 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -07001012 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +11001013 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -08001014};
1015
1016/**
1017 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -05001018 *
1019 * @return {number}
rginda35c456b2012-02-09 17:29:05 -08001020 */
1021hterm.Terminal.prototype.getFontSize = function() {
1022 return this.scrollPort_.getFontSize();
1023};
1024
1025/**
rginda8e92a692012-05-20 19:37:20 -07001026 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -05001027 *
1028 * @return {string}
rginda8e92a692012-05-20 19:37:20 -07001029 */
1030hterm.Terminal.prototype.getFontFamily = function() {
1031 return this.scrollPort_.getFontFamily();
1032};
1033
1034/**
rginda35c456b2012-02-09 17:29:05 -08001035 * Set the CSS "font-family" for this terminal.
1036 */
rginda9f5222b2012-03-05 11:53:28 -08001037hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001038 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1039 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001040 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001041 this.syncBoldSafeState();
1042};
1043
rginda4bba5e12012-06-20 16:15:30 -07001044/**
1045 * Set this.mousePasteButton based on the mouse-paste-button pref,
1046 * autodetecting if necessary.
1047 */
1048hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001049 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001050 if (typeof button == 'number') {
1051 this.mousePasteButton = button;
1052 return;
1053 }
1054
Mike Frysingeree81a002017-12-12 16:14:53 -05001055 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001056 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001057 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001058 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001059 }
1060};
1061
1062/**
1063 * Enable or disable bold based on the enable-bold pref, autodetecting if
1064 * necessary.
1065 */
rginda9f5222b2012-03-05 11:53:28 -08001066hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001067 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001068 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001069 this.primaryScreen_.textAttributes.enableBold = enableBold;
1070 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001071 return;
1072 }
1073
Mike Frysingerdc727792020-04-10 01:41:13 -04001074 const normalSize = this.scrollPort_.measureCharacterSize();
1075 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001076
Mike Frysingerdc727792020-04-10 01:41:13 -04001077 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001078 if (!isBoldSafe) {
1079 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001080 'from normal. Font family is: ' +
1081 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001082 }
rginda9f5222b2012-03-05 11:53:28 -08001083
Robert Gindaed016262012-10-26 16:27:09 -07001084 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1085 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001086};
1087
1088/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001089 * Control text blinking behavior.
1090 *
1091 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001092 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001093hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001094 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001095 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001096 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001097 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001098};
1099
1100/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001101 * Set the mouse cursor style based on the current terminal mode.
1102 */
1103hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001104 this.setCssVar('mouse-cursor-style',
1105 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1106 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001107 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001108};
1109
1110/**
rginda87b86462011-12-14 13:48:03 -08001111 * Return a copy of the current cursor position.
1112 *
Joel Hockey0f933582019-08-27 18:01:51 -07001113 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001114 */
1115hterm.Terminal.prototype.saveCursor = function() {
1116 return this.screen_.cursorPosition.clone();
1117};
1118
Evan Jones2600d4f2016-12-06 09:29:36 -05001119/**
1120 * Return the current text attributes.
1121 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001122 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001123 */
rgindaa19afe22012-01-25 15:40:22 -08001124hterm.Terminal.prototype.getTextAttributes = function() {
1125 return this.screen_.textAttributes;
1126};
1127
Evan Jones2600d4f2016-12-06 09:29:36 -05001128/**
1129 * Set the text attributes.
1130 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001131 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001132 */
rginda1a09aa02012-06-18 21:11:25 -07001133hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1134 this.screen_.textAttributes = textAttributes;
1135};
1136
rginda87b86462011-12-14 13:48:03 -08001137/**
rginda9846e2f2012-01-27 13:53:33 -08001138 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001139 *
1140 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001141 */
1142hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001143 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001144};
1145
1146/**
rginda87b86462011-12-14 13:48:03 -08001147 * Restore a previously saved cursor position.
1148 *
Joel Hockey0f933582019-08-27 18:01:51 -07001149 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001150 */
1151hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001152 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1153 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001154 this.screen_.setCursorPosition(row, column);
1155 if (cursor.column > column ||
1156 cursor.column == column && cursor.overflow) {
1157 this.screen_.cursorPosition.overflow = true;
1158 }
rginda87b86462011-12-14 13:48:03 -08001159};
1160
1161/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001162 * Clear the cursor's overflow flag.
1163 */
1164hterm.Terminal.prototype.clearCursorOverflow = function() {
1165 this.screen_.cursorPosition.overflow = false;
1166};
1167
1168/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001169 * Save the current cursor state to the corresponding screens.
1170 *
1171 * See the hterm.Screen.CursorState class for more details.
1172 *
1173 * @param {boolean=} both If true, update both screens, else only update the
1174 * current screen.
1175 */
1176hterm.Terminal.prototype.saveCursorAndState = function(both) {
1177 if (both) {
1178 this.primaryScreen_.saveCursorAndState(this.vt);
1179 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001180 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001181 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001182 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001183};
1184
1185/**
1186 * Restore the saved cursor state in the corresponding screens.
1187 *
1188 * See the hterm.Screen.CursorState class for more details.
1189 *
1190 * @param {boolean=} both If true, update both screens, else only update the
1191 * current screen.
1192 */
1193hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1194 if (both) {
1195 this.primaryScreen_.restoreCursorAndState(this.vt);
1196 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001197 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001198 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001199 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001200};
1201
1202/**
Robert Ginda830583c2013-08-07 13:20:46 -07001203 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001204 *
1205 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001206 */
1207hterm.Terminal.prototype.setCursorShape = function(shape) {
1208 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001209 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001210};
Robert Ginda830583c2013-08-07 13:20:46 -07001211
1212/**
1213 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001214 *
1215 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001216 */
1217hterm.Terminal.prototype.getCursorShape = function() {
1218 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001219};
Robert Ginda830583c2013-08-07 13:20:46 -07001220
1221/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001222 * Set the screen padding size in pixels.
1223 *
1224 * @param {number} size
1225 */
1226hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001227 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001228 this.scrollPort_.setScreenPaddingSize(size);
1229};
1230
1231/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001232 * Set the screen border size in pixels.
1233 *
1234 * @param {number} size
1235 */
1236hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1237 this.div_.style.borderWidth = `${size}px`;
1238 this.screenBorderSize_ = size;
1239 this.scrollPort_.resize();
1240};
1241
1242/**
rginda87b86462011-12-14 13:48:03 -08001243 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001244 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001245 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001246 */
1247hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001248 if (columnCount == null) {
1249 this.div_.style.width = '100%';
1250 return;
1251 }
1252
Joel Hockey139d82d2020-04-07 23:04:29 -07001253 const rightPadding = Math.max(
1254 this.scrollPort_.screenPaddingSize,
1255 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001256 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001257 (this.scrollPort_.characterSize.width * columnCount) +
1258 this.scrollPort_.screenPaddingSize + rightPadding +
1259 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001260 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001261 this.scheduleSyncCursorPosition_();
1262};
rginda87b86462011-12-14 13:48:03 -08001263
rgindac9bc5502012-01-18 11:48:44 -08001264/**
rginda35c456b2012-02-09 17:29:05 -08001265 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001266 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001267 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001268 */
1269hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001270 if (rowCount == null) {
1271 this.div_.style.height = '100%';
1272 return;
1273 }
1274
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001275 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1276 (2 * this.scrollPort_.screenPaddingSize) +
1277 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001278 this.realizeSize_(this.screenSize.width, rowCount);
1279 this.scheduleSyncCursorPosition_();
1280};
1281
1282/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001283 * Deal with terminal size changes.
1284 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001285 * @param {number} columnCount The number of columns.
1286 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001287 */
1288hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001289 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001290
Mike Frysinger0206e262019-06-13 10:18:19 -04001291 if (columnCount != this.screenSize.width) {
1292 notify = true;
1293 this.realizeWidth_(columnCount);
1294 }
1295
1296 if (rowCount != this.screenSize.height) {
1297 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001298 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001299 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001300
1301 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001302 if (notify) {
1303 this.io.onTerminalResize_(columnCount, rowCount);
1304 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001305};
1306
1307/**
rgindac9bc5502012-01-18 11:48:44 -08001308 * Deal with terminal width changes.
1309 *
1310 * This function does what needs to be done when the terminal width changes
1311 * out from under us. It happens here rather than in onResize_() because this
1312 * code may need to run synchronously to handle programmatic changes of
1313 * terminal width.
1314 *
1315 * Relying on the browser to send us an async resize event means we may not be
1316 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001317 *
1318 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001319 */
1320hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001321 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001322 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001323 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001324
Mike Frysingerdc727792020-04-10 01:41:13 -04001325 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001326 if (deltaColumns == 0) {
1327 // No change, so don't bother recalculating things.
1328 return;
1329 }
rgindac9bc5502012-01-18 11:48:44 -08001330
rginda87b86462011-12-14 13:48:03 -08001331 this.screenSize.width = columnCount;
1332 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001333
1334 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001335 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001336 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001337 }
rgindac9bc5502012-01-18 11:48:44 -08001338 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001339 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001340 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001341 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001342 }
rgindac9bc5502012-01-18 11:48:44 -08001343
1344 this.tabStops_.pop();
1345 }
1346 }
1347
1348 this.screen_.setColumnCount(this.screenSize.width);
1349};
1350
1351/**
1352 * Deal with terminal height changes.
1353 *
1354 * This function does what needs to be done when the terminal height changes
1355 * out from under us. It happens here rather than in onResize_() because this
1356 * code may need to run synchronously to handle programmatic changes of
1357 * terminal height.
1358 *
1359 * Relying on the browser to send us an async resize event means we may not be
1360 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001361 *
1362 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001363 */
1364hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001365 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001366 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001367 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001368
Mike Frysingerdc727792020-04-10 01:41:13 -04001369 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001370 if (deltaRows == 0) {
1371 // No change, so don't bother recalculating things.
1372 return;
1373 }
rgindac9bc5502012-01-18 11:48:44 -08001374
1375 this.screenSize.height = rowCount;
1376
Mike Frysingerdc727792020-04-10 01:41:13 -04001377 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001378
1379 if (deltaRows < 0) {
1380 // Screen got smaller.
1381 deltaRows *= -1;
1382 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001383 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001384 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001385 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001386 }
rgindac9bc5502012-01-18 11:48:44 -08001387
Mike Frysingerbdb34802020-04-07 03:47:32 -04001388 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001389 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001390 }
rgindac9bc5502012-01-18 11:48:44 -08001391
1392 this.screen_.popRow();
1393 deltaRows--;
1394 }
1395
Mike Frysingerdc727792020-04-10 01:41:13 -04001396 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001397 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1398
1399 // We just removed rows from the top of the screen, we need to update
1400 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001401 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001402 } else if (deltaRows > 0) {
1403 // Screen got larger.
1404
1405 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001406 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1407 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001408 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1409 this.screen_.unshiftRows(rows);
1410 deltaRows -= scrollbackCount;
1411 cursor.row += scrollbackCount;
1412 }
1413
Mike Frysingerbdb34802020-04-07 03:47:32 -04001414 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001415 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001416 }
rgindac9bc5502012-01-18 11:48:44 -08001417 }
1418
rginda35c456b2012-02-09 17:29:05 -08001419 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001420 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001421};
1422
1423/**
1424 * Scroll the terminal to the top of the scrollback buffer.
1425 */
1426hterm.Terminal.prototype.scrollHome = function() {
1427 this.scrollPort_.scrollRowToTop(0);
1428};
1429
1430/**
1431 * Scroll the terminal to the end.
1432 */
1433hterm.Terminal.prototype.scrollEnd = function() {
1434 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1435};
1436
1437/**
1438 * Scroll the terminal one page up (minus one line) relative to the current
1439 * position.
1440 */
1441hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001442 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001443};
1444
1445/**
1446 * Scroll the terminal one page down (minus one line) relative to the current
1447 * position.
1448 */
1449hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001450 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001451};
1452
rgindac9bc5502012-01-18 11:48:44 -08001453/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001454 * Scroll the terminal one line up relative to the current position.
1455 */
1456hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001457 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001458 this.scrollPort_.scrollRowToTop(i - 1);
1459};
1460
1461/**
1462 * Scroll the terminal one line down relative to the current position.
1463 */
1464hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001465 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001466 this.scrollPort_.scrollRowToTop(i + 1);
1467};
1468
1469/**
Robert Ginda40932892012-12-10 17:26:40 -08001470 * Clear primary screen, secondary screen, and the scrollback buffer.
1471 */
1472hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001473 this.clearHome(this.primaryScreen_);
1474 this.clearHome(this.alternateScreen_);
1475
1476 this.clearScrollback();
1477};
1478
1479/**
1480 * Clear scrollback buffer.
1481 */
1482hterm.Terminal.prototype.clearScrollback = function() {
1483 // Move to the end of the buffer in case the screen was scrolled back.
1484 // We're going to throw it away which would leave the display invalid.
1485 this.scrollEnd();
1486
Robert Ginda40932892012-12-10 17:26:40 -08001487 this.scrollbackRows_.length = 0;
1488 this.scrollPort_.resetCache();
1489
Mike Frysinger9c482b82018-09-07 02:49:36 -04001490 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1491 const bottom = screen.getHeight();
1492 this.renumberRows_(0, bottom, screen);
1493 });
Robert Ginda40932892012-12-10 17:26:40 -08001494
1495 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001496 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001497};
1498
1499/**
rgindac9bc5502012-01-18 11:48:44 -08001500 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001501 *
1502 * Perform a full reset to the default values listed in
1503 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001504 */
rginda87b86462011-12-14 13:48:03 -08001505hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001506 this.vt.reset();
1507
rgindac9bc5502012-01-18 11:48:44 -08001508 this.clearAllTabStops();
1509 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001510
Joel Hockey42dba8f2020-03-26 16:21:11 -07001511 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001512 const resetScreen = (screen) => {
1513 // We want to make sure to reset the attributes before we clear the screen.
1514 // The attributes might be used to initialize default/empty rows.
1515 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001516 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001517 this.clearHome(screen);
1518 screen.saveCursorAndState(this.vt);
1519 };
1520 resetScreen(this.primaryScreen_);
1521 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001522
Mike Frysinger84301d02017-11-29 13:28:46 -08001523 // Reset terminal options to their default values.
1524 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001525 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1526
Mike Frysinger84301d02017-11-29 13:28:46 -08001527 this.setVTScrollRegion(null, null);
1528
1529 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001530};
1531
rgindac9bc5502012-01-18 11:48:44 -08001532/**
1533 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001534 *
1535 * Perform a soft reset to the default values listed in
1536 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001537 */
rginda0f5c0292012-01-13 11:00:13 -08001538hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001539 this.vt.reset();
1540
rgindab8bc8932012-04-27 12:45:03 -07001541 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001542 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001543
Brad Townb62dfdc2015-03-16 19:07:15 -07001544 // We show the cursor on soft reset but do not alter the blink state.
1545 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1546
Joel Hockey42dba8f2020-03-26 16:21:11 -07001547 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001548 const resetScreen = (screen) => {
1549 // Xterm also resets the color palette on soft reset, even though it doesn't
1550 // seem to be documented anywhere.
1551 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001552 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001553 screen.saveCursorAndState(this.vt);
1554 };
1555 resetScreen(this.primaryScreen_);
1556 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001557
rgindab8bc8932012-04-27 12:45:03 -07001558 // The xterm man page explicitly says this will happen on soft reset.
1559 this.setVTScrollRegion(null, null);
1560
1561 // Xterm also shows the cursor on soft reset, but does not alter the blink
1562 // state.
rgindaa19afe22012-01-25 15:40:22 -08001563 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001564};
1565
rgindac9bc5502012-01-18 11:48:44 -08001566/**
1567 * Move the cursor forward to the next tab stop, or to the last column
1568 * if no more tab stops are set.
1569 */
1570hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001571 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001572
Mike Frysingerdc727792020-04-10 01:41:13 -04001573 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001574 if (this.tabStops_[i] > column) {
1575 this.setCursorColumn(this.tabStops_[i]);
1576 return;
1577 }
1578 }
1579
David Benjamin66e954d2012-05-05 21:08:12 -04001580 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001581 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001582 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001583 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001584};
1585
rgindac9bc5502012-01-18 11:48:44 -08001586/**
1587 * Move the cursor backward to the previous tab stop, or to the first column
1588 * if no previous tab stops are set.
1589 */
1590hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001591 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001592
Mike Frysingerdc727792020-04-10 01:41:13 -04001593 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001594 if (this.tabStops_[i] < column) {
1595 this.setCursorColumn(this.tabStops_[i]);
1596 return;
1597 }
1598 }
1599
1600 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001601};
1602
rgindac9bc5502012-01-18 11:48:44 -08001603/**
1604 * Set a tab stop at the given column.
1605 *
Joel Hockey0f933582019-08-27 18:01:51 -07001606 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001607 */
1608hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001609 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001610 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001611 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001612 }
rgindac9bc5502012-01-18 11:48:44 -08001613
1614 if (this.tabStops_[i] < column) {
1615 this.tabStops_.splice(i + 1, 0, column);
1616 return;
1617 }
1618 }
1619
1620 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001621};
1622
rgindac9bc5502012-01-18 11:48:44 -08001623/**
1624 * Clear the tab stop at the current cursor position.
1625 *
1626 * No effect if there is no tab stop at the current cursor position.
1627 */
1628hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001629 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001630
Mike Frysingerdc727792020-04-10 01:41:13 -04001631 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001632 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001633 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001634 }
rgindac9bc5502012-01-18 11:48:44 -08001635
1636 this.tabStops_.splice(i, 1);
1637};
1638
1639/**
1640 * Clear all tab stops.
1641 */
1642hterm.Terminal.prototype.clearAllTabStops = function() {
1643 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001644 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001645};
1646
1647/**
1648 * Set up the default tab stops, starting from a given column.
1649 *
1650 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001651 * from the specified column, or 0 if no column is provided. It also flags
1652 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001653 *
1654 * This does not clear the existing tab stops first, use clearAllTabStops
1655 * for that.
1656 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001657 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001658 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001659 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001660hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001661 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001662 // Round start up to a default tab stop.
1663 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001664 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001665 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001666 }
David Benjamin66e954d2012-05-05 21:08:12 -04001667
1668 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001669};
1670
rginda6d397402012-01-17 10:58:29 -08001671/**
rginda8ba33642011-12-14 12:31:31 -08001672 * Interpret a sequence of characters.
1673 *
1674 * Incomplete escape sequences are buffered until the next call.
1675 *
1676 * @param {string} str Sequence of characters to interpret or pass through.
1677 */
1678hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001679 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001680 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001681};
1682
1683/**
1684 * Take over the given DIV for use as the terminal display.
1685 *
Joel Hockey0f933582019-08-27 18:01:51 -07001686 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001687 */
1688hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001689 const charset = div.ownerDocument.characterSet.toLowerCase();
1690 if (charset != 'utf-8') {
1691 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1692 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1693 }
1694
rginda87b86462011-12-14 13:48:03 -08001695 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001696 this.div_.style.borderStyle = 'solid';
1697 this.div_.style.borderWidth = 0;
1698 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001699
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001700 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1701
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001702 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1703};
1704
1705/**
1706 * Initialisation of ScrollPort properties which need to be set after its DOM
1707 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001708 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001709 * @private
1710 */
1711hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001712 this.scrollPort_.setBackgroundImage(
1713 this.prefs_.getString('background-image'));
1714 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001715 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001716 this.prefs_.getString('background-position'));
1717 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1718 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1719 this.scrollPort_.setAccessibilityReader(
1720 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001721
rginda0918b652012-04-04 11:26:24 -07001722 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001723
Joel Hockeyd4fca732019-09-20 16:57:03 -07001724 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001725 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001726
Joel Hockeyd4fca732019-09-20 16:57:03 -07001727 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001728 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001729 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001730
rginda8ba33642011-12-14 12:31:31 -08001731 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001732 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301733 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001734
Evan Jones5f9df812016-12-06 09:38:58 -05001735 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001736 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001737
Mike Frysingerdc727792020-04-10 01:41:13 -04001738 const onMouse = this.onMouse_.bind(this);
1739 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001740 screenNode.addEventListener(
1741 'mousedown', /** @type {!EventListener} */ (onMouse));
1742 screenNode.addEventListener(
1743 'mouseup', /** @type {!EventListener} */ (onMouse));
1744 screenNode.addEventListener(
1745 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001746 this.scrollPort_.onScrollWheel = onMouse;
1747
Joel Hockeyd4fca732019-09-20 16:57:03 -07001748 screenNode.addEventListener(
1749 'keydown',
1750 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001751
Toni Barzic0bfa8922013-11-22 11:18:35 -08001752 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001753 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001754 // Listen for mousedown events on the screenNode as in FF the focus
1755 // events don't bubble.
1756 screenNode.addEventListener('mousedown', function() {
1757 setTimeout(this.onFocusChange_.bind(this, true));
1758 }.bind(this));
1759
Toni Barzic0bfa8922013-11-22 11:18:35 -08001760 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001761 'blur', this.onFocusChange_.bind(this, false));
1762
Mike Frysingerdc727792020-04-10 01:41:13 -04001763 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001764 style.textContent = `
1765.cursor-node[focus="false"] {
1766 box-sizing: border-box;
1767 background-color: transparent !important;
1768 border-width: 2px;
1769 border-style: solid;
1770}
1771menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001772 background: #fff;
1773 border-radius: 4px;
1774 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001775 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001776 display: none;
1777 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1778 margin: 0;
1779 padding: 8px 0;
1780 position: absolute;
1781 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001782}
1783menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001784 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001785 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1786 padding: 0.5em 1em;
1787 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001788}
1789menuitem.separator {
1790 border-bottom: none;
1791 height: 0.5em;
1792 padding: 0;
1793}
1794menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001795 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001796}
1797.wc-node {
1798 display: inline-block;
1799 text-align: center;
1800 width: calc(var(--hterm-charsize-width) * 2);
1801 line-height: var(--hterm-charsize-height);
1802}
1803:root {
1804 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1805 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001806 --hterm-blink-node-duration: 0.7s;
1807 --hterm-mouse-cursor-default: default;
1808 --hterm-mouse-cursor-text: text;
1809 --hterm-mouse-cursor-pointer: pointer;
1810 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001811 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001812
Mike Frysinger06ce15d2020-10-21 21:53:29 -04001813${lib.colors.stockPalette.map((c, i) => `
Joel Hockey42dba8f2020-03-26 16:21:11 -07001814 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1815`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001816}
1817.uri-node:hover {
1818 text-decoration: underline;
1819 cursor: var(--hterm-mouse-cursor-pointer);
1820}
1821@keyframes blink {
1822 from { opacity: 1.0; }
1823 to { opacity: 0.0; }
1824}
1825.blink-node {
1826 animation-name: blink;
1827 animation-duration: var(--hterm-blink-node-duration);
1828 animation-iteration-count: infinite;
1829 animation-timing-function: ease-in-out;
1830 animation-direction: alternate;
1831}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001832 // Insert this stock style as the first node so that any user styles will
1833 // override w/out having to use !important everywhere. The rules above mix
1834 // runtime variables with default ones designed to be overridden by the user,
1835 // but we can wait for a concrete case from the users to determine the best
1836 // way to split the sheet up to before & after the user-css settings.
1837 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001838
rginda8ba33642011-12-14 12:31:31 -08001839 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001840 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001841 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001842 this.cursorNode_.style.cssText = `
1843position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001844left: calc(var(--hterm-screen-padding-size) +
1845 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1846top: calc(var(--hterm-screen-padding-size) +
1847 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001848display: ${this.options_.cursorVisible ? '' : 'none'};
1849width: var(--hterm-charsize-width);
1850height: var(--hterm-charsize-height);
1851background-color: var(--hterm-cursor-color);
1852border-color: var(--hterm-cursor-color);
1853-webkit-transition: opacity, background-color 100ms linear;
1854-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001855
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001856 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001857 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1858 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001859
rginda8ba33642011-12-14 12:31:31 -08001860 this.document_.body.appendChild(this.cursorNode_);
1861
rgindad5613292012-06-19 15:40:37 -07001862 // When 'enableMouseDragScroll' is off we reposition this element directly
1863 // under the mouse cursor after a click. This makes Chrome associate
1864 // subsequent mousemove events with the scroll-blocker. Since the
1865 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1866 // events do not cause the scrollport to scroll.
1867 //
1868 // It's a hack, but it's the cleanest way I could find.
1869 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001870 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001871 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001872 this.scrollBlockerNode_.style.cssText =
1873 ('position: absolute;' +
1874 'top: -99px;' +
1875 'display: block;' +
1876 'width: 10px;' +
1877 'height: 10px;');
1878 this.document_.body.appendChild(this.scrollBlockerNode_);
1879
rgindad5613292012-06-19 15:40:37 -07001880 this.scrollPort_.onScrollWheel = onMouse;
1881 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1882 ].forEach(function(event) {
1883 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001884 this.cursorNode_.addEventListener(
1885 event, /** @type {!EventListener} */ (onMouse));
1886 this.document_.addEventListener(
1887 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001888 }.bind(this));
1889
1890 this.cursorNode_.addEventListener('mousedown', function() {
1891 setTimeout(this.focus.bind(this));
1892 }.bind(this));
1893
rginda8ba33642011-12-14 12:31:31 -08001894 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001895
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001896 // Re-sync fonts whenever a web font loads.
1897 this.document_.fonts.addEventListener(
1898 'loadingdone', () => this.syncFontFamily());
1899
rginda87b86462011-12-14 13:48:03 -08001900 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001901 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001902};
1903
rginda0918b652012-04-04 11:26:24 -07001904/**
1905 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001906 *
Joel Hockey0f933582019-08-27 18:01:51 -07001907 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001908 */
rginda87b86462011-12-14 13:48:03 -08001909hterm.Terminal.prototype.getDocument = function() {
1910 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001911};
1912
1913/**
rginda0918b652012-04-04 11:26:24 -07001914 * Focus the terminal.
1915 */
1916hterm.Terminal.prototype.focus = function() {
1917 this.scrollPort_.focus();
1918};
1919
1920/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001921 * Unfocus the terminal.
1922 */
1923hterm.Terminal.prototype.blur = function() {
1924 this.scrollPort_.blur();
1925};
1926
1927/**
rginda8ba33642011-12-14 12:31:31 -08001928 * Return the HTML Element for a given row index.
1929 *
1930 * This is a method from the RowProvider interface. The ScrollPort uses
1931 * it to fetch rows on demand as they are scrolled into view.
1932 *
1933 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1934 * pairs to conserve memory.
1935 *
Joel Hockey0f933582019-08-27 18:01:51 -07001936 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001937 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001938 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001939 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001940 * @override
rginda8ba33642011-12-14 12:31:31 -08001941 */
1942hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001943 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001944 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001945 }
rginda8ba33642011-12-14 12:31:31 -08001946
Mike Frysingerdc727792020-04-10 01:41:13 -04001947 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001948 return this.screen_.rowsArray[screenIndex];
1949};
1950
1951/**
1952 * Return the text content for a given range of rows.
1953 *
1954 * This is a method from the RowProvider interface. The ScrollPort uses
1955 * it to fetch text content on demand when the user attempts to copy their
1956 * selection to the clipboard.
1957 *
Joel Hockey0f933582019-08-27 18:01:51 -07001958 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001959 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001960 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001961 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001962 * relative to the start of the scrollback buffer.
1963 * @return {string} A single string containing the text value of the range of
1964 * rows. Lines will be newline delimited, with no trailing newline.
1965 */
1966hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001967 const ary = [];
1968 for (let i = start; i < end; i++) {
1969 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001970 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001971 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001972 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001973 }
rginda8ba33642011-12-14 12:31:31 -08001974 }
1975
rgindaa09e7332012-08-17 12:49:51 -07001976 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001977};
1978
1979/**
1980 * Return the text content for a given row.
1981 *
1982 * This is a method from the RowProvider interface. The ScrollPort uses
1983 * it to fetch text content on demand when the user attempts to copy their
1984 * selection to the clipboard.
1985 *
Joel Hockey0f933582019-08-27 18:01:51 -07001986 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001987 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001988 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001989 * @return {string} A string containing the text value of the selected row.
1990 */
1991hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001992 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001993 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001994};
1995
1996/**
1997 * Return the total number of rows in the addressable screen and in the
1998 * scrollback buffer of this terminal.
1999 *
2000 * This is a method from the RowProvider interface. The ScrollPort uses
2001 * it to compute the size of the scrollbar.
2002 *
Joel Hockey0f933582019-08-27 18:01:51 -07002003 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07002004 * @override
rginda8ba33642011-12-14 12:31:31 -08002005 */
2006hterm.Terminal.prototype.getRowCount = function() {
2007 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
2008};
2009
2010/**
2011 * Create DOM nodes for new rows and append them to the end of the terminal.
2012 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002013 * The new row is appended to the bottom of the list of rows, and does not
rginda8ba33642011-12-14 12:31:31 -08002014 * require renumbering (of the rowIndex property) of previous rows.
2015 *
2016 * If you think you want a new blank row somewhere in the middle of the
Joel Hockey95fe54e2020-07-23 01:12:24 -07002017 * terminal, look into insertRow_() or moveRows_().
rginda8ba33642011-12-14 12:31:31 -08002018 *
2019 * This method does not pay attention to vtScrollTop/Bottom, since you should
Joel Hockey95fe54e2020-07-23 01:12:24 -07002020 * be using insertRow_() or moveRows_() in cases where they would matter.
rginda8ba33642011-12-14 12:31:31 -08002021 *
2022 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002023 *
2024 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002025 */
2026hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002027 let cursorRow = this.screen_.rowsArray.length;
2028 const offset = this.scrollbackRows_.length + cursorRow;
2029 for (let i = 0; i < count; i++) {
2030 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002031 row.appendChild(this.document_.createTextNode(''));
2032 row.rowIndex = offset + i;
2033 this.screen_.pushRow(row);
2034 }
2035
Mike Frysingerdc727792020-04-10 01:41:13 -04002036 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002037 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002038 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002039 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002040 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002041 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002042 }
rginda8ba33642011-12-14 12:31:31 -08002043 }
2044
Mike Frysingerbdb34802020-04-07 03:47:32 -04002045 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002046 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002047 }
rginda8ba33642011-12-14 12:31:31 -08002048
rginda87b86462011-12-14 13:48:03 -08002049 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002050};
2051
2052/**
Joel Hockey95fe54e2020-07-23 01:12:24 -07002053 * Create a DOM node for a new row and insert it at the current position.
2054 *
2055 * The new row is inserted at the current cursor position, the existing top row
2056 * is moved to scrollback, and lines below are renumbered.
2057 *
2058 * The cursor will be positioned at column 0.
2059 */
2060hterm.Terminal.prototype.insertRow_ = function() {
2061 const row = this.document_.createElement('x-row');
2062 row.appendChild(this.document_.createTextNode(''));
2063
2064 this.scrollbackRows_.push(this.screen_.shiftRow());
2065
2066 const cursorRow = this.screen_.cursorPosition.row;
2067 this.screen_.insertRow(cursorRow, row);
2068
2069 this.renumberRows_(cursorRow, this.screen_.rowsArray.length);
2070
2071 this.setAbsoluteCursorPosition(cursorRow, 0);
2072 if (this.scrollPort_.isScrolledEnd) {
2073 this.scheduleScrollDown_();
2074 }
2075};
2076
2077/**
rginda8ba33642011-12-14 12:31:31 -08002078 * Relocate rows from one part of the addressable screen to another.
2079 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002080 * This is used to recycle rows during VT scrolls where a top region is set
2081 * (those which are driven by VT commands, rather than by the user manipulating
2082 * the scrollbar.)
rginda8ba33642011-12-14 12:31:31 -08002083 *
2084 * In this case, the blank lines scrolled into the scroll region are made of
2085 * the nodes we scrolled off. These have their rowIndex properties carefully
2086 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002087 *
2088 * @param {number} fromIndex The start index.
2089 * @param {number} count The number of rows to move.
2090 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002091 */
2092hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002093 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002094 this.screen_.insertRows(toIndex, ary);
2095
Mike Frysingerdc727792020-04-10 01:41:13 -04002096 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002097 if (fromIndex < toIndex) {
2098 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002099 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002100 } else {
2101 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002102 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002103 }
2104
2105 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002106 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002107};
2108
2109/**
2110 * Renumber the rowIndex property of the given range of rows.
2111 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002112 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002113 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002114 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002115 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002116 *
2117 * @param {number} start The start index.
2118 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002119 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002120 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002121hterm.Terminal.prototype.renumberRows_ = function(
2122 start, end, screen = undefined) {
2123 if (!screen) {
2124 screen = this.screen_;
2125 }
Robert Ginda40932892012-12-10 17:26:40 -08002126
Mike Frysingerdc727792020-04-10 01:41:13 -04002127 const offset = this.scrollbackRows_.length;
2128 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002129 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002130 }
2131};
2132
2133/**
2134 * Print a string to the terminal.
2135 *
2136 * This respects the current insert and wraparound modes. It will add new lines
2137 * to the end of the terminal, scrolling off the top into the scrollback buffer
2138 * if necessary.
2139 *
2140 * The string is *not* parsed for escape codes. Use the interpret() method if
2141 * that's what you're after.
2142 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002143 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002144 */
2145hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002146 this.scheduleSyncCursorPosition_();
2147
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002148 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002149 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002150
Mike Frysingerdc727792020-04-10 01:41:13 -04002151 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002152
Mike Frysingerdc727792020-04-10 01:41:13 -04002153 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002154 // Fun edge case: If the string only contains zero width codepoints (like
2155 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002156 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002157 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002158 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002159
2160 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002161 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2162 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002163 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002164 }
rgindaa19afe22012-01-25 15:40:22 -08002165
Mike Frysingerdc727792020-04-10 01:41:13 -04002166 let count = strWidth - startOffset;
2167 let didOverflow = false;
2168 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002169
rgindaa9abdd82012-08-06 18:05:09 -07002170 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2171 didOverflow = true;
2172 count = this.screenSize.width - this.screen_.cursorPosition.column;
2173 }
rgindaa19afe22012-01-25 15:40:22 -08002174
rgindaa9abdd82012-08-06 18:05:09 -07002175 if (didOverflow && !this.options_.wraparound) {
2176 // If the string overflowed the line but wraparound is off, then the
2177 // last printed character should be the last of the string.
2178 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002179 substr = lib.wc.substr(str, startOffset, count - 1) +
2180 lib.wc.substr(str, strWidth - 1);
2181 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002182 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002183 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002184 }
rgindaa19afe22012-01-25 15:40:22 -08002185
Mike Frysingerdc727792020-04-10 01:41:13 -04002186 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2187 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002188 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2189 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002190
2191 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002192 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002193 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002194 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002195 }
2196 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002197 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002198 }
2199
2200 this.screen_.maybeClipCurrentRow();
2201 startOffset += count;
Shivang Garga72e68c2020-07-18 08:58:32 +05302202 this.findBar.scheduleNotifyChanges(
2203 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rgindaa19afe22012-01-25 15:40:22 -08002204 }
rginda8ba33642011-12-14 12:31:31 -08002205
Mike Frysingerbdb34802020-04-07 03:47:32 -04002206 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002207 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002208 }
rginda8ba33642011-12-14 12:31:31 -08002209};
2210
2211/**
rginda87b86462011-12-14 13:48:03 -08002212 * Set the VT scroll region.
2213 *
rginda87b86462011-12-14 13:48:03 -08002214 * This also resets the cursor position to the absolute (0, 0) position, since
2215 * that's what xterm appears to do.
2216 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002217 * Setting the scroll region to the full height of the terminal will clear
2218 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2219 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2220 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2221 * continue to work as most users would expect.
2222 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002223 * @param {?number} scrollTop The zero-based top of the scroll region.
2224 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002225 * inclusive.
2226 */
2227hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Joel Hockey95fe54e2020-07-23 01:12:24 -07002228 this.vtScrollTop_ = scrollTop;
Joel Hockey4337eac2020-09-11 01:34:38 -07002229 this.vtScrollBottom_ = scrollBottom;
2230 if (scrollBottom == this.screenSize.height - 1) {
2231 this.vtScrollBottom_ = null;
2232 if (scrollTop == 0) {
2233 this.vtScrollTop_ = null;
2234 }
2235 }
rginda87b86462011-12-14 13:48:03 -08002236};
2237
2238/**
rginda8ba33642011-12-14 12:31:31 -08002239 * Return the top row index according to the VT.
2240 *
2241 * This will return 0 unless the terminal has been told to restrict scrolling
2242 * to some lower row. It is used for some VT cursor positioning and scrolling
2243 * commands.
2244 *
Joel Hockey0f933582019-08-27 18:01:51 -07002245 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002246 */
2247hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002248 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002249 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002250 }
rginda8ba33642011-12-14 12:31:31 -08002251
2252 return 0;
rginda87b86462011-12-14 13:48:03 -08002253};
rginda8ba33642011-12-14 12:31:31 -08002254
2255/**
2256 * Return the bottom row index according to the VT.
2257 *
2258 * This will return the height of the terminal unless the it has been told to
2259 * restrict scrolling to some higher row. It is used for some VT cursor
2260 * positioning and scrolling commands.
2261 *
Joel Hockey0f933582019-08-27 18:01:51 -07002262 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002263 */
2264hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002265 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002266 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002267 }
rginda8ba33642011-12-14 12:31:31 -08002268
rginda87b86462011-12-14 13:48:03 -08002269 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002270};
rginda8ba33642011-12-14 12:31:31 -08002271
2272/**
2273 * Process a '\n' character.
2274 *
2275 * If the cursor is on the final row of the terminal this will append a new
2276 * blank row to the screen and scroll the topmost row into the scrollback
2277 * buffer.
2278 *
2279 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002280 *
2281 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2282 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002283 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002284hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002285 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002286 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002287 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002288
Joel Hockey95fe54e2020-07-23 01:12:24 -07002289 const cursorAtEndOfScreen =
2290 (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1);
2291 const cursorAtEndOfVTRegion =
2292 (this.screen_.cursorPosition.row == this.getVTScrollBottom());
Robert Ginda9937abc2013-07-25 16:09:23 -07002293
Joel Hockey95fe54e2020-07-23 01:12:24 -07002294 if (this.vtScrollTop_ != null && cursorAtEndOfVTRegion) {
2295 // A VT Scroll region is active on top, we never append new rows.
2296 // We're at the end of the VT Scroll Region, perform a VT scroll.
2297 this.vtScrollUp(1);
2298 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
Robert Ginda9937abc2013-07-25 16:09:23 -07002299 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002300 // We're at the end of the screen. Append a new row to the terminal,
2301 // shifting the top row into the scrollback.
2302 this.appendRows_(1);
Joel Hockey95fe54e2020-07-23 01:12:24 -07002303 } else if (cursorAtEndOfVTRegion) {
2304 this.insertRow_();
rginda8ba33642011-12-14 12:31:31 -08002305 } else {
rginda87b86462011-12-14 13:48:03 -08002306 // Anywhere else in the screen just moves the cursor.
2307 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002308 }
2309};
2310
2311/**
2312 * Like newLine(), except maintain the cursor column.
2313 */
2314hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002315 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002316 this.newLine();
2317 this.setCursorColumn(column);
2318};
2319
2320/**
rginda87b86462011-12-14 13:48:03 -08002321 * If autoCarriageReturn is set then newLine(), else lineFeed().
2322 */
2323hterm.Terminal.prototype.formFeed = function() {
2324 if (this.options_.autoCarriageReturn) {
2325 this.newLine();
2326 } else {
2327 this.lineFeed();
2328 }
2329};
2330
2331/**
2332 * Move the cursor up one row, possibly inserting a blank line.
2333 *
2334 * The cursor column is not changed.
2335 */
2336hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002337 const scrollTop = this.getVTScrollTop();
2338 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002339
2340 if (currentRow == scrollTop) {
2341 this.insertLines(1);
2342 } else {
2343 this.setAbsoluteCursorRow(currentRow - 1);
2344 }
2345};
2346
2347/**
rginda8ba33642011-12-14 12:31:31 -08002348 * Replace all characters to the left of the current cursor with the space
2349 * character.
2350 *
2351 * TODO(rginda): This should probably *remove* the characters (not just replace
2352 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002353 * position.
rginda8ba33642011-12-14 12:31:31 -08002354 */
2355hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002356 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002357 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002358 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002359 this.screen_.overwriteString(' '.repeat(count), count);
Shivang Garga72e68c2020-07-18 08:58:32 +05302360 this.findBar.scheduleNotifyChanges(
2361 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002362 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002363};
2364
2365/**
David Benjamin684a9b72012-05-01 17:19:58 -04002366 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002367 *
2368 * The cursor position is unchanged.
2369 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002370 * If the current background color is not the default background color this
2371 * will insert spaces rather than delete. This is unfortunate because the
2372 * trailing space will affect text selection, but it's difficult to come up
2373 * with a way to style empty space that wouldn't trip up the hterm.Screen
2374 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002375 *
2376 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2377 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2378 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002379 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002380 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002381 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002382hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002383 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002384 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002385 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002386
Mike Frysingerdc727792020-04-10 01:41:13 -04002387 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002388 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002389
Shivang Garga72e68c2020-07-18 08:58:32 +05302390 this.findBar.scheduleNotifyChanges(
2391 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
2392
Robert Gindaf2547f12012-10-25 20:36:21 -07002393 if (this.screen_.textAttributes.background ===
2394 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002395 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002396 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002397 this.screen_.cursorPosition.column + count) {
2398 this.screen_.deleteChars(count);
2399 this.clearCursorOverflow();
2400 return;
2401 }
2402 }
2403
Mike Frysingerdc727792020-04-10 01:41:13 -04002404 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002405 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002406 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002407 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002408};
2409
2410/**
2411 * Erase the current line.
2412 *
2413 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002414 */
2415hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002416 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002417 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002418 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002419 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002420};
2421
2422/**
David Benjamina08d78f2012-05-05 00:28:49 -04002423 * Erase all characters from the start of the screen to the current cursor
2424 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002425 *
2426 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002427 */
2428hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002429 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002430
2431 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002432
Mike Frysingerdc727792020-04-10 01:41:13 -04002433 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002434 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002435 this.screen_.clearCursorRow();
2436 }
2437
rginda87b86462011-12-14 13:48:03 -08002438 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002439 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002440};
2441
2442/**
2443 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002444 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002445 *
2446 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002447 */
2448hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002449 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002450
2451 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002452
Mike Frysingerdc727792020-04-10 01:41:13 -04002453 const bottom = this.screenSize.height - 1;
2454 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002455 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002456 this.screen_.clearCursorRow();
2457 }
2458
rginda87b86462011-12-14 13:48:03 -08002459 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002460 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002461};
2462
2463/**
2464 * Fill the terminal with a given character.
2465 *
2466 * This methods does not respect the VT scroll region.
2467 *
2468 * @param {string} ch The character to use for the fill.
2469 */
2470hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002471 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002472
2473 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002474 for (let row = 0; row < this.screenSize.height; row++) {
2475 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002476 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002477 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002478 }
Shivang Garga72e68c2020-07-18 08:58:32 +05302479 this.findBar.scheduleNotifyChanges(this.scrollbackRows_.length + row);
rginda87b86462011-12-14 13:48:03 -08002480 }
2481
2482 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002483};
2484
2485/**
rginda9ea433c2012-03-16 11:57:00 -07002486 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002487 *
rginda9ea433c2012-03-16 11:57:00 -07002488 * This does not respect the scroll region.
2489 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002490 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002491 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002492 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002493hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2494 if (!screen) {
2495 screen = this.screen_;
2496 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002497 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002498
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002499 this.accessibilityReader_.clear();
2500
rginda11057d52012-04-25 12:29:56 -07002501 if (bottom == 0) {
2502 // Empty screen, nothing to do.
2503 return;
2504 }
2505
Mike Frysingerdc727792020-04-10 01:41:13 -04002506 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002507 screen.setCursorPosition(i, 0);
2508 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002509 }
2510
rginda9ea433c2012-03-16 11:57:00 -07002511 screen.setCursorPosition(0, 0);
2512};
2513
2514/**
2515 * Erase the entire display without changing the cursor position.
2516 *
2517 * The cursor position is unchanged. This does not respect the scroll
2518 * region.
2519 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002520 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002521 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002522 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002523hterm.Terminal.prototype.clear = function(screen = undefined) {
2524 if (!screen) {
2525 screen = this.screen_;
2526 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002527 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002528 this.clearHome(screen);
2529 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002530};
2531
2532/**
2533 * VT command to insert lines at the current cursor row.
2534 *
2535 * This respects the current scroll region. Rows pushed off the bottom are
2536 * lost (they won't show up in the scrollback buffer).
2537 *
Joel Hockey0f933582019-08-27 18:01:51 -07002538 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002539 */
2540hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002541 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002542
Mike Frysingerdc727792020-04-10 01:41:13 -04002543 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002544 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002545
Robert Ginda579186b2012-09-26 11:40:04 -07002546 // The moveCount is the number of rows we need to relocate to make room for
2547 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002548 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002549 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002550 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002551 }
rginda8ba33642011-12-14 12:31:31 -08002552
Mike Frysingerdc727792020-04-10 01:41:13 -04002553 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002554 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002555 this.screen_.clearCursorRow();
2556 }
rginda8ba33642011-12-14 12:31:31 -08002557};
2558
2559/**
2560 * VT command to delete lines at the current cursor row.
2561 *
2562 * New rows are added to the bottom of scroll region to take their place. New
2563 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002564 *
2565 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002566 */
2567hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002568 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002569
Mike Frysingerdc727792020-04-10 01:41:13 -04002570 const top = cursor.row;
2571 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002572
Mike Frysingerdc727792020-04-10 01:41:13 -04002573 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002574 count = Math.min(count, maxCount);
2575
Mike Frysingerdc727792020-04-10 01:41:13 -04002576 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002577 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002578 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002579 }
rginda8ba33642011-12-14 12:31:31 -08002580
Mike Frysingerdc727792020-04-10 01:41:13 -04002581 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002582 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002583 this.screen_.clearCursorRow();
2584 }
2585
rginda87b86462011-12-14 13:48:03 -08002586 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002587 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002588};
2589
2590/**
2591 * Inserts the given number of spaces at the current cursor position.
2592 *
rginda87b86462011-12-14 13:48:03 -08002593 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002594 *
2595 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002596 */
2597hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002598 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002599
Mike Frysinger73e56462019-07-17 00:23:46 -05002600 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002601 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002602 this.screen_.maybeClipCurrentRow();
Shivang Garga72e68c2020-07-18 08:58:32 +05302603 this.findBar.scheduleNotifyChanges(
2604 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002605
2606 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002607 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002608};
2609
2610/**
2611 * Forward-delete the specified number of characters starting at the cursor
2612 * position.
2613 *
Joel Hockey0f933582019-08-27 18:01:51 -07002614 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002615 */
2616hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002617 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002618 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002619 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002620 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002621 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002622 this.restoreCursor(cursor);
2623 }
2624
Shivang Garga72e68c2020-07-18 08:58:32 +05302625 this.findBar.scheduleNotifyChanges(
2626 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
David Benjamin54e8bf62012-06-01 22:31:40 -04002627 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002628};
2629
2630/**
2631 * Shift rows in the scroll region upwards by a given number of lines.
2632 *
2633 * New rows are inserted at the bottom of the scroll region to fill the
2634 * vacated rows. The new rows not filled out with the current text attributes.
2635 *
2636 * This function does not affect the scrollback rows at all. Rows shifted
2637 * off the top are lost.
2638 *
rginda87b86462011-12-14 13:48:03 -08002639 * The cursor position is not altered.
2640 *
Joel Hockey0f933582019-08-27 18:01:51 -07002641 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002642 */
2643hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002644 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002645
rginda87b86462011-12-14 13:48:03 -08002646 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002647 this.deleteLines(count);
2648
rginda87b86462011-12-14 13:48:03 -08002649 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002650};
2651
2652/**
2653 * Shift rows below the cursor down by a given number of lines.
2654 *
2655 * This function respects the current scroll region.
2656 *
2657 * New rows are inserted at the top of the scroll region to fill the
2658 * vacated rows. The new rows not filled out with the current text attributes.
2659 *
2660 * This function does not affect the scrollback rows at all. Rows shifted
2661 * off the bottom are lost.
2662 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002663 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002664 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002665hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002666 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002667
rginda87b86462011-12-14 13:48:03 -08002668 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002669 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002670
rginda87b86462011-12-14 13:48:03 -08002671 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002672};
2673
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002674/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002675 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002676 *
2677 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002678 * cause Assitive Technology to announce the output of the terminal. It also
2679 * enables other features that aid assistive technology. All the features gated
2680 * behind this flag have a performance impact on the terminal which is why they
2681 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002682 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002683 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002684 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002685hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002686 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002687};
rginda87b86462011-12-14 13:48:03 -08002688
rginda8ba33642011-12-14 12:31:31 -08002689/**
2690 * Set the cursor position.
2691 *
2692 * The cursor row is relative to the scroll region if the terminal has
2693 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2694 *
Joel Hockey0f933582019-08-27 18:01:51 -07002695 * @param {number} row The new zero-based cursor row.
2696 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002697 */
2698hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2699 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002700 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002701 } else {
rginda87b86462011-12-14 13:48:03 -08002702 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002703 }
rginda87b86462011-12-14 13:48:03 -08002704};
rginda8ba33642011-12-14 12:31:31 -08002705
Evan Jones2600d4f2016-12-06 09:29:36 -05002706/**
2707 * Move the cursor relative to its current position.
2708 *
2709 * @param {number} row
2710 * @param {number} column
2711 */
rginda87b86462011-12-14 13:48:03 -08002712hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002713 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002714 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2715 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002716 this.screen_.setCursorPosition(row, column);
2717};
2718
Evan Jones2600d4f2016-12-06 09:29:36 -05002719/**
2720 * Move the cursor to the specified position.
2721 *
2722 * @param {number} row
2723 * @param {number} column
2724 */
rginda87b86462011-12-14 13:48:03 -08002725hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002726 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2727 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002728 this.screen_.setCursorPosition(row, column);
2729};
2730
2731/**
2732 * Set the cursor column.
2733 *
Joel Hockey0f933582019-08-27 18:01:51 -07002734 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002735 */
2736hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002737 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002738};
2739
2740/**
2741 * Return the cursor column.
2742 *
Joel Hockey0f933582019-08-27 18:01:51 -07002743 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002744 */
2745hterm.Terminal.prototype.getCursorColumn = function() {
2746 return this.screen_.cursorPosition.column;
2747};
2748
2749/**
2750 * Set the cursor row.
2751 *
2752 * The cursor row is relative to the scroll region if the terminal has
2753 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2754 *
Joel Hockey0f933582019-08-27 18:01:51 -07002755 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002756 */
rginda87b86462011-12-14 13:48:03 -08002757hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2758 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002759};
2760
2761/**
2762 * Return the cursor row.
2763 *
Joel Hockey0f933582019-08-27 18:01:51 -07002764 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002765 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002766hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002767 return this.screen_.cursorPosition.row;
2768};
2769
2770/**
2771 * Request that the ScrollPort redraw itself soon.
2772 *
2773 * The redraw will happen asynchronously, soon after the call stack winds down.
2774 * Multiple calls will be coalesced into a single redraw.
2775 */
2776hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002777 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002778 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002779 }
rginda8ba33642011-12-14 12:31:31 -08002780
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002781 this.timeouts_.redraw = setTimeout(() => {
2782 delete this.timeouts_.redraw;
2783 this.scrollPort_.redraw_();
2784 });
rginda8ba33642011-12-14 12:31:31 -08002785};
2786
2787/**
2788 * Request that the ScrollPort be scrolled to the bottom.
2789 *
2790 * The scroll will happen asynchronously, soon after the call stack winds down.
2791 * Multiple calls will be coalesced into a single scroll.
2792 *
2793 * This affects the scrollbar position of the ScrollPort, and has nothing to
2794 * do with the VT scroll commands.
2795 */
2796hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002797 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002798 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002799 }
rginda8ba33642011-12-14 12:31:31 -08002800
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002801 this.timeouts_.scrollDown = setTimeout(() => {
2802 delete this.timeouts_.scrollDown;
2803 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2804 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002805};
2806
2807/**
2808 * Move the cursor up a specified number of rows.
2809 *
Joel Hockey0f933582019-08-27 18:01:51 -07002810 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002811 */
2812hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002813 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002814};
2815
2816/**
2817 * Move the cursor down a specified number of rows.
2818 *
Joel Hockey0f933582019-08-27 18:01:51 -07002819 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002820 */
2821hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002822 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002823 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2824 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2825 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002826
Mike Frysingerdc727792020-04-10 01:41:13 -04002827 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2828 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002829 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002830};
2831
2832/**
2833 * Move the cursor left a specified number of columns.
2834 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002835 * If reverse wraparound mode is enabled and the previous row wrapped into
2836 * the current row then we back up through the wraparound as well.
2837 *
Joel Hockey0f933582019-08-27 18:01:51 -07002838 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002839 */
2840hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002841 count = count || 1;
2842
Mike Frysingerbdb34802020-04-07 03:47:32 -04002843 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002844 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002845 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002846
Mike Frysingerdc727792020-04-10 01:41:13 -04002847 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002848 if (this.options_.reverseWraparound) {
2849 if (this.screen_.cursorPosition.overflow) {
2850 // If this cursor is in the right margin, consume one count to get it
2851 // back to the last column. This only applies when we're in reverse
2852 // wraparound mode.
2853 count--;
2854 this.clearCursorOverflow();
2855
Mike Frysingerbdb34802020-04-07 03:47:32 -04002856 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002857 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002858 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002859 }
2860
Mike Frysingerdc727792020-04-10 01:41:13 -04002861 let newRow = this.screen_.cursorPosition.row;
2862 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002863 if (newColumn < 0) {
2864 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2865 if (newRow < 0) {
2866 // xterm also wraps from row 0 to the last row.
2867 newRow = this.screenSize.height + newRow % this.screenSize.height;
2868 }
2869 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2870 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002871
Robert Gindabfb32622014-07-17 13:20:27 -07002872 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2873
2874 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002875 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002876 this.setCursorColumn(newColumn);
2877 }
rginda8ba33642011-12-14 12:31:31 -08002878};
2879
2880/**
2881 * Move the cursor right a specified number of columns.
2882 *
Joel Hockey0f933582019-08-27 18:01:51 -07002883 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002884 */
2885hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002886 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002887
Mike Frysingerbdb34802020-04-07 03:47:32 -04002888 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002889 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002890 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002891
Mike Frysingerdc727792020-04-10 01:41:13 -04002892 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2893 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002894 this.setCursorColumn(column);
2895};
2896
2897/**
2898 * Reverse the foreground and background colors of the terminal.
2899 *
2900 * This only affects text that was drawn with no attributes.
2901 *
2902 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2903 * been drawn with attributes that happen to coincide with the default
2904 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002905 *
2906 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002907 */
2908hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002909 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002910 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002911 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2912 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002913 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002914 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2915 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002916 }
2917};
2918
2919/**
rginda87b86462011-12-14 13:48:03 -08002920 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002921 *
2922 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002923 */
2924hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002925 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002926
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002927 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002928
Michael Kelly485ecd12014-06-09 11:41:56 -04002929 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002930 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002931 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002932 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002933
Robert Ginda92e18102013-03-14 13:56:37 -07002934 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002935 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002936 this.bellSequelchTimeout_ = setTimeout(() => {
2937 this.bellSquelchTimeout_ = null;
2938 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002939 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002940 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002941 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002942
2943 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002944 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002945 this.bellNotificationList_.push(n);
2946 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002947 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002948 }
rginda87b86462011-12-14 13:48:03 -08002949};
2950
2951/**
rginda8ba33642011-12-14 12:31:31 -08002952 * Set the origin mode bit.
2953 *
2954 * If origin mode is on, certain VT cursor and scrolling commands measure their
2955 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2956 * to the top of the addressable screen.
2957 *
2958 * Defaults to off.
2959 *
2960 * @param {boolean} state True to set origin mode, false to unset.
2961 */
2962hterm.Terminal.prototype.setOriginMode = function(state) {
2963 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002964 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002965};
2966
2967/**
2968 * Set the insert mode bit.
2969 *
2970 * If insert mode is on, existing text beyond the cursor position will be
2971 * shifted right to make room for new text. Otherwise, new text overwrites
2972 * any existing text.
2973 *
2974 * Defaults to off.
2975 *
2976 * @param {boolean} state True to set insert mode, false to unset.
2977 */
2978hterm.Terminal.prototype.setInsertMode = function(state) {
2979 this.options_.insertMode = state;
2980};
2981
2982/**
rginda87b86462011-12-14 13:48:03 -08002983 * Set the auto carriage return bit.
2984 *
2985 * If auto carriage return is on then a formfeed character is interpreted
2986 * as a newline, otherwise it's the same as a linefeed. The difference boils
2987 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002988 *
2989 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002990 */
2991hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2992 this.options_.autoCarriageReturn = state;
2993};
2994
2995/**
rginda8ba33642011-12-14 12:31:31 -08002996 * Set the wraparound mode bit.
2997 *
2998 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2999 * to the start of the following row. Otherwise, the cursor is clamped to the
3000 * end of the screen and attempts to write past it are ignored.
3001 *
3002 * Defaults to on.
3003 *
3004 * @param {boolean} state True to set wraparound mode, false to unset.
3005 */
3006hterm.Terminal.prototype.setWraparound = function(state) {
3007 this.options_.wraparound = state;
3008};
3009
3010/**
3011 * Set the reverse-wraparound mode bit.
3012 *
3013 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
3014 * to the end of the previous row. Otherwise, the cursor is clamped to column
3015 * 0.
3016 *
3017 * Defaults to off.
3018 *
3019 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
3020 */
3021hterm.Terminal.prototype.setReverseWraparound = function(state) {
3022 this.options_.reverseWraparound = state;
3023};
3024
3025/**
3026 * Selects between the primary and alternate screens.
3027 *
3028 * If alternate mode is on, the alternate screen is active. Otherwise the
3029 * primary screen is active.
3030 *
3031 * Swapping screens has no effect on the scrollback buffer.
3032 *
3033 * Each screen maintains its own cursor position.
3034 *
3035 * Defaults to off.
3036 *
3037 * @param {boolean} state True to set alternate mode, false to unset.
3038 */
3039hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07003040 if (state == (this.screen_ == this.alternateScreen_)) {
3041 return;
3042 }
3043 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3044 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08003045 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
3046
Joel Hockey42dba8f2020-03-26 16:21:11 -07003047 // Swap color overrides.
3048 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3049 oldOverrides.forEach((c, i) => {
3050 if (!newOverrides.hasOwnProperty(i)) {
3051 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
3052 }
3053 });
3054 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
3055
rginda35c456b2012-02-09 17:29:05 -08003056 if (this.screen_.rowsArray.length &&
3057 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3058 // If the screen changed sizes while we were away, our rowIndexes may
3059 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003060 const offset = this.scrollbackRows_.length;
3061 const ary = this.screen_.rowsArray;
3062 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003063 ary[i].rowIndex = offset + i;
3064 }
3065 }
rginda8ba33642011-12-14 12:31:31 -08003066
rginda35c456b2012-02-09 17:29:05 -08003067 this.realizeWidth_(this.screenSize.width);
3068 this.realizeHeight_(this.screenSize.height);
3069 this.scrollPort_.syncScrollHeight();
3070 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003071
rginda6d397402012-01-17 10:58:29 -08003072 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003073 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003074};
3075
3076/**
3077 * Set the cursor-blink mode bit.
3078 *
3079 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3080 * a visible cursor does not blink.
3081 *
3082 * You should make sure to turn blinking off if you're going to dispose of a
3083 * terminal, otherwise you'll leak a timeout.
3084 *
3085 * Defaults to on.
3086 *
3087 * @param {boolean} state True to set cursor-blink mode, false to unset.
3088 */
3089hterm.Terminal.prototype.setCursorBlink = function(state) {
3090 this.options_.cursorBlink = state;
3091
3092 if (!state && this.timeouts_.cursorBlink) {
3093 clearTimeout(this.timeouts_.cursorBlink);
3094 delete this.timeouts_.cursorBlink;
3095 }
3096
Mike Frysingerbdb34802020-04-07 03:47:32 -04003097 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003098 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003099 }
rginda8ba33642011-12-14 12:31:31 -08003100};
3101
3102/**
3103 * Set the cursor-visible mode bit.
3104 *
3105 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3106 *
3107 * Defaults to on.
3108 *
3109 * @param {boolean} state True to set cursor-visible mode, false to unset.
3110 */
3111hterm.Terminal.prototype.setCursorVisible = function(state) {
3112 this.options_.cursorVisible = state;
3113
3114 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003115 if (this.timeouts_.cursorBlink) {
3116 clearTimeout(this.timeouts_.cursorBlink);
3117 delete this.timeouts_.cursorBlink;
3118 }
rginda87b86462011-12-14 13:48:03 -08003119 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003120 return;
3121 }
3122
rginda87b86462011-12-14 13:48:03 -08003123 this.syncCursorPosition_();
3124
3125 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003126
3127 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003128 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003129 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003130 }
rginda8ba33642011-12-14 12:31:31 -08003131
Robert Gindaea2183e2014-07-17 09:51:51 -07003132 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003133 } else {
3134 if (this.timeouts_.cursorBlink) {
3135 clearTimeout(this.timeouts_.cursorBlink);
3136 delete this.timeouts_.cursorBlink;
3137 }
3138 }
3139};
3140
3141/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003142 * Pause blinking temporarily.
3143 *
3144 * When the cursor moves around, it can be helpful to momentarily pause the
3145 * blinking. This could be when the user is typing in things, or when they're
3146 * moving around with the arrow keys.
3147 */
3148hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3149 if (!this.options_.cursorBlink) {
3150 return;
3151 }
3152
3153 this.cursorBlinkPause_ = true;
3154
3155 // If a timeout is already pending, reset the clock due to the new input.
3156 if (this.timeouts_.cursorBlinkPause) {
3157 clearTimeout(this.timeouts_.cursorBlinkPause);
3158 }
3159 // After 500ms, resume blinking. That seems like a good balance between user
3160 // input timings & responsiveness to resume.
3161 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3162 delete this.timeouts_.cursorBlinkPause;
3163 this.cursorBlinkPause_ = false;
3164 }, 500);
3165};
3166
3167/**
rginda87b86462011-12-14 13:48:03 -08003168 * Synchronizes the visible cursor and document selection with the current
3169 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003170 *
3171 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003172 */
3173hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003174 const topRowIndex = this.scrollPort_.getTopRowIndex();
3175 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3176 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003177 this.screen_.cursorPosition.row;
3178
Raymes Khoury15697f42018-07-17 11:37:18 +10003179 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003180 if (this.accessibilityReader_.accessibilityEnabled) {
3181 // Report the new position of the cursor for accessibility purposes.
3182 const cursorColumnIndex = this.screen_.cursorPosition.column;
3183 const cursorLineText =
3184 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003185 // This will force the selection to be sync'd to the cursor position if the
3186 // user has pressed a key. Generally we would only sync the cursor position
3187 // when selection is collapsed so that if the user has selected something
3188 // we don't clear the selection by moving the selection. However when a
3189 // screen reader is used, it's intuitive for entering a key to move the
3190 // selection to the cursor.
3191 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003192 this.accessibilityReader_.afterCursorChange(
3193 cursorLineText, cursorRowIndex, cursorColumnIndex);
3194 }
3195
rginda8ba33642011-12-14 12:31:31 -08003196 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003197 // Cursor is scrolled off screen, hide it.
3198 this.cursorOffScreen_ = true;
3199 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003200 return false;
rginda8ba33642011-12-14 12:31:31 -08003201 }
3202
Joel Hockey3babf302020-04-22 15:00:06 -07003203 if (this.cursorNode_.style.display == 'none') {
3204 // Re-display the terminal cursor if it was hidden.
3205 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003206 this.cursorNode_.style.display = '';
3207 }
3208
Mike Frysinger44c32202017-08-05 01:13:09 -04003209 // Position the cursor using CSS variable math. If we do the math in JS,
3210 // the float math will end up being more precise than the CSS which will
3211 // cause the cursor tracking to be off.
3212 this.setCssVar(
3213 'cursor-offset-row',
3214 `${cursorRowIndex - topRowIndex} + ` +
3215 `${this.scrollPort_.visibleRowTopMargin}px`);
3216 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003217
3218 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003219 '(' + this.screen_.cursorPosition.column +
3220 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003221 ')');
3222
Joel Hockey72f7fd62020-07-30 19:29:16 -07003223 // Update the caret for a11y purposes unless FindBar has focus which it should
3224 // keep.
3225 if (!this.findBar.hasFocus) {
3226 const selection = this.document_.getSelection();
3227 if (selection && (selection.isCollapsed || forceSyncSelection)) {
3228 this.screen_.syncSelectionCaret(selection);
3229 }
Raymes Khoury15697f42018-07-17 11:37:18 +10003230 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003231 return true;
rginda8ba33642011-12-14 12:31:31 -08003232};
3233
Robert Gindafb1be6a2013-12-11 11:56:22 -08003234/**
3235 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3236 * and character cell dimensions.
3237 */
Robert Ginda830583c2013-08-07 13:20:46 -07003238hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003239 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003240
3241 if (this.cursorNode_.getAttribute('focus') == 'false') {
3242 // Always show a block cursor when unfocused.
3243 shape = hterm.Terminal.cursorShape.BLOCK;
3244 }
3245
Mike Frysingerdc727792020-04-10 01:41:13 -04003246 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003247
3248 switch (shape) {
3249 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003250 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003251 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003252 style.borderLeftStyle = 'solid';
3253 break;
3254
3255 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003256 style.backgroundColor = 'transparent';
3257 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003258 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003259 break;
3260
3261 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003262 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003263 style.borderBottomStyle = '';
3264 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003265 break;
3266 }
3267};
3268
rginda8ba33642011-12-14 12:31:31 -08003269/**
3270 * Synchronizes the visible cursor with the current cursor coordinates.
3271 *
3272 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003273 * Multiple calls will be coalesced into a single sync. This should be called
3274 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003275 */
3276hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003277 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003278 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003279 }
rginda8ba33642011-12-14 12:31:31 -08003280
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003281 if (this.accessibilityReader_.accessibilityEnabled) {
3282 // Report the previous position of the cursor for accessibility purposes.
3283 const cursorRowIndex = this.scrollbackRows_.length +
3284 this.screen_.cursorPosition.row;
3285 const cursorColumnIndex = this.screen_.cursorPosition.column;
3286 const cursorLineText =
3287 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3288 this.accessibilityReader_.beforeCursorChange(
3289 cursorLineText, cursorRowIndex, cursorColumnIndex);
3290 }
3291
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003292 this.timeouts_.syncCursor = setTimeout(() => {
3293 this.syncCursorPosition_();
3294 delete this.timeouts_.syncCursor;
3295 });
rginda87b86462011-12-14 13:48:03 -08003296};
3297
rgindacc2996c2012-02-24 14:59:31 -08003298/**
3299 * Show the terminal overlay for a given amount of time.
3300 *
Jason Lin3d825782020-05-12 11:02:48 +10003301 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003302 *
Mike Frysingerc8e15162020-11-05 16:57:18 -05003303 * @param {string|!Node} msg The message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003304 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003305 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3306 * stay up forever (or until the next overlay).
3307 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003308hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Mike Frysingerc8e15162020-11-05 16:57:18 -05003309 const node = typeof msg === 'string' ? new Text(msg) : msg;
Jason Lin34567412020-05-14 10:32:09 +10003310
Joel Hockeyedac0e72020-05-14 20:16:20 -07003311 if (!this.ready_ || !this.div_) {
3312 return;
3313 }
rgindaf0090c92012-02-10 14:58:52 -08003314
Joel Hockeyedac0e72020-05-14 20:16:20 -07003315 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003316 this.overlayNode_ = this.document_.createElement('div');
3317 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003318 'color: rgb(var(--hterm-background-color));' +
3319 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003320 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003321 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003322 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003323 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003324 'position: absolute;' +
Mike Frysinger8c795ae2020-10-20 04:16:32 -04003325 'user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003326 '-webkit-transition: opacity 180ms ease-in;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003327 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003328
3329 this.overlayNode_.addEventListener('mousedown', function(e) {
3330 e.preventDefault();
3331 e.stopPropagation();
3332 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003333 }
3334
Jason Lin34567412020-05-14 10:32:09 +10003335 this.overlayNode_.textContent = ''; // Remove all children first.
3336 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003337
Mike Frysingerbdb34802020-04-07 03:47:32 -04003338 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003339 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003340 }
rgindaf0090c92012-02-10 14:58:52 -08003341
Mike Frysingerccca8552020-10-22 21:18:44 -04003342 const divSize = this.div_.getBoundingClientRect();
3343 const overlaySize = this.overlayNode_.getBoundingClientRect();
Robert Ginda97769282013-02-01 15:30:30 -08003344
Robert Ginda8a59f762014-07-23 11:29:55 -07003345 this.overlayNode_.style.top =
3346 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003347 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003348 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003349
Mike Frysingerbdb34802020-04-07 03:47:32 -04003350 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003351 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003352 }
rgindaf0090c92012-02-10 14:58:52 -08003353
Jason Lin34567412020-05-14 10:32:09 +10003354 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003355
Mike Frysingerec4225d2020-04-07 05:00:01 -04003356 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003357 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003358 }
rgindacc2996c2012-02-24 14:59:31 -08003359
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003360 this.overlayTimeout_ = setTimeout(() => {
3361 this.overlayNode_.style.opacity = '0';
3362 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003363 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003364};
3365
3366/**
3367 * Hide the terminal overlay immediately.
3368 *
3369 * Useful when we show an overlay for an event with an unknown end time.
3370 */
3371hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003372 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003373 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003374 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003375 this.overlayTimeout_ = null;
3376
Mike Frysingerbdb34802020-04-07 03:47:32 -04003377 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003378 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003379 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003380 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003381};
3382
rginda4bba5e12012-06-20 16:15:30 -07003383/**
3384 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003385 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003386 * Note: In Chrome, this should work unless the user has rejected the permission
3387 * request. In Firefox extension environment, you'll need the "clipboardRead"
3388 * permission. In other environments, this might always fail as the browser
3389 * frequently blocks access for security reasons.
3390 *
3391 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3392 * value is always null. Otherwise, this function uses legacy pasting and
3393 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003394 */
3395hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003396 if (!this.alwaysUseLegacyPasting &&
3397 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003398 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3399 return null;
3400 } else {
3401 // Legacy pasting.
3402 try {
3403 return this.document_.execCommand('paste');
3404 } catch (firefoxException) {
3405 // Ignore this. FF 40 and older would incorrectly throw an exception if
3406 // there was an error instead of returning false.
3407 return false;
3408 }
3409 }
rginda4bba5e12012-06-20 16:15:30 -07003410};
3411
3412/**
3413 * Copy a string to the system clipboard.
3414 *
3415 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003416 *
3417 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003418 */
3419hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003420 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003421 if (!this.clipboardNotice_) {
3422 this.clipboardNotice_ = this.document_.createElement('div');
3423 this.clipboardNotice_.style.textAlign = 'center';
3424 const copyImage = lib.resource.getData('hterm/images/copy');
3425 this.clipboardNotice_.innerHTML =
3426 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3427 }
Mike Frysingerc8e15162020-11-05 16:57:18 -05003428 setTimeout(() => this.showOverlay(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003429 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003430
Mike Frysinger96eacae2019-01-02 18:13:56 -05003431 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003432};
3433
Evan Jones2600d4f2016-12-06 09:29:36 -05003434/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003435 * Display an image.
3436 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003437 * Either URI or buffer or blob fields must be specified.
3438 *
Joel Hockey0f933582019-08-27 18:01:51 -07003439 * @param {{
3440 * name: (string|undefined),
3441 * size: (string|number|undefined),
3442 * preserveAspectRation: (boolean|undefined),
3443 * inline: (boolean|undefined),
3444 * width: (string|number|undefined),
3445 * height: (string|number|undefined),
3446 * align: (string|undefined),
3447 * url: (string|undefined),
3448 * buffer: (!ArrayBuffer|undefined),
3449 * blob: (!Blob|undefined),
3450 * type: (string|undefined),
3451 * }} options The image to display.
3452 * name A human readable string for the image
3453 * size The size (in bytes).
3454 * preserveAspectRatio Whether to preserve aspect.
3455 * inline Whether to display the image inline.
3456 * width The width of the image.
3457 * height The height of the image.
3458 * align Direction to align the image.
3459 * uri The source URI for the image.
3460 * buffer The ArrayBuffer image data.
3461 * blob The Blob image data.
3462 * type The MIME type of the image data.
3463 * @param {function()=} onLoad Callback when loading finishes.
3464 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003465 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003466hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003467 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003468 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003469 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003470 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003471 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003472
3473 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003474 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003475 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003476 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003477
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003478 // See if the mime type is available. If not, guess from the filename.
3479 // We don't list all possible mime types because the browser can usually
3480 // guess it correctly. So list the ones that need a bit more help.
3481 if (!options.type) {
3482 const ary = options.name.split('.');
3483 const ext = ary[ary.length - 1].trim();
3484 switch (ext) {
3485 case 'svg':
3486 case 'svgz':
3487 options.type = 'image/svg+xml';
3488 break;
3489 }
3490 }
3491
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003492 // Has the user approved image display yet?
3493 if (this.allowImagesInline !== true) {
3494 this.newLine();
3495 const row = this.getRowNode(this.scrollbackRows_.length +
3496 this.getCursorRow() - 1);
3497
3498 if (this.allowImagesInline === false) {
3499 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3500 'Inline Images Disabled');
3501 return;
3502 }
3503
3504 // Show a prompt.
3505 let button;
3506 const span = this.document_.createElement('span');
3507 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3508 span.style.fontWeight = 'bold';
3509 span.style.borderWidth = '1px';
3510 span.style.borderStyle = 'dashed';
3511 button = this.document_.createElement('span');
3512 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3513 button.style.marginLeft = '1em';
3514 button.style.borderWidth = '1px';
3515 button.style.borderStyle = 'solid';
3516 button.addEventListener('click', () => {
3517 this.prefs_.set('allow-images-inline', false);
3518 });
3519 span.appendChild(button);
3520 button = this.document_.createElement('span');
3521 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3522 'allow this session');
3523 button.style.marginLeft = '1em';
3524 button.style.borderWidth = '1px';
3525 button.style.borderStyle = 'solid';
3526 button.addEventListener('click', () => {
3527 this.allowImagesInline = true;
3528 });
3529 span.appendChild(button);
3530 button = this.document_.createElement('span');
3531 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3532 button.style.marginLeft = '1em';
3533 button.style.borderWidth = '1px';
3534 button.style.borderStyle = 'solid';
3535 button.addEventListener('click', () => {
3536 this.prefs_.set('allow-images-inline', true);
3537 });
3538 span.appendChild(button);
3539
3540 row.appendChild(span);
3541 return;
3542 }
3543
3544 // See if we should show this object directly, or download it.
3545 if (options.inline) {
3546 const io = this.io.push();
3547 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003548 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003549
3550 // While we're loading the image, eat all the user's input.
3551 io.onVTKeystroke = io.sendString = () => {};
3552
3553 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003554 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003555 if (options.uri !== undefined) {
3556 img.src = options.uri;
3557 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003558 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003559 img.src = URL.createObjectURL(blob);
3560 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003561 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003562 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003563 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003564 img.title = img.alt = options.name;
3565
3566 // Attach the image to the page to let it load/render. It won't stay here.
3567 // This is needed so it's visible and the DOM can calculate the height. If
3568 // the image is hidden or not in the DOM, the height is always 0.
3569 this.document_.body.appendChild(img);
3570
3571 // Wait for the image to finish loading before we try moving it to the
3572 // right place in the terminal.
3573 img.onload = () => {
3574 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003575 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003576 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003577 img.style.maxWidth = `${screenSize.width}px`;
3578 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003579
3580 // Parse a width/height specification.
3581 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003582 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003583 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003584 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003585
3586 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3587 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003588 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003589 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003590 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003591 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003592 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003593 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003594 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003595 }
3596
3597 return '';
3598 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003599 img.style.width = parseDim(
3600 options.width, screenSize.width, '--hterm-charsize-width');
3601 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003602 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003603
3604 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003605 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003606 const padRows = Math.ceil(img.clientHeight /
3607 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003608 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003609 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003610 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003611
3612 // Update the max height in case the user shrinks the character size.
3613 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3614
3615 // Move the image to the last row. This way when we scroll up, it doesn't
3616 // disappear when the first row gets clipped. It will disappear when we
3617 // scroll down and the last row is clipped ...
3618 this.document_.body.removeChild(img);
3619 // Create a wrapper node so we can do an absolute in a relative position.
3620 // This helps with rounding errors between JS & CSS counts.
3621 const div = this.document_.createElement('div');
3622 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003623 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003624 img.style.position = 'absolute';
3625 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3626 div.appendChild(img);
3627 const row = this.getRowNode(this.scrollbackRows_.length +
3628 this.getCursorRow() - 1);
3629 row.appendChild(div);
3630
Mike Frysinger2558ed52019-01-14 01:03:41 -05003631 // Now that the image has been read, we can revoke the source.
3632 if (options.uri === undefined) {
3633 URL.revokeObjectURL(img.src);
3634 }
3635
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003636 io.hideOverlay();
3637 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003638
Mike Frysingerbdb34802020-04-07 03:47:32 -04003639 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003640 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003641 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003642 };
3643
3644 // If we got a malformed image, give up.
3645 img.onerror = (e) => {
3646 this.document_.body.removeChild(img);
3647 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003648 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003649 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003650
Mike Frysingerbdb34802020-04-07 03:47:32 -04003651 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003652 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003653 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003654 };
3655 } else {
3656 // We can't use chrome.downloads.download as that requires "downloads"
3657 // permissions, and that works only in extensions, not apps.
3658 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003659 if (options.uri !== undefined) {
3660 a.href = options.uri;
3661 } else if (options.buffer !== undefined) {
3662 const blob = new Blob([options.buffer]);
3663 a.href = URL.createObjectURL(blob);
3664 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003665 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003666 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003667 a.download = options.name;
3668 this.document_.body.appendChild(a);
3669 a.click();
3670 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003671 if (options.uri === undefined) {
3672 URL.revokeObjectURL(a.href);
3673 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003674 }
3675};
3676
3677/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003678 * Returns the selected text, or null if no text is selected.
3679 *
3680 * @return {string|null}
3681 */
rgindaa09e7332012-08-17 12:49:51 -07003682hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003683 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003684 selection.sync();
3685
Mike Frysingerbdb34802020-04-07 03:47:32 -04003686 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003687 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003688 }
rgindaa09e7332012-08-17 12:49:51 -07003689
rgindaa09e7332012-08-17 12:49:51 -07003690 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003691 let startOffset = selection.startOffset;
3692 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003693
Raymes Khoury334625a2018-06-25 10:29:40 +10003694 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003695 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003696 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003697 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003698
Robert Gindafdbb3f22012-09-06 20:23:06 -07003699 if (node.nodeName != 'X-ROW') {
3700 // If the selection doesn't start on an x-row node, then it must be
3701 // somewhere inside the x-row. Add any characters from previous siblings
3702 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003703
3704 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3705 // If node is the text node in a styled span, move up to the span node.
3706 node = node.parentNode;
3707 }
3708
Robert Gindafdbb3f22012-09-06 20:23:06 -07003709 while (node.previousSibling) {
3710 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003711 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003712 }
rgindaa09e7332012-08-17 12:49:51 -07003713 }
3714
3715 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003716 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003717 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003718 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003719
Robert Gindafdbb3f22012-09-06 20:23:06 -07003720 if (node.nodeName != 'X-ROW') {
3721 // If the selection doesn't end on an x-row node, then it must be
3722 // somewhere inside the x-row. Add any characters from following siblings
3723 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003724
3725 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3726 // If node is the text node in a styled span, move up to the span node.
3727 node = node.parentNode;
3728 }
3729
Robert Gindafdbb3f22012-09-06 20:23:06 -07003730 while (node.nextSibling) {
3731 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003732 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003733 }
rgindaa09e7332012-08-17 12:49:51 -07003734 }
3735
Mike Frysingerdc727792020-04-10 01:41:13 -04003736 const rv = this.getRowsText(selection.startRow.rowIndex,
3737 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003738 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003739};
3740
rginda4bba5e12012-06-20 16:15:30 -07003741/**
3742 * Copy the current selection to the system clipboard, then clear it after a
3743 * short delay.
3744 */
3745hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003746 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003747 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003748 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003749 }
rginda4bba5e12012-06-20 16:15:30 -07003750};
3751
Joel Hockey0f933582019-08-27 18:01:51 -07003752/**
3753 * Show overlay with current terminal size.
3754 */
rgindaf0090c92012-02-10 14:58:52 -08003755hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003756 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003757 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003758 }
rgindaf0090c92012-02-10 14:58:52 -08003759};
3760
rginda87b86462011-12-14 13:48:03 -08003761/**
3762 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3763 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003764 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003765 */
3766hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003767 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003768 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003769 }
rginda87b86462011-12-14 13:48:03 -08003770
Mike Frysinger225c99d2019-10-20 14:02:37 -06003771 this.pauseCursorBlink_();
3772
Mike Frysinger79669762018-12-30 20:51:10 -05003773 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003774};
3775
3776/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003777 * Open the selected url.
3778 */
3779hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003780 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003781
3782 // If there is no selection, try and expand wherever they clicked.
3783 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003784 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003785 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003786
3787 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003788 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003789 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003790 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003791 }
3792
3793 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003794 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003795 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003796 }
Mike Frysinger43472622017-06-26 18:11:07 -04003797
3798 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003799 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003800 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3801 // We have to whitelist a few protocols that lack authorities and thus
3802 // never use the //. Like mailto.
3803 switch (str.split(':', 1)[0]) {
3804 case 'mailto':
3805 break;
3806 default:
3807 str = 'http://' + str;
3808 break;
3809 }
3810 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003811
Mike Frysinger720fa832017-10-23 01:15:52 -04003812 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003813};
Mike Frysinger70b94692017-01-26 18:57:50 -10003814
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003815/**
3816 * Manage the automatic mouse hiding behavior while typing.
3817 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003818 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003819 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003820hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003821 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3822 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003823 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003824 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003825 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003826
3827 this.mouseHideWhileTyping_ = !!v;
3828};
3829
3830/**
3831 * Handler for monitoring user keyboard activity.
3832 *
3833 * This isn't for processing the keystrokes directly, but for updating any
3834 * state that might toggle based on the user using the keyboard at all.
3835 *
Joel Hockey0f933582019-08-27 18:01:51 -07003836 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003837 */
3838hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3839 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003840 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003841 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003842 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003843};
Mike Frysinger70b94692017-01-26 18:57:50 -10003844
3845/**
rgindad5613292012-06-19 15:40:37 -07003846 * Add the terminalRow and terminalColumn properties to mouse events and
3847 * then forward on to onMouse().
3848 *
3849 * The terminalRow and terminalColumn properties contain the (row, column)
3850 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003851 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003852 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003853 */
3854hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003855 if (e.processedByTerminalHandler_) {
3856 // We register our event handlers on the document, as well as the cursor
3857 // and the scroll blocker. Mouse events that occur on the cursor or
3858 // scroll blocker will also appear on the document, but we don't want to
3859 // process them twice.
3860 //
3861 // We can't just prevent bubbling because that has other side effects, so
3862 // we decorate the event object with this property instead.
3863 return;
3864 }
3865
Mike Frysinger468966c2018-08-28 13:48:51 -04003866 // Consume navigation events. Button 3 is usually "browser back" and
3867 // button 4 is "browser forward" which we don't want to happen.
3868 if (e.button > 2) {
3869 e.preventDefault();
3870 // We don't return so click events can be passed to the remote below.
3871 }
3872
Mike Frysingerdc727792020-04-10 01:41:13 -04003873 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003874 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3875
rgindafaa74742012-08-21 13:34:03 -07003876 e.processedByTerminalHandler_ = true;
3877
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003878 // Handle auto hiding of mouse cursor while typing.
3879 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3880 // Make sure the mouse cursor is visible.
3881 this.syncMouseStyle();
3882 // This debounce isn't perfect, but should work well enough for such a
3883 // simple implementation. If the user moved the mouse, we enabled this
3884 // debounce, and then moved the mouse just before the timeout, we wouldn't
3885 // debounce that later movement.
3886 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3887 }
3888
Robert Gindaeda48db2014-07-17 09:25:30 -07003889 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003890 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003891 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003892 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003893 this.scrollPort_.characterSize.height) + 1;
3894 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003895 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003896
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003897 // Clamp row and column.
3898 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3899 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3900
3901 // Ignore mousedown in the scrollbar area.
3902 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003903 return;
3904 }
3905
Joel Hockey3babf302020-04-22 15:00:06 -07003906 if (this.options_.cursorVisible && !reportMouseEvents &&
3907 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003908 // If the cursor is visible and we're not sending mouse events to the
3909 // host app, then we want to hide the terminal cursor when the mouse
3910 // cursor is over top. This keeps the terminal cursor from interfering
3911 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003912 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3913 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3914 this.cursorNode_.style.display = 'none';
3915 } else if (this.cursorNode_.style.display == 'none') {
3916 this.cursorNode_.style.display = '';
3917 }
3918 }
rgindad5613292012-06-19 15:40:37 -07003919
Robert Ginda928cf632014-03-05 15:07:41 -08003920 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003921 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003922
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003923 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003924 // If VT mouse reporting is disabled, or has been defeated with
3925 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003926 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003927 this.setSelectionEnabled(true);
3928 } else {
3929 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003930 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003931 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003932 this.setSelectionEnabled(false);
3933 e.preventDefault();
3934 }
3935 }
3936
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003937 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003938 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003939 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003940 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003941 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003942 }
rgindad5613292012-06-19 15:40:37 -07003943 }
3944
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003945 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003946 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003947 // Ignore links created using OSC-8 as those will open by themselves, and
3948 // the visible text is most likely not the URI they want anyways.
3949 if (e.target.className === 'uri-node') {
3950 return;
3951 }
3952
Mike Frysinger70b94692017-01-26 18:57:50 -10003953 // Debounce this event with the dblclick event. If you try to doubleclick
3954 // a URL to open it, Chrome will fire click then dblclick, but we won't
3955 // have expanded the selection text at the first click event.
3956 clearTimeout(this.timeouts_.openUrl);
3957 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3958 500);
3959 return;
3960 }
3961
Mike Frysinger847577f2017-05-23 23:25:57 -04003962 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003963 if (e.ctrlKey && e.button == 2 /* right button */) {
3964 e.preventDefault();
3965 this.contextMenu.show(e, this);
3966 } else if (e.button == this.mousePasteButton ||
3967 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003968 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003969 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003970 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003971 }
3972 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003973
Mike Frysinger2edd3612017-05-24 00:54:39 -04003974 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003975 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003976 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003977 }
3978
3979 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3980 this.scrollBlockerNode_.engaged) {
3981 // Disengage the scroll-blocker after one of these events.
3982 this.scrollBlockerNode_.engaged = false;
3983 this.scrollBlockerNode_.style.top = '-99px';
3984 }
3985
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003986 // Emulate arrow key presses via scroll wheel events.
3987 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3988 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003989 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003990 const delta =
3991 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003992
Mike Frysinger321063c2018-08-29 15:33:14 -04003993 // Helper to turn a wheel event delta into a series of key presses.
3994 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3995 if (distance == 0) {
3996 return '';
3997 }
3998
3999 // Convert the scroll distance into a number of rows/cols.
4000 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4001 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4002 return data.repeat(cells);
4003 };
4004
4005 // The order between up/down and left/right doesn't really matter.
4006 this.io.sendString(
4007 // Up/down arrow keys.
4008 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4009 'A', 'B') +
4010 // Left/right arrow keys.
4011 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004012 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004013 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004014
4015 e.preventDefault();
4016 }
4017 }
Robert Ginda928cf632014-03-05 15:07:41 -08004018 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004019 if (!this.scrollBlockerNode_.engaged) {
4020 if (e.type == 'mousedown') {
4021 // Move the scroll-blocker into place if we want to keep the scrollport
4022 // from scrolling.
4023 this.scrollBlockerNode_.engaged = true;
4024 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4025 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4026 } else if (e.type == 'mousemove') {
4027 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4028 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004029 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004030 e.preventDefault();
4031 }
4032 }
Robert Ginda928cf632014-03-05 15:07:41 -08004033
4034 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004035 }
4036
Robert Ginda928cf632014-03-05 15:07:41 -08004037 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4038 // Restore this on mouseup in case it was temporarily defeated with a
4039 // alt-mousedown. Only do this when the selection is empty so that
4040 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004041 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004042 }
rgindad5613292012-06-19 15:40:37 -07004043};
4044
4045/**
4046 * Clients should override this if they care to know about mouse events.
4047 *
4048 * The event parameter will be a normal DOM mouse click event with additional
4049 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004050 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004051 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004052 */
4053hterm.Terminal.prototype.onMouse = function(e) { };
4054
4055/**
rginda8e92a692012-05-20 19:37:20 -07004056 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004057 *
4058 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004059 */
Rob Spies06533ba2014-04-24 11:20:37 -07004060hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4061 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004062 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004063
Mike Frysingerbdb34802020-04-07 03:47:32 -04004064 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004065 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004066 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004067
Mike Frysingerbdb34802020-04-07 03:47:32 -04004068 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004069 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004070 }
rginda8e92a692012-05-20 19:37:20 -07004071};
4072
4073/**
rginda8ba33642011-12-14 12:31:31 -08004074 * React when the ScrollPort is scrolled.
4075 */
4076hterm.Terminal.prototype.onScroll_ = function() {
4077 this.scheduleSyncCursorPosition_();
4078};
4079
4080/**
rginda9846e2f2012-01-27 13:53:33 -08004081 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004082 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004083 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004084 */
4085hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004086 this.onPasteData_(e.text);
4087};
4088
4089/**
4090 * Handle pasted data.
4091 *
4092 * @param {string} data The pasted data.
4093 */
4094hterm.Terminal.prototype.onPasteData_ = function(data) {
4095 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004096 if (this.options_.bracketedPaste) {
4097 // We strip out most escape sequences as they can cause issues (like
4098 // inserting an \x1b[201~ midstream). We pass through whitespace
4099 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4100 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004101 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004102 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4103 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4104 }
Robert Gindaa063b202014-07-21 11:08:25 -07004105
4106 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004107};
4108
4109/**
rgindaa09e7332012-08-17 12:49:51 -07004110 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004111 *
Joel Hockey0f933582019-08-27 18:01:51 -07004112 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004113 */
4114hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004115 if (!this.useDefaultWindowCopy) {
4116 e.preventDefault();
4117 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4118 }
rgindaa09e7332012-08-17 12:49:51 -07004119};
4120
4121/**
rginda8ba33642011-12-14 12:31:31 -08004122 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004123 *
4124 * Note: This function should not directly contain code that alters the internal
4125 * state of the terminal. That kind of code belongs in realizeWidth or
4126 * realizeHeight, so that it can be executed synchronously in the case of a
4127 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004128 */
4129hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004130 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4131 this.scrollPort_.characterSize.width) || 0;
4132 const rowCount = lib.f.smartFloorDivide(
4133 this.scrollPort_.getScreenHeight(),
4134 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004135
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004136 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004137 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004138 // gets removed from the document or during the initial load, and we can't
4139 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004140 // This can also happen if called before the scrollPort calculates the
4141 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004142 return;
4143 }
4144
Mike Frysingerdc727792020-04-10 01:41:13 -04004145 const isNewSize = (columnCount != this.screenSize.width ||
4146 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004147 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004148
4149 // We do this even if the size didn't change, just to be sure everything is
4150 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004151 this.realizeSize_(columnCount, rowCount);
Jason Lin002e1392020-07-30 14:20:24 +10004152 this.updateCssCharsize_();
rgindaa8ba17d2012-08-15 14:41:10 -07004153
Mike Frysingerbdb34802020-04-07 03:47:32 -04004154 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004155 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004156 }
rgindaa8ba17d2012-08-15 14:41:10 -07004157
Robert Gindafb1be6a2013-12-11 11:56:22 -08004158 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004159 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004160
4161 if (wasScrolledEnd) {
4162 this.scrollEnd();
4163 }
rginda8ba33642011-12-14 12:31:31 -08004164};
4165
4166/**
4167 * Service the cursor blink timeout.
4168 */
4169hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004170 if (!this.options_.cursorBlink) {
4171 delete this.timeouts_.cursorBlink;
4172 return;
4173 }
4174
Robert Ginda830583c2013-08-07 13:20:46 -07004175 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004176 this.cursorNode_.style.opacity == '0' ||
4177 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004178 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004179 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4180 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004181 } else {
rginda87b86462011-12-14 13:48:03 -08004182 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004183 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4184 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004185 }
4186};
David Reveman8f552492012-03-28 12:18:41 -04004187
4188/**
4189 * Set the scrollbar-visible mode bit.
4190 *
4191 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4192 * Otherwise it will not.
4193 *
4194 * Defaults to on.
4195 *
4196 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4197 */
4198hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4199 this.scrollPort_.setScrollbarVisible(state);
4200};
Michael Kelly485ecd12014-06-09 11:41:56 -04004201
4202/**
Rob Spies49039e52014-12-17 13:40:04 -08004203 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004204 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004205 *
4206 * Defaults to 1.
4207 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004208 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004209 */
4210hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4211 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4212};
4213
4214/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004215 * Close all web notifications created by terminal bells.
4216 */
4217hterm.Terminal.prototype.closeBellNotifications_ = function() {
4218 this.bellNotificationList_.forEach(function(n) {
4219 n.close();
4220 });
4221 this.bellNotificationList_.length = 0;
4222};
Raymes Khourye5d48982018-08-02 09:08:32 +10004223
4224/**
4225 * Syncs the cursor position when the scrollport gains focus.
4226 */
4227hterm.Terminal.prototype.onScrollportFocus_ = function() {
4228 // If the cursor is offscreen we set selection to the last row on the screen.
4229 const topRowIndex = this.scrollPort_.getTopRowIndex();
4230 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4231 const selection = this.document_.getSelection();
4232 if (!this.syncCursorPosition_() && selection) {
4233 selection.collapse(this.getRowNode(bottomRowIndex));
4234 }
4235};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004236
4237/**
4238 * Clients can override this if they want to provide an options page.
4239 */
4240hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4241
4242
4243/**
4244 * Called when user selects to open the options page.
4245 */
4246hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4247 this.onOpenOptionsPage();
4248};