blob: 72282825b164ecedcab1735dd3eea3c8169d7fca [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700109 this.screenBorderSize_ = 0;
110
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.scrollOnOutput_ = null;
112 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400113 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800114
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700115 // True if we should override mouse event reporting to allow local selection.
116 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800117
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400118 // Whether to auto hide the mouse cursor when typing.
119 this.setAutomaticMouseHiding();
120 // Timer to keep mouse visible while it's being used.
121 this.mouseHideDelay_ = null;
122
rgindaf0090c92012-02-10 14:58:52 -0800123 // Terminal bell sound.
124 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400125 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800126 this.bellAudio_.setAttribute('preload', 'auto');
127
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000128 // The AccessibilityReader object for announcing command output.
129 this.accessibilityReader_ = null;
130
Mike Frysingercc114512017-09-11 21:39:17 -0400131 // The context menu object.
132 this.contextMenu = new hterm.ContextMenu();
133
Michael Kelly485ecd12014-06-09 11:41:56 -0400134 // All terminal bell notifications that have been generated (not necessarily
135 // shown).
136 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700137 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
139 // Whether we have permission to display notifications.
140 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
rginda6d397402012-01-17 10:58:29 -0800142 // Cursor position and attributes saved with DECSC.
143 this.savedOptions_ = {};
144
rginda8ba33642011-12-14 12:31:31 -0800145 // The current mode bits for the terminal.
146 this.options_ = new hterm.Options();
147
148 // Timeouts we might need to clear.
149 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800150
151 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800152 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800153
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800154 this.saveCursorAndState(true);
155
Zhu Qunying30d40712017-03-14 16:27:00 -0700156 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800157 this.keyboard = new hterm.Keyboard(this);
158
rginda87b86462011-12-14 13:48:03 -0800159 // General IO interface that can be given to third parties without exposing
160 // the entire terminal object.
161 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800162
rgindad5613292012-06-19 15:40:37 -0700163 // True if mouse-click-drag should scroll the terminal.
164 this.enableMouseDragScroll = true;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400167 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700168 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700169
Zhu Qunying30d40712017-03-14 16:27:00 -0700170 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700171 this.useDefaultWindowCopy = false;
172
173 this.clearSelectionAfterCopy = true;
174
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400175 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800176 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700177
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400178 // Whether we allow images to be shown.
179 this.allowImagesInline = null;
180
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400181 this.reportFocus = false;
182
Jason Linf129f3c2020-03-23 11:52:08 +1100183 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
184 this.alwaysUseLegacyPasting = false;
185
Joel Hockey3a44a442019-10-14 16:22:56 -0700186 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500187 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800188};
189
190/**
Robert Ginda830583c2013-08-07 13:20:46 -0700191 * Possible cursor shapes.
192 */
193hterm.Terminal.cursorShape = {
194 BLOCK: 'BLOCK',
195 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400196 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700197};
198
199/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700200 * Clients should override this to be notified when the terminal is ready
201 * for use.
202 *
203 * The terminal initialization is asynchronous, and shouldn't be used before
204 * this method is called.
205 */
206hterm.Terminal.prototype.onTerminalReady = function() { };
207
208/**
rginda35c456b2012-02-09 17:29:05 -0800209 * Default tab with of 8 to match xterm.
210 */
211hterm.Terminal.prototype.tabWidth = 8;
212
213/**
rginda9f5222b2012-03-05 11:53:28 -0800214 * Select a preference profile.
215 *
216 * This will load the terminal preferences for the given profile name and
217 * associate subsequent preference changes with the new preference profile.
218 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500219 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800220 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400221 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700222 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800223 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400224hterm.Terminal.prototype.setProfile = function(
225 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800227
Mike Frysingerdc727792020-04-10 01:41:13 -0400228 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800229
Mike Frysingerbdb34802020-04-07 03:47:32 -0400230 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700231 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400232 }
rginda9f5222b2012-03-05 11:53:28 -0800233
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700235
236 /**
237 * Clears and reloads key bindings. Used by preferences
238 * 'keybindings' and 'keybindings-os-defaults'.
239 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400240 * @param {*?=} bindings
241 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700242 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700244 terminal.keyboard.bindings.clear();
245
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 // Default to an empty object so we still handle OS defaults.
247 if (bindings === null) {
248 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700249 }
250
251 if (!(bindings instanceof Object)) {
252 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400253 bindings = {};
254 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700255 }
256
257 try {
258 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
259 } catch (ex) {
260 console.error('Error in keybindings preference: ' + ex);
261 }
262 }
263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800265 'alt-gr-mode': function(v) {
266 if (v == null) {
267 if (navigator.language.toLowerCase() == 'en-us') {
268 v = 'none';
269 } else {
270 v = 'right-alt';
271 }
272 } else if (typeof v == 'string') {
273 v = v.toLowerCase();
274 } else {
275 v = 'none';
276 }
277
Mike Frysingerbdb34802020-04-07 03:47:32 -0400278 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800279 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400280 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800281
282 terminal.keyboard.altGrMode = v;
283 },
284
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700285 'alt-backspace-is-meta-backspace': function(v) {
286 terminal.keyboard.altBackspaceIsMetaBackspace = v;
287 },
288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 'alt-is-meta': function(v) {
290 terminal.keyboard.altIsMeta = v;
291 },
292
293 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400294 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400296 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700297
298 terminal.keyboard.altSendsWhat = v;
299 },
300
301 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400302 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800303 if (ary) {
304 terminal.bellAudio_.setAttribute('src',
305 lib.resource.getDataUrl(ary[1]));
306 } else {
307 terminal.bellAudio_.setAttribute('src', v);
308 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 },
310
Michael Kelly485ecd12014-06-09 11:41:56 -0400311 'desktop-notification-bell': function(v) {
312 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700313 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400314 Notification.permission === 'granted';
315 if (!terminal.desktopNotificationBell_) {
316 // Note: We don't call Notification.requestPermission here because
317 // Chrome requires the call be the result of a user action (such as an
318 // onclick handler), and pref listeners are run asynchronously.
319 //
320 // A way of working around this would be to display a dialog in the
321 // terminal with a "click-to-request-permission" button.
322 console.warn('desktop-notification-bell is true but we do not have ' +
323 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400324 }
325 } else {
326 terminal.desktopNotificationBell_ = false;
327 }
328 },
329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 'background-color': function(v) {
331 terminal.setBackgroundColor(v);
332 },
333
334 'background-image': function(v) {
335 terminal.scrollPort_.setBackgroundImage(v);
336 },
337
338 'background-size': function(v) {
339 terminal.scrollPort_.setBackgroundSize(v);
340 },
341
342 'background-position': function(v) {
343 terminal.scrollPort_.setBackgroundPosition(v);
344 },
345
346 'backspace-sends-backspace': function(v) {
347 terminal.keyboard.backspaceSendsBackspace = v;
348 },
349
Brad Town18654b62015-03-12 00:27:45 -0700350 'character-map-overrides': function(v) {
351 if (!(v == null || v instanceof Object)) {
352 console.warn('Preference character-map-modifications is not an ' +
353 'object: ' + v);
354 return;
355 }
356
Mike Frysinger095d4062017-06-14 00:29:48 -0700357 terminal.vt.characterMaps.reset();
358 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700359 },
360
Robert Ginda57f03b42012-09-13 11:02:48 -0700361 'cursor-blink': function(v) {
362 terminal.setCursorBlink(!!v);
363 },
364
Joel Hockey9d10ba12019-05-28 01:25:02 -0700365 'cursor-shape': function(v) {
366 terminal.setCursorShape(v);
367 },
368
Robert Gindaea2183e2014-07-17 09:51:51 -0700369 'cursor-blink-cycle': function(v) {
370 if (v instanceof Array &&
371 typeof v[0] == 'number' &&
372 typeof v[1] == 'number') {
373 terminal.cursorBlinkCycle_ = v;
374 } else if (typeof v == 'number') {
375 terminal.cursorBlinkCycle_ = [v, v];
376 } else {
377 // Fast blink indicates an error.
378 terminal.cursorBlinkCycle_ = [100, 100];
379 }
380 },
381
Robert Ginda57f03b42012-09-13 11:02:48 -0700382 'cursor-color': function(v) {
383 terminal.setCursorColor(v);
384 },
385
386 'color-palette-overrides': function(v) {
387 if (!(v == null || v instanceof Object || v instanceof Array)) {
388 console.warn('Preference color-palette-overrides is not an array or ' +
389 'object: ' + v);
390 return;
rginda9f5222b2012-03-05 11:53:28 -0800391 }
rginda9f5222b2012-03-05 11:53:28 -0800392
Joel Hockey42dba8f2020-03-26 16:21:11 -0700393 // Call terminal.setColorPalette here and below with the new default
394 // value before changing it in lib.colors.colorPalette to ensure that
395 // CSS vars are updated.
396 lib.colors.stockColorPalette.forEach(
397 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700398 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700399
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400401 for (const key in v) {
402 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 if (isNaN(i) || i < 0 || i > 255) {
404 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
405 continue;
406 }
407
408 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400409 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700410 if (rgb) {
411 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700413 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 }
415 }
rginda30f20f62012-04-05 16:36:19 -0700416 }
rginda30f20f62012-04-05 16:36:19 -0700417
Joel Hockey42dba8f2020-03-26 16:21:11 -0700418 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
419 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 },
rginda30f20f62012-04-05 16:36:19 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'copy-on-select': function(v) {
423 terminal.copyOnSelect = !!v;
424 },
rginda9f5222b2012-03-05 11:53:28 -0800425
Rob Spies0bec09b2014-06-06 15:58:09 -0700426 'use-default-window-copy': function(v) {
427 terminal.useDefaultWindowCopy = !!v;
428 },
429
430 'clear-selection-after-copy': function(v) {
431 terminal.clearSelectionAfterCopy = !!v;
432 },
433
Robert Ginda7e5e9522014-03-14 12:23:58 -0700434 'ctrl-plus-minus-zero-zoom': function(v) {
435 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
436 },
437
Robert Gindafb5a3f92014-05-13 14:12:00 -0700438 'ctrl-c-copy': function(v) {
439 terminal.keyboard.ctrlCCopy = v;
440 },
441
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100442 'ctrl-v-paste': function(v) {
443 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700444 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100445 },
446
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700447 'paste-on-drop': function(v) {
448 terminal.scrollPort_.setPasteOnDrop(v);
449 },
450
Masaya Suzuki273aa982014-05-31 07:25:55 +0900451 'east-asian-ambiguous-as-two-column': function(v) {
452 lib.wc.regardCjkAmbiguous = v;
453 },
454
Robert Ginda57f03b42012-09-13 11:02:48 -0700455 'enable-8-bit-control': function(v) {
456 terminal.vt.enable8BitControl = !!v;
457 },
rginda30f20f62012-04-05 16:36:19 -0700458
Robert Ginda57f03b42012-09-13 11:02:48 -0700459 'enable-bold': function(v) {
460 terminal.syncBoldSafeState();
461 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400462
Robert Ginda3e278d72014-03-25 13:18:51 -0700463 'enable-bold-as-bright': function(v) {
464 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
465 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
466 },
467
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400468 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500469 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400470 },
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 'enable-clipboard-write': function(v) {
473 terminal.vt.enableClipboardWrite = !!v;
474 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400475
Robert Ginda3755e752013-05-31 13:34:09 -0700476 'enable-dec12': function(v) {
477 terminal.vt.enableDec12 = !!v;
478 },
479
Mike Frysinger38f267d2018-09-07 02:50:59 -0400480 'enable-csi-j-3': function(v) {
481 terminal.vt.enableCsiJ3 = !!v;
482 },
483
Robert Ginda57f03b42012-09-13 11:02:48 -0700484 'font-family': function(v) {
485 terminal.syncFontFamily();
486 },
rginda30f20f62012-04-05 16:36:19 -0700487
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700489 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700490 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500491 console.error(`Invalid font size: ${v}`);
492 return;
493 }
494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 terminal.setFontSize(v);
496 },
rginda9875d902012-08-20 16:21:57 -0700497
Robert Ginda57f03b42012-09-13 11:02:48 -0700498 'font-smoothing': function(v) {
499 terminal.syncFontFamily();
500 },
rgindade84e382012-04-20 15:39:31 -0700501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 'foreground-color': function(v) {
503 terminal.setForegroundColor(v);
504 },
rginda30f20f62012-04-05 16:36:19 -0700505
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400506 'hide-mouse-while-typing': function(v) {
507 terminal.setAutomaticMouseHiding(v);
508 },
509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'home-keys-scroll': function(v) {
511 terminal.keyboard.homeKeysScroll = v;
512 },
rginda4bba5e12012-06-20 16:15:30 -0700513
Robert Gindaa8165692015-06-15 14:46:31 -0700514 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700515 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
516 },
Robert Gindaa8165692015-06-15 14:46:31 -0700517
Joel Hockey95a9e272020-03-16 21:19:53 -0700518 'keybindings-os-defaults': function(v) {
519 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700520 },
521
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700522 'media-keys-are-fkeys': function(v) {
523 terminal.keyboard.mediaKeysAreFKeys = v;
524 },
525
Robert Ginda57f03b42012-09-13 11:02:48 -0700526 'meta-sends-escape': function(v) {
527 terminal.keyboard.metaSendsEscape = v;
528 },
rginda30f20f62012-04-05 16:36:19 -0700529
Mike Frysinger847577f2017-05-23 23:25:57 -0400530 'mouse-right-click-paste': function(v) {
531 terminal.mouseRightClickPaste = v;
532 },
533
Robert Ginda57f03b42012-09-13 11:02:48 -0700534 'mouse-paste-button': function(v) {
535 terminal.syncMousePasteButton();
536 },
rgindaa8ba17d2012-08-15 14:41:10 -0700537
Robert Gindae76aa9f2014-03-14 12:29:12 -0700538 'page-keys-scroll': function(v) {
539 terminal.keyboard.pageKeysScroll = v;
540 },
541
Robert Ginda40932892012-12-10 17:26:40 -0800542 'pass-alt-number': function(v) {
543 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700544 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800545 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500546 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800547 }
548
549 terminal.passAltNumber = v;
550 },
551
552 'pass-ctrl-number': function(v) {
553 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700554 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800555 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500556 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800557 }
558
559 terminal.passCtrlNumber = v;
560 },
561
Joel Hockey0e052042020-02-19 05:37:19 -0800562 'pass-ctrl-n': function(v) {
563 terminal.passCtrlN = v;
564 },
565
566 'pass-ctrl-t': function(v) {
567 terminal.passCtrlT = v;
568 },
569
570 'pass-ctrl-tab': function(v) {
571 terminal.passCtrlTab = v;
572 },
573
574 'pass-ctrl-w': function(v) {
575 terminal.passCtrlW = v;
576 },
577
Robert Ginda40932892012-12-10 17:26:40 -0800578 'pass-meta-number': function(v) {
579 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700580 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800581 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500582 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800583 }
584
585 terminal.passMetaNumber = v;
586 },
587
Marius Schilder77857b32014-05-14 16:21:26 -0700588 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700589 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700590 },
591
Robert Ginda8cb7d902013-06-20 14:37:18 -0700592 'receive-encoding': function(v) {
593 if (!(/^(utf-8|raw)$/).test(v)) {
594 console.warn('Invalid value for "receive-encoding": ' + v);
595 v = 'utf-8';
596 }
597
598 terminal.vt.characterEncoding = v;
599 },
600
Joel Hockey139d82d2020-04-07 23:04:29 -0700601 'screen-padding-size': function(v) {
602 v = parseInt(v, 10);
603 if (isNaN(v) || v < 0) {
604 console.error(`Invalid screen padding size: ${v}`);
605 return;
606 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700607 terminal.setScreenPaddingSize(v);
608 },
609
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700610 'screen-border-size': function(v) {
611 v = parseInt(v, 10);
612 if (isNaN(v) || v < 0) {
613 console.error(`Invalid screen border size: ${v}`);
614 return;
615 }
616 terminal.setScreenBorderSize(v);
617 },
618
619 'screen-border-color': function(v) {
620 terminal.div_.style.borderColor = v;
621 },
622
Robert Ginda57f03b42012-09-13 11:02:48 -0700623 'scroll-on-keystroke': function(v) {
624 terminal.scrollOnKeystroke_ = v;
625 },
rginda9f5222b2012-03-05 11:53:28 -0800626
Robert Ginda57f03b42012-09-13 11:02:48 -0700627 'scroll-on-output': function(v) {
628 terminal.scrollOnOutput_ = v;
629 },
rginda30f20f62012-04-05 16:36:19 -0700630
Robert Ginda57f03b42012-09-13 11:02:48 -0700631 'scrollbar-visible': function(v) {
632 terminal.setScrollbarVisible(v);
633 },
rginda9f5222b2012-03-05 11:53:28 -0800634
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400635 'scroll-wheel-may-send-arrow-keys': function(v) {
636 terminal.scrollWheelArrowKeys_ = v;
637 },
638
Rob Spies49039e52014-12-17 13:40:04 -0800639 'scroll-wheel-move-multiplier': function(v) {
640 terminal.setScrollWheelMoveMultipler(v);
641 },
642
Robert Ginda57f03b42012-09-13 11:02:48 -0700643 'shift-insert-paste': function(v) {
644 terminal.keyboard.shiftInsertPaste = v;
645 },
rginda9f5222b2012-03-05 11:53:28 -0800646
Mike Frysingera7768922017-07-28 15:00:12 -0400647 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400648 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400649 },
650
Robert Gindae76aa9f2014-03-14 12:29:12 -0700651 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400652 terminal.scrollPort_.setUserCssUrl(v);
653 },
654
655 'user-css-text': function(v) {
656 terminal.scrollPort_.setUserCssText(v);
657 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400658
659 'word-break-match-left': function(v) {
660 terminal.primaryScreen_.wordBreakMatchLeft = v;
661 terminal.alternateScreen_.wordBreakMatchLeft = v;
662 },
663
664 'word-break-match-right': function(v) {
665 terminal.primaryScreen_.wordBreakMatchRight = v;
666 terminal.alternateScreen_.wordBreakMatchRight = v;
667 },
668
669 'word-break-match-middle': function(v) {
670 terminal.primaryScreen_.wordBreakMatchMiddle = v;
671 terminal.alternateScreen_.wordBreakMatchMiddle = v;
672 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400673
674 'allow-images-inline': function(v) {
675 terminal.allowImagesInline = v;
676 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700677 });
rginda30f20f62012-04-05 16:36:19 -0700678
Robert Ginda57f03b42012-09-13 11:02:48 -0700679 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800680 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700681
Mike Frysingerec4225d2020-04-07 05:00:01 -0400682 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700683 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400684 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400685 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700686 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800687};
688
Rob Spies56953412014-04-28 14:09:47 -0700689/**
690 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500691 *
Joel Hockey0f933582019-08-27 18:01:51 -0700692 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700693 */
694hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700695 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700696};
697
Robert Gindaa063b202014-07-21 11:08:25 -0700698/**
699 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500700 *
701 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700702 */
703hterm.Terminal.prototype.setBracketedPaste = function(state) {
704 this.options_.bracketedPaste = state;
705};
Rob Spies56953412014-04-28 14:09:47 -0700706
rginda8e92a692012-05-20 19:37:20 -0700707/**
708 * Set the color for the cursor.
709 *
710 * If you want this setting to persist, set it through prefs_, rather than
711 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500712 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500713 * @param {string=} color The color to set. If not defined, we reset to the
714 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700715 */
716hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400717 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700718 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400719 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500720
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400721 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700722};
723
724/**
725 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400726 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500727 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700728 */
729hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400730 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700731};
732
733/**
rgindad5613292012-06-19 15:40:37 -0700734 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700737 */
738hterm.Terminal.prototype.setSelectionEnabled = function(state) {
739 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700740};
741
742/**
rginda8e92a692012-05-20 19:37:20 -0700743 * Set the background color.
744 *
745 * If you want this setting to persist, set it through prefs_, rather than
746 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500747 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500748 * @param {string=} color The color to set. If not defined, we reset to the
749 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700750 */
751hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400752 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700753 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400754 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500755
Joel Hockey42dba8f2020-03-26 16:21:11 -0700756 this.backgroundColor_ = lib.colors.normalizeCSS(color);
757 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700758};
759
rginda9f5222b2012-03-05 11:53:28 -0800760/**
761 * Return the current terminal background color.
762 *
763 * Intended for use by other classes, so we don't have to expose the entire
764 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500765 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700766 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800767 */
768hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700769 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700770};
771
772/**
773 * Set the foreground color.
774 *
775 * If you want this setting to persist, set it through prefs_, rather than
776 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500777 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500778 * @param {string=} color The color to set. If not defined, we reset to the
779 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700780 */
781hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400782 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700783 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400784 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500785
Joel Hockey42dba8f2020-03-26 16:21:11 -0700786 this.foregroundColor_ = lib.colors.normalizeCSS(color);
787 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800788};
789
790/**
791 * Return the current terminal foreground color.
792 *
793 * Intended for use by other classes, so we don't have to expose the entire
794 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500795 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700796 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800797 */
798hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700799 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800800};
801
802/**
rginda87b86462011-12-14 13:48:03 -0800803 * Create a new instance of a terminal command and run it with a given
804 * argument string.
805 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700806 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700807 * @param {string} commandName The command to run for this terminal.
808 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800809 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700810hterm.Terminal.prototype.runCommandClass = function(
811 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400812 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400813 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700814 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400815 }
rgindaf522ce02012-04-17 17:49:17 -0700816
rginda87b86462011-12-14 13:48:03 -0800817 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700818 {
819 commandName: commandName,
820 args: args,
rginda87b86462011-12-14 13:48:03 -0800821 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700822 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400823 onExit: (code) => {
824 this.io.pop();
825 this.uninstallKeyboard();
826 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
827 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400828 window.close();
829 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400830 },
rginda87b86462011-12-14 13:48:03 -0800831 });
832
rgindafeaf3142012-01-31 15:14:20 -0800833 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800834 this.command.run();
835};
836
837/**
rgindafeaf3142012-01-31 15:14:20 -0800838 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500839 *
840 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800841 */
842hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700843 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800844};
845
846/**
847 * Install the keyboard handler for this terminal.
848 *
849 * This will prevent the browser from seeing any keystrokes sent to the
850 * terminal.
851 */
852hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700853 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400854};
rgindafeaf3142012-01-31 15:14:20 -0800855
856/**
857 * Uninstall the keyboard handler for this terminal.
858 */
859hterm.Terminal.prototype.uninstallKeyboard = function() {
860 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400861};
rgindafeaf3142012-01-31 15:14:20 -0800862
863/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400864 * Set a CSS variable.
865 *
866 * Normally this is used to set variables in the hterm namespace.
867 *
868 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700869 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400870 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400871 */
872hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400873 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400874 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400875 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400876};
877
878/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700879 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
880 * input is valid.
881 *
882 * @param {string} name The variable to set.
883 * @param {?string} rgb The rgb value to assign to the variable.
884 */
885hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
886 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
887 if (ary) {
888 this.setCssVar(name, ary.slice(0, 3).join(','));
889 }
890};
891
892/**
893 * Sets the specified color for the active screen.
894 *
895 * @param {number} i The index into the 256 color palette to set.
896 * @param {?string} rgb The rgb value to assign to the variable.
897 */
898hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
899 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
900 this.setRgbColorCssVar(`color-${i}`, rgb);
901 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
902 }
903};
904
905/**
906 * Returns the current value in the active screen of the specified color.
907 *
908 * @param {number} i Color palette index.
909 * @return {string} rgb color.
910 */
911hterm.Terminal.prototype.getColorPalette = function(i) {
912 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
913 lib.colors.colorPalette[i];
914};
915
916/**
917 * Reset the specified color in the active screen to its default value.
918 *
919 * @param {number} i Color to reset
920 */
921hterm.Terminal.prototype.resetColor = function(i) {
922 this.setColorPalette(i, lib.colors.colorPalette[i]);
923 delete this.screen_.textAttributes.colorPaletteOverrides[i];
924};
925
926/**
927 * Reset the current screen color palette to the default state.
928 */
929hterm.Terminal.prototype.resetColorPalette = function() {
930 this.screen_.textAttributes.colorPaletteOverrides.forEach(
931 (c, i) => this.resetColor(i));
932};
933
934/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500935 * Get a CSS variable.
936 *
937 * Normally this is used to get variables in the hterm namespace.
938 *
939 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400940 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500941 * @return {string} The current setting for this variable.
942 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400943hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500944 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400945 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500946};
947
948/**
Jason Linbbbdb752020-03-06 16:26:59 +1100949 * Update CSS character size variables to match the scrollport.
950 */
951hterm.Terminal.prototype.updateCssCharsize_ = function() {
952 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
953 this.setCssVar('charsize-height',
954 this.scrollPort_.characterSize.height + 'px');
955};
956
957/**
rginda35c456b2012-02-09 17:29:05 -0800958 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800959 *
960 * Call setFontSize(0) to reset to the default font size.
961 *
962 * This function does not modify the font-size preference.
963 *
964 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800965 */
966hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400967 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700968 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400969 }
rginda9f5222b2012-03-05 11:53:28 -0800970
rginda35c456b2012-02-09 17:29:05 -0800971 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -0700972 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +1100973 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800974};
975
976/**
977 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500978 *
979 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800980 */
981hterm.Terminal.prototype.getFontSize = function() {
982 return this.scrollPort_.getFontSize();
983};
984
985/**
rginda8e92a692012-05-20 19:37:20 -0700986 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500987 *
988 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700989 */
990hterm.Terminal.prototype.getFontFamily = function() {
991 return this.scrollPort_.getFontFamily();
992};
993
994/**
rginda35c456b2012-02-09 17:29:05 -0800995 * Set the CSS "font-family" for this terminal.
996 */
rginda9f5222b2012-03-05 11:53:28 -0800997hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700998 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
999 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001000 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001001 this.syncBoldSafeState();
1002};
1003
rginda4bba5e12012-06-20 16:15:30 -07001004/**
1005 * Set this.mousePasteButton based on the mouse-paste-button pref,
1006 * autodetecting if necessary.
1007 */
1008hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001009 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001010 if (typeof button == 'number') {
1011 this.mousePasteButton = button;
1012 return;
1013 }
1014
Mike Frysingeree81a002017-12-12 16:14:53 -05001015 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001016 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001017 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001018 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001019 }
1020};
1021
1022/**
1023 * Enable or disable bold based on the enable-bold pref, autodetecting if
1024 * necessary.
1025 */
rginda9f5222b2012-03-05 11:53:28 -08001026hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001027 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001028 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001029 this.primaryScreen_.textAttributes.enableBold = enableBold;
1030 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001031 return;
1032 }
1033
Mike Frysingerdc727792020-04-10 01:41:13 -04001034 const normalSize = this.scrollPort_.measureCharacterSize();
1035 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001036
Mike Frysingerdc727792020-04-10 01:41:13 -04001037 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001038 if (!isBoldSafe) {
1039 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001040 'from normal. Font family is: ' +
1041 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001042 }
rginda9f5222b2012-03-05 11:53:28 -08001043
Robert Gindaed016262012-10-26 16:27:09 -07001044 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1045 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001046};
1047
1048/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001049 * Control text blinking behavior.
1050 *
1051 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001052 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001053hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001054 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001055 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001056 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001057 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001058};
1059
1060/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001061 * Set the mouse cursor style based on the current terminal mode.
1062 */
1063hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001064 this.setCssVar('mouse-cursor-style',
1065 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1066 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001067 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001068};
1069
1070/**
rginda87b86462011-12-14 13:48:03 -08001071 * Return a copy of the current cursor position.
1072 *
Joel Hockey0f933582019-08-27 18:01:51 -07001073 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001074 */
1075hterm.Terminal.prototype.saveCursor = function() {
1076 return this.screen_.cursorPosition.clone();
1077};
1078
Evan Jones2600d4f2016-12-06 09:29:36 -05001079/**
1080 * Return the current text attributes.
1081 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001082 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001083 */
rgindaa19afe22012-01-25 15:40:22 -08001084hterm.Terminal.prototype.getTextAttributes = function() {
1085 return this.screen_.textAttributes;
1086};
1087
Evan Jones2600d4f2016-12-06 09:29:36 -05001088/**
1089 * Set the text attributes.
1090 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001091 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001092 */
rginda1a09aa02012-06-18 21:11:25 -07001093hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1094 this.screen_.textAttributes = textAttributes;
1095};
1096
rginda87b86462011-12-14 13:48:03 -08001097/**
rgindaf522ce02012-04-17 17:49:17 -07001098 * Return the current browser zoom factor applied to the terminal.
1099 *
1100 * @return {number} The current browser zoom factor.
1101 */
1102hterm.Terminal.prototype.getZoomFactor = function() {
1103 return this.scrollPort_.characterSize.zoomFactor;
1104};
1105
1106/**
rginda9846e2f2012-01-27 13:53:33 -08001107 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001108 *
1109 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001110 */
1111hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001112 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001113};
1114
1115/**
rginda87b86462011-12-14 13:48:03 -08001116 * Restore a previously saved cursor position.
1117 *
Joel Hockey0f933582019-08-27 18:01:51 -07001118 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001119 */
1120hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001121 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1122 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001123 this.screen_.setCursorPosition(row, column);
1124 if (cursor.column > column ||
1125 cursor.column == column && cursor.overflow) {
1126 this.screen_.cursorPosition.overflow = true;
1127 }
rginda87b86462011-12-14 13:48:03 -08001128};
1129
1130/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001131 * Clear the cursor's overflow flag.
1132 */
1133hterm.Terminal.prototype.clearCursorOverflow = function() {
1134 this.screen_.cursorPosition.overflow = false;
1135};
1136
1137/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001138 * Save the current cursor state to the corresponding screens.
1139 *
1140 * See the hterm.Screen.CursorState class for more details.
1141 *
1142 * @param {boolean=} both If true, update both screens, else only update the
1143 * current screen.
1144 */
1145hterm.Terminal.prototype.saveCursorAndState = function(both) {
1146 if (both) {
1147 this.primaryScreen_.saveCursorAndState(this.vt);
1148 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001149 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001150 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001151 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001152};
1153
1154/**
1155 * Restore the saved cursor state in the corresponding screens.
1156 *
1157 * See the hterm.Screen.CursorState class for more details.
1158 *
1159 * @param {boolean=} both If true, update both screens, else only update the
1160 * current screen.
1161 */
1162hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1163 if (both) {
1164 this.primaryScreen_.restoreCursorAndState(this.vt);
1165 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001166 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001167 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001168 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001169};
1170
1171/**
Robert Ginda830583c2013-08-07 13:20:46 -07001172 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001173 *
1174 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001175 */
1176hterm.Terminal.prototype.setCursorShape = function(shape) {
1177 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001178 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001179};
Robert Ginda830583c2013-08-07 13:20:46 -07001180
1181/**
1182 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001183 *
1184 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001185 */
1186hterm.Terminal.prototype.getCursorShape = function() {
1187 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001188};
Robert Ginda830583c2013-08-07 13:20:46 -07001189
1190/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001191 * Set the screen padding size in pixels.
1192 *
1193 * @param {number} size
1194 */
1195hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001196 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001197 this.scrollPort_.setScreenPaddingSize(size);
1198};
1199
1200/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001201 * Set the screen border size in pixels.
1202 *
1203 * @param {number} size
1204 */
1205hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1206 this.div_.style.borderWidth = `${size}px`;
1207 this.screenBorderSize_ = size;
1208 this.scrollPort_.resize();
1209};
1210
1211/**
rginda87b86462011-12-14 13:48:03 -08001212 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001213 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001214 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001215 */
1216hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001217 if (columnCount == null) {
1218 this.div_.style.width = '100%';
1219 return;
1220 }
1221
Joel Hockey139d82d2020-04-07 23:04:29 -07001222 const rightPadding = Math.max(
1223 this.scrollPort_.screenPaddingSize,
1224 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001225 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001226 (this.scrollPort_.characterSize.width * columnCount) +
1227 this.scrollPort_.screenPaddingSize + rightPadding +
1228 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001229 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001230 this.scheduleSyncCursorPosition_();
1231};
rginda87b86462011-12-14 13:48:03 -08001232
rgindac9bc5502012-01-18 11:48:44 -08001233/**
rginda35c456b2012-02-09 17:29:05 -08001234 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001235 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001236 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001237 */
1238hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001239 if (rowCount == null) {
1240 this.div_.style.height = '100%';
1241 return;
1242 }
1243
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001244 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1245 (2 * this.scrollPort_.screenPaddingSize) +
1246 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001247 this.realizeSize_(this.screenSize.width, rowCount);
1248 this.scheduleSyncCursorPosition_();
1249};
1250
1251/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001252 * Deal with terminal size changes.
1253 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001254 * @param {number} columnCount The number of columns.
1255 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001256 */
1257hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001258 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001259
Mike Frysinger0206e262019-06-13 10:18:19 -04001260 if (columnCount != this.screenSize.width) {
1261 notify = true;
1262 this.realizeWidth_(columnCount);
1263 }
1264
1265 if (rowCount != this.screenSize.height) {
1266 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001267 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001268 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001269
1270 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001271 if (notify) {
1272 this.io.onTerminalResize_(columnCount, rowCount);
1273 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001274};
1275
1276/**
rgindac9bc5502012-01-18 11:48:44 -08001277 * Deal with terminal width changes.
1278 *
1279 * This function does what needs to be done when the terminal width changes
1280 * out from under us. It happens here rather than in onResize_() because this
1281 * code may need to run synchronously to handle programmatic changes of
1282 * terminal width.
1283 *
1284 * Relying on the browser to send us an async resize event means we may not be
1285 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001286 *
1287 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001288 */
1289hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001290 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001291 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001292 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001293
Mike Frysingerdc727792020-04-10 01:41:13 -04001294 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001295 if (deltaColumns == 0) {
1296 // No change, so don't bother recalculating things.
1297 return;
1298 }
rgindac9bc5502012-01-18 11:48:44 -08001299
rginda87b86462011-12-14 13:48:03 -08001300 this.screenSize.width = columnCount;
1301 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001302
1303 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001304 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001305 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001306 }
rgindac9bc5502012-01-18 11:48:44 -08001307 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001308 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001309 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001310 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001311 }
rgindac9bc5502012-01-18 11:48:44 -08001312
1313 this.tabStops_.pop();
1314 }
1315 }
1316
1317 this.screen_.setColumnCount(this.screenSize.width);
1318};
1319
1320/**
1321 * Deal with terminal height changes.
1322 *
1323 * This function does what needs to be done when the terminal height changes
1324 * out from under us. It happens here rather than in onResize_() because this
1325 * code may need to run synchronously to handle programmatic changes of
1326 * terminal height.
1327 *
1328 * Relying on the browser to send us an async resize event means we may not be
1329 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001330 *
1331 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001332 */
1333hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001334 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001335 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001336 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001337
Mike Frysingerdc727792020-04-10 01:41:13 -04001338 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001339 if (deltaRows == 0) {
1340 // No change, so don't bother recalculating things.
1341 return;
1342 }
rgindac9bc5502012-01-18 11:48:44 -08001343
1344 this.screenSize.height = rowCount;
1345
Mike Frysingerdc727792020-04-10 01:41:13 -04001346 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001347
1348 if (deltaRows < 0) {
1349 // Screen got smaller.
1350 deltaRows *= -1;
1351 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001352 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001353 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001354 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001355 }
rgindac9bc5502012-01-18 11:48:44 -08001356
Mike Frysingerbdb34802020-04-07 03:47:32 -04001357 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001358 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001359 }
rgindac9bc5502012-01-18 11:48:44 -08001360
1361 this.screen_.popRow();
1362 deltaRows--;
1363 }
1364
Mike Frysingerdc727792020-04-10 01:41:13 -04001365 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001366 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1367
1368 // We just removed rows from the top of the screen, we need to update
1369 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001370 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001371 } else if (deltaRows > 0) {
1372 // Screen got larger.
1373
1374 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001375 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1376 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001377 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1378 this.screen_.unshiftRows(rows);
1379 deltaRows -= scrollbackCount;
1380 cursor.row += scrollbackCount;
1381 }
1382
Mike Frysingerbdb34802020-04-07 03:47:32 -04001383 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001384 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001385 }
rgindac9bc5502012-01-18 11:48:44 -08001386 }
1387
rginda35c456b2012-02-09 17:29:05 -08001388 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001389 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001390};
1391
1392/**
1393 * Scroll the terminal to the top of the scrollback buffer.
1394 */
1395hterm.Terminal.prototype.scrollHome = function() {
1396 this.scrollPort_.scrollRowToTop(0);
1397};
1398
1399/**
1400 * Scroll the terminal to the end.
1401 */
1402hterm.Terminal.prototype.scrollEnd = function() {
1403 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1404};
1405
1406/**
1407 * Scroll the terminal one page up (minus one line) relative to the current
1408 * position.
1409 */
1410hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001411 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001412};
1413
1414/**
1415 * Scroll the terminal one page down (minus one line) relative to the current
1416 * position.
1417 */
1418hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001419 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001420};
1421
rgindac9bc5502012-01-18 11:48:44 -08001422/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001423 * Scroll the terminal one line up relative to the current position.
1424 */
1425hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001426 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001427 this.scrollPort_.scrollRowToTop(i - 1);
1428};
1429
1430/**
1431 * Scroll the terminal one line down relative to the current position.
1432 */
1433hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001434 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001435 this.scrollPort_.scrollRowToTop(i + 1);
1436};
1437
1438/**
Robert Ginda40932892012-12-10 17:26:40 -08001439 * Clear primary screen, secondary screen, and the scrollback buffer.
1440 */
1441hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001442 this.clearHome(this.primaryScreen_);
1443 this.clearHome(this.alternateScreen_);
1444
1445 this.clearScrollback();
1446};
1447
1448/**
1449 * Clear scrollback buffer.
1450 */
1451hterm.Terminal.prototype.clearScrollback = function() {
1452 // Move to the end of the buffer in case the screen was scrolled back.
1453 // We're going to throw it away which would leave the display invalid.
1454 this.scrollEnd();
1455
Robert Ginda40932892012-12-10 17:26:40 -08001456 this.scrollbackRows_.length = 0;
1457 this.scrollPort_.resetCache();
1458
Mike Frysinger9c482b82018-09-07 02:49:36 -04001459 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1460 const bottom = screen.getHeight();
1461 this.renumberRows_(0, bottom, screen);
1462 });
Robert Ginda40932892012-12-10 17:26:40 -08001463
1464 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001465 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001466};
1467
1468/**
rgindac9bc5502012-01-18 11:48:44 -08001469 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001470 *
1471 * Perform a full reset to the default values listed in
1472 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001473 */
rginda87b86462011-12-14 13:48:03 -08001474hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001475 this.vt.reset();
1476
rgindac9bc5502012-01-18 11:48:44 -08001477 this.clearAllTabStops();
1478 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001479
Joel Hockey42dba8f2020-03-26 16:21:11 -07001480 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001481 const resetScreen = (screen) => {
1482 // We want to make sure to reset the attributes before we clear the screen.
1483 // The attributes might be used to initialize default/empty rows.
1484 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001485 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001486 this.clearHome(screen);
1487 screen.saveCursorAndState(this.vt);
1488 };
1489 resetScreen(this.primaryScreen_);
1490 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001491
Mike Frysinger84301d02017-11-29 13:28:46 -08001492 // Reset terminal options to their default values.
1493 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001494 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1495
Mike Frysinger84301d02017-11-29 13:28:46 -08001496 this.setVTScrollRegion(null, null);
1497
1498 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001499};
1500
rgindac9bc5502012-01-18 11:48:44 -08001501/**
1502 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001503 *
1504 * Perform a soft reset to the default values listed in
1505 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001506 */
rginda0f5c0292012-01-13 11:00:13 -08001507hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001508 this.vt.reset();
1509
rgindab8bc8932012-04-27 12:45:03 -07001510 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001511 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001512
Brad Townb62dfdc2015-03-16 19:07:15 -07001513 // We show the cursor on soft reset but do not alter the blink state.
1514 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1515
Joel Hockey42dba8f2020-03-26 16:21:11 -07001516 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001517 const resetScreen = (screen) => {
1518 // Xterm also resets the color palette on soft reset, even though it doesn't
1519 // seem to be documented anywhere.
1520 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001521 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001522 screen.saveCursorAndState(this.vt);
1523 };
1524 resetScreen(this.primaryScreen_);
1525 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001526
rgindab8bc8932012-04-27 12:45:03 -07001527 // The xterm man page explicitly says this will happen on soft reset.
1528 this.setVTScrollRegion(null, null);
1529
1530 // Xterm also shows the cursor on soft reset, but does not alter the blink
1531 // state.
rgindaa19afe22012-01-25 15:40:22 -08001532 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001533};
1534
rgindac9bc5502012-01-18 11:48:44 -08001535/**
1536 * Move the cursor forward to the next tab stop, or to the last column
1537 * if no more tab stops are set.
1538 */
1539hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001540 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001541
Mike Frysingerdc727792020-04-10 01:41:13 -04001542 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001543 if (this.tabStops_[i] > column) {
1544 this.setCursorColumn(this.tabStops_[i]);
1545 return;
1546 }
1547 }
1548
David Benjamin66e954d2012-05-05 21:08:12 -04001549 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001550 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001551 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001552 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001553};
1554
rgindac9bc5502012-01-18 11:48:44 -08001555/**
1556 * Move the cursor backward to the previous tab stop, or to the first column
1557 * if no previous tab stops are set.
1558 */
1559hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001560 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001561
Mike Frysingerdc727792020-04-10 01:41:13 -04001562 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001563 if (this.tabStops_[i] < column) {
1564 this.setCursorColumn(this.tabStops_[i]);
1565 return;
1566 }
1567 }
1568
1569 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001570};
1571
rgindac9bc5502012-01-18 11:48:44 -08001572/**
1573 * Set a tab stop at the given column.
1574 *
Joel Hockey0f933582019-08-27 18:01:51 -07001575 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001576 */
1577hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001578 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001579 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001580 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001581 }
rgindac9bc5502012-01-18 11:48:44 -08001582
1583 if (this.tabStops_[i] < column) {
1584 this.tabStops_.splice(i + 1, 0, column);
1585 return;
1586 }
1587 }
1588
1589 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001590};
1591
rgindac9bc5502012-01-18 11:48:44 -08001592/**
1593 * Clear the tab stop at the current cursor position.
1594 *
1595 * No effect if there is no tab stop at the current cursor position.
1596 */
1597hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001598 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001599
Mike Frysingerdc727792020-04-10 01:41:13 -04001600 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001601 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001602 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001603 }
rgindac9bc5502012-01-18 11:48:44 -08001604
1605 this.tabStops_.splice(i, 1);
1606};
1607
1608/**
1609 * Clear all tab stops.
1610 */
1611hterm.Terminal.prototype.clearAllTabStops = function() {
1612 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001613 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001614};
1615
1616/**
1617 * Set up the default tab stops, starting from a given column.
1618 *
1619 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001620 * from the specified column, or 0 if no column is provided. It also flags
1621 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001622 *
1623 * This does not clear the existing tab stops first, use clearAllTabStops
1624 * for that.
1625 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001626 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001627 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001628 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001629hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001630 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001631 // Round start up to a default tab stop.
1632 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001633 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001634 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001635 }
David Benjamin66e954d2012-05-05 21:08:12 -04001636
1637 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001638};
1639
rginda6d397402012-01-17 10:58:29 -08001640/**
rginda8ba33642011-12-14 12:31:31 -08001641 * Interpret a sequence of characters.
1642 *
1643 * Incomplete escape sequences are buffered until the next call.
1644 *
1645 * @param {string} str Sequence of characters to interpret or pass through.
1646 */
1647hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001648 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001649 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001650};
1651
1652/**
1653 * Take over the given DIV for use as the terminal display.
1654 *
Joel Hockey0f933582019-08-27 18:01:51 -07001655 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001656 */
1657hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001658 const charset = div.ownerDocument.characterSet.toLowerCase();
1659 if (charset != 'utf-8') {
1660 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1661 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1662 }
1663
rginda87b86462011-12-14 13:48:03 -08001664 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001665 this.div_.style.borderStyle = 'solid';
1666 this.div_.style.borderWidth = 0;
1667 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001668
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001669 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1670
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001671 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1672};
1673
1674/**
1675 * Initialisation of ScrollPort properties which need to be set after its DOM
1676 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001677 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001678 * @private
1679 */
1680hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001681 this.scrollPort_.setBackgroundImage(
1682 this.prefs_.getString('background-image'));
1683 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001684 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001685 this.prefs_.getString('background-position'));
1686 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1687 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1688 this.scrollPort_.setAccessibilityReader(
1689 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001690
rginda0918b652012-04-04 11:26:24 -07001691 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001692
Joel Hockeyd4fca732019-09-20 16:57:03 -07001693 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001694 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001695
Joel Hockeyd4fca732019-09-20 16:57:03 -07001696 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001697 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001698 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001699
rginda8ba33642011-12-14 12:31:31 -08001700 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001701 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001702
Evan Jones5f9df812016-12-06 09:38:58 -05001703 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001704 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001705
Mike Frysingerdc727792020-04-10 01:41:13 -04001706 const onMouse = this.onMouse_.bind(this);
1707 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001708 screenNode.addEventListener(
1709 'mousedown', /** @type {!EventListener} */ (onMouse));
1710 screenNode.addEventListener(
1711 'mouseup', /** @type {!EventListener} */ (onMouse));
1712 screenNode.addEventListener(
1713 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001714 this.scrollPort_.onScrollWheel = onMouse;
1715
Joel Hockeyd4fca732019-09-20 16:57:03 -07001716 screenNode.addEventListener(
1717 'keydown',
1718 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001719
Toni Barzic0bfa8922013-11-22 11:18:35 -08001720 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001721 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001722 // Listen for mousedown events on the screenNode as in FF the focus
1723 // events don't bubble.
1724 screenNode.addEventListener('mousedown', function() {
1725 setTimeout(this.onFocusChange_.bind(this, true));
1726 }.bind(this));
1727
Toni Barzic0bfa8922013-11-22 11:18:35 -08001728 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001729 'blur', this.onFocusChange_.bind(this, false));
1730
Mike Frysingerdc727792020-04-10 01:41:13 -04001731 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001732 style.textContent = `
1733.cursor-node[focus="false"] {
1734 box-sizing: border-box;
1735 background-color: transparent !important;
1736 border-width: 2px;
1737 border-style: solid;
1738}
1739menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001740 background: #fff;
1741 border-radius: 4px;
1742 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001743 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001744 display: none;
1745 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1746 margin: 0;
1747 padding: 8px 0;
1748 position: absolute;
1749 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001750}
1751menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001752 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001753 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1754 padding: 0.5em 1em;
1755 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001756}
1757menuitem.separator {
1758 border-bottom: none;
1759 height: 0.5em;
1760 padding: 0;
1761}
1762menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001763 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001764}
1765.wc-node {
1766 display: inline-block;
1767 text-align: center;
1768 width: calc(var(--hterm-charsize-width) * 2);
1769 line-height: var(--hterm-charsize-height);
1770}
1771:root {
1772 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1773 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001774 --hterm-blink-node-duration: 0.7s;
1775 --hterm-mouse-cursor-default: default;
1776 --hterm-mouse-cursor-text: text;
1777 --hterm-mouse-cursor-pointer: pointer;
1778 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001779 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001780
Joel Hockey42dba8f2020-03-26 16:21:11 -07001781${lib.colors.stockColorPalette.map((c, i) => `
1782 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1783`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001784}
1785.uri-node:hover {
1786 text-decoration: underline;
1787 cursor: var(--hterm-mouse-cursor-pointer);
1788}
1789@keyframes blink {
1790 from { opacity: 1.0; }
1791 to { opacity: 0.0; }
1792}
1793.blink-node {
1794 animation-name: blink;
1795 animation-duration: var(--hterm-blink-node-duration);
1796 animation-iteration-count: infinite;
1797 animation-timing-function: ease-in-out;
1798 animation-direction: alternate;
1799}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001800 // Insert this stock style as the first node so that any user styles will
1801 // override w/out having to use !important everywhere. The rules above mix
1802 // runtime variables with default ones designed to be overridden by the user,
1803 // but we can wait for a concrete case from the users to determine the best
1804 // way to split the sheet up to before & after the user-css settings.
1805 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001806
rginda8ba33642011-12-14 12:31:31 -08001807 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001808 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001809 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001810 this.cursorNode_.style.cssText = `
1811position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001812left: calc(var(--hterm-screen-padding-size) +
1813 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1814top: calc(var(--hterm-screen-padding-size) +
1815 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001816display: ${this.options_.cursorVisible ? '' : 'none'};
1817width: var(--hterm-charsize-width);
1818height: var(--hterm-charsize-height);
1819background-color: var(--hterm-cursor-color);
1820border-color: var(--hterm-cursor-color);
1821-webkit-transition: opacity, background-color 100ms linear;
1822-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001823
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001824 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001825 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1826 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001827
rginda8ba33642011-12-14 12:31:31 -08001828 this.document_.body.appendChild(this.cursorNode_);
1829
rgindad5613292012-06-19 15:40:37 -07001830 // When 'enableMouseDragScroll' is off we reposition this element directly
1831 // under the mouse cursor after a click. This makes Chrome associate
1832 // subsequent mousemove events with the scroll-blocker. Since the
1833 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1834 // events do not cause the scrollport to scroll.
1835 //
1836 // It's a hack, but it's the cleanest way I could find.
1837 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001838 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001839 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001840 this.scrollBlockerNode_.style.cssText =
1841 ('position: absolute;' +
1842 'top: -99px;' +
1843 'display: block;' +
1844 'width: 10px;' +
1845 'height: 10px;');
1846 this.document_.body.appendChild(this.scrollBlockerNode_);
1847
rgindad5613292012-06-19 15:40:37 -07001848 this.scrollPort_.onScrollWheel = onMouse;
1849 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1850 ].forEach(function(event) {
1851 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001852 this.cursorNode_.addEventListener(
1853 event, /** @type {!EventListener} */ (onMouse));
1854 this.document_.addEventListener(
1855 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001856 }.bind(this));
1857
1858 this.cursorNode_.addEventListener('mousedown', function() {
1859 setTimeout(this.focus.bind(this));
1860 }.bind(this));
1861
rginda8ba33642011-12-14 12:31:31 -08001862 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001863
rginda87b86462011-12-14 13:48:03 -08001864 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001865 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001866};
1867
rginda0918b652012-04-04 11:26:24 -07001868/**
1869 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001870 *
Joel Hockey0f933582019-08-27 18:01:51 -07001871 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001872 */
rginda87b86462011-12-14 13:48:03 -08001873hterm.Terminal.prototype.getDocument = function() {
1874 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001875};
1876
1877/**
rginda0918b652012-04-04 11:26:24 -07001878 * Focus the terminal.
1879 */
1880hterm.Terminal.prototype.focus = function() {
1881 this.scrollPort_.focus();
1882};
1883
1884/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001885 * Unfocus the terminal.
1886 */
1887hterm.Terminal.prototype.blur = function() {
1888 this.scrollPort_.blur();
1889};
1890
1891/**
rginda8ba33642011-12-14 12:31:31 -08001892 * Return the HTML Element for a given row index.
1893 *
1894 * This is a method from the RowProvider interface. The ScrollPort uses
1895 * it to fetch rows on demand as they are scrolled into view.
1896 *
1897 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1898 * pairs to conserve memory.
1899 *
Joel Hockey0f933582019-08-27 18:01:51 -07001900 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001901 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001902 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001903 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001904 * @override
rginda8ba33642011-12-14 12:31:31 -08001905 */
1906hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001907 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001908 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001909 }
rginda8ba33642011-12-14 12:31:31 -08001910
Mike Frysingerdc727792020-04-10 01:41:13 -04001911 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001912 return this.screen_.rowsArray[screenIndex];
1913};
1914
1915/**
1916 * Return the text content for a given range of rows.
1917 *
1918 * This is a method from the RowProvider interface. The ScrollPort uses
1919 * it to fetch text content on demand when the user attempts to copy their
1920 * selection to the clipboard.
1921 *
Joel Hockey0f933582019-08-27 18:01:51 -07001922 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001923 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001924 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001925 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001926 * relative to the start of the scrollback buffer.
1927 * @return {string} A single string containing the text value of the range of
1928 * rows. Lines will be newline delimited, with no trailing newline.
1929 */
1930hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001931 const ary = [];
1932 for (let i = start; i < end; i++) {
1933 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001934 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001935 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001936 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001937 }
rginda8ba33642011-12-14 12:31:31 -08001938 }
1939
rgindaa09e7332012-08-17 12:49:51 -07001940 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001941};
1942
1943/**
1944 * Return the text content for a given row.
1945 *
1946 * This is a method from the RowProvider interface. The ScrollPort uses
1947 * it to fetch text content on demand when the user attempts to copy their
1948 * selection to the clipboard.
1949 *
Joel Hockey0f933582019-08-27 18:01:51 -07001950 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001951 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001952 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001953 * @return {string} A string containing the text value of the selected row.
1954 */
1955hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001956 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001957 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001958};
1959
1960/**
1961 * Return the total number of rows in the addressable screen and in the
1962 * scrollback buffer of this terminal.
1963 *
1964 * This is a method from the RowProvider interface. The ScrollPort uses
1965 * it to compute the size of the scrollbar.
1966 *
Joel Hockey0f933582019-08-27 18:01:51 -07001967 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001968 * @override
rginda8ba33642011-12-14 12:31:31 -08001969 */
1970hterm.Terminal.prototype.getRowCount = function() {
1971 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1972};
1973
1974/**
1975 * Create DOM nodes for new rows and append them to the end of the terminal.
1976 *
1977 * This is the only correct way to add a new DOM node for a row. Notice that
1978 * the new row is appended to the bottom of the list of rows, and does not
1979 * require renumbering (of the rowIndex property) of previous rows.
1980 *
1981 * If you think you want a new blank row somewhere in the middle of the
1982 * terminal, look into moveRows_().
1983 *
1984 * This method does not pay attention to vtScrollTop/Bottom, since you should
1985 * be using moveRows() in cases where they would matter.
1986 *
1987 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001988 *
1989 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001990 */
1991hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001992 let cursorRow = this.screen_.rowsArray.length;
1993 const offset = this.scrollbackRows_.length + cursorRow;
1994 for (let i = 0; i < count; i++) {
1995 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08001996 row.appendChild(this.document_.createTextNode(''));
1997 row.rowIndex = offset + i;
1998 this.screen_.pushRow(row);
1999 }
2000
Mike Frysingerdc727792020-04-10 01:41:13 -04002001 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002002 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002003 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002004 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002005 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002006 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002007 }
rginda8ba33642011-12-14 12:31:31 -08002008 }
2009
Mike Frysingerbdb34802020-04-07 03:47:32 -04002010 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002011 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002012 }
rginda8ba33642011-12-14 12:31:31 -08002013
rginda87b86462011-12-14 13:48:03 -08002014 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002015};
2016
2017/**
2018 * Relocate rows from one part of the addressable screen to another.
2019 *
2020 * This is used to recycle rows during VT scrolls (those which are driven
2021 * by VT commands, rather than by the user manipulating the scrollbar.)
2022 *
2023 * In this case, the blank lines scrolled into the scroll region are made of
2024 * the nodes we scrolled off. These have their rowIndex properties carefully
2025 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002026 *
2027 * @param {number} fromIndex The start index.
2028 * @param {number} count The number of rows to move.
2029 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002030 */
2031hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002032 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002033 this.screen_.insertRows(toIndex, ary);
2034
Mike Frysingerdc727792020-04-10 01:41:13 -04002035 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002036 if (fromIndex < toIndex) {
2037 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002038 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002039 } else {
2040 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002041 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002042 }
2043
2044 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002045 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002046};
2047
2048/**
2049 * Renumber the rowIndex property of the given range of rows.
2050 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002051 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002052 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002053 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002054 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002055 *
2056 * @param {number} start The start index.
2057 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002058 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002059 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002060hterm.Terminal.prototype.renumberRows_ = function(
2061 start, end, screen = undefined) {
2062 if (!screen) {
2063 screen = this.screen_;
2064 }
Robert Ginda40932892012-12-10 17:26:40 -08002065
Mike Frysingerdc727792020-04-10 01:41:13 -04002066 const offset = this.scrollbackRows_.length;
2067 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002068 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002069 }
2070};
2071
2072/**
2073 * Print a string to the terminal.
2074 *
2075 * This respects the current insert and wraparound modes. It will add new lines
2076 * to the end of the terminal, scrolling off the top into the scrollback buffer
2077 * if necessary.
2078 *
2079 * The string is *not* parsed for escape codes. Use the interpret() method if
2080 * that's what you're after.
2081 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002082 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002083 */
2084hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002085 this.scheduleSyncCursorPosition_();
2086
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002087 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002088 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002089
Mike Frysingerdc727792020-04-10 01:41:13 -04002090 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002091
Mike Frysingerdc727792020-04-10 01:41:13 -04002092 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002093 // Fun edge case: If the string only contains zero width codepoints (like
2094 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002095 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002096 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002097 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002098
2099 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002100 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2101 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002102 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002103 }
rgindaa19afe22012-01-25 15:40:22 -08002104
Mike Frysingerdc727792020-04-10 01:41:13 -04002105 let count = strWidth - startOffset;
2106 let didOverflow = false;
2107 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002108
rgindaa9abdd82012-08-06 18:05:09 -07002109 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2110 didOverflow = true;
2111 count = this.screenSize.width - this.screen_.cursorPosition.column;
2112 }
rgindaa19afe22012-01-25 15:40:22 -08002113
rgindaa9abdd82012-08-06 18:05:09 -07002114 if (didOverflow && !this.options_.wraparound) {
2115 // If the string overflowed the line but wraparound is off, then the
2116 // last printed character should be the last of the string.
2117 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002118 substr = lib.wc.substr(str, startOffset, count - 1) +
2119 lib.wc.substr(str, strWidth - 1);
2120 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002121 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002122 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002123 }
rgindaa19afe22012-01-25 15:40:22 -08002124
Mike Frysingerdc727792020-04-10 01:41:13 -04002125 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2126 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002127 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2128 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002129
2130 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002131 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002132 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002133 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002134 }
2135 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002136 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002137 }
2138
2139 this.screen_.maybeClipCurrentRow();
2140 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002141 }
rginda8ba33642011-12-14 12:31:31 -08002142
Mike Frysingerbdb34802020-04-07 03:47:32 -04002143 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002144 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002145 }
rginda8ba33642011-12-14 12:31:31 -08002146};
2147
2148/**
rginda87b86462011-12-14 13:48:03 -08002149 * Set the VT scroll region.
2150 *
rginda87b86462011-12-14 13:48:03 -08002151 * This also resets the cursor position to the absolute (0, 0) position, since
2152 * that's what xterm appears to do.
2153 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002154 * Setting the scroll region to the full height of the terminal will clear
2155 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2156 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2157 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2158 * continue to work as most users would expect.
2159 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002160 * @param {?number} scrollTop The zero-based top of the scroll region.
2161 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002162 * inclusive.
2163 */
2164hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002165 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002166 this.vtScrollTop_ = null;
2167 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002168 } else {
2169 this.vtScrollTop_ = scrollTop;
2170 this.vtScrollBottom_ = scrollBottom;
2171 }
rginda87b86462011-12-14 13:48:03 -08002172};
2173
2174/**
rginda8ba33642011-12-14 12:31:31 -08002175 * Return the top row index according to the VT.
2176 *
2177 * This will return 0 unless the terminal has been told to restrict scrolling
2178 * to some lower row. It is used for some VT cursor positioning and scrolling
2179 * commands.
2180 *
Joel Hockey0f933582019-08-27 18:01:51 -07002181 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002182 */
2183hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002184 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002185 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002186 }
rginda8ba33642011-12-14 12:31:31 -08002187
2188 return 0;
rginda87b86462011-12-14 13:48:03 -08002189};
rginda8ba33642011-12-14 12:31:31 -08002190
2191/**
2192 * Return the bottom row index according to the VT.
2193 *
2194 * This will return the height of the terminal unless the it has been told to
2195 * restrict scrolling to some higher row. It is used for some VT cursor
2196 * positioning and scrolling commands.
2197 *
Joel Hockey0f933582019-08-27 18:01:51 -07002198 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002199 */
2200hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002201 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002202 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002203 }
rginda8ba33642011-12-14 12:31:31 -08002204
rginda87b86462011-12-14 13:48:03 -08002205 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002206};
rginda8ba33642011-12-14 12:31:31 -08002207
2208/**
2209 * Process a '\n' character.
2210 *
2211 * If the cursor is on the final row of the terminal this will append a new
2212 * blank row to the screen and scroll the topmost row into the scrollback
2213 * buffer.
2214 *
2215 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002216 *
2217 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2218 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002219 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002220hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002221 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002222 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002223 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002224
Mike Frysingerdc727792020-04-10 01:41:13 -04002225 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2226 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002227
2228 if (this.vtScrollBottom_ != null) {
2229 // A VT Scroll region is active, we never append new rows.
2230 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2231 // We're at the end of the VT Scroll Region, perform a VT scroll.
2232 this.vtScrollUp(1);
2233 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2234 } else if (cursorAtEndOfScreen) {
2235 // We're at the end of the screen, the only thing to do is put the
2236 // cursor to column 0.
2237 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2238 } else {
2239 // Anywhere else, advance the cursor row, and reset the column.
2240 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2241 }
2242 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002243 // We're at the end of the screen. Append a new row to the terminal,
2244 // shifting the top row into the scrollback.
2245 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002246 } else {
rginda87b86462011-12-14 13:48:03 -08002247 // Anywhere else in the screen just moves the cursor.
2248 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002249 }
2250};
2251
2252/**
2253 * Like newLine(), except maintain the cursor column.
2254 */
2255hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002256 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002257 this.newLine();
2258 this.setCursorColumn(column);
2259};
2260
2261/**
rginda87b86462011-12-14 13:48:03 -08002262 * If autoCarriageReturn is set then newLine(), else lineFeed().
2263 */
2264hterm.Terminal.prototype.formFeed = function() {
2265 if (this.options_.autoCarriageReturn) {
2266 this.newLine();
2267 } else {
2268 this.lineFeed();
2269 }
2270};
2271
2272/**
2273 * Move the cursor up one row, possibly inserting a blank line.
2274 *
2275 * The cursor column is not changed.
2276 */
2277hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002278 const scrollTop = this.getVTScrollTop();
2279 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002280
2281 if (currentRow == scrollTop) {
2282 this.insertLines(1);
2283 } else {
2284 this.setAbsoluteCursorRow(currentRow - 1);
2285 }
2286};
2287
2288/**
rginda8ba33642011-12-14 12:31:31 -08002289 * Replace all characters to the left of the current cursor with the space
2290 * character.
2291 *
2292 * TODO(rginda): This should probably *remove* the characters (not just replace
2293 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002294 * position.
rginda8ba33642011-12-14 12:31:31 -08002295 */
2296hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002297 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002298 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002299 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002300 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002301 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002302};
2303
2304/**
David Benjamin684a9b72012-05-01 17:19:58 -04002305 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002306 *
2307 * The cursor position is unchanged.
2308 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002309 * If the current background color is not the default background color this
2310 * will insert spaces rather than delete. This is unfortunate because the
2311 * trailing space will affect text selection, but it's difficult to come up
2312 * with a way to style empty space that wouldn't trip up the hterm.Screen
2313 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002314 *
2315 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2316 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2317 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002318 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002319 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002320 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002321hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002322 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002323 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002324 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002325
Mike Frysingerdc727792020-04-10 01:41:13 -04002326 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002327 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002328
2329 if (this.screen_.textAttributes.background ===
2330 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002331 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002332 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002333 this.screen_.cursorPosition.column + count) {
2334 this.screen_.deleteChars(count);
2335 this.clearCursorOverflow();
2336 return;
2337 }
2338 }
2339
Mike Frysingerdc727792020-04-10 01:41:13 -04002340 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002341 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002342 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002343 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002344};
2345
2346/**
2347 * Erase the current line.
2348 *
2349 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002350 */
2351hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002352 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002353 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002354 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002355 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002356};
2357
2358/**
David Benjamina08d78f2012-05-05 00:28:49 -04002359 * Erase all characters from the start of the screen to the current cursor
2360 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002361 *
2362 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002363 */
2364hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002365 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002366
2367 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002368
Mike Frysingerdc727792020-04-10 01:41:13 -04002369 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002370 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002371 this.screen_.clearCursorRow();
2372 }
2373
rginda87b86462011-12-14 13:48:03 -08002374 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002375 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002376};
2377
2378/**
2379 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002380 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002381 *
2382 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002383 */
2384hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002385 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002386
2387 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002388
Mike Frysingerdc727792020-04-10 01:41:13 -04002389 const bottom = this.screenSize.height - 1;
2390 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002391 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002392 this.screen_.clearCursorRow();
2393 }
2394
rginda87b86462011-12-14 13:48:03 -08002395 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002396 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002397};
2398
2399/**
2400 * Fill the terminal with a given character.
2401 *
2402 * This methods does not respect the VT scroll region.
2403 *
2404 * @param {string} ch The character to use for the fill.
2405 */
2406hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002407 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002408
2409 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002410 for (let row = 0; row < this.screenSize.height; row++) {
2411 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002412 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002413 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002414 }
2415 }
2416
2417 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002418};
2419
2420/**
rginda9ea433c2012-03-16 11:57:00 -07002421 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002422 *
rginda9ea433c2012-03-16 11:57:00 -07002423 * This does not respect the scroll region.
2424 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002425 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002426 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002427 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002428hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2429 if (!screen) {
2430 screen = this.screen_;
2431 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002432 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002433
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002434 this.accessibilityReader_.clear();
2435
rginda11057d52012-04-25 12:29:56 -07002436 if (bottom == 0) {
2437 // Empty screen, nothing to do.
2438 return;
2439 }
2440
Mike Frysingerdc727792020-04-10 01:41:13 -04002441 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002442 screen.setCursorPosition(i, 0);
2443 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002444 }
2445
rginda9ea433c2012-03-16 11:57:00 -07002446 screen.setCursorPosition(0, 0);
2447};
2448
2449/**
2450 * Erase the entire display without changing the cursor position.
2451 *
2452 * The cursor position is unchanged. This does not respect the scroll
2453 * region.
2454 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002455 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002456 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002457 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002458hterm.Terminal.prototype.clear = function(screen = undefined) {
2459 if (!screen) {
2460 screen = this.screen_;
2461 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002462 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002463 this.clearHome(screen);
2464 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002465};
2466
2467/**
2468 * VT command to insert lines at the current cursor row.
2469 *
2470 * This respects the current scroll region. Rows pushed off the bottom are
2471 * lost (they won't show up in the scrollback buffer).
2472 *
Joel Hockey0f933582019-08-27 18:01:51 -07002473 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002474 */
2475hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002476 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002477
Mike Frysingerdc727792020-04-10 01:41:13 -04002478 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002479 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002480
Robert Ginda579186b2012-09-26 11:40:04 -07002481 // The moveCount is the number of rows we need to relocate to make room for
2482 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002483 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002484 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002485 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002486 }
rginda8ba33642011-12-14 12:31:31 -08002487
Mike Frysingerdc727792020-04-10 01:41:13 -04002488 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002489 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002490 this.screen_.clearCursorRow();
2491 }
rginda8ba33642011-12-14 12:31:31 -08002492};
2493
2494/**
2495 * VT command to delete lines at the current cursor row.
2496 *
2497 * New rows are added to the bottom of scroll region to take their place. New
2498 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002499 *
2500 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002501 */
2502hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002503 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002504
Mike Frysingerdc727792020-04-10 01:41:13 -04002505 const top = cursor.row;
2506 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002507
Mike Frysingerdc727792020-04-10 01:41:13 -04002508 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002509 count = Math.min(count, maxCount);
2510
Mike Frysingerdc727792020-04-10 01:41:13 -04002511 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002512 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002513 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002514 }
rginda8ba33642011-12-14 12:31:31 -08002515
Mike Frysingerdc727792020-04-10 01:41:13 -04002516 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002517 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002518 this.screen_.clearCursorRow();
2519 }
2520
rginda87b86462011-12-14 13:48:03 -08002521 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002522 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002523};
2524
2525/**
2526 * Inserts the given number of spaces at the current cursor position.
2527 *
rginda87b86462011-12-14 13:48:03 -08002528 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002529 *
2530 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002531 */
2532hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002533 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002534
Mike Frysinger73e56462019-07-17 00:23:46 -05002535 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002536 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002537 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002538
2539 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002540 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002541};
2542
2543/**
2544 * Forward-delete the specified number of characters starting at the cursor
2545 * position.
2546 *
Joel Hockey0f933582019-08-27 18:01:51 -07002547 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002548 */
2549hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002550 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002551 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002552 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002553 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002554 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002555 this.restoreCursor(cursor);
2556 }
2557
David Benjamin54e8bf62012-06-01 22:31:40 -04002558 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002559};
2560
2561/**
2562 * Shift rows in the scroll region upwards by a given number of lines.
2563 *
2564 * New rows are inserted at the bottom of the scroll region to fill the
2565 * vacated rows. The new rows not filled out with the current text attributes.
2566 *
2567 * This function does not affect the scrollback rows at all. Rows shifted
2568 * off the top are lost.
2569 *
rginda87b86462011-12-14 13:48:03 -08002570 * The cursor position is not altered.
2571 *
Joel Hockey0f933582019-08-27 18:01:51 -07002572 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002573 */
2574hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002575 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002576
rginda87b86462011-12-14 13:48:03 -08002577 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002578 this.deleteLines(count);
2579
rginda87b86462011-12-14 13:48:03 -08002580 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002581};
2582
2583/**
2584 * Shift rows below the cursor down by a given number of lines.
2585 *
2586 * This function respects the current scroll region.
2587 *
2588 * New rows are inserted at the top of the scroll region to fill the
2589 * vacated rows. The new rows not filled out with the current text attributes.
2590 *
2591 * This function does not affect the scrollback rows at all. Rows shifted
2592 * off the bottom are lost.
2593 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002594 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002595 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002596hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002597 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002598
rginda87b86462011-12-14 13:48:03 -08002599 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002600 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002601
rginda87b86462011-12-14 13:48:03 -08002602 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002603};
2604
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002605/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002606 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002607 *
2608 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002609 * cause Assitive Technology to announce the output of the terminal. It also
2610 * enables other features that aid assistive technology. All the features gated
2611 * behind this flag have a performance impact on the terminal which is why they
2612 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002613 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002614 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002615 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002616hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002617 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002618};
rginda87b86462011-12-14 13:48:03 -08002619
rginda8ba33642011-12-14 12:31:31 -08002620/**
2621 * Set the cursor position.
2622 *
2623 * The cursor row is relative to the scroll region if the terminal has
2624 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2625 *
Joel Hockey0f933582019-08-27 18:01:51 -07002626 * @param {number} row The new zero-based cursor row.
2627 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002628 */
2629hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2630 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002631 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002632 } else {
rginda87b86462011-12-14 13:48:03 -08002633 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002634 }
rginda87b86462011-12-14 13:48:03 -08002635};
rginda8ba33642011-12-14 12:31:31 -08002636
Evan Jones2600d4f2016-12-06 09:29:36 -05002637/**
2638 * Move the cursor relative to its current position.
2639 *
2640 * @param {number} row
2641 * @param {number} column
2642 */
rginda87b86462011-12-14 13:48:03 -08002643hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002644 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002645 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2646 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002647 this.screen_.setCursorPosition(row, column);
2648};
2649
Evan Jones2600d4f2016-12-06 09:29:36 -05002650/**
2651 * Move the cursor to the specified position.
2652 *
2653 * @param {number} row
2654 * @param {number} column
2655 */
rginda87b86462011-12-14 13:48:03 -08002656hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002657 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2658 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002659 this.screen_.setCursorPosition(row, column);
2660};
2661
2662/**
2663 * Set the cursor column.
2664 *
Joel Hockey0f933582019-08-27 18:01:51 -07002665 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002666 */
2667hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002668 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002669};
2670
2671/**
2672 * Return the cursor column.
2673 *
Joel Hockey0f933582019-08-27 18:01:51 -07002674 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002675 */
2676hterm.Terminal.prototype.getCursorColumn = function() {
2677 return this.screen_.cursorPosition.column;
2678};
2679
2680/**
2681 * Set the cursor row.
2682 *
2683 * The cursor row is relative to the scroll region if the terminal has
2684 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2685 *
Joel Hockey0f933582019-08-27 18:01:51 -07002686 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002687 */
rginda87b86462011-12-14 13:48:03 -08002688hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2689 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002690};
2691
2692/**
2693 * Return the cursor row.
2694 *
Joel Hockey0f933582019-08-27 18:01:51 -07002695 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002696 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002697hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002698 return this.screen_.cursorPosition.row;
2699};
2700
2701/**
2702 * Request that the ScrollPort redraw itself soon.
2703 *
2704 * The redraw will happen asynchronously, soon after the call stack winds down.
2705 * Multiple calls will be coalesced into a single redraw.
2706 */
2707hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002708 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002709 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002710 }
rginda8ba33642011-12-14 12:31:31 -08002711
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002712 this.timeouts_.redraw = setTimeout(() => {
2713 delete this.timeouts_.redraw;
2714 this.scrollPort_.redraw_();
2715 });
rginda8ba33642011-12-14 12:31:31 -08002716};
2717
2718/**
2719 * Request that the ScrollPort be scrolled to the bottom.
2720 *
2721 * The scroll will happen asynchronously, soon after the call stack winds down.
2722 * Multiple calls will be coalesced into a single scroll.
2723 *
2724 * This affects the scrollbar position of the ScrollPort, and has nothing to
2725 * do with the VT scroll commands.
2726 */
2727hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002728 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002729 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002730 }
rginda8ba33642011-12-14 12:31:31 -08002731
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002732 this.timeouts_.scrollDown = setTimeout(() => {
2733 delete this.timeouts_.scrollDown;
2734 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2735 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002736};
2737
2738/**
2739 * Move the cursor up a specified number of rows.
2740 *
Joel Hockey0f933582019-08-27 18:01:51 -07002741 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002742 */
2743hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002744 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002745};
2746
2747/**
2748 * Move the cursor down a specified number of rows.
2749 *
Joel Hockey0f933582019-08-27 18:01:51 -07002750 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002751 */
2752hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002753 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002754 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2755 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2756 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002757
Mike Frysingerdc727792020-04-10 01:41:13 -04002758 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2759 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002760 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002761};
2762
2763/**
2764 * Move the cursor left a specified number of columns.
2765 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002766 * If reverse wraparound mode is enabled and the previous row wrapped into
2767 * the current row then we back up through the wraparound as well.
2768 *
Joel Hockey0f933582019-08-27 18:01:51 -07002769 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002770 */
2771hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002772 count = count || 1;
2773
Mike Frysingerbdb34802020-04-07 03:47:32 -04002774 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002775 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002776 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002777
Mike Frysingerdc727792020-04-10 01:41:13 -04002778 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002779 if (this.options_.reverseWraparound) {
2780 if (this.screen_.cursorPosition.overflow) {
2781 // If this cursor is in the right margin, consume one count to get it
2782 // back to the last column. This only applies when we're in reverse
2783 // wraparound mode.
2784 count--;
2785 this.clearCursorOverflow();
2786
Mike Frysingerbdb34802020-04-07 03:47:32 -04002787 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002788 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002789 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002790 }
2791
Mike Frysingerdc727792020-04-10 01:41:13 -04002792 let newRow = this.screen_.cursorPosition.row;
2793 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002794 if (newColumn < 0) {
2795 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2796 if (newRow < 0) {
2797 // xterm also wraps from row 0 to the last row.
2798 newRow = this.screenSize.height + newRow % this.screenSize.height;
2799 }
2800 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2801 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002802
Robert Gindabfb32622014-07-17 13:20:27 -07002803 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2804
2805 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002806 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002807 this.setCursorColumn(newColumn);
2808 }
rginda8ba33642011-12-14 12:31:31 -08002809};
2810
2811/**
2812 * Move the cursor right a specified number of columns.
2813 *
Joel Hockey0f933582019-08-27 18:01:51 -07002814 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002815 */
2816hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002817 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002818
Mike Frysingerbdb34802020-04-07 03:47:32 -04002819 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002820 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002821 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002822
Mike Frysingerdc727792020-04-10 01:41:13 -04002823 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2824 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002825 this.setCursorColumn(column);
2826};
2827
2828/**
2829 * Reverse the foreground and background colors of the terminal.
2830 *
2831 * This only affects text that was drawn with no attributes.
2832 *
2833 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2834 * been drawn with attributes that happen to coincide with the default
2835 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002836 *
2837 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002838 */
2839hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002840 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002841 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002842 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2843 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002844 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002845 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2846 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002847 }
2848};
2849
2850/**
rginda87b86462011-12-14 13:48:03 -08002851 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002852 *
2853 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002854 */
2855hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002856 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002857
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002858 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002859
Michael Kelly485ecd12014-06-09 11:41:56 -04002860 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002861 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002862 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002863 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002864
Robert Ginda92e18102013-03-14 13:56:37 -07002865 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002866 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002867 this.bellSequelchTimeout_ = setTimeout(() => {
2868 this.bellSquelchTimeout_ = null;
2869 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002870 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002871 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002872 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002873
2874 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002875 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002876 this.bellNotificationList_.push(n);
2877 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002878 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002879 }
rginda87b86462011-12-14 13:48:03 -08002880};
2881
2882/**
rginda8ba33642011-12-14 12:31:31 -08002883 * Set the origin mode bit.
2884 *
2885 * If origin mode is on, certain VT cursor and scrolling commands measure their
2886 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2887 * to the top of the addressable screen.
2888 *
2889 * Defaults to off.
2890 *
2891 * @param {boolean} state True to set origin mode, false to unset.
2892 */
2893hterm.Terminal.prototype.setOriginMode = function(state) {
2894 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002895 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002896};
2897
2898/**
2899 * Set the insert mode bit.
2900 *
2901 * If insert mode is on, existing text beyond the cursor position will be
2902 * shifted right to make room for new text. Otherwise, new text overwrites
2903 * any existing text.
2904 *
2905 * Defaults to off.
2906 *
2907 * @param {boolean} state True to set insert mode, false to unset.
2908 */
2909hterm.Terminal.prototype.setInsertMode = function(state) {
2910 this.options_.insertMode = state;
2911};
2912
2913/**
rginda87b86462011-12-14 13:48:03 -08002914 * Set the auto carriage return bit.
2915 *
2916 * If auto carriage return is on then a formfeed character is interpreted
2917 * as a newline, otherwise it's the same as a linefeed. The difference boils
2918 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002919 *
2920 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002921 */
2922hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2923 this.options_.autoCarriageReturn = state;
2924};
2925
2926/**
rginda8ba33642011-12-14 12:31:31 -08002927 * Set the wraparound mode bit.
2928 *
2929 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2930 * to the start of the following row. Otherwise, the cursor is clamped to the
2931 * end of the screen and attempts to write past it are ignored.
2932 *
2933 * Defaults to on.
2934 *
2935 * @param {boolean} state True to set wraparound mode, false to unset.
2936 */
2937hterm.Terminal.prototype.setWraparound = function(state) {
2938 this.options_.wraparound = state;
2939};
2940
2941/**
2942 * Set the reverse-wraparound mode bit.
2943 *
2944 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2945 * to the end of the previous row. Otherwise, the cursor is clamped to column
2946 * 0.
2947 *
2948 * Defaults to off.
2949 *
2950 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2951 */
2952hterm.Terminal.prototype.setReverseWraparound = function(state) {
2953 this.options_.reverseWraparound = state;
2954};
2955
2956/**
2957 * Selects between the primary and alternate screens.
2958 *
2959 * If alternate mode is on, the alternate screen is active. Otherwise the
2960 * primary screen is active.
2961 *
2962 * Swapping screens has no effect on the scrollback buffer.
2963 *
2964 * Each screen maintains its own cursor position.
2965 *
2966 * Defaults to off.
2967 *
2968 * @param {boolean} state True to set alternate mode, false to unset.
2969 */
2970hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002971 if (state == (this.screen_ == this.alternateScreen_)) {
2972 return;
2973 }
2974 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2975 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002976 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2977
Joel Hockey42dba8f2020-03-26 16:21:11 -07002978 // Swap color overrides.
2979 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2980 oldOverrides.forEach((c, i) => {
2981 if (!newOverrides.hasOwnProperty(i)) {
2982 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2983 }
2984 });
2985 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2986
rginda35c456b2012-02-09 17:29:05 -08002987 if (this.screen_.rowsArray.length &&
2988 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2989 // If the screen changed sizes while we were away, our rowIndexes may
2990 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002991 const offset = this.scrollbackRows_.length;
2992 const ary = this.screen_.rowsArray;
2993 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002994 ary[i].rowIndex = offset + i;
2995 }
2996 }
rginda8ba33642011-12-14 12:31:31 -08002997
rginda35c456b2012-02-09 17:29:05 -08002998 this.realizeWidth_(this.screenSize.width);
2999 this.realizeHeight_(this.screenSize.height);
3000 this.scrollPort_.syncScrollHeight();
3001 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003002
rginda6d397402012-01-17 10:58:29 -08003003 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003004 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003005};
3006
3007/**
3008 * Set the cursor-blink mode bit.
3009 *
3010 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3011 * a visible cursor does not blink.
3012 *
3013 * You should make sure to turn blinking off if you're going to dispose of a
3014 * terminal, otherwise you'll leak a timeout.
3015 *
3016 * Defaults to on.
3017 *
3018 * @param {boolean} state True to set cursor-blink mode, false to unset.
3019 */
3020hterm.Terminal.prototype.setCursorBlink = function(state) {
3021 this.options_.cursorBlink = state;
3022
3023 if (!state && this.timeouts_.cursorBlink) {
3024 clearTimeout(this.timeouts_.cursorBlink);
3025 delete this.timeouts_.cursorBlink;
3026 }
3027
Mike Frysingerbdb34802020-04-07 03:47:32 -04003028 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003029 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003030 }
rginda8ba33642011-12-14 12:31:31 -08003031};
3032
3033/**
3034 * Set the cursor-visible mode bit.
3035 *
3036 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3037 *
3038 * Defaults to on.
3039 *
3040 * @param {boolean} state True to set cursor-visible mode, false to unset.
3041 */
3042hterm.Terminal.prototype.setCursorVisible = function(state) {
3043 this.options_.cursorVisible = state;
3044
3045 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003046 if (this.timeouts_.cursorBlink) {
3047 clearTimeout(this.timeouts_.cursorBlink);
3048 delete this.timeouts_.cursorBlink;
3049 }
rginda87b86462011-12-14 13:48:03 -08003050 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003051 return;
3052 }
3053
rginda87b86462011-12-14 13:48:03 -08003054 this.syncCursorPosition_();
3055
3056 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003057
3058 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003059 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003060 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003061 }
rginda8ba33642011-12-14 12:31:31 -08003062
Robert Gindaea2183e2014-07-17 09:51:51 -07003063 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003064 } else {
3065 if (this.timeouts_.cursorBlink) {
3066 clearTimeout(this.timeouts_.cursorBlink);
3067 delete this.timeouts_.cursorBlink;
3068 }
3069 }
3070};
3071
3072/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003073 * Pause blinking temporarily.
3074 *
3075 * When the cursor moves around, it can be helpful to momentarily pause the
3076 * blinking. This could be when the user is typing in things, or when they're
3077 * moving around with the arrow keys.
3078 */
3079hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3080 if (!this.options_.cursorBlink) {
3081 return;
3082 }
3083
3084 this.cursorBlinkPause_ = true;
3085
3086 // If a timeout is already pending, reset the clock due to the new input.
3087 if (this.timeouts_.cursorBlinkPause) {
3088 clearTimeout(this.timeouts_.cursorBlinkPause);
3089 }
3090 // After 500ms, resume blinking. That seems like a good balance between user
3091 // input timings & responsiveness to resume.
3092 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3093 delete this.timeouts_.cursorBlinkPause;
3094 this.cursorBlinkPause_ = false;
3095 }, 500);
3096};
3097
3098/**
rginda87b86462011-12-14 13:48:03 -08003099 * Synchronizes the visible cursor and document selection with the current
3100 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003101 *
3102 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003103 */
3104hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003105 const topRowIndex = this.scrollPort_.getTopRowIndex();
3106 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3107 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003108 this.screen_.cursorPosition.row;
3109
Raymes Khoury15697f42018-07-17 11:37:18 +10003110 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003111 if (this.accessibilityReader_.accessibilityEnabled) {
3112 // Report the new position of the cursor for accessibility purposes.
3113 const cursorColumnIndex = this.screen_.cursorPosition.column;
3114 const cursorLineText =
3115 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003116 // This will force the selection to be sync'd to the cursor position if the
3117 // user has pressed a key. Generally we would only sync the cursor position
3118 // when selection is collapsed so that if the user has selected something
3119 // we don't clear the selection by moving the selection. However when a
3120 // screen reader is used, it's intuitive for entering a key to move the
3121 // selection to the cursor.
3122 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003123 this.accessibilityReader_.afterCursorChange(
3124 cursorLineText, cursorRowIndex, cursorColumnIndex);
3125 }
3126
rginda8ba33642011-12-14 12:31:31 -08003127 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003128 // Cursor is scrolled off screen, hide it.
3129 this.cursorOffScreen_ = true;
3130 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003131 return false;
rginda8ba33642011-12-14 12:31:31 -08003132 }
3133
Joel Hockey3babf302020-04-22 15:00:06 -07003134 if (this.cursorNode_.style.display == 'none') {
3135 // Re-display the terminal cursor if it was hidden.
3136 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003137 this.cursorNode_.style.display = '';
3138 }
3139
Mike Frysinger44c32202017-08-05 01:13:09 -04003140 // Position the cursor using CSS variable math. If we do the math in JS,
3141 // the float math will end up being more precise than the CSS which will
3142 // cause the cursor tracking to be off.
3143 this.setCssVar(
3144 'cursor-offset-row',
3145 `${cursorRowIndex - topRowIndex} + ` +
3146 `${this.scrollPort_.visibleRowTopMargin}px`);
3147 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003148
3149 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003150 '(' + this.screen_.cursorPosition.column +
3151 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003152 ')');
3153
3154 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003155 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003156 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003157 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003158 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003159 return true;
rginda8ba33642011-12-14 12:31:31 -08003160};
3161
Robert Gindafb1be6a2013-12-11 11:56:22 -08003162/**
3163 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3164 * and character cell dimensions.
3165 */
Robert Ginda830583c2013-08-07 13:20:46 -07003166hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003167 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003168
3169 if (this.cursorNode_.getAttribute('focus') == 'false') {
3170 // Always show a block cursor when unfocused.
3171 shape = hterm.Terminal.cursorShape.BLOCK;
3172 }
3173
Mike Frysingerdc727792020-04-10 01:41:13 -04003174 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003175
3176 switch (shape) {
3177 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003178 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003179 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003180 style.borderLeftStyle = 'solid';
3181 break;
3182
3183 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003184 style.backgroundColor = 'transparent';
3185 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003186 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003187 break;
3188
3189 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003190 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003191 style.borderBottomStyle = '';
3192 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003193 break;
3194 }
3195};
3196
rginda8ba33642011-12-14 12:31:31 -08003197/**
3198 * Synchronizes the visible cursor with the current cursor coordinates.
3199 *
3200 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003201 * Multiple calls will be coalesced into a single sync. This should be called
3202 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003203 */
3204hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003205 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003206 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003207 }
rginda8ba33642011-12-14 12:31:31 -08003208
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003209 if (this.accessibilityReader_.accessibilityEnabled) {
3210 // Report the previous position of the cursor for accessibility purposes.
3211 const cursorRowIndex = this.scrollbackRows_.length +
3212 this.screen_.cursorPosition.row;
3213 const cursorColumnIndex = this.screen_.cursorPosition.column;
3214 const cursorLineText =
3215 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3216 this.accessibilityReader_.beforeCursorChange(
3217 cursorLineText, cursorRowIndex, cursorColumnIndex);
3218 }
3219
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003220 this.timeouts_.syncCursor = setTimeout(() => {
3221 this.syncCursorPosition_();
3222 delete this.timeouts_.syncCursor;
3223 });
rginda87b86462011-12-14 13:48:03 -08003224};
3225
rgindacc2996c2012-02-24 14:59:31 -08003226/**
rgindaf522ce02012-04-17 17:49:17 -07003227 * Show or hide the zoom warning.
3228 *
3229 * The zoom warning is a message warning the user that their browser zoom must
3230 * be set to 100% in order for hterm to function properly.
3231 *
3232 * @param {boolean} state True to show the message, false to hide it.
3233 */
3234hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3235 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003236 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003237 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003238 }
rgindaf522ce02012-04-17 17:49:17 -07003239
3240 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003241 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003242 this.zoomWarningNode_.style.cssText = (
3243 'color: black;' +
3244 'background-color: #ff2222;' +
3245 'font-size: large;' +
3246 'border-radius: 8px;' +
3247 'opacity: 0.75;' +
3248 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3249 'top: 0.5em;' +
3250 'right: 1.2em;' +
3251 'position: absolute;' +
3252 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003253 '-webkit-user-select: none;' +
3254 '-moz-text-size-adjust: none;' +
3255 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003256
3257 this.zoomWarningNode_.addEventListener('click', function(e) {
3258 this.parentNode.removeChild(this);
3259 });
rgindaf522ce02012-04-17 17:49:17 -07003260 }
3261
Mike Frysingerb7289952019-03-23 16:05:38 -07003262 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003263 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003264 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003265
rgindaf522ce02012-04-17 17:49:17 -07003266 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3267
3268 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003269 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003270 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003271 }
rgindaf522ce02012-04-17 17:49:17 -07003272 } else if (this.zoomWarningNode_.parentNode) {
3273 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3274 }
3275};
3276
3277/**
rgindacc2996c2012-02-24 14:59:31 -08003278 * Show the terminal overlay for a given amount of time.
3279 *
Jason Lin3d825782020-05-12 11:02:48 +10003280 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003281 *
3282 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003283 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003284 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3285 * stay up forever (or until the next overlay).
3286 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003287hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003288 this.showOverlayWithNode(new Text(msg), timeout);
3289};
3290
3291/**
3292 * Show the terminal overlay for a given amount of time.
3293 *
3294 * The terminal overlay appears in inverse video, centered over the terminal.
3295 *
3296 * @param {!Node} node The node to display in the overlay.
3297 * @param {?number=} timeout The amount of time to wait before fading out
3298 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3299 * stay up forever (or until the next overlay).
3300 */
3301hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003302 if (!this.ready_ || !this.div_) {
3303 return;
3304 }
rgindaf0090c92012-02-10 14:58:52 -08003305
Joel Hockeyedac0e72020-05-14 20:16:20 -07003306 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003307 this.overlayNode_ = this.document_.createElement('div');
3308 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003309 'color: rgb(var(--hterm-background-color));' +
3310 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003311 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003312 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003313 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003314 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003315 'position: absolute;' +
3316 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003317 '-webkit-transition: opacity 180ms ease-in;' +
3318 '-moz-user-select: none;' +
3319 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003320
3321 this.overlayNode_.addEventListener('mousedown', function(e) {
3322 e.preventDefault();
3323 e.stopPropagation();
3324 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003325 }
3326
Jason Lin34567412020-05-14 10:32:09 +10003327 this.overlayNode_.textContent = ''; // Remove all children first.
3328 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003329
Mike Frysingerbdb34802020-04-07 03:47:32 -04003330 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003331 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003332 }
rgindaf0090c92012-02-10 14:58:52 -08003333
Mike Frysingerdc727792020-04-10 01:41:13 -04003334 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3335 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003336
Robert Ginda8a59f762014-07-23 11:29:55 -07003337 this.overlayNode_.style.top =
3338 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003339 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003340 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003341
Mike Frysingerbdb34802020-04-07 03:47:32 -04003342 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003343 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003344 }
rgindaf0090c92012-02-10 14:58:52 -08003345
Jason Lin34567412020-05-14 10:32:09 +10003346 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003347
Mike Frysingerec4225d2020-04-07 05:00:01 -04003348 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003349 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003350 }
rgindacc2996c2012-02-24 14:59:31 -08003351
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003352 this.overlayTimeout_ = setTimeout(() => {
3353 this.overlayNode_.style.opacity = '0';
3354 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003355 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003356};
3357
3358/**
3359 * Hide the terminal overlay immediately.
3360 *
3361 * Useful when we show an overlay for an event with an unknown end time.
3362 */
3363hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003364 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003365 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003366 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003367 this.overlayTimeout_ = null;
3368
Mike Frysingerbdb34802020-04-07 03:47:32 -04003369 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003370 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003371 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003372 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003373};
3374
rginda4bba5e12012-06-20 16:15:30 -07003375/**
3376 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003377 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003378 * Note: In Chrome, this should work unless the user has rejected the permission
3379 * request. In Firefox extension environment, you'll need the "clipboardRead"
3380 * permission. In other environments, this might always fail as the browser
3381 * frequently blocks access for security reasons.
3382 *
3383 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3384 * value is always null. Otherwise, this function uses legacy pasting and
3385 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003386 */
3387hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003388 if (!this.alwaysUseLegacyPasting &&
3389 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003390 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3391 return null;
3392 } else {
3393 // Legacy pasting.
3394 try {
3395 return this.document_.execCommand('paste');
3396 } catch (firefoxException) {
3397 // Ignore this. FF 40 and older would incorrectly throw an exception if
3398 // there was an error instead of returning false.
3399 return false;
3400 }
3401 }
rginda4bba5e12012-06-20 16:15:30 -07003402};
3403
3404/**
3405 * Copy a string to the system clipboard.
3406 *
3407 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003408 *
3409 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003410 */
3411hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003412 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003413 if (!this.clipboardNotice_) {
3414 this.clipboardNotice_ = this.document_.createElement('div');
3415 this.clipboardNotice_.style.textAlign = 'center';
3416 const copyImage = lib.resource.getData('hterm/images/copy');
3417 this.clipboardNotice_.innerHTML =
3418 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3419 }
3420 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003421 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003422
Mike Frysinger96eacae2019-01-02 18:13:56 -05003423 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003424};
3425
Evan Jones2600d4f2016-12-06 09:29:36 -05003426/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003427 * Display an image.
3428 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003429 * Either URI or buffer or blob fields must be specified.
3430 *
Joel Hockey0f933582019-08-27 18:01:51 -07003431 * @param {{
3432 * name: (string|undefined),
3433 * size: (string|number|undefined),
3434 * preserveAspectRation: (boolean|undefined),
3435 * inline: (boolean|undefined),
3436 * width: (string|number|undefined),
3437 * height: (string|number|undefined),
3438 * align: (string|undefined),
3439 * url: (string|undefined),
3440 * buffer: (!ArrayBuffer|undefined),
3441 * blob: (!Blob|undefined),
3442 * type: (string|undefined),
3443 * }} options The image to display.
3444 * name A human readable string for the image
3445 * size The size (in bytes).
3446 * preserveAspectRatio Whether to preserve aspect.
3447 * inline Whether to display the image inline.
3448 * width The width of the image.
3449 * height The height of the image.
3450 * align Direction to align the image.
3451 * uri The source URI for the image.
3452 * buffer The ArrayBuffer image data.
3453 * blob The Blob image data.
3454 * type The MIME type of the image data.
3455 * @param {function()=} onLoad Callback when loading finishes.
3456 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003457 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003458hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003459 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003460 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003461 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003462 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003463 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003464
3465 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003466 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003467 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003468 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003469
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003470 // See if the mime type is available. If not, guess from the filename.
3471 // We don't list all possible mime types because the browser can usually
3472 // guess it correctly. So list the ones that need a bit more help.
3473 if (!options.type) {
3474 const ary = options.name.split('.');
3475 const ext = ary[ary.length - 1].trim();
3476 switch (ext) {
3477 case 'svg':
3478 case 'svgz':
3479 options.type = 'image/svg+xml';
3480 break;
3481 }
3482 }
3483
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003484 // Has the user approved image display yet?
3485 if (this.allowImagesInline !== true) {
3486 this.newLine();
3487 const row = this.getRowNode(this.scrollbackRows_.length +
3488 this.getCursorRow() - 1);
3489
3490 if (this.allowImagesInline === false) {
3491 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3492 'Inline Images Disabled');
3493 return;
3494 }
3495
3496 // Show a prompt.
3497 let button;
3498 const span = this.document_.createElement('span');
3499 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3500 span.style.fontWeight = 'bold';
3501 span.style.borderWidth = '1px';
3502 span.style.borderStyle = 'dashed';
3503 button = this.document_.createElement('span');
3504 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3505 button.style.marginLeft = '1em';
3506 button.style.borderWidth = '1px';
3507 button.style.borderStyle = 'solid';
3508 button.addEventListener('click', () => {
3509 this.prefs_.set('allow-images-inline', false);
3510 });
3511 span.appendChild(button);
3512 button = this.document_.createElement('span');
3513 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3514 'allow this session');
3515 button.style.marginLeft = '1em';
3516 button.style.borderWidth = '1px';
3517 button.style.borderStyle = 'solid';
3518 button.addEventListener('click', () => {
3519 this.allowImagesInline = true;
3520 });
3521 span.appendChild(button);
3522 button = this.document_.createElement('span');
3523 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3524 button.style.marginLeft = '1em';
3525 button.style.borderWidth = '1px';
3526 button.style.borderStyle = 'solid';
3527 button.addEventListener('click', () => {
3528 this.prefs_.set('allow-images-inline', true);
3529 });
3530 span.appendChild(button);
3531
3532 row.appendChild(span);
3533 return;
3534 }
3535
3536 // See if we should show this object directly, or download it.
3537 if (options.inline) {
3538 const io = this.io.push();
3539 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003540 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003541
3542 // While we're loading the image, eat all the user's input.
3543 io.onVTKeystroke = io.sendString = () => {};
3544
3545 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003546 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003547 if (options.uri !== undefined) {
3548 img.src = options.uri;
3549 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003550 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003551 img.src = URL.createObjectURL(blob);
3552 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003553 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003554 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003555 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003556 img.title = img.alt = options.name;
3557
3558 // Attach the image to the page to let it load/render. It won't stay here.
3559 // This is needed so it's visible and the DOM can calculate the height. If
3560 // the image is hidden or not in the DOM, the height is always 0.
3561 this.document_.body.appendChild(img);
3562
3563 // Wait for the image to finish loading before we try moving it to the
3564 // right place in the terminal.
3565 img.onload = () => {
3566 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003567 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003568 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003569 img.style.maxWidth = `${screenSize.width}px`;
3570 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003571
3572 // Parse a width/height specification.
3573 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003574 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003575 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003576 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003577
3578 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3579 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003580 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003581 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003582 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003583 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003584 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003585 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003586 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 }
3588
3589 return '';
3590 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003591 img.style.width = parseDim(
3592 options.width, screenSize.width, '--hterm-charsize-width');
3593 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003594 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003595
3596 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003597 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003598 const padRows = Math.ceil(img.clientHeight /
3599 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003600 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003601 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003602 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003603
3604 // Update the max height in case the user shrinks the character size.
3605 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3606
3607 // Move the image to the last row. This way when we scroll up, it doesn't
3608 // disappear when the first row gets clipped. It will disappear when we
3609 // scroll down and the last row is clipped ...
3610 this.document_.body.removeChild(img);
3611 // Create a wrapper node so we can do an absolute in a relative position.
3612 // This helps with rounding errors between JS & CSS counts.
3613 const div = this.document_.createElement('div');
3614 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003615 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003616 img.style.position = 'absolute';
3617 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3618 div.appendChild(img);
3619 const row = this.getRowNode(this.scrollbackRows_.length +
3620 this.getCursorRow() - 1);
3621 row.appendChild(div);
3622
Mike Frysinger2558ed52019-01-14 01:03:41 -05003623 // Now that the image has been read, we can revoke the source.
3624 if (options.uri === undefined) {
3625 URL.revokeObjectURL(img.src);
3626 }
3627
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003628 io.hideOverlay();
3629 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003630
Mike Frysingerbdb34802020-04-07 03:47:32 -04003631 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003632 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003633 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003634 };
3635
3636 // If we got a malformed image, give up.
3637 img.onerror = (e) => {
3638 this.document_.body.removeChild(img);
3639 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003640 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003641 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003642
Mike Frysingerbdb34802020-04-07 03:47:32 -04003643 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003644 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003645 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003646 };
3647 } else {
3648 // We can't use chrome.downloads.download as that requires "downloads"
3649 // permissions, and that works only in extensions, not apps.
3650 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003651 if (options.uri !== undefined) {
3652 a.href = options.uri;
3653 } else if (options.buffer !== undefined) {
3654 const blob = new Blob([options.buffer]);
3655 a.href = URL.createObjectURL(blob);
3656 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003657 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003658 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003659 a.download = options.name;
3660 this.document_.body.appendChild(a);
3661 a.click();
3662 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003663 if (options.uri === undefined) {
3664 URL.revokeObjectURL(a.href);
3665 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003666 }
3667};
3668
3669/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003670 * Returns the selected text, or null if no text is selected.
3671 *
3672 * @return {string|null}
3673 */
rgindaa09e7332012-08-17 12:49:51 -07003674hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003675 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003676 selection.sync();
3677
Mike Frysingerbdb34802020-04-07 03:47:32 -04003678 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003679 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 }
rgindaa09e7332012-08-17 12:49:51 -07003681
rgindaa09e7332012-08-17 12:49:51 -07003682 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003683 let startOffset = selection.startOffset;
3684 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003685
Raymes Khoury334625a2018-06-25 10:29:40 +10003686 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003687 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003688 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003689 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003690
Robert Gindafdbb3f22012-09-06 20:23:06 -07003691 if (node.nodeName != 'X-ROW') {
3692 // If the selection doesn't start on an x-row node, then it must be
3693 // somewhere inside the x-row. Add any characters from previous siblings
3694 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003695
3696 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3697 // If node is the text node in a styled span, move up to the span node.
3698 node = node.parentNode;
3699 }
3700
Robert Gindafdbb3f22012-09-06 20:23:06 -07003701 while (node.previousSibling) {
3702 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003703 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003704 }
rgindaa09e7332012-08-17 12:49:51 -07003705 }
3706
3707 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003708 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003709 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003710 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003711
Robert Gindafdbb3f22012-09-06 20:23:06 -07003712 if (node.nodeName != 'X-ROW') {
3713 // If the selection doesn't end on an x-row node, then it must be
3714 // somewhere inside the x-row. Add any characters from following siblings
3715 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003716
3717 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3718 // If node is the text node in a styled span, move up to the span node.
3719 node = node.parentNode;
3720 }
3721
Robert Gindafdbb3f22012-09-06 20:23:06 -07003722 while (node.nextSibling) {
3723 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003724 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003725 }
rgindaa09e7332012-08-17 12:49:51 -07003726 }
3727
Mike Frysingerdc727792020-04-10 01:41:13 -04003728 const rv = this.getRowsText(selection.startRow.rowIndex,
3729 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003730 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003731};
3732
rginda4bba5e12012-06-20 16:15:30 -07003733/**
3734 * Copy the current selection to the system clipboard, then clear it after a
3735 * short delay.
3736 */
3737hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003738 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003739 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003740 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003741 }
rginda4bba5e12012-06-20 16:15:30 -07003742};
3743
Joel Hockey0f933582019-08-27 18:01:51 -07003744/**
3745 * Show overlay with current terminal size.
3746 */
rgindaf0090c92012-02-10 14:58:52 -08003747hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003748 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003749 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003750 }
rgindaf0090c92012-02-10 14:58:52 -08003751};
3752
rginda87b86462011-12-14 13:48:03 -08003753/**
3754 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3755 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003756 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003757 */
3758hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003759 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003760 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003761 }
rginda87b86462011-12-14 13:48:03 -08003762
Mike Frysinger225c99d2019-10-20 14:02:37 -06003763 this.pauseCursorBlink_();
3764
Mike Frysinger79669762018-12-30 20:51:10 -05003765 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003766};
3767
3768/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003769 * Open the selected url.
3770 */
3771hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003772 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003773
3774 // If there is no selection, try and expand wherever they clicked.
3775 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003776 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003777 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003778
3779 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003780 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003781 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003782 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003783 }
3784
3785 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003786 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003787 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003788 }
Mike Frysinger43472622017-06-26 18:11:07 -04003789
3790 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003791 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003792 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3793 // We have to whitelist a few protocols that lack authorities and thus
3794 // never use the //. Like mailto.
3795 switch (str.split(':', 1)[0]) {
3796 case 'mailto':
3797 break;
3798 default:
3799 str = 'http://' + str;
3800 break;
3801 }
3802 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003803
Mike Frysinger720fa832017-10-23 01:15:52 -04003804 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003805};
Mike Frysinger70b94692017-01-26 18:57:50 -10003806
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003807/**
3808 * Manage the automatic mouse hiding behavior while typing.
3809 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003810 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003811 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003812hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003813 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3814 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003815 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003816 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003817 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003818
3819 this.mouseHideWhileTyping_ = !!v;
3820};
3821
3822/**
3823 * Handler for monitoring user keyboard activity.
3824 *
3825 * This isn't for processing the keystrokes directly, but for updating any
3826 * state that might toggle based on the user using the keyboard at all.
3827 *
Joel Hockey0f933582019-08-27 18:01:51 -07003828 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003829 */
3830hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3831 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003832 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003833 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003834 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003835};
Mike Frysinger70b94692017-01-26 18:57:50 -10003836
3837/**
rgindad5613292012-06-19 15:40:37 -07003838 * Add the terminalRow and terminalColumn properties to mouse events and
3839 * then forward on to onMouse().
3840 *
3841 * The terminalRow and terminalColumn properties contain the (row, column)
3842 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003843 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003844 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003845 */
3846hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003847 if (e.processedByTerminalHandler_) {
3848 // We register our event handlers on the document, as well as the cursor
3849 // and the scroll blocker. Mouse events that occur on the cursor or
3850 // scroll blocker will also appear on the document, but we don't want to
3851 // process them twice.
3852 //
3853 // We can't just prevent bubbling because that has other side effects, so
3854 // we decorate the event object with this property instead.
3855 return;
3856 }
3857
Mike Frysinger468966c2018-08-28 13:48:51 -04003858 // Consume navigation events. Button 3 is usually "browser back" and
3859 // button 4 is "browser forward" which we don't want to happen.
3860 if (e.button > 2) {
3861 e.preventDefault();
3862 // We don't return so click events can be passed to the remote below.
3863 }
3864
Mike Frysingerdc727792020-04-10 01:41:13 -04003865 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003866 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3867
rgindafaa74742012-08-21 13:34:03 -07003868 e.processedByTerminalHandler_ = true;
3869
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003870 // Handle auto hiding of mouse cursor while typing.
3871 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3872 // Make sure the mouse cursor is visible.
3873 this.syncMouseStyle();
3874 // This debounce isn't perfect, but should work well enough for such a
3875 // simple implementation. If the user moved the mouse, we enabled this
3876 // debounce, and then moved the mouse just before the timeout, we wouldn't
3877 // debounce that later movement.
3878 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3879 }
3880
Robert Gindaeda48db2014-07-17 09:25:30 -07003881 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003882 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003883 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003884 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003885 this.scrollPort_.characterSize.height) + 1;
3886 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003887 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003888
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003889 // Clamp row and column.
3890 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3891 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3892
3893 // Ignore mousedown in the scrollbar area.
3894 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003895 return;
3896 }
3897
Joel Hockey3babf302020-04-22 15:00:06 -07003898 if (this.options_.cursorVisible && !reportMouseEvents &&
3899 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003900 // If the cursor is visible and we're not sending mouse events to the
3901 // host app, then we want to hide the terminal cursor when the mouse
3902 // cursor is over top. This keeps the terminal cursor from interfering
3903 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003904 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3905 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3906 this.cursorNode_.style.display = 'none';
3907 } else if (this.cursorNode_.style.display == 'none') {
3908 this.cursorNode_.style.display = '';
3909 }
3910 }
rgindad5613292012-06-19 15:40:37 -07003911
Robert Ginda928cf632014-03-05 15:07:41 -08003912 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003913 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003914
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003915 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003916 // If VT mouse reporting is disabled, or has been defeated with
3917 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003918 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003919 this.setSelectionEnabled(true);
3920 } else {
3921 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003922 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003923 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003924 this.setSelectionEnabled(false);
3925 e.preventDefault();
3926 }
3927 }
3928
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003929 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003930 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003931 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003932 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003933 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003934 }
rgindad5613292012-06-19 15:40:37 -07003935 }
3936
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003937 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003938 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003939 // Ignore links created using OSC-8 as those will open by themselves, and
3940 // the visible text is most likely not the URI they want anyways.
3941 if (e.target.className === 'uri-node') {
3942 return;
3943 }
3944
Mike Frysinger70b94692017-01-26 18:57:50 -10003945 // Debounce this event with the dblclick event. If you try to doubleclick
3946 // a URL to open it, Chrome will fire click then dblclick, but we won't
3947 // have expanded the selection text at the first click event.
3948 clearTimeout(this.timeouts_.openUrl);
3949 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3950 500);
3951 return;
3952 }
3953
Mike Frysinger847577f2017-05-23 23:25:57 -04003954 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003955 if (e.ctrlKey && e.button == 2 /* right button */) {
3956 e.preventDefault();
3957 this.contextMenu.show(e, this);
3958 } else if (e.button == this.mousePasteButton ||
3959 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003960 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003961 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003962 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003963 }
3964 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003965
Mike Frysinger2edd3612017-05-24 00:54:39 -04003966 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003967 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003968 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003969 }
3970
3971 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3972 this.scrollBlockerNode_.engaged) {
3973 // Disengage the scroll-blocker after one of these events.
3974 this.scrollBlockerNode_.engaged = false;
3975 this.scrollBlockerNode_.style.top = '-99px';
3976 }
3977
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003978 // Emulate arrow key presses via scroll wheel events.
3979 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3980 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003981 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003982 const delta =
3983 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003984
Mike Frysinger321063c2018-08-29 15:33:14 -04003985 // Helper to turn a wheel event delta into a series of key presses.
3986 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3987 if (distance == 0) {
3988 return '';
3989 }
3990
3991 // Convert the scroll distance into a number of rows/cols.
3992 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3993 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3994 return data.repeat(cells);
3995 };
3996
3997 // The order between up/down and left/right doesn't really matter.
3998 this.io.sendString(
3999 // Up/down arrow keys.
4000 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4001 'A', 'B') +
4002 // Left/right arrow keys.
4003 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004004 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004005 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004006
4007 e.preventDefault();
4008 }
4009 }
Robert Ginda928cf632014-03-05 15:07:41 -08004010 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004011 if (!this.scrollBlockerNode_.engaged) {
4012 if (e.type == 'mousedown') {
4013 // Move the scroll-blocker into place if we want to keep the scrollport
4014 // from scrolling.
4015 this.scrollBlockerNode_.engaged = true;
4016 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4017 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4018 } else if (e.type == 'mousemove') {
4019 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4020 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004021 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004022 e.preventDefault();
4023 }
4024 }
Robert Ginda928cf632014-03-05 15:07:41 -08004025
4026 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004027 }
4028
Robert Ginda928cf632014-03-05 15:07:41 -08004029 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4030 // Restore this on mouseup in case it was temporarily defeated with a
4031 // alt-mousedown. Only do this when the selection is empty so that
4032 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004033 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004034 }
rgindad5613292012-06-19 15:40:37 -07004035};
4036
4037/**
4038 * Clients should override this if they care to know about mouse events.
4039 *
4040 * The event parameter will be a normal DOM mouse click event with additional
4041 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004042 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004043 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004044 */
4045hterm.Terminal.prototype.onMouse = function(e) { };
4046
4047/**
rginda8e92a692012-05-20 19:37:20 -07004048 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004049 *
4050 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004051 */
Rob Spies06533ba2014-04-24 11:20:37 -07004052hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4053 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004054 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004055
Mike Frysingerbdb34802020-04-07 03:47:32 -04004056 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004057 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004058 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004059
Mike Frysingerbdb34802020-04-07 03:47:32 -04004060 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004061 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004062 }
rginda8e92a692012-05-20 19:37:20 -07004063};
4064
4065/**
rginda8ba33642011-12-14 12:31:31 -08004066 * React when the ScrollPort is scrolled.
4067 */
4068hterm.Terminal.prototype.onScroll_ = function() {
4069 this.scheduleSyncCursorPosition_();
4070};
4071
4072/**
rginda9846e2f2012-01-27 13:53:33 -08004073 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004074 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004075 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004076 */
4077hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004078 this.onPasteData_(e.text);
4079};
4080
4081/**
4082 * Handle pasted data.
4083 *
4084 * @param {string} data The pasted data.
4085 */
4086hterm.Terminal.prototype.onPasteData_ = function(data) {
4087 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004088 if (this.options_.bracketedPaste) {
4089 // We strip out most escape sequences as they can cause issues (like
4090 // inserting an \x1b[201~ midstream). We pass through whitespace
4091 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4092 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004093 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004094 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4095 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4096 }
Robert Gindaa063b202014-07-21 11:08:25 -07004097
4098 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004099};
4100
4101/**
rgindaa09e7332012-08-17 12:49:51 -07004102 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004103 *
Joel Hockey0f933582019-08-27 18:01:51 -07004104 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004105 */
4106hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004107 if (!this.useDefaultWindowCopy) {
4108 e.preventDefault();
4109 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4110 }
rgindaa09e7332012-08-17 12:49:51 -07004111};
4112
4113/**
rginda8ba33642011-12-14 12:31:31 -08004114 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004115 *
4116 * Note: This function should not directly contain code that alters the internal
4117 * state of the terminal. That kind of code belongs in realizeWidth or
4118 * realizeHeight, so that it can be executed synchronously in the case of a
4119 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004120 */
4121hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004122 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4123 this.scrollPort_.characterSize.width) || 0;
4124 const rowCount = lib.f.smartFloorDivide(
4125 this.scrollPort_.getScreenHeight(),
4126 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004127
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004128 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004129 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004130 // gets removed from the document or during the initial load, and we can't
4131 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004132 // This can also happen if called before the scrollPort calculates the
4133 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004134 return;
4135 }
4136
Mike Frysingerdc727792020-04-10 01:41:13 -04004137 const isNewSize = (columnCount != this.screenSize.width ||
4138 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004139 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004140
4141 // We do this even if the size didn't change, just to be sure everything is
4142 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004143 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004144 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004145
Mike Frysingerbdb34802020-04-07 03:47:32 -04004146 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004147 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004148 }
rgindaa8ba17d2012-08-15 14:41:10 -07004149
Robert Gindafb1be6a2013-12-11 11:56:22 -08004150 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004151 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004152
4153 if (wasScrolledEnd) {
4154 this.scrollEnd();
4155 }
rginda8ba33642011-12-14 12:31:31 -08004156};
4157
4158/**
4159 * Service the cursor blink timeout.
4160 */
4161hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004162 if (!this.options_.cursorBlink) {
4163 delete this.timeouts_.cursorBlink;
4164 return;
4165 }
4166
Robert Ginda830583c2013-08-07 13:20:46 -07004167 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004168 this.cursorNode_.style.opacity == '0' ||
4169 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004170 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004171 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4172 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004173 } else {
rginda87b86462011-12-14 13:48:03 -08004174 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004175 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4176 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004177 }
4178};
David Reveman8f552492012-03-28 12:18:41 -04004179
4180/**
4181 * Set the scrollbar-visible mode bit.
4182 *
4183 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4184 * Otherwise it will not.
4185 *
4186 * Defaults to on.
4187 *
4188 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4189 */
4190hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4191 this.scrollPort_.setScrollbarVisible(state);
4192};
Michael Kelly485ecd12014-06-09 11:41:56 -04004193
4194/**
Rob Spies49039e52014-12-17 13:40:04 -08004195 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004196 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004197 *
4198 * Defaults to 1.
4199 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004200 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004201 */
4202hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4203 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4204};
4205
4206/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004207 * Close all web notifications created by terminal bells.
4208 */
4209hterm.Terminal.prototype.closeBellNotifications_ = function() {
4210 this.bellNotificationList_.forEach(function(n) {
4211 n.close();
4212 });
4213 this.bellNotificationList_.length = 0;
4214};
Raymes Khourye5d48982018-08-02 09:08:32 +10004215
4216/**
4217 * Syncs the cursor position when the scrollport gains focus.
4218 */
4219hterm.Terminal.prototype.onScrollportFocus_ = function() {
4220 // If the cursor is offscreen we set selection to the last row on the screen.
4221 const topRowIndex = this.scrollPort_.getTopRowIndex();
4222 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4223 const selection = this.document_.getSelection();
4224 if (!this.syncCursorPosition_() && selection) {
4225 selection.collapse(this.getRowNode(bottomRowIndex));
4226 }
4227};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004228
4229/**
4230 * Clients can override this if they want to provide an options page.
4231 */
4232hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4233
4234
4235/**
4236 * Called when user selects to open the options page.
4237 */
4238hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4239 this.onOpenOptionsPage();
4240};