blob: 548e6f66d56963dc6d75d7e0faee33ec303e3f9e [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700109 this.screenBorderSize_ = 0;
110
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.scrollOnOutput_ = null;
112 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400113 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800114
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700115 // True if we should override mouse event reporting to allow local selection.
116 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800117
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400118 // Whether to auto hide the mouse cursor when typing.
119 this.setAutomaticMouseHiding();
120 // Timer to keep mouse visible while it's being used.
121 this.mouseHideDelay_ = null;
122
rgindaf0090c92012-02-10 14:58:52 -0800123 // Terminal bell sound.
124 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400125 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800126 this.bellAudio_.setAttribute('preload', 'auto');
127
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000128 // The AccessibilityReader object for announcing command output.
129 this.accessibilityReader_ = null;
130
Mike Frysingercc114512017-09-11 21:39:17 -0400131 // The context menu object.
132 this.contextMenu = new hterm.ContextMenu();
133
Michael Kelly485ecd12014-06-09 11:41:56 -0400134 // All terminal bell notifications that have been generated (not necessarily
135 // shown).
136 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700137 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
139 // Whether we have permission to display notifications.
140 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
rginda6d397402012-01-17 10:58:29 -0800142 // Cursor position and attributes saved with DECSC.
143 this.savedOptions_ = {};
144
rginda8ba33642011-12-14 12:31:31 -0800145 // The current mode bits for the terminal.
146 this.options_ = new hterm.Options();
147
148 // Timeouts we might need to clear.
149 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800150
151 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800152 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800153
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800154 this.saveCursorAndState(true);
155
Zhu Qunying30d40712017-03-14 16:27:00 -0700156 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800157 this.keyboard = new hterm.Keyboard(this);
158
rginda87b86462011-12-14 13:48:03 -0800159 // General IO interface that can be given to third parties without exposing
160 // the entire terminal object.
161 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800162
rgindad5613292012-06-19 15:40:37 -0700163 // True if mouse-click-drag should scroll the terminal.
164 this.enableMouseDragScroll = true;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400167 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700168 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700169
Zhu Qunying30d40712017-03-14 16:27:00 -0700170 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700171 this.useDefaultWindowCopy = false;
172
173 this.clearSelectionAfterCopy = true;
174
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400175 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800176 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700177
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400178 // Whether we allow images to be shown.
179 this.allowImagesInline = null;
180
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400181 this.reportFocus = false;
182
Jason Linf129f3c2020-03-23 11:52:08 +1100183 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
184 this.alwaysUseLegacyPasting = false;
185
Joel Hockey3a44a442019-10-14 16:22:56 -0700186 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500187 function() { this.onTerminalReady(); }.bind(this));
shivanggargde5387e2020-06-10 01:31:16 +0530188
189 /** @const */
190 this.findBar = new hterm.FindBar(this);
rginda87b86462011-12-14 13:48:03 -0800191};
192
193/**
Robert Ginda830583c2013-08-07 13:20:46 -0700194 * Possible cursor shapes.
195 */
196hterm.Terminal.cursorShape = {
197 BLOCK: 'BLOCK',
198 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400199 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700200};
201
202/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700203 * Clients should override this to be notified when the terminal is ready
204 * for use.
205 *
206 * The terminal initialization is asynchronous, and shouldn't be used before
207 * this method is called.
208 */
209hterm.Terminal.prototype.onTerminalReady = function() { };
210
211/**
rginda35c456b2012-02-09 17:29:05 -0800212 * Default tab with of 8 to match xterm.
213 */
214hterm.Terminal.prototype.tabWidth = 8;
215
216/**
rginda9f5222b2012-03-05 11:53:28 -0800217 * Select a preference profile.
218 *
219 * This will load the terminal preferences for the given profile name and
220 * associate subsequent preference changes with the new preference profile.
221 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500222 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800223 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400224 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700225 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800226 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400227hterm.Terminal.prototype.setProfile = function(
228 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800230
Mike Frysingerdc727792020-04-10 01:41:13 -0400231 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800232
Mike Frysingerbdb34802020-04-07 03:47:32 -0400233 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400235 }
rginda9f5222b2012-03-05 11:53:28 -0800236
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700238
239 /**
240 * Clears and reloads key bindings. Used by preferences
241 * 'keybindings' and 'keybindings-os-defaults'.
242 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 * @param {*?=} bindings
244 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700245 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700247 terminal.keyboard.bindings.clear();
248
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400249 // Default to an empty object so we still handle OS defaults.
250 if (bindings === null) {
251 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700252 }
253
254 if (!(bindings instanceof Object)) {
255 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400256 bindings = {};
257 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700258 }
259
260 try {
261 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
262 } catch (ex) {
263 console.error('Error in keybindings preference: ' + ex);
264 }
265 }
266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800268 'alt-gr-mode': function(v) {
269 if (v == null) {
270 if (navigator.language.toLowerCase() == 'en-us') {
271 v = 'none';
272 } else {
273 v = 'right-alt';
274 }
275 } else if (typeof v == 'string') {
276 v = v.toLowerCase();
277 } else {
278 v = 'none';
279 }
280
Mike Frysingerbdb34802020-04-07 03:47:32 -0400281 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800282 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400283 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800284
285 terminal.keyboard.altGrMode = v;
286 },
287
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700288 'alt-backspace-is-meta-backspace': function(v) {
289 terminal.keyboard.altBackspaceIsMetaBackspace = v;
290 },
291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'alt-is-meta': function(v) {
293 terminal.keyboard.altIsMeta = v;
294 },
295
296 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400297 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700298 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400299 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700300
301 terminal.keyboard.altSendsWhat = v;
302 },
303
304 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400305 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800306 if (ary) {
307 terminal.bellAudio_.setAttribute('src',
308 lib.resource.getDataUrl(ary[1]));
309 } else {
310 terminal.bellAudio_.setAttribute('src', v);
311 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 },
313
Michael Kelly485ecd12014-06-09 11:41:56 -0400314 'desktop-notification-bell': function(v) {
315 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700316 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400317 Notification.permission === 'granted';
318 if (!terminal.desktopNotificationBell_) {
319 // Note: We don't call Notification.requestPermission here because
320 // Chrome requires the call be the result of a user action (such as an
321 // onclick handler), and pref listeners are run asynchronously.
322 //
323 // A way of working around this would be to display a dialog in the
324 // terminal with a "click-to-request-permission" button.
325 console.warn('desktop-notification-bell is true but we do not have ' +
326 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400327 }
328 } else {
329 terminal.desktopNotificationBell_ = false;
330 }
331 },
332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'background-color': function(v) {
334 terminal.setBackgroundColor(v);
335 },
336
337 'background-image': function(v) {
338 terminal.scrollPort_.setBackgroundImage(v);
339 },
340
341 'background-size': function(v) {
342 terminal.scrollPort_.setBackgroundSize(v);
343 },
344
345 'background-position': function(v) {
346 terminal.scrollPort_.setBackgroundPosition(v);
347 },
348
349 'backspace-sends-backspace': function(v) {
350 terminal.keyboard.backspaceSendsBackspace = v;
351 },
352
Brad Town18654b62015-03-12 00:27:45 -0700353 'character-map-overrides': function(v) {
354 if (!(v == null || v instanceof Object)) {
355 console.warn('Preference character-map-modifications is not an ' +
356 'object: ' + v);
357 return;
358 }
359
Mike Frysinger095d4062017-06-14 00:29:48 -0700360 terminal.vt.characterMaps.reset();
361 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700362 },
363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'cursor-blink': function(v) {
365 terminal.setCursorBlink(!!v);
366 },
367
Joel Hockey9d10ba12019-05-28 01:25:02 -0700368 'cursor-shape': function(v) {
369 terminal.setCursorShape(v);
370 },
371
Robert Gindaea2183e2014-07-17 09:51:51 -0700372 'cursor-blink-cycle': function(v) {
373 if (v instanceof Array &&
374 typeof v[0] == 'number' &&
375 typeof v[1] == 'number') {
376 terminal.cursorBlinkCycle_ = v;
377 } else if (typeof v == 'number') {
378 terminal.cursorBlinkCycle_ = [v, v];
379 } else {
380 // Fast blink indicates an error.
381 terminal.cursorBlinkCycle_ = [100, 100];
382 }
383 },
384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'cursor-color': function(v) {
386 terminal.setCursorColor(v);
387 },
388
389 'color-palette-overrides': function(v) {
390 if (!(v == null || v instanceof Object || v instanceof Array)) {
391 console.warn('Preference color-palette-overrides is not an array or ' +
392 'object: ' + v);
393 return;
rginda9f5222b2012-03-05 11:53:28 -0800394 }
rginda9f5222b2012-03-05 11:53:28 -0800395
Joel Hockey42dba8f2020-03-26 16:21:11 -0700396 // Call terminal.setColorPalette here and below with the new default
397 // value before changing it in lib.colors.colorPalette to ensure that
398 // CSS vars are updated.
399 lib.colors.stockColorPalette.forEach(
400 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400404 for (const key in v) {
405 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700406 if (isNaN(i) || i < 0 || i > 255) {
407 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
408 continue;
409 }
410
411 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400412 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700413 if (rgb) {
414 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700416 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 }
418 }
rginda30f20f62012-04-05 16:36:19 -0700419 }
rginda30f20f62012-04-05 16:36:19 -0700420
Joel Hockey42dba8f2020-03-26 16:21:11 -0700421 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
422 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'copy-on-select': function(v) {
426 terminal.copyOnSelect = !!v;
427 },
rginda9f5222b2012-03-05 11:53:28 -0800428
Rob Spies0bec09b2014-06-06 15:58:09 -0700429 'use-default-window-copy': function(v) {
430 terminal.useDefaultWindowCopy = !!v;
431 },
432
433 'clear-selection-after-copy': function(v) {
434 terminal.clearSelectionAfterCopy = !!v;
435 },
436
Robert Ginda7e5e9522014-03-14 12:23:58 -0700437 'ctrl-plus-minus-zero-zoom': function(v) {
438 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
439 },
440
Robert Gindafb5a3f92014-05-13 14:12:00 -0700441 'ctrl-c-copy': function(v) {
442 terminal.keyboard.ctrlCCopy = v;
443 },
444
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100445 'ctrl-v-paste': function(v) {
446 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700447 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100448 },
449
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700450 'paste-on-drop': function(v) {
451 terminal.scrollPort_.setPasteOnDrop(v);
452 },
453
Masaya Suzuki273aa982014-05-31 07:25:55 +0900454 'east-asian-ambiguous-as-two-column': function(v) {
455 lib.wc.regardCjkAmbiguous = v;
456 },
457
Robert Ginda57f03b42012-09-13 11:02:48 -0700458 'enable-8-bit-control': function(v) {
459 terminal.vt.enable8BitControl = !!v;
460 },
rginda30f20f62012-04-05 16:36:19 -0700461
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 'enable-bold': function(v) {
463 terminal.syncBoldSafeState();
464 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400465
Robert Ginda3e278d72014-03-25 13:18:51 -0700466 'enable-bold-as-bright': function(v) {
467 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
468 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
469 },
470
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400471 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500472 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400473 },
474
Robert Ginda57f03b42012-09-13 11:02:48 -0700475 'enable-clipboard-write': function(v) {
476 terminal.vt.enableClipboardWrite = !!v;
477 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400478
Robert Ginda3755e752013-05-31 13:34:09 -0700479 'enable-dec12': function(v) {
480 terminal.vt.enableDec12 = !!v;
481 },
482
Mike Frysinger38f267d2018-09-07 02:50:59 -0400483 'enable-csi-j-3': function(v) {
484 terminal.vt.enableCsiJ3 = !!v;
485 },
486
shivanggarg2b7b0d52020-07-10 11:01:34 +0530487 'find-result-color': function(v) {
488 terminal.findBar.setFindResultColor(v);
489 },
490
Robert Ginda57f03b42012-09-13 11:02:48 -0700491 'font-family': function(v) {
492 terminal.syncFontFamily();
493 },
rginda30f20f62012-04-05 16:36:19 -0700494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700496 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700497 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500498 console.error(`Invalid font size: ${v}`);
499 return;
500 }
501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 terminal.setFontSize(v);
503 },
rginda9875d902012-08-20 16:21:57 -0700504
Robert Ginda57f03b42012-09-13 11:02:48 -0700505 'font-smoothing': function(v) {
506 terminal.syncFontFamily();
507 },
rgindade84e382012-04-20 15:39:31 -0700508
Robert Ginda57f03b42012-09-13 11:02:48 -0700509 'foreground-color': function(v) {
510 terminal.setForegroundColor(v);
511 },
rginda30f20f62012-04-05 16:36:19 -0700512
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400513 'hide-mouse-while-typing': function(v) {
514 terminal.setAutomaticMouseHiding(v);
515 },
516
Robert Ginda57f03b42012-09-13 11:02:48 -0700517 'home-keys-scroll': function(v) {
518 terminal.keyboard.homeKeysScroll = v;
519 },
rginda4bba5e12012-06-20 16:15:30 -0700520
Robert Gindaa8165692015-06-15 14:46:31 -0700521 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700522 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
523 },
Robert Gindaa8165692015-06-15 14:46:31 -0700524
Joel Hockey95a9e272020-03-16 21:19:53 -0700525 'keybindings-os-defaults': function(v) {
526 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700527 },
528
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700529 'media-keys-are-fkeys': function(v) {
530 terminal.keyboard.mediaKeysAreFKeys = v;
531 },
532
Robert Ginda57f03b42012-09-13 11:02:48 -0700533 'meta-sends-escape': function(v) {
534 terminal.keyboard.metaSendsEscape = v;
535 },
rginda30f20f62012-04-05 16:36:19 -0700536
Mike Frysinger847577f2017-05-23 23:25:57 -0400537 'mouse-right-click-paste': function(v) {
538 terminal.mouseRightClickPaste = v;
539 },
540
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 'mouse-paste-button': function(v) {
542 terminal.syncMousePasteButton();
543 },
rgindaa8ba17d2012-08-15 14:41:10 -0700544
Robert Gindae76aa9f2014-03-14 12:29:12 -0700545 'page-keys-scroll': function(v) {
546 terminal.keyboard.pageKeysScroll = v;
547 },
548
Robert Ginda40932892012-12-10 17:26:40 -0800549 'pass-alt-number': function(v) {
550 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700551 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800552 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500553 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800554 }
555
556 terminal.passAltNumber = v;
557 },
558
559 'pass-ctrl-number': function(v) {
560 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700561 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800562 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500563 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800564 }
565
566 terminal.passCtrlNumber = v;
567 },
568
Joel Hockey0e052042020-02-19 05:37:19 -0800569 'pass-ctrl-n': function(v) {
570 terminal.passCtrlN = v;
571 },
572
573 'pass-ctrl-t': function(v) {
574 terminal.passCtrlT = v;
575 },
576
577 'pass-ctrl-tab': function(v) {
578 terminal.passCtrlTab = v;
579 },
580
581 'pass-ctrl-w': function(v) {
582 terminal.passCtrlW = v;
583 },
584
Robert Ginda40932892012-12-10 17:26:40 -0800585 'pass-meta-number': function(v) {
586 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700587 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800588 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500589 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800590 }
591
592 terminal.passMetaNumber = v;
593 },
594
Marius Schilder77857b32014-05-14 16:21:26 -0700595 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700596 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700597 },
598
Robert Ginda8cb7d902013-06-20 14:37:18 -0700599 'receive-encoding': function(v) {
600 if (!(/^(utf-8|raw)$/).test(v)) {
601 console.warn('Invalid value for "receive-encoding": ' + v);
602 v = 'utf-8';
603 }
604
605 terminal.vt.characterEncoding = v;
606 },
607
Joel Hockey139d82d2020-04-07 23:04:29 -0700608 'screen-padding-size': function(v) {
609 v = parseInt(v, 10);
610 if (isNaN(v) || v < 0) {
611 console.error(`Invalid screen padding size: ${v}`);
612 return;
613 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700614 terminal.setScreenPaddingSize(v);
615 },
616
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700617 'screen-border-size': function(v) {
618 v = parseInt(v, 10);
619 if (isNaN(v) || v < 0) {
620 console.error(`Invalid screen border size: ${v}`);
621 return;
622 }
623 terminal.setScreenBorderSize(v);
624 },
625
626 'screen-border-color': function(v) {
627 terminal.div_.style.borderColor = v;
628 },
629
Robert Ginda57f03b42012-09-13 11:02:48 -0700630 'scroll-on-keystroke': function(v) {
631 terminal.scrollOnKeystroke_ = v;
632 },
rginda9f5222b2012-03-05 11:53:28 -0800633
Robert Ginda57f03b42012-09-13 11:02:48 -0700634 'scroll-on-output': function(v) {
635 terminal.scrollOnOutput_ = v;
636 },
rginda30f20f62012-04-05 16:36:19 -0700637
Robert Ginda57f03b42012-09-13 11:02:48 -0700638 'scrollbar-visible': function(v) {
639 terminal.setScrollbarVisible(v);
640 },
rginda9f5222b2012-03-05 11:53:28 -0800641
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400642 'scroll-wheel-may-send-arrow-keys': function(v) {
643 terminal.scrollWheelArrowKeys_ = v;
644 },
645
Rob Spies49039e52014-12-17 13:40:04 -0800646 'scroll-wheel-move-multiplier': function(v) {
647 terminal.setScrollWheelMoveMultipler(v);
648 },
649
Robert Ginda57f03b42012-09-13 11:02:48 -0700650 'shift-insert-paste': function(v) {
651 terminal.keyboard.shiftInsertPaste = v;
652 },
rginda9f5222b2012-03-05 11:53:28 -0800653
Mike Frysingera7768922017-07-28 15:00:12 -0400654 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400655 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400656 },
657
Robert Gindae76aa9f2014-03-14 12:29:12 -0700658 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400659 terminal.scrollPort_.setUserCssUrl(v);
660 },
661
662 'user-css-text': function(v) {
663 terminal.scrollPort_.setUserCssText(v);
664 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400665
666 'word-break-match-left': function(v) {
667 terminal.primaryScreen_.wordBreakMatchLeft = v;
668 terminal.alternateScreen_.wordBreakMatchLeft = v;
669 },
670
671 'word-break-match-right': function(v) {
672 terminal.primaryScreen_.wordBreakMatchRight = v;
673 terminal.alternateScreen_.wordBreakMatchRight = v;
674 },
675
676 'word-break-match-middle': function(v) {
677 terminal.primaryScreen_.wordBreakMatchMiddle = v;
678 terminal.alternateScreen_.wordBreakMatchMiddle = v;
679 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400680
681 'allow-images-inline': function(v) {
682 terminal.allowImagesInline = v;
683 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700684 });
rginda30f20f62012-04-05 16:36:19 -0700685
Robert Ginda57f03b42012-09-13 11:02:48 -0700686 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800687 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700688
Mike Frysingerec4225d2020-04-07 05:00:01 -0400689 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700690 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400691 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400692 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700693 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800694};
695
Rob Spies56953412014-04-28 14:09:47 -0700696/**
697 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500698 *
Joel Hockey0f933582019-08-27 18:01:51 -0700699 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700700 */
701hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700702 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700703};
704
Robert Gindaa063b202014-07-21 11:08:25 -0700705/**
706 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500707 *
708 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700709 */
710hterm.Terminal.prototype.setBracketedPaste = function(state) {
711 this.options_.bracketedPaste = state;
712};
Rob Spies56953412014-04-28 14:09:47 -0700713
rginda8e92a692012-05-20 19:37:20 -0700714/**
715 * Set the color for the cursor.
716 *
717 * If you want this setting to persist, set it through prefs_, rather than
718 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500719 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500720 * @param {string=} color The color to set. If not defined, we reset to the
721 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700722 */
723hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400724 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700725 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400726 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500727
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400728 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700729};
730
731/**
732 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400733 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500734 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700735 */
736hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400737 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700738};
739
740/**
rgindad5613292012-06-19 15:40:37 -0700741 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500742 *
743 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700744 */
745hterm.Terminal.prototype.setSelectionEnabled = function(state) {
746 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700747};
748
749/**
rginda8e92a692012-05-20 19:37:20 -0700750 * Set the background color.
751 *
752 * If you want this setting to persist, set it through prefs_, rather than
753 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500754 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500755 * @param {string=} color The color to set. If not defined, we reset to the
756 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700757 */
758hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400759 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700760 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400761 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500762
Joel Hockey42dba8f2020-03-26 16:21:11 -0700763 this.backgroundColor_ = lib.colors.normalizeCSS(color);
764 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700765};
766
rginda9f5222b2012-03-05 11:53:28 -0800767/**
768 * Return the current terminal background color.
769 *
770 * Intended for use by other classes, so we don't have to expose the entire
771 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500772 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700773 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800774 */
775hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700776 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700777};
778
779/**
780 * Set the foreground color.
781 *
782 * If you want this setting to persist, set it through prefs_, rather than
783 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500784 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500785 * @param {string=} color The color to set. If not defined, we reset to the
786 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700787 */
788hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400789 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700790 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400791 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500792
Joel Hockey42dba8f2020-03-26 16:21:11 -0700793 this.foregroundColor_ = lib.colors.normalizeCSS(color);
794 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800795};
796
797/**
798 * Return the current terminal foreground color.
799 *
800 * Intended for use by other classes, so we don't have to expose the entire
801 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500802 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700803 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800804 */
805hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700806 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800807};
808
809/**
rginda87b86462011-12-14 13:48:03 -0800810 * Create a new instance of a terminal command and run it with a given
811 * argument string.
812 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700813 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700814 * @param {string} commandName The command to run for this terminal.
815 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800816 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700817hterm.Terminal.prototype.runCommandClass = function(
818 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400819 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400820 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700821 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400822 }
rgindaf522ce02012-04-17 17:49:17 -0700823
rginda87b86462011-12-14 13:48:03 -0800824 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700825 {
826 commandName: commandName,
827 args: args,
rginda87b86462011-12-14 13:48:03 -0800828 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700829 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400830 onExit: (code) => {
831 this.io.pop();
832 this.uninstallKeyboard();
833 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
834 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400835 window.close();
836 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400837 },
rginda87b86462011-12-14 13:48:03 -0800838 });
839
rgindafeaf3142012-01-31 15:14:20 -0800840 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800841 this.command.run();
842};
843
844/**
rgindafeaf3142012-01-31 15:14:20 -0800845 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500846 *
847 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800848 */
849hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700850 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800851};
852
853/**
854 * Install the keyboard handler for this terminal.
855 *
856 * This will prevent the browser from seeing any keystrokes sent to the
857 * terminal.
858 */
859hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700860 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400861};
rgindafeaf3142012-01-31 15:14:20 -0800862
863/**
864 * Uninstall the keyboard handler for this terminal.
865 */
866hterm.Terminal.prototype.uninstallKeyboard = function() {
867 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400868};
rgindafeaf3142012-01-31 15:14:20 -0800869
870/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400871 * Set a CSS variable.
872 *
873 * Normally this is used to set variables in the hterm namespace.
874 *
875 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700876 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400877 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400878 */
879hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400880 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400881 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400882 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400883};
884
885/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700886 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
887 * input is valid.
888 *
889 * @param {string} name The variable to set.
890 * @param {?string} rgb The rgb value to assign to the variable.
891 */
892hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
893 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
894 if (ary) {
895 this.setCssVar(name, ary.slice(0, 3).join(','));
896 }
897};
898
899/**
900 * Sets the specified color for the active screen.
901 *
902 * @param {number} i The index into the 256 color palette to set.
903 * @param {?string} rgb The rgb value to assign to the variable.
904 */
905hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
906 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
907 this.setRgbColorCssVar(`color-${i}`, rgb);
908 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
909 }
910};
911
912/**
913 * Returns the current value in the active screen of the specified color.
914 *
915 * @param {number} i Color palette index.
916 * @return {string} rgb color.
917 */
918hterm.Terminal.prototype.getColorPalette = function(i) {
919 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
920 lib.colors.colorPalette[i];
921};
922
923/**
924 * Reset the specified color in the active screen to its default value.
925 *
926 * @param {number} i Color to reset
927 */
928hterm.Terminal.prototype.resetColor = function(i) {
929 this.setColorPalette(i, lib.colors.colorPalette[i]);
930 delete this.screen_.textAttributes.colorPaletteOverrides[i];
931};
932
933/**
934 * Reset the current screen color palette to the default state.
935 */
936hterm.Terminal.prototype.resetColorPalette = function() {
937 this.screen_.textAttributes.colorPaletteOverrides.forEach(
938 (c, i) => this.resetColor(i));
939};
940
941/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500942 * Get a CSS variable.
943 *
944 * Normally this is used to get variables in the hterm namespace.
945 *
946 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400947 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500948 * @return {string} The current setting for this variable.
949 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400950hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500951 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400952 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500953};
954
955/**
Jason Linbbbdb752020-03-06 16:26:59 +1100956 * Update CSS character size variables to match the scrollport.
957 */
958hterm.Terminal.prototype.updateCssCharsize_ = function() {
959 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
960 this.setCssVar('charsize-height',
961 this.scrollPort_.characterSize.height + 'px');
962};
963
964/**
rginda35c456b2012-02-09 17:29:05 -0800965 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800966 *
967 * Call setFontSize(0) to reset to the default font size.
968 *
969 * This function does not modify the font-size preference.
970 *
971 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800972 */
973hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400974 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700975 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400976 }
rginda9f5222b2012-03-05 11:53:28 -0800977
rginda35c456b2012-02-09 17:29:05 -0800978 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -0700979 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +1100980 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800981};
982
983/**
984 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500985 *
986 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800987 */
988hterm.Terminal.prototype.getFontSize = function() {
989 return this.scrollPort_.getFontSize();
990};
991
992/**
rginda8e92a692012-05-20 19:37:20 -0700993 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500994 *
995 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700996 */
997hterm.Terminal.prototype.getFontFamily = function() {
998 return this.scrollPort_.getFontFamily();
999};
1000
1001/**
rginda35c456b2012-02-09 17:29:05 -08001002 * Set the CSS "font-family" for this terminal.
1003 */
rginda9f5222b2012-03-05 11:53:28 -08001004hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001005 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1006 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001007 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001008 this.syncBoldSafeState();
1009};
1010
rginda4bba5e12012-06-20 16:15:30 -07001011/**
1012 * Set this.mousePasteButton based on the mouse-paste-button pref,
1013 * autodetecting if necessary.
1014 */
1015hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001016 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001017 if (typeof button == 'number') {
1018 this.mousePasteButton = button;
1019 return;
1020 }
1021
Mike Frysingeree81a002017-12-12 16:14:53 -05001022 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001023 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001024 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001025 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001026 }
1027};
1028
1029/**
1030 * Enable or disable bold based on the enable-bold pref, autodetecting if
1031 * necessary.
1032 */
rginda9f5222b2012-03-05 11:53:28 -08001033hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001034 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001035 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001036 this.primaryScreen_.textAttributes.enableBold = enableBold;
1037 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001038 return;
1039 }
1040
Mike Frysingerdc727792020-04-10 01:41:13 -04001041 const normalSize = this.scrollPort_.measureCharacterSize();
1042 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001043
Mike Frysingerdc727792020-04-10 01:41:13 -04001044 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001045 if (!isBoldSafe) {
1046 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001047 'from normal. Font family is: ' +
1048 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001049 }
rginda9f5222b2012-03-05 11:53:28 -08001050
Robert Gindaed016262012-10-26 16:27:09 -07001051 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1052 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001053};
1054
1055/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001056 * Control text blinking behavior.
1057 *
1058 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001059 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001060hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001061 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001062 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001063 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001064 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001065};
1066
1067/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001068 * Set the mouse cursor style based on the current terminal mode.
1069 */
1070hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001071 this.setCssVar('mouse-cursor-style',
1072 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1073 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001074 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001075};
1076
1077/**
rginda87b86462011-12-14 13:48:03 -08001078 * Return a copy of the current cursor position.
1079 *
Joel Hockey0f933582019-08-27 18:01:51 -07001080 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001081 */
1082hterm.Terminal.prototype.saveCursor = function() {
1083 return this.screen_.cursorPosition.clone();
1084};
1085
Evan Jones2600d4f2016-12-06 09:29:36 -05001086/**
1087 * Return the current text attributes.
1088 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001089 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001090 */
rgindaa19afe22012-01-25 15:40:22 -08001091hterm.Terminal.prototype.getTextAttributes = function() {
1092 return this.screen_.textAttributes;
1093};
1094
Evan Jones2600d4f2016-12-06 09:29:36 -05001095/**
1096 * Set the text attributes.
1097 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001098 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001099 */
rginda1a09aa02012-06-18 21:11:25 -07001100hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1101 this.screen_.textAttributes = textAttributes;
1102};
1103
rginda87b86462011-12-14 13:48:03 -08001104/**
rgindaf522ce02012-04-17 17:49:17 -07001105 * Return the current browser zoom factor applied to the terminal.
1106 *
1107 * @return {number} The current browser zoom factor.
1108 */
1109hterm.Terminal.prototype.getZoomFactor = function() {
1110 return this.scrollPort_.characterSize.zoomFactor;
1111};
1112
1113/**
rginda9846e2f2012-01-27 13:53:33 -08001114 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001115 *
1116 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001117 */
1118hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001119 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001120};
1121
1122/**
rginda87b86462011-12-14 13:48:03 -08001123 * Restore a previously saved cursor position.
1124 *
Joel Hockey0f933582019-08-27 18:01:51 -07001125 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001126 */
1127hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001128 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1129 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001130 this.screen_.setCursorPosition(row, column);
1131 if (cursor.column > column ||
1132 cursor.column == column && cursor.overflow) {
1133 this.screen_.cursorPosition.overflow = true;
1134 }
rginda87b86462011-12-14 13:48:03 -08001135};
1136
1137/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001138 * Clear the cursor's overflow flag.
1139 */
1140hterm.Terminal.prototype.clearCursorOverflow = function() {
1141 this.screen_.cursorPosition.overflow = false;
1142};
1143
1144/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001145 * Save the current cursor state to the corresponding screens.
1146 *
1147 * See the hterm.Screen.CursorState class for more details.
1148 *
1149 * @param {boolean=} both If true, update both screens, else only update the
1150 * current screen.
1151 */
1152hterm.Terminal.prototype.saveCursorAndState = function(both) {
1153 if (both) {
1154 this.primaryScreen_.saveCursorAndState(this.vt);
1155 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001156 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001157 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001158 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001159};
1160
1161/**
1162 * Restore the saved cursor state in the corresponding screens.
1163 *
1164 * See the hterm.Screen.CursorState class for more details.
1165 *
1166 * @param {boolean=} both If true, update both screens, else only update the
1167 * current screen.
1168 */
1169hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1170 if (both) {
1171 this.primaryScreen_.restoreCursorAndState(this.vt);
1172 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001173 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001174 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001175 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001176};
1177
1178/**
Robert Ginda830583c2013-08-07 13:20:46 -07001179 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001180 *
1181 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001182 */
1183hterm.Terminal.prototype.setCursorShape = function(shape) {
1184 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001185 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001186};
Robert Ginda830583c2013-08-07 13:20:46 -07001187
1188/**
1189 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001190 *
1191 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001192 */
1193hterm.Terminal.prototype.getCursorShape = function() {
1194 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001195};
Robert Ginda830583c2013-08-07 13:20:46 -07001196
1197/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001198 * Set the screen padding size in pixels.
1199 *
1200 * @param {number} size
1201 */
1202hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001203 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001204 this.scrollPort_.setScreenPaddingSize(size);
1205};
1206
1207/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001208 * Set the screen border size in pixels.
1209 *
1210 * @param {number} size
1211 */
1212hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1213 this.div_.style.borderWidth = `${size}px`;
1214 this.screenBorderSize_ = size;
1215 this.scrollPort_.resize();
1216};
1217
1218/**
rginda87b86462011-12-14 13:48:03 -08001219 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001220 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001221 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001222 */
1223hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001224 if (columnCount == null) {
1225 this.div_.style.width = '100%';
1226 return;
1227 }
1228
Joel Hockey139d82d2020-04-07 23:04:29 -07001229 const rightPadding = Math.max(
1230 this.scrollPort_.screenPaddingSize,
1231 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001232 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001233 (this.scrollPort_.characterSize.width * columnCount) +
1234 this.scrollPort_.screenPaddingSize + rightPadding +
1235 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001236 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001237 this.scheduleSyncCursorPosition_();
1238};
rginda87b86462011-12-14 13:48:03 -08001239
rgindac9bc5502012-01-18 11:48:44 -08001240/**
rginda35c456b2012-02-09 17:29:05 -08001241 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001242 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001243 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001244 */
1245hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001246 if (rowCount == null) {
1247 this.div_.style.height = '100%';
1248 return;
1249 }
1250
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001251 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1252 (2 * this.scrollPort_.screenPaddingSize) +
1253 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001254 this.realizeSize_(this.screenSize.width, rowCount);
1255 this.scheduleSyncCursorPosition_();
1256};
1257
1258/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001259 * Deal with terminal size changes.
1260 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001261 * @param {number} columnCount The number of columns.
1262 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001263 */
1264hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001265 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001266
Mike Frysinger0206e262019-06-13 10:18:19 -04001267 if (columnCount != this.screenSize.width) {
1268 notify = true;
1269 this.realizeWidth_(columnCount);
1270 }
1271
1272 if (rowCount != this.screenSize.height) {
1273 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001274 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001275 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001276
1277 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001278 if (notify) {
1279 this.io.onTerminalResize_(columnCount, rowCount);
1280 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001281};
1282
1283/**
rgindac9bc5502012-01-18 11:48:44 -08001284 * Deal with terminal width changes.
1285 *
1286 * This function does what needs to be done when the terminal width changes
1287 * out from under us. It happens here rather than in onResize_() because this
1288 * code may need to run synchronously to handle programmatic changes of
1289 * terminal width.
1290 *
1291 * Relying on the browser to send us an async resize event means we may not be
1292 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001293 *
1294 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001295 */
1296hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001297 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001298 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001299 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001300
Mike Frysingerdc727792020-04-10 01:41:13 -04001301 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001302 if (deltaColumns == 0) {
1303 // No change, so don't bother recalculating things.
1304 return;
1305 }
rgindac9bc5502012-01-18 11:48:44 -08001306
rginda87b86462011-12-14 13:48:03 -08001307 this.screenSize.width = columnCount;
1308 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001309
1310 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001311 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001312 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001313 }
rgindac9bc5502012-01-18 11:48:44 -08001314 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001315 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001316 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001317 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001318 }
rgindac9bc5502012-01-18 11:48:44 -08001319
1320 this.tabStops_.pop();
1321 }
1322 }
1323
1324 this.screen_.setColumnCount(this.screenSize.width);
1325};
1326
1327/**
1328 * Deal with terminal height changes.
1329 *
1330 * This function does what needs to be done when the terminal height changes
1331 * out from under us. It happens here rather than in onResize_() because this
1332 * code may need to run synchronously to handle programmatic changes of
1333 * terminal height.
1334 *
1335 * Relying on the browser to send us an async resize event means we may not be
1336 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001337 *
1338 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001339 */
1340hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001341 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001342 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001343 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001344
Mike Frysingerdc727792020-04-10 01:41:13 -04001345 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001346 if (deltaRows == 0) {
1347 // No change, so don't bother recalculating things.
1348 return;
1349 }
rgindac9bc5502012-01-18 11:48:44 -08001350
1351 this.screenSize.height = rowCount;
1352
Mike Frysingerdc727792020-04-10 01:41:13 -04001353 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001354
1355 if (deltaRows < 0) {
1356 // Screen got smaller.
1357 deltaRows *= -1;
1358 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001359 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001360 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001361 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001362 }
rgindac9bc5502012-01-18 11:48:44 -08001363
Mike Frysingerbdb34802020-04-07 03:47:32 -04001364 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001365 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001366 }
rgindac9bc5502012-01-18 11:48:44 -08001367
1368 this.screen_.popRow();
1369 deltaRows--;
1370 }
1371
Mike Frysingerdc727792020-04-10 01:41:13 -04001372 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001373 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1374
1375 // We just removed rows from the top of the screen, we need to update
1376 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001377 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001378 } else if (deltaRows > 0) {
1379 // Screen got larger.
1380
1381 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001382 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1383 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001384 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1385 this.screen_.unshiftRows(rows);
1386 deltaRows -= scrollbackCount;
1387 cursor.row += scrollbackCount;
1388 }
1389
Mike Frysingerbdb34802020-04-07 03:47:32 -04001390 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001391 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001392 }
rgindac9bc5502012-01-18 11:48:44 -08001393 }
1394
rginda35c456b2012-02-09 17:29:05 -08001395 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001396 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001397};
1398
1399/**
1400 * Scroll the terminal to the top of the scrollback buffer.
1401 */
1402hterm.Terminal.prototype.scrollHome = function() {
1403 this.scrollPort_.scrollRowToTop(0);
1404};
1405
1406/**
1407 * Scroll the terminal to the end.
1408 */
1409hterm.Terminal.prototype.scrollEnd = function() {
1410 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1411};
1412
1413/**
1414 * Scroll the terminal one page up (minus one line) relative to the current
1415 * position.
1416 */
1417hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001418 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001419};
1420
1421/**
1422 * Scroll the terminal one page down (minus one line) relative to the current
1423 * position.
1424 */
1425hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001426 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001427};
1428
rgindac9bc5502012-01-18 11:48:44 -08001429/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001430 * Scroll the terminal one line up relative to the current position.
1431 */
1432hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001433 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001434 this.scrollPort_.scrollRowToTop(i - 1);
1435};
1436
1437/**
1438 * Scroll the terminal one line down relative to the current position.
1439 */
1440hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001441 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001442 this.scrollPort_.scrollRowToTop(i + 1);
1443};
1444
1445/**
Robert Ginda40932892012-12-10 17:26:40 -08001446 * Clear primary screen, secondary screen, and the scrollback buffer.
1447 */
1448hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001449 this.clearHome(this.primaryScreen_);
1450 this.clearHome(this.alternateScreen_);
1451
1452 this.clearScrollback();
1453};
1454
1455/**
1456 * Clear scrollback buffer.
1457 */
1458hterm.Terminal.prototype.clearScrollback = function() {
1459 // Move to the end of the buffer in case the screen was scrolled back.
1460 // We're going to throw it away which would leave the display invalid.
1461 this.scrollEnd();
1462
Robert Ginda40932892012-12-10 17:26:40 -08001463 this.scrollbackRows_.length = 0;
1464 this.scrollPort_.resetCache();
1465
Mike Frysinger9c482b82018-09-07 02:49:36 -04001466 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1467 const bottom = screen.getHeight();
1468 this.renumberRows_(0, bottom, screen);
1469 });
Robert Ginda40932892012-12-10 17:26:40 -08001470
1471 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001472 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001473};
1474
1475/**
rgindac9bc5502012-01-18 11:48:44 -08001476 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001477 *
1478 * Perform a full reset to the default values listed in
1479 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001480 */
rginda87b86462011-12-14 13:48:03 -08001481hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001482 this.vt.reset();
1483
rgindac9bc5502012-01-18 11:48:44 -08001484 this.clearAllTabStops();
1485 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001486
Joel Hockey42dba8f2020-03-26 16:21:11 -07001487 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001488 const resetScreen = (screen) => {
1489 // We want to make sure to reset the attributes before we clear the screen.
1490 // The attributes might be used to initialize default/empty rows.
1491 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001492 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001493 this.clearHome(screen);
1494 screen.saveCursorAndState(this.vt);
1495 };
1496 resetScreen(this.primaryScreen_);
1497 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001498
Mike Frysinger84301d02017-11-29 13:28:46 -08001499 // Reset terminal options to their default values.
1500 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001501 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1502
Mike Frysinger84301d02017-11-29 13:28:46 -08001503 this.setVTScrollRegion(null, null);
1504
1505 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001506};
1507
rgindac9bc5502012-01-18 11:48:44 -08001508/**
1509 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001510 *
1511 * Perform a soft reset to the default values listed in
1512 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001513 */
rginda0f5c0292012-01-13 11:00:13 -08001514hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001515 this.vt.reset();
1516
rgindab8bc8932012-04-27 12:45:03 -07001517 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001518 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001519
Brad Townb62dfdc2015-03-16 19:07:15 -07001520 // We show the cursor on soft reset but do not alter the blink state.
1521 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1522
Joel Hockey42dba8f2020-03-26 16:21:11 -07001523 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001524 const resetScreen = (screen) => {
1525 // Xterm also resets the color palette on soft reset, even though it doesn't
1526 // seem to be documented anywhere.
1527 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001528 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001529 screen.saveCursorAndState(this.vt);
1530 };
1531 resetScreen(this.primaryScreen_);
1532 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001533
rgindab8bc8932012-04-27 12:45:03 -07001534 // The xterm man page explicitly says this will happen on soft reset.
1535 this.setVTScrollRegion(null, null);
1536
1537 // Xterm also shows the cursor on soft reset, but does not alter the blink
1538 // state.
rgindaa19afe22012-01-25 15:40:22 -08001539 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001540};
1541
rgindac9bc5502012-01-18 11:48:44 -08001542/**
1543 * Move the cursor forward to the next tab stop, or to the last column
1544 * if no more tab stops are set.
1545 */
1546hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001547 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001548
Mike Frysingerdc727792020-04-10 01:41:13 -04001549 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001550 if (this.tabStops_[i] > column) {
1551 this.setCursorColumn(this.tabStops_[i]);
1552 return;
1553 }
1554 }
1555
David Benjamin66e954d2012-05-05 21:08:12 -04001556 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001557 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001558 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001559 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001560};
1561
rgindac9bc5502012-01-18 11:48:44 -08001562/**
1563 * Move the cursor backward to the previous tab stop, or to the first column
1564 * if no previous tab stops are set.
1565 */
1566hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001567 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001568
Mike Frysingerdc727792020-04-10 01:41:13 -04001569 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001570 if (this.tabStops_[i] < column) {
1571 this.setCursorColumn(this.tabStops_[i]);
1572 return;
1573 }
1574 }
1575
1576 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001577};
1578
rgindac9bc5502012-01-18 11:48:44 -08001579/**
1580 * Set a tab stop at the given column.
1581 *
Joel Hockey0f933582019-08-27 18:01:51 -07001582 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001583 */
1584hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001585 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001586 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001587 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001588 }
rgindac9bc5502012-01-18 11:48:44 -08001589
1590 if (this.tabStops_[i] < column) {
1591 this.tabStops_.splice(i + 1, 0, column);
1592 return;
1593 }
1594 }
1595
1596 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001597};
1598
rgindac9bc5502012-01-18 11:48:44 -08001599/**
1600 * Clear the tab stop at the current cursor position.
1601 *
1602 * No effect if there is no tab stop at the current cursor position.
1603 */
1604hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001605 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001606
Mike Frysingerdc727792020-04-10 01:41:13 -04001607 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001608 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001609 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001610 }
rgindac9bc5502012-01-18 11:48:44 -08001611
1612 this.tabStops_.splice(i, 1);
1613};
1614
1615/**
1616 * Clear all tab stops.
1617 */
1618hterm.Terminal.prototype.clearAllTabStops = function() {
1619 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001620 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001621};
1622
1623/**
1624 * Set up the default tab stops, starting from a given column.
1625 *
1626 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001627 * from the specified column, or 0 if no column is provided. It also flags
1628 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001629 *
1630 * This does not clear the existing tab stops first, use clearAllTabStops
1631 * for that.
1632 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001633 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001634 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001635 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001636hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001637 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001638 // Round start up to a default tab stop.
1639 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001640 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001641 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001642 }
David Benjamin66e954d2012-05-05 21:08:12 -04001643
1644 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001645};
1646
rginda6d397402012-01-17 10:58:29 -08001647/**
rginda8ba33642011-12-14 12:31:31 -08001648 * Interpret a sequence of characters.
1649 *
1650 * Incomplete escape sequences are buffered until the next call.
1651 *
1652 * @param {string} str Sequence of characters to interpret or pass through.
1653 */
1654hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001655 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001656 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001657};
1658
1659/**
1660 * Take over the given DIV for use as the terminal display.
1661 *
Joel Hockey0f933582019-08-27 18:01:51 -07001662 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001663 */
1664hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001665 const charset = div.ownerDocument.characterSet.toLowerCase();
1666 if (charset != 'utf-8') {
1667 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1668 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1669 }
1670
rginda87b86462011-12-14 13:48:03 -08001671 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001672 this.div_.style.borderStyle = 'solid';
1673 this.div_.style.borderWidth = 0;
1674 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001675
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001676 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1677
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001678 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1679};
1680
1681/**
1682 * Initialisation of ScrollPort properties which need to be set after its DOM
1683 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001684 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001685 * @private
1686 */
1687hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001688 this.scrollPort_.setBackgroundImage(
1689 this.prefs_.getString('background-image'));
1690 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001691 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001692 this.prefs_.getString('background-position'));
1693 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1694 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1695 this.scrollPort_.setAccessibilityReader(
1696 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001697
rginda0918b652012-04-04 11:26:24 -07001698 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001699
Joel Hockeyd4fca732019-09-20 16:57:03 -07001700 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001701 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001702
Joel Hockeyd4fca732019-09-20 16:57:03 -07001703 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001704 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001705 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001706
rginda8ba33642011-12-14 12:31:31 -08001707 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001708 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301709 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001710
Evan Jones5f9df812016-12-06 09:38:58 -05001711 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001712 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001713
Mike Frysingerdc727792020-04-10 01:41:13 -04001714 const onMouse = this.onMouse_.bind(this);
1715 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001716 screenNode.addEventListener(
1717 'mousedown', /** @type {!EventListener} */ (onMouse));
1718 screenNode.addEventListener(
1719 'mouseup', /** @type {!EventListener} */ (onMouse));
1720 screenNode.addEventListener(
1721 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001722 this.scrollPort_.onScrollWheel = onMouse;
1723
Joel Hockeyd4fca732019-09-20 16:57:03 -07001724 screenNode.addEventListener(
1725 'keydown',
1726 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001727
Toni Barzic0bfa8922013-11-22 11:18:35 -08001728 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001729 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001730 // Listen for mousedown events on the screenNode as in FF the focus
1731 // events don't bubble.
1732 screenNode.addEventListener('mousedown', function() {
1733 setTimeout(this.onFocusChange_.bind(this, true));
1734 }.bind(this));
1735
Toni Barzic0bfa8922013-11-22 11:18:35 -08001736 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001737 'blur', this.onFocusChange_.bind(this, false));
1738
Mike Frysingerdc727792020-04-10 01:41:13 -04001739 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001740 style.textContent = `
1741.cursor-node[focus="false"] {
1742 box-sizing: border-box;
1743 background-color: transparent !important;
1744 border-width: 2px;
1745 border-style: solid;
1746}
1747menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001748 background: #fff;
1749 border-radius: 4px;
1750 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001751 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001752 display: none;
1753 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1754 margin: 0;
1755 padding: 8px 0;
1756 position: absolute;
1757 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001758}
1759menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001760 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001761 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1762 padding: 0.5em 1em;
1763 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001764}
1765menuitem.separator {
1766 border-bottom: none;
1767 height: 0.5em;
1768 padding: 0;
1769}
1770menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001771 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001772}
1773.wc-node {
1774 display: inline-block;
1775 text-align: center;
1776 width: calc(var(--hterm-charsize-width) * 2);
1777 line-height: var(--hterm-charsize-height);
1778}
1779:root {
1780 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1781 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001782 --hterm-blink-node-duration: 0.7s;
1783 --hterm-mouse-cursor-default: default;
1784 --hterm-mouse-cursor-text: text;
1785 --hterm-mouse-cursor-pointer: pointer;
1786 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001787 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001788
Joel Hockey42dba8f2020-03-26 16:21:11 -07001789${lib.colors.stockColorPalette.map((c, i) => `
1790 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1791`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001792}
1793.uri-node:hover {
1794 text-decoration: underline;
1795 cursor: var(--hterm-mouse-cursor-pointer);
1796}
1797@keyframes blink {
1798 from { opacity: 1.0; }
1799 to { opacity: 0.0; }
1800}
1801.blink-node {
1802 animation-name: blink;
1803 animation-duration: var(--hterm-blink-node-duration);
1804 animation-iteration-count: infinite;
1805 animation-timing-function: ease-in-out;
1806 animation-direction: alternate;
1807}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001808 // Insert this stock style as the first node so that any user styles will
1809 // override w/out having to use !important everywhere. The rules above mix
1810 // runtime variables with default ones designed to be overridden by the user,
1811 // but we can wait for a concrete case from the users to determine the best
1812 // way to split the sheet up to before & after the user-css settings.
1813 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001814
rginda8ba33642011-12-14 12:31:31 -08001815 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001816 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001817 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001818 this.cursorNode_.style.cssText = `
1819position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001820left: calc(var(--hterm-screen-padding-size) +
1821 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1822top: calc(var(--hterm-screen-padding-size) +
1823 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001824display: ${this.options_.cursorVisible ? '' : 'none'};
1825width: var(--hterm-charsize-width);
1826height: var(--hterm-charsize-height);
1827background-color: var(--hterm-cursor-color);
1828border-color: var(--hterm-cursor-color);
1829-webkit-transition: opacity, background-color 100ms linear;
1830-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001831
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001832 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001833 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1834 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001835
rginda8ba33642011-12-14 12:31:31 -08001836 this.document_.body.appendChild(this.cursorNode_);
1837
rgindad5613292012-06-19 15:40:37 -07001838 // When 'enableMouseDragScroll' is off we reposition this element directly
1839 // under the mouse cursor after a click. This makes Chrome associate
1840 // subsequent mousemove events with the scroll-blocker. Since the
1841 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1842 // events do not cause the scrollport to scroll.
1843 //
1844 // It's a hack, but it's the cleanest way I could find.
1845 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001846 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001847 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001848 this.scrollBlockerNode_.style.cssText =
1849 ('position: absolute;' +
1850 'top: -99px;' +
1851 'display: block;' +
1852 'width: 10px;' +
1853 'height: 10px;');
1854 this.document_.body.appendChild(this.scrollBlockerNode_);
1855
rgindad5613292012-06-19 15:40:37 -07001856 this.scrollPort_.onScrollWheel = onMouse;
1857 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1858 ].forEach(function(event) {
1859 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001860 this.cursorNode_.addEventListener(
1861 event, /** @type {!EventListener} */ (onMouse));
1862 this.document_.addEventListener(
1863 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001864 }.bind(this));
1865
1866 this.cursorNode_.addEventListener('mousedown', function() {
1867 setTimeout(this.focus.bind(this));
1868 }.bind(this));
1869
rginda8ba33642011-12-14 12:31:31 -08001870 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001871
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001872 // Re-sync fonts whenever a web font loads.
1873 this.document_.fonts.addEventListener(
1874 'loadingdone', () => this.syncFontFamily());
1875
rginda87b86462011-12-14 13:48:03 -08001876 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001877 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001878};
1879
rginda0918b652012-04-04 11:26:24 -07001880/**
1881 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001882 *
Joel Hockey0f933582019-08-27 18:01:51 -07001883 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001884 */
rginda87b86462011-12-14 13:48:03 -08001885hterm.Terminal.prototype.getDocument = function() {
1886 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001887};
1888
1889/**
rginda0918b652012-04-04 11:26:24 -07001890 * Focus the terminal.
1891 */
1892hterm.Terminal.prototype.focus = function() {
1893 this.scrollPort_.focus();
1894};
1895
1896/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001897 * Unfocus the terminal.
1898 */
1899hterm.Terminal.prototype.blur = function() {
1900 this.scrollPort_.blur();
1901};
1902
1903/**
rginda8ba33642011-12-14 12:31:31 -08001904 * Return the HTML Element for a given row index.
1905 *
1906 * This is a method from the RowProvider interface. The ScrollPort uses
1907 * it to fetch rows on demand as they are scrolled into view.
1908 *
1909 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1910 * pairs to conserve memory.
1911 *
Joel Hockey0f933582019-08-27 18:01:51 -07001912 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001913 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001914 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001915 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001916 * @override
rginda8ba33642011-12-14 12:31:31 -08001917 */
1918hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001919 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001920 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001921 }
rginda8ba33642011-12-14 12:31:31 -08001922
Mike Frysingerdc727792020-04-10 01:41:13 -04001923 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001924 return this.screen_.rowsArray[screenIndex];
1925};
1926
1927/**
1928 * Return the text content for a given range of rows.
1929 *
1930 * This is a method from the RowProvider interface. The ScrollPort uses
1931 * it to fetch text content on demand when the user attempts to copy their
1932 * selection to the clipboard.
1933 *
Joel Hockey0f933582019-08-27 18:01:51 -07001934 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001935 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001936 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001937 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001938 * relative to the start of the scrollback buffer.
1939 * @return {string} A single string containing the text value of the range of
1940 * rows. Lines will be newline delimited, with no trailing newline.
1941 */
1942hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001943 const ary = [];
1944 for (let i = start; i < end; i++) {
1945 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001946 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001947 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001948 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001949 }
rginda8ba33642011-12-14 12:31:31 -08001950 }
1951
rgindaa09e7332012-08-17 12:49:51 -07001952 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001953};
1954
1955/**
1956 * Return the text content for a given row.
1957 *
1958 * This is a method from the RowProvider interface. The ScrollPort uses
1959 * it to fetch text content on demand when the user attempts to copy their
1960 * selection to the clipboard.
1961 *
Joel Hockey0f933582019-08-27 18:01:51 -07001962 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001963 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001964 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001965 * @return {string} A string containing the text value of the selected row.
1966 */
1967hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001968 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001969 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001970};
1971
1972/**
1973 * Return the total number of rows in the addressable screen and in the
1974 * scrollback buffer of this terminal.
1975 *
1976 * This is a method from the RowProvider interface. The ScrollPort uses
1977 * it to compute the size of the scrollbar.
1978 *
Joel Hockey0f933582019-08-27 18:01:51 -07001979 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001980 * @override
rginda8ba33642011-12-14 12:31:31 -08001981 */
1982hterm.Terminal.prototype.getRowCount = function() {
1983 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1984};
1985
1986/**
1987 * Create DOM nodes for new rows and append them to the end of the terminal.
1988 *
1989 * This is the only correct way to add a new DOM node for a row. Notice that
1990 * the new row is appended to the bottom of the list of rows, and does not
1991 * require renumbering (of the rowIndex property) of previous rows.
1992 *
1993 * If you think you want a new blank row somewhere in the middle of the
1994 * terminal, look into moveRows_().
1995 *
1996 * This method does not pay attention to vtScrollTop/Bottom, since you should
1997 * be using moveRows() in cases where they would matter.
1998 *
1999 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002000 *
2001 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002002 */
2003hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002004 let cursorRow = this.screen_.rowsArray.length;
2005 const offset = this.scrollbackRows_.length + cursorRow;
2006 for (let i = 0; i < count; i++) {
2007 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002008 row.appendChild(this.document_.createTextNode(''));
2009 row.rowIndex = offset + i;
2010 this.screen_.pushRow(row);
2011 }
2012
Mike Frysingerdc727792020-04-10 01:41:13 -04002013 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002014 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002015 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002016 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002017 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002018 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002019 }
rginda8ba33642011-12-14 12:31:31 -08002020 }
2021
Mike Frysingerbdb34802020-04-07 03:47:32 -04002022 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002023 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002024 }
rginda8ba33642011-12-14 12:31:31 -08002025
rginda87b86462011-12-14 13:48:03 -08002026 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002027};
2028
2029/**
2030 * Relocate rows from one part of the addressable screen to another.
2031 *
2032 * This is used to recycle rows during VT scrolls (those which are driven
2033 * by VT commands, rather than by the user manipulating the scrollbar.)
2034 *
2035 * In this case, the blank lines scrolled into the scroll region are made of
2036 * the nodes we scrolled off. These have their rowIndex properties carefully
2037 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002038 *
2039 * @param {number} fromIndex The start index.
2040 * @param {number} count The number of rows to move.
2041 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002042 */
2043hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002044 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002045 this.screen_.insertRows(toIndex, ary);
2046
Mike Frysingerdc727792020-04-10 01:41:13 -04002047 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002048 if (fromIndex < toIndex) {
2049 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002050 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002051 } else {
2052 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002053 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002054 }
2055
2056 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002057 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002058};
2059
2060/**
2061 * Renumber the rowIndex property of the given range of rows.
2062 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002063 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002064 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002065 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002066 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002067 *
2068 * @param {number} start The start index.
2069 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002070 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002071 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002072hterm.Terminal.prototype.renumberRows_ = function(
2073 start, end, screen = undefined) {
2074 if (!screen) {
2075 screen = this.screen_;
2076 }
Robert Ginda40932892012-12-10 17:26:40 -08002077
Mike Frysingerdc727792020-04-10 01:41:13 -04002078 const offset = this.scrollbackRows_.length;
2079 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002080 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002081 }
2082};
2083
2084/**
2085 * Print a string to the terminal.
2086 *
2087 * This respects the current insert and wraparound modes. It will add new lines
2088 * to the end of the terminal, scrolling off the top into the scrollback buffer
2089 * if necessary.
2090 *
2091 * The string is *not* parsed for escape codes. Use the interpret() method if
2092 * that's what you're after.
2093 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002094 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002095 */
2096hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002097 this.scheduleSyncCursorPosition_();
2098
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002099 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002100 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002101
Mike Frysingerdc727792020-04-10 01:41:13 -04002102 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002103
Mike Frysingerdc727792020-04-10 01:41:13 -04002104 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002105 // Fun edge case: If the string only contains zero width codepoints (like
2106 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002107 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002108 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002109 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002110
2111 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002112 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2113 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002114 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002115 }
rgindaa19afe22012-01-25 15:40:22 -08002116
Mike Frysingerdc727792020-04-10 01:41:13 -04002117 let count = strWidth - startOffset;
2118 let didOverflow = false;
2119 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002120
rgindaa9abdd82012-08-06 18:05:09 -07002121 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2122 didOverflow = true;
2123 count = this.screenSize.width - this.screen_.cursorPosition.column;
2124 }
rgindaa19afe22012-01-25 15:40:22 -08002125
rgindaa9abdd82012-08-06 18:05:09 -07002126 if (didOverflow && !this.options_.wraparound) {
2127 // If the string overflowed the line but wraparound is off, then the
2128 // last printed character should be the last of the string.
2129 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002130 substr = lib.wc.substr(str, startOffset, count - 1) +
2131 lib.wc.substr(str, strWidth - 1);
2132 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002133 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002134 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002135 }
rgindaa19afe22012-01-25 15:40:22 -08002136
Mike Frysingerdc727792020-04-10 01:41:13 -04002137 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2138 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002139 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2140 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002141
2142 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002143 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002144 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002145 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002146 }
2147 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002148 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002149 }
2150
2151 this.screen_.maybeClipCurrentRow();
2152 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002153 }
rginda8ba33642011-12-14 12:31:31 -08002154
Mike Frysingerbdb34802020-04-07 03:47:32 -04002155 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002156 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002157 }
rginda8ba33642011-12-14 12:31:31 -08002158};
2159
2160/**
rginda87b86462011-12-14 13:48:03 -08002161 * Set the VT scroll region.
2162 *
rginda87b86462011-12-14 13:48:03 -08002163 * This also resets the cursor position to the absolute (0, 0) position, since
2164 * that's what xterm appears to do.
2165 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002166 * Setting the scroll region to the full height of the terminal will clear
2167 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2168 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2169 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2170 * continue to work as most users would expect.
2171 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002172 * @param {?number} scrollTop The zero-based top of the scroll region.
2173 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002174 * inclusive.
2175 */
2176hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002177 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002178 this.vtScrollTop_ = null;
2179 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002180 } else {
2181 this.vtScrollTop_ = scrollTop;
2182 this.vtScrollBottom_ = scrollBottom;
2183 }
rginda87b86462011-12-14 13:48:03 -08002184};
2185
2186/**
rginda8ba33642011-12-14 12:31:31 -08002187 * Return the top row index according to the VT.
2188 *
2189 * This will return 0 unless the terminal has been told to restrict scrolling
2190 * to some lower row. It is used for some VT cursor positioning and scrolling
2191 * commands.
2192 *
Joel Hockey0f933582019-08-27 18:01:51 -07002193 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002194 */
2195hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002196 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002197 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002198 }
rginda8ba33642011-12-14 12:31:31 -08002199
2200 return 0;
rginda87b86462011-12-14 13:48:03 -08002201};
rginda8ba33642011-12-14 12:31:31 -08002202
2203/**
2204 * Return the bottom row index according to the VT.
2205 *
2206 * This will return the height of the terminal unless the it has been told to
2207 * restrict scrolling to some higher row. It is used for some VT cursor
2208 * positioning and scrolling commands.
2209 *
Joel Hockey0f933582019-08-27 18:01:51 -07002210 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002211 */
2212hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002213 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002214 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002215 }
rginda8ba33642011-12-14 12:31:31 -08002216
rginda87b86462011-12-14 13:48:03 -08002217 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002218};
rginda8ba33642011-12-14 12:31:31 -08002219
2220/**
2221 * Process a '\n' character.
2222 *
2223 * If the cursor is on the final row of the terminal this will append a new
2224 * blank row to the screen and scroll the topmost row into the scrollback
2225 * buffer.
2226 *
2227 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002228 *
2229 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2230 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002231 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002232hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002233 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002234 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002235 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002236
Mike Frysingerdc727792020-04-10 01:41:13 -04002237 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2238 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002239
2240 if (this.vtScrollBottom_ != null) {
2241 // A VT Scroll region is active, we never append new rows.
2242 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2243 // We're at the end of the VT Scroll Region, perform a VT scroll.
2244 this.vtScrollUp(1);
2245 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2246 } else if (cursorAtEndOfScreen) {
2247 // We're at the end of the screen, the only thing to do is put the
2248 // cursor to column 0.
2249 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2250 } else {
2251 // Anywhere else, advance the cursor row, and reset the column.
2252 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2253 }
2254 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002255 // We're at the end of the screen. Append a new row to the terminal,
2256 // shifting the top row into the scrollback.
2257 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002258 } else {
rginda87b86462011-12-14 13:48:03 -08002259 // Anywhere else in the screen just moves the cursor.
2260 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002261 }
2262};
2263
2264/**
2265 * Like newLine(), except maintain the cursor column.
2266 */
2267hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002268 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002269 this.newLine();
2270 this.setCursorColumn(column);
2271};
2272
2273/**
rginda87b86462011-12-14 13:48:03 -08002274 * If autoCarriageReturn is set then newLine(), else lineFeed().
2275 */
2276hterm.Terminal.prototype.formFeed = function() {
2277 if (this.options_.autoCarriageReturn) {
2278 this.newLine();
2279 } else {
2280 this.lineFeed();
2281 }
2282};
2283
2284/**
2285 * Move the cursor up one row, possibly inserting a blank line.
2286 *
2287 * The cursor column is not changed.
2288 */
2289hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002290 const scrollTop = this.getVTScrollTop();
2291 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002292
2293 if (currentRow == scrollTop) {
2294 this.insertLines(1);
2295 } else {
2296 this.setAbsoluteCursorRow(currentRow - 1);
2297 }
2298};
2299
2300/**
rginda8ba33642011-12-14 12:31:31 -08002301 * Replace all characters to the left of the current cursor with the space
2302 * character.
2303 *
2304 * TODO(rginda): This should probably *remove* the characters (not just replace
2305 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002306 * position.
rginda8ba33642011-12-14 12:31:31 -08002307 */
2308hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002309 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002310 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002311 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002312 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002313 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002314};
2315
2316/**
David Benjamin684a9b72012-05-01 17:19:58 -04002317 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002318 *
2319 * The cursor position is unchanged.
2320 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002321 * If the current background color is not the default background color this
2322 * will insert spaces rather than delete. This is unfortunate because the
2323 * trailing space will affect text selection, but it's difficult to come up
2324 * with a way to style empty space that wouldn't trip up the hterm.Screen
2325 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002326 *
2327 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2328 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2329 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002330 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002331 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002332 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002333hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002334 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002335 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002336 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002337
Mike Frysingerdc727792020-04-10 01:41:13 -04002338 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002339 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002340
2341 if (this.screen_.textAttributes.background ===
2342 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002343 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002344 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002345 this.screen_.cursorPosition.column + count) {
2346 this.screen_.deleteChars(count);
2347 this.clearCursorOverflow();
2348 return;
2349 }
2350 }
2351
Mike Frysingerdc727792020-04-10 01:41:13 -04002352 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002353 this.screen_.overwriteString(' '.repeat(count), count);
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/**
2359 * Erase the current line.
2360 *
2361 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002362 */
2363hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002364 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002365 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002366 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002367 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002368};
2369
2370/**
David Benjamina08d78f2012-05-05 00:28:49 -04002371 * Erase all characters from the start of the screen to the current cursor
2372 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002373 *
2374 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002375 */
2376hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002377 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002378
2379 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002380
Mike Frysingerdc727792020-04-10 01:41:13 -04002381 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002382 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002383 this.screen_.clearCursorRow();
2384 }
2385
rginda87b86462011-12-14 13:48:03 -08002386 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002387 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002388};
2389
2390/**
2391 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002392 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002393 *
2394 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002395 */
2396hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002397 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002398
2399 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002400
Mike Frysingerdc727792020-04-10 01:41:13 -04002401 const bottom = this.screenSize.height - 1;
2402 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002403 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002404 this.screen_.clearCursorRow();
2405 }
2406
rginda87b86462011-12-14 13:48:03 -08002407 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002408 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002409};
2410
2411/**
2412 * Fill the terminal with a given character.
2413 *
2414 * This methods does not respect the VT scroll region.
2415 *
2416 * @param {string} ch The character to use for the fill.
2417 */
2418hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002419 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002420
2421 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002422 for (let row = 0; row < this.screenSize.height; row++) {
2423 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002424 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002425 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002426 }
2427 }
2428
2429 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002430};
2431
2432/**
rginda9ea433c2012-03-16 11:57:00 -07002433 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002434 *
rginda9ea433c2012-03-16 11:57:00 -07002435 * This does not respect the scroll region.
2436 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002437 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002438 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002439 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002440hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2441 if (!screen) {
2442 screen = this.screen_;
2443 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002444 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002445
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002446 this.accessibilityReader_.clear();
2447
rginda11057d52012-04-25 12:29:56 -07002448 if (bottom == 0) {
2449 // Empty screen, nothing to do.
2450 return;
2451 }
2452
Mike Frysingerdc727792020-04-10 01:41:13 -04002453 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002454 screen.setCursorPosition(i, 0);
2455 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002456 }
2457
rginda9ea433c2012-03-16 11:57:00 -07002458 screen.setCursorPosition(0, 0);
2459};
2460
2461/**
2462 * Erase the entire display without changing the cursor position.
2463 *
2464 * The cursor position is unchanged. This does not respect the scroll
2465 * region.
2466 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002467 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002468 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002469 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002470hterm.Terminal.prototype.clear = function(screen = undefined) {
2471 if (!screen) {
2472 screen = this.screen_;
2473 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002474 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002475 this.clearHome(screen);
2476 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002477};
2478
2479/**
2480 * VT command to insert lines at the current cursor row.
2481 *
2482 * This respects the current scroll region. Rows pushed off the bottom are
2483 * lost (they won't show up in the scrollback buffer).
2484 *
Joel Hockey0f933582019-08-27 18:01:51 -07002485 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002486 */
2487hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002488 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002489
Mike Frysingerdc727792020-04-10 01:41:13 -04002490 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002491 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002492
Robert Ginda579186b2012-09-26 11:40:04 -07002493 // The moveCount is the number of rows we need to relocate to make room for
2494 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002495 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002496 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002497 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002498 }
rginda8ba33642011-12-14 12:31:31 -08002499
Mike Frysingerdc727792020-04-10 01:41:13 -04002500 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002501 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002502 this.screen_.clearCursorRow();
2503 }
rginda8ba33642011-12-14 12:31:31 -08002504};
2505
2506/**
2507 * VT command to delete lines at the current cursor row.
2508 *
2509 * New rows are added to the bottom of scroll region to take their place. New
2510 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002511 *
2512 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002513 */
2514hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002515 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002516
Mike Frysingerdc727792020-04-10 01:41:13 -04002517 const top = cursor.row;
2518 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002519
Mike Frysingerdc727792020-04-10 01:41:13 -04002520 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002521 count = Math.min(count, maxCount);
2522
Mike Frysingerdc727792020-04-10 01:41:13 -04002523 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002524 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002525 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002526 }
rginda8ba33642011-12-14 12:31:31 -08002527
Mike Frysingerdc727792020-04-10 01:41:13 -04002528 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002529 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002530 this.screen_.clearCursorRow();
2531 }
2532
rginda87b86462011-12-14 13:48:03 -08002533 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002534 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002535};
2536
2537/**
2538 * Inserts the given number of spaces at the current cursor position.
2539 *
rginda87b86462011-12-14 13:48:03 -08002540 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002541 *
2542 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002543 */
2544hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002545 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002546
Mike Frysinger73e56462019-07-17 00:23:46 -05002547 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002548 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002549 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002550
2551 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002552 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002553};
2554
2555/**
2556 * Forward-delete the specified number of characters starting at the cursor
2557 * position.
2558 *
Joel Hockey0f933582019-08-27 18:01:51 -07002559 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002560 */
2561hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002562 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002563 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002564 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002565 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002566 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002567 this.restoreCursor(cursor);
2568 }
2569
David Benjamin54e8bf62012-06-01 22:31:40 -04002570 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002571};
2572
2573/**
2574 * Shift rows in the scroll region upwards by a given number of lines.
2575 *
2576 * New rows are inserted at the bottom of the scroll region to fill the
2577 * vacated rows. The new rows not filled out with the current text attributes.
2578 *
2579 * This function does not affect the scrollback rows at all. Rows shifted
2580 * off the top are lost.
2581 *
rginda87b86462011-12-14 13:48:03 -08002582 * The cursor position is not altered.
2583 *
Joel Hockey0f933582019-08-27 18:01:51 -07002584 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002585 */
2586hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002587 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002588
rginda87b86462011-12-14 13:48:03 -08002589 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002590 this.deleteLines(count);
2591
rginda87b86462011-12-14 13:48:03 -08002592 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002593};
2594
2595/**
2596 * Shift rows below the cursor down by a given number of lines.
2597 *
2598 * This function respects the current scroll region.
2599 *
2600 * New rows are inserted at the top of the scroll region to fill the
2601 * vacated rows. The new rows not filled out with the current text attributes.
2602 *
2603 * This function does not affect the scrollback rows at all. Rows shifted
2604 * off the bottom are lost.
2605 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002606 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002607 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002608hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002609 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002610
rginda87b86462011-12-14 13:48:03 -08002611 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002612 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002613
rginda87b86462011-12-14 13:48:03 -08002614 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002615};
2616
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002617/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002618 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002619 *
2620 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002621 * cause Assitive Technology to announce the output of the terminal. It also
2622 * enables other features that aid assistive technology. All the features gated
2623 * behind this flag have a performance impact on the terminal which is why they
2624 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002625 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002626 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002627 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002628hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002629 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002630};
rginda87b86462011-12-14 13:48:03 -08002631
rginda8ba33642011-12-14 12:31:31 -08002632/**
2633 * Set the cursor position.
2634 *
2635 * The cursor row is relative to the scroll region if the terminal has
2636 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2637 *
Joel Hockey0f933582019-08-27 18:01:51 -07002638 * @param {number} row The new zero-based cursor row.
2639 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002640 */
2641hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2642 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002643 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002644 } else {
rginda87b86462011-12-14 13:48:03 -08002645 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002646 }
rginda87b86462011-12-14 13:48:03 -08002647};
rginda8ba33642011-12-14 12:31:31 -08002648
Evan Jones2600d4f2016-12-06 09:29:36 -05002649/**
2650 * Move the cursor relative to its current position.
2651 *
2652 * @param {number} row
2653 * @param {number} column
2654 */
rginda87b86462011-12-14 13:48:03 -08002655hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002656 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002657 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2658 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002659 this.screen_.setCursorPosition(row, column);
2660};
2661
Evan Jones2600d4f2016-12-06 09:29:36 -05002662/**
2663 * Move the cursor to the specified position.
2664 *
2665 * @param {number} row
2666 * @param {number} column
2667 */
rginda87b86462011-12-14 13:48:03 -08002668hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002669 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2670 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002671 this.screen_.setCursorPosition(row, column);
2672};
2673
2674/**
2675 * Set the cursor column.
2676 *
Joel Hockey0f933582019-08-27 18:01:51 -07002677 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002678 */
2679hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002680 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002681};
2682
2683/**
2684 * Return the cursor column.
2685 *
Joel Hockey0f933582019-08-27 18:01:51 -07002686 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002687 */
2688hterm.Terminal.prototype.getCursorColumn = function() {
2689 return this.screen_.cursorPosition.column;
2690};
2691
2692/**
2693 * Set the cursor row.
2694 *
2695 * The cursor row is relative to the scroll region if the terminal has
2696 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2697 *
Joel Hockey0f933582019-08-27 18:01:51 -07002698 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002699 */
rginda87b86462011-12-14 13:48:03 -08002700hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2701 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002702};
2703
2704/**
2705 * Return the cursor row.
2706 *
Joel Hockey0f933582019-08-27 18:01:51 -07002707 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002708 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002709hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002710 return this.screen_.cursorPosition.row;
2711};
2712
2713/**
2714 * Request that the ScrollPort redraw itself soon.
2715 *
2716 * The redraw will happen asynchronously, soon after the call stack winds down.
2717 * Multiple calls will be coalesced into a single redraw.
2718 */
2719hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002720 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002721 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002722 }
rginda8ba33642011-12-14 12:31:31 -08002723
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002724 this.timeouts_.redraw = setTimeout(() => {
2725 delete this.timeouts_.redraw;
2726 this.scrollPort_.redraw_();
2727 });
rginda8ba33642011-12-14 12:31:31 -08002728};
2729
2730/**
2731 * Request that the ScrollPort be scrolled to the bottom.
2732 *
2733 * The scroll will happen asynchronously, soon after the call stack winds down.
2734 * Multiple calls will be coalesced into a single scroll.
2735 *
2736 * This affects the scrollbar position of the ScrollPort, and has nothing to
2737 * do with the VT scroll commands.
2738 */
2739hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002740 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002741 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002742 }
rginda8ba33642011-12-14 12:31:31 -08002743
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002744 this.timeouts_.scrollDown = setTimeout(() => {
2745 delete this.timeouts_.scrollDown;
2746 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2747 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002748};
2749
2750/**
2751 * Move the cursor up a specified number of rows.
2752 *
Joel Hockey0f933582019-08-27 18:01:51 -07002753 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002754 */
2755hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002756 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002757};
2758
2759/**
2760 * Move the cursor down a specified number of rows.
2761 *
Joel Hockey0f933582019-08-27 18:01:51 -07002762 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002763 */
2764hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002765 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002766 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2767 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2768 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002769
Mike Frysingerdc727792020-04-10 01:41:13 -04002770 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2771 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002772 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002773};
2774
2775/**
2776 * Move the cursor left a specified number of columns.
2777 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002778 * If reverse wraparound mode is enabled and the previous row wrapped into
2779 * the current row then we back up through the wraparound as well.
2780 *
Joel Hockey0f933582019-08-27 18:01:51 -07002781 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002782 */
2783hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002784 count = count || 1;
2785
Mike Frysingerbdb34802020-04-07 03:47:32 -04002786 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002787 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002788 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002789
Mike Frysingerdc727792020-04-10 01:41:13 -04002790 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002791 if (this.options_.reverseWraparound) {
2792 if (this.screen_.cursorPosition.overflow) {
2793 // If this cursor is in the right margin, consume one count to get it
2794 // back to the last column. This only applies when we're in reverse
2795 // wraparound mode.
2796 count--;
2797 this.clearCursorOverflow();
2798
Mike Frysingerbdb34802020-04-07 03:47:32 -04002799 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002800 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002801 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002802 }
2803
Mike Frysingerdc727792020-04-10 01:41:13 -04002804 let newRow = this.screen_.cursorPosition.row;
2805 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002806 if (newColumn < 0) {
2807 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2808 if (newRow < 0) {
2809 // xterm also wraps from row 0 to the last row.
2810 newRow = this.screenSize.height + newRow % this.screenSize.height;
2811 }
2812 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2813 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002814
Robert Gindabfb32622014-07-17 13:20:27 -07002815 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2816
2817 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002818 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002819 this.setCursorColumn(newColumn);
2820 }
rginda8ba33642011-12-14 12:31:31 -08002821};
2822
2823/**
2824 * Move the cursor right a specified number of columns.
2825 *
Joel Hockey0f933582019-08-27 18:01:51 -07002826 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002827 */
2828hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002829 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002830
Mike Frysingerbdb34802020-04-07 03:47:32 -04002831 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002832 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002833 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002834
Mike Frysingerdc727792020-04-10 01:41:13 -04002835 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2836 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002837 this.setCursorColumn(column);
2838};
2839
2840/**
2841 * Reverse the foreground and background colors of the terminal.
2842 *
2843 * This only affects text that was drawn with no attributes.
2844 *
2845 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2846 * been drawn with attributes that happen to coincide with the default
2847 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002848 *
2849 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002850 */
2851hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002852 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002853 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002854 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2855 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002856 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002857 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2858 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002859 }
2860};
2861
2862/**
rginda87b86462011-12-14 13:48:03 -08002863 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002864 *
2865 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002866 */
2867hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002868 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002869
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002870 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002871
Michael Kelly485ecd12014-06-09 11:41:56 -04002872 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002873 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002874 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002875 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002876
Robert Ginda92e18102013-03-14 13:56:37 -07002877 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002878 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002879 this.bellSequelchTimeout_ = setTimeout(() => {
2880 this.bellSquelchTimeout_ = null;
2881 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002882 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002883 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002884 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002885
2886 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002887 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002888 this.bellNotificationList_.push(n);
2889 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002890 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002891 }
rginda87b86462011-12-14 13:48:03 -08002892};
2893
2894/**
rginda8ba33642011-12-14 12:31:31 -08002895 * Set the origin mode bit.
2896 *
2897 * If origin mode is on, certain VT cursor and scrolling commands measure their
2898 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2899 * to the top of the addressable screen.
2900 *
2901 * Defaults to off.
2902 *
2903 * @param {boolean} state True to set origin mode, false to unset.
2904 */
2905hterm.Terminal.prototype.setOriginMode = function(state) {
2906 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002907 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002908};
2909
2910/**
2911 * Set the insert mode bit.
2912 *
2913 * If insert mode is on, existing text beyond the cursor position will be
2914 * shifted right to make room for new text. Otherwise, new text overwrites
2915 * any existing text.
2916 *
2917 * Defaults to off.
2918 *
2919 * @param {boolean} state True to set insert mode, false to unset.
2920 */
2921hterm.Terminal.prototype.setInsertMode = function(state) {
2922 this.options_.insertMode = state;
2923};
2924
2925/**
rginda87b86462011-12-14 13:48:03 -08002926 * Set the auto carriage return bit.
2927 *
2928 * If auto carriage return is on then a formfeed character is interpreted
2929 * as a newline, otherwise it's the same as a linefeed. The difference boils
2930 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002931 *
2932 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002933 */
2934hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2935 this.options_.autoCarriageReturn = state;
2936};
2937
2938/**
rginda8ba33642011-12-14 12:31:31 -08002939 * Set the wraparound mode bit.
2940 *
2941 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2942 * to the start of the following row. Otherwise, the cursor is clamped to the
2943 * end of the screen and attempts to write past it are ignored.
2944 *
2945 * Defaults to on.
2946 *
2947 * @param {boolean} state True to set wraparound mode, false to unset.
2948 */
2949hterm.Terminal.prototype.setWraparound = function(state) {
2950 this.options_.wraparound = state;
2951};
2952
2953/**
2954 * Set the reverse-wraparound mode bit.
2955 *
2956 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2957 * to the end of the previous row. Otherwise, the cursor is clamped to column
2958 * 0.
2959 *
2960 * Defaults to off.
2961 *
2962 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2963 */
2964hterm.Terminal.prototype.setReverseWraparound = function(state) {
2965 this.options_.reverseWraparound = state;
2966};
2967
2968/**
2969 * Selects between the primary and alternate screens.
2970 *
2971 * If alternate mode is on, the alternate screen is active. Otherwise the
2972 * primary screen is active.
2973 *
2974 * Swapping screens has no effect on the scrollback buffer.
2975 *
2976 * Each screen maintains its own cursor position.
2977 *
2978 * Defaults to off.
2979 *
2980 * @param {boolean} state True to set alternate mode, false to unset.
2981 */
2982hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002983 if (state == (this.screen_ == this.alternateScreen_)) {
2984 return;
2985 }
2986 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2987 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002988 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2989
Joel Hockey42dba8f2020-03-26 16:21:11 -07002990 // Swap color overrides.
2991 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2992 oldOverrides.forEach((c, i) => {
2993 if (!newOverrides.hasOwnProperty(i)) {
2994 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2995 }
2996 });
2997 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2998
rginda35c456b2012-02-09 17:29:05 -08002999 if (this.screen_.rowsArray.length &&
3000 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3001 // If the screen changed sizes while we were away, our rowIndexes may
3002 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003003 const offset = this.scrollbackRows_.length;
3004 const ary = this.screen_.rowsArray;
3005 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003006 ary[i].rowIndex = offset + i;
3007 }
3008 }
rginda8ba33642011-12-14 12:31:31 -08003009
rginda35c456b2012-02-09 17:29:05 -08003010 this.realizeWidth_(this.screenSize.width);
3011 this.realizeHeight_(this.screenSize.height);
3012 this.scrollPort_.syncScrollHeight();
3013 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003014
rginda6d397402012-01-17 10:58:29 -08003015 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003016 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003017};
3018
3019/**
3020 * Set the cursor-blink mode bit.
3021 *
3022 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3023 * a visible cursor does not blink.
3024 *
3025 * You should make sure to turn blinking off if you're going to dispose of a
3026 * terminal, otherwise you'll leak a timeout.
3027 *
3028 * Defaults to on.
3029 *
3030 * @param {boolean} state True to set cursor-blink mode, false to unset.
3031 */
3032hterm.Terminal.prototype.setCursorBlink = function(state) {
3033 this.options_.cursorBlink = state;
3034
3035 if (!state && this.timeouts_.cursorBlink) {
3036 clearTimeout(this.timeouts_.cursorBlink);
3037 delete this.timeouts_.cursorBlink;
3038 }
3039
Mike Frysingerbdb34802020-04-07 03:47:32 -04003040 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003041 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003042 }
rginda8ba33642011-12-14 12:31:31 -08003043};
3044
3045/**
3046 * Set the cursor-visible mode bit.
3047 *
3048 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3049 *
3050 * Defaults to on.
3051 *
3052 * @param {boolean} state True to set cursor-visible mode, false to unset.
3053 */
3054hterm.Terminal.prototype.setCursorVisible = function(state) {
3055 this.options_.cursorVisible = state;
3056
3057 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003058 if (this.timeouts_.cursorBlink) {
3059 clearTimeout(this.timeouts_.cursorBlink);
3060 delete this.timeouts_.cursorBlink;
3061 }
rginda87b86462011-12-14 13:48:03 -08003062 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003063 return;
3064 }
3065
rginda87b86462011-12-14 13:48:03 -08003066 this.syncCursorPosition_();
3067
3068 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003069
3070 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003071 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003072 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003073 }
rginda8ba33642011-12-14 12:31:31 -08003074
Robert Gindaea2183e2014-07-17 09:51:51 -07003075 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003076 } else {
3077 if (this.timeouts_.cursorBlink) {
3078 clearTimeout(this.timeouts_.cursorBlink);
3079 delete this.timeouts_.cursorBlink;
3080 }
3081 }
3082};
3083
3084/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003085 * Pause blinking temporarily.
3086 *
3087 * When the cursor moves around, it can be helpful to momentarily pause the
3088 * blinking. This could be when the user is typing in things, or when they're
3089 * moving around with the arrow keys.
3090 */
3091hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3092 if (!this.options_.cursorBlink) {
3093 return;
3094 }
3095
3096 this.cursorBlinkPause_ = true;
3097
3098 // If a timeout is already pending, reset the clock due to the new input.
3099 if (this.timeouts_.cursorBlinkPause) {
3100 clearTimeout(this.timeouts_.cursorBlinkPause);
3101 }
3102 // After 500ms, resume blinking. That seems like a good balance between user
3103 // input timings & responsiveness to resume.
3104 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3105 delete this.timeouts_.cursorBlinkPause;
3106 this.cursorBlinkPause_ = false;
3107 }, 500);
3108};
3109
3110/**
rginda87b86462011-12-14 13:48:03 -08003111 * Synchronizes the visible cursor and document selection with the current
3112 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003113 *
3114 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003115 */
3116hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003117 const topRowIndex = this.scrollPort_.getTopRowIndex();
3118 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3119 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003120 this.screen_.cursorPosition.row;
3121
Raymes Khoury15697f42018-07-17 11:37:18 +10003122 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003123 if (this.accessibilityReader_.accessibilityEnabled) {
3124 // Report the new position of the cursor for accessibility purposes.
3125 const cursorColumnIndex = this.screen_.cursorPosition.column;
3126 const cursorLineText =
3127 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003128 // This will force the selection to be sync'd to the cursor position if the
3129 // user has pressed a key. Generally we would only sync the cursor position
3130 // when selection is collapsed so that if the user has selected something
3131 // we don't clear the selection by moving the selection. However when a
3132 // screen reader is used, it's intuitive for entering a key to move the
3133 // selection to the cursor.
3134 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003135 this.accessibilityReader_.afterCursorChange(
3136 cursorLineText, cursorRowIndex, cursorColumnIndex);
3137 }
3138
rginda8ba33642011-12-14 12:31:31 -08003139 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003140 // Cursor is scrolled off screen, hide it.
3141 this.cursorOffScreen_ = true;
3142 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003143 return false;
rginda8ba33642011-12-14 12:31:31 -08003144 }
3145
Joel Hockey3babf302020-04-22 15:00:06 -07003146 if (this.cursorNode_.style.display == 'none') {
3147 // Re-display the terminal cursor if it was hidden.
3148 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003149 this.cursorNode_.style.display = '';
3150 }
3151
Mike Frysinger44c32202017-08-05 01:13:09 -04003152 // Position the cursor using CSS variable math. If we do the math in JS,
3153 // the float math will end up being more precise than the CSS which will
3154 // cause the cursor tracking to be off.
3155 this.setCssVar(
3156 'cursor-offset-row',
3157 `${cursorRowIndex - topRowIndex} + ` +
3158 `${this.scrollPort_.visibleRowTopMargin}px`);
3159 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003160
3161 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003162 '(' + this.screen_.cursorPosition.column +
3163 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003164 ')');
3165
3166 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003167 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003168 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003169 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003170 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003171 return true;
rginda8ba33642011-12-14 12:31:31 -08003172};
3173
Robert Gindafb1be6a2013-12-11 11:56:22 -08003174/**
3175 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3176 * and character cell dimensions.
3177 */
Robert Ginda830583c2013-08-07 13:20:46 -07003178hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003179 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003180
3181 if (this.cursorNode_.getAttribute('focus') == 'false') {
3182 // Always show a block cursor when unfocused.
3183 shape = hterm.Terminal.cursorShape.BLOCK;
3184 }
3185
Mike Frysingerdc727792020-04-10 01:41:13 -04003186 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003187
3188 switch (shape) {
3189 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003190 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003191 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003192 style.borderLeftStyle = 'solid';
3193 break;
3194
3195 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003196 style.backgroundColor = 'transparent';
3197 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003198 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003199 break;
3200
3201 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003202 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003203 style.borderBottomStyle = '';
3204 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003205 break;
3206 }
3207};
3208
rginda8ba33642011-12-14 12:31:31 -08003209/**
3210 * Synchronizes the visible cursor with the current cursor coordinates.
3211 *
3212 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003213 * Multiple calls will be coalesced into a single sync. This should be called
3214 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003215 */
3216hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003217 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003218 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003219 }
rginda8ba33642011-12-14 12:31:31 -08003220
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003221 if (this.accessibilityReader_.accessibilityEnabled) {
3222 // Report the previous position of the cursor for accessibility purposes.
3223 const cursorRowIndex = this.scrollbackRows_.length +
3224 this.screen_.cursorPosition.row;
3225 const cursorColumnIndex = this.screen_.cursorPosition.column;
3226 const cursorLineText =
3227 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3228 this.accessibilityReader_.beforeCursorChange(
3229 cursorLineText, cursorRowIndex, cursorColumnIndex);
3230 }
3231
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003232 this.timeouts_.syncCursor = setTimeout(() => {
3233 this.syncCursorPosition_();
3234 delete this.timeouts_.syncCursor;
3235 });
rginda87b86462011-12-14 13:48:03 -08003236};
3237
rgindacc2996c2012-02-24 14:59:31 -08003238/**
rgindaf522ce02012-04-17 17:49:17 -07003239 * Show or hide the zoom warning.
3240 *
3241 * The zoom warning is a message warning the user that their browser zoom must
3242 * be set to 100% in order for hterm to function properly.
3243 *
3244 * @param {boolean} state True to show the message, false to hide it.
3245 */
3246hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3247 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003248 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003249 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003250 }
rgindaf522ce02012-04-17 17:49:17 -07003251
3252 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003253 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003254 this.zoomWarningNode_.style.cssText = (
3255 'color: black;' +
3256 'background-color: #ff2222;' +
3257 'font-size: large;' +
3258 'border-radius: 8px;' +
3259 'opacity: 0.75;' +
3260 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3261 'top: 0.5em;' +
3262 'right: 1.2em;' +
3263 'position: absolute;' +
3264 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003265 '-webkit-user-select: none;' +
3266 '-moz-text-size-adjust: none;' +
3267 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003268
3269 this.zoomWarningNode_.addEventListener('click', function(e) {
3270 this.parentNode.removeChild(this);
3271 });
rgindaf522ce02012-04-17 17:49:17 -07003272 }
3273
Mike Frysingerb7289952019-03-23 16:05:38 -07003274 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003275 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003276 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003277
rgindaf522ce02012-04-17 17:49:17 -07003278 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3279
3280 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003281 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003282 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003283 }
rgindaf522ce02012-04-17 17:49:17 -07003284 } else if (this.zoomWarningNode_.parentNode) {
3285 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3286 }
3287};
3288
3289/**
rgindacc2996c2012-02-24 14:59:31 -08003290 * Show the terminal overlay for a given amount of time.
3291 *
Jason Lin3d825782020-05-12 11:02:48 +10003292 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003293 *
3294 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003295 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003296 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3297 * stay up forever (or until the next overlay).
3298 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003299hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003300 this.showOverlayWithNode(new Text(msg), timeout);
3301};
3302
3303/**
3304 * Show the terminal overlay for a given amount of time.
3305 *
3306 * The terminal overlay appears in inverse video, centered over the terminal.
3307 *
3308 * @param {!Node} node The node to display in the overlay.
3309 * @param {?number=} timeout The amount of time to wait before fading out
3310 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3311 * stay up forever (or until the next overlay).
3312 */
3313hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003314 if (!this.ready_ || !this.div_) {
3315 return;
3316 }
rgindaf0090c92012-02-10 14:58:52 -08003317
Joel Hockeyedac0e72020-05-14 20:16:20 -07003318 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003319 this.overlayNode_ = this.document_.createElement('div');
3320 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003321 'color: rgb(var(--hterm-background-color));' +
3322 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003323 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003324 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003325 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003326 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003327 'position: absolute;' +
3328 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003329 '-webkit-transition: opacity 180ms ease-in;' +
3330 '-moz-user-select: none;' +
3331 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003332
3333 this.overlayNode_.addEventListener('mousedown', function(e) {
3334 e.preventDefault();
3335 e.stopPropagation();
3336 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003337 }
3338
Jason Lin34567412020-05-14 10:32:09 +10003339 this.overlayNode_.textContent = ''; // Remove all children first.
3340 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003341
Mike Frysingerbdb34802020-04-07 03:47:32 -04003342 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003343 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003344 }
rgindaf0090c92012-02-10 14:58:52 -08003345
Mike Frysingerdc727792020-04-10 01:41:13 -04003346 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3347 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003348
Robert Ginda8a59f762014-07-23 11:29:55 -07003349 this.overlayNode_.style.top =
3350 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003351 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003352 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003353
Mike Frysingerbdb34802020-04-07 03:47:32 -04003354 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003355 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003356 }
rgindaf0090c92012-02-10 14:58:52 -08003357
Jason Lin34567412020-05-14 10:32:09 +10003358 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003359
Mike Frysingerec4225d2020-04-07 05:00:01 -04003360 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003361 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003362 }
rgindacc2996c2012-02-24 14:59:31 -08003363
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003364 this.overlayTimeout_ = setTimeout(() => {
3365 this.overlayNode_.style.opacity = '0';
3366 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003367 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003368};
3369
3370/**
3371 * Hide the terminal overlay immediately.
3372 *
3373 * Useful when we show an overlay for an event with an unknown end time.
3374 */
3375hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003376 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003377 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003378 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003379 this.overlayTimeout_ = null;
3380
Mike Frysingerbdb34802020-04-07 03:47:32 -04003381 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003382 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003383 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003384 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003385};
3386
rginda4bba5e12012-06-20 16:15:30 -07003387/**
3388 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003389 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003390 * Note: In Chrome, this should work unless the user has rejected the permission
3391 * request. In Firefox extension environment, you'll need the "clipboardRead"
3392 * permission. In other environments, this might always fail as the browser
3393 * frequently blocks access for security reasons.
3394 *
3395 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3396 * value is always null. Otherwise, this function uses legacy pasting and
3397 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003398 */
3399hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003400 if (!this.alwaysUseLegacyPasting &&
3401 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003402 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3403 return null;
3404 } else {
3405 // Legacy pasting.
3406 try {
3407 return this.document_.execCommand('paste');
3408 } catch (firefoxException) {
3409 // Ignore this. FF 40 and older would incorrectly throw an exception if
3410 // there was an error instead of returning false.
3411 return false;
3412 }
3413 }
rginda4bba5e12012-06-20 16:15:30 -07003414};
3415
3416/**
3417 * Copy a string to the system clipboard.
3418 *
3419 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003420 *
3421 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003422 */
3423hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003424 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003425 if (!this.clipboardNotice_) {
3426 this.clipboardNotice_ = this.document_.createElement('div');
3427 this.clipboardNotice_.style.textAlign = 'center';
3428 const copyImage = lib.resource.getData('hterm/images/copy');
3429 this.clipboardNotice_.innerHTML =
3430 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3431 }
3432 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003433 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003434
Mike Frysinger96eacae2019-01-02 18:13:56 -05003435 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003436};
3437
Evan Jones2600d4f2016-12-06 09:29:36 -05003438/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003439 * Display an image.
3440 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003441 * Either URI or buffer or blob fields must be specified.
3442 *
Joel Hockey0f933582019-08-27 18:01:51 -07003443 * @param {{
3444 * name: (string|undefined),
3445 * size: (string|number|undefined),
3446 * preserveAspectRation: (boolean|undefined),
3447 * inline: (boolean|undefined),
3448 * width: (string|number|undefined),
3449 * height: (string|number|undefined),
3450 * align: (string|undefined),
3451 * url: (string|undefined),
3452 * buffer: (!ArrayBuffer|undefined),
3453 * blob: (!Blob|undefined),
3454 * type: (string|undefined),
3455 * }} options The image to display.
3456 * name A human readable string for the image
3457 * size The size (in bytes).
3458 * preserveAspectRatio Whether to preserve aspect.
3459 * inline Whether to display the image inline.
3460 * width The width of the image.
3461 * height The height of the image.
3462 * align Direction to align the image.
3463 * uri The source URI for the image.
3464 * buffer The ArrayBuffer image data.
3465 * blob The Blob image data.
3466 * type The MIME type of the image data.
3467 * @param {function()=} onLoad Callback when loading finishes.
3468 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003469 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003470hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003471 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003472 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003473 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003474 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003475 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003476
3477 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003478 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003479 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003480 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003481
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003482 // See if the mime type is available. If not, guess from the filename.
3483 // We don't list all possible mime types because the browser can usually
3484 // guess it correctly. So list the ones that need a bit more help.
3485 if (!options.type) {
3486 const ary = options.name.split('.');
3487 const ext = ary[ary.length - 1].trim();
3488 switch (ext) {
3489 case 'svg':
3490 case 'svgz':
3491 options.type = 'image/svg+xml';
3492 break;
3493 }
3494 }
3495
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003496 // Has the user approved image display yet?
3497 if (this.allowImagesInline !== true) {
3498 this.newLine();
3499 const row = this.getRowNode(this.scrollbackRows_.length +
3500 this.getCursorRow() - 1);
3501
3502 if (this.allowImagesInline === false) {
3503 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3504 'Inline Images Disabled');
3505 return;
3506 }
3507
3508 // Show a prompt.
3509 let button;
3510 const span = this.document_.createElement('span');
3511 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3512 span.style.fontWeight = 'bold';
3513 span.style.borderWidth = '1px';
3514 span.style.borderStyle = 'dashed';
3515 button = this.document_.createElement('span');
3516 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3517 button.style.marginLeft = '1em';
3518 button.style.borderWidth = '1px';
3519 button.style.borderStyle = 'solid';
3520 button.addEventListener('click', () => {
3521 this.prefs_.set('allow-images-inline', false);
3522 });
3523 span.appendChild(button);
3524 button = this.document_.createElement('span');
3525 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3526 'allow this session');
3527 button.style.marginLeft = '1em';
3528 button.style.borderWidth = '1px';
3529 button.style.borderStyle = 'solid';
3530 button.addEventListener('click', () => {
3531 this.allowImagesInline = true;
3532 });
3533 span.appendChild(button);
3534 button = this.document_.createElement('span');
3535 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3536 button.style.marginLeft = '1em';
3537 button.style.borderWidth = '1px';
3538 button.style.borderStyle = 'solid';
3539 button.addEventListener('click', () => {
3540 this.prefs_.set('allow-images-inline', true);
3541 });
3542 span.appendChild(button);
3543
3544 row.appendChild(span);
3545 return;
3546 }
3547
3548 // See if we should show this object directly, or download it.
3549 if (options.inline) {
3550 const io = this.io.push();
3551 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003552 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003553
3554 // While we're loading the image, eat all the user's input.
3555 io.onVTKeystroke = io.sendString = () => {};
3556
3557 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003558 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003559 if (options.uri !== undefined) {
3560 img.src = options.uri;
3561 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003562 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003563 img.src = URL.createObjectURL(blob);
3564 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003565 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003566 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003567 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003568 img.title = img.alt = options.name;
3569
3570 // Attach the image to the page to let it load/render. It won't stay here.
3571 // This is needed so it's visible and the DOM can calculate the height. If
3572 // the image is hidden or not in the DOM, the height is always 0.
3573 this.document_.body.appendChild(img);
3574
3575 // Wait for the image to finish loading before we try moving it to the
3576 // right place in the terminal.
3577 img.onload = () => {
3578 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003579 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003580 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003581 img.style.maxWidth = `${screenSize.width}px`;
3582 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003583
3584 // Parse a width/height specification.
3585 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003586 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003588 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003589
3590 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3591 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003592 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003593 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003594 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003595 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003596 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003597 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003598 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003599 }
3600
3601 return '';
3602 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003603 img.style.width = parseDim(
3604 options.width, screenSize.width, '--hterm-charsize-width');
3605 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003606 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003607
3608 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003609 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003610 const padRows = Math.ceil(img.clientHeight /
3611 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003612 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003613 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003614 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003615
3616 // Update the max height in case the user shrinks the character size.
3617 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3618
3619 // Move the image to the last row. This way when we scroll up, it doesn't
3620 // disappear when the first row gets clipped. It will disappear when we
3621 // scroll down and the last row is clipped ...
3622 this.document_.body.removeChild(img);
3623 // Create a wrapper node so we can do an absolute in a relative position.
3624 // This helps with rounding errors between JS & CSS counts.
3625 const div = this.document_.createElement('div');
3626 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003627 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003628 img.style.position = 'absolute';
3629 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3630 div.appendChild(img);
3631 const row = this.getRowNode(this.scrollbackRows_.length +
3632 this.getCursorRow() - 1);
3633 row.appendChild(div);
3634
Mike Frysinger2558ed52019-01-14 01:03:41 -05003635 // Now that the image has been read, we can revoke the source.
3636 if (options.uri === undefined) {
3637 URL.revokeObjectURL(img.src);
3638 }
3639
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003640 io.hideOverlay();
3641 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003642
Mike Frysingerbdb34802020-04-07 03:47:32 -04003643 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003644 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003645 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003646 };
3647
3648 // If we got a malformed image, give up.
3649 img.onerror = (e) => {
3650 this.document_.body.removeChild(img);
3651 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003652 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003653 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003654
Mike Frysingerbdb34802020-04-07 03:47:32 -04003655 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003656 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003657 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003658 };
3659 } else {
3660 // We can't use chrome.downloads.download as that requires "downloads"
3661 // permissions, and that works only in extensions, not apps.
3662 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003663 if (options.uri !== undefined) {
3664 a.href = options.uri;
3665 } else if (options.buffer !== undefined) {
3666 const blob = new Blob([options.buffer]);
3667 a.href = URL.createObjectURL(blob);
3668 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003669 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003670 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003671 a.download = options.name;
3672 this.document_.body.appendChild(a);
3673 a.click();
3674 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003675 if (options.uri === undefined) {
3676 URL.revokeObjectURL(a.href);
3677 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003678 }
3679};
3680
3681/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003682 * Returns the selected text, or null if no text is selected.
3683 *
3684 * @return {string|null}
3685 */
rgindaa09e7332012-08-17 12:49:51 -07003686hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003687 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003688 selection.sync();
3689
Mike Frysingerbdb34802020-04-07 03:47:32 -04003690 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003691 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003692 }
rgindaa09e7332012-08-17 12:49:51 -07003693
rgindaa09e7332012-08-17 12:49:51 -07003694 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003695 let startOffset = selection.startOffset;
3696 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003697
Raymes Khoury334625a2018-06-25 10:29:40 +10003698 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003699 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003700 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003701 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003702
Robert Gindafdbb3f22012-09-06 20:23:06 -07003703 if (node.nodeName != 'X-ROW') {
3704 // If the selection doesn't start on an x-row node, then it must be
3705 // somewhere inside the x-row. Add any characters from previous siblings
3706 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003707
3708 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3709 // If node is the text node in a styled span, move up to the span node.
3710 node = node.parentNode;
3711 }
3712
Robert Gindafdbb3f22012-09-06 20:23:06 -07003713 while (node.previousSibling) {
3714 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003715 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003716 }
rgindaa09e7332012-08-17 12:49:51 -07003717 }
3718
3719 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003720 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003721 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003722 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003723
Robert Gindafdbb3f22012-09-06 20:23:06 -07003724 if (node.nodeName != 'X-ROW') {
3725 // If the selection doesn't end on an x-row node, then it must be
3726 // somewhere inside the x-row. Add any characters from following siblings
3727 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003728
3729 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3730 // If node is the text node in a styled span, move up to the span node.
3731 node = node.parentNode;
3732 }
3733
Robert Gindafdbb3f22012-09-06 20:23:06 -07003734 while (node.nextSibling) {
3735 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003736 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003737 }
rgindaa09e7332012-08-17 12:49:51 -07003738 }
3739
Mike Frysingerdc727792020-04-10 01:41:13 -04003740 const rv = this.getRowsText(selection.startRow.rowIndex,
3741 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003742 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003743};
3744
rginda4bba5e12012-06-20 16:15:30 -07003745/**
3746 * Copy the current selection to the system clipboard, then clear it after a
3747 * short delay.
3748 */
3749hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003750 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003751 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003752 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003753 }
rginda4bba5e12012-06-20 16:15:30 -07003754};
3755
Joel Hockey0f933582019-08-27 18:01:51 -07003756/**
3757 * Show overlay with current terminal size.
3758 */
rgindaf0090c92012-02-10 14:58:52 -08003759hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003760 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003761 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003762 }
rgindaf0090c92012-02-10 14:58:52 -08003763};
3764
rginda87b86462011-12-14 13:48:03 -08003765/**
3766 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3767 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003768 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003769 */
3770hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003771 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003772 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003773 }
rginda87b86462011-12-14 13:48:03 -08003774
Mike Frysinger225c99d2019-10-20 14:02:37 -06003775 this.pauseCursorBlink_();
3776
Mike Frysinger79669762018-12-30 20:51:10 -05003777 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003778};
3779
3780/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003781 * Open the selected url.
3782 */
3783hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003784 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003785
3786 // If there is no selection, try and expand wherever they clicked.
3787 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003788 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003789 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003790
3791 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003792 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003793 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003794 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003795 }
3796
3797 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003798 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003799 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003800 }
Mike Frysinger43472622017-06-26 18:11:07 -04003801
3802 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003803 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003804 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3805 // We have to whitelist a few protocols that lack authorities and thus
3806 // never use the //. Like mailto.
3807 switch (str.split(':', 1)[0]) {
3808 case 'mailto':
3809 break;
3810 default:
3811 str = 'http://' + str;
3812 break;
3813 }
3814 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003815
Mike Frysinger720fa832017-10-23 01:15:52 -04003816 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003817};
Mike Frysinger70b94692017-01-26 18:57:50 -10003818
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003819/**
3820 * Manage the automatic mouse hiding behavior while typing.
3821 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003822 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003823 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003824hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003825 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3826 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003827 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003828 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003829 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003830
3831 this.mouseHideWhileTyping_ = !!v;
3832};
3833
3834/**
3835 * Handler for monitoring user keyboard activity.
3836 *
3837 * This isn't for processing the keystrokes directly, but for updating any
3838 * state that might toggle based on the user using the keyboard at all.
3839 *
Joel Hockey0f933582019-08-27 18:01:51 -07003840 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003841 */
3842hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3843 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003844 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003845 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003846 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003847};
Mike Frysinger70b94692017-01-26 18:57:50 -10003848
3849/**
rgindad5613292012-06-19 15:40:37 -07003850 * Add the terminalRow and terminalColumn properties to mouse events and
3851 * then forward on to onMouse().
3852 *
3853 * The terminalRow and terminalColumn properties contain the (row, column)
3854 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003855 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003856 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003857 */
3858hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003859 if (e.processedByTerminalHandler_) {
3860 // We register our event handlers on the document, as well as the cursor
3861 // and the scroll blocker. Mouse events that occur on the cursor or
3862 // scroll blocker will also appear on the document, but we don't want to
3863 // process them twice.
3864 //
3865 // We can't just prevent bubbling because that has other side effects, so
3866 // we decorate the event object with this property instead.
3867 return;
3868 }
3869
Mike Frysinger468966c2018-08-28 13:48:51 -04003870 // Consume navigation events. Button 3 is usually "browser back" and
3871 // button 4 is "browser forward" which we don't want to happen.
3872 if (e.button > 2) {
3873 e.preventDefault();
3874 // We don't return so click events can be passed to the remote below.
3875 }
3876
Mike Frysingerdc727792020-04-10 01:41:13 -04003877 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003878 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3879
rgindafaa74742012-08-21 13:34:03 -07003880 e.processedByTerminalHandler_ = true;
3881
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003882 // Handle auto hiding of mouse cursor while typing.
3883 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3884 // Make sure the mouse cursor is visible.
3885 this.syncMouseStyle();
3886 // This debounce isn't perfect, but should work well enough for such a
3887 // simple implementation. If the user moved the mouse, we enabled this
3888 // debounce, and then moved the mouse just before the timeout, we wouldn't
3889 // debounce that later movement.
3890 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3891 }
3892
Robert Gindaeda48db2014-07-17 09:25:30 -07003893 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003894 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003895 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003896 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003897 this.scrollPort_.characterSize.height) + 1;
3898 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003899 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003900
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003901 // Clamp row and column.
3902 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3903 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3904
3905 // Ignore mousedown in the scrollbar area.
3906 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003907 return;
3908 }
3909
Joel Hockey3babf302020-04-22 15:00:06 -07003910 if (this.options_.cursorVisible && !reportMouseEvents &&
3911 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003912 // If the cursor is visible and we're not sending mouse events to the
3913 // host app, then we want to hide the terminal cursor when the mouse
3914 // cursor is over top. This keeps the terminal cursor from interfering
3915 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003916 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3917 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3918 this.cursorNode_.style.display = 'none';
3919 } else if (this.cursorNode_.style.display == 'none') {
3920 this.cursorNode_.style.display = '';
3921 }
3922 }
rgindad5613292012-06-19 15:40:37 -07003923
Robert Ginda928cf632014-03-05 15:07:41 -08003924 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003925 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003926
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003927 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003928 // If VT mouse reporting is disabled, or has been defeated with
3929 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003930 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003931 this.setSelectionEnabled(true);
3932 } else {
3933 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003934 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003935 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003936 this.setSelectionEnabled(false);
3937 e.preventDefault();
3938 }
3939 }
3940
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003941 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003942 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003943 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003944 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003945 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003946 }
rgindad5613292012-06-19 15:40:37 -07003947 }
3948
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003949 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003950 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003951 // Ignore links created using OSC-8 as those will open by themselves, and
3952 // the visible text is most likely not the URI they want anyways.
3953 if (e.target.className === 'uri-node') {
3954 return;
3955 }
3956
Mike Frysinger70b94692017-01-26 18:57:50 -10003957 // Debounce this event with the dblclick event. If you try to doubleclick
3958 // a URL to open it, Chrome will fire click then dblclick, but we won't
3959 // have expanded the selection text at the first click event.
3960 clearTimeout(this.timeouts_.openUrl);
3961 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3962 500);
3963 return;
3964 }
3965
Mike Frysinger847577f2017-05-23 23:25:57 -04003966 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003967 if (e.ctrlKey && e.button == 2 /* right button */) {
3968 e.preventDefault();
3969 this.contextMenu.show(e, this);
3970 } else if (e.button == this.mousePasteButton ||
3971 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003972 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003973 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003974 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003975 }
3976 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003977
Mike Frysinger2edd3612017-05-24 00:54:39 -04003978 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003979 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003980 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003981 }
3982
3983 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3984 this.scrollBlockerNode_.engaged) {
3985 // Disengage the scroll-blocker after one of these events.
3986 this.scrollBlockerNode_.engaged = false;
3987 this.scrollBlockerNode_.style.top = '-99px';
3988 }
3989
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003990 // Emulate arrow key presses via scroll wheel events.
3991 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3992 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003993 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003994 const delta =
3995 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003996
Mike Frysinger321063c2018-08-29 15:33:14 -04003997 // Helper to turn a wheel event delta into a series of key presses.
3998 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3999 if (distance == 0) {
4000 return '';
4001 }
4002
4003 // Convert the scroll distance into a number of rows/cols.
4004 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4005 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4006 return data.repeat(cells);
4007 };
4008
4009 // The order between up/down and left/right doesn't really matter.
4010 this.io.sendString(
4011 // Up/down arrow keys.
4012 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4013 'A', 'B') +
4014 // Left/right arrow keys.
4015 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004016 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004017 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004018
4019 e.preventDefault();
4020 }
4021 }
Robert Ginda928cf632014-03-05 15:07:41 -08004022 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004023 if (!this.scrollBlockerNode_.engaged) {
4024 if (e.type == 'mousedown') {
4025 // Move the scroll-blocker into place if we want to keep the scrollport
4026 // from scrolling.
4027 this.scrollBlockerNode_.engaged = true;
4028 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4029 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4030 } else if (e.type == 'mousemove') {
4031 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4032 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004033 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004034 e.preventDefault();
4035 }
4036 }
Robert Ginda928cf632014-03-05 15:07:41 -08004037
4038 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004039 }
4040
Robert Ginda928cf632014-03-05 15:07:41 -08004041 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4042 // Restore this on mouseup in case it was temporarily defeated with a
4043 // alt-mousedown. Only do this when the selection is empty so that
4044 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004045 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004046 }
rgindad5613292012-06-19 15:40:37 -07004047};
4048
4049/**
4050 * Clients should override this if they care to know about mouse events.
4051 *
4052 * The event parameter will be a normal DOM mouse click event with additional
4053 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004054 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004055 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004056 */
4057hterm.Terminal.prototype.onMouse = function(e) { };
4058
4059/**
rginda8e92a692012-05-20 19:37:20 -07004060 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004061 *
4062 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004063 */
Rob Spies06533ba2014-04-24 11:20:37 -07004064hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4065 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004066 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004067
Mike Frysingerbdb34802020-04-07 03:47:32 -04004068 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004069 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004070 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004071
Mike Frysingerbdb34802020-04-07 03:47:32 -04004072 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004073 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004074 }
rginda8e92a692012-05-20 19:37:20 -07004075};
4076
4077/**
rginda8ba33642011-12-14 12:31:31 -08004078 * React when the ScrollPort is scrolled.
4079 */
4080hterm.Terminal.prototype.onScroll_ = function() {
4081 this.scheduleSyncCursorPosition_();
4082};
4083
4084/**
rginda9846e2f2012-01-27 13:53:33 -08004085 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004086 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004087 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004088 */
4089hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004090 this.onPasteData_(e.text);
4091};
4092
4093/**
4094 * Handle pasted data.
4095 *
4096 * @param {string} data The pasted data.
4097 */
4098hterm.Terminal.prototype.onPasteData_ = function(data) {
4099 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004100 if (this.options_.bracketedPaste) {
4101 // We strip out most escape sequences as they can cause issues (like
4102 // inserting an \x1b[201~ midstream). We pass through whitespace
4103 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4104 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004105 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004106 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4107 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4108 }
Robert Gindaa063b202014-07-21 11:08:25 -07004109
4110 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004111};
4112
4113/**
rgindaa09e7332012-08-17 12:49:51 -07004114 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004115 *
Joel Hockey0f933582019-08-27 18:01:51 -07004116 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004117 */
4118hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004119 if (!this.useDefaultWindowCopy) {
4120 e.preventDefault();
4121 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4122 }
rgindaa09e7332012-08-17 12:49:51 -07004123};
4124
4125/**
rginda8ba33642011-12-14 12:31:31 -08004126 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004127 *
4128 * Note: This function should not directly contain code that alters the internal
4129 * state of the terminal. That kind of code belongs in realizeWidth or
4130 * realizeHeight, so that it can be executed synchronously in the case of a
4131 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004132 */
4133hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004134 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4135 this.scrollPort_.characterSize.width) || 0;
4136 const rowCount = lib.f.smartFloorDivide(
4137 this.scrollPort_.getScreenHeight(),
4138 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004139
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004140 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004141 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004142 // gets removed from the document or during the initial load, and we can't
4143 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004144 // This can also happen if called before the scrollPort calculates the
4145 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004146 return;
4147 }
4148
Mike Frysingerdc727792020-04-10 01:41:13 -04004149 const isNewSize = (columnCount != this.screenSize.width ||
4150 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004151 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004152
4153 // We do this even if the size didn't change, just to be sure everything is
4154 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004155 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004156 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004157
Mike Frysingerbdb34802020-04-07 03:47:32 -04004158 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004159 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004160 }
rgindaa8ba17d2012-08-15 14:41:10 -07004161
Robert Gindafb1be6a2013-12-11 11:56:22 -08004162 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004163 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004164
4165 if (wasScrolledEnd) {
4166 this.scrollEnd();
4167 }
rginda8ba33642011-12-14 12:31:31 -08004168};
4169
4170/**
4171 * Service the cursor blink timeout.
4172 */
4173hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004174 if (!this.options_.cursorBlink) {
4175 delete this.timeouts_.cursorBlink;
4176 return;
4177 }
4178
Robert Ginda830583c2013-08-07 13:20:46 -07004179 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004180 this.cursorNode_.style.opacity == '0' ||
4181 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004182 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004183 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4184 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004185 } else {
rginda87b86462011-12-14 13:48:03 -08004186 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004187 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4188 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004189 }
4190};
David Reveman8f552492012-03-28 12:18:41 -04004191
4192/**
4193 * Set the scrollbar-visible mode bit.
4194 *
4195 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4196 * Otherwise it will not.
4197 *
4198 * Defaults to on.
4199 *
4200 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4201 */
4202hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4203 this.scrollPort_.setScrollbarVisible(state);
4204};
Michael Kelly485ecd12014-06-09 11:41:56 -04004205
4206/**
Rob Spies49039e52014-12-17 13:40:04 -08004207 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004208 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004209 *
4210 * Defaults to 1.
4211 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004212 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004213 */
4214hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4215 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4216};
4217
4218/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004219 * Close all web notifications created by terminal bells.
4220 */
4221hterm.Terminal.prototype.closeBellNotifications_ = function() {
4222 this.bellNotificationList_.forEach(function(n) {
4223 n.close();
4224 });
4225 this.bellNotificationList_.length = 0;
4226};
Raymes Khourye5d48982018-08-02 09:08:32 +10004227
4228/**
4229 * Syncs the cursor position when the scrollport gains focus.
4230 */
4231hterm.Terminal.prototype.onScrollportFocus_ = function() {
4232 // If the cursor is offscreen we set selection to the last row on the screen.
4233 const topRowIndex = this.scrollPort_.getTopRowIndex();
4234 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4235 const selection = this.document_.getSelection();
4236 if (!this.syncCursorPosition_() && selection) {
4237 selection.collapse(this.getRowNode(bottomRowIndex));
4238 }
4239};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004240
4241/**
4242 * Clients can override this if they want to provide an options page.
4243 */
4244hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4245
4246
4247/**
4248 * Called when user selects to open the options page.
4249 */
4250hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4251 this.onOpenOptionsPage();
4252};