blob: dd4888e036e4b19b6d7f3c26b115ba1989e04624 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700109 this.screenBorderSize_ = 0;
110
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.scrollOnOutput_ = null;
112 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400113 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800114
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700115 // True if we should override mouse event reporting to allow local selection.
116 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800117
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400118 // Whether to auto hide the mouse cursor when typing.
119 this.setAutomaticMouseHiding();
120 // Timer to keep mouse visible while it's being used.
121 this.mouseHideDelay_ = null;
122
rgindaf0090c92012-02-10 14:58:52 -0800123 // Terminal bell sound.
124 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400125 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800126 this.bellAudio_.setAttribute('preload', 'auto');
127
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000128 // The AccessibilityReader object for announcing command output.
129 this.accessibilityReader_ = null;
130
Mike Frysingercc114512017-09-11 21:39:17 -0400131 // The context menu object.
132 this.contextMenu = new hterm.ContextMenu();
133
Michael Kelly485ecd12014-06-09 11:41:56 -0400134 // All terminal bell notifications that have been generated (not necessarily
135 // shown).
136 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700137 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
139 // Whether we have permission to display notifications.
140 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
rginda6d397402012-01-17 10:58:29 -0800142 // Cursor position and attributes saved with DECSC.
143 this.savedOptions_ = {};
144
rginda8ba33642011-12-14 12:31:31 -0800145 // The current mode bits for the terminal.
146 this.options_ = new hterm.Options();
147
148 // Timeouts we might need to clear.
149 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800150
151 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800152 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800153
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800154 this.saveCursorAndState(true);
155
Zhu Qunying30d40712017-03-14 16:27:00 -0700156 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800157 this.keyboard = new hterm.Keyboard(this);
158
rginda87b86462011-12-14 13:48:03 -0800159 // General IO interface that can be given to third parties without exposing
160 // the entire terminal object.
161 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800162
rgindad5613292012-06-19 15:40:37 -0700163 // True if mouse-click-drag should scroll the terminal.
164 this.enableMouseDragScroll = true;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400167 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700168 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700169
Zhu Qunying30d40712017-03-14 16:27:00 -0700170 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700171 this.useDefaultWindowCopy = false;
172
173 this.clearSelectionAfterCopy = true;
174
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400175 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800176 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700177
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400178 // Whether we allow images to be shown.
179 this.allowImagesInline = null;
180
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400181 this.reportFocus = false;
182
Jason Linf129f3c2020-03-23 11:52:08 +1100183 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
184 this.alwaysUseLegacyPasting = false;
185
Joel Hockey3a44a442019-10-14 16:22:56 -0700186 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500187 function() { this.onTerminalReady(); }.bind(this));
shivanggargde5387e2020-06-10 01:31:16 +0530188
189 /** @const */
190 this.findBar = new hterm.FindBar(this);
rginda87b86462011-12-14 13:48:03 -0800191};
192
193/**
Robert Ginda830583c2013-08-07 13:20:46 -0700194 * Possible cursor shapes.
195 */
196hterm.Terminal.cursorShape = {
197 BLOCK: 'BLOCK',
198 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400199 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700200};
201
202/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700203 * Clients should override this to be notified when the terminal is ready
204 * for use.
205 *
206 * The terminal initialization is asynchronous, and shouldn't be used before
207 * this method is called.
208 */
209hterm.Terminal.prototype.onTerminalReady = function() { };
210
211/**
rginda35c456b2012-02-09 17:29:05 -0800212 * Default tab with of 8 to match xterm.
213 */
214hterm.Terminal.prototype.tabWidth = 8;
215
216/**
rginda9f5222b2012-03-05 11:53:28 -0800217 * Select a preference profile.
218 *
219 * This will load the terminal preferences for the given profile name and
220 * associate subsequent preference changes with the new preference profile.
221 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500222 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800223 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400224 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700225 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800226 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400227hterm.Terminal.prototype.setProfile = function(
228 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800230
Mike Frysingerdc727792020-04-10 01:41:13 -0400231 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800232
Mike Frysingerbdb34802020-04-07 03:47:32 -0400233 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400235 }
rginda9f5222b2012-03-05 11:53:28 -0800236
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700238
239 /**
240 * Clears and reloads key bindings. Used by preferences
241 * 'keybindings' and 'keybindings-os-defaults'.
242 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 * @param {*?=} bindings
244 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700245 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700247 terminal.keyboard.bindings.clear();
248
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400249 // Default to an empty object so we still handle OS defaults.
250 if (bindings === null) {
251 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700252 }
253
254 if (!(bindings instanceof Object)) {
255 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400256 bindings = {};
257 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700258 }
259
260 try {
261 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
262 } catch (ex) {
263 console.error('Error in keybindings preference: ' + ex);
264 }
265 }
266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800268 'alt-gr-mode': function(v) {
269 if (v == null) {
270 if (navigator.language.toLowerCase() == 'en-us') {
271 v = 'none';
272 } else {
273 v = 'right-alt';
274 }
275 } else if (typeof v == 'string') {
276 v = v.toLowerCase();
277 } else {
278 v = 'none';
279 }
280
Mike Frysingerbdb34802020-04-07 03:47:32 -0400281 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800282 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400283 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800284
285 terminal.keyboard.altGrMode = v;
286 },
287
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700288 'alt-backspace-is-meta-backspace': function(v) {
289 terminal.keyboard.altBackspaceIsMetaBackspace = v;
290 },
291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'alt-is-meta': function(v) {
293 terminal.keyboard.altIsMeta = v;
294 },
295
296 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400297 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700298 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400299 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700300
301 terminal.keyboard.altSendsWhat = v;
302 },
303
304 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400305 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800306 if (ary) {
307 terminal.bellAudio_.setAttribute('src',
308 lib.resource.getDataUrl(ary[1]));
309 } else {
310 terminal.bellAudio_.setAttribute('src', v);
311 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 },
313
Michael Kelly485ecd12014-06-09 11:41:56 -0400314 'desktop-notification-bell': function(v) {
315 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700316 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400317 Notification.permission === 'granted';
318 if (!terminal.desktopNotificationBell_) {
319 // Note: We don't call Notification.requestPermission here because
320 // Chrome requires the call be the result of a user action (such as an
321 // onclick handler), and pref listeners are run asynchronously.
322 //
323 // A way of working around this would be to display a dialog in the
324 // terminal with a "click-to-request-permission" button.
325 console.warn('desktop-notification-bell is true but we do not have ' +
326 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400327 }
328 } else {
329 terminal.desktopNotificationBell_ = false;
330 }
331 },
332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'background-color': function(v) {
334 terminal.setBackgroundColor(v);
335 },
336
337 'background-image': function(v) {
338 terminal.scrollPort_.setBackgroundImage(v);
339 },
340
341 'background-size': function(v) {
342 terminal.scrollPort_.setBackgroundSize(v);
343 },
344
345 'background-position': function(v) {
346 terminal.scrollPort_.setBackgroundPosition(v);
347 },
348
349 'backspace-sends-backspace': function(v) {
350 terminal.keyboard.backspaceSendsBackspace = v;
351 },
352
Brad Town18654b62015-03-12 00:27:45 -0700353 'character-map-overrides': function(v) {
354 if (!(v == null || v instanceof Object)) {
355 console.warn('Preference character-map-modifications is not an ' +
356 'object: ' + v);
357 return;
358 }
359
Mike Frysinger095d4062017-06-14 00:29:48 -0700360 terminal.vt.characterMaps.reset();
361 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700362 },
363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'cursor-blink': function(v) {
365 terminal.setCursorBlink(!!v);
366 },
367
Joel Hockey9d10ba12019-05-28 01:25:02 -0700368 'cursor-shape': function(v) {
369 terminal.setCursorShape(v);
370 },
371
Robert Gindaea2183e2014-07-17 09:51:51 -0700372 'cursor-blink-cycle': function(v) {
373 if (v instanceof Array &&
374 typeof v[0] == 'number' &&
375 typeof v[1] == 'number') {
376 terminal.cursorBlinkCycle_ = v;
377 } else if (typeof v == 'number') {
378 terminal.cursorBlinkCycle_ = [v, v];
379 } else {
380 // Fast blink indicates an error.
381 terminal.cursorBlinkCycle_ = [100, 100];
382 }
383 },
384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'cursor-color': function(v) {
386 terminal.setCursorColor(v);
387 },
388
389 'color-palette-overrides': function(v) {
390 if (!(v == null || v instanceof Object || v instanceof Array)) {
391 console.warn('Preference color-palette-overrides is not an array or ' +
392 'object: ' + v);
393 return;
rginda9f5222b2012-03-05 11:53:28 -0800394 }
rginda9f5222b2012-03-05 11:53:28 -0800395
Joel Hockey42dba8f2020-03-26 16:21:11 -0700396 // Call terminal.setColorPalette here and below with the new default
397 // value before changing it in lib.colors.colorPalette to ensure that
398 // CSS vars are updated.
Mike Frysinger06ce15d2020-10-21 21:53:29 -0400399 lib.colors.stockPalette.forEach((c, i) => terminal.setColorPalette(i, c));
400 lib.colors.colorPalette = lib.colors.stockPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700401
Robert Ginda57f03b42012-09-13 11:02:48 -0700402 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400403 for (const key in v) {
404 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 if (isNaN(i) || i < 0 || i > 255) {
406 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
407 continue;
408 }
409
410 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400411 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700412 if (rgb) {
413 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700415 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 }
417 }
rginda30f20f62012-04-05 16:36:19 -0700418 }
rginda30f20f62012-04-05 16:36:19 -0700419
Joel Hockey42dba8f2020-03-26 16:21:11 -0700420 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
421 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 },
rginda30f20f62012-04-05 16:36:19 -0700423
Robert Ginda57f03b42012-09-13 11:02:48 -0700424 'copy-on-select': function(v) {
425 terminal.copyOnSelect = !!v;
426 },
rginda9f5222b2012-03-05 11:53:28 -0800427
Rob Spies0bec09b2014-06-06 15:58:09 -0700428 'use-default-window-copy': function(v) {
429 terminal.useDefaultWindowCopy = !!v;
430 },
431
432 'clear-selection-after-copy': function(v) {
433 terminal.clearSelectionAfterCopy = !!v;
434 },
435
Robert Ginda7e5e9522014-03-14 12:23:58 -0700436 'ctrl-plus-minus-zero-zoom': function(v) {
437 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
438 },
439
Robert Gindafb5a3f92014-05-13 14:12:00 -0700440 'ctrl-c-copy': function(v) {
441 terminal.keyboard.ctrlCCopy = v;
442 },
443
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100444 'ctrl-v-paste': function(v) {
445 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700446 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100447 },
448
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700449 'paste-on-drop': function(v) {
450 terminal.scrollPort_.setPasteOnDrop(v);
451 },
452
Masaya Suzuki273aa982014-05-31 07:25:55 +0900453 'east-asian-ambiguous-as-two-column': function(v) {
454 lib.wc.regardCjkAmbiguous = v;
455 },
456
Robert Ginda57f03b42012-09-13 11:02:48 -0700457 'enable-8-bit-control': function(v) {
458 terminal.vt.enable8BitControl = !!v;
459 },
rginda30f20f62012-04-05 16:36:19 -0700460
Robert Ginda57f03b42012-09-13 11:02:48 -0700461 'enable-bold': function(v) {
462 terminal.syncBoldSafeState();
463 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400464
Robert Ginda3e278d72014-03-25 13:18:51 -0700465 'enable-bold-as-bright': function(v) {
466 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
467 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
468 },
469
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400470 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500471 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400472 },
473
Robert Ginda57f03b42012-09-13 11:02:48 -0700474 'enable-clipboard-write': function(v) {
475 terminal.vt.enableClipboardWrite = !!v;
476 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400477
Robert Ginda3755e752013-05-31 13:34:09 -0700478 'enable-dec12': function(v) {
479 terminal.vt.enableDec12 = !!v;
480 },
481
Mike Frysinger38f267d2018-09-07 02:50:59 -0400482 'enable-csi-j-3': function(v) {
483 terminal.vt.enableCsiJ3 = !!v;
484 },
485
shivanggarg2b7b0d52020-07-10 11:01:34 +0530486 'find-result-color': function(v) {
487 terminal.findBar.setFindResultColor(v);
488 },
489
shivanggarga72e8ba2020-07-16 07:11:17 +0530490 'find-result-selected-color': function(v) {
491 terminal.findBar.setFindResultSelectedColor(v);
492 },
493
Robert Ginda57f03b42012-09-13 11:02:48 -0700494 'font-family': function(v) {
495 terminal.syncFontFamily();
496 },
rginda30f20f62012-04-05 16:36:19 -0700497
Robert Ginda57f03b42012-09-13 11:02:48 -0700498 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700499 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700500 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500501 console.error(`Invalid font size: ${v}`);
502 return;
503 }
504
Robert Ginda57f03b42012-09-13 11:02:48 -0700505 terminal.setFontSize(v);
506 },
rginda9875d902012-08-20 16:21:57 -0700507
Robert Ginda57f03b42012-09-13 11:02:48 -0700508 'font-smoothing': function(v) {
509 terminal.syncFontFamily();
510 },
rgindade84e382012-04-20 15:39:31 -0700511
Robert Ginda57f03b42012-09-13 11:02:48 -0700512 'foreground-color': function(v) {
513 terminal.setForegroundColor(v);
514 },
rginda30f20f62012-04-05 16:36:19 -0700515
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400516 'hide-mouse-while-typing': function(v) {
517 terminal.setAutomaticMouseHiding(v);
518 },
519
Robert Ginda57f03b42012-09-13 11:02:48 -0700520 'home-keys-scroll': function(v) {
521 terminal.keyboard.homeKeysScroll = v;
522 },
rginda4bba5e12012-06-20 16:15:30 -0700523
Robert Gindaa8165692015-06-15 14:46:31 -0700524 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700525 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
526 },
Robert Gindaa8165692015-06-15 14:46:31 -0700527
Joel Hockey95a9e272020-03-16 21:19:53 -0700528 'keybindings-os-defaults': function(v) {
529 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700530 },
531
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700532 'media-keys-are-fkeys': function(v) {
533 terminal.keyboard.mediaKeysAreFKeys = v;
534 },
535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'meta-sends-escape': function(v) {
537 terminal.keyboard.metaSendsEscape = v;
538 },
rginda30f20f62012-04-05 16:36:19 -0700539
Mike Frysinger847577f2017-05-23 23:25:57 -0400540 'mouse-right-click-paste': function(v) {
541 terminal.mouseRightClickPaste = v;
542 },
543
Robert Ginda57f03b42012-09-13 11:02:48 -0700544 'mouse-paste-button': function(v) {
545 terminal.syncMousePasteButton();
546 },
rgindaa8ba17d2012-08-15 14:41:10 -0700547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'page-keys-scroll': function(v) {
549 terminal.keyboard.pageKeysScroll = v;
550 },
551
Robert Ginda40932892012-12-10 17:26:40 -0800552 'pass-alt-number': function(v) {
553 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700554 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800555 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500556 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800557 }
558
559 terminal.passAltNumber = v;
560 },
561
562 'pass-ctrl-number': function(v) {
563 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700564 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800565 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500566 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800567 }
568
569 terminal.passCtrlNumber = v;
570 },
571
Joel Hockey0e052042020-02-19 05:37:19 -0800572 'pass-ctrl-n': function(v) {
573 terminal.passCtrlN = v;
574 },
575
576 'pass-ctrl-t': function(v) {
577 terminal.passCtrlT = v;
578 },
579
580 'pass-ctrl-tab': function(v) {
581 terminal.passCtrlTab = v;
582 },
583
584 'pass-ctrl-w': function(v) {
585 terminal.passCtrlW = v;
586 },
587
Robert Ginda40932892012-12-10 17:26:40 -0800588 'pass-meta-number': function(v) {
589 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700590 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800591 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500592 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800593 }
594
595 terminal.passMetaNumber = v;
596 },
597
Marius Schilder77857b32014-05-14 16:21:26 -0700598 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700599 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700600 },
601
Robert Ginda8cb7d902013-06-20 14:37:18 -0700602 'receive-encoding': function(v) {
603 if (!(/^(utf-8|raw)$/).test(v)) {
604 console.warn('Invalid value for "receive-encoding": ' + v);
605 v = 'utf-8';
606 }
607
608 terminal.vt.characterEncoding = v;
609 },
610
Joel Hockey139d82d2020-04-07 23:04:29 -0700611 'screen-padding-size': function(v) {
612 v = parseInt(v, 10);
613 if (isNaN(v) || v < 0) {
614 console.error(`Invalid screen padding size: ${v}`);
615 return;
616 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700617 terminal.setScreenPaddingSize(v);
618 },
619
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700620 'screen-border-size': function(v) {
621 v = parseInt(v, 10);
622 if (isNaN(v) || v < 0) {
623 console.error(`Invalid screen border size: ${v}`);
624 return;
625 }
626 terminal.setScreenBorderSize(v);
627 },
628
629 'screen-border-color': function(v) {
630 terminal.div_.style.borderColor = v;
631 },
632
Robert Ginda57f03b42012-09-13 11:02:48 -0700633 'scroll-on-keystroke': function(v) {
634 terminal.scrollOnKeystroke_ = v;
635 },
rginda9f5222b2012-03-05 11:53:28 -0800636
Robert Ginda57f03b42012-09-13 11:02:48 -0700637 'scroll-on-output': function(v) {
638 terminal.scrollOnOutput_ = v;
639 },
rginda30f20f62012-04-05 16:36:19 -0700640
Robert Ginda57f03b42012-09-13 11:02:48 -0700641 'scrollbar-visible': function(v) {
642 terminal.setScrollbarVisible(v);
643 },
rginda9f5222b2012-03-05 11:53:28 -0800644
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400645 'scroll-wheel-may-send-arrow-keys': function(v) {
646 terminal.scrollWheelArrowKeys_ = v;
647 },
648
Rob Spies49039e52014-12-17 13:40:04 -0800649 'scroll-wheel-move-multiplier': function(v) {
650 terminal.setScrollWheelMoveMultipler(v);
651 },
652
Robert Ginda57f03b42012-09-13 11:02:48 -0700653 'shift-insert-paste': function(v) {
654 terminal.keyboard.shiftInsertPaste = v;
655 },
rginda9f5222b2012-03-05 11:53:28 -0800656
Mike Frysingera7768922017-07-28 15:00:12 -0400657 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400658 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400659 },
660
Robert Gindae76aa9f2014-03-14 12:29:12 -0700661 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400662 terminal.scrollPort_.setUserCssUrl(v);
663 },
664
665 'user-css-text': function(v) {
666 terminal.scrollPort_.setUserCssText(v);
667 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400668
669 'word-break-match-left': function(v) {
670 terminal.primaryScreen_.wordBreakMatchLeft = v;
671 terminal.alternateScreen_.wordBreakMatchLeft = v;
672 },
673
674 'word-break-match-right': function(v) {
675 terminal.primaryScreen_.wordBreakMatchRight = v;
676 terminal.alternateScreen_.wordBreakMatchRight = v;
677 },
678
679 'word-break-match-middle': function(v) {
680 terminal.primaryScreen_.wordBreakMatchMiddle = v;
681 terminal.alternateScreen_.wordBreakMatchMiddle = v;
682 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400683
684 'allow-images-inline': function(v) {
685 terminal.allowImagesInline = v;
686 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700687 });
rginda30f20f62012-04-05 16:36:19 -0700688
Robert Ginda57f03b42012-09-13 11:02:48 -0700689 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800690 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700691
Mike Frysingerec4225d2020-04-07 05:00:01 -0400692 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700693 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400694 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400695 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700696 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800697};
698
Rob Spies56953412014-04-28 14:09:47 -0700699/**
700 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500701 *
Joel Hockey0f933582019-08-27 18:01:51 -0700702 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700703 */
704hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700705 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700706};
707
Robert Gindaa063b202014-07-21 11:08:25 -0700708/**
709 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500710 *
711 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700712 */
713hterm.Terminal.prototype.setBracketedPaste = function(state) {
714 this.options_.bracketedPaste = state;
715};
Rob Spies56953412014-04-28 14:09:47 -0700716
rginda8e92a692012-05-20 19:37:20 -0700717/**
718 * Set the color for the cursor.
719 *
720 * If you want this setting to persist, set it through prefs_, rather than
721 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500722 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500723 * @param {string=} color The color to set. If not defined, we reset to the
724 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700725 */
726hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400727 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700728 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400729 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500730
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400731 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700732};
733
734/**
735 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400736 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500737 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700738 */
739hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400740 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700741};
742
743/**
rgindad5613292012-06-19 15:40:37 -0700744 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500745 *
746 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700747 */
748hterm.Terminal.prototype.setSelectionEnabled = function(state) {
749 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700750};
751
752/**
Joel Hockeyfde20592020-08-02 15:05:27 -0700753 * Set the background image.
754 *
755 * If you want this setting to persist, set it through prefs_, rather than
756 * with this method.
757 *
758 * @param {string=} cssUrl The image to set as a css url. If not defined, we
759 * reset to the saved user preference.
760 */
761hterm.Terminal.prototype.setBackgroundImage = function(cssUrl) {
762 if (cssUrl === undefined) {
763 cssUrl = this.prefs_.getString('background-image');
764 }
765 this.scrollPort_.setBackgroundImage(cssUrl);
766};
767
768/**
rginda8e92a692012-05-20 19:37:20 -0700769 * Set the background color.
770 *
771 * If you want this setting to persist, set it through prefs_, rather than
772 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500773 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500774 * @param {string=} color The color to set. If not defined, we reset to the
775 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700776 */
777hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400778 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700779 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400780 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500781
Joel Hockey42dba8f2020-03-26 16:21:11 -0700782 this.backgroundColor_ = lib.colors.normalizeCSS(color);
783 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700784};
785
rginda9f5222b2012-03-05 11:53:28 -0800786/**
787 * Return the current terminal background color.
788 *
789 * Intended for use by other classes, so we don't have to expose the entire
790 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500791 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700792 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800793 */
794hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700795 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700796};
797
798/**
799 * Set the foreground color.
800 *
801 * If you want this setting to persist, set it through prefs_, rather than
802 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500803 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500804 * @param {string=} color The color to set. If not defined, we reset to the
805 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700806 */
807hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400808 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700809 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400810 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500811
Joel Hockey42dba8f2020-03-26 16:21:11 -0700812 this.foregroundColor_ = lib.colors.normalizeCSS(color);
813 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800814};
815
816/**
817 * Return the current terminal foreground color.
818 *
819 * Intended for use by other classes, so we don't have to expose the entire
820 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500821 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700822 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800823 */
824hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700825 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800826};
827
828/**
rginda87b86462011-12-14 13:48:03 -0800829 * Create a new instance of a terminal command and run it with a given
830 * argument string.
831 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700832 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700833 * @param {string} commandName The command to run for this terminal.
834 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800835 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700836hterm.Terminal.prototype.runCommandClass = function(
837 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400838 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400839 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700840 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400841 }
rgindaf522ce02012-04-17 17:49:17 -0700842
rginda87b86462011-12-14 13:48:03 -0800843 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700844 {
845 commandName: commandName,
846 args: args,
rginda87b86462011-12-14 13:48:03 -0800847 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700848 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400849 onExit: (code) => {
850 this.io.pop();
851 this.uninstallKeyboard();
852 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
853 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400854 window.close();
855 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400856 },
rginda87b86462011-12-14 13:48:03 -0800857 });
858
rgindafeaf3142012-01-31 15:14:20 -0800859 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800860 this.command.run();
861};
862
863/**
rgindafeaf3142012-01-31 15:14:20 -0800864 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500865 *
866 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800867 */
868hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700869 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800870};
871
872/**
873 * Install the keyboard handler for this terminal.
874 *
875 * This will prevent the browser from seeing any keystrokes sent to the
876 * terminal.
877 */
878hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700879 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400880};
rgindafeaf3142012-01-31 15:14:20 -0800881
882/**
883 * Uninstall the keyboard handler for this terminal.
884 */
885hterm.Terminal.prototype.uninstallKeyboard = function() {
886 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400887};
rgindafeaf3142012-01-31 15:14:20 -0800888
889/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400890 * Set a CSS variable.
891 *
892 * Normally this is used to set variables in the hterm namespace.
893 *
894 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700895 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400896 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400897 */
898hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400899 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400900 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400901 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400902};
903
904/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700905 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
906 * input is valid.
907 *
908 * @param {string} name The variable to set.
909 * @param {?string} rgb The rgb value to assign to the variable.
910 */
911hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
912 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
913 if (ary) {
914 this.setCssVar(name, ary.slice(0, 3).join(','));
915 }
916};
917
918/**
919 * Sets the specified color for the active screen.
920 *
921 * @param {number} i The index into the 256 color palette to set.
922 * @param {?string} rgb The rgb value to assign to the variable.
923 */
924hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
925 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
926 this.setRgbColorCssVar(`color-${i}`, rgb);
927 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
928 }
929};
930
931/**
932 * Returns the current value in the active screen of the specified color.
933 *
934 * @param {number} i Color palette index.
935 * @return {string} rgb color.
936 */
937hterm.Terminal.prototype.getColorPalette = function(i) {
938 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
939 lib.colors.colorPalette[i];
940};
941
942/**
943 * Reset the specified color in the active screen to its default value.
944 *
945 * @param {number} i Color to reset
946 */
947hterm.Terminal.prototype.resetColor = function(i) {
948 this.setColorPalette(i, lib.colors.colorPalette[i]);
949 delete this.screen_.textAttributes.colorPaletteOverrides[i];
950};
951
952/**
953 * Reset the current screen color palette to the default state.
954 */
955hterm.Terminal.prototype.resetColorPalette = function() {
956 this.screen_.textAttributes.colorPaletteOverrides.forEach(
957 (c, i) => this.resetColor(i));
958};
959
960/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500961 * Get a CSS variable.
962 *
963 * Normally this is used to get variables in the hterm namespace.
964 *
965 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400966 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500967 * @return {string} The current setting for this variable.
968 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400969hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500970 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400971 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500972};
973
974/**
shivanggargf3b362a2020-07-10 11:06:35 +0530975 * @return {!hterm.ScrollPort}
976 */
977hterm.Terminal.prototype.getScrollPort = function() {
978 return this.scrollPort_;
979};
980
981/**
Jason Linbbbdb752020-03-06 16:26:59 +1100982 * Update CSS character size variables to match the scrollport.
983 */
984hterm.Terminal.prototype.updateCssCharsize_ = function() {
985 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
986 this.setCssVar('charsize-height',
987 this.scrollPort_.characterSize.height + 'px');
988};
989
990/**
rginda35c456b2012-02-09 17:29:05 -0800991 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800992 *
993 * Call setFontSize(0) to reset to the default font size.
994 *
995 * This function does not modify the font-size preference.
996 *
997 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800998 */
999hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001000 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001001 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001002 }
rginda9f5222b2012-03-05 11:53:28 -08001003
rginda35c456b2012-02-09 17:29:05 -08001004 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -07001005 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +11001006 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -08001007};
1008
1009/**
1010 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -05001011 *
1012 * @return {number}
rginda35c456b2012-02-09 17:29:05 -08001013 */
1014hterm.Terminal.prototype.getFontSize = function() {
1015 return this.scrollPort_.getFontSize();
1016};
1017
1018/**
rginda8e92a692012-05-20 19:37:20 -07001019 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @return {string}
rginda8e92a692012-05-20 19:37:20 -07001022 */
1023hterm.Terminal.prototype.getFontFamily = function() {
1024 return this.scrollPort_.getFontFamily();
1025};
1026
1027/**
rginda35c456b2012-02-09 17:29:05 -08001028 * Set the CSS "font-family" for this terminal.
1029 */
rginda9f5222b2012-03-05 11:53:28 -08001030hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001031 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1032 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001033 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001034 this.syncBoldSafeState();
1035};
1036
rginda4bba5e12012-06-20 16:15:30 -07001037/**
1038 * Set this.mousePasteButton based on the mouse-paste-button pref,
1039 * autodetecting if necessary.
1040 */
1041hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001042 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001043 if (typeof button == 'number') {
1044 this.mousePasteButton = button;
1045 return;
1046 }
1047
Mike Frysingeree81a002017-12-12 16:14:53 -05001048 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001049 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001050 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001051 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001052 }
1053};
1054
1055/**
1056 * Enable or disable bold based on the enable-bold pref, autodetecting if
1057 * necessary.
1058 */
rginda9f5222b2012-03-05 11:53:28 -08001059hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001060 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001061 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001062 this.primaryScreen_.textAttributes.enableBold = enableBold;
1063 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001064 return;
1065 }
1066
Mike Frysingerdc727792020-04-10 01:41:13 -04001067 const normalSize = this.scrollPort_.measureCharacterSize();
1068 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001069
Mike Frysingerdc727792020-04-10 01:41:13 -04001070 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001071 if (!isBoldSafe) {
1072 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001073 'from normal. Font family is: ' +
1074 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001075 }
rginda9f5222b2012-03-05 11:53:28 -08001076
Robert Gindaed016262012-10-26 16:27:09 -07001077 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1078 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001079};
1080
1081/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001082 * Control text blinking behavior.
1083 *
1084 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001085 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001086hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001087 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001088 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001089 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001090 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001091};
1092
1093/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001094 * Set the mouse cursor style based on the current terminal mode.
1095 */
1096hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001097 this.setCssVar('mouse-cursor-style',
1098 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1099 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001100 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001101};
1102
1103/**
rginda87b86462011-12-14 13:48:03 -08001104 * Return a copy of the current cursor position.
1105 *
Joel Hockey0f933582019-08-27 18:01:51 -07001106 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001107 */
1108hterm.Terminal.prototype.saveCursor = function() {
1109 return this.screen_.cursorPosition.clone();
1110};
1111
Evan Jones2600d4f2016-12-06 09:29:36 -05001112/**
1113 * Return the current text attributes.
1114 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001115 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001116 */
rgindaa19afe22012-01-25 15:40:22 -08001117hterm.Terminal.prototype.getTextAttributes = function() {
1118 return this.screen_.textAttributes;
1119};
1120
Evan Jones2600d4f2016-12-06 09:29:36 -05001121/**
1122 * Set the text attributes.
1123 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001124 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001125 */
rginda1a09aa02012-06-18 21:11:25 -07001126hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1127 this.screen_.textAttributes = textAttributes;
1128};
1129
rginda87b86462011-12-14 13:48:03 -08001130/**
rgindaf522ce02012-04-17 17:49:17 -07001131 * Return the current browser zoom factor applied to the terminal.
1132 *
1133 * @return {number} The current browser zoom factor.
1134 */
1135hterm.Terminal.prototype.getZoomFactor = function() {
1136 return this.scrollPort_.characterSize.zoomFactor;
1137};
1138
1139/**
rginda9846e2f2012-01-27 13:53:33 -08001140 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001141 *
1142 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001143 */
1144hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001145 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001146};
1147
1148/**
rginda87b86462011-12-14 13:48:03 -08001149 * Restore a previously saved cursor position.
1150 *
Joel Hockey0f933582019-08-27 18:01:51 -07001151 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001152 */
1153hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001154 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1155 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001156 this.screen_.setCursorPosition(row, column);
1157 if (cursor.column > column ||
1158 cursor.column == column && cursor.overflow) {
1159 this.screen_.cursorPosition.overflow = true;
1160 }
rginda87b86462011-12-14 13:48:03 -08001161};
1162
1163/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001164 * Clear the cursor's overflow flag.
1165 */
1166hterm.Terminal.prototype.clearCursorOverflow = function() {
1167 this.screen_.cursorPosition.overflow = false;
1168};
1169
1170/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001171 * Save the current cursor state to the corresponding screens.
1172 *
1173 * See the hterm.Screen.CursorState class for more details.
1174 *
1175 * @param {boolean=} both If true, update both screens, else only update the
1176 * current screen.
1177 */
1178hterm.Terminal.prototype.saveCursorAndState = function(both) {
1179 if (both) {
1180 this.primaryScreen_.saveCursorAndState(this.vt);
1181 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001182 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001183 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001184 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001185};
1186
1187/**
1188 * Restore the saved cursor state in the corresponding screens.
1189 *
1190 * See the hterm.Screen.CursorState class for more details.
1191 *
1192 * @param {boolean=} both If true, update both screens, else only update the
1193 * current screen.
1194 */
1195hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1196 if (both) {
1197 this.primaryScreen_.restoreCursorAndState(this.vt);
1198 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001199 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001200 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001201 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001202};
1203
1204/**
Robert Ginda830583c2013-08-07 13:20:46 -07001205 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001206 *
1207 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001208 */
1209hterm.Terminal.prototype.setCursorShape = function(shape) {
1210 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001211 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001212};
Robert Ginda830583c2013-08-07 13:20:46 -07001213
1214/**
1215 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001216 *
1217 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001218 */
1219hterm.Terminal.prototype.getCursorShape = function() {
1220 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001221};
Robert Ginda830583c2013-08-07 13:20:46 -07001222
1223/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001224 * Set the screen padding size in pixels.
1225 *
1226 * @param {number} size
1227 */
1228hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001229 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001230 this.scrollPort_.setScreenPaddingSize(size);
1231};
1232
1233/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001234 * Set the screen border size in pixels.
1235 *
1236 * @param {number} size
1237 */
1238hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1239 this.div_.style.borderWidth = `${size}px`;
1240 this.screenBorderSize_ = size;
1241 this.scrollPort_.resize();
1242};
1243
1244/**
rginda87b86462011-12-14 13:48:03 -08001245 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001246 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001247 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001248 */
1249hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001250 if (columnCount == null) {
1251 this.div_.style.width = '100%';
1252 return;
1253 }
1254
Joel Hockey139d82d2020-04-07 23:04:29 -07001255 const rightPadding = Math.max(
1256 this.scrollPort_.screenPaddingSize,
1257 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001258 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001259 (this.scrollPort_.characterSize.width * columnCount) +
1260 this.scrollPort_.screenPaddingSize + rightPadding +
1261 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001262 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001263 this.scheduleSyncCursorPosition_();
1264};
rginda87b86462011-12-14 13:48:03 -08001265
rgindac9bc5502012-01-18 11:48:44 -08001266/**
rginda35c456b2012-02-09 17:29:05 -08001267 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001268 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001269 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001270 */
1271hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001272 if (rowCount == null) {
1273 this.div_.style.height = '100%';
1274 return;
1275 }
1276
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001277 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1278 (2 * this.scrollPort_.screenPaddingSize) +
1279 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001280 this.realizeSize_(this.screenSize.width, rowCount);
1281 this.scheduleSyncCursorPosition_();
1282};
1283
1284/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001285 * Deal with terminal size changes.
1286 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001287 * @param {number} columnCount The number of columns.
1288 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001289 */
1290hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001291 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001292
Mike Frysinger0206e262019-06-13 10:18:19 -04001293 if (columnCount != this.screenSize.width) {
1294 notify = true;
1295 this.realizeWidth_(columnCount);
1296 }
1297
1298 if (rowCount != this.screenSize.height) {
1299 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001300 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001301 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001302
1303 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001304 if (notify) {
1305 this.io.onTerminalResize_(columnCount, rowCount);
1306 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001307};
1308
1309/**
rgindac9bc5502012-01-18 11:48:44 -08001310 * Deal with terminal width changes.
1311 *
1312 * This function does what needs to be done when the terminal width changes
1313 * out from under us. It happens here rather than in onResize_() because this
1314 * code may need to run synchronously to handle programmatic changes of
1315 * terminal width.
1316 *
1317 * Relying on the browser to send us an async resize event means we may not be
1318 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001319 *
1320 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001321 */
1322hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001323 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001324 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001325 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001326
Mike Frysingerdc727792020-04-10 01:41:13 -04001327 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001328 if (deltaColumns == 0) {
1329 // No change, so don't bother recalculating things.
1330 return;
1331 }
rgindac9bc5502012-01-18 11:48:44 -08001332
rginda87b86462011-12-14 13:48:03 -08001333 this.screenSize.width = columnCount;
1334 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001335
1336 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001337 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001338 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001339 }
rgindac9bc5502012-01-18 11:48:44 -08001340 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001341 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001342 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001343 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001344 }
rgindac9bc5502012-01-18 11:48:44 -08001345
1346 this.tabStops_.pop();
1347 }
1348 }
1349
1350 this.screen_.setColumnCount(this.screenSize.width);
1351};
1352
1353/**
1354 * Deal with terminal height changes.
1355 *
1356 * This function does what needs to be done when the terminal height changes
1357 * out from under us. It happens here rather than in onResize_() because this
1358 * code may need to run synchronously to handle programmatic changes of
1359 * terminal height.
1360 *
1361 * Relying on the browser to send us an async resize event means we may not be
1362 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001363 *
1364 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001365 */
1366hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001367 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001368 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001369 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001370
Mike Frysingerdc727792020-04-10 01:41:13 -04001371 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001372 if (deltaRows == 0) {
1373 // No change, so don't bother recalculating things.
1374 return;
1375 }
rgindac9bc5502012-01-18 11:48:44 -08001376
1377 this.screenSize.height = rowCount;
1378
Mike Frysingerdc727792020-04-10 01:41:13 -04001379 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001380
1381 if (deltaRows < 0) {
1382 // Screen got smaller.
1383 deltaRows *= -1;
1384 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001385 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001386 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001387 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001388 }
rgindac9bc5502012-01-18 11:48:44 -08001389
Mike Frysingerbdb34802020-04-07 03:47:32 -04001390 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001391 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001392 }
rgindac9bc5502012-01-18 11:48:44 -08001393
1394 this.screen_.popRow();
1395 deltaRows--;
1396 }
1397
Mike Frysingerdc727792020-04-10 01:41:13 -04001398 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001399 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1400
1401 // We just removed rows from the top of the screen, we need to update
1402 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001403 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001404 } else if (deltaRows > 0) {
1405 // Screen got larger.
1406
1407 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001408 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1409 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001410 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1411 this.screen_.unshiftRows(rows);
1412 deltaRows -= scrollbackCount;
1413 cursor.row += scrollbackCount;
1414 }
1415
Mike Frysingerbdb34802020-04-07 03:47:32 -04001416 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001417 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001418 }
rgindac9bc5502012-01-18 11:48:44 -08001419 }
1420
rginda35c456b2012-02-09 17:29:05 -08001421 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001422 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001423};
1424
1425/**
1426 * Scroll the terminal to the top of the scrollback buffer.
1427 */
1428hterm.Terminal.prototype.scrollHome = function() {
1429 this.scrollPort_.scrollRowToTop(0);
1430};
1431
1432/**
1433 * Scroll the terminal to the end.
1434 */
1435hterm.Terminal.prototype.scrollEnd = function() {
1436 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1437};
1438
1439/**
1440 * Scroll the terminal one page up (minus one line) relative to the current
1441 * position.
1442 */
1443hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001444 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001445};
1446
1447/**
1448 * Scroll the terminal one page down (minus one line) relative to the current
1449 * position.
1450 */
1451hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001452 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001453};
1454
rgindac9bc5502012-01-18 11:48:44 -08001455/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001456 * Scroll the terminal one line up relative to the current position.
1457 */
1458hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001459 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001460 this.scrollPort_.scrollRowToTop(i - 1);
1461};
1462
1463/**
1464 * Scroll the terminal one line down relative to the current position.
1465 */
1466hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001467 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001468 this.scrollPort_.scrollRowToTop(i + 1);
1469};
1470
1471/**
Robert Ginda40932892012-12-10 17:26:40 -08001472 * Clear primary screen, secondary screen, and the scrollback buffer.
1473 */
1474hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001475 this.clearHome(this.primaryScreen_);
1476 this.clearHome(this.alternateScreen_);
1477
1478 this.clearScrollback();
1479};
1480
1481/**
1482 * Clear scrollback buffer.
1483 */
1484hterm.Terminal.prototype.clearScrollback = function() {
1485 // Move to the end of the buffer in case the screen was scrolled back.
1486 // We're going to throw it away which would leave the display invalid.
1487 this.scrollEnd();
1488
Robert Ginda40932892012-12-10 17:26:40 -08001489 this.scrollbackRows_.length = 0;
1490 this.scrollPort_.resetCache();
1491
Mike Frysinger9c482b82018-09-07 02:49:36 -04001492 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1493 const bottom = screen.getHeight();
1494 this.renumberRows_(0, bottom, screen);
1495 });
Robert Ginda40932892012-12-10 17:26:40 -08001496
1497 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001498 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001499};
1500
1501/**
rgindac9bc5502012-01-18 11:48:44 -08001502 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001503 *
1504 * Perform a full reset to the default values listed in
1505 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001506 */
rginda87b86462011-12-14 13:48:03 -08001507hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001508 this.vt.reset();
1509
rgindac9bc5502012-01-18 11:48:44 -08001510 this.clearAllTabStops();
1511 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001512
Joel Hockey42dba8f2020-03-26 16:21:11 -07001513 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001514 const resetScreen = (screen) => {
1515 // We want to make sure to reset the attributes before we clear the screen.
1516 // The attributes might be used to initialize default/empty rows.
1517 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001518 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001519 this.clearHome(screen);
1520 screen.saveCursorAndState(this.vt);
1521 };
1522 resetScreen(this.primaryScreen_);
1523 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001524
Mike Frysinger84301d02017-11-29 13:28:46 -08001525 // Reset terminal options to their default values.
1526 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001527 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1528
Mike Frysinger84301d02017-11-29 13:28:46 -08001529 this.setVTScrollRegion(null, null);
1530
1531 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001532};
1533
rgindac9bc5502012-01-18 11:48:44 -08001534/**
1535 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001536 *
1537 * Perform a soft reset to the default values listed in
1538 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001539 */
rginda0f5c0292012-01-13 11:00:13 -08001540hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001541 this.vt.reset();
1542
rgindab8bc8932012-04-27 12:45:03 -07001543 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001544 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001545
Brad Townb62dfdc2015-03-16 19:07:15 -07001546 // We show the cursor on soft reset but do not alter the blink state.
1547 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1548
Joel Hockey42dba8f2020-03-26 16:21:11 -07001549 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001550 const resetScreen = (screen) => {
1551 // Xterm also resets the color palette on soft reset, even though it doesn't
1552 // seem to be documented anywhere.
1553 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001554 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001555 screen.saveCursorAndState(this.vt);
1556 };
1557 resetScreen(this.primaryScreen_);
1558 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001559
rgindab8bc8932012-04-27 12:45:03 -07001560 // The xterm man page explicitly says this will happen on soft reset.
1561 this.setVTScrollRegion(null, null);
1562
1563 // Xterm also shows the cursor on soft reset, but does not alter the blink
1564 // state.
rgindaa19afe22012-01-25 15:40:22 -08001565 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001566};
1567
rgindac9bc5502012-01-18 11:48:44 -08001568/**
1569 * Move the cursor forward to the next tab stop, or to the last column
1570 * if no more tab stops are set.
1571 */
1572hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001573 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001574
Mike Frysingerdc727792020-04-10 01:41:13 -04001575 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001576 if (this.tabStops_[i] > column) {
1577 this.setCursorColumn(this.tabStops_[i]);
1578 return;
1579 }
1580 }
1581
David Benjamin66e954d2012-05-05 21:08:12 -04001582 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001583 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001584 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001585 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001586};
1587
rgindac9bc5502012-01-18 11:48:44 -08001588/**
1589 * Move the cursor backward to the previous tab stop, or to the first column
1590 * if no previous tab stops are set.
1591 */
1592hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001593 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001594
Mike Frysingerdc727792020-04-10 01:41:13 -04001595 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001596 if (this.tabStops_[i] < column) {
1597 this.setCursorColumn(this.tabStops_[i]);
1598 return;
1599 }
1600 }
1601
1602 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001603};
1604
rgindac9bc5502012-01-18 11:48:44 -08001605/**
1606 * Set a tab stop at the given column.
1607 *
Joel Hockey0f933582019-08-27 18:01:51 -07001608 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001609 */
1610hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001611 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001612 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001613 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001614 }
rgindac9bc5502012-01-18 11:48:44 -08001615
1616 if (this.tabStops_[i] < column) {
1617 this.tabStops_.splice(i + 1, 0, column);
1618 return;
1619 }
1620 }
1621
1622 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001623};
1624
rgindac9bc5502012-01-18 11:48:44 -08001625/**
1626 * Clear the tab stop at the current cursor position.
1627 *
1628 * No effect if there is no tab stop at the current cursor position.
1629 */
1630hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001631 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001632
Mike Frysingerdc727792020-04-10 01:41:13 -04001633 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001634 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001635 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001636 }
rgindac9bc5502012-01-18 11:48:44 -08001637
1638 this.tabStops_.splice(i, 1);
1639};
1640
1641/**
1642 * Clear all tab stops.
1643 */
1644hterm.Terminal.prototype.clearAllTabStops = function() {
1645 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001646 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001647};
1648
1649/**
1650 * Set up the default tab stops, starting from a given column.
1651 *
1652 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001653 * from the specified column, or 0 if no column is provided. It also flags
1654 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001655 *
1656 * This does not clear the existing tab stops first, use clearAllTabStops
1657 * for that.
1658 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001659 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001660 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001661 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001662hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001663 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001664 // Round start up to a default tab stop.
1665 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001666 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001667 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001668 }
David Benjamin66e954d2012-05-05 21:08:12 -04001669
1670 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001671};
1672
rginda6d397402012-01-17 10:58:29 -08001673/**
rginda8ba33642011-12-14 12:31:31 -08001674 * Interpret a sequence of characters.
1675 *
1676 * Incomplete escape sequences are buffered until the next call.
1677 *
1678 * @param {string} str Sequence of characters to interpret or pass through.
1679 */
1680hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001681 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001682 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001683};
1684
1685/**
1686 * Take over the given DIV for use as the terminal display.
1687 *
Joel Hockey0f933582019-08-27 18:01:51 -07001688 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001689 */
1690hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001691 const charset = div.ownerDocument.characterSet.toLowerCase();
1692 if (charset != 'utf-8') {
1693 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1694 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1695 }
1696
rginda87b86462011-12-14 13:48:03 -08001697 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001698 this.div_.style.borderStyle = 'solid';
1699 this.div_.style.borderWidth = 0;
1700 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001701
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001702 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1703
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001704 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1705};
1706
1707/**
1708 * Initialisation of ScrollPort properties which need to be set after its DOM
1709 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001710 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001711 * @private
1712 */
1713hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001714 this.scrollPort_.setBackgroundImage(
1715 this.prefs_.getString('background-image'));
1716 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001717 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001718 this.prefs_.getString('background-position'));
1719 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1720 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1721 this.scrollPort_.setAccessibilityReader(
1722 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001723
rginda0918b652012-04-04 11:26:24 -07001724 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001725
Joel Hockeyd4fca732019-09-20 16:57:03 -07001726 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001727 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001728
Joel Hockeyd4fca732019-09-20 16:57:03 -07001729 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001730 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001731 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001732
rginda8ba33642011-12-14 12:31:31 -08001733 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001734 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301735 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001736
Evan Jones5f9df812016-12-06 09:38:58 -05001737 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001738 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001739
Mike Frysingerdc727792020-04-10 01:41:13 -04001740 const onMouse = this.onMouse_.bind(this);
1741 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001742 screenNode.addEventListener(
1743 'mousedown', /** @type {!EventListener} */ (onMouse));
1744 screenNode.addEventListener(
1745 'mouseup', /** @type {!EventListener} */ (onMouse));
1746 screenNode.addEventListener(
1747 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001748 this.scrollPort_.onScrollWheel = onMouse;
1749
Joel Hockeyd4fca732019-09-20 16:57:03 -07001750 screenNode.addEventListener(
1751 'keydown',
1752 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001753
Toni Barzic0bfa8922013-11-22 11:18:35 -08001754 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001755 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001756 // Listen for mousedown events on the screenNode as in FF the focus
1757 // events don't bubble.
1758 screenNode.addEventListener('mousedown', function() {
1759 setTimeout(this.onFocusChange_.bind(this, true));
1760 }.bind(this));
1761
Toni Barzic0bfa8922013-11-22 11:18:35 -08001762 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001763 'blur', this.onFocusChange_.bind(this, false));
1764
Mike Frysingerdc727792020-04-10 01:41:13 -04001765 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001766 style.textContent = `
1767.cursor-node[focus="false"] {
1768 box-sizing: border-box;
1769 background-color: transparent !important;
1770 border-width: 2px;
1771 border-style: solid;
1772}
1773menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001774 background: #fff;
1775 border-radius: 4px;
1776 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001777 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001778 display: none;
1779 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1780 margin: 0;
1781 padding: 8px 0;
1782 position: absolute;
1783 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001784}
1785menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001786 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001787 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1788 padding: 0.5em 1em;
1789 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001790}
1791menuitem.separator {
1792 border-bottom: none;
1793 height: 0.5em;
1794 padding: 0;
1795}
1796menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001797 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001798}
1799.wc-node {
1800 display: inline-block;
1801 text-align: center;
1802 width: calc(var(--hterm-charsize-width) * 2);
1803 line-height: var(--hterm-charsize-height);
1804}
1805:root {
1806 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1807 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001808 --hterm-blink-node-duration: 0.7s;
1809 --hterm-mouse-cursor-default: default;
1810 --hterm-mouse-cursor-text: text;
1811 --hterm-mouse-cursor-pointer: pointer;
1812 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001813 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001814
Mike Frysinger06ce15d2020-10-21 21:53:29 -04001815${lib.colors.stockPalette.map((c, i) => `
Joel Hockey42dba8f2020-03-26 16:21:11 -07001816 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1817`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001818}
1819.uri-node:hover {
1820 text-decoration: underline;
1821 cursor: var(--hterm-mouse-cursor-pointer);
1822}
1823@keyframes blink {
1824 from { opacity: 1.0; }
1825 to { opacity: 0.0; }
1826}
1827.blink-node {
1828 animation-name: blink;
1829 animation-duration: var(--hterm-blink-node-duration);
1830 animation-iteration-count: infinite;
1831 animation-timing-function: ease-in-out;
1832 animation-direction: alternate;
1833}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001834 // Insert this stock style as the first node so that any user styles will
1835 // override w/out having to use !important everywhere. The rules above mix
1836 // runtime variables with default ones designed to be overridden by the user,
1837 // but we can wait for a concrete case from the users to determine the best
1838 // way to split the sheet up to before & after the user-css settings.
1839 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001840
rginda8ba33642011-12-14 12:31:31 -08001841 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001842 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001843 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001844 this.cursorNode_.style.cssText = `
1845position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001846left: calc(var(--hterm-screen-padding-size) +
1847 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1848top: calc(var(--hterm-screen-padding-size) +
1849 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001850display: ${this.options_.cursorVisible ? '' : 'none'};
1851width: var(--hterm-charsize-width);
1852height: var(--hterm-charsize-height);
1853background-color: var(--hterm-cursor-color);
1854border-color: var(--hterm-cursor-color);
1855-webkit-transition: opacity, background-color 100ms linear;
1856-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001857
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001858 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001859 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1860 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001861
rginda8ba33642011-12-14 12:31:31 -08001862 this.document_.body.appendChild(this.cursorNode_);
1863
rgindad5613292012-06-19 15:40:37 -07001864 // When 'enableMouseDragScroll' is off we reposition this element directly
1865 // under the mouse cursor after a click. This makes Chrome associate
1866 // subsequent mousemove events with the scroll-blocker. Since the
1867 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1868 // events do not cause the scrollport to scroll.
1869 //
1870 // It's a hack, but it's the cleanest way I could find.
1871 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001872 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001873 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001874 this.scrollBlockerNode_.style.cssText =
1875 ('position: absolute;' +
1876 'top: -99px;' +
1877 'display: block;' +
1878 'width: 10px;' +
1879 'height: 10px;');
1880 this.document_.body.appendChild(this.scrollBlockerNode_);
1881
rgindad5613292012-06-19 15:40:37 -07001882 this.scrollPort_.onScrollWheel = onMouse;
1883 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1884 ].forEach(function(event) {
1885 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001886 this.cursorNode_.addEventListener(
1887 event, /** @type {!EventListener} */ (onMouse));
1888 this.document_.addEventListener(
1889 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001890 }.bind(this));
1891
1892 this.cursorNode_.addEventListener('mousedown', function() {
1893 setTimeout(this.focus.bind(this));
1894 }.bind(this));
1895
rginda8ba33642011-12-14 12:31:31 -08001896 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001897
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001898 // Re-sync fonts whenever a web font loads.
1899 this.document_.fonts.addEventListener(
1900 'loadingdone', () => this.syncFontFamily());
1901
rginda87b86462011-12-14 13:48:03 -08001902 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001903 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001904};
1905
rginda0918b652012-04-04 11:26:24 -07001906/**
1907 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001908 *
Joel Hockey0f933582019-08-27 18:01:51 -07001909 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001910 */
rginda87b86462011-12-14 13:48:03 -08001911hterm.Terminal.prototype.getDocument = function() {
1912 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001913};
1914
1915/**
rginda0918b652012-04-04 11:26:24 -07001916 * Focus the terminal.
1917 */
1918hterm.Terminal.prototype.focus = function() {
1919 this.scrollPort_.focus();
1920};
1921
1922/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001923 * Unfocus the terminal.
1924 */
1925hterm.Terminal.prototype.blur = function() {
1926 this.scrollPort_.blur();
1927};
1928
1929/**
rginda8ba33642011-12-14 12:31:31 -08001930 * Return the HTML Element for a given row index.
1931 *
1932 * This is a method from the RowProvider interface. The ScrollPort uses
1933 * it to fetch rows on demand as they are scrolled into view.
1934 *
1935 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1936 * pairs to conserve memory.
1937 *
Joel Hockey0f933582019-08-27 18:01:51 -07001938 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001939 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001940 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001941 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001942 * @override
rginda8ba33642011-12-14 12:31:31 -08001943 */
1944hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001945 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001946 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001947 }
rginda8ba33642011-12-14 12:31:31 -08001948
Mike Frysingerdc727792020-04-10 01:41:13 -04001949 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001950 return this.screen_.rowsArray[screenIndex];
1951};
1952
1953/**
1954 * Return the text content for a given range of rows.
1955 *
1956 * This is a method from the RowProvider interface. The ScrollPort uses
1957 * it to fetch text content on demand when the user attempts to copy their
1958 * selection to the clipboard.
1959 *
Joel Hockey0f933582019-08-27 18:01:51 -07001960 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001961 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001962 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001963 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001964 * relative to the start of the scrollback buffer.
1965 * @return {string} A single string containing the text value of the range of
1966 * rows. Lines will be newline delimited, with no trailing newline.
1967 */
1968hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001969 const ary = [];
1970 for (let i = start; i < end; i++) {
1971 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001972 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001973 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001974 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001975 }
rginda8ba33642011-12-14 12:31:31 -08001976 }
1977
rgindaa09e7332012-08-17 12:49:51 -07001978 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001979};
1980
1981/**
1982 * Return the text content for a given row.
1983 *
1984 * This is a method from the RowProvider interface. The ScrollPort uses
1985 * it to fetch text content on demand when the user attempts to copy their
1986 * selection to the clipboard.
1987 *
Joel Hockey0f933582019-08-27 18:01:51 -07001988 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001989 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001990 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001991 * @return {string} A string containing the text value of the selected row.
1992 */
1993hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001994 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001995 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001996};
1997
1998/**
1999 * Return the total number of rows in the addressable screen and in the
2000 * scrollback buffer of this terminal.
2001 *
2002 * This is a method from the RowProvider interface. The ScrollPort uses
2003 * it to compute the size of the scrollbar.
2004 *
Joel Hockey0f933582019-08-27 18:01:51 -07002005 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07002006 * @override
rginda8ba33642011-12-14 12:31:31 -08002007 */
2008hterm.Terminal.prototype.getRowCount = function() {
2009 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
2010};
2011
2012/**
2013 * Create DOM nodes for new rows and append them to the end of the terminal.
2014 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002015 * The new row is appended to the bottom of the list of rows, and does not
rginda8ba33642011-12-14 12:31:31 -08002016 * require renumbering (of the rowIndex property) of previous rows.
2017 *
2018 * If you think you want a new blank row somewhere in the middle of the
Joel Hockey95fe54e2020-07-23 01:12:24 -07002019 * terminal, look into insertRow_() or moveRows_().
rginda8ba33642011-12-14 12:31:31 -08002020 *
2021 * This method does not pay attention to vtScrollTop/Bottom, since you should
Joel Hockey95fe54e2020-07-23 01:12:24 -07002022 * be using insertRow_() or moveRows_() in cases where they would matter.
rginda8ba33642011-12-14 12:31:31 -08002023 *
2024 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002025 *
2026 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002027 */
2028hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002029 let cursorRow = this.screen_.rowsArray.length;
2030 const offset = this.scrollbackRows_.length + cursorRow;
2031 for (let i = 0; i < count; i++) {
2032 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002033 row.appendChild(this.document_.createTextNode(''));
2034 row.rowIndex = offset + i;
2035 this.screen_.pushRow(row);
2036 }
2037
Mike Frysingerdc727792020-04-10 01:41:13 -04002038 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002039 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002040 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002041 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002042 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002043 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002044 }
rginda8ba33642011-12-14 12:31:31 -08002045 }
2046
Mike Frysingerbdb34802020-04-07 03:47:32 -04002047 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002048 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002049 }
rginda8ba33642011-12-14 12:31:31 -08002050
rginda87b86462011-12-14 13:48:03 -08002051 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002052};
2053
2054/**
Joel Hockey95fe54e2020-07-23 01:12:24 -07002055 * Create a DOM node for a new row and insert it at the current position.
2056 *
2057 * The new row is inserted at the current cursor position, the existing top row
2058 * is moved to scrollback, and lines below are renumbered.
2059 *
2060 * The cursor will be positioned at column 0.
2061 */
2062hterm.Terminal.prototype.insertRow_ = function() {
2063 const row = this.document_.createElement('x-row');
2064 row.appendChild(this.document_.createTextNode(''));
2065
2066 this.scrollbackRows_.push(this.screen_.shiftRow());
2067
2068 const cursorRow = this.screen_.cursorPosition.row;
2069 this.screen_.insertRow(cursorRow, row);
2070
2071 this.renumberRows_(cursorRow, this.screen_.rowsArray.length);
2072
2073 this.setAbsoluteCursorPosition(cursorRow, 0);
2074 if (this.scrollPort_.isScrolledEnd) {
2075 this.scheduleScrollDown_();
2076 }
2077};
2078
2079/**
rginda8ba33642011-12-14 12:31:31 -08002080 * Relocate rows from one part of the addressable screen to another.
2081 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002082 * This is used to recycle rows during VT scrolls where a top region is set
2083 * (those which are driven by VT commands, rather than by the user manipulating
2084 * the scrollbar.)
rginda8ba33642011-12-14 12:31:31 -08002085 *
2086 * In this case, the blank lines scrolled into the scroll region are made of
2087 * the nodes we scrolled off. These have their rowIndex properties carefully
2088 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002089 *
2090 * @param {number} fromIndex The start index.
2091 * @param {number} count The number of rows to move.
2092 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002093 */
2094hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002095 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002096 this.screen_.insertRows(toIndex, ary);
2097
Mike Frysingerdc727792020-04-10 01:41:13 -04002098 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002099 if (fromIndex < toIndex) {
2100 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002101 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002102 } else {
2103 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002104 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002105 }
2106
2107 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002108 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002109};
2110
2111/**
2112 * Renumber the rowIndex property of the given range of rows.
2113 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002114 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002115 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002116 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002117 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002118 *
2119 * @param {number} start The start index.
2120 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002121 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002122 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002123hterm.Terminal.prototype.renumberRows_ = function(
2124 start, end, screen = undefined) {
2125 if (!screen) {
2126 screen = this.screen_;
2127 }
Robert Ginda40932892012-12-10 17:26:40 -08002128
Mike Frysingerdc727792020-04-10 01:41:13 -04002129 const offset = this.scrollbackRows_.length;
2130 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002131 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002132 }
2133};
2134
2135/**
2136 * Print a string to the terminal.
2137 *
2138 * This respects the current insert and wraparound modes. It will add new lines
2139 * to the end of the terminal, scrolling off the top into the scrollback buffer
2140 * if necessary.
2141 *
2142 * The string is *not* parsed for escape codes. Use the interpret() method if
2143 * that's what you're after.
2144 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002145 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002146 */
2147hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002148 this.scheduleSyncCursorPosition_();
2149
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002150 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002151 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002152
Mike Frysingerdc727792020-04-10 01:41:13 -04002153 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002154
Mike Frysingerdc727792020-04-10 01:41:13 -04002155 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002156 // Fun edge case: If the string only contains zero width codepoints (like
2157 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002158 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002159 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002160 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002161
2162 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002163 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2164 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002165 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002166 }
rgindaa19afe22012-01-25 15:40:22 -08002167
Mike Frysingerdc727792020-04-10 01:41:13 -04002168 let count = strWidth - startOffset;
2169 let didOverflow = false;
2170 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002171
rgindaa9abdd82012-08-06 18:05:09 -07002172 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2173 didOverflow = true;
2174 count = this.screenSize.width - this.screen_.cursorPosition.column;
2175 }
rgindaa19afe22012-01-25 15:40:22 -08002176
rgindaa9abdd82012-08-06 18:05:09 -07002177 if (didOverflow && !this.options_.wraparound) {
2178 // If the string overflowed the line but wraparound is off, then the
2179 // last printed character should be the last of the string.
2180 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002181 substr = lib.wc.substr(str, startOffset, count - 1) +
2182 lib.wc.substr(str, strWidth - 1);
2183 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002184 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002185 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002186 }
rgindaa19afe22012-01-25 15:40:22 -08002187
Mike Frysingerdc727792020-04-10 01:41:13 -04002188 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2189 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002190 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2191 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002192
2193 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002194 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002195 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002196 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002197 }
2198 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002199 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002200 }
2201
2202 this.screen_.maybeClipCurrentRow();
2203 startOffset += count;
Shivang Garga72e68c2020-07-18 08:58:32 +05302204 this.findBar.scheduleNotifyChanges(
2205 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rgindaa19afe22012-01-25 15:40:22 -08002206 }
rginda8ba33642011-12-14 12:31:31 -08002207
Mike Frysingerbdb34802020-04-07 03:47:32 -04002208 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002209 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002210 }
rginda8ba33642011-12-14 12:31:31 -08002211};
2212
2213/**
rginda87b86462011-12-14 13:48:03 -08002214 * Set the VT scroll region.
2215 *
rginda87b86462011-12-14 13:48:03 -08002216 * This also resets the cursor position to the absolute (0, 0) position, since
2217 * that's what xterm appears to do.
2218 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002219 * Setting the scroll region to the full height of the terminal will clear
2220 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2221 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2222 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2223 * continue to work as most users would expect.
2224 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002225 * @param {?number} scrollTop The zero-based top of the scroll region.
2226 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002227 * inclusive.
2228 */
2229hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Joel Hockey95fe54e2020-07-23 01:12:24 -07002230 this.vtScrollTop_ = scrollTop;
Joel Hockey4337eac2020-09-11 01:34:38 -07002231 this.vtScrollBottom_ = scrollBottom;
2232 if (scrollBottom == this.screenSize.height - 1) {
2233 this.vtScrollBottom_ = null;
2234 if (scrollTop == 0) {
2235 this.vtScrollTop_ = null;
2236 }
2237 }
rginda87b86462011-12-14 13:48:03 -08002238};
2239
2240/**
rginda8ba33642011-12-14 12:31:31 -08002241 * Return the top row index according to the VT.
2242 *
2243 * This will return 0 unless the terminal has been told to restrict scrolling
2244 * to some lower row. It is used for some VT cursor positioning and scrolling
2245 * commands.
2246 *
Joel Hockey0f933582019-08-27 18:01:51 -07002247 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002248 */
2249hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002250 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002251 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002252 }
rginda8ba33642011-12-14 12:31:31 -08002253
2254 return 0;
rginda87b86462011-12-14 13:48:03 -08002255};
rginda8ba33642011-12-14 12:31:31 -08002256
2257/**
2258 * Return the bottom row index according to the VT.
2259 *
2260 * This will return the height of the terminal unless the it has been told to
2261 * restrict scrolling to some higher row. It is used for some VT cursor
2262 * positioning and scrolling commands.
2263 *
Joel Hockey0f933582019-08-27 18:01:51 -07002264 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002265 */
2266hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002267 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002268 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002269 }
rginda8ba33642011-12-14 12:31:31 -08002270
rginda87b86462011-12-14 13:48:03 -08002271 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002272};
rginda8ba33642011-12-14 12:31:31 -08002273
2274/**
2275 * Process a '\n' character.
2276 *
2277 * If the cursor is on the final row of the terminal this will append a new
2278 * blank row to the screen and scroll the topmost row into the scrollback
2279 * buffer.
2280 *
2281 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002282 *
2283 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2284 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002285 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002286hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002287 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002288 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002289 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002290
Joel Hockey95fe54e2020-07-23 01:12:24 -07002291 const cursorAtEndOfScreen =
2292 (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1);
2293 const cursorAtEndOfVTRegion =
2294 (this.screen_.cursorPosition.row == this.getVTScrollBottom());
Robert Ginda9937abc2013-07-25 16:09:23 -07002295
Joel Hockey95fe54e2020-07-23 01:12:24 -07002296 if (this.vtScrollTop_ != null && cursorAtEndOfVTRegion) {
2297 // A VT Scroll region is active on top, we never append new rows.
2298 // We're at the end of the VT Scroll Region, perform a VT scroll.
2299 this.vtScrollUp(1);
2300 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
Robert Ginda9937abc2013-07-25 16:09:23 -07002301 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002302 // We're at the end of the screen. Append a new row to the terminal,
2303 // shifting the top row into the scrollback.
2304 this.appendRows_(1);
Joel Hockey95fe54e2020-07-23 01:12:24 -07002305 } else if (cursorAtEndOfVTRegion) {
2306 this.insertRow_();
rginda8ba33642011-12-14 12:31:31 -08002307 } else {
rginda87b86462011-12-14 13:48:03 -08002308 // Anywhere else in the screen just moves the cursor.
2309 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002310 }
2311};
2312
2313/**
2314 * Like newLine(), except maintain the cursor column.
2315 */
2316hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002317 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002318 this.newLine();
2319 this.setCursorColumn(column);
2320};
2321
2322/**
rginda87b86462011-12-14 13:48:03 -08002323 * If autoCarriageReturn is set then newLine(), else lineFeed().
2324 */
2325hterm.Terminal.prototype.formFeed = function() {
2326 if (this.options_.autoCarriageReturn) {
2327 this.newLine();
2328 } else {
2329 this.lineFeed();
2330 }
2331};
2332
2333/**
2334 * Move the cursor up one row, possibly inserting a blank line.
2335 *
2336 * The cursor column is not changed.
2337 */
2338hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002339 const scrollTop = this.getVTScrollTop();
2340 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002341
2342 if (currentRow == scrollTop) {
2343 this.insertLines(1);
2344 } else {
2345 this.setAbsoluteCursorRow(currentRow - 1);
2346 }
2347};
2348
2349/**
rginda8ba33642011-12-14 12:31:31 -08002350 * Replace all characters to the left of the current cursor with the space
2351 * character.
2352 *
2353 * TODO(rginda): This should probably *remove* the characters (not just replace
2354 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002355 * position.
rginda8ba33642011-12-14 12:31:31 -08002356 */
2357hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002358 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002359 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002360 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002361 this.screen_.overwriteString(' '.repeat(count), count);
Shivang Garga72e68c2020-07-18 08:58:32 +05302362 this.findBar.scheduleNotifyChanges(
2363 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002364 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002365};
2366
2367/**
David Benjamin684a9b72012-05-01 17:19:58 -04002368 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002369 *
2370 * The cursor position is unchanged.
2371 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002372 * If the current background color is not the default background color this
2373 * will insert spaces rather than delete. This is unfortunate because the
2374 * trailing space will affect text selection, but it's difficult to come up
2375 * with a way to style empty space that wouldn't trip up the hterm.Screen
2376 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002377 *
2378 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2379 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2380 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002381 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002382 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002383 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002384hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002385 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002386 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002387 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002388
Mike Frysingerdc727792020-04-10 01:41:13 -04002389 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002390 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002391
Shivang Garga72e68c2020-07-18 08:58:32 +05302392 this.findBar.scheduleNotifyChanges(
2393 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
2394
Robert Gindaf2547f12012-10-25 20:36:21 -07002395 if (this.screen_.textAttributes.background ===
2396 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002397 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002398 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002399 this.screen_.cursorPosition.column + count) {
2400 this.screen_.deleteChars(count);
2401 this.clearCursorOverflow();
2402 return;
2403 }
2404 }
2405
Mike Frysingerdc727792020-04-10 01:41:13 -04002406 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002407 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002408 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002409 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002410};
2411
2412/**
2413 * Erase the current line.
2414 *
2415 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002416 */
2417hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002418 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002419 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002420 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002421 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002422};
2423
2424/**
David Benjamina08d78f2012-05-05 00:28:49 -04002425 * Erase all characters from the start of the screen to the current cursor
2426 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002427 *
2428 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002429 */
2430hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002431 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002432
2433 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002434
Mike Frysingerdc727792020-04-10 01:41:13 -04002435 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002436 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002437 this.screen_.clearCursorRow();
2438 }
2439
rginda87b86462011-12-14 13:48:03 -08002440 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002441 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002442};
2443
2444/**
2445 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002446 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002447 *
2448 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002449 */
2450hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002451 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002452
2453 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002454
Mike Frysingerdc727792020-04-10 01:41:13 -04002455 const bottom = this.screenSize.height - 1;
2456 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002457 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002458 this.screen_.clearCursorRow();
2459 }
2460
rginda87b86462011-12-14 13:48:03 -08002461 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002462 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002463};
2464
2465/**
2466 * Fill the terminal with a given character.
2467 *
2468 * This methods does not respect the VT scroll region.
2469 *
2470 * @param {string} ch The character to use for the fill.
2471 */
2472hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002473 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002474
2475 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002476 for (let row = 0; row < this.screenSize.height; row++) {
2477 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002478 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002479 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002480 }
Shivang Garga72e68c2020-07-18 08:58:32 +05302481 this.findBar.scheduleNotifyChanges(this.scrollbackRows_.length + row);
rginda87b86462011-12-14 13:48:03 -08002482 }
2483
2484 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002485};
2486
2487/**
rginda9ea433c2012-03-16 11:57:00 -07002488 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002489 *
rginda9ea433c2012-03-16 11:57:00 -07002490 * This does not respect the scroll region.
2491 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002492 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002493 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002494 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002495hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2496 if (!screen) {
2497 screen = this.screen_;
2498 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002499 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002500
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002501 this.accessibilityReader_.clear();
2502
rginda11057d52012-04-25 12:29:56 -07002503 if (bottom == 0) {
2504 // Empty screen, nothing to do.
2505 return;
2506 }
2507
Mike Frysingerdc727792020-04-10 01:41:13 -04002508 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002509 screen.setCursorPosition(i, 0);
2510 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002511 }
2512
rginda9ea433c2012-03-16 11:57:00 -07002513 screen.setCursorPosition(0, 0);
2514};
2515
2516/**
2517 * Erase the entire display without changing the cursor position.
2518 *
2519 * The cursor position is unchanged. This does not respect the scroll
2520 * region.
2521 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002522 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002523 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002524 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002525hterm.Terminal.prototype.clear = function(screen = undefined) {
2526 if (!screen) {
2527 screen = this.screen_;
2528 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002529 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002530 this.clearHome(screen);
2531 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002532};
2533
2534/**
2535 * VT command to insert lines at the current cursor row.
2536 *
2537 * This respects the current scroll region. Rows pushed off the bottom are
2538 * lost (they won't show up in the scrollback buffer).
2539 *
Joel Hockey0f933582019-08-27 18:01:51 -07002540 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002541 */
2542hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002543 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002544
Mike Frysingerdc727792020-04-10 01:41:13 -04002545 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002546 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002547
Robert Ginda579186b2012-09-26 11:40:04 -07002548 // The moveCount is the number of rows we need to relocate to make room for
2549 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002550 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002551 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002552 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002553 }
rginda8ba33642011-12-14 12:31:31 -08002554
Mike Frysingerdc727792020-04-10 01:41:13 -04002555 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002556 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002557 this.screen_.clearCursorRow();
2558 }
rginda8ba33642011-12-14 12:31:31 -08002559};
2560
2561/**
2562 * VT command to delete lines at the current cursor row.
2563 *
2564 * New rows are added to the bottom of scroll region to take their place. New
2565 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002566 *
2567 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002568 */
2569hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002570 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002571
Mike Frysingerdc727792020-04-10 01:41:13 -04002572 const top = cursor.row;
2573 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002574
Mike Frysingerdc727792020-04-10 01:41:13 -04002575 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002576 count = Math.min(count, maxCount);
2577
Mike Frysingerdc727792020-04-10 01:41:13 -04002578 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002579 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002580 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002581 }
rginda8ba33642011-12-14 12:31:31 -08002582
Mike Frysingerdc727792020-04-10 01:41:13 -04002583 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002584 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002585 this.screen_.clearCursorRow();
2586 }
2587
rginda87b86462011-12-14 13:48:03 -08002588 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002589 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002590};
2591
2592/**
2593 * Inserts the given number of spaces at the current cursor position.
2594 *
rginda87b86462011-12-14 13:48:03 -08002595 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002596 *
2597 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002598 */
2599hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002600 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002601
Mike Frysinger73e56462019-07-17 00:23:46 -05002602 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002603 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002604 this.screen_.maybeClipCurrentRow();
Shivang Garga72e68c2020-07-18 08:58:32 +05302605 this.findBar.scheduleNotifyChanges(
2606 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002607
2608 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002609 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002610};
2611
2612/**
2613 * Forward-delete the specified number of characters starting at the cursor
2614 * position.
2615 *
Joel Hockey0f933582019-08-27 18:01:51 -07002616 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002617 */
2618hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002619 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002620 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002621 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002622 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002623 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002624 this.restoreCursor(cursor);
2625 }
2626
Shivang Garga72e68c2020-07-18 08:58:32 +05302627 this.findBar.scheduleNotifyChanges(
2628 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
David Benjamin54e8bf62012-06-01 22:31:40 -04002629 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002630};
2631
2632/**
2633 * Shift rows in the scroll region upwards by a given number of lines.
2634 *
2635 * New rows are inserted at the bottom of the scroll region to fill the
2636 * vacated rows. The new rows not filled out with the current text attributes.
2637 *
2638 * This function does not affect the scrollback rows at all. Rows shifted
2639 * off the top are lost.
2640 *
rginda87b86462011-12-14 13:48:03 -08002641 * The cursor position is not altered.
2642 *
Joel Hockey0f933582019-08-27 18:01:51 -07002643 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002644 */
2645hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002646 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002647
rginda87b86462011-12-14 13:48:03 -08002648 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002649 this.deleteLines(count);
2650
rginda87b86462011-12-14 13:48:03 -08002651 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002652};
2653
2654/**
2655 * Shift rows below the cursor down by a given number of lines.
2656 *
2657 * This function respects the current scroll region.
2658 *
2659 * New rows are inserted at the top of the scroll region to fill the
2660 * vacated rows. The new rows not filled out with the current text attributes.
2661 *
2662 * This function does not affect the scrollback rows at all. Rows shifted
2663 * off the bottom are lost.
2664 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002665 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002666 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002667hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002668 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002669
rginda87b86462011-12-14 13:48:03 -08002670 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002671 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002672
rginda87b86462011-12-14 13:48:03 -08002673 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002674};
2675
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002676/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002677 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002678 *
2679 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002680 * cause Assitive Technology to announce the output of the terminal. It also
2681 * enables other features that aid assistive technology. All the features gated
2682 * behind this flag have a performance impact on the terminal which is why they
2683 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002684 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002685 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002686 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002687hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002688 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002689};
rginda87b86462011-12-14 13:48:03 -08002690
rginda8ba33642011-12-14 12:31:31 -08002691/**
2692 * Set the cursor position.
2693 *
2694 * The cursor row is relative to the scroll region if the terminal has
2695 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2696 *
Joel Hockey0f933582019-08-27 18:01:51 -07002697 * @param {number} row The new zero-based cursor row.
2698 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002699 */
2700hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2701 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002702 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002703 } else {
rginda87b86462011-12-14 13:48:03 -08002704 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002705 }
rginda87b86462011-12-14 13:48:03 -08002706};
rginda8ba33642011-12-14 12:31:31 -08002707
Evan Jones2600d4f2016-12-06 09:29:36 -05002708/**
2709 * Move the cursor relative to its current position.
2710 *
2711 * @param {number} row
2712 * @param {number} column
2713 */
rginda87b86462011-12-14 13:48:03 -08002714hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002715 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002716 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2717 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002718 this.screen_.setCursorPosition(row, column);
2719};
2720
Evan Jones2600d4f2016-12-06 09:29:36 -05002721/**
2722 * Move the cursor to the specified position.
2723 *
2724 * @param {number} row
2725 * @param {number} column
2726 */
rginda87b86462011-12-14 13:48:03 -08002727hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002728 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2729 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002730 this.screen_.setCursorPosition(row, column);
2731};
2732
2733/**
2734 * Set the cursor column.
2735 *
Joel Hockey0f933582019-08-27 18:01:51 -07002736 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002737 */
2738hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002739 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002740};
2741
2742/**
2743 * Return the cursor column.
2744 *
Joel Hockey0f933582019-08-27 18:01:51 -07002745 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002746 */
2747hterm.Terminal.prototype.getCursorColumn = function() {
2748 return this.screen_.cursorPosition.column;
2749};
2750
2751/**
2752 * Set the cursor row.
2753 *
2754 * The cursor row is relative to the scroll region if the terminal has
2755 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2756 *
Joel Hockey0f933582019-08-27 18:01:51 -07002757 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002758 */
rginda87b86462011-12-14 13:48:03 -08002759hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2760 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002761};
2762
2763/**
2764 * Return the cursor row.
2765 *
Joel Hockey0f933582019-08-27 18:01:51 -07002766 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002767 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002768hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002769 return this.screen_.cursorPosition.row;
2770};
2771
2772/**
2773 * Request that the ScrollPort redraw itself soon.
2774 *
2775 * The redraw will happen asynchronously, soon after the call stack winds down.
2776 * Multiple calls will be coalesced into a single redraw.
2777 */
2778hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002779 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002780 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002781 }
rginda8ba33642011-12-14 12:31:31 -08002782
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002783 this.timeouts_.redraw = setTimeout(() => {
2784 delete this.timeouts_.redraw;
2785 this.scrollPort_.redraw_();
2786 });
rginda8ba33642011-12-14 12:31:31 -08002787};
2788
2789/**
2790 * Request that the ScrollPort be scrolled to the bottom.
2791 *
2792 * The scroll will happen asynchronously, soon after the call stack winds down.
2793 * Multiple calls will be coalesced into a single scroll.
2794 *
2795 * This affects the scrollbar position of the ScrollPort, and has nothing to
2796 * do with the VT scroll commands.
2797 */
2798hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002799 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002800 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002801 }
rginda8ba33642011-12-14 12:31:31 -08002802
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002803 this.timeouts_.scrollDown = setTimeout(() => {
2804 delete this.timeouts_.scrollDown;
2805 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2806 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002807};
2808
2809/**
2810 * Move the cursor up a specified number of rows.
2811 *
Joel Hockey0f933582019-08-27 18:01:51 -07002812 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002813 */
2814hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002815 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002816};
2817
2818/**
2819 * Move the cursor down a specified number of rows.
2820 *
Joel Hockey0f933582019-08-27 18:01:51 -07002821 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002822 */
2823hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002824 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002825 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2826 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2827 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002828
Mike Frysingerdc727792020-04-10 01:41:13 -04002829 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2830 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002831 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002832};
2833
2834/**
2835 * Move the cursor left a specified number of columns.
2836 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002837 * If reverse wraparound mode is enabled and the previous row wrapped into
2838 * the current row then we back up through the wraparound as well.
2839 *
Joel Hockey0f933582019-08-27 18:01:51 -07002840 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002841 */
2842hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002843 count = count || 1;
2844
Mike Frysingerbdb34802020-04-07 03:47:32 -04002845 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002846 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002847 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002848
Mike Frysingerdc727792020-04-10 01:41:13 -04002849 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002850 if (this.options_.reverseWraparound) {
2851 if (this.screen_.cursorPosition.overflow) {
2852 // If this cursor is in the right margin, consume one count to get it
2853 // back to the last column. This only applies when we're in reverse
2854 // wraparound mode.
2855 count--;
2856 this.clearCursorOverflow();
2857
Mike Frysingerbdb34802020-04-07 03:47:32 -04002858 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002859 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002860 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002861 }
2862
Mike Frysingerdc727792020-04-10 01:41:13 -04002863 let newRow = this.screen_.cursorPosition.row;
2864 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002865 if (newColumn < 0) {
2866 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2867 if (newRow < 0) {
2868 // xterm also wraps from row 0 to the last row.
2869 newRow = this.screenSize.height + newRow % this.screenSize.height;
2870 }
2871 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2872 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002873
Robert Gindabfb32622014-07-17 13:20:27 -07002874 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2875
2876 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002877 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002878 this.setCursorColumn(newColumn);
2879 }
rginda8ba33642011-12-14 12:31:31 -08002880};
2881
2882/**
2883 * Move the cursor right a specified number of columns.
2884 *
Joel Hockey0f933582019-08-27 18:01:51 -07002885 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002886 */
2887hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002888 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002889
Mike Frysingerbdb34802020-04-07 03:47:32 -04002890 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002891 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002892 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002893
Mike Frysingerdc727792020-04-10 01:41:13 -04002894 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2895 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002896 this.setCursorColumn(column);
2897};
2898
2899/**
2900 * Reverse the foreground and background colors of the terminal.
2901 *
2902 * This only affects text that was drawn with no attributes.
2903 *
2904 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2905 * been drawn with attributes that happen to coincide with the default
2906 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002907 *
2908 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002909 */
2910hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002911 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002912 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002913 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2914 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002915 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002916 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2917 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002918 }
2919};
2920
2921/**
rginda87b86462011-12-14 13:48:03 -08002922 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002923 *
2924 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002925 */
2926hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002927 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002928
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002929 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002930
Michael Kelly485ecd12014-06-09 11:41:56 -04002931 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002932 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002933 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002934 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002935
Robert Ginda92e18102013-03-14 13:56:37 -07002936 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002937 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002938 this.bellSequelchTimeout_ = setTimeout(() => {
2939 this.bellSquelchTimeout_ = null;
2940 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002941 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002942 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002943 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002944
2945 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002946 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002947 this.bellNotificationList_.push(n);
2948 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002949 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002950 }
rginda87b86462011-12-14 13:48:03 -08002951};
2952
2953/**
rginda8ba33642011-12-14 12:31:31 -08002954 * Set the origin mode bit.
2955 *
2956 * If origin mode is on, certain VT cursor and scrolling commands measure their
2957 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2958 * to the top of the addressable screen.
2959 *
2960 * Defaults to off.
2961 *
2962 * @param {boolean} state True to set origin mode, false to unset.
2963 */
2964hterm.Terminal.prototype.setOriginMode = function(state) {
2965 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002966 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002967};
2968
2969/**
2970 * Set the insert mode bit.
2971 *
2972 * If insert mode is on, existing text beyond the cursor position will be
2973 * shifted right to make room for new text. Otherwise, new text overwrites
2974 * any existing text.
2975 *
2976 * Defaults to off.
2977 *
2978 * @param {boolean} state True to set insert mode, false to unset.
2979 */
2980hterm.Terminal.prototype.setInsertMode = function(state) {
2981 this.options_.insertMode = state;
2982};
2983
2984/**
rginda87b86462011-12-14 13:48:03 -08002985 * Set the auto carriage return bit.
2986 *
2987 * If auto carriage return is on then a formfeed character is interpreted
2988 * as a newline, otherwise it's the same as a linefeed. The difference boils
2989 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002990 *
2991 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002992 */
2993hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2994 this.options_.autoCarriageReturn = state;
2995};
2996
2997/**
rginda8ba33642011-12-14 12:31:31 -08002998 * Set the wraparound mode bit.
2999 *
3000 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
3001 * to the start of the following row. Otherwise, the cursor is clamped to the
3002 * end of the screen and attempts to write past it are ignored.
3003 *
3004 * Defaults to on.
3005 *
3006 * @param {boolean} state True to set wraparound mode, false to unset.
3007 */
3008hterm.Terminal.prototype.setWraparound = function(state) {
3009 this.options_.wraparound = state;
3010};
3011
3012/**
3013 * Set the reverse-wraparound mode bit.
3014 *
3015 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
3016 * to the end of the previous row. Otherwise, the cursor is clamped to column
3017 * 0.
3018 *
3019 * Defaults to off.
3020 *
3021 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
3022 */
3023hterm.Terminal.prototype.setReverseWraparound = function(state) {
3024 this.options_.reverseWraparound = state;
3025};
3026
3027/**
3028 * Selects between the primary and alternate screens.
3029 *
3030 * If alternate mode is on, the alternate screen is active. Otherwise the
3031 * primary screen is active.
3032 *
3033 * Swapping screens has no effect on the scrollback buffer.
3034 *
3035 * Each screen maintains its own cursor position.
3036 *
3037 * Defaults to off.
3038 *
3039 * @param {boolean} state True to set alternate mode, false to unset.
3040 */
3041hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07003042 if (state == (this.screen_ == this.alternateScreen_)) {
3043 return;
3044 }
3045 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3046 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08003047 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
3048
Joel Hockey42dba8f2020-03-26 16:21:11 -07003049 // Swap color overrides.
3050 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3051 oldOverrides.forEach((c, i) => {
3052 if (!newOverrides.hasOwnProperty(i)) {
3053 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
3054 }
3055 });
3056 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
3057
rginda35c456b2012-02-09 17:29:05 -08003058 if (this.screen_.rowsArray.length &&
3059 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3060 // If the screen changed sizes while we were away, our rowIndexes may
3061 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003062 const offset = this.scrollbackRows_.length;
3063 const ary = this.screen_.rowsArray;
3064 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003065 ary[i].rowIndex = offset + i;
3066 }
3067 }
rginda8ba33642011-12-14 12:31:31 -08003068
rginda35c456b2012-02-09 17:29:05 -08003069 this.realizeWidth_(this.screenSize.width);
3070 this.realizeHeight_(this.screenSize.height);
3071 this.scrollPort_.syncScrollHeight();
3072 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003073
rginda6d397402012-01-17 10:58:29 -08003074 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003075 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003076};
3077
3078/**
3079 * Set the cursor-blink mode bit.
3080 *
3081 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3082 * a visible cursor does not blink.
3083 *
3084 * You should make sure to turn blinking off if you're going to dispose of a
3085 * terminal, otherwise you'll leak a timeout.
3086 *
3087 * Defaults to on.
3088 *
3089 * @param {boolean} state True to set cursor-blink mode, false to unset.
3090 */
3091hterm.Terminal.prototype.setCursorBlink = function(state) {
3092 this.options_.cursorBlink = state;
3093
3094 if (!state && this.timeouts_.cursorBlink) {
3095 clearTimeout(this.timeouts_.cursorBlink);
3096 delete this.timeouts_.cursorBlink;
3097 }
3098
Mike Frysingerbdb34802020-04-07 03:47:32 -04003099 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003100 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003101 }
rginda8ba33642011-12-14 12:31:31 -08003102};
3103
3104/**
3105 * Set the cursor-visible mode bit.
3106 *
3107 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3108 *
3109 * Defaults to on.
3110 *
3111 * @param {boolean} state True to set cursor-visible mode, false to unset.
3112 */
3113hterm.Terminal.prototype.setCursorVisible = function(state) {
3114 this.options_.cursorVisible = state;
3115
3116 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003117 if (this.timeouts_.cursorBlink) {
3118 clearTimeout(this.timeouts_.cursorBlink);
3119 delete this.timeouts_.cursorBlink;
3120 }
rginda87b86462011-12-14 13:48:03 -08003121 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003122 return;
3123 }
3124
rginda87b86462011-12-14 13:48:03 -08003125 this.syncCursorPosition_();
3126
3127 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003128
3129 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003130 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003131 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003132 }
rginda8ba33642011-12-14 12:31:31 -08003133
Robert Gindaea2183e2014-07-17 09:51:51 -07003134 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003135 } else {
3136 if (this.timeouts_.cursorBlink) {
3137 clearTimeout(this.timeouts_.cursorBlink);
3138 delete this.timeouts_.cursorBlink;
3139 }
3140 }
3141};
3142
3143/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003144 * Pause blinking temporarily.
3145 *
3146 * When the cursor moves around, it can be helpful to momentarily pause the
3147 * blinking. This could be when the user is typing in things, or when they're
3148 * moving around with the arrow keys.
3149 */
3150hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3151 if (!this.options_.cursorBlink) {
3152 return;
3153 }
3154
3155 this.cursorBlinkPause_ = true;
3156
3157 // If a timeout is already pending, reset the clock due to the new input.
3158 if (this.timeouts_.cursorBlinkPause) {
3159 clearTimeout(this.timeouts_.cursorBlinkPause);
3160 }
3161 // After 500ms, resume blinking. That seems like a good balance between user
3162 // input timings & responsiveness to resume.
3163 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3164 delete this.timeouts_.cursorBlinkPause;
3165 this.cursorBlinkPause_ = false;
3166 }, 500);
3167};
3168
3169/**
rginda87b86462011-12-14 13:48:03 -08003170 * Synchronizes the visible cursor and document selection with the current
3171 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003172 *
3173 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003174 */
3175hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003176 const topRowIndex = this.scrollPort_.getTopRowIndex();
3177 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3178 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003179 this.screen_.cursorPosition.row;
3180
Raymes Khoury15697f42018-07-17 11:37:18 +10003181 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003182 if (this.accessibilityReader_.accessibilityEnabled) {
3183 // Report the new position of the cursor for accessibility purposes.
3184 const cursorColumnIndex = this.screen_.cursorPosition.column;
3185 const cursorLineText =
3186 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003187 // This will force the selection to be sync'd to the cursor position if the
3188 // user has pressed a key. Generally we would only sync the cursor position
3189 // when selection is collapsed so that if the user has selected something
3190 // we don't clear the selection by moving the selection. However when a
3191 // screen reader is used, it's intuitive for entering a key to move the
3192 // selection to the cursor.
3193 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003194 this.accessibilityReader_.afterCursorChange(
3195 cursorLineText, cursorRowIndex, cursorColumnIndex);
3196 }
3197
rginda8ba33642011-12-14 12:31:31 -08003198 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003199 // Cursor is scrolled off screen, hide it.
3200 this.cursorOffScreen_ = true;
3201 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003202 return false;
rginda8ba33642011-12-14 12:31:31 -08003203 }
3204
Joel Hockey3babf302020-04-22 15:00:06 -07003205 if (this.cursorNode_.style.display == 'none') {
3206 // Re-display the terminal cursor if it was hidden.
3207 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003208 this.cursorNode_.style.display = '';
3209 }
3210
Mike Frysinger44c32202017-08-05 01:13:09 -04003211 // Position the cursor using CSS variable math. If we do the math in JS,
3212 // the float math will end up being more precise than the CSS which will
3213 // cause the cursor tracking to be off.
3214 this.setCssVar(
3215 'cursor-offset-row',
3216 `${cursorRowIndex - topRowIndex} + ` +
3217 `${this.scrollPort_.visibleRowTopMargin}px`);
3218 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003219
3220 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003221 '(' + this.screen_.cursorPosition.column +
3222 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003223 ')');
3224
Joel Hockey72f7fd62020-07-30 19:29:16 -07003225 // Update the caret for a11y purposes unless FindBar has focus which it should
3226 // keep.
3227 if (!this.findBar.hasFocus) {
3228 const selection = this.document_.getSelection();
3229 if (selection && (selection.isCollapsed || forceSyncSelection)) {
3230 this.screen_.syncSelectionCaret(selection);
3231 }
Raymes Khoury15697f42018-07-17 11:37:18 +10003232 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003233 return true;
rginda8ba33642011-12-14 12:31:31 -08003234};
3235
Robert Gindafb1be6a2013-12-11 11:56:22 -08003236/**
3237 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3238 * and character cell dimensions.
3239 */
Robert Ginda830583c2013-08-07 13:20:46 -07003240hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003241 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003242
3243 if (this.cursorNode_.getAttribute('focus') == 'false') {
3244 // Always show a block cursor when unfocused.
3245 shape = hterm.Terminal.cursorShape.BLOCK;
3246 }
3247
Mike Frysingerdc727792020-04-10 01:41:13 -04003248 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003249
3250 switch (shape) {
3251 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003252 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003253 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003254 style.borderLeftStyle = 'solid';
3255 break;
3256
3257 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003258 style.backgroundColor = 'transparent';
3259 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003260 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003261 break;
3262
3263 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003264 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003265 style.borderBottomStyle = '';
3266 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003267 break;
3268 }
3269};
3270
rginda8ba33642011-12-14 12:31:31 -08003271/**
3272 * Synchronizes the visible cursor with the current cursor coordinates.
3273 *
3274 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003275 * Multiple calls will be coalesced into a single sync. This should be called
3276 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003277 */
3278hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003279 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003280 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003281 }
rginda8ba33642011-12-14 12:31:31 -08003282
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003283 if (this.accessibilityReader_.accessibilityEnabled) {
3284 // Report the previous position of the cursor for accessibility purposes.
3285 const cursorRowIndex = this.scrollbackRows_.length +
3286 this.screen_.cursorPosition.row;
3287 const cursorColumnIndex = this.screen_.cursorPosition.column;
3288 const cursorLineText =
3289 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3290 this.accessibilityReader_.beforeCursorChange(
3291 cursorLineText, cursorRowIndex, cursorColumnIndex);
3292 }
3293
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003294 this.timeouts_.syncCursor = setTimeout(() => {
3295 this.syncCursorPosition_();
3296 delete this.timeouts_.syncCursor;
3297 });
rginda87b86462011-12-14 13:48:03 -08003298};
3299
rgindacc2996c2012-02-24 14:59:31 -08003300/**
rgindaf522ce02012-04-17 17:49:17 -07003301 * Show or hide the zoom warning.
3302 *
3303 * The zoom warning is a message warning the user that their browser zoom must
3304 * be set to 100% in order for hterm to function properly.
3305 *
3306 * @param {boolean} state True to show the message, false to hide it.
3307 */
3308hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3309 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003310 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003311 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003312 }
rgindaf522ce02012-04-17 17:49:17 -07003313
3314 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003315 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003316 this.zoomWarningNode_.style.cssText = (
3317 'color: black;' +
3318 'background-color: #ff2222;' +
3319 'font-size: large;' +
3320 'border-radius: 8px;' +
3321 'opacity: 0.75;' +
3322 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3323 'top: 0.5em;' +
3324 'right: 1.2em;' +
3325 'position: absolute;' +
3326 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003327 '-webkit-user-select: none;' +
3328 '-moz-text-size-adjust: none;' +
3329 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003330
3331 this.zoomWarningNode_.addEventListener('click', function(e) {
3332 this.parentNode.removeChild(this);
3333 });
rgindaf522ce02012-04-17 17:49:17 -07003334 }
3335
Mike Frysingerb7289952019-03-23 16:05:38 -07003336 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003337 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003338 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003339
rgindaf522ce02012-04-17 17:49:17 -07003340 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3341
3342 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003343 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003344 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003345 }
rgindaf522ce02012-04-17 17:49:17 -07003346 } else if (this.zoomWarningNode_.parentNode) {
3347 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3348 }
3349};
3350
3351/**
rgindacc2996c2012-02-24 14:59:31 -08003352 * Show the terminal overlay for a given amount of time.
3353 *
Jason Lin3d825782020-05-12 11:02:48 +10003354 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003355 *
3356 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003357 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003358 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3359 * stay up forever (or until the next overlay).
3360 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003361hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003362 this.showOverlayWithNode(new Text(msg), timeout);
3363};
3364
3365/**
3366 * Show the terminal overlay for a given amount of time.
3367 *
3368 * The terminal overlay appears in inverse video, centered over the terminal.
3369 *
3370 * @param {!Node} node The node to display in the overlay.
3371 * @param {?number=} timeout The amount of time to wait before fading out
3372 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3373 * stay up forever (or until the next overlay).
3374 */
3375hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003376 if (!this.ready_ || !this.div_) {
3377 return;
3378 }
rgindaf0090c92012-02-10 14:58:52 -08003379
Joel Hockeyedac0e72020-05-14 20:16:20 -07003380 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003381 this.overlayNode_ = this.document_.createElement('div');
3382 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003383 'color: rgb(var(--hterm-background-color));' +
3384 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003385 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003386 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003387 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003388 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003389 'position: absolute;' +
3390 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003391 '-webkit-transition: opacity 180ms ease-in;' +
3392 '-moz-user-select: none;' +
3393 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003394
3395 this.overlayNode_.addEventListener('mousedown', function(e) {
3396 e.preventDefault();
3397 e.stopPropagation();
3398 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003399 }
3400
Jason Lin34567412020-05-14 10:32:09 +10003401 this.overlayNode_.textContent = ''; // Remove all children first.
3402 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003403
Mike Frysingerbdb34802020-04-07 03:47:32 -04003404 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003405 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003406 }
rgindaf0090c92012-02-10 14:58:52 -08003407
Mike Frysingerdc727792020-04-10 01:41:13 -04003408 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3409 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003410
Robert Ginda8a59f762014-07-23 11:29:55 -07003411 this.overlayNode_.style.top =
3412 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003413 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003414 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003415
Mike Frysingerbdb34802020-04-07 03:47:32 -04003416 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003417 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003418 }
rgindaf0090c92012-02-10 14:58:52 -08003419
Jason Lin34567412020-05-14 10:32:09 +10003420 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003421
Mike Frysingerec4225d2020-04-07 05:00:01 -04003422 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003423 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003424 }
rgindacc2996c2012-02-24 14:59:31 -08003425
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003426 this.overlayTimeout_ = setTimeout(() => {
3427 this.overlayNode_.style.opacity = '0';
3428 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003429 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003430};
3431
3432/**
3433 * Hide the terminal overlay immediately.
3434 *
3435 * Useful when we show an overlay for an event with an unknown end time.
3436 */
3437hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003438 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003439 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003440 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003441 this.overlayTimeout_ = null;
3442
Mike Frysingerbdb34802020-04-07 03:47:32 -04003443 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003444 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003445 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003446 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003447};
3448
rginda4bba5e12012-06-20 16:15:30 -07003449/**
3450 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003451 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003452 * Note: In Chrome, this should work unless the user has rejected the permission
3453 * request. In Firefox extension environment, you'll need the "clipboardRead"
3454 * permission. In other environments, this might always fail as the browser
3455 * frequently blocks access for security reasons.
3456 *
3457 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3458 * value is always null. Otherwise, this function uses legacy pasting and
3459 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003460 */
3461hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003462 if (!this.alwaysUseLegacyPasting &&
3463 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003464 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3465 return null;
3466 } else {
3467 // Legacy pasting.
3468 try {
3469 return this.document_.execCommand('paste');
3470 } catch (firefoxException) {
3471 // Ignore this. FF 40 and older would incorrectly throw an exception if
3472 // there was an error instead of returning false.
3473 return false;
3474 }
3475 }
rginda4bba5e12012-06-20 16:15:30 -07003476};
3477
3478/**
3479 * Copy a string to the system clipboard.
3480 *
3481 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003482 *
3483 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003484 */
3485hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003486 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003487 if (!this.clipboardNotice_) {
3488 this.clipboardNotice_ = this.document_.createElement('div');
3489 this.clipboardNotice_.style.textAlign = 'center';
3490 const copyImage = lib.resource.getData('hterm/images/copy');
3491 this.clipboardNotice_.innerHTML =
3492 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3493 }
3494 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003495 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003496
Mike Frysinger96eacae2019-01-02 18:13:56 -05003497 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003498};
3499
Evan Jones2600d4f2016-12-06 09:29:36 -05003500/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003501 * Display an image.
3502 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003503 * Either URI or buffer or blob fields must be specified.
3504 *
Joel Hockey0f933582019-08-27 18:01:51 -07003505 * @param {{
3506 * name: (string|undefined),
3507 * size: (string|number|undefined),
3508 * preserveAspectRation: (boolean|undefined),
3509 * inline: (boolean|undefined),
3510 * width: (string|number|undefined),
3511 * height: (string|number|undefined),
3512 * align: (string|undefined),
3513 * url: (string|undefined),
3514 * buffer: (!ArrayBuffer|undefined),
3515 * blob: (!Blob|undefined),
3516 * type: (string|undefined),
3517 * }} options The image to display.
3518 * name A human readable string for the image
3519 * size The size (in bytes).
3520 * preserveAspectRatio Whether to preserve aspect.
3521 * inline Whether to display the image inline.
3522 * width The width of the image.
3523 * height The height of the image.
3524 * align Direction to align the image.
3525 * uri The source URI for the image.
3526 * buffer The ArrayBuffer image data.
3527 * blob The Blob image data.
3528 * type The MIME type of the image data.
3529 * @param {function()=} onLoad Callback when loading finishes.
3530 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003531 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003532hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003533 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003534 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003535 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003536 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003537 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003538
3539 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003540 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003541 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003542 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003543
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003544 // See if the mime type is available. If not, guess from the filename.
3545 // We don't list all possible mime types because the browser can usually
3546 // guess it correctly. So list the ones that need a bit more help.
3547 if (!options.type) {
3548 const ary = options.name.split('.');
3549 const ext = ary[ary.length - 1].trim();
3550 switch (ext) {
3551 case 'svg':
3552 case 'svgz':
3553 options.type = 'image/svg+xml';
3554 break;
3555 }
3556 }
3557
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003558 // Has the user approved image display yet?
3559 if (this.allowImagesInline !== true) {
3560 this.newLine();
3561 const row = this.getRowNode(this.scrollbackRows_.length +
3562 this.getCursorRow() - 1);
3563
3564 if (this.allowImagesInline === false) {
3565 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3566 'Inline Images Disabled');
3567 return;
3568 }
3569
3570 // Show a prompt.
3571 let button;
3572 const span = this.document_.createElement('span');
3573 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3574 span.style.fontWeight = 'bold';
3575 span.style.borderWidth = '1px';
3576 span.style.borderStyle = 'dashed';
3577 button = this.document_.createElement('span');
3578 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3579 button.style.marginLeft = '1em';
3580 button.style.borderWidth = '1px';
3581 button.style.borderStyle = 'solid';
3582 button.addEventListener('click', () => {
3583 this.prefs_.set('allow-images-inline', false);
3584 });
3585 span.appendChild(button);
3586 button = this.document_.createElement('span');
3587 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3588 'allow this session');
3589 button.style.marginLeft = '1em';
3590 button.style.borderWidth = '1px';
3591 button.style.borderStyle = 'solid';
3592 button.addEventListener('click', () => {
3593 this.allowImagesInline = true;
3594 });
3595 span.appendChild(button);
3596 button = this.document_.createElement('span');
3597 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3598 button.style.marginLeft = '1em';
3599 button.style.borderWidth = '1px';
3600 button.style.borderStyle = 'solid';
3601 button.addEventListener('click', () => {
3602 this.prefs_.set('allow-images-inline', true);
3603 });
3604 span.appendChild(button);
3605
3606 row.appendChild(span);
3607 return;
3608 }
3609
3610 // See if we should show this object directly, or download it.
3611 if (options.inline) {
3612 const io = this.io.push();
3613 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003614 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003615
3616 // While we're loading the image, eat all the user's input.
3617 io.onVTKeystroke = io.sendString = () => {};
3618
3619 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003620 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003621 if (options.uri !== undefined) {
3622 img.src = options.uri;
3623 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003624 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003625 img.src = URL.createObjectURL(blob);
3626 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003627 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003628 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003629 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003630 img.title = img.alt = options.name;
3631
3632 // Attach the image to the page to let it load/render. It won't stay here.
3633 // This is needed so it's visible and the DOM can calculate the height. If
3634 // the image is hidden or not in the DOM, the height is always 0.
3635 this.document_.body.appendChild(img);
3636
3637 // Wait for the image to finish loading before we try moving it to the
3638 // right place in the terminal.
3639 img.onload = () => {
3640 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003641 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003642 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003643 img.style.maxWidth = `${screenSize.width}px`;
3644 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003645
3646 // Parse a width/height specification.
3647 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003648 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003649 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003650 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003651
3652 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3653 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003654 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003655 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003656 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003657 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003658 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003659 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003660 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003661 }
3662
3663 return '';
3664 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003665 img.style.width = parseDim(
3666 options.width, screenSize.width, '--hterm-charsize-width');
3667 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003668 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003669
3670 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003671 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003672 const padRows = Math.ceil(img.clientHeight /
3673 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003674 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003675 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003676 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003677
3678 // Update the max height in case the user shrinks the character size.
3679 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3680
3681 // Move the image to the last row. This way when we scroll up, it doesn't
3682 // disappear when the first row gets clipped. It will disappear when we
3683 // scroll down and the last row is clipped ...
3684 this.document_.body.removeChild(img);
3685 // Create a wrapper node so we can do an absolute in a relative position.
3686 // This helps with rounding errors between JS & CSS counts.
3687 const div = this.document_.createElement('div');
3688 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003689 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003690 img.style.position = 'absolute';
3691 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3692 div.appendChild(img);
3693 const row = this.getRowNode(this.scrollbackRows_.length +
3694 this.getCursorRow() - 1);
3695 row.appendChild(div);
3696
Mike Frysinger2558ed52019-01-14 01:03:41 -05003697 // Now that the image has been read, we can revoke the source.
3698 if (options.uri === undefined) {
3699 URL.revokeObjectURL(img.src);
3700 }
3701
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003702 io.hideOverlay();
3703 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003704
Mike Frysingerbdb34802020-04-07 03:47:32 -04003705 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003706 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003707 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003708 };
3709
3710 // If we got a malformed image, give up.
3711 img.onerror = (e) => {
3712 this.document_.body.removeChild(img);
3713 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003714 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003715 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003716
Mike Frysingerbdb34802020-04-07 03:47:32 -04003717 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003718 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003719 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003720 };
3721 } else {
3722 // We can't use chrome.downloads.download as that requires "downloads"
3723 // permissions, and that works only in extensions, not apps.
3724 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003725 if (options.uri !== undefined) {
3726 a.href = options.uri;
3727 } else if (options.buffer !== undefined) {
3728 const blob = new Blob([options.buffer]);
3729 a.href = URL.createObjectURL(blob);
3730 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003731 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003732 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003733 a.download = options.name;
3734 this.document_.body.appendChild(a);
3735 a.click();
3736 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003737 if (options.uri === undefined) {
3738 URL.revokeObjectURL(a.href);
3739 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003740 }
3741};
3742
3743/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003744 * Returns the selected text, or null if no text is selected.
3745 *
3746 * @return {string|null}
3747 */
rgindaa09e7332012-08-17 12:49:51 -07003748hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003749 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003750 selection.sync();
3751
Mike Frysingerbdb34802020-04-07 03:47:32 -04003752 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003753 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003754 }
rgindaa09e7332012-08-17 12:49:51 -07003755
rgindaa09e7332012-08-17 12:49:51 -07003756 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003757 let startOffset = selection.startOffset;
3758 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003759
Raymes Khoury334625a2018-06-25 10:29:40 +10003760 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003761 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003762 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003763 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003764
Robert Gindafdbb3f22012-09-06 20:23:06 -07003765 if (node.nodeName != 'X-ROW') {
3766 // If the selection doesn't start on an x-row node, then it must be
3767 // somewhere inside the x-row. Add any characters from previous siblings
3768 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003769
3770 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3771 // If node is the text node in a styled span, move up to the span node.
3772 node = node.parentNode;
3773 }
3774
Robert Gindafdbb3f22012-09-06 20:23:06 -07003775 while (node.previousSibling) {
3776 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003777 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003778 }
rgindaa09e7332012-08-17 12:49:51 -07003779 }
3780
3781 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003782 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003783 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003784 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003785
Robert Gindafdbb3f22012-09-06 20:23:06 -07003786 if (node.nodeName != 'X-ROW') {
3787 // If the selection doesn't end on an x-row node, then it must be
3788 // somewhere inside the x-row. Add any characters from following siblings
3789 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003790
3791 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3792 // If node is the text node in a styled span, move up to the span node.
3793 node = node.parentNode;
3794 }
3795
Robert Gindafdbb3f22012-09-06 20:23:06 -07003796 while (node.nextSibling) {
3797 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003798 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003799 }
rgindaa09e7332012-08-17 12:49:51 -07003800 }
3801
Mike Frysingerdc727792020-04-10 01:41:13 -04003802 const rv = this.getRowsText(selection.startRow.rowIndex,
3803 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003804 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003805};
3806
rginda4bba5e12012-06-20 16:15:30 -07003807/**
3808 * Copy the current selection to the system clipboard, then clear it after a
3809 * short delay.
3810 */
3811hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003812 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003813 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003814 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003815 }
rginda4bba5e12012-06-20 16:15:30 -07003816};
3817
Joel Hockey0f933582019-08-27 18:01:51 -07003818/**
3819 * Show overlay with current terminal size.
3820 */
rgindaf0090c92012-02-10 14:58:52 -08003821hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003822 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003823 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003824 }
rgindaf0090c92012-02-10 14:58:52 -08003825};
3826
rginda87b86462011-12-14 13:48:03 -08003827/**
3828 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3829 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003830 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003831 */
3832hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003833 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003834 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003835 }
rginda87b86462011-12-14 13:48:03 -08003836
Mike Frysinger225c99d2019-10-20 14:02:37 -06003837 this.pauseCursorBlink_();
3838
Mike Frysinger79669762018-12-30 20:51:10 -05003839 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003840};
3841
3842/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003843 * Open the selected url.
3844 */
3845hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003846 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003847
3848 // If there is no selection, try and expand wherever they clicked.
3849 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003850 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003851 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003852
3853 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003854 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003855 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003856 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003857 }
3858
3859 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003860 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003861 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003862 }
Mike Frysinger43472622017-06-26 18:11:07 -04003863
3864 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003865 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003866 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3867 // We have to whitelist a few protocols that lack authorities and thus
3868 // never use the //. Like mailto.
3869 switch (str.split(':', 1)[0]) {
3870 case 'mailto':
3871 break;
3872 default:
3873 str = 'http://' + str;
3874 break;
3875 }
3876 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003877
Mike Frysinger720fa832017-10-23 01:15:52 -04003878 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003879};
Mike Frysinger70b94692017-01-26 18:57:50 -10003880
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003881/**
3882 * Manage the automatic mouse hiding behavior while typing.
3883 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003884 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003885 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003886hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003887 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3888 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003889 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003890 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003891 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003892
3893 this.mouseHideWhileTyping_ = !!v;
3894};
3895
3896/**
3897 * Handler for monitoring user keyboard activity.
3898 *
3899 * This isn't for processing the keystrokes directly, but for updating any
3900 * state that might toggle based on the user using the keyboard at all.
3901 *
Joel Hockey0f933582019-08-27 18:01:51 -07003902 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003903 */
3904hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3905 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003906 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003907 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003908 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003909};
Mike Frysinger70b94692017-01-26 18:57:50 -10003910
3911/**
rgindad5613292012-06-19 15:40:37 -07003912 * Add the terminalRow and terminalColumn properties to mouse events and
3913 * then forward on to onMouse().
3914 *
3915 * The terminalRow and terminalColumn properties contain the (row, column)
3916 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003917 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003918 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003919 */
3920hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003921 if (e.processedByTerminalHandler_) {
3922 // We register our event handlers on the document, as well as the cursor
3923 // and the scroll blocker. Mouse events that occur on the cursor or
3924 // scroll blocker will also appear on the document, but we don't want to
3925 // process them twice.
3926 //
3927 // We can't just prevent bubbling because that has other side effects, so
3928 // we decorate the event object with this property instead.
3929 return;
3930 }
3931
Mike Frysinger468966c2018-08-28 13:48:51 -04003932 // Consume navigation events. Button 3 is usually "browser back" and
3933 // button 4 is "browser forward" which we don't want to happen.
3934 if (e.button > 2) {
3935 e.preventDefault();
3936 // We don't return so click events can be passed to the remote below.
3937 }
3938
Mike Frysingerdc727792020-04-10 01:41:13 -04003939 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003940 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3941
rgindafaa74742012-08-21 13:34:03 -07003942 e.processedByTerminalHandler_ = true;
3943
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003944 // Handle auto hiding of mouse cursor while typing.
3945 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3946 // Make sure the mouse cursor is visible.
3947 this.syncMouseStyle();
3948 // This debounce isn't perfect, but should work well enough for such a
3949 // simple implementation. If the user moved the mouse, we enabled this
3950 // debounce, and then moved the mouse just before the timeout, we wouldn't
3951 // debounce that later movement.
3952 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3953 }
3954
Robert Gindaeda48db2014-07-17 09:25:30 -07003955 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003956 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003957 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003958 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003959 this.scrollPort_.characterSize.height) + 1;
3960 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003961 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003962
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003963 // Clamp row and column.
3964 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3965 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3966
3967 // Ignore mousedown in the scrollbar area.
3968 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003969 return;
3970 }
3971
Joel Hockey3babf302020-04-22 15:00:06 -07003972 if (this.options_.cursorVisible && !reportMouseEvents &&
3973 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003974 // If the cursor is visible and we're not sending mouse events to the
3975 // host app, then we want to hide the terminal cursor when the mouse
3976 // cursor is over top. This keeps the terminal cursor from interfering
3977 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003978 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3979 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3980 this.cursorNode_.style.display = 'none';
3981 } else if (this.cursorNode_.style.display == 'none') {
3982 this.cursorNode_.style.display = '';
3983 }
3984 }
rgindad5613292012-06-19 15:40:37 -07003985
Robert Ginda928cf632014-03-05 15:07:41 -08003986 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003987 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003988
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003989 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003990 // If VT mouse reporting is disabled, or has been defeated with
3991 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003992 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003993 this.setSelectionEnabled(true);
3994 } else {
3995 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003996 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003997 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003998 this.setSelectionEnabled(false);
3999 e.preventDefault();
4000 }
4001 }
4002
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004003 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08004004 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004005 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04004006 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05004007 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004008 }
rgindad5613292012-06-19 15:40:37 -07004009 }
4010
Mike Frysingerda2e84f2020-06-01 17:53:21 -04004011 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07004012 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04004013 // Ignore links created using OSC-8 as those will open by themselves, and
4014 // the visible text is most likely not the URI they want anyways.
4015 if (e.target.className === 'uri-node') {
4016 return;
4017 }
4018
Mike Frysinger70b94692017-01-26 18:57:50 -10004019 // Debounce this event with the dblclick event. If you try to doubleclick
4020 // a URL to open it, Chrome will fire click then dblclick, but we won't
4021 // have expanded the selection text at the first click event.
4022 clearTimeout(this.timeouts_.openUrl);
4023 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
4024 500);
4025 return;
4026 }
4027
Mike Frysinger847577f2017-05-23 23:25:57 -04004028 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04004029 if (e.ctrlKey && e.button == 2 /* right button */) {
4030 e.preventDefault();
4031 this.contextMenu.show(e, this);
4032 } else if (e.button == this.mousePasteButton ||
4033 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04004034 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04004035 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004036 }
Mike Frysinger847577f2017-05-23 23:25:57 -04004037 }
4038 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004039
Mike Frysinger2edd3612017-05-24 00:54:39 -04004040 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004041 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05004042 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004043 }
4044
4045 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
4046 this.scrollBlockerNode_.engaged) {
4047 // Disengage the scroll-blocker after one of these events.
4048 this.scrollBlockerNode_.engaged = false;
4049 this.scrollBlockerNode_.style.top = '-99px';
4050 }
4051
Mike Frysinger3c9fa072017-07-13 10:21:13 -04004052 // Emulate arrow key presses via scroll wheel events.
4053 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
4054 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04004055 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07004056 const delta =
4057 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04004058
Mike Frysinger321063c2018-08-29 15:33:14 -04004059 // Helper to turn a wheel event delta into a series of key presses.
4060 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
4061 if (distance == 0) {
4062 return '';
4063 }
4064
4065 // Convert the scroll distance into a number of rows/cols.
4066 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4067 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4068 return data.repeat(cells);
4069 };
4070
4071 // The order between up/down and left/right doesn't really matter.
4072 this.io.sendString(
4073 // Up/down arrow keys.
4074 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4075 'A', 'B') +
4076 // Left/right arrow keys.
4077 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004078 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004079 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004080
4081 e.preventDefault();
4082 }
4083 }
Robert Ginda928cf632014-03-05 15:07:41 -08004084 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004085 if (!this.scrollBlockerNode_.engaged) {
4086 if (e.type == 'mousedown') {
4087 // Move the scroll-blocker into place if we want to keep the scrollport
4088 // from scrolling.
4089 this.scrollBlockerNode_.engaged = true;
4090 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4091 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4092 } else if (e.type == 'mousemove') {
4093 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4094 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004095 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004096 e.preventDefault();
4097 }
4098 }
Robert Ginda928cf632014-03-05 15:07:41 -08004099
4100 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004101 }
4102
Robert Ginda928cf632014-03-05 15:07:41 -08004103 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4104 // Restore this on mouseup in case it was temporarily defeated with a
4105 // alt-mousedown. Only do this when the selection is empty so that
4106 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004107 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004108 }
rgindad5613292012-06-19 15:40:37 -07004109};
4110
4111/**
4112 * Clients should override this if they care to know about mouse events.
4113 *
4114 * The event parameter will be a normal DOM mouse click event with additional
4115 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004116 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004117 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004118 */
4119hterm.Terminal.prototype.onMouse = function(e) { };
4120
4121/**
rginda8e92a692012-05-20 19:37:20 -07004122 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004123 *
4124 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004125 */
Rob Spies06533ba2014-04-24 11:20:37 -07004126hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4127 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004128 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004129
Mike Frysingerbdb34802020-04-07 03:47:32 -04004130 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004131 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004132 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004133
Mike Frysingerbdb34802020-04-07 03:47:32 -04004134 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004135 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004136 }
rginda8e92a692012-05-20 19:37:20 -07004137};
4138
4139/**
rginda8ba33642011-12-14 12:31:31 -08004140 * React when the ScrollPort is scrolled.
4141 */
4142hterm.Terminal.prototype.onScroll_ = function() {
4143 this.scheduleSyncCursorPosition_();
4144};
4145
4146/**
rginda9846e2f2012-01-27 13:53:33 -08004147 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004148 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004149 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004150 */
4151hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004152 this.onPasteData_(e.text);
4153};
4154
4155/**
4156 * Handle pasted data.
4157 *
4158 * @param {string} data The pasted data.
4159 */
4160hterm.Terminal.prototype.onPasteData_ = function(data) {
4161 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004162 if (this.options_.bracketedPaste) {
4163 // We strip out most escape sequences as they can cause issues (like
4164 // inserting an \x1b[201~ midstream). We pass through whitespace
4165 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4166 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004167 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004168 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4169 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4170 }
Robert Gindaa063b202014-07-21 11:08:25 -07004171
4172 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004173};
4174
4175/**
rgindaa09e7332012-08-17 12:49:51 -07004176 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004177 *
Joel Hockey0f933582019-08-27 18:01:51 -07004178 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004179 */
4180hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004181 if (!this.useDefaultWindowCopy) {
4182 e.preventDefault();
4183 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4184 }
rgindaa09e7332012-08-17 12:49:51 -07004185};
4186
4187/**
rginda8ba33642011-12-14 12:31:31 -08004188 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004189 *
4190 * Note: This function should not directly contain code that alters the internal
4191 * state of the terminal. That kind of code belongs in realizeWidth or
4192 * realizeHeight, so that it can be executed synchronously in the case of a
4193 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004194 */
4195hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004196 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4197 this.scrollPort_.characterSize.width) || 0;
4198 const rowCount = lib.f.smartFloorDivide(
4199 this.scrollPort_.getScreenHeight(),
4200 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004201
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004202 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004203 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004204 // gets removed from the document or during the initial load, and we can't
4205 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004206 // This can also happen if called before the scrollPort calculates the
4207 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004208 return;
4209 }
4210
Mike Frysingerdc727792020-04-10 01:41:13 -04004211 const isNewSize = (columnCount != this.screenSize.width ||
4212 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004213 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004214
4215 // We do this even if the size didn't change, just to be sure everything is
4216 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004217 this.realizeSize_(columnCount, rowCount);
Jason Lin002e1392020-07-30 14:20:24 +10004218 this.updateCssCharsize_();
rgindaf522ce02012-04-17 17:49:17 -07004219 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004220
Mike Frysingerbdb34802020-04-07 03:47:32 -04004221 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004222 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004223 }
rgindaa8ba17d2012-08-15 14:41:10 -07004224
Robert Gindafb1be6a2013-12-11 11:56:22 -08004225 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004226 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004227
4228 if (wasScrolledEnd) {
4229 this.scrollEnd();
4230 }
rginda8ba33642011-12-14 12:31:31 -08004231};
4232
4233/**
4234 * Service the cursor blink timeout.
4235 */
4236hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004237 if (!this.options_.cursorBlink) {
4238 delete this.timeouts_.cursorBlink;
4239 return;
4240 }
4241
Robert Ginda830583c2013-08-07 13:20:46 -07004242 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004243 this.cursorNode_.style.opacity == '0' ||
4244 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004245 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004246 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4247 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004248 } else {
rginda87b86462011-12-14 13:48:03 -08004249 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004250 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4251 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004252 }
4253};
David Reveman8f552492012-03-28 12:18:41 -04004254
4255/**
4256 * Set the scrollbar-visible mode bit.
4257 *
4258 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4259 * Otherwise it will not.
4260 *
4261 * Defaults to on.
4262 *
4263 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4264 */
4265hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4266 this.scrollPort_.setScrollbarVisible(state);
4267};
Michael Kelly485ecd12014-06-09 11:41:56 -04004268
4269/**
Rob Spies49039e52014-12-17 13:40:04 -08004270 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004271 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004272 *
4273 * Defaults to 1.
4274 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004275 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004276 */
4277hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4278 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4279};
4280
4281/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004282 * Close all web notifications created by terminal bells.
4283 */
4284hterm.Terminal.prototype.closeBellNotifications_ = function() {
4285 this.bellNotificationList_.forEach(function(n) {
4286 n.close();
4287 });
4288 this.bellNotificationList_.length = 0;
4289};
Raymes Khourye5d48982018-08-02 09:08:32 +10004290
4291/**
4292 * Syncs the cursor position when the scrollport gains focus.
4293 */
4294hterm.Terminal.prototype.onScrollportFocus_ = function() {
4295 // If the cursor is offscreen we set selection to the last row on the screen.
4296 const topRowIndex = this.scrollPort_.getTopRowIndex();
4297 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4298 const selection = this.document_.getSelection();
4299 if (!this.syncCursorPosition_() && selection) {
4300 selection.collapse(this.getRowNode(bottomRowIndex));
4301 }
4302};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004303
4304/**
4305 * Clients can override this if they want to provide an options page.
4306 */
4307hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4308
4309
4310/**
4311 * Called when user selects to open the options page.
4312 */
4313hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4314 this.onOpenOptionsPage();
4315};