blob: ad83ee5d18ba7a13cf94e4828ed337970cb0881e [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400109 /** @type {!Map<number, string>} */
110 this.colorPaletteOverrides_ = new Map();
111
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700112 this.screenBorderSize_ = 0;
113
Robert Ginda57f03b42012-09-13 11:02:48 -0700114 this.scrollOnOutput_ = null;
115 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400116 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800117
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700118 // True if we should override mouse event reporting to allow local selection.
119 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800120
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400121 // Whether to auto hide the mouse cursor when typing.
122 this.setAutomaticMouseHiding();
123 // Timer to keep mouse visible while it's being used.
124 this.mouseHideDelay_ = null;
125
rgindaf0090c92012-02-10 14:58:52 -0800126 // Terminal bell sound.
127 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400128 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800129 this.bellAudio_.setAttribute('preload', 'auto');
130
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000131 // The AccessibilityReader object for announcing command output.
132 this.accessibilityReader_ = null;
133
Mike Frysingercc114512017-09-11 21:39:17 -0400134 // The context menu object.
135 this.contextMenu = new hterm.ContextMenu();
136
Michael Kelly485ecd12014-06-09 11:41:56 -0400137 // All terminal bell notifications that have been generated (not necessarily
138 // shown).
139 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700140 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
142 // Whether we have permission to display notifications.
143 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400144
rginda6d397402012-01-17 10:58:29 -0800145 // Cursor position and attributes saved with DECSC.
146 this.savedOptions_ = {};
147
rginda8ba33642011-12-14 12:31:31 -0800148 // The current mode bits for the terminal.
149 this.options_ = new hterm.Options();
150
151 // Timeouts we might need to clear.
152 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800153
154 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800155 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800156
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800157 this.saveCursorAndState(true);
158
Zhu Qunying30d40712017-03-14 16:27:00 -0700159 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800160 this.keyboard = new hterm.Keyboard(this);
161
rginda87b86462011-12-14 13:48:03 -0800162 // General IO interface that can be given to third parties without exposing
163 // the entire terminal object.
164 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800165
rgindad5613292012-06-19 15:40:37 -0700166 // True if mouse-click-drag should scroll the terminal.
167 this.enableMouseDragScroll = true;
168
Robert Ginda57f03b42012-09-13 11:02:48 -0700169 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400170 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700171 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700172
Zhu Qunying30d40712017-03-14 16:27:00 -0700173 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700174 this.useDefaultWindowCopy = false;
175
176 this.clearSelectionAfterCopy = true;
177
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400178 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800179 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700180
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400181 // Whether we allow images to be shown.
182 this.allowImagesInline = null;
183
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400184 this.reportFocus = false;
185
Jason Linf129f3c2020-03-23 11:52:08 +1100186 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
187 this.alwaysUseLegacyPasting = false;
188
Joel Hockey3a44a442019-10-14 16:22:56 -0700189 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500190 function() { this.onTerminalReady(); }.bind(this));
shivanggargde5387e2020-06-10 01:31:16 +0530191
192 /** @const */
193 this.findBar = new hterm.FindBar(this);
rginda87b86462011-12-14 13:48:03 -0800194};
195
196/**
Robert Ginda830583c2013-08-07 13:20:46 -0700197 * Possible cursor shapes.
198 */
199hterm.Terminal.cursorShape = {
200 BLOCK: 'BLOCK',
201 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400202 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700203};
204
205/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700206 * Clients should override this to be notified when the terminal is ready
207 * for use.
208 *
209 * The terminal initialization is asynchronous, and shouldn't be used before
210 * this method is called.
211 */
212hterm.Terminal.prototype.onTerminalReady = function() { };
213
214/**
rginda35c456b2012-02-09 17:29:05 -0800215 * Default tab with of 8 to match xterm.
216 */
217hterm.Terminal.prototype.tabWidth = 8;
218
219/**
rginda9f5222b2012-03-05 11:53:28 -0800220 * Select a preference profile.
221 *
222 * This will load the terminal preferences for the given profile name and
223 * associate subsequent preference changes with the new preference profile.
224 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500225 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800226 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400227 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700228 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800229 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400230hterm.Terminal.prototype.setProfile = function(
231 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700232 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800233
Mike Frysingerdc727792020-04-10 01:41:13 -0400234 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800235
Mike Frysingerbdb34802020-04-07 03:47:32 -0400236 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400238 }
rginda9f5222b2012-03-05 11:53:28 -0800239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700241
242 /**
243 * Clears and reloads key bindings. Used by preferences
244 * 'keybindings' and 'keybindings-os-defaults'.
245 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 * @param {*?=} bindings
247 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700248 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400249 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700250 terminal.keyboard.bindings.clear();
251
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400252 // Default to an empty object so we still handle OS defaults.
253 if (bindings === null) {
254 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700255 }
256
257 if (!(bindings instanceof Object)) {
258 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400259 bindings = {};
260 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700261 }
262
263 try {
264 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
265 } catch (ex) {
266 console.error('Error in keybindings preference: ' + ex);
267 }
268 }
269
Robert Ginda57f03b42012-09-13 11:02:48 -0700270 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800271 'alt-gr-mode': function(v) {
272 if (v == null) {
273 if (navigator.language.toLowerCase() == 'en-us') {
274 v = 'none';
275 } else {
276 v = 'right-alt';
277 }
278 } else if (typeof v == 'string') {
279 v = v.toLowerCase();
280 } else {
281 v = 'none';
282 }
283
Mike Frysingerbdb34802020-04-07 03:47:32 -0400284 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800285 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400286 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800287
288 terminal.keyboard.altGrMode = v;
289 },
290
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700291 'alt-backspace-is-meta-backspace': function(v) {
292 terminal.keyboard.altBackspaceIsMetaBackspace = v;
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'alt-is-meta': function(v) {
296 terminal.keyboard.altIsMeta = v;
297 },
298
299 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400300 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700301 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400302 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700303
304 terminal.keyboard.altSendsWhat = v;
305 },
306
307 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400308 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800309 if (ary) {
310 terminal.bellAudio_.setAttribute('src',
311 lib.resource.getDataUrl(ary[1]));
312 } else {
313 terminal.bellAudio_.setAttribute('src', v);
314 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700315 },
316
Michael Kelly485ecd12014-06-09 11:41:56 -0400317 'desktop-notification-bell': function(v) {
318 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700319 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400320 Notification.permission === 'granted';
321 if (!terminal.desktopNotificationBell_) {
322 // Note: We don't call Notification.requestPermission here because
323 // Chrome requires the call be the result of a user action (such as an
324 // onclick handler), and pref listeners are run asynchronously.
325 //
326 // A way of working around this would be to display a dialog in the
327 // terminal with a "click-to-request-permission" button.
328 console.warn('desktop-notification-bell is true but we do not have ' +
329 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400330 }
331 } else {
332 terminal.desktopNotificationBell_ = false;
333 }
334 },
335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'background-color': function(v) {
337 terminal.setBackgroundColor(v);
338 },
339
340 'background-image': function(v) {
341 terminal.scrollPort_.setBackgroundImage(v);
342 },
343
344 'background-size': function(v) {
345 terminal.scrollPort_.setBackgroundSize(v);
346 },
347
348 'background-position': function(v) {
349 terminal.scrollPort_.setBackgroundPosition(v);
350 },
351
352 'backspace-sends-backspace': function(v) {
353 terminal.keyboard.backspaceSendsBackspace = v;
354 },
355
Brad Town18654b62015-03-12 00:27:45 -0700356 'character-map-overrides': function(v) {
357 if (!(v == null || v instanceof Object)) {
358 console.warn('Preference character-map-modifications is not an ' +
359 'object: ' + v);
360 return;
361 }
362
Mike Frysinger095d4062017-06-14 00:29:48 -0700363 terminal.vt.characterMaps.reset();
364 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700365 },
366
Robert Ginda57f03b42012-09-13 11:02:48 -0700367 'cursor-blink': function(v) {
368 terminal.setCursorBlink(!!v);
369 },
370
Joel Hockey9d10ba12019-05-28 01:25:02 -0700371 'cursor-shape': function(v) {
372 terminal.setCursorShape(v);
373 },
374
Robert Gindaea2183e2014-07-17 09:51:51 -0700375 'cursor-blink-cycle': function(v) {
376 if (v instanceof Array &&
377 typeof v[0] == 'number' &&
378 typeof v[1] == 'number') {
379 terminal.cursorBlinkCycle_ = v;
380 } else if (typeof v == 'number') {
381 terminal.cursorBlinkCycle_ = [v, v];
382 } else {
383 // Fast blink indicates an error.
384 terminal.cursorBlinkCycle_ = [100, 100];
385 }
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'cursor-color': function(v) {
389 terminal.setCursorColor(v);
390 },
391
392 'color-palette-overrides': function(v) {
393 if (!(v == null || v instanceof Object || v instanceof Array)) {
394 console.warn('Preference color-palette-overrides is not an array or ' +
395 'object: ' + v);
396 return;
rginda9f5222b2012-03-05 11:53:28 -0800397 }
rginda9f5222b2012-03-05 11:53:28 -0800398
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400399 // Reset all existing colors first as the new palette override might not
400 // have the same mappings. If the old one set colors the new one doesn't,
401 // those old mappings have to get cleared first.
Mike Frysinger06ce15d2020-10-21 21:53:29 -0400402 lib.colors.stockPalette.forEach((c, i) => terminal.setColorPalette(i, c));
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400403 terminal.colorPaletteOverrides_.clear();
rginda39bdf6f2012-04-10 16:50:55 -0700404
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400406 for (const key in v) {
407 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 if (isNaN(i) || i < 0 || i > 255) {
409 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
410 continue;
411 }
412
413 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400414 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700415 if (rgb) {
416 terminal.setColorPalette(i, rgb);
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400417 terminal.colorPaletteOverrides_.set(i, rgb);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700418 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700419 }
420 }
rginda30f20f62012-04-05 16:36:19 -0700421 }
rginda30f20f62012-04-05 16:36:19 -0700422
Joel Hockey42dba8f2020-03-26 16:21:11 -0700423 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
424 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 },
rginda30f20f62012-04-05 16:36:19 -0700426
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 'copy-on-select': function(v) {
428 terminal.copyOnSelect = !!v;
429 },
rginda9f5222b2012-03-05 11:53:28 -0800430
Rob Spies0bec09b2014-06-06 15:58:09 -0700431 'use-default-window-copy': function(v) {
432 terminal.useDefaultWindowCopy = !!v;
433 },
434
435 'clear-selection-after-copy': function(v) {
436 terminal.clearSelectionAfterCopy = !!v;
437 },
438
Robert Ginda7e5e9522014-03-14 12:23:58 -0700439 'ctrl-plus-minus-zero-zoom': function(v) {
440 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
441 },
442
Robert Gindafb5a3f92014-05-13 14:12:00 -0700443 'ctrl-c-copy': function(v) {
444 terminal.keyboard.ctrlCCopy = v;
445 },
446
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100447 'ctrl-v-paste': function(v) {
448 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700449 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100450 },
451
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700452 'paste-on-drop': function(v) {
453 terminal.scrollPort_.setPasteOnDrop(v);
454 },
455
Masaya Suzuki273aa982014-05-31 07:25:55 +0900456 'east-asian-ambiguous-as-two-column': function(v) {
457 lib.wc.regardCjkAmbiguous = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'enable-8-bit-control': function(v) {
461 terminal.vt.enable8BitControl = !!v;
462 },
rginda30f20f62012-04-05 16:36:19 -0700463
Robert Ginda57f03b42012-09-13 11:02:48 -0700464 'enable-bold': function(v) {
465 terminal.syncBoldSafeState();
466 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400467
Robert Ginda3e278d72014-03-25 13:18:51 -0700468 'enable-bold-as-bright': function(v) {
469 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
470 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
471 },
472
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400473 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500474 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400475 },
476
Robert Ginda57f03b42012-09-13 11:02:48 -0700477 'enable-clipboard-write': function(v) {
478 terminal.vt.enableClipboardWrite = !!v;
479 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400480
Robert Ginda3755e752013-05-31 13:34:09 -0700481 'enable-dec12': function(v) {
482 terminal.vt.enableDec12 = !!v;
483 },
484
Mike Frysinger38f267d2018-09-07 02:50:59 -0400485 'enable-csi-j-3': function(v) {
486 terminal.vt.enableCsiJ3 = !!v;
487 },
488
shivanggarg2b7b0d52020-07-10 11:01:34 +0530489 'find-result-color': function(v) {
490 terminal.findBar.setFindResultColor(v);
491 },
492
shivanggarga72e8ba2020-07-16 07:11:17 +0530493 'find-result-selected-color': function(v) {
494 terminal.findBar.setFindResultSelectedColor(v);
495 },
496
Robert Ginda57f03b42012-09-13 11:02:48 -0700497 'font-family': function(v) {
498 terminal.syncFontFamily();
499 },
rginda30f20f62012-04-05 16:36:19 -0700500
Robert Ginda57f03b42012-09-13 11:02:48 -0700501 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700502 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700503 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500504 console.error(`Invalid font size: ${v}`);
505 return;
506 }
507
Robert Ginda57f03b42012-09-13 11:02:48 -0700508 terminal.setFontSize(v);
509 },
rginda9875d902012-08-20 16:21:57 -0700510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'font-smoothing': function(v) {
512 terminal.syncFontFamily();
513 },
rgindade84e382012-04-20 15:39:31 -0700514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'foreground-color': function(v) {
516 terminal.setForegroundColor(v);
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400519 'hide-mouse-while-typing': function(v) {
520 terminal.setAutomaticMouseHiding(v);
521 },
522
Robert Ginda57f03b42012-09-13 11:02:48 -0700523 'home-keys-scroll': function(v) {
524 terminal.keyboard.homeKeysScroll = v;
525 },
rginda4bba5e12012-06-20 16:15:30 -0700526
Robert Gindaa8165692015-06-15 14:46:31 -0700527 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700528 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
529 },
Robert Gindaa8165692015-06-15 14:46:31 -0700530
Joel Hockey95a9e272020-03-16 21:19:53 -0700531 'keybindings-os-defaults': function(v) {
532 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700533 },
534
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700535 'media-keys-are-fkeys': function(v) {
536 terminal.keyboard.mediaKeysAreFKeys = v;
537 },
538
Robert Ginda57f03b42012-09-13 11:02:48 -0700539 'meta-sends-escape': function(v) {
540 terminal.keyboard.metaSendsEscape = v;
541 },
rginda30f20f62012-04-05 16:36:19 -0700542
Mike Frysinger847577f2017-05-23 23:25:57 -0400543 'mouse-right-click-paste': function(v) {
544 terminal.mouseRightClickPaste = v;
545 },
546
Robert Ginda57f03b42012-09-13 11:02:48 -0700547 'mouse-paste-button': function(v) {
548 terminal.syncMousePasteButton();
549 },
rgindaa8ba17d2012-08-15 14:41:10 -0700550
Robert Gindae76aa9f2014-03-14 12:29:12 -0700551 'page-keys-scroll': function(v) {
552 terminal.keyboard.pageKeysScroll = v;
553 },
554
Robert Ginda40932892012-12-10 17:26:40 -0800555 'pass-alt-number': function(v) {
556 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700557 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800558 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500559 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800560 }
561
562 terminal.passAltNumber = v;
563 },
564
565 'pass-ctrl-number': function(v) {
566 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700567 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800568 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500569 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800570 }
571
572 terminal.passCtrlNumber = v;
573 },
574
Joel Hockey0e052042020-02-19 05:37:19 -0800575 'pass-ctrl-n': function(v) {
576 terminal.passCtrlN = v;
577 },
578
579 'pass-ctrl-t': function(v) {
580 terminal.passCtrlT = v;
581 },
582
583 'pass-ctrl-tab': function(v) {
584 terminal.passCtrlTab = v;
585 },
586
587 'pass-ctrl-w': function(v) {
588 terminal.passCtrlW = v;
589 },
590
Robert Ginda40932892012-12-10 17:26:40 -0800591 'pass-meta-number': function(v) {
592 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700593 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800594 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500595 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800596 }
597
598 terminal.passMetaNumber = v;
599 },
600
Marius Schilder77857b32014-05-14 16:21:26 -0700601 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700602 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700603 },
604
Robert Ginda8cb7d902013-06-20 14:37:18 -0700605 'receive-encoding': function(v) {
606 if (!(/^(utf-8|raw)$/).test(v)) {
607 console.warn('Invalid value for "receive-encoding": ' + v);
608 v = 'utf-8';
609 }
610
611 terminal.vt.characterEncoding = v;
612 },
613
Joel Hockey139d82d2020-04-07 23:04:29 -0700614 'screen-padding-size': function(v) {
615 v = parseInt(v, 10);
616 if (isNaN(v) || v < 0) {
617 console.error(`Invalid screen padding size: ${v}`);
618 return;
619 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700620 terminal.setScreenPaddingSize(v);
621 },
622
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700623 'screen-border-size': function(v) {
624 v = parseInt(v, 10);
625 if (isNaN(v) || v < 0) {
626 console.error(`Invalid screen border size: ${v}`);
627 return;
628 }
629 terminal.setScreenBorderSize(v);
630 },
631
632 'screen-border-color': function(v) {
633 terminal.div_.style.borderColor = v;
634 },
635
Robert Ginda57f03b42012-09-13 11:02:48 -0700636 'scroll-on-keystroke': function(v) {
637 terminal.scrollOnKeystroke_ = v;
638 },
rginda9f5222b2012-03-05 11:53:28 -0800639
Robert Ginda57f03b42012-09-13 11:02:48 -0700640 'scroll-on-output': function(v) {
641 terminal.scrollOnOutput_ = v;
642 },
rginda30f20f62012-04-05 16:36:19 -0700643
Robert Ginda57f03b42012-09-13 11:02:48 -0700644 'scrollbar-visible': function(v) {
645 terminal.setScrollbarVisible(v);
646 },
rginda9f5222b2012-03-05 11:53:28 -0800647
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400648 'scroll-wheel-may-send-arrow-keys': function(v) {
649 terminal.scrollWheelArrowKeys_ = v;
650 },
651
Rob Spies49039e52014-12-17 13:40:04 -0800652 'scroll-wheel-move-multiplier': function(v) {
653 terminal.setScrollWheelMoveMultipler(v);
654 },
655
Robert Ginda57f03b42012-09-13 11:02:48 -0700656 'shift-insert-paste': function(v) {
657 terminal.keyboard.shiftInsertPaste = v;
658 },
rginda9f5222b2012-03-05 11:53:28 -0800659
Mike Frysingera7768922017-07-28 15:00:12 -0400660 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400661 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400662 },
663
Robert Gindae76aa9f2014-03-14 12:29:12 -0700664 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400665 terminal.scrollPort_.setUserCssUrl(v);
666 },
667
668 'user-css-text': function(v) {
669 terminal.scrollPort_.setUserCssText(v);
670 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400671
672 'word-break-match-left': function(v) {
673 terminal.primaryScreen_.wordBreakMatchLeft = v;
674 terminal.alternateScreen_.wordBreakMatchLeft = v;
675 },
676
677 'word-break-match-right': function(v) {
678 terminal.primaryScreen_.wordBreakMatchRight = v;
679 terminal.alternateScreen_.wordBreakMatchRight = v;
680 },
681
682 'word-break-match-middle': function(v) {
683 terminal.primaryScreen_.wordBreakMatchMiddle = v;
684 terminal.alternateScreen_.wordBreakMatchMiddle = v;
685 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400686
687 'allow-images-inline': function(v) {
688 terminal.allowImagesInline = v;
689 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700690 });
rginda30f20f62012-04-05 16:36:19 -0700691
Robert Ginda57f03b42012-09-13 11:02:48 -0700692 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800693 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700694
Mike Frysingerec4225d2020-04-07 05:00:01 -0400695 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700696 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400697 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400698 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700699 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800700};
701
Rob Spies56953412014-04-28 14:09:47 -0700702/**
703 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500704 *
Joel Hockey0f933582019-08-27 18:01:51 -0700705 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700706 */
707hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700708 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700709};
710
Robert Gindaa063b202014-07-21 11:08:25 -0700711/**
712 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500713 *
714 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700715 */
716hterm.Terminal.prototype.setBracketedPaste = function(state) {
717 this.options_.bracketedPaste = state;
718};
Rob Spies56953412014-04-28 14:09:47 -0700719
rginda8e92a692012-05-20 19:37:20 -0700720/**
721 * Set the color for the cursor.
722 *
723 * If you want this setting to persist, set it through prefs_, rather than
724 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500725 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500726 * @param {string=} color The color to set. If not defined, we reset to the
727 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700728 */
729hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400730 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700731 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400732 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500733
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400734 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700735};
736
737/**
738 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400739 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500740 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700741 */
742hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400743 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700744};
745
746/**
rgindad5613292012-06-19 15:40:37 -0700747 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500748 *
749 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700750 */
751hterm.Terminal.prototype.setSelectionEnabled = function(state) {
752 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700753};
754
755/**
Joel Hockeyfde20592020-08-02 15:05:27 -0700756 * Set the background image.
757 *
758 * If you want this setting to persist, set it through prefs_, rather than
759 * with this method.
760 *
761 * @param {string=} cssUrl The image to set as a css url. If not defined, we
762 * reset to the saved user preference.
763 */
764hterm.Terminal.prototype.setBackgroundImage = function(cssUrl) {
765 if (cssUrl === undefined) {
766 cssUrl = this.prefs_.getString('background-image');
767 }
768 this.scrollPort_.setBackgroundImage(cssUrl);
769};
770
771/**
rginda8e92a692012-05-20 19:37:20 -0700772 * Set the background color.
773 *
774 * If you want this setting to persist, set it through prefs_, rather than
775 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500776 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500777 * @param {string=} color The color to set. If not defined, we reset to the
778 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700779 */
780hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400781 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700782 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400783 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500784
Joel Hockey42dba8f2020-03-26 16:21:11 -0700785 this.backgroundColor_ = lib.colors.normalizeCSS(color);
786 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700787};
788
rginda9f5222b2012-03-05 11:53:28 -0800789/**
790 * Return the current terminal background color.
791 *
792 * Intended for use by other classes, so we don't have to expose the entire
793 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500794 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700795 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800796 */
797hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700798 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700799};
800
801/**
802 * Set the foreground color.
803 *
804 * If you want this setting to persist, set it through prefs_, rather than
805 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500806 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500807 * @param {string=} color The color to set. If not defined, we reset to the
808 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700809 */
810hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400811 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700812 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400813 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500814
Joel Hockey42dba8f2020-03-26 16:21:11 -0700815 this.foregroundColor_ = lib.colors.normalizeCSS(color);
816 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800817};
818
819/**
820 * Return the current terminal foreground color.
821 *
822 * Intended for use by other classes, so we don't have to expose the entire
823 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500824 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700825 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800826 */
827hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700828 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800829};
830
831/**
rginda87b86462011-12-14 13:48:03 -0800832 * Create a new instance of a terminal command and run it with a given
833 * argument string.
834 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700835 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700836 * @param {string} commandName The command to run for this terminal.
837 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800838 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700839hterm.Terminal.prototype.runCommandClass = function(
840 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400841 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400842 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700843 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400844 }
rgindaf522ce02012-04-17 17:49:17 -0700845
rginda87b86462011-12-14 13:48:03 -0800846 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700847 {
848 commandName: commandName,
849 args: args,
rginda87b86462011-12-14 13:48:03 -0800850 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700851 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400852 onExit: (code) => {
853 this.io.pop();
854 this.uninstallKeyboard();
855 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
856 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400857 window.close();
858 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400859 },
rginda87b86462011-12-14 13:48:03 -0800860 });
861
rgindafeaf3142012-01-31 15:14:20 -0800862 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800863 this.command.run();
864};
865
866/**
rgindafeaf3142012-01-31 15:14:20 -0800867 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500868 *
869 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800870 */
871hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700872 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800873};
874
875/**
876 * Install the keyboard handler for this terminal.
877 *
878 * This will prevent the browser from seeing any keystrokes sent to the
879 * terminal.
880 */
881hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700882 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400883};
rgindafeaf3142012-01-31 15:14:20 -0800884
885/**
886 * Uninstall the keyboard handler for this terminal.
887 */
888hterm.Terminal.prototype.uninstallKeyboard = function() {
889 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400890};
rgindafeaf3142012-01-31 15:14:20 -0800891
892/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400893 * Set a CSS variable.
894 *
895 * Normally this is used to set variables in the hterm namespace.
896 *
897 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700898 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400899 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400900 */
901hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400902 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400903 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400904 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400905};
906
907/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700908 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
909 * input is valid.
910 *
911 * @param {string} name The variable to set.
912 * @param {?string} rgb The rgb value to assign to the variable.
913 */
914hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
915 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
916 if (ary) {
917 this.setCssVar(name, ary.slice(0, 3).join(','));
918 }
919};
920
921/**
922 * Sets the specified color for the active screen.
923 *
924 * @param {number} i The index into the 256 color palette to set.
925 * @param {?string} rgb The rgb value to assign to the variable.
926 */
927hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
928 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
929 this.setRgbColorCssVar(`color-${i}`, rgb);
930 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
931 }
932};
933
934/**
935 * Returns the current value in the active screen of the specified color.
936 *
937 * @param {number} i Color palette index.
938 * @return {string} rgb color.
939 */
940hterm.Terminal.prototype.getColorPalette = function(i) {
941 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400942 this.colorPaletteOverrides_.get(i) ||
943 lib.colors.stockPalette[i];
Joel Hockey42dba8f2020-03-26 16:21:11 -0700944};
945
946/**
947 * Reset the specified color in the active screen to its default value.
948 *
949 * @param {number} i Color to reset
950 */
951hterm.Terminal.prototype.resetColor = function(i) {
Mike Frysingerb9a5bf32020-10-21 21:52:29 -0400952 this.setColorPalette(
953 i, this.colorPaletteOverrides_.get(i) || lib.colors.stockPalette[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700954 delete this.screen_.textAttributes.colorPaletteOverrides[i];
955};
956
957/**
958 * Reset the current screen color palette to the default state.
959 */
960hterm.Terminal.prototype.resetColorPalette = function() {
961 this.screen_.textAttributes.colorPaletteOverrides.forEach(
962 (c, i) => this.resetColor(i));
963};
964
965/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500966 * Get a CSS variable.
967 *
968 * Normally this is used to get variables in the hterm namespace.
969 *
970 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400971 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500972 * @return {string} The current setting for this variable.
973 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400974hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500975 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400976 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500977};
978
979/**
shivanggargf3b362a2020-07-10 11:06:35 +0530980 * @return {!hterm.ScrollPort}
981 */
982hterm.Terminal.prototype.getScrollPort = function() {
983 return this.scrollPort_;
984};
985
986/**
Jason Linbbbdb752020-03-06 16:26:59 +1100987 * Update CSS character size variables to match the scrollport.
988 */
989hterm.Terminal.prototype.updateCssCharsize_ = function() {
990 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
991 this.setCssVar('charsize-height',
992 this.scrollPort_.characterSize.height + 'px');
993};
994
995/**
rginda35c456b2012-02-09 17:29:05 -0800996 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800997 *
998 * Call setFontSize(0) to reset to the default font size.
999 *
1000 * This function does not modify the font-size preference.
1001 *
1002 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -08001003 */
1004hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001005 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001006 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001007 }
rginda9f5222b2012-03-05 11:53:28 -08001008
rginda35c456b2012-02-09 17:29:05 -08001009 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -07001010 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +11001011 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -08001012};
1013
1014/**
1015 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -05001016 *
1017 * @return {number}
rginda35c456b2012-02-09 17:29:05 -08001018 */
1019hterm.Terminal.prototype.getFontSize = function() {
1020 return this.scrollPort_.getFontSize();
1021};
1022
1023/**
rginda8e92a692012-05-20 19:37:20 -07001024 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -05001025 *
1026 * @return {string}
rginda8e92a692012-05-20 19:37:20 -07001027 */
1028hterm.Terminal.prototype.getFontFamily = function() {
1029 return this.scrollPort_.getFontFamily();
1030};
1031
1032/**
rginda35c456b2012-02-09 17:29:05 -08001033 * Set the CSS "font-family" for this terminal.
1034 */
rginda9f5222b2012-03-05 11:53:28 -08001035hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001036 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1037 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001038 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001039 this.syncBoldSafeState();
1040};
1041
rginda4bba5e12012-06-20 16:15:30 -07001042/**
1043 * Set this.mousePasteButton based on the mouse-paste-button pref,
1044 * autodetecting if necessary.
1045 */
1046hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001047 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001048 if (typeof button == 'number') {
1049 this.mousePasteButton = button;
1050 return;
1051 }
1052
Mike Frysingeree81a002017-12-12 16:14:53 -05001053 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001054 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001055 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001056 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001057 }
1058};
1059
1060/**
1061 * Enable or disable bold based on the enable-bold pref, autodetecting if
1062 * necessary.
1063 */
rginda9f5222b2012-03-05 11:53:28 -08001064hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001065 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001066 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001067 this.primaryScreen_.textAttributes.enableBold = enableBold;
1068 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001069 return;
1070 }
1071
Mike Frysingerdc727792020-04-10 01:41:13 -04001072 const normalSize = this.scrollPort_.measureCharacterSize();
1073 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001074
Mike Frysingerdc727792020-04-10 01:41:13 -04001075 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001076 if (!isBoldSafe) {
1077 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001078 'from normal. Font family is: ' +
1079 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001080 }
rginda9f5222b2012-03-05 11:53:28 -08001081
Robert Gindaed016262012-10-26 16:27:09 -07001082 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1083 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001084};
1085
1086/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001087 * Control text blinking behavior.
1088 *
1089 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001090 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001091hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001092 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001093 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001094 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001095 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001096};
1097
1098/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001099 * Set the mouse cursor style based on the current terminal mode.
1100 */
1101hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001102 this.setCssVar('mouse-cursor-style',
1103 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1104 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001105 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001106};
1107
1108/**
rginda87b86462011-12-14 13:48:03 -08001109 * Return a copy of the current cursor position.
1110 *
Joel Hockey0f933582019-08-27 18:01:51 -07001111 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001112 */
1113hterm.Terminal.prototype.saveCursor = function() {
1114 return this.screen_.cursorPosition.clone();
1115};
1116
Evan Jones2600d4f2016-12-06 09:29:36 -05001117/**
1118 * Return the current text attributes.
1119 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001120 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001121 */
rgindaa19afe22012-01-25 15:40:22 -08001122hterm.Terminal.prototype.getTextAttributes = function() {
1123 return this.screen_.textAttributes;
1124};
1125
Evan Jones2600d4f2016-12-06 09:29:36 -05001126/**
1127 * Set the text attributes.
1128 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001129 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001130 */
rginda1a09aa02012-06-18 21:11:25 -07001131hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1132 this.screen_.textAttributes = textAttributes;
1133};
1134
rginda87b86462011-12-14 13:48:03 -08001135/**
rginda9846e2f2012-01-27 13:53:33 -08001136 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001137 *
1138 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001139 */
1140hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001141 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001142};
1143
1144/**
rginda87b86462011-12-14 13:48:03 -08001145 * Restore a previously saved cursor position.
1146 *
Joel Hockey0f933582019-08-27 18:01:51 -07001147 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001148 */
1149hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001150 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1151 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001152 this.screen_.setCursorPosition(row, column);
1153 if (cursor.column > column ||
1154 cursor.column == column && cursor.overflow) {
1155 this.screen_.cursorPosition.overflow = true;
1156 }
rginda87b86462011-12-14 13:48:03 -08001157};
1158
1159/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001160 * Clear the cursor's overflow flag.
1161 */
1162hterm.Terminal.prototype.clearCursorOverflow = function() {
1163 this.screen_.cursorPosition.overflow = false;
1164};
1165
1166/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001167 * Save the current cursor state to the corresponding screens.
1168 *
1169 * See the hterm.Screen.CursorState class for more details.
1170 *
1171 * @param {boolean=} both If true, update both screens, else only update the
1172 * current screen.
1173 */
1174hterm.Terminal.prototype.saveCursorAndState = function(both) {
1175 if (both) {
1176 this.primaryScreen_.saveCursorAndState(this.vt);
1177 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001178 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001179 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001180 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001181};
1182
1183/**
1184 * Restore the saved cursor state in the corresponding screens.
1185 *
1186 * See the hterm.Screen.CursorState class for more details.
1187 *
1188 * @param {boolean=} both If true, update both screens, else only update the
1189 * current screen.
1190 */
1191hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1192 if (both) {
1193 this.primaryScreen_.restoreCursorAndState(this.vt);
1194 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001195 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001196 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001197 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001198};
1199
1200/**
Robert Ginda830583c2013-08-07 13:20:46 -07001201 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001202 *
1203 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001204 */
1205hterm.Terminal.prototype.setCursorShape = function(shape) {
1206 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001207 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001208};
Robert Ginda830583c2013-08-07 13:20:46 -07001209
1210/**
1211 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001212 *
1213 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001214 */
1215hterm.Terminal.prototype.getCursorShape = function() {
1216 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001217};
Robert Ginda830583c2013-08-07 13:20:46 -07001218
1219/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001220 * Set the screen padding size in pixels.
1221 *
1222 * @param {number} size
1223 */
1224hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001225 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001226 this.scrollPort_.setScreenPaddingSize(size);
1227};
1228
1229/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001230 * Set the screen border size in pixels.
1231 *
1232 * @param {number} size
1233 */
1234hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1235 this.div_.style.borderWidth = `${size}px`;
1236 this.screenBorderSize_ = size;
1237 this.scrollPort_.resize();
1238};
1239
1240/**
rginda87b86462011-12-14 13:48:03 -08001241 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001242 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001243 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001244 */
1245hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001246 if (columnCount == null) {
1247 this.div_.style.width = '100%';
1248 return;
1249 }
1250
Joel Hockey139d82d2020-04-07 23:04:29 -07001251 const rightPadding = Math.max(
1252 this.scrollPort_.screenPaddingSize,
1253 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001254 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001255 (this.scrollPort_.characterSize.width * columnCount) +
1256 this.scrollPort_.screenPaddingSize + rightPadding +
1257 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001258 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001259 this.scheduleSyncCursorPosition_();
1260};
rginda87b86462011-12-14 13:48:03 -08001261
rgindac9bc5502012-01-18 11:48:44 -08001262/**
rginda35c456b2012-02-09 17:29:05 -08001263 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001264 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001265 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001266 */
1267hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001268 if (rowCount == null) {
1269 this.div_.style.height = '100%';
1270 return;
1271 }
1272
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001273 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1274 (2 * this.scrollPort_.screenPaddingSize) +
1275 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001276 this.realizeSize_(this.screenSize.width, rowCount);
1277 this.scheduleSyncCursorPosition_();
1278};
1279
1280/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001281 * Deal with terminal size changes.
1282 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001283 * @param {number} columnCount The number of columns.
1284 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001285 */
1286hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001287 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001288
Mike Frysinger0206e262019-06-13 10:18:19 -04001289 if (columnCount != this.screenSize.width) {
1290 notify = true;
1291 this.realizeWidth_(columnCount);
1292 }
1293
1294 if (rowCount != this.screenSize.height) {
1295 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001296 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001297 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001298
1299 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001300 if (notify) {
1301 this.io.onTerminalResize_(columnCount, rowCount);
1302 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001303};
1304
1305/**
rgindac9bc5502012-01-18 11:48:44 -08001306 * Deal with terminal width changes.
1307 *
1308 * This function does what needs to be done when the terminal width changes
1309 * out from under us. It happens here rather than in onResize_() because this
1310 * code may need to run synchronously to handle programmatic changes of
1311 * terminal width.
1312 *
1313 * Relying on the browser to send us an async resize event means we may not be
1314 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001315 *
1316 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001317 */
1318hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001319 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001320 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001321 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001322
Mike Frysingerdc727792020-04-10 01:41:13 -04001323 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001324 if (deltaColumns == 0) {
1325 // No change, so don't bother recalculating things.
1326 return;
1327 }
rgindac9bc5502012-01-18 11:48:44 -08001328
rginda87b86462011-12-14 13:48:03 -08001329 this.screenSize.width = columnCount;
1330 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001331
1332 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001333 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001334 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001335 }
rgindac9bc5502012-01-18 11:48:44 -08001336 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001337 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001338 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001339 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001340 }
rgindac9bc5502012-01-18 11:48:44 -08001341
1342 this.tabStops_.pop();
1343 }
1344 }
1345
1346 this.screen_.setColumnCount(this.screenSize.width);
1347};
1348
1349/**
1350 * Deal with terminal height changes.
1351 *
1352 * This function does what needs to be done when the terminal height changes
1353 * out from under us. It happens here rather than in onResize_() because this
1354 * code may need to run synchronously to handle programmatic changes of
1355 * terminal height.
1356 *
1357 * Relying on the browser to send us an async resize event means we may not be
1358 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001359 *
1360 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001361 */
1362hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001363 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001364 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001365 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001366
Mike Frysingerdc727792020-04-10 01:41:13 -04001367 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001368 if (deltaRows == 0) {
1369 // No change, so don't bother recalculating things.
1370 return;
1371 }
rgindac9bc5502012-01-18 11:48:44 -08001372
1373 this.screenSize.height = rowCount;
1374
Mike Frysingerdc727792020-04-10 01:41:13 -04001375 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001376
1377 if (deltaRows < 0) {
1378 // Screen got smaller.
1379 deltaRows *= -1;
1380 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001381 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001382 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001383 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001384 }
rgindac9bc5502012-01-18 11:48:44 -08001385
Mike Frysingerbdb34802020-04-07 03:47:32 -04001386 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001387 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001388 }
rgindac9bc5502012-01-18 11:48:44 -08001389
1390 this.screen_.popRow();
1391 deltaRows--;
1392 }
1393
Mike Frysingerdc727792020-04-10 01:41:13 -04001394 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001395 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1396
1397 // We just removed rows from the top of the screen, we need to update
1398 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001399 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001400 } else if (deltaRows > 0) {
1401 // Screen got larger.
1402
1403 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001404 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1405 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001406 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1407 this.screen_.unshiftRows(rows);
1408 deltaRows -= scrollbackCount;
1409 cursor.row += scrollbackCount;
1410 }
1411
Mike Frysingerbdb34802020-04-07 03:47:32 -04001412 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001413 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001414 }
rgindac9bc5502012-01-18 11:48:44 -08001415 }
1416
rginda35c456b2012-02-09 17:29:05 -08001417 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001418 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001419};
1420
1421/**
1422 * Scroll the terminal to the top of the scrollback buffer.
1423 */
1424hterm.Terminal.prototype.scrollHome = function() {
1425 this.scrollPort_.scrollRowToTop(0);
1426};
1427
1428/**
1429 * Scroll the terminal to the end.
1430 */
1431hterm.Terminal.prototype.scrollEnd = function() {
1432 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1433};
1434
1435/**
1436 * Scroll the terminal one page up (minus one line) relative to the current
1437 * position.
1438 */
1439hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001440 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001441};
1442
1443/**
1444 * Scroll the terminal one page down (minus one line) relative to the current
1445 * position.
1446 */
1447hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001448 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001449};
1450
rgindac9bc5502012-01-18 11:48:44 -08001451/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001452 * Scroll the terminal one line up relative to the current position.
1453 */
1454hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001455 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001456 this.scrollPort_.scrollRowToTop(i - 1);
1457};
1458
1459/**
1460 * Scroll the terminal one line down relative to the current position.
1461 */
1462hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001463 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001464 this.scrollPort_.scrollRowToTop(i + 1);
1465};
1466
1467/**
Robert Ginda40932892012-12-10 17:26:40 -08001468 * Clear primary screen, secondary screen, and the scrollback buffer.
1469 */
1470hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001471 this.clearHome(this.primaryScreen_);
1472 this.clearHome(this.alternateScreen_);
1473
1474 this.clearScrollback();
1475};
1476
1477/**
1478 * Clear scrollback buffer.
1479 */
1480hterm.Terminal.prototype.clearScrollback = function() {
1481 // Move to the end of the buffer in case the screen was scrolled back.
1482 // We're going to throw it away which would leave the display invalid.
1483 this.scrollEnd();
1484
Robert Ginda40932892012-12-10 17:26:40 -08001485 this.scrollbackRows_.length = 0;
1486 this.scrollPort_.resetCache();
1487
Mike Frysinger9c482b82018-09-07 02:49:36 -04001488 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1489 const bottom = screen.getHeight();
1490 this.renumberRows_(0, bottom, screen);
1491 });
Robert Ginda40932892012-12-10 17:26:40 -08001492
1493 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001494 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001495};
1496
1497/**
rgindac9bc5502012-01-18 11:48:44 -08001498 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001499 *
1500 * Perform a full reset to the default values listed in
1501 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001502 */
rginda87b86462011-12-14 13:48:03 -08001503hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001504 this.vt.reset();
1505
rgindac9bc5502012-01-18 11:48:44 -08001506 this.clearAllTabStops();
1507 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001508
Joel Hockey42dba8f2020-03-26 16:21:11 -07001509 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001510 const resetScreen = (screen) => {
1511 // We want to make sure to reset the attributes before we clear the screen.
1512 // The attributes might be used to initialize default/empty rows.
1513 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001514 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001515 this.clearHome(screen);
1516 screen.saveCursorAndState(this.vt);
1517 };
1518 resetScreen(this.primaryScreen_);
1519 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001520
Mike Frysinger84301d02017-11-29 13:28:46 -08001521 // Reset terminal options to their default values.
1522 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001523 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1524
Mike Frysinger84301d02017-11-29 13:28:46 -08001525 this.setVTScrollRegion(null, null);
1526
1527 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001528};
1529
rgindac9bc5502012-01-18 11:48:44 -08001530/**
1531 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001532 *
1533 * Perform a soft reset to the default values listed in
1534 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001535 */
rginda0f5c0292012-01-13 11:00:13 -08001536hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001537 this.vt.reset();
1538
rgindab8bc8932012-04-27 12:45:03 -07001539 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001540 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001541
Brad Townb62dfdc2015-03-16 19:07:15 -07001542 // We show the cursor on soft reset but do not alter the blink state.
1543 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1544
Joel Hockey42dba8f2020-03-26 16:21:11 -07001545 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001546 const resetScreen = (screen) => {
1547 // Xterm also resets the color palette on soft reset, even though it doesn't
1548 // seem to be documented anywhere.
1549 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001550 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001551 screen.saveCursorAndState(this.vt);
1552 };
1553 resetScreen(this.primaryScreen_);
1554 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001555
rgindab8bc8932012-04-27 12:45:03 -07001556 // The xterm man page explicitly says this will happen on soft reset.
1557 this.setVTScrollRegion(null, null);
1558
1559 // Xterm also shows the cursor on soft reset, but does not alter the blink
1560 // state.
rgindaa19afe22012-01-25 15:40:22 -08001561 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001562};
1563
rgindac9bc5502012-01-18 11:48:44 -08001564/**
1565 * Move the cursor forward to the next tab stop, or to the last column
1566 * if no more tab stops are set.
1567 */
1568hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001569 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001570
Mike Frysingerdc727792020-04-10 01:41:13 -04001571 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001572 if (this.tabStops_[i] > column) {
1573 this.setCursorColumn(this.tabStops_[i]);
1574 return;
1575 }
1576 }
1577
David Benjamin66e954d2012-05-05 21:08:12 -04001578 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001579 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001580 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001581 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001582};
1583
rgindac9bc5502012-01-18 11:48:44 -08001584/**
1585 * Move the cursor backward to the previous tab stop, or to the first column
1586 * if no previous tab stops are set.
1587 */
1588hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001589 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001590
Mike Frysingerdc727792020-04-10 01:41:13 -04001591 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001592 if (this.tabStops_[i] < column) {
1593 this.setCursorColumn(this.tabStops_[i]);
1594 return;
1595 }
1596 }
1597
1598 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001599};
1600
rgindac9bc5502012-01-18 11:48:44 -08001601/**
1602 * Set a tab stop at the given column.
1603 *
Joel Hockey0f933582019-08-27 18:01:51 -07001604 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001605 */
1606hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001607 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001608 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001609 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001610 }
rgindac9bc5502012-01-18 11:48:44 -08001611
1612 if (this.tabStops_[i] < column) {
1613 this.tabStops_.splice(i + 1, 0, column);
1614 return;
1615 }
1616 }
1617
1618 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001619};
1620
rgindac9bc5502012-01-18 11:48:44 -08001621/**
1622 * Clear the tab stop at the current cursor position.
1623 *
1624 * No effect if there is no tab stop at the current cursor position.
1625 */
1626hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001627 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001628
Mike Frysingerdc727792020-04-10 01:41:13 -04001629 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001630 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001631 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001632 }
rgindac9bc5502012-01-18 11:48:44 -08001633
1634 this.tabStops_.splice(i, 1);
1635};
1636
1637/**
1638 * Clear all tab stops.
1639 */
1640hterm.Terminal.prototype.clearAllTabStops = function() {
1641 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001642 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001643};
1644
1645/**
1646 * Set up the default tab stops, starting from a given column.
1647 *
1648 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001649 * from the specified column, or 0 if no column is provided. It also flags
1650 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001651 *
1652 * This does not clear the existing tab stops first, use clearAllTabStops
1653 * for that.
1654 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001655 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001656 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001657 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001658hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001659 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001660 // Round start up to a default tab stop.
1661 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001662 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001663 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001664 }
David Benjamin66e954d2012-05-05 21:08:12 -04001665
1666 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001667};
1668
rginda6d397402012-01-17 10:58:29 -08001669/**
rginda8ba33642011-12-14 12:31:31 -08001670 * Interpret a sequence of characters.
1671 *
1672 * Incomplete escape sequences are buffered until the next call.
1673 *
1674 * @param {string} str Sequence of characters to interpret or pass through.
1675 */
1676hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001677 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001678 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001679};
1680
1681/**
1682 * Take over the given DIV for use as the terminal display.
1683 *
Joel Hockey0f933582019-08-27 18:01:51 -07001684 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001685 */
1686hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001687 const charset = div.ownerDocument.characterSet.toLowerCase();
1688 if (charset != 'utf-8') {
1689 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1690 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1691 }
1692
rginda87b86462011-12-14 13:48:03 -08001693 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001694 this.div_.style.borderStyle = 'solid';
1695 this.div_.style.borderWidth = 0;
1696 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001697
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001698 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1699
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001700 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1701};
1702
1703/**
1704 * Initialisation of ScrollPort properties which need to be set after its DOM
1705 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001706 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001707 * @private
1708 */
1709hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001710 this.scrollPort_.setBackgroundImage(
1711 this.prefs_.getString('background-image'));
1712 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001713 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001714 this.prefs_.getString('background-position'));
1715 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1716 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1717 this.scrollPort_.setAccessibilityReader(
1718 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001719
rginda0918b652012-04-04 11:26:24 -07001720 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001721
Joel Hockeyd4fca732019-09-20 16:57:03 -07001722 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001723 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001724
Joel Hockeyd4fca732019-09-20 16:57:03 -07001725 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001726 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001727 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001728
rginda8ba33642011-12-14 12:31:31 -08001729 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001730 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301731 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001732
Evan Jones5f9df812016-12-06 09:38:58 -05001733 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001734 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001735
Mike Frysingerdc727792020-04-10 01:41:13 -04001736 const onMouse = this.onMouse_.bind(this);
1737 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001738 screenNode.addEventListener(
1739 'mousedown', /** @type {!EventListener} */ (onMouse));
1740 screenNode.addEventListener(
1741 'mouseup', /** @type {!EventListener} */ (onMouse));
1742 screenNode.addEventListener(
1743 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001744 this.scrollPort_.onScrollWheel = onMouse;
1745
Joel Hockeyd4fca732019-09-20 16:57:03 -07001746 screenNode.addEventListener(
1747 'keydown',
1748 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001749
Toni Barzic0bfa8922013-11-22 11:18:35 -08001750 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001751 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001752 // Listen for mousedown events on the screenNode as in FF the focus
1753 // events don't bubble.
1754 screenNode.addEventListener('mousedown', function() {
1755 setTimeout(this.onFocusChange_.bind(this, true));
1756 }.bind(this));
1757
Toni Barzic0bfa8922013-11-22 11:18:35 -08001758 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001759 'blur', this.onFocusChange_.bind(this, false));
1760
Mike Frysingerdc727792020-04-10 01:41:13 -04001761 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001762 style.textContent = `
1763.cursor-node[focus="false"] {
1764 box-sizing: border-box;
1765 background-color: transparent !important;
1766 border-width: 2px;
1767 border-style: solid;
1768}
1769menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001770 background: #fff;
1771 border-radius: 4px;
1772 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001773 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001774 display: none;
1775 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1776 margin: 0;
1777 padding: 8px 0;
1778 position: absolute;
1779 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001780}
1781menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001782 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001783 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1784 padding: 0.5em 1em;
1785 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001786}
1787menuitem.separator {
1788 border-bottom: none;
1789 height: 0.5em;
1790 padding: 0;
1791}
1792menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001793 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001794}
1795.wc-node {
1796 display: inline-block;
1797 text-align: center;
1798 width: calc(var(--hterm-charsize-width) * 2);
1799 line-height: var(--hterm-charsize-height);
1800}
1801:root {
1802 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1803 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001804 --hterm-blink-node-duration: 0.7s;
1805 --hterm-mouse-cursor-default: default;
1806 --hterm-mouse-cursor-text: text;
1807 --hterm-mouse-cursor-pointer: pointer;
1808 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001809 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001810
Mike Frysinger06ce15d2020-10-21 21:53:29 -04001811${lib.colors.stockPalette.map((c, i) => `
Joel Hockey42dba8f2020-03-26 16:21:11 -07001812 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1813`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001814}
1815.uri-node:hover {
1816 text-decoration: underline;
1817 cursor: var(--hterm-mouse-cursor-pointer);
1818}
1819@keyframes blink {
1820 from { opacity: 1.0; }
1821 to { opacity: 0.0; }
1822}
1823.blink-node {
1824 animation-name: blink;
1825 animation-duration: var(--hterm-blink-node-duration);
1826 animation-iteration-count: infinite;
1827 animation-timing-function: ease-in-out;
1828 animation-direction: alternate;
1829}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001830 // Insert this stock style as the first node so that any user styles will
1831 // override w/out having to use !important everywhere. The rules above mix
1832 // runtime variables with default ones designed to be overridden by the user,
1833 // but we can wait for a concrete case from the users to determine the best
1834 // way to split the sheet up to before & after the user-css settings.
1835 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001836
rginda8ba33642011-12-14 12:31:31 -08001837 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001838 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001839 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001840 this.cursorNode_.style.cssText = `
1841position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001842left: calc(var(--hterm-screen-padding-size) +
1843 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1844top: calc(var(--hterm-screen-padding-size) +
1845 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001846display: ${this.options_.cursorVisible ? '' : 'none'};
1847width: var(--hterm-charsize-width);
1848height: var(--hterm-charsize-height);
1849background-color: var(--hterm-cursor-color);
1850border-color: var(--hterm-cursor-color);
1851-webkit-transition: opacity, background-color 100ms linear;
1852-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001853
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001854 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001855 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1856 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001857
rginda8ba33642011-12-14 12:31:31 -08001858 this.document_.body.appendChild(this.cursorNode_);
1859
rgindad5613292012-06-19 15:40:37 -07001860 // When 'enableMouseDragScroll' is off we reposition this element directly
1861 // under the mouse cursor after a click. This makes Chrome associate
1862 // subsequent mousemove events with the scroll-blocker. Since the
1863 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1864 // events do not cause the scrollport to scroll.
1865 //
1866 // It's a hack, but it's the cleanest way I could find.
1867 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001868 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001869 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001870 this.scrollBlockerNode_.style.cssText =
1871 ('position: absolute;' +
1872 'top: -99px;' +
1873 'display: block;' +
1874 'width: 10px;' +
1875 'height: 10px;');
1876 this.document_.body.appendChild(this.scrollBlockerNode_);
1877
rgindad5613292012-06-19 15:40:37 -07001878 this.scrollPort_.onScrollWheel = onMouse;
1879 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1880 ].forEach(function(event) {
1881 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001882 this.cursorNode_.addEventListener(
1883 event, /** @type {!EventListener} */ (onMouse));
1884 this.document_.addEventListener(
1885 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001886 }.bind(this));
1887
1888 this.cursorNode_.addEventListener('mousedown', function() {
1889 setTimeout(this.focus.bind(this));
1890 }.bind(this));
1891
rginda8ba33642011-12-14 12:31:31 -08001892 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001893
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001894 // Re-sync fonts whenever a web font loads.
1895 this.document_.fonts.addEventListener(
1896 'loadingdone', () => this.syncFontFamily());
1897
rginda87b86462011-12-14 13:48:03 -08001898 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001899 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001900};
1901
rginda0918b652012-04-04 11:26:24 -07001902/**
1903 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001904 *
Joel Hockey0f933582019-08-27 18:01:51 -07001905 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001906 */
rginda87b86462011-12-14 13:48:03 -08001907hterm.Terminal.prototype.getDocument = function() {
1908 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001909};
1910
1911/**
rginda0918b652012-04-04 11:26:24 -07001912 * Focus the terminal.
1913 */
1914hterm.Terminal.prototype.focus = function() {
1915 this.scrollPort_.focus();
1916};
1917
1918/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001919 * Unfocus the terminal.
1920 */
1921hterm.Terminal.prototype.blur = function() {
1922 this.scrollPort_.blur();
1923};
1924
1925/**
rginda8ba33642011-12-14 12:31:31 -08001926 * Return the HTML Element for a given row index.
1927 *
1928 * This is a method from the RowProvider interface. The ScrollPort uses
1929 * it to fetch rows on demand as they are scrolled into view.
1930 *
1931 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1932 * pairs to conserve memory.
1933 *
Joel Hockey0f933582019-08-27 18:01:51 -07001934 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001935 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001936 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001937 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001938 * @override
rginda8ba33642011-12-14 12:31:31 -08001939 */
1940hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001941 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001942 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001943 }
rginda8ba33642011-12-14 12:31:31 -08001944
Mike Frysingerdc727792020-04-10 01:41:13 -04001945 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001946 return this.screen_.rowsArray[screenIndex];
1947};
1948
1949/**
1950 * Return the text content for a given range of rows.
1951 *
1952 * This is a method from the RowProvider interface. The ScrollPort uses
1953 * it to fetch text content on demand when the user attempts to copy their
1954 * selection to the clipboard.
1955 *
Joel Hockey0f933582019-08-27 18:01:51 -07001956 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001957 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001958 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001959 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001960 * relative to the start of the scrollback buffer.
1961 * @return {string} A single string containing the text value of the range of
1962 * rows. Lines will be newline delimited, with no trailing newline.
1963 */
1964hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001965 const ary = [];
1966 for (let i = start; i < end; i++) {
1967 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001968 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001969 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001970 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001971 }
rginda8ba33642011-12-14 12:31:31 -08001972 }
1973
rgindaa09e7332012-08-17 12:49:51 -07001974 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001975};
1976
1977/**
1978 * Return the text content for a given row.
1979 *
1980 * This is a method from the RowProvider interface. The ScrollPort uses
1981 * it to fetch text content on demand when the user attempts to copy their
1982 * selection to the clipboard.
1983 *
Joel Hockey0f933582019-08-27 18:01:51 -07001984 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001985 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001986 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001987 * @return {string} A string containing the text value of the selected row.
1988 */
1989hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001990 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001991 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001992};
1993
1994/**
1995 * Return the total number of rows in the addressable screen and in the
1996 * scrollback buffer of this terminal.
1997 *
1998 * This is a method from the RowProvider interface. The ScrollPort uses
1999 * it to compute the size of the scrollbar.
2000 *
Joel Hockey0f933582019-08-27 18:01:51 -07002001 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07002002 * @override
rginda8ba33642011-12-14 12:31:31 -08002003 */
2004hterm.Terminal.prototype.getRowCount = function() {
2005 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
2006};
2007
2008/**
2009 * Create DOM nodes for new rows and append them to the end of the terminal.
2010 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002011 * The new row is appended to the bottom of the list of rows, and does not
rginda8ba33642011-12-14 12:31:31 -08002012 * require renumbering (of the rowIndex property) of previous rows.
2013 *
2014 * If you think you want a new blank row somewhere in the middle of the
Joel Hockey95fe54e2020-07-23 01:12:24 -07002015 * terminal, look into insertRow_() or moveRows_().
rginda8ba33642011-12-14 12:31:31 -08002016 *
2017 * This method does not pay attention to vtScrollTop/Bottom, since you should
Joel Hockey95fe54e2020-07-23 01:12:24 -07002018 * be using insertRow_() or moveRows_() in cases where they would matter.
rginda8ba33642011-12-14 12:31:31 -08002019 *
2020 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002021 *
2022 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002023 */
2024hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002025 let cursorRow = this.screen_.rowsArray.length;
2026 const offset = this.scrollbackRows_.length + cursorRow;
2027 for (let i = 0; i < count; i++) {
2028 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002029 row.appendChild(this.document_.createTextNode(''));
2030 row.rowIndex = offset + i;
2031 this.screen_.pushRow(row);
2032 }
2033
Mike Frysingerdc727792020-04-10 01:41:13 -04002034 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002035 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002036 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002037 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002038 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002039 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002040 }
rginda8ba33642011-12-14 12:31:31 -08002041 }
2042
Mike Frysingerbdb34802020-04-07 03:47:32 -04002043 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002044 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002045 }
rginda8ba33642011-12-14 12:31:31 -08002046
rginda87b86462011-12-14 13:48:03 -08002047 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002048};
2049
2050/**
Joel Hockey95fe54e2020-07-23 01:12:24 -07002051 * Create a DOM node for a new row and insert it at the current position.
2052 *
2053 * The new row is inserted at the current cursor position, the existing top row
2054 * is moved to scrollback, and lines below are renumbered.
2055 *
2056 * The cursor will be positioned at column 0.
2057 */
2058hterm.Terminal.prototype.insertRow_ = function() {
2059 const row = this.document_.createElement('x-row');
2060 row.appendChild(this.document_.createTextNode(''));
2061
2062 this.scrollbackRows_.push(this.screen_.shiftRow());
2063
2064 const cursorRow = this.screen_.cursorPosition.row;
2065 this.screen_.insertRow(cursorRow, row);
2066
2067 this.renumberRows_(cursorRow, this.screen_.rowsArray.length);
2068
2069 this.setAbsoluteCursorPosition(cursorRow, 0);
2070 if (this.scrollPort_.isScrolledEnd) {
2071 this.scheduleScrollDown_();
2072 }
2073};
2074
2075/**
rginda8ba33642011-12-14 12:31:31 -08002076 * Relocate rows from one part of the addressable screen to another.
2077 *
Joel Hockey95fe54e2020-07-23 01:12:24 -07002078 * This is used to recycle rows during VT scrolls where a top region is set
2079 * (those which are driven by VT commands, rather than by the user manipulating
2080 * the scrollbar.)
rginda8ba33642011-12-14 12:31:31 -08002081 *
2082 * In this case, the blank lines scrolled into the scroll region are made of
2083 * the nodes we scrolled off. These have their rowIndex properties carefully
2084 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002085 *
2086 * @param {number} fromIndex The start index.
2087 * @param {number} count The number of rows to move.
2088 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002089 */
2090hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002091 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002092 this.screen_.insertRows(toIndex, ary);
2093
Mike Frysingerdc727792020-04-10 01:41:13 -04002094 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002095 if (fromIndex < toIndex) {
2096 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002097 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002098 } else {
2099 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002100 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002101 }
2102
2103 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002104 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002105};
2106
2107/**
2108 * Renumber the rowIndex property of the given range of rows.
2109 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002110 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002111 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002112 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002113 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002114 *
2115 * @param {number} start The start index.
2116 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002117 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002118 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002119hterm.Terminal.prototype.renumberRows_ = function(
2120 start, end, screen = undefined) {
2121 if (!screen) {
2122 screen = this.screen_;
2123 }
Robert Ginda40932892012-12-10 17:26:40 -08002124
Mike Frysingerdc727792020-04-10 01:41:13 -04002125 const offset = this.scrollbackRows_.length;
2126 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002127 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002128 }
2129};
2130
2131/**
2132 * Print a string to the terminal.
2133 *
2134 * This respects the current insert and wraparound modes. It will add new lines
2135 * to the end of the terminal, scrolling off the top into the scrollback buffer
2136 * if necessary.
2137 *
2138 * The string is *not* parsed for escape codes. Use the interpret() method if
2139 * that's what you're after.
2140 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002141 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002142 */
2143hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002144 this.scheduleSyncCursorPosition_();
2145
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002146 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002147 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002148
Mike Frysingerdc727792020-04-10 01:41:13 -04002149 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002150
Mike Frysingerdc727792020-04-10 01:41:13 -04002151 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002152 // Fun edge case: If the string only contains zero width codepoints (like
2153 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002154 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002155 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002156 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002157
2158 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002159 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2160 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002161 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002162 }
rgindaa19afe22012-01-25 15:40:22 -08002163
Mike Frysingerdc727792020-04-10 01:41:13 -04002164 let count = strWidth - startOffset;
2165 let didOverflow = false;
2166 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002167
rgindaa9abdd82012-08-06 18:05:09 -07002168 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2169 didOverflow = true;
2170 count = this.screenSize.width - this.screen_.cursorPosition.column;
2171 }
rgindaa19afe22012-01-25 15:40:22 -08002172
rgindaa9abdd82012-08-06 18:05:09 -07002173 if (didOverflow && !this.options_.wraparound) {
2174 // If the string overflowed the line but wraparound is off, then the
2175 // last printed character should be the last of the string.
2176 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002177 substr = lib.wc.substr(str, startOffset, count - 1) +
2178 lib.wc.substr(str, strWidth - 1);
2179 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002180 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002181 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002182 }
rgindaa19afe22012-01-25 15:40:22 -08002183
Mike Frysingerdc727792020-04-10 01:41:13 -04002184 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2185 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002186 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2187 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002188
2189 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002190 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002191 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002192 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002193 }
2194 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002195 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002196 }
2197
2198 this.screen_.maybeClipCurrentRow();
2199 startOffset += count;
Shivang Garga72e68c2020-07-18 08:58:32 +05302200 this.findBar.scheduleNotifyChanges(
2201 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rgindaa19afe22012-01-25 15:40:22 -08002202 }
rginda8ba33642011-12-14 12:31:31 -08002203
Mike Frysingerbdb34802020-04-07 03:47:32 -04002204 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002205 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002206 }
rginda8ba33642011-12-14 12:31:31 -08002207};
2208
2209/**
rginda87b86462011-12-14 13:48:03 -08002210 * Set the VT scroll region.
2211 *
rginda87b86462011-12-14 13:48:03 -08002212 * This also resets the cursor position to the absolute (0, 0) position, since
2213 * that's what xterm appears to do.
2214 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002215 * Setting the scroll region to the full height of the terminal will clear
2216 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2217 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2218 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2219 * continue to work as most users would expect.
2220 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002221 * @param {?number} scrollTop The zero-based top of the scroll region.
2222 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002223 * inclusive.
2224 */
2225hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Joel Hockey95fe54e2020-07-23 01:12:24 -07002226 this.vtScrollTop_ = scrollTop;
Joel Hockey4337eac2020-09-11 01:34:38 -07002227 this.vtScrollBottom_ = scrollBottom;
2228 if (scrollBottom == this.screenSize.height - 1) {
2229 this.vtScrollBottom_ = null;
2230 if (scrollTop == 0) {
2231 this.vtScrollTop_ = null;
2232 }
2233 }
rginda87b86462011-12-14 13:48:03 -08002234};
2235
2236/**
rginda8ba33642011-12-14 12:31:31 -08002237 * Return the top row index according to the VT.
2238 *
2239 * This will return 0 unless the terminal has been told to restrict scrolling
2240 * to some lower row. It is used for some VT cursor positioning and scrolling
2241 * commands.
2242 *
Joel Hockey0f933582019-08-27 18:01:51 -07002243 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002244 */
2245hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002246 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002247 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002248 }
rginda8ba33642011-12-14 12:31:31 -08002249
2250 return 0;
rginda87b86462011-12-14 13:48:03 -08002251};
rginda8ba33642011-12-14 12:31:31 -08002252
2253/**
2254 * Return the bottom row index according to the VT.
2255 *
2256 * This will return the height of the terminal unless the it has been told to
2257 * restrict scrolling to some higher row. It is used for some VT cursor
2258 * positioning and scrolling commands.
2259 *
Joel Hockey0f933582019-08-27 18:01:51 -07002260 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002261 */
2262hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002263 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002264 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002265 }
rginda8ba33642011-12-14 12:31:31 -08002266
rginda87b86462011-12-14 13:48:03 -08002267 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002268};
rginda8ba33642011-12-14 12:31:31 -08002269
2270/**
2271 * Process a '\n' character.
2272 *
2273 * If the cursor is on the final row of the terminal this will append a new
2274 * blank row to the screen and scroll the topmost row into the scrollback
2275 * buffer.
2276 *
2277 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002278 *
2279 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2280 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002281 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002282hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002283 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002284 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002285 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002286
Joel Hockey95fe54e2020-07-23 01:12:24 -07002287 const cursorAtEndOfScreen =
2288 (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1);
2289 const cursorAtEndOfVTRegion =
2290 (this.screen_.cursorPosition.row == this.getVTScrollBottom());
Robert Ginda9937abc2013-07-25 16:09:23 -07002291
Joel Hockey95fe54e2020-07-23 01:12:24 -07002292 if (this.vtScrollTop_ != null && cursorAtEndOfVTRegion) {
2293 // A VT Scroll region is active on top, we never append new rows.
2294 // We're at the end of the VT Scroll Region, perform a VT scroll.
2295 this.vtScrollUp(1);
2296 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
Robert Ginda9937abc2013-07-25 16:09:23 -07002297 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002298 // We're at the end of the screen. Append a new row to the terminal,
2299 // shifting the top row into the scrollback.
2300 this.appendRows_(1);
Joel Hockey95fe54e2020-07-23 01:12:24 -07002301 } else if (cursorAtEndOfVTRegion) {
2302 this.insertRow_();
rginda8ba33642011-12-14 12:31:31 -08002303 } else {
rginda87b86462011-12-14 13:48:03 -08002304 // Anywhere else in the screen just moves the cursor.
2305 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002306 }
2307};
2308
2309/**
2310 * Like newLine(), except maintain the cursor column.
2311 */
2312hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002313 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002314 this.newLine();
2315 this.setCursorColumn(column);
2316};
2317
2318/**
rginda87b86462011-12-14 13:48:03 -08002319 * If autoCarriageReturn is set then newLine(), else lineFeed().
2320 */
2321hterm.Terminal.prototype.formFeed = function() {
2322 if (this.options_.autoCarriageReturn) {
2323 this.newLine();
2324 } else {
2325 this.lineFeed();
2326 }
2327};
2328
2329/**
2330 * Move the cursor up one row, possibly inserting a blank line.
2331 *
2332 * The cursor column is not changed.
2333 */
2334hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002335 const scrollTop = this.getVTScrollTop();
2336 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002337
2338 if (currentRow == scrollTop) {
2339 this.insertLines(1);
2340 } else {
2341 this.setAbsoluteCursorRow(currentRow - 1);
2342 }
2343};
2344
2345/**
rginda8ba33642011-12-14 12:31:31 -08002346 * Replace all characters to the left of the current cursor with the space
2347 * character.
2348 *
2349 * TODO(rginda): This should probably *remove* the characters (not just replace
2350 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002351 * position.
rginda8ba33642011-12-14 12:31:31 -08002352 */
2353hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002354 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002355 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002356 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002357 this.screen_.overwriteString(' '.repeat(count), count);
Shivang Garga72e68c2020-07-18 08:58:32 +05302358 this.findBar.scheduleNotifyChanges(
2359 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002360 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002361};
2362
2363/**
David Benjamin684a9b72012-05-01 17:19:58 -04002364 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002365 *
2366 * The cursor position is unchanged.
2367 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002368 * If the current background color is not the default background color this
2369 * will insert spaces rather than delete. This is unfortunate because the
2370 * trailing space will affect text selection, but it's difficult to come up
2371 * with a way to style empty space that wouldn't trip up the hterm.Screen
2372 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002373 *
2374 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2375 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2376 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002377 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002378 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002379 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002380hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002381 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002382 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002383 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002384
Mike Frysingerdc727792020-04-10 01:41:13 -04002385 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002386 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002387
Shivang Garga72e68c2020-07-18 08:58:32 +05302388 this.findBar.scheduleNotifyChanges(
2389 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
2390
Robert Gindaf2547f12012-10-25 20:36:21 -07002391 if (this.screen_.textAttributes.background ===
2392 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002393 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002394 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002395 this.screen_.cursorPosition.column + count) {
2396 this.screen_.deleteChars(count);
2397 this.clearCursorOverflow();
2398 return;
2399 }
2400 }
2401
Mike Frysingerdc727792020-04-10 01:41:13 -04002402 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002403 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002404 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002405 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002406};
2407
2408/**
2409 * Erase the current line.
2410 *
2411 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002412 */
2413hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002414 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002415 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002416 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002417 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002418};
2419
2420/**
David Benjamina08d78f2012-05-05 00:28:49 -04002421 * Erase all characters from the start of the screen to the current cursor
2422 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002423 *
2424 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002425 */
2426hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002427 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002428
2429 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002430
Mike Frysingerdc727792020-04-10 01:41:13 -04002431 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002432 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002433 this.screen_.clearCursorRow();
2434 }
2435
rginda87b86462011-12-14 13:48:03 -08002436 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002437 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002438};
2439
2440/**
2441 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002442 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002443 *
2444 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002445 */
2446hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002447 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002448
2449 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002450
Mike Frysingerdc727792020-04-10 01:41:13 -04002451 const bottom = this.screenSize.height - 1;
2452 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002453 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002454 this.screen_.clearCursorRow();
2455 }
2456
rginda87b86462011-12-14 13:48:03 -08002457 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002458 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002459};
2460
2461/**
2462 * Fill the terminal with a given character.
2463 *
2464 * This methods does not respect the VT scroll region.
2465 *
2466 * @param {string} ch The character to use for the fill.
2467 */
2468hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002469 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002470
2471 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002472 for (let row = 0; row < this.screenSize.height; row++) {
2473 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002474 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002475 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002476 }
Shivang Garga72e68c2020-07-18 08:58:32 +05302477 this.findBar.scheduleNotifyChanges(this.scrollbackRows_.length + row);
rginda87b86462011-12-14 13:48:03 -08002478 }
2479
2480 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002481};
2482
2483/**
rginda9ea433c2012-03-16 11:57:00 -07002484 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002485 *
rginda9ea433c2012-03-16 11:57:00 -07002486 * This does not respect the scroll region.
2487 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002488 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002489 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002490 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002491hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2492 if (!screen) {
2493 screen = this.screen_;
2494 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002495 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002496
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002497 this.accessibilityReader_.clear();
2498
rginda11057d52012-04-25 12:29:56 -07002499 if (bottom == 0) {
2500 // Empty screen, nothing to do.
2501 return;
2502 }
2503
Mike Frysingerdc727792020-04-10 01:41:13 -04002504 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002505 screen.setCursorPosition(i, 0);
2506 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002507 }
2508
rginda9ea433c2012-03-16 11:57:00 -07002509 screen.setCursorPosition(0, 0);
2510};
2511
2512/**
2513 * Erase the entire display without changing the cursor position.
2514 *
2515 * The cursor position is unchanged. This does not respect the scroll
2516 * region.
2517 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002518 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002519 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002520 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002521hterm.Terminal.prototype.clear = function(screen = undefined) {
2522 if (!screen) {
2523 screen = this.screen_;
2524 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002525 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002526 this.clearHome(screen);
2527 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002528};
2529
2530/**
2531 * VT command to insert lines at the current cursor row.
2532 *
2533 * This respects the current scroll region. Rows pushed off the bottom are
2534 * lost (they won't show up in the scrollback buffer).
2535 *
Joel Hockey0f933582019-08-27 18:01:51 -07002536 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002537 */
2538hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002539 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002540
Mike Frysingerdc727792020-04-10 01:41:13 -04002541 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002542 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002543
Robert Ginda579186b2012-09-26 11:40:04 -07002544 // The moveCount is the number of rows we need to relocate to make room for
2545 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002546 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002547 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002548 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002549 }
rginda8ba33642011-12-14 12:31:31 -08002550
Mike Frysingerdc727792020-04-10 01:41:13 -04002551 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002552 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002553 this.screen_.clearCursorRow();
2554 }
rginda8ba33642011-12-14 12:31:31 -08002555};
2556
2557/**
2558 * VT command to delete lines at the current cursor row.
2559 *
2560 * New rows are added to the bottom of scroll region to take their place. New
2561 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002562 *
2563 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002564 */
2565hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002566 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002567
Mike Frysingerdc727792020-04-10 01:41:13 -04002568 const top = cursor.row;
2569 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002570
Mike Frysingerdc727792020-04-10 01:41:13 -04002571 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002572 count = Math.min(count, maxCount);
2573
Mike Frysingerdc727792020-04-10 01:41:13 -04002574 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002575 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002576 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002577 }
rginda8ba33642011-12-14 12:31:31 -08002578
Mike Frysingerdc727792020-04-10 01:41:13 -04002579 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002580 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002581 this.screen_.clearCursorRow();
2582 }
2583
rginda87b86462011-12-14 13:48:03 -08002584 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002585 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002586};
2587
2588/**
2589 * Inserts the given number of spaces at the current cursor position.
2590 *
rginda87b86462011-12-14 13:48:03 -08002591 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002592 *
2593 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002594 */
2595hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002596 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002597
Mike Frysinger73e56462019-07-17 00:23:46 -05002598 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002599 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002600 this.screen_.maybeClipCurrentRow();
Shivang Garga72e68c2020-07-18 08:58:32 +05302601 this.findBar.scheduleNotifyChanges(
2602 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
rginda87b86462011-12-14 13:48:03 -08002603
2604 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002605 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002606};
2607
2608/**
2609 * Forward-delete the specified number of characters starting at the cursor
2610 * position.
2611 *
Joel Hockey0f933582019-08-27 18:01:51 -07002612 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002613 */
2614hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002615 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002616 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002617 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002618 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002619 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002620 this.restoreCursor(cursor);
2621 }
2622
Shivang Garga72e68c2020-07-18 08:58:32 +05302623 this.findBar.scheduleNotifyChanges(
2624 this.scrollbackRows_.length + this.screen_.cursorPosition.row);
David Benjamin54e8bf62012-06-01 22:31:40 -04002625 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002626};
2627
2628/**
2629 * Shift rows in the scroll region upwards by a given number of lines.
2630 *
2631 * New rows are inserted at the bottom of the scroll region to fill the
2632 * vacated rows. The new rows not filled out with the current text attributes.
2633 *
2634 * This function does not affect the scrollback rows at all. Rows shifted
2635 * off the top are lost.
2636 *
rginda87b86462011-12-14 13:48:03 -08002637 * The cursor position is not altered.
2638 *
Joel Hockey0f933582019-08-27 18:01:51 -07002639 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002640 */
2641hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002642 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002643
rginda87b86462011-12-14 13:48:03 -08002644 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002645 this.deleteLines(count);
2646
rginda87b86462011-12-14 13:48:03 -08002647 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002648};
2649
2650/**
2651 * Shift rows below the cursor down by a given number of lines.
2652 *
2653 * This function respects the current scroll region.
2654 *
2655 * New rows are inserted at the top of the scroll region to fill the
2656 * vacated rows. The new rows not filled out with the current text attributes.
2657 *
2658 * This function does not affect the scrollback rows at all. Rows shifted
2659 * off the bottom are lost.
2660 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002661 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002662 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002663hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002664 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002665
rginda87b86462011-12-14 13:48:03 -08002666 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002667 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002668
rginda87b86462011-12-14 13:48:03 -08002669 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002670};
2671
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002672/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002673 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002674 *
2675 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002676 * cause Assitive Technology to announce the output of the terminal. It also
2677 * enables other features that aid assistive technology. All the features gated
2678 * behind this flag have a performance impact on the terminal which is why they
2679 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002680 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002681 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002682 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002683hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002684 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002685};
rginda87b86462011-12-14 13:48:03 -08002686
rginda8ba33642011-12-14 12:31:31 -08002687/**
2688 * Set the cursor position.
2689 *
2690 * The cursor row is relative to the scroll region if the terminal has
2691 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2692 *
Joel Hockey0f933582019-08-27 18:01:51 -07002693 * @param {number} row The new zero-based cursor row.
2694 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002695 */
2696hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2697 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002698 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002699 } else {
rginda87b86462011-12-14 13:48:03 -08002700 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002701 }
rginda87b86462011-12-14 13:48:03 -08002702};
rginda8ba33642011-12-14 12:31:31 -08002703
Evan Jones2600d4f2016-12-06 09:29:36 -05002704/**
2705 * Move the cursor relative to its current position.
2706 *
2707 * @param {number} row
2708 * @param {number} column
2709 */
rginda87b86462011-12-14 13:48:03 -08002710hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002711 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002712 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2713 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002714 this.screen_.setCursorPosition(row, column);
2715};
2716
Evan Jones2600d4f2016-12-06 09:29:36 -05002717/**
2718 * Move the cursor to the specified position.
2719 *
2720 * @param {number} row
2721 * @param {number} column
2722 */
rginda87b86462011-12-14 13:48:03 -08002723hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002724 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2725 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002726 this.screen_.setCursorPosition(row, column);
2727};
2728
2729/**
2730 * Set the cursor column.
2731 *
Joel Hockey0f933582019-08-27 18:01:51 -07002732 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002733 */
2734hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002735 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002736};
2737
2738/**
2739 * Return the cursor column.
2740 *
Joel Hockey0f933582019-08-27 18:01:51 -07002741 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002742 */
2743hterm.Terminal.prototype.getCursorColumn = function() {
2744 return this.screen_.cursorPosition.column;
2745};
2746
2747/**
2748 * Set the cursor row.
2749 *
2750 * The cursor row is relative to the scroll region if the terminal has
2751 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2752 *
Joel Hockey0f933582019-08-27 18:01:51 -07002753 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002754 */
rginda87b86462011-12-14 13:48:03 -08002755hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2756 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002757};
2758
2759/**
2760 * Return the cursor row.
2761 *
Joel Hockey0f933582019-08-27 18:01:51 -07002762 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002763 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002764hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002765 return this.screen_.cursorPosition.row;
2766};
2767
2768/**
2769 * Request that the ScrollPort redraw itself soon.
2770 *
2771 * The redraw will happen asynchronously, soon after the call stack winds down.
2772 * Multiple calls will be coalesced into a single redraw.
2773 */
2774hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002775 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002776 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002777 }
rginda8ba33642011-12-14 12:31:31 -08002778
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002779 this.timeouts_.redraw = setTimeout(() => {
2780 delete this.timeouts_.redraw;
2781 this.scrollPort_.redraw_();
2782 });
rginda8ba33642011-12-14 12:31:31 -08002783};
2784
2785/**
2786 * Request that the ScrollPort be scrolled to the bottom.
2787 *
2788 * The scroll will happen asynchronously, soon after the call stack winds down.
2789 * Multiple calls will be coalesced into a single scroll.
2790 *
2791 * This affects the scrollbar position of the ScrollPort, and has nothing to
2792 * do with the VT scroll commands.
2793 */
2794hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002795 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002796 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002797 }
rginda8ba33642011-12-14 12:31:31 -08002798
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002799 this.timeouts_.scrollDown = setTimeout(() => {
2800 delete this.timeouts_.scrollDown;
2801 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2802 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002803};
2804
2805/**
2806 * Move the cursor up a specified number of rows.
2807 *
Joel Hockey0f933582019-08-27 18:01:51 -07002808 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002809 */
2810hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002811 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002812};
2813
2814/**
2815 * Move the cursor down a specified number of rows.
2816 *
Joel Hockey0f933582019-08-27 18:01:51 -07002817 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002818 */
2819hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002820 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002821 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2822 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2823 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002824
Mike Frysingerdc727792020-04-10 01:41:13 -04002825 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2826 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002827 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002828};
2829
2830/**
2831 * Move the cursor left a specified number of columns.
2832 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002833 * If reverse wraparound mode is enabled and the previous row wrapped into
2834 * the current row then we back up through the wraparound as well.
2835 *
Joel Hockey0f933582019-08-27 18:01:51 -07002836 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002837 */
2838hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002839 count = count || 1;
2840
Mike Frysingerbdb34802020-04-07 03:47:32 -04002841 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002842 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002843 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002844
Mike Frysingerdc727792020-04-10 01:41:13 -04002845 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002846 if (this.options_.reverseWraparound) {
2847 if (this.screen_.cursorPosition.overflow) {
2848 // If this cursor is in the right margin, consume one count to get it
2849 // back to the last column. This only applies when we're in reverse
2850 // wraparound mode.
2851 count--;
2852 this.clearCursorOverflow();
2853
Mike Frysingerbdb34802020-04-07 03:47:32 -04002854 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002855 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002856 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002857 }
2858
Mike Frysingerdc727792020-04-10 01:41:13 -04002859 let newRow = this.screen_.cursorPosition.row;
2860 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002861 if (newColumn < 0) {
2862 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2863 if (newRow < 0) {
2864 // xterm also wraps from row 0 to the last row.
2865 newRow = this.screenSize.height + newRow % this.screenSize.height;
2866 }
2867 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2868 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002869
Robert Gindabfb32622014-07-17 13:20:27 -07002870 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2871
2872 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002873 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002874 this.setCursorColumn(newColumn);
2875 }
rginda8ba33642011-12-14 12:31:31 -08002876};
2877
2878/**
2879 * Move the cursor right a specified number of columns.
2880 *
Joel Hockey0f933582019-08-27 18:01:51 -07002881 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002882 */
2883hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002884 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002885
Mike Frysingerbdb34802020-04-07 03:47:32 -04002886 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002887 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002888 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002889
Mike Frysingerdc727792020-04-10 01:41:13 -04002890 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2891 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002892 this.setCursorColumn(column);
2893};
2894
2895/**
2896 * Reverse the foreground and background colors of the terminal.
2897 *
2898 * This only affects text that was drawn with no attributes.
2899 *
2900 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2901 * been drawn with attributes that happen to coincide with the default
2902 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002903 *
2904 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002905 */
2906hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002907 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002908 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002909 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2910 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002911 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002912 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2913 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002914 }
2915};
2916
2917/**
rginda87b86462011-12-14 13:48:03 -08002918 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002919 *
2920 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002921 */
2922hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002923 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002924
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002925 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002926
Michael Kelly485ecd12014-06-09 11:41:56 -04002927 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002928 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002929 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002930 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002931
Robert Ginda92e18102013-03-14 13:56:37 -07002932 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002933 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002934 this.bellSequelchTimeout_ = setTimeout(() => {
2935 this.bellSquelchTimeout_ = null;
2936 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002937 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002938 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002939 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002940
2941 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002942 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002943 this.bellNotificationList_.push(n);
2944 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002945 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002946 }
rginda87b86462011-12-14 13:48:03 -08002947};
2948
2949/**
rginda8ba33642011-12-14 12:31:31 -08002950 * Set the origin mode bit.
2951 *
2952 * If origin mode is on, certain VT cursor and scrolling commands measure their
2953 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2954 * to the top of the addressable screen.
2955 *
2956 * Defaults to off.
2957 *
2958 * @param {boolean} state True to set origin mode, false to unset.
2959 */
2960hterm.Terminal.prototype.setOriginMode = function(state) {
2961 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002962 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002963};
2964
2965/**
2966 * Set the insert mode bit.
2967 *
2968 * If insert mode is on, existing text beyond the cursor position will be
2969 * shifted right to make room for new text. Otherwise, new text overwrites
2970 * any existing text.
2971 *
2972 * Defaults to off.
2973 *
2974 * @param {boolean} state True to set insert mode, false to unset.
2975 */
2976hterm.Terminal.prototype.setInsertMode = function(state) {
2977 this.options_.insertMode = state;
2978};
2979
2980/**
rginda87b86462011-12-14 13:48:03 -08002981 * Set the auto carriage return bit.
2982 *
2983 * If auto carriage return is on then a formfeed character is interpreted
2984 * as a newline, otherwise it's the same as a linefeed. The difference boils
2985 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002986 *
2987 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002988 */
2989hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2990 this.options_.autoCarriageReturn = state;
2991};
2992
2993/**
rginda8ba33642011-12-14 12:31:31 -08002994 * Set the wraparound mode bit.
2995 *
2996 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2997 * to the start of the following row. Otherwise, the cursor is clamped to the
2998 * end of the screen and attempts to write past it are ignored.
2999 *
3000 * Defaults to on.
3001 *
3002 * @param {boolean} state True to set wraparound mode, false to unset.
3003 */
3004hterm.Terminal.prototype.setWraparound = function(state) {
3005 this.options_.wraparound = state;
3006};
3007
3008/**
3009 * Set the reverse-wraparound mode bit.
3010 *
3011 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
3012 * to the end of the previous row. Otherwise, the cursor is clamped to column
3013 * 0.
3014 *
3015 * Defaults to off.
3016 *
3017 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
3018 */
3019hterm.Terminal.prototype.setReverseWraparound = function(state) {
3020 this.options_.reverseWraparound = state;
3021};
3022
3023/**
3024 * Selects between the primary and alternate screens.
3025 *
3026 * If alternate mode is on, the alternate screen is active. Otherwise the
3027 * primary screen is active.
3028 *
3029 * Swapping screens has no effect on the scrollback buffer.
3030 *
3031 * Each screen maintains its own cursor position.
3032 *
3033 * Defaults to off.
3034 *
3035 * @param {boolean} state True to set alternate mode, false to unset.
3036 */
3037hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07003038 if (state == (this.screen_ == this.alternateScreen_)) {
3039 return;
3040 }
3041 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3042 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08003043 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
3044
Joel Hockey42dba8f2020-03-26 16:21:11 -07003045 // Swap color overrides.
3046 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3047 oldOverrides.forEach((c, i) => {
3048 if (!newOverrides.hasOwnProperty(i)) {
3049 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
3050 }
3051 });
3052 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
3053
rginda35c456b2012-02-09 17:29:05 -08003054 if (this.screen_.rowsArray.length &&
3055 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3056 // If the screen changed sizes while we were away, our rowIndexes may
3057 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003058 const offset = this.scrollbackRows_.length;
3059 const ary = this.screen_.rowsArray;
3060 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003061 ary[i].rowIndex = offset + i;
3062 }
3063 }
rginda8ba33642011-12-14 12:31:31 -08003064
rginda35c456b2012-02-09 17:29:05 -08003065 this.realizeWidth_(this.screenSize.width);
3066 this.realizeHeight_(this.screenSize.height);
3067 this.scrollPort_.syncScrollHeight();
3068 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003069
rginda6d397402012-01-17 10:58:29 -08003070 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003071 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003072};
3073
3074/**
3075 * Set the cursor-blink mode bit.
3076 *
3077 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3078 * a visible cursor does not blink.
3079 *
3080 * You should make sure to turn blinking off if you're going to dispose of a
3081 * terminal, otherwise you'll leak a timeout.
3082 *
3083 * Defaults to on.
3084 *
3085 * @param {boolean} state True to set cursor-blink mode, false to unset.
3086 */
3087hterm.Terminal.prototype.setCursorBlink = function(state) {
3088 this.options_.cursorBlink = state;
3089
3090 if (!state && this.timeouts_.cursorBlink) {
3091 clearTimeout(this.timeouts_.cursorBlink);
3092 delete this.timeouts_.cursorBlink;
3093 }
3094
Mike Frysingerbdb34802020-04-07 03:47:32 -04003095 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003096 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003097 }
rginda8ba33642011-12-14 12:31:31 -08003098};
3099
3100/**
3101 * Set the cursor-visible mode bit.
3102 *
3103 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3104 *
3105 * Defaults to on.
3106 *
3107 * @param {boolean} state True to set cursor-visible mode, false to unset.
3108 */
3109hterm.Terminal.prototype.setCursorVisible = function(state) {
3110 this.options_.cursorVisible = state;
3111
3112 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003113 if (this.timeouts_.cursorBlink) {
3114 clearTimeout(this.timeouts_.cursorBlink);
3115 delete this.timeouts_.cursorBlink;
3116 }
rginda87b86462011-12-14 13:48:03 -08003117 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003118 return;
3119 }
3120
rginda87b86462011-12-14 13:48:03 -08003121 this.syncCursorPosition_();
3122
3123 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003124
3125 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003126 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003127 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003128 }
rginda8ba33642011-12-14 12:31:31 -08003129
Robert Gindaea2183e2014-07-17 09:51:51 -07003130 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003131 } else {
3132 if (this.timeouts_.cursorBlink) {
3133 clearTimeout(this.timeouts_.cursorBlink);
3134 delete this.timeouts_.cursorBlink;
3135 }
3136 }
3137};
3138
3139/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003140 * Pause blinking temporarily.
3141 *
3142 * When the cursor moves around, it can be helpful to momentarily pause the
3143 * blinking. This could be when the user is typing in things, or when they're
3144 * moving around with the arrow keys.
3145 */
3146hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3147 if (!this.options_.cursorBlink) {
3148 return;
3149 }
3150
3151 this.cursorBlinkPause_ = true;
3152
3153 // If a timeout is already pending, reset the clock due to the new input.
3154 if (this.timeouts_.cursorBlinkPause) {
3155 clearTimeout(this.timeouts_.cursorBlinkPause);
3156 }
3157 // After 500ms, resume blinking. That seems like a good balance between user
3158 // input timings & responsiveness to resume.
3159 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3160 delete this.timeouts_.cursorBlinkPause;
3161 this.cursorBlinkPause_ = false;
3162 }, 500);
3163};
3164
3165/**
rginda87b86462011-12-14 13:48:03 -08003166 * Synchronizes the visible cursor and document selection with the current
3167 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003168 *
3169 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003170 */
3171hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003172 const topRowIndex = this.scrollPort_.getTopRowIndex();
3173 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3174 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003175 this.screen_.cursorPosition.row;
3176
Raymes Khoury15697f42018-07-17 11:37:18 +10003177 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003178 if (this.accessibilityReader_.accessibilityEnabled) {
3179 // Report the new position of the cursor for accessibility purposes.
3180 const cursorColumnIndex = this.screen_.cursorPosition.column;
3181 const cursorLineText =
3182 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003183 // This will force the selection to be sync'd to the cursor position if the
3184 // user has pressed a key. Generally we would only sync the cursor position
3185 // when selection is collapsed so that if the user has selected something
3186 // we don't clear the selection by moving the selection. However when a
3187 // screen reader is used, it's intuitive for entering a key to move the
3188 // selection to the cursor.
3189 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003190 this.accessibilityReader_.afterCursorChange(
3191 cursorLineText, cursorRowIndex, cursorColumnIndex);
3192 }
3193
rginda8ba33642011-12-14 12:31:31 -08003194 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003195 // Cursor is scrolled off screen, hide it.
3196 this.cursorOffScreen_ = true;
3197 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003198 return false;
rginda8ba33642011-12-14 12:31:31 -08003199 }
3200
Joel Hockey3babf302020-04-22 15:00:06 -07003201 if (this.cursorNode_.style.display == 'none') {
3202 // Re-display the terminal cursor if it was hidden.
3203 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003204 this.cursorNode_.style.display = '';
3205 }
3206
Mike Frysinger44c32202017-08-05 01:13:09 -04003207 // Position the cursor using CSS variable math. If we do the math in JS,
3208 // the float math will end up being more precise than the CSS which will
3209 // cause the cursor tracking to be off.
3210 this.setCssVar(
3211 'cursor-offset-row',
3212 `${cursorRowIndex - topRowIndex} + ` +
3213 `${this.scrollPort_.visibleRowTopMargin}px`);
3214 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003215
3216 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003217 '(' + this.screen_.cursorPosition.column +
3218 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003219 ')');
3220
Joel Hockey72f7fd62020-07-30 19:29:16 -07003221 // Update the caret for a11y purposes unless FindBar has focus which it should
3222 // keep.
3223 if (!this.findBar.hasFocus) {
3224 const selection = this.document_.getSelection();
3225 if (selection && (selection.isCollapsed || forceSyncSelection)) {
3226 this.screen_.syncSelectionCaret(selection);
3227 }
Raymes Khoury15697f42018-07-17 11:37:18 +10003228 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003229 return true;
rginda8ba33642011-12-14 12:31:31 -08003230};
3231
Robert Gindafb1be6a2013-12-11 11:56:22 -08003232/**
3233 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3234 * and character cell dimensions.
3235 */
Robert Ginda830583c2013-08-07 13:20:46 -07003236hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003237 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003238
3239 if (this.cursorNode_.getAttribute('focus') == 'false') {
3240 // Always show a block cursor when unfocused.
3241 shape = hterm.Terminal.cursorShape.BLOCK;
3242 }
3243
Mike Frysingerdc727792020-04-10 01:41:13 -04003244 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003245
3246 switch (shape) {
3247 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003248 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003249 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003250 style.borderLeftStyle = 'solid';
3251 break;
3252
3253 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003254 style.backgroundColor = 'transparent';
3255 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003256 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003257 break;
3258
3259 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003260 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003261 style.borderBottomStyle = '';
3262 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003263 break;
3264 }
3265};
3266
rginda8ba33642011-12-14 12:31:31 -08003267/**
3268 * Synchronizes the visible cursor with the current cursor coordinates.
3269 *
3270 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003271 * Multiple calls will be coalesced into a single sync. This should be called
3272 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003273 */
3274hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003275 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003276 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003277 }
rginda8ba33642011-12-14 12:31:31 -08003278
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003279 if (this.accessibilityReader_.accessibilityEnabled) {
3280 // Report the previous position of the cursor for accessibility purposes.
3281 const cursorRowIndex = this.scrollbackRows_.length +
3282 this.screen_.cursorPosition.row;
3283 const cursorColumnIndex = this.screen_.cursorPosition.column;
3284 const cursorLineText =
3285 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3286 this.accessibilityReader_.beforeCursorChange(
3287 cursorLineText, cursorRowIndex, cursorColumnIndex);
3288 }
3289
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003290 this.timeouts_.syncCursor = setTimeout(() => {
3291 this.syncCursorPosition_();
3292 delete this.timeouts_.syncCursor;
3293 });
rginda87b86462011-12-14 13:48:03 -08003294};
3295
rgindacc2996c2012-02-24 14:59:31 -08003296/**
3297 * Show the terminal overlay for a given amount of time.
3298 *
Jason Lin3d825782020-05-12 11:02:48 +10003299 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003300 *
3301 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003302 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003303 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3304 * stay up forever (or until the next overlay).
3305 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003306hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003307 this.showOverlayWithNode(new Text(msg), timeout);
3308};
3309
3310/**
3311 * Show the terminal overlay for a given amount of time.
3312 *
3313 * The terminal overlay appears in inverse video, centered over the terminal.
3314 *
3315 * @param {!Node} node The node to display in the overlay.
3316 * @param {?number=} timeout The amount of time to wait before fading out
3317 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3318 * stay up forever (or until the next overlay).
3319 */
3320hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003321 if (!this.ready_ || !this.div_) {
3322 return;
3323 }
rgindaf0090c92012-02-10 14:58:52 -08003324
Joel Hockeyedac0e72020-05-14 20:16:20 -07003325 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003326 this.overlayNode_ = this.document_.createElement('div');
3327 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003328 'color: rgb(var(--hterm-background-color));' +
3329 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003330 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003331 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003332 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003333 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003334 'position: absolute;' +
Mike Frysinger8c795ae2020-10-20 04:16:32 -04003335 'user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003336 '-webkit-transition: opacity 180ms ease-in;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003337 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003338
3339 this.overlayNode_.addEventListener('mousedown', function(e) {
3340 e.preventDefault();
3341 e.stopPropagation();
3342 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003343 }
3344
Jason Lin34567412020-05-14 10:32:09 +10003345 this.overlayNode_.textContent = ''; // Remove all children first.
3346 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003347
Mike Frysingerbdb34802020-04-07 03:47:32 -04003348 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003349 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003350 }
rgindaf0090c92012-02-10 14:58:52 -08003351
Mike Frysingerccca8552020-10-22 21:18:44 -04003352 const divSize = this.div_.getBoundingClientRect();
3353 const overlaySize = this.overlayNode_.getBoundingClientRect();
Robert Ginda97769282013-02-01 15:30:30 -08003354
Robert Ginda8a59f762014-07-23 11:29:55 -07003355 this.overlayNode_.style.top =
3356 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003357 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003358 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003359
Mike Frysingerbdb34802020-04-07 03:47:32 -04003360 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003361 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003362 }
rgindaf0090c92012-02-10 14:58:52 -08003363
Jason Lin34567412020-05-14 10:32:09 +10003364 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003365
Mike Frysingerec4225d2020-04-07 05:00:01 -04003366 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003367 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003368 }
rgindacc2996c2012-02-24 14:59:31 -08003369
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003370 this.overlayTimeout_ = setTimeout(() => {
3371 this.overlayNode_.style.opacity = '0';
3372 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003373 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003374};
3375
3376/**
3377 * Hide the terminal overlay immediately.
3378 *
3379 * Useful when we show an overlay for an event with an unknown end time.
3380 */
3381hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003382 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003383 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003384 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003385 this.overlayTimeout_ = null;
3386
Mike Frysingerbdb34802020-04-07 03:47:32 -04003387 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003388 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003389 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003390 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003391};
3392
rginda4bba5e12012-06-20 16:15:30 -07003393/**
3394 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003395 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003396 * Note: In Chrome, this should work unless the user has rejected the permission
3397 * request. In Firefox extension environment, you'll need the "clipboardRead"
3398 * permission. In other environments, this might always fail as the browser
3399 * frequently blocks access for security reasons.
3400 *
3401 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3402 * value is always null. Otherwise, this function uses legacy pasting and
3403 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003404 */
3405hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003406 if (!this.alwaysUseLegacyPasting &&
3407 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003408 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3409 return null;
3410 } else {
3411 // Legacy pasting.
3412 try {
3413 return this.document_.execCommand('paste');
3414 } catch (firefoxException) {
3415 // Ignore this. FF 40 and older would incorrectly throw an exception if
3416 // there was an error instead of returning false.
3417 return false;
3418 }
3419 }
rginda4bba5e12012-06-20 16:15:30 -07003420};
3421
3422/**
3423 * Copy a string to the system clipboard.
3424 *
3425 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003426 *
3427 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003428 */
3429hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003430 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003431 if (!this.clipboardNotice_) {
3432 this.clipboardNotice_ = this.document_.createElement('div');
3433 this.clipboardNotice_.style.textAlign = 'center';
3434 const copyImage = lib.resource.getData('hterm/images/copy');
3435 this.clipboardNotice_.innerHTML =
3436 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3437 }
3438 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003439 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003440
Mike Frysinger96eacae2019-01-02 18:13:56 -05003441 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003442};
3443
Evan Jones2600d4f2016-12-06 09:29:36 -05003444/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003445 * Display an image.
3446 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003447 * Either URI or buffer or blob fields must be specified.
3448 *
Joel Hockey0f933582019-08-27 18:01:51 -07003449 * @param {{
3450 * name: (string|undefined),
3451 * size: (string|number|undefined),
3452 * preserveAspectRation: (boolean|undefined),
3453 * inline: (boolean|undefined),
3454 * width: (string|number|undefined),
3455 * height: (string|number|undefined),
3456 * align: (string|undefined),
3457 * url: (string|undefined),
3458 * buffer: (!ArrayBuffer|undefined),
3459 * blob: (!Blob|undefined),
3460 * type: (string|undefined),
3461 * }} options The image to display.
3462 * name A human readable string for the image
3463 * size The size (in bytes).
3464 * preserveAspectRatio Whether to preserve aspect.
3465 * inline Whether to display the image inline.
3466 * width The width of the image.
3467 * height The height of the image.
3468 * align Direction to align the image.
3469 * uri The source URI for the image.
3470 * buffer The ArrayBuffer image data.
3471 * blob The Blob image data.
3472 * type The MIME type of the image data.
3473 * @param {function()=} onLoad Callback when loading finishes.
3474 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003475 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003476hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003477 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003478 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003479 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003480 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003481 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003482
3483 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003484 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003485 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003486 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003487
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003488 // See if the mime type is available. If not, guess from the filename.
3489 // We don't list all possible mime types because the browser can usually
3490 // guess it correctly. So list the ones that need a bit more help.
3491 if (!options.type) {
3492 const ary = options.name.split('.');
3493 const ext = ary[ary.length - 1].trim();
3494 switch (ext) {
3495 case 'svg':
3496 case 'svgz':
3497 options.type = 'image/svg+xml';
3498 break;
3499 }
3500 }
3501
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003502 // Has the user approved image display yet?
3503 if (this.allowImagesInline !== true) {
3504 this.newLine();
3505 const row = this.getRowNode(this.scrollbackRows_.length +
3506 this.getCursorRow() - 1);
3507
3508 if (this.allowImagesInline === false) {
3509 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3510 'Inline Images Disabled');
3511 return;
3512 }
3513
3514 // Show a prompt.
3515 let button;
3516 const span = this.document_.createElement('span');
3517 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3518 span.style.fontWeight = 'bold';
3519 span.style.borderWidth = '1px';
3520 span.style.borderStyle = 'dashed';
3521 button = this.document_.createElement('span');
3522 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3523 button.style.marginLeft = '1em';
3524 button.style.borderWidth = '1px';
3525 button.style.borderStyle = 'solid';
3526 button.addEventListener('click', () => {
3527 this.prefs_.set('allow-images-inline', false);
3528 });
3529 span.appendChild(button);
3530 button = this.document_.createElement('span');
3531 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3532 'allow this session');
3533 button.style.marginLeft = '1em';
3534 button.style.borderWidth = '1px';
3535 button.style.borderStyle = 'solid';
3536 button.addEventListener('click', () => {
3537 this.allowImagesInline = true;
3538 });
3539 span.appendChild(button);
3540 button = this.document_.createElement('span');
3541 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3542 button.style.marginLeft = '1em';
3543 button.style.borderWidth = '1px';
3544 button.style.borderStyle = 'solid';
3545 button.addEventListener('click', () => {
3546 this.prefs_.set('allow-images-inline', true);
3547 });
3548 span.appendChild(button);
3549
3550 row.appendChild(span);
3551 return;
3552 }
3553
3554 // See if we should show this object directly, or download it.
3555 if (options.inline) {
3556 const io = this.io.push();
3557 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003558 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003559
3560 // While we're loading the image, eat all the user's input.
3561 io.onVTKeystroke = io.sendString = () => {};
3562
3563 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003564 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003565 if (options.uri !== undefined) {
3566 img.src = options.uri;
3567 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003568 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003569 img.src = URL.createObjectURL(blob);
3570 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003571 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003572 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003573 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003574 img.title = img.alt = options.name;
3575
3576 // Attach the image to the page to let it load/render. It won't stay here.
3577 // This is needed so it's visible and the DOM can calculate the height. If
3578 // the image is hidden or not in the DOM, the height is always 0.
3579 this.document_.body.appendChild(img);
3580
3581 // Wait for the image to finish loading before we try moving it to the
3582 // right place in the terminal.
3583 img.onload = () => {
3584 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003585 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003586 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003587 img.style.maxWidth = `${screenSize.width}px`;
3588 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003589
3590 // Parse a width/height specification.
3591 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003592 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003593 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003594 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003595
3596 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3597 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003598 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003599 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003600 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003601 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003602 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003603 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003604 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003605 }
3606
3607 return '';
3608 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003609 img.style.width = parseDim(
3610 options.width, screenSize.width, '--hterm-charsize-width');
3611 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003612 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003613
3614 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003615 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003616 const padRows = Math.ceil(img.clientHeight /
3617 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003618 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003619 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003620 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003621
3622 // Update the max height in case the user shrinks the character size.
3623 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3624
3625 // Move the image to the last row. This way when we scroll up, it doesn't
3626 // disappear when the first row gets clipped. It will disappear when we
3627 // scroll down and the last row is clipped ...
3628 this.document_.body.removeChild(img);
3629 // Create a wrapper node so we can do an absolute in a relative position.
3630 // This helps with rounding errors between JS & CSS counts.
3631 const div = this.document_.createElement('div');
3632 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003633 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003634 img.style.position = 'absolute';
3635 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3636 div.appendChild(img);
3637 const row = this.getRowNode(this.scrollbackRows_.length +
3638 this.getCursorRow() - 1);
3639 row.appendChild(div);
3640
Mike Frysinger2558ed52019-01-14 01:03:41 -05003641 // Now that the image has been read, we can revoke the source.
3642 if (options.uri === undefined) {
3643 URL.revokeObjectURL(img.src);
3644 }
3645
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003646 io.hideOverlay();
3647 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003648
Mike Frysingerbdb34802020-04-07 03:47:32 -04003649 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003650 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003651 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003652 };
3653
3654 // If we got a malformed image, give up.
3655 img.onerror = (e) => {
3656 this.document_.body.removeChild(img);
3657 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003658 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003659 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003660
Mike Frysingerbdb34802020-04-07 03:47:32 -04003661 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003662 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003663 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003664 };
3665 } else {
3666 // We can't use chrome.downloads.download as that requires "downloads"
3667 // permissions, and that works only in extensions, not apps.
3668 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003669 if (options.uri !== undefined) {
3670 a.href = options.uri;
3671 } else if (options.buffer !== undefined) {
3672 const blob = new Blob([options.buffer]);
3673 a.href = URL.createObjectURL(blob);
3674 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003675 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003676 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003677 a.download = options.name;
3678 this.document_.body.appendChild(a);
3679 a.click();
3680 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003681 if (options.uri === undefined) {
3682 URL.revokeObjectURL(a.href);
3683 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003684 }
3685};
3686
3687/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003688 * Returns the selected text, or null if no text is selected.
3689 *
3690 * @return {string|null}
3691 */
rgindaa09e7332012-08-17 12:49:51 -07003692hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003693 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003694 selection.sync();
3695
Mike Frysingerbdb34802020-04-07 03:47:32 -04003696 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003697 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003698 }
rgindaa09e7332012-08-17 12:49:51 -07003699
rgindaa09e7332012-08-17 12:49:51 -07003700 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003701 let startOffset = selection.startOffset;
3702 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003703
Raymes Khoury334625a2018-06-25 10:29:40 +10003704 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003705 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003706 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003707 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003708
Robert Gindafdbb3f22012-09-06 20:23:06 -07003709 if (node.nodeName != 'X-ROW') {
3710 // If the selection doesn't start on an x-row node, then it must be
3711 // somewhere inside the x-row. Add any characters from previous siblings
3712 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003713
3714 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3715 // If node is the text node in a styled span, move up to the span node.
3716 node = node.parentNode;
3717 }
3718
Robert Gindafdbb3f22012-09-06 20:23:06 -07003719 while (node.previousSibling) {
3720 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003721 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003722 }
rgindaa09e7332012-08-17 12:49:51 -07003723 }
3724
3725 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003726 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003727 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003728 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003729
Robert Gindafdbb3f22012-09-06 20:23:06 -07003730 if (node.nodeName != 'X-ROW') {
3731 // If the selection doesn't end on an x-row node, then it must be
3732 // somewhere inside the x-row. Add any characters from following siblings
3733 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003734
3735 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3736 // If node is the text node in a styled span, move up to the span node.
3737 node = node.parentNode;
3738 }
3739
Robert Gindafdbb3f22012-09-06 20:23:06 -07003740 while (node.nextSibling) {
3741 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003742 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003743 }
rgindaa09e7332012-08-17 12:49:51 -07003744 }
3745
Mike Frysingerdc727792020-04-10 01:41:13 -04003746 const rv = this.getRowsText(selection.startRow.rowIndex,
3747 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003748 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003749};
3750
rginda4bba5e12012-06-20 16:15:30 -07003751/**
3752 * Copy the current selection to the system clipboard, then clear it after a
3753 * short delay.
3754 */
3755hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003756 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003757 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003758 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003759 }
rginda4bba5e12012-06-20 16:15:30 -07003760};
3761
Joel Hockey0f933582019-08-27 18:01:51 -07003762/**
3763 * Show overlay with current terminal size.
3764 */
rgindaf0090c92012-02-10 14:58:52 -08003765hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003766 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003767 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003768 }
rgindaf0090c92012-02-10 14:58:52 -08003769};
3770
rginda87b86462011-12-14 13:48:03 -08003771/**
3772 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3773 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003774 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003775 */
3776hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003777 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003778 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003779 }
rginda87b86462011-12-14 13:48:03 -08003780
Mike Frysinger225c99d2019-10-20 14:02:37 -06003781 this.pauseCursorBlink_();
3782
Mike Frysinger79669762018-12-30 20:51:10 -05003783 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003784};
3785
3786/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003787 * Open the selected url.
3788 */
3789hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003790 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003791
3792 // If there is no selection, try and expand wherever they clicked.
3793 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003794 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003795 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003796
3797 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003798 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003799 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003800 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003801 }
3802
3803 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003804 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003805 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003806 }
Mike Frysinger43472622017-06-26 18:11:07 -04003807
3808 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003809 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003810 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3811 // We have to whitelist a few protocols that lack authorities and thus
3812 // never use the //. Like mailto.
3813 switch (str.split(':', 1)[0]) {
3814 case 'mailto':
3815 break;
3816 default:
3817 str = 'http://' + str;
3818 break;
3819 }
3820 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003821
Mike Frysinger720fa832017-10-23 01:15:52 -04003822 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003823};
Mike Frysinger70b94692017-01-26 18:57:50 -10003824
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003825/**
3826 * Manage the automatic mouse hiding behavior while typing.
3827 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003828 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003829 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003830hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003831 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3832 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003833 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003834 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003835 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003836
3837 this.mouseHideWhileTyping_ = !!v;
3838};
3839
3840/**
3841 * Handler for monitoring user keyboard activity.
3842 *
3843 * This isn't for processing the keystrokes directly, but for updating any
3844 * state that might toggle based on the user using the keyboard at all.
3845 *
Joel Hockey0f933582019-08-27 18:01:51 -07003846 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003847 */
3848hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3849 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003850 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003851 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003852 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003853};
Mike Frysinger70b94692017-01-26 18:57:50 -10003854
3855/**
rgindad5613292012-06-19 15:40:37 -07003856 * Add the terminalRow and terminalColumn properties to mouse events and
3857 * then forward on to onMouse().
3858 *
3859 * The terminalRow and terminalColumn properties contain the (row, column)
3860 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003861 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003862 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003863 */
3864hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003865 if (e.processedByTerminalHandler_) {
3866 // We register our event handlers on the document, as well as the cursor
3867 // and the scroll blocker. Mouse events that occur on the cursor or
3868 // scroll blocker will also appear on the document, but we don't want to
3869 // process them twice.
3870 //
3871 // We can't just prevent bubbling because that has other side effects, so
3872 // we decorate the event object with this property instead.
3873 return;
3874 }
3875
Mike Frysinger468966c2018-08-28 13:48:51 -04003876 // Consume navigation events. Button 3 is usually "browser back" and
3877 // button 4 is "browser forward" which we don't want to happen.
3878 if (e.button > 2) {
3879 e.preventDefault();
3880 // We don't return so click events can be passed to the remote below.
3881 }
3882
Mike Frysingerdc727792020-04-10 01:41:13 -04003883 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003884 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3885
rgindafaa74742012-08-21 13:34:03 -07003886 e.processedByTerminalHandler_ = true;
3887
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003888 // Handle auto hiding of mouse cursor while typing.
3889 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3890 // Make sure the mouse cursor is visible.
3891 this.syncMouseStyle();
3892 // This debounce isn't perfect, but should work well enough for such a
3893 // simple implementation. If the user moved the mouse, we enabled this
3894 // debounce, and then moved the mouse just before the timeout, we wouldn't
3895 // debounce that later movement.
3896 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3897 }
3898
Robert Gindaeda48db2014-07-17 09:25:30 -07003899 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003900 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003901 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003902 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003903 this.scrollPort_.characterSize.height) + 1;
3904 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003905 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003906
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003907 // Clamp row and column.
3908 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3909 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3910
3911 // Ignore mousedown in the scrollbar area.
3912 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003913 return;
3914 }
3915
Joel Hockey3babf302020-04-22 15:00:06 -07003916 if (this.options_.cursorVisible && !reportMouseEvents &&
3917 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003918 // If the cursor is visible and we're not sending mouse events to the
3919 // host app, then we want to hide the terminal cursor when the mouse
3920 // cursor is over top. This keeps the terminal cursor from interfering
3921 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003922 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3923 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3924 this.cursorNode_.style.display = 'none';
3925 } else if (this.cursorNode_.style.display == 'none') {
3926 this.cursorNode_.style.display = '';
3927 }
3928 }
rgindad5613292012-06-19 15:40:37 -07003929
Robert Ginda928cf632014-03-05 15:07:41 -08003930 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003931 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003932
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003933 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003934 // If VT mouse reporting is disabled, or has been defeated with
3935 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003936 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003937 this.setSelectionEnabled(true);
3938 } else {
3939 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003940 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003941 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003942 this.setSelectionEnabled(false);
3943 e.preventDefault();
3944 }
3945 }
3946
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003947 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003948 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003949 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003950 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003951 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003952 }
rgindad5613292012-06-19 15:40:37 -07003953 }
3954
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003955 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003956 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003957 // Ignore links created using OSC-8 as those will open by themselves, and
3958 // the visible text is most likely not the URI they want anyways.
3959 if (e.target.className === 'uri-node') {
3960 return;
3961 }
3962
Mike Frysinger70b94692017-01-26 18:57:50 -10003963 // Debounce this event with the dblclick event. If you try to doubleclick
3964 // a URL to open it, Chrome will fire click then dblclick, but we won't
3965 // have expanded the selection text at the first click event.
3966 clearTimeout(this.timeouts_.openUrl);
3967 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3968 500);
3969 return;
3970 }
3971
Mike Frysinger847577f2017-05-23 23:25:57 -04003972 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003973 if (e.ctrlKey && e.button == 2 /* right button */) {
3974 e.preventDefault();
3975 this.contextMenu.show(e, this);
3976 } else if (e.button == this.mousePasteButton ||
3977 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003978 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003979 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003980 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003981 }
3982 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003983
Mike Frysinger2edd3612017-05-24 00:54:39 -04003984 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003985 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003986 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003987 }
3988
3989 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3990 this.scrollBlockerNode_.engaged) {
3991 // Disengage the scroll-blocker after one of these events.
3992 this.scrollBlockerNode_.engaged = false;
3993 this.scrollBlockerNode_.style.top = '-99px';
3994 }
3995
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003996 // Emulate arrow key presses via scroll wheel events.
3997 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3998 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003999 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07004000 const delta =
4001 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04004002
Mike Frysinger321063c2018-08-29 15:33:14 -04004003 // Helper to turn a wheel event delta into a series of key presses.
4004 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
4005 if (distance == 0) {
4006 return '';
4007 }
4008
4009 // Convert the scroll distance into a number of rows/cols.
4010 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4011 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4012 return data.repeat(cells);
4013 };
4014
4015 // The order between up/down and left/right doesn't really matter.
4016 this.io.sendString(
4017 // Up/down arrow keys.
4018 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4019 'A', 'B') +
4020 // Left/right arrow keys.
4021 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004022 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004023 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004024
4025 e.preventDefault();
4026 }
4027 }
Robert Ginda928cf632014-03-05 15:07:41 -08004028 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004029 if (!this.scrollBlockerNode_.engaged) {
4030 if (e.type == 'mousedown') {
4031 // Move the scroll-blocker into place if we want to keep the scrollport
4032 // from scrolling.
4033 this.scrollBlockerNode_.engaged = true;
4034 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4035 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4036 } else if (e.type == 'mousemove') {
4037 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4038 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004039 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004040 e.preventDefault();
4041 }
4042 }
Robert Ginda928cf632014-03-05 15:07:41 -08004043
4044 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004045 }
4046
Robert Ginda928cf632014-03-05 15:07:41 -08004047 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4048 // Restore this on mouseup in case it was temporarily defeated with a
4049 // alt-mousedown. Only do this when the selection is empty so that
4050 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004051 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004052 }
rgindad5613292012-06-19 15:40:37 -07004053};
4054
4055/**
4056 * Clients should override this if they care to know about mouse events.
4057 *
4058 * The event parameter will be a normal DOM mouse click event with additional
4059 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004060 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004061 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004062 */
4063hterm.Terminal.prototype.onMouse = function(e) { };
4064
4065/**
rginda8e92a692012-05-20 19:37:20 -07004066 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004067 *
4068 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004069 */
Rob Spies06533ba2014-04-24 11:20:37 -07004070hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4071 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004072 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004073
Mike Frysingerbdb34802020-04-07 03:47:32 -04004074 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004075 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004076 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004077
Mike Frysingerbdb34802020-04-07 03:47:32 -04004078 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004079 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004080 }
rginda8e92a692012-05-20 19:37:20 -07004081};
4082
4083/**
rginda8ba33642011-12-14 12:31:31 -08004084 * React when the ScrollPort is scrolled.
4085 */
4086hterm.Terminal.prototype.onScroll_ = function() {
4087 this.scheduleSyncCursorPosition_();
4088};
4089
4090/**
rginda9846e2f2012-01-27 13:53:33 -08004091 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004092 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004093 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004094 */
4095hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004096 this.onPasteData_(e.text);
4097};
4098
4099/**
4100 * Handle pasted data.
4101 *
4102 * @param {string} data The pasted data.
4103 */
4104hterm.Terminal.prototype.onPasteData_ = function(data) {
4105 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004106 if (this.options_.bracketedPaste) {
4107 // We strip out most escape sequences as they can cause issues (like
4108 // inserting an \x1b[201~ midstream). We pass through whitespace
4109 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4110 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004111 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004112 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4113 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4114 }
Robert Gindaa063b202014-07-21 11:08:25 -07004115
4116 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004117};
4118
4119/**
rgindaa09e7332012-08-17 12:49:51 -07004120 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004121 *
Joel Hockey0f933582019-08-27 18:01:51 -07004122 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004123 */
4124hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004125 if (!this.useDefaultWindowCopy) {
4126 e.preventDefault();
4127 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4128 }
rgindaa09e7332012-08-17 12:49:51 -07004129};
4130
4131/**
rginda8ba33642011-12-14 12:31:31 -08004132 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004133 *
4134 * Note: This function should not directly contain code that alters the internal
4135 * state of the terminal. That kind of code belongs in realizeWidth or
4136 * realizeHeight, so that it can be executed synchronously in the case of a
4137 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004138 */
4139hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004140 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4141 this.scrollPort_.characterSize.width) || 0;
4142 const rowCount = lib.f.smartFloorDivide(
4143 this.scrollPort_.getScreenHeight(),
4144 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004145
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004146 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004147 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004148 // gets removed from the document or during the initial load, and we can't
4149 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004150 // This can also happen if called before the scrollPort calculates the
4151 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004152 return;
4153 }
4154
Mike Frysingerdc727792020-04-10 01:41:13 -04004155 const isNewSize = (columnCount != this.screenSize.width ||
4156 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004157 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004158
4159 // We do this even if the size didn't change, just to be sure everything is
4160 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004161 this.realizeSize_(columnCount, rowCount);
Jason Lin002e1392020-07-30 14:20:24 +10004162 this.updateCssCharsize_();
rgindaa8ba17d2012-08-15 14:41:10 -07004163
Mike Frysingerbdb34802020-04-07 03:47:32 -04004164 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004165 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004166 }
rgindaa8ba17d2012-08-15 14:41:10 -07004167
Robert Gindafb1be6a2013-12-11 11:56:22 -08004168 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004169 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004170
4171 if (wasScrolledEnd) {
4172 this.scrollEnd();
4173 }
rginda8ba33642011-12-14 12:31:31 -08004174};
4175
4176/**
4177 * Service the cursor blink timeout.
4178 */
4179hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004180 if (!this.options_.cursorBlink) {
4181 delete this.timeouts_.cursorBlink;
4182 return;
4183 }
4184
Robert Ginda830583c2013-08-07 13:20:46 -07004185 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004186 this.cursorNode_.style.opacity == '0' ||
4187 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004188 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004189 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4190 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004191 } else {
rginda87b86462011-12-14 13:48:03 -08004192 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004193 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4194 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004195 }
4196};
David Reveman8f552492012-03-28 12:18:41 -04004197
4198/**
4199 * Set the scrollbar-visible mode bit.
4200 *
4201 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4202 * Otherwise it will not.
4203 *
4204 * Defaults to on.
4205 *
4206 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4207 */
4208hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4209 this.scrollPort_.setScrollbarVisible(state);
4210};
Michael Kelly485ecd12014-06-09 11:41:56 -04004211
4212/**
Rob Spies49039e52014-12-17 13:40:04 -08004213 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004214 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004215 *
4216 * Defaults to 1.
4217 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004218 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004219 */
4220hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4221 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4222};
4223
4224/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004225 * Close all web notifications created by terminal bells.
4226 */
4227hterm.Terminal.prototype.closeBellNotifications_ = function() {
4228 this.bellNotificationList_.forEach(function(n) {
4229 n.close();
4230 });
4231 this.bellNotificationList_.length = 0;
4232};
Raymes Khourye5d48982018-08-02 09:08:32 +10004233
4234/**
4235 * Syncs the cursor position when the scrollport gains focus.
4236 */
4237hterm.Terminal.prototype.onScrollportFocus_ = function() {
4238 // If the cursor is offscreen we set selection to the last row on the screen.
4239 const topRowIndex = this.scrollPort_.getTopRowIndex();
4240 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4241 const selection = this.document_.getSelection();
4242 if (!this.syncCursorPosition_() && selection) {
4243 selection.collapse(this.getRowNode(bottomRowIndex));
4244 }
4245};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004246
4247/**
4248 * Clients can override this if they want to provide an options page.
4249 */
4250hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4251
4252
4253/**
4254 * Called when user selects to open the options page.
4255 */
4256hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4257 this.onOpenOptionsPage();
4258};