blob: 13ca6b426717af3f18060a6aa1cd0ff4c0d2773c [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Robert Ginda57f03b42012-09-13 11:02:48 -070030 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080031
Joel Hockeyd4fca732019-09-20 16:57:03 -070032 /** @type {?hterm.PreferenceManager} */
33 this.prefs_ = null;
34
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070054 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070055 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080056
rginda87b86462011-12-14 13:48:03 -080057 // The div that contains this terminal.
58 this.div_ = null;
59
rgindac9bc5502012-01-18 11:48:44 -080060 // The document that contains the scrollPort. Defaulted to the global
61 // document here so that the terminal is functional even if it hasn't been
62 // inserted into a document yet, but re-set in decorate().
63 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080064
rginda8ba33642011-12-14 12:31:31 -080065 // The rows that have scrolled off screen and are no longer addressable.
66 this.scrollbackRows_ = [];
67
rgindac9bc5502012-01-18 11:48:44 -080068 // Saved tab stops.
69 this.tabStops_ = [];
70
David Benjamin66e954d2012-05-05 21:08:12 -040071 // Keep track of whether default tab stops have been erased; after a TBC
72 // clears all tab stops, defaults aren't restored on resize until a reset.
73 this.defaultTabStops = true;
74
rginda8ba33642011-12-14 12:31:31 -080075 // The VT's notion of the top and bottom rows. Used during some VT
76 // cursor positioning and scrolling commands.
77 this.vtScrollTop_ = null;
78 this.vtScrollBottom_ = null;
79
80 // The DIV element for the visible cursor.
81 this.cursorNode_ = null;
82
Robert Ginda830583c2013-08-07 13:20:46 -070083 // The current cursor shape of the terminal.
84 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
85
Robert Gindaea2183e2014-07-17 09:51:51 -070086 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
87 this.cursorBlinkCycle_ = [100, 100];
88
Mike Frysinger225c99d2019-10-20 14:02:37 -060089 // Whether to temporarily disable blinking.
90 this.cursorBlinkPause_ = false;
91
Joel Hockey3babf302020-04-22 15:00:06 -070092 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
93 this.cursorOffScreen_ = false;
94
Robert Gindaea2183e2014-07-17 09:51:51 -070095 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
96 // cursor on/off servicing.
97 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
98
rginda9f5222b2012-03-05 11:53:28 -080099 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700100 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700101 /** @type {?string} */
102 this.backgroundColor_ = null;
103 /** @type {?string} */
104 this.foregroundColor_ = null;
105
Robert Ginda57f03b42012-09-13 11:02:48 -0700106 this.scrollOnOutput_ = null;
107 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400108 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800109
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700110 // True if we should override mouse event reporting to allow local selection.
111 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800112
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400113 // Whether to auto hide the mouse cursor when typing.
114 this.setAutomaticMouseHiding();
115 // Timer to keep mouse visible while it's being used.
116 this.mouseHideDelay_ = null;
117
rgindaf0090c92012-02-10 14:58:52 -0800118 // Terminal bell sound.
119 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400120 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800121 this.bellAudio_.setAttribute('preload', 'auto');
122
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000123 // The AccessibilityReader object for announcing command output.
124 this.accessibilityReader_ = null;
125
Mike Frysingercc114512017-09-11 21:39:17 -0400126 // The context menu object.
127 this.contextMenu = new hterm.ContextMenu();
128
Michael Kelly485ecd12014-06-09 11:41:56 -0400129 // All terminal bell notifications that have been generated (not necessarily
130 // shown).
131 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700132 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400133
134 // Whether we have permission to display notifications.
135 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400136
rginda6d397402012-01-17 10:58:29 -0800137 // Cursor position and attributes saved with DECSC.
138 this.savedOptions_ = {};
139
rginda8ba33642011-12-14 12:31:31 -0800140 // The current mode bits for the terminal.
141 this.options_ = new hterm.Options();
142
143 // Timeouts we might need to clear.
144 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800145
146 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800147 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800148
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800149 this.saveCursorAndState(true);
150
Zhu Qunying30d40712017-03-14 16:27:00 -0700151 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800152 this.keyboard = new hterm.Keyboard(this);
153
rginda87b86462011-12-14 13:48:03 -0800154 // General IO interface that can be given to third parties without exposing
155 // the entire terminal object.
156 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800157
rgindad5613292012-06-19 15:40:37 -0700158 // True if mouse-click-drag should scroll the terminal.
159 this.enableMouseDragScroll = true;
160
Robert Ginda57f03b42012-09-13 11:02:48 -0700161 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400162 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700163 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700164
Zhu Qunying30d40712017-03-14 16:27:00 -0700165 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700166 this.useDefaultWindowCopy = false;
167
168 this.clearSelectionAfterCopy = true;
169
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400170 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800171 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700172
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400173 // Whether we allow images to be shown.
174 this.allowImagesInline = null;
175
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400176 this.reportFocus = false;
177
Jason Linf129f3c2020-03-23 11:52:08 +1100178 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
179 this.alwaysUseLegacyPasting = false;
180
Joel Hockey3a44a442019-10-14 16:22:56 -0700181 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500182 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800183};
184
185/**
Robert Ginda830583c2013-08-07 13:20:46 -0700186 * Possible cursor shapes.
187 */
188hterm.Terminal.cursorShape = {
189 BLOCK: 'BLOCK',
190 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400191 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700192};
193
194/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 * Clients should override this to be notified when the terminal is ready
196 * for use.
197 *
198 * The terminal initialization is asynchronous, and shouldn't be used before
199 * this method is called.
200 */
201hterm.Terminal.prototype.onTerminalReady = function() { };
202
203/**
rginda35c456b2012-02-09 17:29:05 -0800204 * Default tab with of 8 to match xterm.
205 */
206hterm.Terminal.prototype.tabWidth = 8;
207
208/**
rginda9f5222b2012-03-05 11:53:28 -0800209 * Select a preference profile.
210 *
211 * This will load the terminal preferences for the given profile name and
212 * associate subsequent preference changes with the new preference profile.
213 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500214 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800215 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400216 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700217 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800218 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400219hterm.Terminal.prototype.setProfile = function(
220 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700221 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800222
Mike Frysingerdc727792020-04-10 01:41:13 -0400223 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800224
Mike Frysingerbdb34802020-04-07 03:47:32 -0400225 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400227 }
rginda9f5222b2012-03-05 11:53:28 -0800228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700230
231 /**
232 * Clears and reloads key bindings. Used by preferences
233 * 'keybindings' and 'keybindings-os-defaults'.
234 *
235 * @param {*} bindings
236 * @param {*} useOsDefaults
237 */
238 function loadKeyBindings(bindings, useOsDefaults) {
239 terminal.keyboard.bindings.clear();
240
241 if (!bindings) {
242 return;
243 }
244
245 if (!(bindings instanceof Object)) {
246 console.error('Error in keybindings preference: Expected object');
247 return;
248 }
249
250 try {
251 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
252 } catch (ex) {
253 console.error('Error in keybindings preference: ' + ex);
254 }
255 }
256
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800258 'alt-gr-mode': function(v) {
259 if (v == null) {
260 if (navigator.language.toLowerCase() == 'en-us') {
261 v = 'none';
262 } else {
263 v = 'right-alt';
264 }
265 } else if (typeof v == 'string') {
266 v = v.toLowerCase();
267 } else {
268 v = 'none';
269 }
270
Mike Frysingerbdb34802020-04-07 03:47:32 -0400271 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800272 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400273 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800274
275 terminal.keyboard.altGrMode = v;
276 },
277
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700278 'alt-backspace-is-meta-backspace': function(v) {
279 terminal.keyboard.altBackspaceIsMetaBackspace = v;
280 },
281
Robert Ginda57f03b42012-09-13 11:02:48 -0700282 'alt-is-meta': function(v) {
283 terminal.keyboard.altIsMeta = v;
284 },
285
286 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400287 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400289 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700290
291 terminal.keyboard.altSendsWhat = v;
292 },
293
294 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400295 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800296 if (ary) {
297 terminal.bellAudio_.setAttribute('src',
298 lib.resource.getDataUrl(ary[1]));
299 } else {
300 terminal.bellAudio_.setAttribute('src', v);
301 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700302 },
303
Michael Kelly485ecd12014-06-09 11:41:56 -0400304 'desktop-notification-bell': function(v) {
305 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700306 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400307 Notification.permission === 'granted';
308 if (!terminal.desktopNotificationBell_) {
309 // Note: We don't call Notification.requestPermission here because
310 // Chrome requires the call be the result of a user action (such as an
311 // onclick handler), and pref listeners are run asynchronously.
312 //
313 // A way of working around this would be to display a dialog in the
314 // terminal with a "click-to-request-permission" button.
315 console.warn('desktop-notification-bell is true but we do not have ' +
316 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400317 }
318 } else {
319 terminal.desktopNotificationBell_ = false;
320 }
321 },
322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 'background-color': function(v) {
324 terminal.setBackgroundColor(v);
325 },
326
327 'background-image': function(v) {
328 terminal.scrollPort_.setBackgroundImage(v);
329 },
330
331 'background-size': function(v) {
332 terminal.scrollPort_.setBackgroundSize(v);
333 },
334
335 'background-position': function(v) {
336 terminal.scrollPort_.setBackgroundPosition(v);
337 },
338
339 'backspace-sends-backspace': function(v) {
340 terminal.keyboard.backspaceSendsBackspace = v;
341 },
342
Brad Town18654b62015-03-12 00:27:45 -0700343 'character-map-overrides': function(v) {
344 if (!(v == null || v instanceof Object)) {
345 console.warn('Preference character-map-modifications is not an ' +
346 'object: ' + v);
347 return;
348 }
349
Mike Frysinger095d4062017-06-14 00:29:48 -0700350 terminal.vt.characterMaps.reset();
351 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700352 },
353
Robert Ginda57f03b42012-09-13 11:02:48 -0700354 'cursor-blink': function(v) {
355 terminal.setCursorBlink(!!v);
356 },
357
Joel Hockey9d10ba12019-05-28 01:25:02 -0700358 'cursor-shape': function(v) {
359 terminal.setCursorShape(v);
360 },
361
Robert Gindaea2183e2014-07-17 09:51:51 -0700362 'cursor-blink-cycle': function(v) {
363 if (v instanceof Array &&
364 typeof v[0] == 'number' &&
365 typeof v[1] == 'number') {
366 terminal.cursorBlinkCycle_ = v;
367 } else if (typeof v == 'number') {
368 terminal.cursorBlinkCycle_ = [v, v];
369 } else {
370 // Fast blink indicates an error.
371 terminal.cursorBlinkCycle_ = [100, 100];
372 }
373 },
374
Robert Ginda57f03b42012-09-13 11:02:48 -0700375 'cursor-color': function(v) {
376 terminal.setCursorColor(v);
377 },
378
379 'color-palette-overrides': function(v) {
380 if (!(v == null || v instanceof Object || v instanceof Array)) {
381 console.warn('Preference color-palette-overrides is not an array or ' +
382 'object: ' + v);
383 return;
rginda9f5222b2012-03-05 11:53:28 -0800384 }
rginda9f5222b2012-03-05 11:53:28 -0800385
Joel Hockey42dba8f2020-03-26 16:21:11 -0700386 // Call terminal.setColorPalette here and below with the new default
387 // value before changing it in lib.colors.colorPalette to ensure that
388 // CSS vars are updated.
389 lib.colors.stockColorPalette.forEach(
390 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700392
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400394 for (const key in v) {
395 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 if (isNaN(i) || i < 0 || i > 255) {
397 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
398 continue;
399 }
400
401 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400402 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700403 if (rgb) {
404 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700406 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 }
408 }
rginda30f20f62012-04-05 16:36:19 -0700409 }
rginda30f20f62012-04-05 16:36:19 -0700410
Joel Hockey42dba8f2020-03-26 16:21:11 -0700411 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
412 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700413 },
rginda30f20f62012-04-05 16:36:19 -0700414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 'copy-on-select': function(v) {
416 terminal.copyOnSelect = !!v;
417 },
rginda9f5222b2012-03-05 11:53:28 -0800418
Rob Spies0bec09b2014-06-06 15:58:09 -0700419 'use-default-window-copy': function(v) {
420 terminal.useDefaultWindowCopy = !!v;
421 },
422
423 'clear-selection-after-copy': function(v) {
424 terminal.clearSelectionAfterCopy = !!v;
425 },
426
Robert Ginda7e5e9522014-03-14 12:23:58 -0700427 'ctrl-plus-minus-zero-zoom': function(v) {
428 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
429 },
430
Robert Gindafb5a3f92014-05-13 14:12:00 -0700431 'ctrl-c-copy': function(v) {
432 terminal.keyboard.ctrlCCopy = v;
433 },
434
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100435 'ctrl-v-paste': function(v) {
436 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700437 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100438 },
439
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700440 'paste-on-drop': function(v) {
441 terminal.scrollPort_.setPasteOnDrop(v);
442 },
443
Masaya Suzuki273aa982014-05-31 07:25:55 +0900444 'east-asian-ambiguous-as-two-column': function(v) {
445 lib.wc.regardCjkAmbiguous = v;
446 },
447
Robert Ginda57f03b42012-09-13 11:02:48 -0700448 'enable-8-bit-control': function(v) {
449 terminal.vt.enable8BitControl = !!v;
450 },
rginda30f20f62012-04-05 16:36:19 -0700451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'enable-bold': function(v) {
453 terminal.syncBoldSafeState();
454 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400455
Robert Ginda3e278d72014-03-25 13:18:51 -0700456 'enable-bold-as-bright': function(v) {
457 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
458 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
459 },
460
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400461 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500462 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400463 },
464
Robert Ginda57f03b42012-09-13 11:02:48 -0700465 'enable-clipboard-write': function(v) {
466 terminal.vt.enableClipboardWrite = !!v;
467 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400468
Robert Ginda3755e752013-05-31 13:34:09 -0700469 'enable-dec12': function(v) {
470 terminal.vt.enableDec12 = !!v;
471 },
472
Mike Frysinger38f267d2018-09-07 02:50:59 -0400473 'enable-csi-j-3': function(v) {
474 terminal.vt.enableCsiJ3 = !!v;
475 },
476
Robert Ginda57f03b42012-09-13 11:02:48 -0700477 'font-family': function(v) {
478 terminal.syncFontFamily();
479 },
rginda30f20f62012-04-05 16:36:19 -0700480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700482 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700483 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500484 console.error(`Invalid font size: ${v}`);
485 return;
486 }
487
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 terminal.setFontSize(v);
489 },
rginda9875d902012-08-20 16:21:57 -0700490
Robert Ginda57f03b42012-09-13 11:02:48 -0700491 'font-smoothing': function(v) {
492 terminal.syncFontFamily();
493 },
rgindade84e382012-04-20 15:39:31 -0700494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 'foreground-color': function(v) {
496 terminal.setForegroundColor(v);
497 },
rginda30f20f62012-04-05 16:36:19 -0700498
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400499 'hide-mouse-while-typing': function(v) {
500 terminal.setAutomaticMouseHiding(v);
501 },
502
Robert Ginda57f03b42012-09-13 11:02:48 -0700503 'home-keys-scroll': function(v) {
504 terminal.keyboard.homeKeysScroll = v;
505 },
rginda4bba5e12012-06-20 16:15:30 -0700506
Robert Gindaa8165692015-06-15 14:46:31 -0700507 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700508 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
509 },
Robert Gindaa8165692015-06-15 14:46:31 -0700510
Joel Hockey95a9e272020-03-16 21:19:53 -0700511 'keybindings-os-defaults': function(v) {
512 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700513 },
514
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700515 'media-keys-are-fkeys': function(v) {
516 terminal.keyboard.mediaKeysAreFKeys = v;
517 },
518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'meta-sends-escape': function(v) {
520 terminal.keyboard.metaSendsEscape = v;
521 },
rginda30f20f62012-04-05 16:36:19 -0700522
Mike Frysinger847577f2017-05-23 23:25:57 -0400523 'mouse-right-click-paste': function(v) {
524 terminal.mouseRightClickPaste = v;
525 },
526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'mouse-paste-button': function(v) {
528 terminal.syncMousePasteButton();
529 },
rgindaa8ba17d2012-08-15 14:41:10 -0700530
Robert Gindae76aa9f2014-03-14 12:29:12 -0700531 'page-keys-scroll': function(v) {
532 terminal.keyboard.pageKeysScroll = v;
533 },
534
Robert Ginda40932892012-12-10 17:26:40 -0800535 'pass-alt-number': function(v) {
536 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700537 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800538 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500539 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800540 }
541
542 terminal.passAltNumber = v;
543 },
544
545 'pass-ctrl-number': function(v) {
546 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700547 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800548 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500549 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800550 }
551
552 terminal.passCtrlNumber = v;
553 },
554
Joel Hockey0e052042020-02-19 05:37:19 -0800555 'pass-ctrl-n': function(v) {
556 terminal.passCtrlN = v;
557 },
558
559 'pass-ctrl-t': function(v) {
560 terminal.passCtrlT = v;
561 },
562
563 'pass-ctrl-tab': function(v) {
564 terminal.passCtrlTab = v;
565 },
566
567 'pass-ctrl-w': function(v) {
568 terminal.passCtrlW = v;
569 },
570
Robert Ginda40932892012-12-10 17:26:40 -0800571 'pass-meta-number': function(v) {
572 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700573 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800574 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500575 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800576 }
577
578 terminal.passMetaNumber = v;
579 },
580
Marius Schilder77857b32014-05-14 16:21:26 -0700581 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700582 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700583 },
584
Robert Ginda8cb7d902013-06-20 14:37:18 -0700585 'receive-encoding': function(v) {
586 if (!(/^(utf-8|raw)$/).test(v)) {
587 console.warn('Invalid value for "receive-encoding": ' + v);
588 v = 'utf-8';
589 }
590
591 terminal.vt.characterEncoding = v;
592 },
593
Joel Hockey139d82d2020-04-07 23:04:29 -0700594 'screen-padding-size': function(v) {
595 v = parseInt(v, 10);
596 if (isNaN(v) || v < 0) {
597 console.error(`Invalid screen padding size: ${v}`);
598 return;
599 }
600 terminal.setCssVar('screen-padding-size', `${v}px`);
601 terminal.setScreenPaddingSize(v);
602 },
603
Robert Ginda57f03b42012-09-13 11:02:48 -0700604 'scroll-on-keystroke': function(v) {
605 terminal.scrollOnKeystroke_ = v;
606 },
rginda9f5222b2012-03-05 11:53:28 -0800607
Robert Ginda57f03b42012-09-13 11:02:48 -0700608 'scroll-on-output': function(v) {
609 terminal.scrollOnOutput_ = v;
610 },
rginda30f20f62012-04-05 16:36:19 -0700611
Robert Ginda57f03b42012-09-13 11:02:48 -0700612 'scrollbar-visible': function(v) {
613 terminal.setScrollbarVisible(v);
614 },
rginda9f5222b2012-03-05 11:53:28 -0800615
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400616 'scroll-wheel-may-send-arrow-keys': function(v) {
617 terminal.scrollWheelArrowKeys_ = v;
618 },
619
Rob Spies49039e52014-12-17 13:40:04 -0800620 'scroll-wheel-move-multiplier': function(v) {
621 terminal.setScrollWheelMoveMultipler(v);
622 },
623
Robert Ginda57f03b42012-09-13 11:02:48 -0700624 'shift-insert-paste': function(v) {
625 terminal.keyboard.shiftInsertPaste = v;
626 },
rginda9f5222b2012-03-05 11:53:28 -0800627
Mike Frysingera7768922017-07-28 15:00:12 -0400628 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400629 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400630 },
631
Robert Gindae76aa9f2014-03-14 12:29:12 -0700632 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400633 terminal.scrollPort_.setUserCssUrl(v);
634 },
635
636 'user-css-text': function(v) {
637 terminal.scrollPort_.setUserCssText(v);
638 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400639
640 'word-break-match-left': function(v) {
641 terminal.primaryScreen_.wordBreakMatchLeft = v;
642 terminal.alternateScreen_.wordBreakMatchLeft = v;
643 },
644
645 'word-break-match-right': function(v) {
646 terminal.primaryScreen_.wordBreakMatchRight = v;
647 terminal.alternateScreen_.wordBreakMatchRight = v;
648 },
649
650 'word-break-match-middle': function(v) {
651 terminal.primaryScreen_.wordBreakMatchMiddle = v;
652 terminal.alternateScreen_.wordBreakMatchMiddle = v;
653 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400654
655 'allow-images-inline': function(v) {
656 terminal.allowImagesInline = v;
657 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700658 });
rginda30f20f62012-04-05 16:36:19 -0700659
Robert Ginda57f03b42012-09-13 11:02:48 -0700660 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800661 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700662
Mike Frysingerec4225d2020-04-07 05:00:01 -0400663 if (callback) {
664 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400665 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700666 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800667};
668
Rob Spies56953412014-04-28 14:09:47 -0700669/**
670 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500671 *
Joel Hockey0f933582019-08-27 18:01:51 -0700672 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700673 */
674hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700675 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700676};
677
Robert Gindaa063b202014-07-21 11:08:25 -0700678/**
679 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500680 *
681 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700682 */
683hterm.Terminal.prototype.setBracketedPaste = function(state) {
684 this.options_.bracketedPaste = state;
685};
Rob Spies56953412014-04-28 14:09:47 -0700686
rginda8e92a692012-05-20 19:37:20 -0700687/**
688 * Set the color for the cursor.
689 *
690 * If you want this setting to persist, set it through prefs_, rather than
691 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500692 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500693 * @param {string=} color The color to set. If not defined, we reset to the
694 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700695 */
696hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400697 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700698 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400699 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500700
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400701 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700702};
703
704/**
705 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400706 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500707 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700708 */
709hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400710 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700711};
712
713/**
rgindad5613292012-06-19 15:40:37 -0700714 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500715 *
716 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700717 */
718hterm.Terminal.prototype.setSelectionEnabled = function(state) {
719 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700720};
721
722/**
rginda8e92a692012-05-20 19:37:20 -0700723 * Set the background color.
724 *
725 * If you want this setting to persist, set it through prefs_, rather than
726 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500727 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500728 * @param {string=} color The color to set. If not defined, we reset to the
729 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700730 */
731hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400732 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700733 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400734 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500735
Joel Hockey42dba8f2020-03-26 16:21:11 -0700736 this.backgroundColor_ = lib.colors.normalizeCSS(color);
737 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700738};
739
rginda9f5222b2012-03-05 11:53:28 -0800740/**
741 * Return the current terminal background color.
742 *
743 * Intended for use by other classes, so we don't have to expose the entire
744 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500745 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700746 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800747 */
748hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700749 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700750};
751
752/**
753 * Set the foreground color.
754 *
755 * If you want this setting to persist, set it through prefs_, rather than
756 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500757 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500758 * @param {string=} color The color to set. If not defined, we reset to the
759 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700760 */
761hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400762 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700763 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400764 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500765
Joel Hockey42dba8f2020-03-26 16:21:11 -0700766 this.foregroundColor_ = lib.colors.normalizeCSS(color);
767 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800768};
769
770/**
771 * Return the current terminal foreground color.
772 *
773 * Intended for use by other classes, so we don't have to expose the entire
774 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500775 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700776 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800777 */
778hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700779 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800780};
781
782/**
rginda87b86462011-12-14 13:48:03 -0800783 * Create a new instance of a terminal command and run it with a given
784 * argument string.
785 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700786 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700787 * @param {string} commandName The command to run for this terminal.
788 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800789 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700790hterm.Terminal.prototype.runCommandClass = function(
791 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400792 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400793 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700794 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400795 }
rgindaf522ce02012-04-17 17:49:17 -0700796
rginda87b86462011-12-14 13:48:03 -0800797 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700798 {
799 commandName: commandName,
800 args: args,
rginda87b86462011-12-14 13:48:03 -0800801 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700802 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400803 onExit: (code) => {
804 this.io.pop();
805 this.uninstallKeyboard();
806 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
807 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400808 window.close();
809 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400810 },
rginda87b86462011-12-14 13:48:03 -0800811 });
812
rgindafeaf3142012-01-31 15:14:20 -0800813 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800814 this.command.run();
815};
816
817/**
rgindafeaf3142012-01-31 15:14:20 -0800818 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500819 *
820 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800821 */
822hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700823 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800824};
825
826/**
827 * Install the keyboard handler for this terminal.
828 *
829 * This will prevent the browser from seeing any keystrokes sent to the
830 * terminal.
831 */
832hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700833 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400834};
rgindafeaf3142012-01-31 15:14:20 -0800835
836/**
837 * Uninstall the keyboard handler for this terminal.
838 */
839hterm.Terminal.prototype.uninstallKeyboard = function() {
840 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400841};
rgindafeaf3142012-01-31 15:14:20 -0800842
843/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400844 * Set a CSS variable.
845 *
846 * Normally this is used to set variables in the hterm namespace.
847 *
848 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700849 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400850 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400851 */
852hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400853 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400854 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400855 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400856};
857
858/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700859 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
860 * input is valid.
861 *
862 * @param {string} name The variable to set.
863 * @param {?string} rgb The rgb value to assign to the variable.
864 */
865hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
866 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
867 if (ary) {
868 this.setCssVar(name, ary.slice(0, 3).join(','));
869 }
870};
871
872/**
873 * Sets the specified color for the active screen.
874 *
875 * @param {number} i The index into the 256 color palette to set.
876 * @param {?string} rgb The rgb value to assign to the variable.
877 */
878hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
879 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
880 this.setRgbColorCssVar(`color-${i}`, rgb);
881 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
882 }
883};
884
885/**
886 * Returns the current value in the active screen of the specified color.
887 *
888 * @param {number} i Color palette index.
889 * @return {string} rgb color.
890 */
891hterm.Terminal.prototype.getColorPalette = function(i) {
892 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
893 lib.colors.colorPalette[i];
894};
895
896/**
897 * Reset the specified color in the active screen to its default value.
898 *
899 * @param {number} i Color to reset
900 */
901hterm.Terminal.prototype.resetColor = function(i) {
902 this.setColorPalette(i, lib.colors.colorPalette[i]);
903 delete this.screen_.textAttributes.colorPaletteOverrides[i];
904};
905
906/**
907 * Reset the current screen color palette to the default state.
908 */
909hterm.Terminal.prototype.resetColorPalette = function() {
910 this.screen_.textAttributes.colorPaletteOverrides.forEach(
911 (c, i) => this.resetColor(i));
912};
913
914/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500915 * Get a CSS variable.
916 *
917 * Normally this is used to get variables in the hterm namespace.
918 *
919 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400920 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500921 * @return {string} The current setting for this variable.
922 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400923hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500924 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400925 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500926};
927
928/**
Jason Linbbbdb752020-03-06 16:26:59 +1100929 * Update CSS character size variables to match the scrollport.
930 */
931hterm.Terminal.prototype.updateCssCharsize_ = function() {
932 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
933 this.setCssVar('charsize-height',
934 this.scrollPort_.characterSize.height + 'px');
935};
936
937/**
rginda35c456b2012-02-09 17:29:05 -0800938 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800939 *
940 * Call setFontSize(0) to reset to the default font size.
941 *
942 * This function does not modify the font-size preference.
943 *
944 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800945 */
946hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400947 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700948 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400949 }
rginda9f5222b2012-03-05 11:53:28 -0800950
rginda35c456b2012-02-09 17:29:05 -0800951 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100952 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800953};
954
955/**
956 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500957 *
958 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800959 */
960hterm.Terminal.prototype.getFontSize = function() {
961 return this.scrollPort_.getFontSize();
962};
963
964/**
rginda8e92a692012-05-20 19:37:20 -0700965 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500966 *
967 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700968 */
969hterm.Terminal.prototype.getFontFamily = function() {
970 return this.scrollPort_.getFontFamily();
971};
972
973/**
rginda35c456b2012-02-09 17:29:05 -0800974 * Set the CSS "font-family" for this terminal.
975 */
rginda9f5222b2012-03-05 11:53:28 -0800976hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700977 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
978 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100979 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800980 this.syncBoldSafeState();
981};
982
rginda4bba5e12012-06-20 16:15:30 -0700983/**
984 * Set this.mousePasteButton based on the mouse-paste-button pref,
985 * autodetecting if necessary.
986 */
987hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -0400988 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -0700989 if (typeof button == 'number') {
990 this.mousePasteButton = button;
991 return;
992 }
993
Mike Frysingeree81a002017-12-12 16:14:53 -0500994 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400995 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700996 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400997 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700998 }
999};
1000
1001/**
1002 * Enable or disable bold based on the enable-bold pref, autodetecting if
1003 * necessary.
1004 */
rginda9f5222b2012-03-05 11:53:28 -08001005hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001006 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001007 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001008 this.primaryScreen_.textAttributes.enableBold = enableBold;
1009 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001010 return;
1011 }
1012
Mike Frysingerdc727792020-04-10 01:41:13 -04001013 const normalSize = this.scrollPort_.measureCharacterSize();
1014 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001015
Mike Frysingerdc727792020-04-10 01:41:13 -04001016 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001017 if (!isBoldSafe) {
1018 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001019 'from normal. Font family is: ' +
1020 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001021 }
rginda9f5222b2012-03-05 11:53:28 -08001022
Robert Gindaed016262012-10-26 16:27:09 -07001023 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1024 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001025};
1026
1027/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001028 * Control text blinking behavior.
1029 *
1030 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001031 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001032hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001033 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001034 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001035 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001036 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001037};
1038
1039/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001040 * Set the mouse cursor style based on the current terminal mode.
1041 */
1042hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001043 this.setCssVar('mouse-cursor-style',
1044 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1045 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001046 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001047};
1048
1049/**
rginda87b86462011-12-14 13:48:03 -08001050 * Return a copy of the current cursor position.
1051 *
Joel Hockey0f933582019-08-27 18:01:51 -07001052 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001053 */
1054hterm.Terminal.prototype.saveCursor = function() {
1055 return this.screen_.cursorPosition.clone();
1056};
1057
Evan Jones2600d4f2016-12-06 09:29:36 -05001058/**
1059 * Return the current text attributes.
1060 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001061 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001062 */
rgindaa19afe22012-01-25 15:40:22 -08001063hterm.Terminal.prototype.getTextAttributes = function() {
1064 return this.screen_.textAttributes;
1065};
1066
Evan Jones2600d4f2016-12-06 09:29:36 -05001067/**
1068 * Set the text attributes.
1069 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001070 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001071 */
rginda1a09aa02012-06-18 21:11:25 -07001072hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1073 this.screen_.textAttributes = textAttributes;
1074};
1075
rginda87b86462011-12-14 13:48:03 -08001076/**
rgindaf522ce02012-04-17 17:49:17 -07001077 * Return the current browser zoom factor applied to the terminal.
1078 *
1079 * @return {number} The current browser zoom factor.
1080 */
1081hterm.Terminal.prototype.getZoomFactor = function() {
1082 return this.scrollPort_.characterSize.zoomFactor;
1083};
1084
1085/**
rginda9846e2f2012-01-27 13:53:33 -08001086 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001087 *
1088 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001089 */
1090hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001091 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001092};
1093
1094/**
rginda87b86462011-12-14 13:48:03 -08001095 * Restore a previously saved cursor position.
1096 *
Joel Hockey0f933582019-08-27 18:01:51 -07001097 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001098 */
1099hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001100 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1101 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001102 this.screen_.setCursorPosition(row, column);
1103 if (cursor.column > column ||
1104 cursor.column == column && cursor.overflow) {
1105 this.screen_.cursorPosition.overflow = true;
1106 }
rginda87b86462011-12-14 13:48:03 -08001107};
1108
1109/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001110 * Clear the cursor's overflow flag.
1111 */
1112hterm.Terminal.prototype.clearCursorOverflow = function() {
1113 this.screen_.cursorPosition.overflow = false;
1114};
1115
1116/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001117 * Save the current cursor state to the corresponding screens.
1118 *
1119 * See the hterm.Screen.CursorState class for more details.
1120 *
1121 * @param {boolean=} both If true, update both screens, else only update the
1122 * current screen.
1123 */
1124hterm.Terminal.prototype.saveCursorAndState = function(both) {
1125 if (both) {
1126 this.primaryScreen_.saveCursorAndState(this.vt);
1127 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001128 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001129 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001130 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001131};
1132
1133/**
1134 * Restore the saved cursor state in the corresponding screens.
1135 *
1136 * See the hterm.Screen.CursorState class for more details.
1137 *
1138 * @param {boolean=} both If true, update both screens, else only update the
1139 * current screen.
1140 */
1141hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1142 if (both) {
1143 this.primaryScreen_.restoreCursorAndState(this.vt);
1144 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001145 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001146 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001147 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001148};
1149
1150/**
Robert Ginda830583c2013-08-07 13:20:46 -07001151 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001152 *
1153 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001154 */
1155hterm.Terminal.prototype.setCursorShape = function(shape) {
1156 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001157 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001158};
Robert Ginda830583c2013-08-07 13:20:46 -07001159
1160/**
1161 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001162 *
1163 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001164 */
1165hterm.Terminal.prototype.getCursorShape = function() {
1166 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001167};
Robert Ginda830583c2013-08-07 13:20:46 -07001168
1169/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001170 * Set the screen padding size in pixels.
1171 *
1172 * @param {number} size
1173 */
1174hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
1175 this.scrollPort_.setScreenPaddingSize(size);
1176};
1177
1178/**
rginda87b86462011-12-14 13:48:03 -08001179 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001180 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001181 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001182 */
1183hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001184 if (columnCount == null) {
1185 this.div_.style.width = '100%';
1186 return;
1187 }
1188
Joel Hockey139d82d2020-04-07 23:04:29 -07001189 const rightPadding = Math.max(
1190 this.scrollPort_.screenPaddingSize,
1191 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001192 this.div_.style.width = Math.ceil(
Joel Hockey139d82d2020-04-07 23:04:29 -07001193 this.scrollPort_.characterSize.width * columnCount +
1194 this.scrollPort_.screenPaddingSize + rightPadding) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001195 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001196 this.scheduleSyncCursorPosition_();
1197};
rginda87b86462011-12-14 13:48:03 -08001198
rgindac9bc5502012-01-18 11:48:44 -08001199/**
rginda35c456b2012-02-09 17:29:05 -08001200 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001201 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001202 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001203 */
1204hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001205 if (rowCount == null) {
1206 this.div_.style.height = '100%';
1207 return;
1208 }
1209
Joel Hockey139d82d2020-04-07 23:04:29 -07001210 this.div_.style.height = this.scrollPort_.characterSize.height * rowCount +
1211 (2 * this.scrollPort_.screenPaddingSize) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001212 this.realizeSize_(this.screenSize.width, rowCount);
1213 this.scheduleSyncCursorPosition_();
1214};
1215
1216/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001217 * Deal with terminal size changes.
1218 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001219 * @param {number} columnCount The number of columns.
1220 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001221 */
1222hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001223 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001224
Mike Frysinger0206e262019-06-13 10:18:19 -04001225 if (columnCount != this.screenSize.width) {
1226 notify = true;
1227 this.realizeWidth_(columnCount);
1228 }
1229
1230 if (rowCount != this.screenSize.height) {
1231 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001232 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001233 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001234
1235 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001236 if (notify) {
1237 this.io.onTerminalResize_(columnCount, rowCount);
1238 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001239};
1240
1241/**
rgindac9bc5502012-01-18 11:48:44 -08001242 * Deal with terminal width changes.
1243 *
1244 * This function does what needs to be done when the terminal width changes
1245 * out from under us. It happens here rather than in onResize_() because this
1246 * code may need to run synchronously to handle programmatic changes of
1247 * terminal width.
1248 *
1249 * Relying on the browser to send us an async resize event means we may not be
1250 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001251 *
1252 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001253 */
1254hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001255 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001256 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001257 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001258
Mike Frysingerdc727792020-04-10 01:41:13 -04001259 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001260 if (deltaColumns == 0) {
1261 // No change, so don't bother recalculating things.
1262 return;
1263 }
rgindac9bc5502012-01-18 11:48:44 -08001264
rginda87b86462011-12-14 13:48:03 -08001265 this.screenSize.width = columnCount;
1266 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001267
1268 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001269 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001270 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001271 }
rgindac9bc5502012-01-18 11:48:44 -08001272 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001273 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001274 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001275 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001276 }
rgindac9bc5502012-01-18 11:48:44 -08001277
1278 this.tabStops_.pop();
1279 }
1280 }
1281
1282 this.screen_.setColumnCount(this.screenSize.width);
1283};
1284
1285/**
1286 * Deal with terminal height changes.
1287 *
1288 * This function does what needs to be done when the terminal height changes
1289 * out from under us. It happens here rather than in onResize_() because this
1290 * code may need to run synchronously to handle programmatic changes of
1291 * terminal height.
1292 *
1293 * Relying on the browser to send us an async resize event means we may not be
1294 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001295 *
1296 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001297 */
1298hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001299 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001300 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001301 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001302
Mike Frysingerdc727792020-04-10 01:41:13 -04001303 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001304 if (deltaRows == 0) {
1305 // No change, so don't bother recalculating things.
1306 return;
1307 }
rgindac9bc5502012-01-18 11:48:44 -08001308
1309 this.screenSize.height = rowCount;
1310
Mike Frysingerdc727792020-04-10 01:41:13 -04001311 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001312
1313 if (deltaRows < 0) {
1314 // Screen got smaller.
1315 deltaRows *= -1;
1316 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001317 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001318 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001319 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001320 }
rgindac9bc5502012-01-18 11:48:44 -08001321
Mike Frysingerbdb34802020-04-07 03:47:32 -04001322 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001323 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001324 }
rgindac9bc5502012-01-18 11:48:44 -08001325
1326 this.screen_.popRow();
1327 deltaRows--;
1328 }
1329
Mike Frysingerdc727792020-04-10 01:41:13 -04001330 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001331 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1332
1333 // We just removed rows from the top of the screen, we need to update
1334 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001335 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001336 } else if (deltaRows > 0) {
1337 // Screen got larger.
1338
1339 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001340 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1341 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001342 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1343 this.screen_.unshiftRows(rows);
1344 deltaRows -= scrollbackCount;
1345 cursor.row += scrollbackCount;
1346 }
1347
Mike Frysingerbdb34802020-04-07 03:47:32 -04001348 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001349 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001350 }
rgindac9bc5502012-01-18 11:48:44 -08001351 }
1352
rginda35c456b2012-02-09 17:29:05 -08001353 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001354 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001355};
1356
1357/**
1358 * Scroll the terminal to the top of the scrollback buffer.
1359 */
1360hterm.Terminal.prototype.scrollHome = function() {
1361 this.scrollPort_.scrollRowToTop(0);
1362};
1363
1364/**
1365 * Scroll the terminal to the end.
1366 */
1367hterm.Terminal.prototype.scrollEnd = function() {
1368 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1369};
1370
1371/**
1372 * Scroll the terminal one page up (minus one line) relative to the current
1373 * position.
1374 */
1375hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001376 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001377};
1378
1379/**
1380 * Scroll the terminal one page down (minus one line) relative to the current
1381 * position.
1382 */
1383hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001384 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001385};
1386
rgindac9bc5502012-01-18 11:48:44 -08001387/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001388 * Scroll the terminal one line up relative to the current position.
1389 */
1390hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001391 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001392 this.scrollPort_.scrollRowToTop(i - 1);
1393};
1394
1395/**
1396 * Scroll the terminal one line down relative to the current position.
1397 */
1398hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001399 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001400 this.scrollPort_.scrollRowToTop(i + 1);
1401};
1402
1403/**
Robert Ginda40932892012-12-10 17:26:40 -08001404 * Clear primary screen, secondary screen, and the scrollback buffer.
1405 */
1406hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001407 this.clearHome(this.primaryScreen_);
1408 this.clearHome(this.alternateScreen_);
1409
1410 this.clearScrollback();
1411};
1412
1413/**
1414 * Clear scrollback buffer.
1415 */
1416hterm.Terminal.prototype.clearScrollback = function() {
1417 // Move to the end of the buffer in case the screen was scrolled back.
1418 // We're going to throw it away which would leave the display invalid.
1419 this.scrollEnd();
1420
Robert Ginda40932892012-12-10 17:26:40 -08001421 this.scrollbackRows_.length = 0;
1422 this.scrollPort_.resetCache();
1423
Mike Frysinger9c482b82018-09-07 02:49:36 -04001424 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1425 const bottom = screen.getHeight();
1426 this.renumberRows_(0, bottom, screen);
1427 });
Robert Ginda40932892012-12-10 17:26:40 -08001428
1429 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001430 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001431};
1432
1433/**
rgindac9bc5502012-01-18 11:48:44 -08001434 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001435 *
1436 * Perform a full reset to the default values listed in
1437 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001438 */
rginda87b86462011-12-14 13:48:03 -08001439hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001440 this.vt.reset();
1441
rgindac9bc5502012-01-18 11:48:44 -08001442 this.clearAllTabStops();
1443 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001444
Joel Hockey42dba8f2020-03-26 16:21:11 -07001445 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001446 const resetScreen = (screen) => {
1447 // We want to make sure to reset the attributes before we clear the screen.
1448 // The attributes might be used to initialize default/empty rows.
1449 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001450 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001451 this.clearHome(screen);
1452 screen.saveCursorAndState(this.vt);
1453 };
1454 resetScreen(this.primaryScreen_);
1455 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001456
Mike Frysinger84301d02017-11-29 13:28:46 -08001457 // Reset terminal options to their default values.
1458 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001459 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1460
Mike Frysinger84301d02017-11-29 13:28:46 -08001461 this.setVTScrollRegion(null, null);
1462
1463 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001464};
1465
rgindac9bc5502012-01-18 11:48:44 -08001466/**
1467 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001468 *
1469 * Perform a soft reset to the default values listed in
1470 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001471 */
rginda0f5c0292012-01-13 11:00:13 -08001472hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001473 this.vt.reset();
1474
rgindab8bc8932012-04-27 12:45:03 -07001475 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001476 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001477
Brad Townb62dfdc2015-03-16 19:07:15 -07001478 // We show the cursor on soft reset but do not alter the blink state.
1479 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1480
Joel Hockey42dba8f2020-03-26 16:21:11 -07001481 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001482 const resetScreen = (screen) => {
1483 // Xterm also resets the color palette on soft reset, even though it doesn't
1484 // seem to be documented anywhere.
1485 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001486 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001487 screen.saveCursorAndState(this.vt);
1488 };
1489 resetScreen(this.primaryScreen_);
1490 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001491
rgindab8bc8932012-04-27 12:45:03 -07001492 // The xterm man page explicitly says this will happen on soft reset.
1493 this.setVTScrollRegion(null, null);
1494
1495 // Xterm also shows the cursor on soft reset, but does not alter the blink
1496 // state.
rgindaa19afe22012-01-25 15:40:22 -08001497 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001498};
1499
rgindac9bc5502012-01-18 11:48:44 -08001500/**
1501 * Move the cursor forward to the next tab stop, or to the last column
1502 * if no more tab stops are set.
1503 */
1504hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001505 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001506
Mike Frysingerdc727792020-04-10 01:41:13 -04001507 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001508 if (this.tabStops_[i] > column) {
1509 this.setCursorColumn(this.tabStops_[i]);
1510 return;
1511 }
1512 }
1513
David Benjamin66e954d2012-05-05 21:08:12 -04001514 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001515 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001516 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001517 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001518};
1519
rgindac9bc5502012-01-18 11:48:44 -08001520/**
1521 * Move the cursor backward to the previous tab stop, or to the first column
1522 * if no previous tab stops are set.
1523 */
1524hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001525 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001526
Mike Frysingerdc727792020-04-10 01:41:13 -04001527 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001528 if (this.tabStops_[i] < column) {
1529 this.setCursorColumn(this.tabStops_[i]);
1530 return;
1531 }
1532 }
1533
1534 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001535};
1536
rgindac9bc5502012-01-18 11:48:44 -08001537/**
1538 * Set a tab stop at the given column.
1539 *
Joel Hockey0f933582019-08-27 18:01:51 -07001540 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001541 */
1542hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001543 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001544 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001545 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001546 }
rgindac9bc5502012-01-18 11:48:44 -08001547
1548 if (this.tabStops_[i] < column) {
1549 this.tabStops_.splice(i + 1, 0, column);
1550 return;
1551 }
1552 }
1553
1554 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001555};
1556
rgindac9bc5502012-01-18 11:48:44 -08001557/**
1558 * Clear the tab stop at the current cursor position.
1559 *
1560 * No effect if there is no tab stop at the current cursor position.
1561 */
1562hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001563 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001564
Mike Frysingerdc727792020-04-10 01:41:13 -04001565 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001566 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001567 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001568 }
rgindac9bc5502012-01-18 11:48:44 -08001569
1570 this.tabStops_.splice(i, 1);
1571};
1572
1573/**
1574 * Clear all tab stops.
1575 */
1576hterm.Terminal.prototype.clearAllTabStops = function() {
1577 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001578 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001579};
1580
1581/**
1582 * Set up the default tab stops, starting from a given column.
1583 *
1584 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001585 * from the specified column, or 0 if no column is provided. It also flags
1586 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001587 *
1588 * This does not clear the existing tab stops first, use clearAllTabStops
1589 * for that.
1590 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001591 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001592 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001593 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001594hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001595 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001596 // Round start up to a default tab stop.
1597 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001598 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001599 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001600 }
David Benjamin66e954d2012-05-05 21:08:12 -04001601
1602 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001603};
1604
rginda6d397402012-01-17 10:58:29 -08001605/**
rginda8ba33642011-12-14 12:31:31 -08001606 * Interpret a sequence of characters.
1607 *
1608 * Incomplete escape sequences are buffered until the next call.
1609 *
1610 * @param {string} str Sequence of characters to interpret or pass through.
1611 */
1612hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001613 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001614 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001615};
1616
1617/**
1618 * Take over the given DIV for use as the terminal display.
1619 *
Joel Hockey0f933582019-08-27 18:01:51 -07001620 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001621 */
1622hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001623 const charset = div.ownerDocument.characterSet.toLowerCase();
1624 if (charset != 'utf-8') {
1625 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1626 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1627 }
1628
rginda87b86462011-12-14 13:48:03 -08001629 this.div_ = div;
1630
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001631 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1632
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001633 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1634};
1635
1636/**
1637 * Initialisation of ScrollPort properties which need to be set after its DOM
1638 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001639 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001640 * @private
1641 */
1642hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001643 this.scrollPort_.setBackgroundImage(
1644 this.prefs_.getString('background-image'));
1645 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001646 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001647 this.prefs_.getString('background-position'));
1648 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1649 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1650 this.scrollPort_.setAccessibilityReader(
1651 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001652
rginda0918b652012-04-04 11:26:24 -07001653 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001654
Joel Hockeyd4fca732019-09-20 16:57:03 -07001655 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001656 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001657
Joel Hockeyd4fca732019-09-20 16:57:03 -07001658 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001659 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001660 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001661
rginda8ba33642011-12-14 12:31:31 -08001662 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001663 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001664
Evan Jones5f9df812016-12-06 09:38:58 -05001665 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001666 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001667
Mike Frysingerdc727792020-04-10 01:41:13 -04001668 const onMouse = this.onMouse_.bind(this);
1669 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001670 screenNode.addEventListener(
1671 'mousedown', /** @type {!EventListener} */ (onMouse));
1672 screenNode.addEventListener(
1673 'mouseup', /** @type {!EventListener} */ (onMouse));
1674 screenNode.addEventListener(
1675 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001676 this.scrollPort_.onScrollWheel = onMouse;
1677
Joel Hockeyd4fca732019-09-20 16:57:03 -07001678 screenNode.addEventListener(
1679 'keydown',
1680 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001681
Toni Barzic0bfa8922013-11-22 11:18:35 -08001682 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001683 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001684 // Listen for mousedown events on the screenNode as in FF the focus
1685 // events don't bubble.
1686 screenNode.addEventListener('mousedown', function() {
1687 setTimeout(this.onFocusChange_.bind(this, true));
1688 }.bind(this));
1689
Toni Barzic0bfa8922013-11-22 11:18:35 -08001690 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001691 'blur', this.onFocusChange_.bind(this, false));
1692
Mike Frysingerdc727792020-04-10 01:41:13 -04001693 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001694 style.textContent = `
1695.cursor-node[focus="false"] {
1696 box-sizing: border-box;
1697 background-color: transparent !important;
1698 border-width: 2px;
1699 border-style: solid;
1700}
1701menu {
1702 margin: 0;
1703 padding: 0;
1704 cursor: var(--hterm-mouse-cursor-pointer);
1705}
1706menuitem {
1707 white-space: nowrap;
1708 border-bottom: 1px dashed;
1709 display: block;
1710 padding: 0.3em 0.3em 0 0.3em;
1711}
1712menuitem.separator {
1713 border-bottom: none;
1714 height: 0.5em;
1715 padding: 0;
1716}
1717menuitem:hover {
1718 color: var(--hterm-cursor-color);
1719}
1720.wc-node {
1721 display: inline-block;
1722 text-align: center;
1723 width: calc(var(--hterm-charsize-width) * 2);
1724 line-height: var(--hterm-charsize-height);
1725}
1726:root {
1727 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1728 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001729 --hterm-blink-node-duration: 0.7s;
1730 --hterm-mouse-cursor-default: default;
1731 --hterm-mouse-cursor-text: text;
1732 --hterm-mouse-cursor-pointer: pointer;
1733 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001734 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001735
Joel Hockey42dba8f2020-03-26 16:21:11 -07001736${lib.colors.stockColorPalette.map((c, i) => `
1737 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1738`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001739}
1740.uri-node:hover {
1741 text-decoration: underline;
1742 cursor: var(--hterm-mouse-cursor-pointer);
1743}
1744@keyframes blink {
1745 from { opacity: 1.0; }
1746 to { opacity: 0.0; }
1747}
1748.blink-node {
1749 animation-name: blink;
1750 animation-duration: var(--hterm-blink-node-duration);
1751 animation-iteration-count: infinite;
1752 animation-timing-function: ease-in-out;
1753 animation-direction: alternate;
1754}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001755 // Insert this stock style as the first node so that any user styles will
1756 // override w/out having to use !important everywhere. The rules above mix
1757 // runtime variables with default ones designed to be overridden by the user,
1758 // but we can wait for a concrete case from the users to determine the best
1759 // way to split the sheet up to before & after the user-css settings.
1760 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001761
rginda8ba33642011-12-14 12:31:31 -08001762 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001763 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001764 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001765 this.cursorNode_.style.cssText = `
1766position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001767left: calc(var(--hterm-screen-padding-size) +
1768 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1769top: calc(var(--hterm-screen-padding-size) +
1770 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001771display: ${this.options_.cursorVisible ? '' : 'none'};
1772width: var(--hterm-charsize-width);
1773height: var(--hterm-charsize-height);
1774background-color: var(--hterm-cursor-color);
1775border-color: var(--hterm-cursor-color);
1776-webkit-transition: opacity, background-color 100ms linear;
1777-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001778
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001779 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001780 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1781 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001782
rginda8ba33642011-12-14 12:31:31 -08001783 this.document_.body.appendChild(this.cursorNode_);
1784
rgindad5613292012-06-19 15:40:37 -07001785 // When 'enableMouseDragScroll' is off we reposition this element directly
1786 // under the mouse cursor after a click. This makes Chrome associate
1787 // subsequent mousemove events with the scroll-blocker. Since the
1788 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1789 // events do not cause the scrollport to scroll.
1790 //
1791 // It's a hack, but it's the cleanest way I could find.
1792 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001793 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001794 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001795 this.scrollBlockerNode_.style.cssText =
1796 ('position: absolute;' +
1797 'top: -99px;' +
1798 'display: block;' +
1799 'width: 10px;' +
1800 'height: 10px;');
1801 this.document_.body.appendChild(this.scrollBlockerNode_);
1802
rgindad5613292012-06-19 15:40:37 -07001803 this.scrollPort_.onScrollWheel = onMouse;
1804 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1805 ].forEach(function(event) {
1806 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001807 this.cursorNode_.addEventListener(
1808 event, /** @type {!EventListener} */ (onMouse));
1809 this.document_.addEventListener(
1810 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001811 }.bind(this));
1812
1813 this.cursorNode_.addEventListener('mousedown', function() {
1814 setTimeout(this.focus.bind(this));
1815 }.bind(this));
1816
rginda8ba33642011-12-14 12:31:31 -08001817 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001818
rginda87b86462011-12-14 13:48:03 -08001819 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001820 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001821};
1822
rginda0918b652012-04-04 11:26:24 -07001823/**
1824 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001825 *
Joel Hockey0f933582019-08-27 18:01:51 -07001826 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001827 */
rginda87b86462011-12-14 13:48:03 -08001828hterm.Terminal.prototype.getDocument = function() {
1829 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001830};
1831
1832/**
rginda0918b652012-04-04 11:26:24 -07001833 * Focus the terminal.
1834 */
1835hterm.Terminal.prototype.focus = function() {
1836 this.scrollPort_.focus();
1837};
1838
1839/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001840 * Unfocus the terminal.
1841 */
1842hterm.Terminal.prototype.blur = function() {
1843 this.scrollPort_.blur();
1844};
1845
1846/**
rginda8ba33642011-12-14 12:31:31 -08001847 * Return the HTML Element for a given row index.
1848 *
1849 * This is a method from the RowProvider interface. The ScrollPort uses
1850 * it to fetch rows on demand as they are scrolled into view.
1851 *
1852 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1853 * pairs to conserve memory.
1854 *
Joel Hockey0f933582019-08-27 18:01:51 -07001855 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001856 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001857 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001858 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001859 * @override
rginda8ba33642011-12-14 12:31:31 -08001860 */
1861hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001862 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001863 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001864 }
rginda8ba33642011-12-14 12:31:31 -08001865
Mike Frysingerdc727792020-04-10 01:41:13 -04001866 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001867 return this.screen_.rowsArray[screenIndex];
1868};
1869
1870/**
1871 * Return the text content for a given range of rows.
1872 *
1873 * This is a method from the RowProvider interface. The ScrollPort uses
1874 * it to fetch text content on demand when the user attempts to copy their
1875 * selection to the clipboard.
1876 *
Joel Hockey0f933582019-08-27 18:01:51 -07001877 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001878 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001879 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001880 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001881 * relative to the start of the scrollback buffer.
1882 * @return {string} A single string containing the text value of the range of
1883 * rows. Lines will be newline delimited, with no trailing newline.
1884 */
1885hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001886 const ary = [];
1887 for (let i = start; i < end; i++) {
1888 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001889 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001890 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001891 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001892 }
rginda8ba33642011-12-14 12:31:31 -08001893 }
1894
rgindaa09e7332012-08-17 12:49:51 -07001895 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001896};
1897
1898/**
1899 * Return the text content for a given row.
1900 *
1901 * This is a method from the RowProvider interface. The ScrollPort uses
1902 * it to fetch text content on demand when the user attempts to copy their
1903 * selection to the clipboard.
1904 *
Joel Hockey0f933582019-08-27 18:01:51 -07001905 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001906 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001907 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001908 * @return {string} A string containing the text value of the selected row.
1909 */
1910hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001911 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001912 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001913};
1914
1915/**
1916 * Return the total number of rows in the addressable screen and in the
1917 * scrollback buffer of this terminal.
1918 *
1919 * This is a method from the RowProvider interface. The ScrollPort uses
1920 * it to compute the size of the scrollbar.
1921 *
Joel Hockey0f933582019-08-27 18:01:51 -07001922 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001923 * @override
rginda8ba33642011-12-14 12:31:31 -08001924 */
1925hterm.Terminal.prototype.getRowCount = function() {
1926 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1927};
1928
1929/**
1930 * Create DOM nodes for new rows and append them to the end of the terminal.
1931 *
1932 * This is the only correct way to add a new DOM node for a row. Notice that
1933 * the new row is appended to the bottom of the list of rows, and does not
1934 * require renumbering (of the rowIndex property) of previous rows.
1935 *
1936 * If you think you want a new blank row somewhere in the middle of the
1937 * terminal, look into moveRows_().
1938 *
1939 * This method does not pay attention to vtScrollTop/Bottom, since you should
1940 * be using moveRows() in cases where they would matter.
1941 *
1942 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001943 *
1944 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001945 */
1946hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001947 let cursorRow = this.screen_.rowsArray.length;
1948 const offset = this.scrollbackRows_.length + cursorRow;
1949 for (let i = 0; i < count; i++) {
1950 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08001951 row.appendChild(this.document_.createTextNode(''));
1952 row.rowIndex = offset + i;
1953 this.screen_.pushRow(row);
1954 }
1955
Mike Frysingerdc727792020-04-10 01:41:13 -04001956 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001957 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001958 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08001959 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001960 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07001961 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04001962 }
rginda8ba33642011-12-14 12:31:31 -08001963 }
1964
Mike Frysingerbdb34802020-04-07 03:47:32 -04001965 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08001966 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001967 }
rginda8ba33642011-12-14 12:31:31 -08001968
rginda87b86462011-12-14 13:48:03 -08001969 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001970};
1971
1972/**
1973 * Relocate rows from one part of the addressable screen to another.
1974 *
1975 * This is used to recycle rows during VT scrolls (those which are driven
1976 * by VT commands, rather than by the user manipulating the scrollbar.)
1977 *
1978 * In this case, the blank lines scrolled into the scroll region are made of
1979 * the nodes we scrolled off. These have their rowIndex properties carefully
1980 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001981 *
1982 * @param {number} fromIndex The start index.
1983 * @param {number} count The number of rows to move.
1984 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001985 */
1986hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001987 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08001988 this.screen_.insertRows(toIndex, ary);
1989
Mike Frysingerdc727792020-04-10 01:41:13 -04001990 let start, end;
rginda8ba33642011-12-14 12:31:31 -08001991 if (fromIndex < toIndex) {
1992 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001993 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001994 } else {
1995 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001996 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001997 }
1998
1999 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002000 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002001};
2002
2003/**
2004 * Renumber the rowIndex property of the given range of rows.
2005 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002006 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002007 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002008 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002009 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002010 *
2011 * @param {number} start The start index.
2012 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002013 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002014 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002015hterm.Terminal.prototype.renumberRows_ = function(
2016 start, end, screen = undefined) {
2017 if (!screen) {
2018 screen = this.screen_;
2019 }
Robert Ginda40932892012-12-10 17:26:40 -08002020
Mike Frysingerdc727792020-04-10 01:41:13 -04002021 const offset = this.scrollbackRows_.length;
2022 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002023 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002024 }
2025};
2026
2027/**
2028 * Print a string to the terminal.
2029 *
2030 * This respects the current insert and wraparound modes. It will add new lines
2031 * to the end of the terminal, scrolling off the top into the scrollback buffer
2032 * if necessary.
2033 *
2034 * The string is *not* parsed for escape codes. Use the interpret() method if
2035 * that's what you're after.
2036 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002037 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002038 */
2039hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002040 this.scheduleSyncCursorPosition_();
2041
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002042 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002043 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002044
Mike Frysingerdc727792020-04-10 01:41:13 -04002045 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002046
Mike Frysingerdc727792020-04-10 01:41:13 -04002047 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002048 // Fun edge case: If the string only contains zero width codepoints (like
2049 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002050 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002051 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002052 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002053
2054 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002055 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2056 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002057 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002058 }
rgindaa19afe22012-01-25 15:40:22 -08002059
Mike Frysingerdc727792020-04-10 01:41:13 -04002060 let count = strWidth - startOffset;
2061 let didOverflow = false;
2062 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002063
rgindaa9abdd82012-08-06 18:05:09 -07002064 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2065 didOverflow = true;
2066 count = this.screenSize.width - this.screen_.cursorPosition.column;
2067 }
rgindaa19afe22012-01-25 15:40:22 -08002068
rgindaa9abdd82012-08-06 18:05:09 -07002069 if (didOverflow && !this.options_.wraparound) {
2070 // If the string overflowed the line but wraparound is off, then the
2071 // last printed character should be the last of the string.
2072 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002073 substr = lib.wc.substr(str, startOffset, count - 1) +
2074 lib.wc.substr(str, strWidth - 1);
2075 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002076 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002077 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002078 }
rgindaa19afe22012-01-25 15:40:22 -08002079
Mike Frysingerdc727792020-04-10 01:41:13 -04002080 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2081 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002082 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2083 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002084
2085 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002086 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002087 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002088 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002089 }
2090 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002091 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002092 }
2093
2094 this.screen_.maybeClipCurrentRow();
2095 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002096 }
rginda8ba33642011-12-14 12:31:31 -08002097
Mike Frysingerbdb34802020-04-07 03:47:32 -04002098 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002099 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002100 }
rginda8ba33642011-12-14 12:31:31 -08002101};
2102
2103/**
rginda87b86462011-12-14 13:48:03 -08002104 * Set the VT scroll region.
2105 *
rginda87b86462011-12-14 13:48:03 -08002106 * This also resets the cursor position to the absolute (0, 0) position, since
2107 * that's what xterm appears to do.
2108 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002109 * Setting the scroll region to the full height of the terminal will clear
2110 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2111 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2112 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2113 * continue to work as most users would expect.
2114 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002115 * @param {?number} scrollTop The zero-based top of the scroll region.
2116 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002117 * inclusive.
2118 */
2119hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002120 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002121 this.vtScrollTop_ = null;
2122 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002123 } else {
2124 this.vtScrollTop_ = scrollTop;
2125 this.vtScrollBottom_ = scrollBottom;
2126 }
rginda87b86462011-12-14 13:48:03 -08002127};
2128
2129/**
rginda8ba33642011-12-14 12:31:31 -08002130 * Return the top row index according to the VT.
2131 *
2132 * This will return 0 unless the terminal has been told to restrict scrolling
2133 * to some lower row. It is used for some VT cursor positioning and scrolling
2134 * commands.
2135 *
Joel Hockey0f933582019-08-27 18:01:51 -07002136 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002137 */
2138hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002139 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002140 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002141 }
rginda8ba33642011-12-14 12:31:31 -08002142
2143 return 0;
rginda87b86462011-12-14 13:48:03 -08002144};
rginda8ba33642011-12-14 12:31:31 -08002145
2146/**
2147 * Return the bottom row index according to the VT.
2148 *
2149 * This will return the height of the terminal unless the it has been told to
2150 * restrict scrolling to some higher row. It is used for some VT cursor
2151 * positioning and scrolling commands.
2152 *
Joel Hockey0f933582019-08-27 18:01:51 -07002153 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002154 */
2155hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002156 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002157 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002158 }
rginda8ba33642011-12-14 12:31:31 -08002159
rginda87b86462011-12-14 13:48:03 -08002160 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002161};
rginda8ba33642011-12-14 12:31:31 -08002162
2163/**
2164 * Process a '\n' character.
2165 *
2166 * If the cursor is on the final row of the terminal this will append a new
2167 * blank row to the screen and scroll the topmost row into the scrollback
2168 * buffer.
2169 *
2170 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002171 *
2172 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2173 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002174 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002175hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002176 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002177 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002178 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002179
Mike Frysingerdc727792020-04-10 01:41:13 -04002180 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2181 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002182
2183 if (this.vtScrollBottom_ != null) {
2184 // A VT Scroll region is active, we never append new rows.
2185 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2186 // We're at the end of the VT Scroll Region, perform a VT scroll.
2187 this.vtScrollUp(1);
2188 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2189 } else if (cursorAtEndOfScreen) {
2190 // We're at the end of the screen, the only thing to do is put the
2191 // cursor to column 0.
2192 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2193 } else {
2194 // Anywhere else, advance the cursor row, and reset the column.
2195 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2196 }
2197 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002198 // We're at the end of the screen. Append a new row to the terminal,
2199 // shifting the top row into the scrollback.
2200 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002201 } else {
rginda87b86462011-12-14 13:48:03 -08002202 // Anywhere else in the screen just moves the cursor.
2203 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002204 }
2205};
2206
2207/**
2208 * Like newLine(), except maintain the cursor column.
2209 */
2210hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002211 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002212 this.newLine();
2213 this.setCursorColumn(column);
2214};
2215
2216/**
rginda87b86462011-12-14 13:48:03 -08002217 * If autoCarriageReturn is set then newLine(), else lineFeed().
2218 */
2219hterm.Terminal.prototype.formFeed = function() {
2220 if (this.options_.autoCarriageReturn) {
2221 this.newLine();
2222 } else {
2223 this.lineFeed();
2224 }
2225};
2226
2227/**
2228 * Move the cursor up one row, possibly inserting a blank line.
2229 *
2230 * The cursor column is not changed.
2231 */
2232hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002233 const scrollTop = this.getVTScrollTop();
2234 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002235
2236 if (currentRow == scrollTop) {
2237 this.insertLines(1);
2238 } else {
2239 this.setAbsoluteCursorRow(currentRow - 1);
2240 }
2241};
2242
2243/**
rginda8ba33642011-12-14 12:31:31 -08002244 * Replace all characters to the left of the current cursor with the space
2245 * character.
2246 *
2247 * TODO(rginda): This should probably *remove* the characters (not just replace
2248 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002249 * position.
rginda8ba33642011-12-14 12:31:31 -08002250 */
2251hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002252 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002253 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002254 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002255 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002256 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002257};
2258
2259/**
David Benjamin684a9b72012-05-01 17:19:58 -04002260 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002261 *
2262 * The cursor position is unchanged.
2263 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002264 * If the current background color is not the default background color this
2265 * will insert spaces rather than delete. This is unfortunate because the
2266 * trailing space will affect text selection, but it's difficult to come up
2267 * with a way to style empty space that wouldn't trip up the hterm.Screen
2268 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002269 *
2270 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2271 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2272 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002273 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002274 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002275 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002276hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002277 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002278 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002279 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002280
Mike Frysingerdc727792020-04-10 01:41:13 -04002281 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002282 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002283
2284 if (this.screen_.textAttributes.background ===
2285 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002286 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002287 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002288 this.screen_.cursorPosition.column + count) {
2289 this.screen_.deleteChars(count);
2290 this.clearCursorOverflow();
2291 return;
2292 }
2293 }
2294
Mike Frysingerdc727792020-04-10 01:41:13 -04002295 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002296 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002297 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002298 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002299};
2300
2301/**
2302 * Erase the current line.
2303 *
2304 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002305 */
2306hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002307 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002308 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002309 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002310 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002311};
2312
2313/**
David Benjamina08d78f2012-05-05 00:28:49 -04002314 * Erase all characters from the start of the screen to the current cursor
2315 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002316 *
2317 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002318 */
2319hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002320 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002321
2322 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002323
Mike Frysingerdc727792020-04-10 01:41:13 -04002324 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002325 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002326 this.screen_.clearCursorRow();
2327 }
2328
rginda87b86462011-12-14 13:48:03 -08002329 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002330 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002331};
2332
2333/**
2334 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002335 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002336 *
2337 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002338 */
2339hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002340 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002341
2342 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002343
Mike Frysingerdc727792020-04-10 01:41:13 -04002344 const bottom = this.screenSize.height - 1;
2345 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002346 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002347 this.screen_.clearCursorRow();
2348 }
2349
rginda87b86462011-12-14 13:48:03 -08002350 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002351 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002352};
2353
2354/**
2355 * Fill the terminal with a given character.
2356 *
2357 * This methods does not respect the VT scroll region.
2358 *
2359 * @param {string} ch The character to use for the fill.
2360 */
2361hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002362 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002363
2364 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002365 for (let row = 0; row < this.screenSize.height; row++) {
2366 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002367 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002368 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002369 }
2370 }
2371
2372 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002373};
2374
2375/**
rginda9ea433c2012-03-16 11:57:00 -07002376 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002377 *
rginda9ea433c2012-03-16 11:57:00 -07002378 * This does not respect the scroll region.
2379 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002380 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002381 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002382 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002383hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2384 if (!screen) {
2385 screen = this.screen_;
2386 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002387 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002388
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002389 this.accessibilityReader_.clear();
2390
rginda11057d52012-04-25 12:29:56 -07002391 if (bottom == 0) {
2392 // Empty screen, nothing to do.
2393 return;
2394 }
2395
Mike Frysingerdc727792020-04-10 01:41:13 -04002396 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002397 screen.setCursorPosition(i, 0);
2398 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002399 }
2400
rginda9ea433c2012-03-16 11:57:00 -07002401 screen.setCursorPosition(0, 0);
2402};
2403
2404/**
2405 * Erase the entire display without changing the cursor position.
2406 *
2407 * The cursor position is unchanged. This does not respect the scroll
2408 * region.
2409 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002410 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002411 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002412 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002413hterm.Terminal.prototype.clear = function(screen = undefined) {
2414 if (!screen) {
2415 screen = this.screen_;
2416 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002417 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002418 this.clearHome(screen);
2419 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002420};
2421
2422/**
2423 * VT command to insert lines at the current cursor row.
2424 *
2425 * This respects the current scroll region. Rows pushed off the bottom are
2426 * lost (they won't show up in the scrollback buffer).
2427 *
Joel Hockey0f933582019-08-27 18:01:51 -07002428 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002429 */
2430hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002431 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002432
Mike Frysingerdc727792020-04-10 01:41:13 -04002433 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002434 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002435
Robert Ginda579186b2012-09-26 11:40:04 -07002436 // The moveCount is the number of rows we need to relocate to make room for
2437 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002438 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002439 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002440 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002441 }
rginda8ba33642011-12-14 12:31:31 -08002442
Mike Frysingerdc727792020-04-10 01:41:13 -04002443 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002444 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002445 this.screen_.clearCursorRow();
2446 }
rginda8ba33642011-12-14 12:31:31 -08002447};
2448
2449/**
2450 * VT command to delete lines at the current cursor row.
2451 *
2452 * New rows are added to the bottom of scroll region to take their place. New
2453 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002454 *
2455 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002456 */
2457hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002458 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002459
Mike Frysingerdc727792020-04-10 01:41:13 -04002460 const top = cursor.row;
2461 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002462
Mike Frysingerdc727792020-04-10 01:41:13 -04002463 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002464 count = Math.min(count, maxCount);
2465
Mike Frysingerdc727792020-04-10 01:41:13 -04002466 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002467 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002468 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002469 }
rginda8ba33642011-12-14 12:31:31 -08002470
Mike Frysingerdc727792020-04-10 01:41:13 -04002471 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002472 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002473 this.screen_.clearCursorRow();
2474 }
2475
rginda87b86462011-12-14 13:48:03 -08002476 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002477 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002478};
2479
2480/**
2481 * Inserts the given number of spaces at the current cursor position.
2482 *
rginda87b86462011-12-14 13:48:03 -08002483 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002484 *
2485 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002486 */
2487hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002488 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002489
Mike Frysinger73e56462019-07-17 00:23:46 -05002490 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002491 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002492 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002493
2494 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002495 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002496};
2497
2498/**
2499 * Forward-delete the specified number of characters starting at the cursor
2500 * position.
2501 *
Joel Hockey0f933582019-08-27 18:01:51 -07002502 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002503 */
2504hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002505 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002506 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002507 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002508 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002509 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002510 this.restoreCursor(cursor);
2511 }
2512
David Benjamin54e8bf62012-06-01 22:31:40 -04002513 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002514};
2515
2516/**
2517 * Shift rows in the scroll region upwards by a given number of lines.
2518 *
2519 * New rows are inserted at the bottom of the scroll region to fill the
2520 * vacated rows. The new rows not filled out with the current text attributes.
2521 *
2522 * This function does not affect the scrollback rows at all. Rows shifted
2523 * off the top are lost.
2524 *
rginda87b86462011-12-14 13:48:03 -08002525 * The cursor position is not altered.
2526 *
Joel Hockey0f933582019-08-27 18:01:51 -07002527 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002528 */
2529hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002530 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002531
rginda87b86462011-12-14 13:48:03 -08002532 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002533 this.deleteLines(count);
2534
rginda87b86462011-12-14 13:48:03 -08002535 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002536};
2537
2538/**
2539 * Shift rows below the cursor down by a given number of lines.
2540 *
2541 * This function respects the current scroll region.
2542 *
2543 * New rows are inserted at the top of the scroll region to fill the
2544 * vacated rows. The new rows not filled out with the current text attributes.
2545 *
2546 * This function does not affect the scrollback rows at all. Rows shifted
2547 * off the bottom are lost.
2548 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002549 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002550 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002551hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002552 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002553
rginda87b86462011-12-14 13:48:03 -08002554 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002555 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002556
rginda87b86462011-12-14 13:48:03 -08002557 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002558};
2559
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002560/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002561 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002562 *
2563 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002564 * cause Assitive Technology to announce the output of the terminal. It also
2565 * enables other features that aid assistive technology. All the features gated
2566 * behind this flag have a performance impact on the terminal which is why they
2567 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002568 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002569 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002570 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002571hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002572 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002573};
rginda87b86462011-12-14 13:48:03 -08002574
rginda8ba33642011-12-14 12:31:31 -08002575/**
2576 * Set the cursor position.
2577 *
2578 * The cursor row is relative to the scroll region if the terminal has
2579 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2580 *
Joel Hockey0f933582019-08-27 18:01:51 -07002581 * @param {number} row The new zero-based cursor row.
2582 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002583 */
2584hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2585 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002586 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002587 } else {
rginda87b86462011-12-14 13:48:03 -08002588 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002589 }
rginda87b86462011-12-14 13:48:03 -08002590};
rginda8ba33642011-12-14 12:31:31 -08002591
Evan Jones2600d4f2016-12-06 09:29:36 -05002592/**
2593 * Move the cursor relative to its current position.
2594 *
2595 * @param {number} row
2596 * @param {number} column
2597 */
rginda87b86462011-12-14 13:48:03 -08002598hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002599 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002600 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2601 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002602 this.screen_.setCursorPosition(row, column);
2603};
2604
Evan Jones2600d4f2016-12-06 09:29:36 -05002605/**
2606 * Move the cursor to the specified position.
2607 *
2608 * @param {number} row
2609 * @param {number} column
2610 */
rginda87b86462011-12-14 13:48:03 -08002611hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002612 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2613 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002614 this.screen_.setCursorPosition(row, column);
2615};
2616
2617/**
2618 * Set the cursor column.
2619 *
Joel Hockey0f933582019-08-27 18:01:51 -07002620 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002621 */
2622hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002623 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002624};
2625
2626/**
2627 * Return the cursor column.
2628 *
Joel Hockey0f933582019-08-27 18:01:51 -07002629 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002630 */
2631hterm.Terminal.prototype.getCursorColumn = function() {
2632 return this.screen_.cursorPosition.column;
2633};
2634
2635/**
2636 * Set the cursor row.
2637 *
2638 * The cursor row is relative to the scroll region if the terminal has
2639 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2640 *
Joel Hockey0f933582019-08-27 18:01:51 -07002641 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002642 */
rginda87b86462011-12-14 13:48:03 -08002643hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2644 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002645};
2646
2647/**
2648 * Return the cursor row.
2649 *
Joel Hockey0f933582019-08-27 18:01:51 -07002650 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002651 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002652hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002653 return this.screen_.cursorPosition.row;
2654};
2655
2656/**
2657 * Request that the ScrollPort redraw itself soon.
2658 *
2659 * The redraw will happen asynchronously, soon after the call stack winds down.
2660 * Multiple calls will be coalesced into a single redraw.
2661 */
2662hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002663 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002664 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002665 }
rginda8ba33642011-12-14 12:31:31 -08002666
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002667 this.timeouts_.redraw = setTimeout(() => {
2668 delete this.timeouts_.redraw;
2669 this.scrollPort_.redraw_();
2670 });
rginda8ba33642011-12-14 12:31:31 -08002671};
2672
2673/**
2674 * Request that the ScrollPort be scrolled to the bottom.
2675 *
2676 * The scroll will happen asynchronously, soon after the call stack winds down.
2677 * Multiple calls will be coalesced into a single scroll.
2678 *
2679 * This affects the scrollbar position of the ScrollPort, and has nothing to
2680 * do with the VT scroll commands.
2681 */
2682hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002683 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002684 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002685 }
rginda8ba33642011-12-14 12:31:31 -08002686
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002687 this.timeouts_.scrollDown = setTimeout(() => {
2688 delete this.timeouts_.scrollDown;
2689 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2690 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002691};
2692
2693/**
2694 * Move the cursor up a specified number of rows.
2695 *
Joel Hockey0f933582019-08-27 18:01:51 -07002696 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002697 */
2698hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002699 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002700};
2701
2702/**
2703 * Move the cursor down a specified number of rows.
2704 *
Joel Hockey0f933582019-08-27 18:01:51 -07002705 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002706 */
2707hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002708 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002709 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2710 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2711 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002712
Mike Frysingerdc727792020-04-10 01:41:13 -04002713 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2714 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002715 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002716};
2717
2718/**
2719 * Move the cursor left a specified number of columns.
2720 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002721 * If reverse wraparound mode is enabled and the previous row wrapped into
2722 * the current row then we back up through the wraparound as well.
2723 *
Joel Hockey0f933582019-08-27 18:01:51 -07002724 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002725 */
2726hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002727 count = count || 1;
2728
Mike Frysingerbdb34802020-04-07 03:47:32 -04002729 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002730 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002731 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002732
Mike Frysingerdc727792020-04-10 01:41:13 -04002733 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002734 if (this.options_.reverseWraparound) {
2735 if (this.screen_.cursorPosition.overflow) {
2736 // If this cursor is in the right margin, consume one count to get it
2737 // back to the last column. This only applies when we're in reverse
2738 // wraparound mode.
2739 count--;
2740 this.clearCursorOverflow();
2741
Mike Frysingerbdb34802020-04-07 03:47:32 -04002742 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002743 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002744 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002745 }
2746
Mike Frysingerdc727792020-04-10 01:41:13 -04002747 let newRow = this.screen_.cursorPosition.row;
2748 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002749 if (newColumn < 0) {
2750 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2751 if (newRow < 0) {
2752 // xterm also wraps from row 0 to the last row.
2753 newRow = this.screenSize.height + newRow % this.screenSize.height;
2754 }
2755 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2756 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002757
Robert Gindabfb32622014-07-17 13:20:27 -07002758 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2759
2760 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002761 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002762 this.setCursorColumn(newColumn);
2763 }
rginda8ba33642011-12-14 12:31:31 -08002764};
2765
2766/**
2767 * Move the cursor right a specified number of columns.
2768 *
Joel Hockey0f933582019-08-27 18:01:51 -07002769 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002770 */
2771hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002772 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002773
Mike Frysingerbdb34802020-04-07 03:47:32 -04002774 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002775 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002776 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002777
Mike Frysingerdc727792020-04-10 01:41:13 -04002778 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2779 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002780 this.setCursorColumn(column);
2781};
2782
2783/**
2784 * Reverse the foreground and background colors of the terminal.
2785 *
2786 * This only affects text that was drawn with no attributes.
2787 *
2788 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2789 * been drawn with attributes that happen to coincide with the default
2790 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002791 *
2792 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002793 */
2794hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002795 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002796 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002797 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2798 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002799 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002800 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2801 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002802 }
2803};
2804
2805/**
rginda87b86462011-12-14 13:48:03 -08002806 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002807 *
2808 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002809 */
2810hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002811 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002812
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002813 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002814
Michael Kelly485ecd12014-06-09 11:41:56 -04002815 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002816 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002817 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002818 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002819
Robert Ginda92e18102013-03-14 13:56:37 -07002820 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002821 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002822 this.bellSequelchTimeout_ = setTimeout(() => {
2823 this.bellSquelchTimeout_ = null;
2824 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002825 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002826 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002827 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002828
2829 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002830 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002831 this.bellNotificationList_.push(n);
2832 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002833 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002834 }
rginda87b86462011-12-14 13:48:03 -08002835};
2836
2837/**
rginda8ba33642011-12-14 12:31:31 -08002838 * Set the origin mode bit.
2839 *
2840 * If origin mode is on, certain VT cursor and scrolling commands measure their
2841 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2842 * to the top of the addressable screen.
2843 *
2844 * Defaults to off.
2845 *
2846 * @param {boolean} state True to set origin mode, false to unset.
2847 */
2848hterm.Terminal.prototype.setOriginMode = function(state) {
2849 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002850 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002851};
2852
2853/**
2854 * Set the insert mode bit.
2855 *
2856 * If insert mode is on, existing text beyond the cursor position will be
2857 * shifted right to make room for new text. Otherwise, new text overwrites
2858 * any existing text.
2859 *
2860 * Defaults to off.
2861 *
2862 * @param {boolean} state True to set insert mode, false to unset.
2863 */
2864hterm.Terminal.prototype.setInsertMode = function(state) {
2865 this.options_.insertMode = state;
2866};
2867
2868/**
rginda87b86462011-12-14 13:48:03 -08002869 * Set the auto carriage return bit.
2870 *
2871 * If auto carriage return is on then a formfeed character is interpreted
2872 * as a newline, otherwise it's the same as a linefeed. The difference boils
2873 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002874 *
2875 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002876 */
2877hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2878 this.options_.autoCarriageReturn = state;
2879};
2880
2881/**
rginda8ba33642011-12-14 12:31:31 -08002882 * Set the wraparound mode bit.
2883 *
2884 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2885 * to the start of the following row. Otherwise, the cursor is clamped to the
2886 * end of the screen and attempts to write past it are ignored.
2887 *
2888 * Defaults to on.
2889 *
2890 * @param {boolean} state True to set wraparound mode, false to unset.
2891 */
2892hterm.Terminal.prototype.setWraparound = function(state) {
2893 this.options_.wraparound = state;
2894};
2895
2896/**
2897 * Set the reverse-wraparound mode bit.
2898 *
2899 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2900 * to the end of the previous row. Otherwise, the cursor is clamped to column
2901 * 0.
2902 *
2903 * Defaults to off.
2904 *
2905 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2906 */
2907hterm.Terminal.prototype.setReverseWraparound = function(state) {
2908 this.options_.reverseWraparound = state;
2909};
2910
2911/**
2912 * Selects between the primary and alternate screens.
2913 *
2914 * If alternate mode is on, the alternate screen is active. Otherwise the
2915 * primary screen is active.
2916 *
2917 * Swapping screens has no effect on the scrollback buffer.
2918 *
2919 * Each screen maintains its own cursor position.
2920 *
2921 * Defaults to off.
2922 *
2923 * @param {boolean} state True to set alternate mode, false to unset.
2924 */
2925hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002926 if (state == (this.screen_ == this.alternateScreen_)) {
2927 return;
2928 }
2929 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2930 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002931 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2932
Joel Hockey42dba8f2020-03-26 16:21:11 -07002933 // Swap color overrides.
2934 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2935 oldOverrides.forEach((c, i) => {
2936 if (!newOverrides.hasOwnProperty(i)) {
2937 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2938 }
2939 });
2940 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2941
rginda35c456b2012-02-09 17:29:05 -08002942 if (this.screen_.rowsArray.length &&
2943 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2944 // If the screen changed sizes while we were away, our rowIndexes may
2945 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002946 const offset = this.scrollbackRows_.length;
2947 const ary = this.screen_.rowsArray;
2948 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002949 ary[i].rowIndex = offset + i;
2950 }
2951 }
rginda8ba33642011-12-14 12:31:31 -08002952
rginda35c456b2012-02-09 17:29:05 -08002953 this.realizeWidth_(this.screenSize.width);
2954 this.realizeHeight_(this.screenSize.height);
2955 this.scrollPort_.syncScrollHeight();
2956 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002957
rginda6d397402012-01-17 10:58:29 -08002958 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002959 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002960};
2961
2962/**
2963 * Set the cursor-blink mode bit.
2964 *
2965 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2966 * a visible cursor does not blink.
2967 *
2968 * You should make sure to turn blinking off if you're going to dispose of a
2969 * terminal, otherwise you'll leak a timeout.
2970 *
2971 * Defaults to on.
2972 *
2973 * @param {boolean} state True to set cursor-blink mode, false to unset.
2974 */
2975hterm.Terminal.prototype.setCursorBlink = function(state) {
2976 this.options_.cursorBlink = state;
2977
2978 if (!state && this.timeouts_.cursorBlink) {
2979 clearTimeout(this.timeouts_.cursorBlink);
2980 delete this.timeouts_.cursorBlink;
2981 }
2982
Mike Frysingerbdb34802020-04-07 03:47:32 -04002983 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08002984 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002985 }
rginda8ba33642011-12-14 12:31:31 -08002986};
2987
2988/**
2989 * Set the cursor-visible mode bit.
2990 *
2991 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2992 *
2993 * Defaults to on.
2994 *
2995 * @param {boolean} state True to set cursor-visible mode, false to unset.
2996 */
2997hterm.Terminal.prototype.setCursorVisible = function(state) {
2998 this.options_.cursorVisible = state;
2999
3000 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003001 if (this.timeouts_.cursorBlink) {
3002 clearTimeout(this.timeouts_.cursorBlink);
3003 delete this.timeouts_.cursorBlink;
3004 }
rginda87b86462011-12-14 13:48:03 -08003005 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003006 return;
3007 }
3008
rginda87b86462011-12-14 13:48:03 -08003009 this.syncCursorPosition_();
3010
3011 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003012
3013 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003014 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003015 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003016 }
rginda8ba33642011-12-14 12:31:31 -08003017
Robert Gindaea2183e2014-07-17 09:51:51 -07003018 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003019 } else {
3020 if (this.timeouts_.cursorBlink) {
3021 clearTimeout(this.timeouts_.cursorBlink);
3022 delete this.timeouts_.cursorBlink;
3023 }
3024 }
3025};
3026
3027/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003028 * Pause blinking temporarily.
3029 *
3030 * When the cursor moves around, it can be helpful to momentarily pause the
3031 * blinking. This could be when the user is typing in things, or when they're
3032 * moving around with the arrow keys.
3033 */
3034hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3035 if (!this.options_.cursorBlink) {
3036 return;
3037 }
3038
3039 this.cursorBlinkPause_ = true;
3040
3041 // If a timeout is already pending, reset the clock due to the new input.
3042 if (this.timeouts_.cursorBlinkPause) {
3043 clearTimeout(this.timeouts_.cursorBlinkPause);
3044 }
3045 // After 500ms, resume blinking. That seems like a good balance between user
3046 // input timings & responsiveness to resume.
3047 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3048 delete this.timeouts_.cursorBlinkPause;
3049 this.cursorBlinkPause_ = false;
3050 }, 500);
3051};
3052
3053/**
rginda87b86462011-12-14 13:48:03 -08003054 * Synchronizes the visible cursor and document selection with the current
3055 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003056 *
3057 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003058 */
3059hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003060 const topRowIndex = this.scrollPort_.getTopRowIndex();
3061 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3062 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003063 this.screen_.cursorPosition.row;
3064
Raymes Khoury15697f42018-07-17 11:37:18 +10003065 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003066 if (this.accessibilityReader_.accessibilityEnabled) {
3067 // Report the new position of the cursor for accessibility purposes.
3068 const cursorColumnIndex = this.screen_.cursorPosition.column;
3069 const cursorLineText =
3070 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003071 // This will force the selection to be sync'd to the cursor position if the
3072 // user has pressed a key. Generally we would only sync the cursor position
3073 // when selection is collapsed so that if the user has selected something
3074 // we don't clear the selection by moving the selection. However when a
3075 // screen reader is used, it's intuitive for entering a key to move the
3076 // selection to the cursor.
3077 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003078 this.accessibilityReader_.afterCursorChange(
3079 cursorLineText, cursorRowIndex, cursorColumnIndex);
3080 }
3081
rginda8ba33642011-12-14 12:31:31 -08003082 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003083 // Cursor is scrolled off screen, hide it.
3084 this.cursorOffScreen_ = true;
3085 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003086 return false;
rginda8ba33642011-12-14 12:31:31 -08003087 }
3088
Joel Hockey3babf302020-04-22 15:00:06 -07003089 if (this.cursorNode_.style.display == 'none') {
3090 // Re-display the terminal cursor if it was hidden.
3091 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003092 this.cursorNode_.style.display = '';
3093 }
3094
Mike Frysinger44c32202017-08-05 01:13:09 -04003095 // Position the cursor using CSS variable math. If we do the math in JS,
3096 // the float math will end up being more precise than the CSS which will
3097 // cause the cursor tracking to be off.
3098 this.setCssVar(
3099 'cursor-offset-row',
3100 `${cursorRowIndex - topRowIndex} + ` +
3101 `${this.scrollPort_.visibleRowTopMargin}px`);
3102 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003103
3104 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003105 '(' + this.screen_.cursorPosition.column +
3106 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003107 ')');
3108
3109 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003110 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003111 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003112 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003113 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003114 return true;
rginda8ba33642011-12-14 12:31:31 -08003115};
3116
Robert Gindafb1be6a2013-12-11 11:56:22 -08003117/**
3118 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3119 * and character cell dimensions.
3120 */
Robert Ginda830583c2013-08-07 13:20:46 -07003121hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003122 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003123
3124 if (this.cursorNode_.getAttribute('focus') == 'false') {
3125 // Always show a block cursor when unfocused.
3126 shape = hterm.Terminal.cursorShape.BLOCK;
3127 }
3128
Mike Frysingerdc727792020-04-10 01:41:13 -04003129 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003130
3131 switch (shape) {
3132 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003133 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003134 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003135 style.borderLeftStyle = 'solid';
3136 break;
3137
3138 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003139 style.backgroundColor = 'transparent';
3140 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003141 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003142 break;
3143
3144 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003145 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003146 style.borderBottomStyle = '';
3147 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003148 break;
3149 }
3150};
3151
rginda8ba33642011-12-14 12:31:31 -08003152/**
3153 * Synchronizes the visible cursor with the current cursor coordinates.
3154 *
3155 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003156 * Multiple calls will be coalesced into a single sync. This should be called
3157 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003158 */
3159hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003160 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003161 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003162 }
rginda8ba33642011-12-14 12:31:31 -08003163
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003164 if (this.accessibilityReader_.accessibilityEnabled) {
3165 // Report the previous position of the cursor for accessibility purposes.
3166 const cursorRowIndex = this.scrollbackRows_.length +
3167 this.screen_.cursorPosition.row;
3168 const cursorColumnIndex = this.screen_.cursorPosition.column;
3169 const cursorLineText =
3170 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3171 this.accessibilityReader_.beforeCursorChange(
3172 cursorLineText, cursorRowIndex, cursorColumnIndex);
3173 }
3174
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003175 this.timeouts_.syncCursor = setTimeout(() => {
3176 this.syncCursorPosition_();
3177 delete this.timeouts_.syncCursor;
3178 });
rginda87b86462011-12-14 13:48:03 -08003179};
3180
rgindacc2996c2012-02-24 14:59:31 -08003181/**
rgindaf522ce02012-04-17 17:49:17 -07003182 * Show or hide the zoom warning.
3183 *
3184 * The zoom warning is a message warning the user that their browser zoom must
3185 * be set to 100% in order for hterm to function properly.
3186 *
3187 * @param {boolean} state True to show the message, false to hide it.
3188 */
3189hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3190 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003191 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003192 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003193 }
rgindaf522ce02012-04-17 17:49:17 -07003194
3195 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003196 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003197 this.zoomWarningNode_.style.cssText = (
3198 'color: black;' +
3199 'background-color: #ff2222;' +
3200 'font-size: large;' +
3201 'border-radius: 8px;' +
3202 'opacity: 0.75;' +
3203 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3204 'top: 0.5em;' +
3205 'right: 1.2em;' +
3206 'position: absolute;' +
3207 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003208 '-webkit-user-select: none;' +
3209 '-moz-text-size-adjust: none;' +
3210 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003211
3212 this.zoomWarningNode_.addEventListener('click', function(e) {
3213 this.parentNode.removeChild(this);
3214 });
rgindaf522ce02012-04-17 17:49:17 -07003215 }
3216
Mike Frysingerb7289952019-03-23 16:05:38 -07003217 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003218 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003219 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003220
rgindaf522ce02012-04-17 17:49:17 -07003221 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3222
3223 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003224 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003225 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003226 }
rgindaf522ce02012-04-17 17:49:17 -07003227 } else if (this.zoomWarningNode_.parentNode) {
3228 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3229 }
3230};
3231
3232/**
rgindacc2996c2012-02-24 14:59:31 -08003233 * Show the terminal overlay for a given amount of time.
3234 *
3235 * The terminal overlay appears in inverse video in a large font, centered
3236 * over the terminal. You should probably keep the overlay message brief,
3237 * since it's in a large font and you probably aren't going to check the size
3238 * of the terminal first.
3239 *
3240 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003241 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003242 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3243 * stay up forever (or until the next overlay).
3244 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003245hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
rgindaf0090c92012-02-10 14:58:52 -08003246 if (!this.overlayNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003247 if (!this.div_) {
rgindaf0090c92012-02-10 14:58:52 -08003248 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003249 }
rgindaf0090c92012-02-10 14:58:52 -08003250
3251 this.overlayNode_ = this.document_.createElement('div');
3252 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003253 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003254 'font-size: xx-large;' +
3255 'opacity: 0.75;' +
3256 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3257 'position: absolute;' +
3258 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003259 '-webkit-transition: opacity 180ms ease-in;' +
3260 '-moz-user-select: none;' +
3261 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003262
3263 this.overlayNode_.addEventListener('mousedown', function(e) {
3264 e.preventDefault();
3265 e.stopPropagation();
3266 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003267 }
3268
rginda9f5222b2012-03-05 11:53:28 -08003269 this.overlayNode_.style.color = this.prefs_.get('background-color');
3270 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3271 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3272
rgindaf0090c92012-02-10 14:58:52 -08003273 this.overlayNode_.textContent = msg;
3274 this.overlayNode_.style.opacity = '0.75';
3275
Mike Frysingerbdb34802020-04-07 03:47:32 -04003276 if (!this.overlayNode_.parentNode) {
rgindaf0090c92012-02-10 14:58:52 -08003277 this.div_.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003278 }
rgindaf0090c92012-02-10 14:58:52 -08003279
Mike Frysingerdc727792020-04-10 01:41:13 -04003280 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3281 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003282
Robert Ginda8a59f762014-07-23 11:29:55 -07003283 this.overlayNode_.style.top =
3284 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003285 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003286 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003287
Mike Frysingerbdb34802020-04-07 03:47:32 -04003288 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003289 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003290 }
rgindaf0090c92012-02-10 14:58:52 -08003291
Raymes Khouryc7a06382018-07-04 10:25:45 +10003292 this.accessibilityReader_.assertiveAnnounce(msg);
3293
Mike Frysingerec4225d2020-04-07 05:00:01 -04003294 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003295 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003296 }
rgindacc2996c2012-02-24 14:59:31 -08003297
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003298 this.overlayTimeout_ = setTimeout(() => {
3299 this.overlayNode_.style.opacity = '0';
3300 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003301 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003302};
3303
3304/**
3305 * Hide the terminal overlay immediately.
3306 *
3307 * Useful when we show an overlay for an event with an unknown end time.
3308 */
3309hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003310 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003311 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003312 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003313 this.overlayTimeout_ = null;
3314
Mike Frysingerbdb34802020-04-07 03:47:32 -04003315 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003316 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003317 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003318 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003319};
3320
rginda4bba5e12012-06-20 16:15:30 -07003321/**
3322 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003323 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003324 * Note: In Chrome, this should work unless the user has rejected the permission
3325 * request. In Firefox extension environment, you'll need the "clipboardRead"
3326 * permission. In other environments, this might always fail as the browser
3327 * frequently blocks access for security reasons.
3328 *
3329 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3330 * value is always null. Otherwise, this function uses legacy pasting and
3331 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003332 */
3333hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003334 if (!this.alwaysUseLegacyPasting &&
3335 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003336 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3337 return null;
3338 } else {
3339 // Legacy pasting.
3340 try {
3341 return this.document_.execCommand('paste');
3342 } catch (firefoxException) {
3343 // Ignore this. FF 40 and older would incorrectly throw an exception if
3344 // there was an error instead of returning false.
3345 return false;
3346 }
3347 }
rginda4bba5e12012-06-20 16:15:30 -07003348};
3349
3350/**
3351 * Copy a string to the system clipboard.
3352 *
3353 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003354 *
3355 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003356 */
3357hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003358 if (this.prefs_.get('enable-clipboard-notice')) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003359 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003360 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003361
Mike Frysinger96eacae2019-01-02 18:13:56 -05003362 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003363};
3364
Evan Jones2600d4f2016-12-06 09:29:36 -05003365/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003366 * Display an image.
3367 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003368 * Either URI or buffer or blob fields must be specified.
3369 *
Joel Hockey0f933582019-08-27 18:01:51 -07003370 * @param {{
3371 * name: (string|undefined),
3372 * size: (string|number|undefined),
3373 * preserveAspectRation: (boolean|undefined),
3374 * inline: (boolean|undefined),
3375 * width: (string|number|undefined),
3376 * height: (string|number|undefined),
3377 * align: (string|undefined),
3378 * url: (string|undefined),
3379 * buffer: (!ArrayBuffer|undefined),
3380 * blob: (!Blob|undefined),
3381 * type: (string|undefined),
3382 * }} options The image to display.
3383 * name A human readable string for the image
3384 * size The size (in bytes).
3385 * preserveAspectRatio Whether to preserve aspect.
3386 * inline Whether to display the image inline.
3387 * width The width of the image.
3388 * height The height of the image.
3389 * align Direction to align the image.
3390 * uri The source URI for the image.
3391 * buffer The ArrayBuffer image data.
3392 * blob The Blob image data.
3393 * type The MIME type of the image data.
3394 * @param {function()=} onLoad Callback when loading finishes.
3395 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003396 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003397hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003398 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003399 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003400 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003401 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003402 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003403
3404 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003405 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003406 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003407 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003408
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003409 // See if the mime type is available. If not, guess from the filename.
3410 // We don't list all possible mime types because the browser can usually
3411 // guess it correctly. So list the ones that need a bit more help.
3412 if (!options.type) {
3413 const ary = options.name.split('.');
3414 const ext = ary[ary.length - 1].trim();
3415 switch (ext) {
3416 case 'svg':
3417 case 'svgz':
3418 options.type = 'image/svg+xml';
3419 break;
3420 }
3421 }
3422
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003423 // Has the user approved image display yet?
3424 if (this.allowImagesInline !== true) {
3425 this.newLine();
3426 const row = this.getRowNode(this.scrollbackRows_.length +
3427 this.getCursorRow() - 1);
3428
3429 if (this.allowImagesInline === false) {
3430 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3431 'Inline Images Disabled');
3432 return;
3433 }
3434
3435 // Show a prompt.
3436 let button;
3437 const span = this.document_.createElement('span');
3438 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3439 span.style.fontWeight = 'bold';
3440 span.style.borderWidth = '1px';
3441 span.style.borderStyle = 'dashed';
3442 button = this.document_.createElement('span');
3443 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3444 button.style.marginLeft = '1em';
3445 button.style.borderWidth = '1px';
3446 button.style.borderStyle = 'solid';
3447 button.addEventListener('click', () => {
3448 this.prefs_.set('allow-images-inline', false);
3449 });
3450 span.appendChild(button);
3451 button = this.document_.createElement('span');
3452 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3453 'allow this session');
3454 button.style.marginLeft = '1em';
3455 button.style.borderWidth = '1px';
3456 button.style.borderStyle = 'solid';
3457 button.addEventListener('click', () => {
3458 this.allowImagesInline = true;
3459 });
3460 span.appendChild(button);
3461 button = this.document_.createElement('span');
3462 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3463 button.style.marginLeft = '1em';
3464 button.style.borderWidth = '1px';
3465 button.style.borderStyle = 'solid';
3466 button.addEventListener('click', () => {
3467 this.prefs_.set('allow-images-inline', true);
3468 });
3469 span.appendChild(button);
3470
3471 row.appendChild(span);
3472 return;
3473 }
3474
3475 // See if we should show this object directly, or download it.
3476 if (options.inline) {
3477 const io = this.io.push();
3478 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003479 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003480
3481 // While we're loading the image, eat all the user's input.
3482 io.onVTKeystroke = io.sendString = () => {};
3483
3484 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003485 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003486 if (options.uri !== undefined) {
3487 img.src = options.uri;
3488 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003489 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003490 img.src = URL.createObjectURL(blob);
3491 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003492 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003493 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003494 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003495 img.title = img.alt = options.name;
3496
3497 // Attach the image to the page to let it load/render. It won't stay here.
3498 // This is needed so it's visible and the DOM can calculate the height. If
3499 // the image is hidden or not in the DOM, the height is always 0.
3500 this.document_.body.appendChild(img);
3501
3502 // Wait for the image to finish loading before we try moving it to the
3503 // right place in the terminal.
3504 img.onload = () => {
3505 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003506 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003507 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003508 img.style.maxWidth = `${screenSize.width}px`;
3509 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003510
3511 // Parse a width/height specification.
3512 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003513 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003514 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003515 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003516
3517 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3518 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003519 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003520 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003521 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003522 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003523 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003524 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003525 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003526 }
3527
3528 return '';
3529 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003530 img.style.width = parseDim(
3531 options.width, screenSize.width, '--hterm-charsize-width');
3532 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003533 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003534
3535 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003536 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003537 const padRows = Math.ceil(img.clientHeight /
3538 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003539 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003540 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003541 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003542
3543 // Update the max height in case the user shrinks the character size.
3544 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3545
3546 // Move the image to the last row. This way when we scroll up, it doesn't
3547 // disappear when the first row gets clipped. It will disappear when we
3548 // scroll down and the last row is clipped ...
3549 this.document_.body.removeChild(img);
3550 // Create a wrapper node so we can do an absolute in a relative position.
3551 // This helps with rounding errors between JS & CSS counts.
3552 const div = this.document_.createElement('div');
3553 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003554 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003555 img.style.position = 'absolute';
3556 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3557 div.appendChild(img);
3558 const row = this.getRowNode(this.scrollbackRows_.length +
3559 this.getCursorRow() - 1);
3560 row.appendChild(div);
3561
Mike Frysinger2558ed52019-01-14 01:03:41 -05003562 // Now that the image has been read, we can revoke the source.
3563 if (options.uri === undefined) {
3564 URL.revokeObjectURL(img.src);
3565 }
3566
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003567 io.hideOverlay();
3568 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003569
Mike Frysingerbdb34802020-04-07 03:47:32 -04003570 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003571 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003572 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003573 };
3574
3575 // If we got a malformed image, give up.
3576 img.onerror = (e) => {
3577 this.document_.body.removeChild(img);
3578 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003579 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003580 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003581
Mike Frysingerbdb34802020-04-07 03:47:32 -04003582 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003583 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003584 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003585 };
3586 } else {
3587 // We can't use chrome.downloads.download as that requires "downloads"
3588 // permissions, and that works only in extensions, not apps.
3589 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003590 if (options.uri !== undefined) {
3591 a.href = options.uri;
3592 } else if (options.buffer !== undefined) {
3593 const blob = new Blob([options.buffer]);
3594 a.href = URL.createObjectURL(blob);
3595 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003596 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003597 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003598 a.download = options.name;
3599 this.document_.body.appendChild(a);
3600 a.click();
3601 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003602 if (options.uri === undefined) {
3603 URL.revokeObjectURL(a.href);
3604 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003605 }
3606};
3607
3608/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003609 * Returns the selected text, or null if no text is selected.
3610 *
3611 * @return {string|null}
3612 */
rgindaa09e7332012-08-17 12:49:51 -07003613hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003614 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003615 selection.sync();
3616
Mike Frysingerbdb34802020-04-07 03:47:32 -04003617 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003618 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003619 }
rgindaa09e7332012-08-17 12:49:51 -07003620
rgindaa09e7332012-08-17 12:49:51 -07003621 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003622 let startOffset = selection.startOffset;
3623 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003624
Raymes Khoury334625a2018-06-25 10:29:40 +10003625 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003626 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003627 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003628 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003629
Robert Gindafdbb3f22012-09-06 20:23:06 -07003630 if (node.nodeName != 'X-ROW') {
3631 // If the selection doesn't start on an x-row node, then it must be
3632 // somewhere inside the x-row. Add any characters from previous siblings
3633 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003634
3635 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3636 // If node is the text node in a styled span, move up to the span node.
3637 node = node.parentNode;
3638 }
3639
Robert Gindafdbb3f22012-09-06 20:23:06 -07003640 while (node.previousSibling) {
3641 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003642 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003643 }
rgindaa09e7332012-08-17 12:49:51 -07003644 }
3645
3646 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003647 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003648 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003649 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003650
Robert Gindafdbb3f22012-09-06 20:23:06 -07003651 if (node.nodeName != 'X-ROW') {
3652 // If the selection doesn't end on an x-row node, then it must be
3653 // somewhere inside the x-row. Add any characters from following siblings
3654 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003655
3656 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3657 // If node is the text node in a styled span, move up to the span node.
3658 node = node.parentNode;
3659 }
3660
Robert Gindafdbb3f22012-09-06 20:23:06 -07003661 while (node.nextSibling) {
3662 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003663 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003664 }
rgindaa09e7332012-08-17 12:49:51 -07003665 }
3666
Mike Frysingerdc727792020-04-10 01:41:13 -04003667 const rv = this.getRowsText(selection.startRow.rowIndex,
3668 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003669 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003670};
3671
rginda4bba5e12012-06-20 16:15:30 -07003672/**
3673 * Copy the current selection to the system clipboard, then clear it after a
3674 * short delay.
3675 */
3676hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003677 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003678 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003679 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 }
rginda4bba5e12012-06-20 16:15:30 -07003681};
3682
Joel Hockey0f933582019-08-27 18:01:51 -07003683/**
3684 * Show overlay with current terminal size.
3685 */
rgindaf0090c92012-02-10 14:58:52 -08003686hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003687 if (this.prefs_.get('enable-resize-status')) {
3688 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3689 }
rgindaf0090c92012-02-10 14:58:52 -08003690};
3691
rginda87b86462011-12-14 13:48:03 -08003692/**
3693 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3694 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003695 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003696 */
3697hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003698 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003699 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003700 }
rginda87b86462011-12-14 13:48:03 -08003701
Mike Frysinger225c99d2019-10-20 14:02:37 -06003702 this.pauseCursorBlink_();
3703
Mike Frysinger79669762018-12-30 20:51:10 -05003704 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003705};
3706
3707/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003708 * Open the selected url.
3709 */
3710hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003711 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003712
3713 // If there is no selection, try and expand wherever they clicked.
3714 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003715 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003716 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003717
3718 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003719 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003720 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003721 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003722 }
3723
3724 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003725 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003726 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003727 }
Mike Frysinger43472622017-06-26 18:11:07 -04003728
3729 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003730 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003731 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3732 // We have to whitelist a few protocols that lack authorities and thus
3733 // never use the //. Like mailto.
3734 switch (str.split(':', 1)[0]) {
3735 case 'mailto':
3736 break;
3737 default:
3738 str = 'http://' + str;
3739 break;
3740 }
3741 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003742
Mike Frysinger720fa832017-10-23 01:15:52 -04003743 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003744};
Mike Frysinger70b94692017-01-26 18:57:50 -10003745
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003746/**
3747 * Manage the automatic mouse hiding behavior while typing.
3748 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003749 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003750 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003751hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003752 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3753 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003754 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003755 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003756 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003757
3758 this.mouseHideWhileTyping_ = !!v;
3759};
3760
3761/**
3762 * Handler for monitoring user keyboard activity.
3763 *
3764 * This isn't for processing the keystrokes directly, but for updating any
3765 * state that might toggle based on the user using the keyboard at all.
3766 *
Joel Hockey0f933582019-08-27 18:01:51 -07003767 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003768 */
3769hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3770 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003771 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003772 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003773 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003774};
Mike Frysinger70b94692017-01-26 18:57:50 -10003775
3776/**
rgindad5613292012-06-19 15:40:37 -07003777 * Add the terminalRow and terminalColumn properties to mouse events and
3778 * then forward on to onMouse().
3779 *
3780 * The terminalRow and terminalColumn properties contain the (row, column)
3781 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003782 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003783 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003784 */
3785hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003786 if (e.processedByTerminalHandler_) {
3787 // We register our event handlers on the document, as well as the cursor
3788 // and the scroll blocker. Mouse events that occur on the cursor or
3789 // scroll blocker will also appear on the document, but we don't want to
3790 // process them twice.
3791 //
3792 // We can't just prevent bubbling because that has other side effects, so
3793 // we decorate the event object with this property instead.
3794 return;
3795 }
3796
Mike Frysinger468966c2018-08-28 13:48:51 -04003797 // Consume navigation events. Button 3 is usually "browser back" and
3798 // button 4 is "browser forward" which we don't want to happen.
3799 if (e.button > 2) {
3800 e.preventDefault();
3801 // We don't return so click events can be passed to the remote below.
3802 }
3803
Mike Frysingerdc727792020-04-10 01:41:13 -04003804 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003805 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3806
rgindafaa74742012-08-21 13:34:03 -07003807 e.processedByTerminalHandler_ = true;
3808
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003809 // Handle auto hiding of mouse cursor while typing.
3810 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3811 // Make sure the mouse cursor is visible.
3812 this.syncMouseStyle();
3813 // This debounce isn't perfect, but should work well enough for such a
3814 // simple implementation. If the user moved the mouse, we enabled this
3815 // debounce, and then moved the mouse just before the timeout, we wouldn't
3816 // debounce that later movement.
3817 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3818 }
3819
Robert Gindaeda48db2014-07-17 09:25:30 -07003820 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003821 e.terminalRow = Math.floor(
3822 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3823 this.scrollPort_.characterSize.height) + 1;
3824 e.terminalColumn = Math.floor(
3825 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003826
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003827 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3828 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003829 return;
3830 }
3831
Joel Hockey3babf302020-04-22 15:00:06 -07003832 if (this.options_.cursorVisible && !reportMouseEvents &&
3833 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003834 // If the cursor is visible and we're not sending mouse events to the
3835 // host app, then we want to hide the terminal cursor when the mouse
3836 // cursor is over top. This keeps the terminal cursor from interfering
3837 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003838 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3839 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3840 this.cursorNode_.style.display = 'none';
3841 } else if (this.cursorNode_.style.display == 'none') {
3842 this.cursorNode_.style.display = '';
3843 }
3844 }
rgindad5613292012-06-19 15:40:37 -07003845
Robert Ginda928cf632014-03-05 15:07:41 -08003846 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003847 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003848
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003849 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003850 // If VT mouse reporting is disabled, or has been defeated with
3851 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003852 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003853 this.setSelectionEnabled(true);
3854 } else {
3855 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003856 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003857 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003858 this.setSelectionEnabled(false);
3859 e.preventDefault();
3860 }
3861 }
3862
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003863 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003864 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003865 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003866 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003867 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003868 }
rgindad5613292012-06-19 15:40:37 -07003869 }
3870
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003871 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003872 // Debounce this event with the dblclick event. If you try to doubleclick
3873 // a URL to open it, Chrome will fire click then dblclick, but we won't
3874 // have expanded the selection text at the first click event.
3875 clearTimeout(this.timeouts_.openUrl);
3876 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3877 500);
3878 return;
3879 }
3880
Mike Frysinger847577f2017-05-23 23:25:57 -04003881 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003882 if (e.ctrlKey && e.button == 2 /* right button */) {
3883 e.preventDefault();
3884 this.contextMenu.show(e, this);
3885 } else if (e.button == this.mousePasteButton ||
3886 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003887 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003888 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003889 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003890 }
3891 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003892
Mike Frysinger2edd3612017-05-24 00:54:39 -04003893 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003894 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003895 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003896 }
3897
3898 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3899 this.scrollBlockerNode_.engaged) {
3900 // Disengage the scroll-blocker after one of these events.
3901 this.scrollBlockerNode_.engaged = false;
3902 this.scrollBlockerNode_.style.top = '-99px';
3903 }
3904
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003905 // Emulate arrow key presses via scroll wheel events.
3906 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3907 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003908 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003909 const delta =
3910 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003911
Mike Frysinger321063c2018-08-29 15:33:14 -04003912 // Helper to turn a wheel event delta into a series of key presses.
3913 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3914 if (distance == 0) {
3915 return '';
3916 }
3917
3918 // Convert the scroll distance into a number of rows/cols.
3919 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3920 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3921 return data.repeat(cells);
3922 };
3923
3924 // The order between up/down and left/right doesn't really matter.
3925 this.io.sendString(
3926 // Up/down arrow keys.
3927 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3928 'A', 'B') +
3929 // Left/right arrow keys.
3930 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10003931 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04003932 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003933
3934 e.preventDefault();
3935 }
3936 }
Robert Ginda928cf632014-03-05 15:07:41 -08003937 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003938 if (!this.scrollBlockerNode_.engaged) {
3939 if (e.type == 'mousedown') {
3940 // Move the scroll-blocker into place if we want to keep the scrollport
3941 // from scrolling.
3942 this.scrollBlockerNode_.engaged = true;
3943 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3944 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3945 } else if (e.type == 'mousemove') {
3946 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3947 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003948 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003949 e.preventDefault();
3950 }
3951 }
Robert Ginda928cf632014-03-05 15:07:41 -08003952
3953 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003954 }
3955
Robert Ginda928cf632014-03-05 15:07:41 -08003956 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3957 // Restore this on mouseup in case it was temporarily defeated with a
3958 // alt-mousedown. Only do this when the selection is empty so that
3959 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003960 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003961 }
rgindad5613292012-06-19 15:40:37 -07003962};
3963
3964/**
3965 * Clients should override this if they care to know about mouse events.
3966 *
3967 * The event parameter will be a normal DOM mouse click event with additional
3968 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003969 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003970 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003971 */
3972hterm.Terminal.prototype.onMouse = function(e) { };
3973
3974/**
rginda8e92a692012-05-20 19:37:20 -07003975 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003976 *
3977 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003978 */
Rob Spies06533ba2014-04-24 11:20:37 -07003979hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3980 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003981 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003982
Mike Frysingerbdb34802020-04-07 03:47:32 -04003983 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003984 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003985 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003986
Mike Frysingerbdb34802020-04-07 03:47:32 -04003987 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04003988 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003989 }
rginda8e92a692012-05-20 19:37:20 -07003990};
3991
3992/**
rginda8ba33642011-12-14 12:31:31 -08003993 * React when the ScrollPort is scrolled.
3994 */
3995hterm.Terminal.prototype.onScroll_ = function() {
3996 this.scheduleSyncCursorPosition_();
3997};
3998
3999/**
rginda9846e2f2012-01-27 13:53:33 -08004000 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004001 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004002 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004003 */
4004hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004005 this.onPasteData_(e.text);
4006};
4007
4008/**
4009 * Handle pasted data.
4010 *
4011 * @param {string} data The pasted data.
4012 */
4013hterm.Terminal.prototype.onPasteData_ = function(data) {
4014 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004015 if (this.options_.bracketedPaste) {
4016 // We strip out most escape sequences as they can cause issues (like
4017 // inserting an \x1b[201~ midstream). We pass through whitespace
4018 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4019 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004020 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004021 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4022 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4023 }
Robert Gindaa063b202014-07-21 11:08:25 -07004024
4025 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004026};
4027
4028/**
rgindaa09e7332012-08-17 12:49:51 -07004029 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004030 *
Joel Hockey0f933582019-08-27 18:01:51 -07004031 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004032 */
4033hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004034 if (!this.useDefaultWindowCopy) {
4035 e.preventDefault();
4036 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4037 }
rgindaa09e7332012-08-17 12:49:51 -07004038};
4039
4040/**
rginda8ba33642011-12-14 12:31:31 -08004041 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004042 *
4043 * Note: This function should not directly contain code that alters the internal
4044 * state of the terminal. That kind of code belongs in realizeWidth or
4045 * realizeHeight, so that it can be executed synchronously in the case of a
4046 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004047 */
4048hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004049 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4050 this.scrollPort_.characterSize.width) || 0;
4051 const rowCount = lib.f.smartFloorDivide(
4052 this.scrollPort_.getScreenHeight(),
4053 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004054
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004055 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004056 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004057 // gets removed from the document or during the initial load, and we can't
4058 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004059 // This can also happen if called before the scrollPort calculates the
4060 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004061 return;
4062 }
4063
Mike Frysingerdc727792020-04-10 01:41:13 -04004064 const isNewSize = (columnCount != this.screenSize.width ||
4065 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004066 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004067
4068 // We do this even if the size didn't change, just to be sure everything is
4069 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004070 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004071 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004072
Mike Frysingerbdb34802020-04-07 03:47:32 -04004073 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004074 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004075 }
rgindaa8ba17d2012-08-15 14:41:10 -07004076
Robert Gindafb1be6a2013-12-11 11:56:22 -08004077 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004078 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004079
4080 if (wasScrolledEnd) {
4081 this.scrollEnd();
4082 }
rginda8ba33642011-12-14 12:31:31 -08004083};
4084
4085/**
4086 * Service the cursor blink timeout.
4087 */
4088hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004089 if (!this.options_.cursorBlink) {
4090 delete this.timeouts_.cursorBlink;
4091 return;
4092 }
4093
Robert Ginda830583c2013-08-07 13:20:46 -07004094 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004095 this.cursorNode_.style.opacity == '0' ||
4096 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004097 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004098 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4099 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004100 } else {
rginda87b86462011-12-14 13:48:03 -08004101 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004102 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4103 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004104 }
4105};
David Reveman8f552492012-03-28 12:18:41 -04004106
4107/**
4108 * Set the scrollbar-visible mode bit.
4109 *
4110 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4111 * Otherwise it will not.
4112 *
4113 * Defaults to on.
4114 *
4115 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4116 */
4117hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4118 this.scrollPort_.setScrollbarVisible(state);
4119};
Michael Kelly485ecd12014-06-09 11:41:56 -04004120
4121/**
Rob Spies49039e52014-12-17 13:40:04 -08004122 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004123 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004124 *
4125 * Defaults to 1.
4126 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004127 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004128 */
4129hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4130 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4131};
4132
4133/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004134 * Close all web notifications created by terminal bells.
4135 */
4136hterm.Terminal.prototype.closeBellNotifications_ = function() {
4137 this.bellNotificationList_.forEach(function(n) {
4138 n.close();
4139 });
4140 this.bellNotificationList_.length = 0;
4141};
Raymes Khourye5d48982018-08-02 09:08:32 +10004142
4143/**
4144 * Syncs the cursor position when the scrollport gains focus.
4145 */
4146hterm.Terminal.prototype.onScrollportFocus_ = function() {
4147 // If the cursor is offscreen we set selection to the last row on the screen.
4148 const topRowIndex = this.scrollPort_.getTopRowIndex();
4149 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4150 const selection = this.document_.getSelection();
4151 if (!this.syncCursorPosition_() && selection) {
4152 selection.collapse(this.getRowNode(bottomRowIndex));
4153 }
4154};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004155
4156/**
4157 * Clients can override this if they want to provide an options page.
4158 */
4159hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4160
4161
4162/**
4163 * Called when user selects to open the options page.
4164 */
4165hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4166 this.onOpenOptionsPage();
4167};