blob: 0a14da8a939505f8228dbdd3650f5f0c8ad62e3b [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
shivanggarga72e8ba2020-07-16 07:11:17 +0530491 'find-result-selected-color': function(v) {
492 terminal.findBar.setFindResultSelectedColor(v);
493 },
494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 'font-family': function(v) {
496 terminal.syncFontFamily();
497 },
rginda30f20f62012-04-05 16:36:19 -0700498
Robert Ginda57f03b42012-09-13 11:02:48 -0700499 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700500 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700501 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500502 console.error(`Invalid font size: ${v}`);
503 return;
504 }
505
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 terminal.setFontSize(v);
507 },
rginda9875d902012-08-20 16:21:57 -0700508
Robert Ginda57f03b42012-09-13 11:02:48 -0700509 'font-smoothing': function(v) {
510 terminal.syncFontFamily();
511 },
rgindade84e382012-04-20 15:39:31 -0700512
Robert Ginda57f03b42012-09-13 11:02:48 -0700513 'foreground-color': function(v) {
514 terminal.setForegroundColor(v);
515 },
rginda30f20f62012-04-05 16:36:19 -0700516
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400517 'hide-mouse-while-typing': function(v) {
518 terminal.setAutomaticMouseHiding(v);
519 },
520
Robert Ginda57f03b42012-09-13 11:02:48 -0700521 'home-keys-scroll': function(v) {
522 terminal.keyboard.homeKeysScroll = v;
523 },
rginda4bba5e12012-06-20 16:15:30 -0700524
Robert Gindaa8165692015-06-15 14:46:31 -0700525 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700526 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
527 },
Robert Gindaa8165692015-06-15 14:46:31 -0700528
Joel Hockey95a9e272020-03-16 21:19:53 -0700529 'keybindings-os-defaults': function(v) {
530 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700531 },
532
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700533 'media-keys-are-fkeys': function(v) {
534 terminal.keyboard.mediaKeysAreFKeys = v;
535 },
536
Robert Ginda57f03b42012-09-13 11:02:48 -0700537 'meta-sends-escape': function(v) {
538 terminal.keyboard.metaSendsEscape = v;
539 },
rginda30f20f62012-04-05 16:36:19 -0700540
Mike Frysinger847577f2017-05-23 23:25:57 -0400541 'mouse-right-click-paste': function(v) {
542 terminal.mouseRightClickPaste = v;
543 },
544
Robert Ginda57f03b42012-09-13 11:02:48 -0700545 'mouse-paste-button': function(v) {
546 terminal.syncMousePasteButton();
547 },
rgindaa8ba17d2012-08-15 14:41:10 -0700548
Robert Gindae76aa9f2014-03-14 12:29:12 -0700549 'page-keys-scroll': function(v) {
550 terminal.keyboard.pageKeysScroll = v;
551 },
552
Robert Ginda40932892012-12-10 17:26:40 -0800553 'pass-alt-number': function(v) {
554 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700555 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800556 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500557 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800558 }
559
560 terminal.passAltNumber = v;
561 },
562
563 'pass-ctrl-number': function(v) {
564 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700565 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800566 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500567 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800568 }
569
570 terminal.passCtrlNumber = v;
571 },
572
Joel Hockey0e052042020-02-19 05:37:19 -0800573 'pass-ctrl-n': function(v) {
574 terminal.passCtrlN = v;
575 },
576
577 'pass-ctrl-t': function(v) {
578 terminal.passCtrlT = v;
579 },
580
581 'pass-ctrl-tab': function(v) {
582 terminal.passCtrlTab = v;
583 },
584
585 'pass-ctrl-w': function(v) {
586 terminal.passCtrlW = v;
587 },
588
Robert Ginda40932892012-12-10 17:26:40 -0800589 'pass-meta-number': function(v) {
590 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700591 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800592 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500593 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800594 }
595
596 terminal.passMetaNumber = v;
597 },
598
Marius Schilder77857b32014-05-14 16:21:26 -0700599 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700600 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700601 },
602
Robert Ginda8cb7d902013-06-20 14:37:18 -0700603 'receive-encoding': function(v) {
604 if (!(/^(utf-8|raw)$/).test(v)) {
605 console.warn('Invalid value for "receive-encoding": ' + v);
606 v = 'utf-8';
607 }
608
609 terminal.vt.characterEncoding = v;
610 },
611
Joel Hockey139d82d2020-04-07 23:04:29 -0700612 'screen-padding-size': function(v) {
613 v = parseInt(v, 10);
614 if (isNaN(v) || v < 0) {
615 console.error(`Invalid screen padding size: ${v}`);
616 return;
617 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700618 terminal.setScreenPaddingSize(v);
619 },
620
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700621 'screen-border-size': function(v) {
622 v = parseInt(v, 10);
623 if (isNaN(v) || v < 0) {
624 console.error(`Invalid screen border size: ${v}`);
625 return;
626 }
627 terminal.setScreenBorderSize(v);
628 },
629
630 'screen-border-color': function(v) {
631 terminal.div_.style.borderColor = v;
632 },
633
Robert Ginda57f03b42012-09-13 11:02:48 -0700634 'scroll-on-keystroke': function(v) {
635 terminal.scrollOnKeystroke_ = v;
636 },
rginda9f5222b2012-03-05 11:53:28 -0800637
Robert Ginda57f03b42012-09-13 11:02:48 -0700638 'scroll-on-output': function(v) {
639 terminal.scrollOnOutput_ = v;
640 },
rginda30f20f62012-04-05 16:36:19 -0700641
Robert Ginda57f03b42012-09-13 11:02:48 -0700642 'scrollbar-visible': function(v) {
643 terminal.setScrollbarVisible(v);
644 },
rginda9f5222b2012-03-05 11:53:28 -0800645
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400646 'scroll-wheel-may-send-arrow-keys': function(v) {
647 terminal.scrollWheelArrowKeys_ = v;
648 },
649
Rob Spies49039e52014-12-17 13:40:04 -0800650 'scroll-wheel-move-multiplier': function(v) {
651 terminal.setScrollWheelMoveMultipler(v);
652 },
653
Robert Ginda57f03b42012-09-13 11:02:48 -0700654 'shift-insert-paste': function(v) {
655 terminal.keyboard.shiftInsertPaste = v;
656 },
rginda9f5222b2012-03-05 11:53:28 -0800657
Mike Frysingera7768922017-07-28 15:00:12 -0400658 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400659 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400660 },
661
Robert Gindae76aa9f2014-03-14 12:29:12 -0700662 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400663 terminal.scrollPort_.setUserCssUrl(v);
664 },
665
666 'user-css-text': function(v) {
667 terminal.scrollPort_.setUserCssText(v);
668 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400669
670 'word-break-match-left': function(v) {
671 terminal.primaryScreen_.wordBreakMatchLeft = v;
672 terminal.alternateScreen_.wordBreakMatchLeft = v;
673 },
674
675 'word-break-match-right': function(v) {
676 terminal.primaryScreen_.wordBreakMatchRight = v;
677 terminal.alternateScreen_.wordBreakMatchRight = v;
678 },
679
680 'word-break-match-middle': function(v) {
681 terminal.primaryScreen_.wordBreakMatchMiddle = v;
682 terminal.alternateScreen_.wordBreakMatchMiddle = v;
683 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400684
685 'allow-images-inline': function(v) {
686 terminal.allowImagesInline = v;
687 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700688 });
rginda30f20f62012-04-05 16:36:19 -0700689
Robert Ginda57f03b42012-09-13 11:02:48 -0700690 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800691 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700692
Mike Frysingerec4225d2020-04-07 05:00:01 -0400693 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700694 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400695 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400696 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700697 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800698};
699
Rob Spies56953412014-04-28 14:09:47 -0700700/**
701 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500702 *
Joel Hockey0f933582019-08-27 18:01:51 -0700703 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700704 */
705hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700706 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700707};
708
Robert Gindaa063b202014-07-21 11:08:25 -0700709/**
710 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500711 *
712 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700713 */
714hterm.Terminal.prototype.setBracketedPaste = function(state) {
715 this.options_.bracketedPaste = state;
716};
Rob Spies56953412014-04-28 14:09:47 -0700717
rginda8e92a692012-05-20 19:37:20 -0700718/**
719 * Set the color for the cursor.
720 *
721 * If you want this setting to persist, set it through prefs_, rather than
722 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500723 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500724 * @param {string=} color The color to set. If not defined, we reset to the
725 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700726 */
727hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400728 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700729 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400730 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500731
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400732 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700733};
734
735/**
736 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400737 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500738 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700739 */
740hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400741 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700742};
743
744/**
rgindad5613292012-06-19 15:40:37 -0700745 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500746 *
747 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700748 */
749hterm.Terminal.prototype.setSelectionEnabled = function(state) {
750 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700751};
752
753/**
rginda8e92a692012-05-20 19:37:20 -0700754 * Set the background color.
755 *
756 * If you want this setting to persist, set it through prefs_, rather than
757 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500758 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500759 * @param {string=} color The color to set. If not defined, we reset to the
760 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700761 */
762hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400763 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700764 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400765 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500766
Joel Hockey42dba8f2020-03-26 16:21:11 -0700767 this.backgroundColor_ = lib.colors.normalizeCSS(color);
768 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700769};
770
rginda9f5222b2012-03-05 11:53:28 -0800771/**
772 * Return the current terminal background color.
773 *
774 * Intended for use by other classes, so we don't have to expose the entire
775 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500776 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700777 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800778 */
779hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700780 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700781};
782
783/**
784 * Set the foreground color.
785 *
786 * If you want this setting to persist, set it through prefs_, rather than
787 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500788 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500789 * @param {string=} color The color to set. If not defined, we reset to the
790 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700791 */
792hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400793 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700794 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400795 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500796
Joel Hockey42dba8f2020-03-26 16:21:11 -0700797 this.foregroundColor_ = lib.colors.normalizeCSS(color);
798 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800799};
800
801/**
802 * Return the current terminal foreground color.
803 *
804 * Intended for use by other classes, so we don't have to expose the entire
805 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500806 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700807 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800808 */
809hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700810 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800811};
812
813/**
rginda87b86462011-12-14 13:48:03 -0800814 * Create a new instance of a terminal command and run it with a given
815 * argument string.
816 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700817 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700818 * @param {string} commandName The command to run for this terminal.
819 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800820 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700821hterm.Terminal.prototype.runCommandClass = function(
822 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400823 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400824 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700825 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400826 }
rgindaf522ce02012-04-17 17:49:17 -0700827
rginda87b86462011-12-14 13:48:03 -0800828 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700829 {
830 commandName: commandName,
831 args: args,
rginda87b86462011-12-14 13:48:03 -0800832 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700833 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400834 onExit: (code) => {
835 this.io.pop();
836 this.uninstallKeyboard();
837 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
838 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400839 window.close();
840 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400841 },
rginda87b86462011-12-14 13:48:03 -0800842 });
843
rgindafeaf3142012-01-31 15:14:20 -0800844 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800845 this.command.run();
846};
847
848/**
rgindafeaf3142012-01-31 15:14:20 -0800849 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500850 *
851 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800852 */
853hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700854 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800855};
856
857/**
858 * Install the keyboard handler for this terminal.
859 *
860 * This will prevent the browser from seeing any keystrokes sent to the
861 * terminal.
862 */
863hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700864 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400865};
rgindafeaf3142012-01-31 15:14:20 -0800866
867/**
868 * Uninstall the keyboard handler for this terminal.
869 */
870hterm.Terminal.prototype.uninstallKeyboard = function() {
871 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400872};
rgindafeaf3142012-01-31 15:14:20 -0800873
874/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400875 * Set a CSS variable.
876 *
877 * Normally this is used to set variables in the hterm namespace.
878 *
879 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700880 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400881 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400882 */
883hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400884 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400885 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400886 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400887};
888
889/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700890 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
891 * input is valid.
892 *
893 * @param {string} name The variable to set.
894 * @param {?string} rgb The rgb value to assign to the variable.
895 */
896hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
897 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
898 if (ary) {
899 this.setCssVar(name, ary.slice(0, 3).join(','));
900 }
901};
902
903/**
904 * Sets the specified color for the active screen.
905 *
906 * @param {number} i The index into the 256 color palette to set.
907 * @param {?string} rgb The rgb value to assign to the variable.
908 */
909hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
910 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
911 this.setRgbColorCssVar(`color-${i}`, rgb);
912 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
913 }
914};
915
916/**
917 * Returns the current value in the active screen of the specified color.
918 *
919 * @param {number} i Color palette index.
920 * @return {string} rgb color.
921 */
922hterm.Terminal.prototype.getColorPalette = function(i) {
923 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
924 lib.colors.colorPalette[i];
925};
926
927/**
928 * Reset the specified color in the active screen to its default value.
929 *
930 * @param {number} i Color to reset
931 */
932hterm.Terminal.prototype.resetColor = function(i) {
933 this.setColorPalette(i, lib.colors.colorPalette[i]);
934 delete this.screen_.textAttributes.colorPaletteOverrides[i];
935};
936
937/**
938 * Reset the current screen color palette to the default state.
939 */
940hterm.Terminal.prototype.resetColorPalette = function() {
941 this.screen_.textAttributes.colorPaletteOverrides.forEach(
942 (c, i) => this.resetColor(i));
943};
944
945/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500946 * Get a CSS variable.
947 *
948 * Normally this is used to get variables in the hterm namespace.
949 *
950 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400951 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500952 * @return {string} The current setting for this variable.
953 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400954hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500955 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400956 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500957};
958
959/**
shivanggargf3b362a2020-07-10 11:06:35 +0530960 * @return {!hterm.ScrollPort}
961 */
962hterm.Terminal.prototype.getScrollPort = function() {
963 return this.scrollPort_;
964};
965
966/**
Jason Linbbbdb752020-03-06 16:26:59 +1100967 * Update CSS character size variables to match the scrollport.
968 */
969hterm.Terminal.prototype.updateCssCharsize_ = function() {
970 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
971 this.setCssVar('charsize-height',
972 this.scrollPort_.characterSize.height + 'px');
973};
974
975/**
rginda35c456b2012-02-09 17:29:05 -0800976 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800977 *
978 * Call setFontSize(0) to reset to the default font size.
979 *
980 * This function does not modify the font-size preference.
981 *
982 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800983 */
984hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400985 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700986 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400987 }
rginda9f5222b2012-03-05 11:53:28 -0800988
rginda35c456b2012-02-09 17:29:05 -0800989 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -0700990 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +1100991 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800992};
993
994/**
995 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500996 *
997 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800998 */
999hterm.Terminal.prototype.getFontSize = function() {
1000 return this.scrollPort_.getFontSize();
1001};
1002
1003/**
rginda8e92a692012-05-20 19:37:20 -07001004 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -05001005 *
1006 * @return {string}
rginda8e92a692012-05-20 19:37:20 -07001007 */
1008hterm.Terminal.prototype.getFontFamily = function() {
1009 return this.scrollPort_.getFontFamily();
1010};
1011
1012/**
rginda35c456b2012-02-09 17:29:05 -08001013 * Set the CSS "font-family" for this terminal.
1014 */
rginda9f5222b2012-03-05 11:53:28 -08001015hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001016 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1017 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001018 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001019 this.syncBoldSafeState();
1020};
1021
rginda4bba5e12012-06-20 16:15:30 -07001022/**
1023 * Set this.mousePasteButton based on the mouse-paste-button pref,
1024 * autodetecting if necessary.
1025 */
1026hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001027 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001028 if (typeof button == 'number') {
1029 this.mousePasteButton = button;
1030 return;
1031 }
1032
Mike Frysingeree81a002017-12-12 16:14:53 -05001033 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001034 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001035 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001036 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001037 }
1038};
1039
1040/**
1041 * Enable or disable bold based on the enable-bold pref, autodetecting if
1042 * necessary.
1043 */
rginda9f5222b2012-03-05 11:53:28 -08001044hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001045 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001046 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001047 this.primaryScreen_.textAttributes.enableBold = enableBold;
1048 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001049 return;
1050 }
1051
Mike Frysingerdc727792020-04-10 01:41:13 -04001052 const normalSize = this.scrollPort_.measureCharacterSize();
1053 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001054
Mike Frysingerdc727792020-04-10 01:41:13 -04001055 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001056 if (!isBoldSafe) {
1057 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001058 'from normal. Font family is: ' +
1059 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001060 }
rginda9f5222b2012-03-05 11:53:28 -08001061
Robert Gindaed016262012-10-26 16:27:09 -07001062 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1063 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001064};
1065
1066/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001067 * Control text blinking behavior.
1068 *
1069 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001070 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001071hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001072 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001073 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001074 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001075 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001076};
1077
1078/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001079 * Set the mouse cursor style based on the current terminal mode.
1080 */
1081hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001082 this.setCssVar('mouse-cursor-style',
1083 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1084 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001085 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001086};
1087
1088/**
rginda87b86462011-12-14 13:48:03 -08001089 * Return a copy of the current cursor position.
1090 *
Joel Hockey0f933582019-08-27 18:01:51 -07001091 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001092 */
1093hterm.Terminal.prototype.saveCursor = function() {
1094 return this.screen_.cursorPosition.clone();
1095};
1096
Evan Jones2600d4f2016-12-06 09:29:36 -05001097/**
1098 * Return the current text attributes.
1099 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001100 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001101 */
rgindaa19afe22012-01-25 15:40:22 -08001102hterm.Terminal.prototype.getTextAttributes = function() {
1103 return this.screen_.textAttributes;
1104};
1105
Evan Jones2600d4f2016-12-06 09:29:36 -05001106/**
1107 * Set the text attributes.
1108 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001109 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001110 */
rginda1a09aa02012-06-18 21:11:25 -07001111hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1112 this.screen_.textAttributes = textAttributes;
1113};
1114
rginda87b86462011-12-14 13:48:03 -08001115/**
rgindaf522ce02012-04-17 17:49:17 -07001116 * Return the current browser zoom factor applied to the terminal.
1117 *
1118 * @return {number} The current browser zoom factor.
1119 */
1120hterm.Terminal.prototype.getZoomFactor = function() {
1121 return this.scrollPort_.characterSize.zoomFactor;
1122};
1123
1124/**
rginda9846e2f2012-01-27 13:53:33 -08001125 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001126 *
1127 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001128 */
1129hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001130 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001131};
1132
1133/**
rginda87b86462011-12-14 13:48:03 -08001134 * Restore a previously saved cursor position.
1135 *
Joel Hockey0f933582019-08-27 18:01:51 -07001136 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001137 */
1138hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001139 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1140 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001141 this.screen_.setCursorPosition(row, column);
1142 if (cursor.column > column ||
1143 cursor.column == column && cursor.overflow) {
1144 this.screen_.cursorPosition.overflow = true;
1145 }
rginda87b86462011-12-14 13:48:03 -08001146};
1147
1148/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001149 * Clear the cursor's overflow flag.
1150 */
1151hterm.Terminal.prototype.clearCursorOverflow = function() {
1152 this.screen_.cursorPosition.overflow = false;
1153};
1154
1155/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001156 * Save the current cursor state to the corresponding screens.
1157 *
1158 * See the hterm.Screen.CursorState class for more details.
1159 *
1160 * @param {boolean=} both If true, update both screens, else only update the
1161 * current screen.
1162 */
1163hterm.Terminal.prototype.saveCursorAndState = function(both) {
1164 if (both) {
1165 this.primaryScreen_.saveCursorAndState(this.vt);
1166 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001167 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001168 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001169 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001170};
1171
1172/**
1173 * Restore the saved cursor state in the corresponding screens.
1174 *
1175 * See the hterm.Screen.CursorState class for more details.
1176 *
1177 * @param {boolean=} both If true, update both screens, else only update the
1178 * current screen.
1179 */
1180hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1181 if (both) {
1182 this.primaryScreen_.restoreCursorAndState(this.vt);
1183 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001184 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001185 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001186 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001187};
1188
1189/**
Robert Ginda830583c2013-08-07 13:20:46 -07001190 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001191 *
1192 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001193 */
1194hterm.Terminal.prototype.setCursorShape = function(shape) {
1195 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001196 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001197};
Robert Ginda830583c2013-08-07 13:20:46 -07001198
1199/**
1200 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001201 *
1202 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001203 */
1204hterm.Terminal.prototype.getCursorShape = function() {
1205 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001206};
Robert Ginda830583c2013-08-07 13:20:46 -07001207
1208/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001209 * Set the screen padding size in pixels.
1210 *
1211 * @param {number} size
1212 */
1213hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001214 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001215 this.scrollPort_.setScreenPaddingSize(size);
1216};
1217
1218/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001219 * Set the screen border size in pixels.
1220 *
1221 * @param {number} size
1222 */
1223hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1224 this.div_.style.borderWidth = `${size}px`;
1225 this.screenBorderSize_ = size;
1226 this.scrollPort_.resize();
1227};
1228
1229/**
rginda87b86462011-12-14 13:48:03 -08001230 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001231 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001232 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001233 */
1234hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001235 if (columnCount == null) {
1236 this.div_.style.width = '100%';
1237 return;
1238 }
1239
Joel Hockey139d82d2020-04-07 23:04:29 -07001240 const rightPadding = Math.max(
1241 this.scrollPort_.screenPaddingSize,
1242 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001243 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001244 (this.scrollPort_.characterSize.width * columnCount) +
1245 this.scrollPort_.screenPaddingSize + rightPadding +
1246 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001247 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001248 this.scheduleSyncCursorPosition_();
1249};
rginda87b86462011-12-14 13:48:03 -08001250
rgindac9bc5502012-01-18 11:48:44 -08001251/**
rginda35c456b2012-02-09 17:29:05 -08001252 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001253 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001254 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001255 */
1256hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001257 if (rowCount == null) {
1258 this.div_.style.height = '100%';
1259 return;
1260 }
1261
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001262 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1263 (2 * this.scrollPort_.screenPaddingSize) +
1264 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001265 this.realizeSize_(this.screenSize.width, rowCount);
1266 this.scheduleSyncCursorPosition_();
1267};
1268
1269/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001270 * Deal with terminal size changes.
1271 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001272 * @param {number} columnCount The number of columns.
1273 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001274 */
1275hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001276 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001277
Mike Frysinger0206e262019-06-13 10:18:19 -04001278 if (columnCount != this.screenSize.width) {
1279 notify = true;
1280 this.realizeWidth_(columnCount);
1281 }
1282
1283 if (rowCount != this.screenSize.height) {
1284 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001285 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001286 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001287
1288 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001289 if (notify) {
1290 this.io.onTerminalResize_(columnCount, rowCount);
1291 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001292};
1293
1294/**
rgindac9bc5502012-01-18 11:48:44 -08001295 * Deal with terminal width changes.
1296 *
1297 * This function does what needs to be done when the terminal width changes
1298 * out from under us. It happens here rather than in onResize_() because this
1299 * code may need to run synchronously to handle programmatic changes of
1300 * terminal width.
1301 *
1302 * Relying on the browser to send us an async resize event means we may not be
1303 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001304 *
1305 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001306 */
1307hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001308 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001309 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001310 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001311
Mike Frysingerdc727792020-04-10 01:41:13 -04001312 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001313 if (deltaColumns == 0) {
1314 // No change, so don't bother recalculating things.
1315 return;
1316 }
rgindac9bc5502012-01-18 11:48:44 -08001317
rginda87b86462011-12-14 13:48:03 -08001318 this.screenSize.width = columnCount;
1319 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001320
1321 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001322 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001323 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001324 }
rgindac9bc5502012-01-18 11:48:44 -08001325 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001326 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001327 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001328 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001329 }
rgindac9bc5502012-01-18 11:48:44 -08001330
1331 this.tabStops_.pop();
1332 }
1333 }
1334
1335 this.screen_.setColumnCount(this.screenSize.width);
1336};
1337
1338/**
1339 * Deal with terminal height changes.
1340 *
1341 * This function does what needs to be done when the terminal height changes
1342 * out from under us. It happens here rather than in onResize_() because this
1343 * code may need to run synchronously to handle programmatic changes of
1344 * terminal height.
1345 *
1346 * Relying on the browser to send us an async resize event means we may not be
1347 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001348 *
1349 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001350 */
1351hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001352 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001353 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001354 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001355
Mike Frysingerdc727792020-04-10 01:41:13 -04001356 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001357 if (deltaRows == 0) {
1358 // No change, so don't bother recalculating things.
1359 return;
1360 }
rgindac9bc5502012-01-18 11:48:44 -08001361
1362 this.screenSize.height = rowCount;
1363
Mike Frysingerdc727792020-04-10 01:41:13 -04001364 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001365
1366 if (deltaRows < 0) {
1367 // Screen got smaller.
1368 deltaRows *= -1;
1369 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001370 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001371 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001372 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001373 }
rgindac9bc5502012-01-18 11:48:44 -08001374
Mike Frysingerbdb34802020-04-07 03:47:32 -04001375 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001376 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001377 }
rgindac9bc5502012-01-18 11:48:44 -08001378
1379 this.screen_.popRow();
1380 deltaRows--;
1381 }
1382
Mike Frysingerdc727792020-04-10 01:41:13 -04001383 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001384 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1385
1386 // We just removed rows from the top of the screen, we need to update
1387 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001388 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001389 } else if (deltaRows > 0) {
1390 // Screen got larger.
1391
1392 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001393 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1394 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001395 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1396 this.screen_.unshiftRows(rows);
1397 deltaRows -= scrollbackCount;
1398 cursor.row += scrollbackCount;
1399 }
1400
Mike Frysingerbdb34802020-04-07 03:47:32 -04001401 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001402 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001403 }
rgindac9bc5502012-01-18 11:48:44 -08001404 }
1405
rginda35c456b2012-02-09 17:29:05 -08001406 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001407 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001408};
1409
1410/**
1411 * Scroll the terminal to the top of the scrollback buffer.
1412 */
1413hterm.Terminal.prototype.scrollHome = function() {
1414 this.scrollPort_.scrollRowToTop(0);
1415};
1416
1417/**
1418 * Scroll the terminal to the end.
1419 */
1420hterm.Terminal.prototype.scrollEnd = function() {
1421 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1422};
1423
1424/**
1425 * Scroll the terminal one page up (minus one line) relative to the current
1426 * position.
1427 */
1428hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001429 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001430};
1431
1432/**
1433 * Scroll the terminal one page down (minus one line) relative to the current
1434 * position.
1435 */
1436hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001437 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001438};
1439
rgindac9bc5502012-01-18 11:48:44 -08001440/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001441 * Scroll the terminal one line up relative to the current position.
1442 */
1443hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001444 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001445 this.scrollPort_.scrollRowToTop(i - 1);
1446};
1447
1448/**
1449 * Scroll the terminal one line down relative to the current position.
1450 */
1451hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001452 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001453 this.scrollPort_.scrollRowToTop(i + 1);
1454};
1455
1456/**
Robert Ginda40932892012-12-10 17:26:40 -08001457 * Clear primary screen, secondary screen, and the scrollback buffer.
1458 */
1459hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001460 this.clearHome(this.primaryScreen_);
1461 this.clearHome(this.alternateScreen_);
1462
1463 this.clearScrollback();
1464};
1465
1466/**
1467 * Clear scrollback buffer.
1468 */
1469hterm.Terminal.prototype.clearScrollback = function() {
1470 // Move to the end of the buffer in case the screen was scrolled back.
1471 // We're going to throw it away which would leave the display invalid.
1472 this.scrollEnd();
1473
Robert Ginda40932892012-12-10 17:26:40 -08001474 this.scrollbackRows_.length = 0;
1475 this.scrollPort_.resetCache();
1476
Mike Frysinger9c482b82018-09-07 02:49:36 -04001477 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1478 const bottom = screen.getHeight();
1479 this.renumberRows_(0, bottom, screen);
1480 });
Robert Ginda40932892012-12-10 17:26:40 -08001481
1482 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001483 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001484};
1485
1486/**
rgindac9bc5502012-01-18 11:48:44 -08001487 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001488 *
1489 * Perform a full reset to the default values listed in
1490 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001491 */
rginda87b86462011-12-14 13:48:03 -08001492hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001493 this.vt.reset();
1494
rgindac9bc5502012-01-18 11:48:44 -08001495 this.clearAllTabStops();
1496 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001497
Joel Hockey42dba8f2020-03-26 16:21:11 -07001498 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001499 const resetScreen = (screen) => {
1500 // We want to make sure to reset the attributes before we clear the screen.
1501 // The attributes might be used to initialize default/empty rows.
1502 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001503 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001504 this.clearHome(screen);
1505 screen.saveCursorAndState(this.vt);
1506 };
1507 resetScreen(this.primaryScreen_);
1508 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001509
Mike Frysinger84301d02017-11-29 13:28:46 -08001510 // Reset terminal options to their default values.
1511 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001512 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1513
Mike Frysinger84301d02017-11-29 13:28:46 -08001514 this.setVTScrollRegion(null, null);
1515
1516 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001517};
1518
rgindac9bc5502012-01-18 11:48:44 -08001519/**
1520 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001521 *
1522 * Perform a soft reset to the default values listed in
1523 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001524 */
rginda0f5c0292012-01-13 11:00:13 -08001525hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001526 this.vt.reset();
1527
rgindab8bc8932012-04-27 12:45:03 -07001528 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001529 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001530
Brad Townb62dfdc2015-03-16 19:07:15 -07001531 // We show the cursor on soft reset but do not alter the blink state.
1532 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1533
Joel Hockey42dba8f2020-03-26 16:21:11 -07001534 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001535 const resetScreen = (screen) => {
1536 // Xterm also resets the color palette on soft reset, even though it doesn't
1537 // seem to be documented anywhere.
1538 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001539 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001540 screen.saveCursorAndState(this.vt);
1541 };
1542 resetScreen(this.primaryScreen_);
1543 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001544
rgindab8bc8932012-04-27 12:45:03 -07001545 // The xterm man page explicitly says this will happen on soft reset.
1546 this.setVTScrollRegion(null, null);
1547
1548 // Xterm also shows the cursor on soft reset, but does not alter the blink
1549 // state.
rgindaa19afe22012-01-25 15:40:22 -08001550 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001551};
1552
rgindac9bc5502012-01-18 11:48:44 -08001553/**
1554 * Move the cursor forward to the next tab stop, or to the last column
1555 * if no more tab stops are set.
1556 */
1557hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001558 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001559
Mike Frysingerdc727792020-04-10 01:41:13 -04001560 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001561 if (this.tabStops_[i] > column) {
1562 this.setCursorColumn(this.tabStops_[i]);
1563 return;
1564 }
1565 }
1566
David Benjamin66e954d2012-05-05 21:08:12 -04001567 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001568 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001569 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001570 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001571};
1572
rgindac9bc5502012-01-18 11:48:44 -08001573/**
1574 * Move the cursor backward to the previous tab stop, or to the first column
1575 * if no previous tab stops are set.
1576 */
1577hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001578 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001579
Mike Frysingerdc727792020-04-10 01:41:13 -04001580 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001581 if (this.tabStops_[i] < column) {
1582 this.setCursorColumn(this.tabStops_[i]);
1583 return;
1584 }
1585 }
1586
1587 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001588};
1589
rgindac9bc5502012-01-18 11:48:44 -08001590/**
1591 * Set a tab stop at the given column.
1592 *
Joel Hockey0f933582019-08-27 18:01:51 -07001593 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001594 */
1595hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001596 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001597 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001598 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001599 }
rgindac9bc5502012-01-18 11:48:44 -08001600
1601 if (this.tabStops_[i] < column) {
1602 this.tabStops_.splice(i + 1, 0, column);
1603 return;
1604 }
1605 }
1606
1607 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001608};
1609
rgindac9bc5502012-01-18 11:48:44 -08001610/**
1611 * Clear the tab stop at the current cursor position.
1612 *
1613 * No effect if there is no tab stop at the current cursor position.
1614 */
1615hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001616 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001617
Mike Frysingerdc727792020-04-10 01:41:13 -04001618 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001619 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001620 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001621 }
rgindac9bc5502012-01-18 11:48:44 -08001622
1623 this.tabStops_.splice(i, 1);
1624};
1625
1626/**
1627 * Clear all tab stops.
1628 */
1629hterm.Terminal.prototype.clearAllTabStops = function() {
1630 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001631 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001632};
1633
1634/**
1635 * Set up the default tab stops, starting from a given column.
1636 *
1637 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001638 * from the specified column, or 0 if no column is provided. It also flags
1639 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001640 *
1641 * This does not clear the existing tab stops first, use clearAllTabStops
1642 * for that.
1643 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001644 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001645 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001646 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001647hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001648 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001649 // Round start up to a default tab stop.
1650 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001651 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001652 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001653 }
David Benjamin66e954d2012-05-05 21:08:12 -04001654
1655 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001656};
1657
rginda6d397402012-01-17 10:58:29 -08001658/**
rginda8ba33642011-12-14 12:31:31 -08001659 * Interpret a sequence of characters.
1660 *
1661 * Incomplete escape sequences are buffered until the next call.
1662 *
1663 * @param {string} str Sequence of characters to interpret or pass through.
1664 */
1665hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001666 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001667 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001668};
1669
1670/**
1671 * Take over the given DIV for use as the terminal display.
1672 *
Joel Hockey0f933582019-08-27 18:01:51 -07001673 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001674 */
1675hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001676 const charset = div.ownerDocument.characterSet.toLowerCase();
1677 if (charset != 'utf-8') {
1678 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1679 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1680 }
1681
rginda87b86462011-12-14 13:48:03 -08001682 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001683 this.div_.style.borderStyle = 'solid';
1684 this.div_.style.borderWidth = 0;
1685 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001686
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001687 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1688
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001689 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1690};
1691
1692/**
1693 * Initialisation of ScrollPort properties which need to be set after its DOM
1694 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001695 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001696 * @private
1697 */
1698hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001699 this.scrollPort_.setBackgroundImage(
1700 this.prefs_.getString('background-image'));
1701 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001702 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001703 this.prefs_.getString('background-position'));
1704 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1705 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1706 this.scrollPort_.setAccessibilityReader(
1707 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001708
rginda0918b652012-04-04 11:26:24 -07001709 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001710
Joel Hockeyd4fca732019-09-20 16:57:03 -07001711 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001712 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001713
Joel Hockeyd4fca732019-09-20 16:57:03 -07001714 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001715 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001716 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001717
rginda8ba33642011-12-14 12:31:31 -08001718 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001719 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301720 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001721
Evan Jones5f9df812016-12-06 09:38:58 -05001722 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001723 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001724
Mike Frysingerdc727792020-04-10 01:41:13 -04001725 const onMouse = this.onMouse_.bind(this);
1726 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001727 screenNode.addEventListener(
1728 'mousedown', /** @type {!EventListener} */ (onMouse));
1729 screenNode.addEventListener(
1730 'mouseup', /** @type {!EventListener} */ (onMouse));
1731 screenNode.addEventListener(
1732 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001733 this.scrollPort_.onScrollWheel = onMouse;
1734
Joel Hockeyd4fca732019-09-20 16:57:03 -07001735 screenNode.addEventListener(
1736 'keydown',
1737 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001738
Toni Barzic0bfa8922013-11-22 11:18:35 -08001739 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001740 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001741 // Listen for mousedown events on the screenNode as in FF the focus
1742 // events don't bubble.
1743 screenNode.addEventListener('mousedown', function() {
1744 setTimeout(this.onFocusChange_.bind(this, true));
1745 }.bind(this));
1746
Toni Barzic0bfa8922013-11-22 11:18:35 -08001747 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001748 'blur', this.onFocusChange_.bind(this, false));
1749
Mike Frysingerdc727792020-04-10 01:41:13 -04001750 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001751 style.textContent = `
1752.cursor-node[focus="false"] {
1753 box-sizing: border-box;
1754 background-color: transparent !important;
1755 border-width: 2px;
1756 border-style: solid;
1757}
1758menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001759 background: #fff;
1760 border-radius: 4px;
1761 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001762 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001763 display: none;
1764 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1765 margin: 0;
1766 padding: 8px 0;
1767 position: absolute;
1768 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001769}
1770menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001771 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001772 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1773 padding: 0.5em 1em;
1774 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001775}
1776menuitem.separator {
1777 border-bottom: none;
1778 height: 0.5em;
1779 padding: 0;
1780}
1781menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001782 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001783}
1784.wc-node {
1785 display: inline-block;
1786 text-align: center;
1787 width: calc(var(--hterm-charsize-width) * 2);
1788 line-height: var(--hterm-charsize-height);
1789}
1790:root {
1791 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1792 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001793 --hterm-blink-node-duration: 0.7s;
1794 --hterm-mouse-cursor-default: default;
1795 --hterm-mouse-cursor-text: text;
1796 --hterm-mouse-cursor-pointer: pointer;
1797 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001798 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001799
Joel Hockey42dba8f2020-03-26 16:21:11 -07001800${lib.colors.stockColorPalette.map((c, i) => `
1801 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1802`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001803}
1804.uri-node:hover {
1805 text-decoration: underline;
1806 cursor: var(--hterm-mouse-cursor-pointer);
1807}
1808@keyframes blink {
1809 from { opacity: 1.0; }
1810 to { opacity: 0.0; }
1811}
1812.blink-node {
1813 animation-name: blink;
1814 animation-duration: var(--hterm-blink-node-duration);
1815 animation-iteration-count: infinite;
1816 animation-timing-function: ease-in-out;
1817 animation-direction: alternate;
1818}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001819 // Insert this stock style as the first node so that any user styles will
1820 // override w/out having to use !important everywhere. The rules above mix
1821 // runtime variables with default ones designed to be overridden by the user,
1822 // but we can wait for a concrete case from the users to determine the best
1823 // way to split the sheet up to before & after the user-css settings.
1824 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001825
rginda8ba33642011-12-14 12:31:31 -08001826 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001827 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001828 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001829 this.cursorNode_.style.cssText = `
1830position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001831left: calc(var(--hterm-screen-padding-size) +
1832 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1833top: calc(var(--hterm-screen-padding-size) +
1834 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001835display: ${this.options_.cursorVisible ? '' : 'none'};
1836width: var(--hterm-charsize-width);
1837height: var(--hterm-charsize-height);
1838background-color: var(--hterm-cursor-color);
1839border-color: var(--hterm-cursor-color);
1840-webkit-transition: opacity, background-color 100ms linear;
1841-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001842
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001843 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001844 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1845 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001846
rginda8ba33642011-12-14 12:31:31 -08001847 this.document_.body.appendChild(this.cursorNode_);
1848
rgindad5613292012-06-19 15:40:37 -07001849 // When 'enableMouseDragScroll' is off we reposition this element directly
1850 // under the mouse cursor after a click. This makes Chrome associate
1851 // subsequent mousemove events with the scroll-blocker. Since the
1852 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1853 // events do not cause the scrollport to scroll.
1854 //
1855 // It's a hack, but it's the cleanest way I could find.
1856 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001857 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001858 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001859 this.scrollBlockerNode_.style.cssText =
1860 ('position: absolute;' +
1861 'top: -99px;' +
1862 'display: block;' +
1863 'width: 10px;' +
1864 'height: 10px;');
1865 this.document_.body.appendChild(this.scrollBlockerNode_);
1866
rgindad5613292012-06-19 15:40:37 -07001867 this.scrollPort_.onScrollWheel = onMouse;
1868 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1869 ].forEach(function(event) {
1870 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001871 this.cursorNode_.addEventListener(
1872 event, /** @type {!EventListener} */ (onMouse));
1873 this.document_.addEventListener(
1874 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001875 }.bind(this));
1876
1877 this.cursorNode_.addEventListener('mousedown', function() {
1878 setTimeout(this.focus.bind(this));
1879 }.bind(this));
1880
rginda8ba33642011-12-14 12:31:31 -08001881 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001882
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001883 // Re-sync fonts whenever a web font loads.
1884 this.document_.fonts.addEventListener(
1885 'loadingdone', () => this.syncFontFamily());
1886
rginda87b86462011-12-14 13:48:03 -08001887 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001888 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001889};
1890
rginda0918b652012-04-04 11:26:24 -07001891/**
1892 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001893 *
Joel Hockey0f933582019-08-27 18:01:51 -07001894 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001895 */
rginda87b86462011-12-14 13:48:03 -08001896hterm.Terminal.prototype.getDocument = function() {
1897 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001898};
1899
1900/**
rginda0918b652012-04-04 11:26:24 -07001901 * Focus the terminal.
1902 */
1903hterm.Terminal.prototype.focus = function() {
1904 this.scrollPort_.focus();
1905};
1906
1907/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001908 * Unfocus the terminal.
1909 */
1910hterm.Terminal.prototype.blur = function() {
1911 this.scrollPort_.blur();
1912};
1913
1914/**
rginda8ba33642011-12-14 12:31:31 -08001915 * Return the HTML Element for a given row index.
1916 *
1917 * This is a method from the RowProvider interface. The ScrollPort uses
1918 * it to fetch rows on demand as they are scrolled into view.
1919 *
1920 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1921 * pairs to conserve memory.
1922 *
Joel Hockey0f933582019-08-27 18:01:51 -07001923 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001924 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001925 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001926 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001927 * @override
rginda8ba33642011-12-14 12:31:31 -08001928 */
1929hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001930 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001931 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001932 }
rginda8ba33642011-12-14 12:31:31 -08001933
Mike Frysingerdc727792020-04-10 01:41:13 -04001934 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001935 return this.screen_.rowsArray[screenIndex];
1936};
1937
1938/**
1939 * Return the text content for a given range of rows.
1940 *
1941 * This is a method from the RowProvider interface. The ScrollPort uses
1942 * it to fetch text content on demand when the user attempts to copy their
1943 * selection to the clipboard.
1944 *
Joel Hockey0f933582019-08-27 18:01:51 -07001945 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001946 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001947 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001948 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001949 * relative to the start of the scrollback buffer.
1950 * @return {string} A single string containing the text value of the range of
1951 * rows. Lines will be newline delimited, with no trailing newline.
1952 */
1953hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001954 const ary = [];
1955 for (let i = start; i < end; i++) {
1956 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001957 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001958 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001959 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001960 }
rginda8ba33642011-12-14 12:31:31 -08001961 }
1962
rgindaa09e7332012-08-17 12:49:51 -07001963 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001964};
1965
1966/**
1967 * Return the text content for a given row.
1968 *
1969 * This is a method from the RowProvider interface. The ScrollPort uses
1970 * it to fetch text content on demand when the user attempts to copy their
1971 * selection to the clipboard.
1972 *
Joel Hockey0f933582019-08-27 18:01:51 -07001973 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001974 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001975 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001976 * @return {string} A string containing the text value of the selected row.
1977 */
1978hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001979 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001980 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001981};
1982
1983/**
1984 * Return the total number of rows in the addressable screen and in the
1985 * scrollback buffer of this terminal.
1986 *
1987 * This is a method from the RowProvider interface. The ScrollPort uses
1988 * it to compute the size of the scrollbar.
1989 *
Joel Hockey0f933582019-08-27 18:01:51 -07001990 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001991 * @override
rginda8ba33642011-12-14 12:31:31 -08001992 */
1993hterm.Terminal.prototype.getRowCount = function() {
1994 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1995};
1996
1997/**
1998 * Create DOM nodes for new rows and append them to the end of the terminal.
1999 *
2000 * This is the only correct way to add a new DOM node for a row. Notice that
2001 * the new row is appended to the bottom of the list of rows, and does not
2002 * require renumbering (of the rowIndex property) of previous rows.
2003 *
2004 * If you think you want a new blank row somewhere in the middle of the
2005 * terminal, look into moveRows_().
2006 *
2007 * This method does not pay attention to vtScrollTop/Bottom, since you should
2008 * be using moveRows() in cases where they would matter.
2009 *
2010 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002011 *
2012 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002013 */
2014hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002015 let cursorRow = this.screen_.rowsArray.length;
2016 const offset = this.scrollbackRows_.length + cursorRow;
2017 for (let i = 0; i < count; i++) {
2018 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002019 row.appendChild(this.document_.createTextNode(''));
2020 row.rowIndex = offset + i;
2021 this.screen_.pushRow(row);
2022 }
2023
Mike Frysingerdc727792020-04-10 01:41:13 -04002024 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002025 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002026 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002027 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002028 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002029 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002030 }
rginda8ba33642011-12-14 12:31:31 -08002031 }
2032
Mike Frysingerbdb34802020-04-07 03:47:32 -04002033 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002034 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002035 }
rginda8ba33642011-12-14 12:31:31 -08002036
rginda87b86462011-12-14 13:48:03 -08002037 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002038};
2039
2040/**
2041 * Relocate rows from one part of the addressable screen to another.
2042 *
2043 * This is used to recycle rows during VT scrolls (those which are driven
2044 * by VT commands, rather than by the user manipulating the scrollbar.)
2045 *
2046 * In this case, the blank lines scrolled into the scroll region are made of
2047 * the nodes we scrolled off. These have their rowIndex properties carefully
2048 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002049 *
2050 * @param {number} fromIndex The start index.
2051 * @param {number} count The number of rows to move.
2052 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002053 */
2054hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002055 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002056 this.screen_.insertRows(toIndex, ary);
2057
Mike Frysingerdc727792020-04-10 01:41:13 -04002058 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002059 if (fromIndex < toIndex) {
2060 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002061 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002062 } else {
2063 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002064 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002065 }
2066
2067 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002068 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002069};
2070
2071/**
2072 * Renumber the rowIndex property of the given range of rows.
2073 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002074 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002075 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002076 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002077 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002078 *
2079 * @param {number} start The start index.
2080 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002081 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002082 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002083hterm.Terminal.prototype.renumberRows_ = function(
2084 start, end, screen = undefined) {
2085 if (!screen) {
2086 screen = this.screen_;
2087 }
Robert Ginda40932892012-12-10 17:26:40 -08002088
Mike Frysingerdc727792020-04-10 01:41:13 -04002089 const offset = this.scrollbackRows_.length;
2090 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002091 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002092 }
2093};
2094
2095/**
2096 * Print a string to the terminal.
2097 *
2098 * This respects the current insert and wraparound modes. It will add new lines
2099 * to the end of the terminal, scrolling off the top into the scrollback buffer
2100 * if necessary.
2101 *
2102 * The string is *not* parsed for escape codes. Use the interpret() method if
2103 * that's what you're after.
2104 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002105 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002106 */
2107hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002108 this.scheduleSyncCursorPosition_();
2109
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002110 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002111 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002112
Mike Frysingerdc727792020-04-10 01:41:13 -04002113 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002114
Mike Frysingerdc727792020-04-10 01:41:13 -04002115 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002116 // Fun edge case: If the string only contains zero width codepoints (like
2117 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002118 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002119 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002120 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002121
2122 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002123 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2124 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002125 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002126 }
rgindaa19afe22012-01-25 15:40:22 -08002127
Mike Frysingerdc727792020-04-10 01:41:13 -04002128 let count = strWidth - startOffset;
2129 let didOverflow = false;
2130 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002131
rgindaa9abdd82012-08-06 18:05:09 -07002132 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2133 didOverflow = true;
2134 count = this.screenSize.width - this.screen_.cursorPosition.column;
2135 }
rgindaa19afe22012-01-25 15:40:22 -08002136
rgindaa9abdd82012-08-06 18:05:09 -07002137 if (didOverflow && !this.options_.wraparound) {
2138 // If the string overflowed the line but wraparound is off, then the
2139 // last printed character should be the last of the string.
2140 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002141 substr = lib.wc.substr(str, startOffset, count - 1) +
2142 lib.wc.substr(str, strWidth - 1);
2143 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002144 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002145 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002146 }
rgindaa19afe22012-01-25 15:40:22 -08002147
Mike Frysingerdc727792020-04-10 01:41:13 -04002148 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2149 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002150 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2151 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002152
2153 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002154 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002155 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002156 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002157 }
2158 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002159 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002160 }
2161
2162 this.screen_.maybeClipCurrentRow();
2163 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002164 }
rginda8ba33642011-12-14 12:31:31 -08002165
Mike Frysingerbdb34802020-04-07 03:47:32 -04002166 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002167 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002168 }
rginda8ba33642011-12-14 12:31:31 -08002169};
2170
2171/**
rginda87b86462011-12-14 13:48:03 -08002172 * Set the VT scroll region.
2173 *
rginda87b86462011-12-14 13:48:03 -08002174 * This also resets the cursor position to the absolute (0, 0) position, since
2175 * that's what xterm appears to do.
2176 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002177 * Setting the scroll region to the full height of the terminal will clear
2178 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2179 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2180 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2181 * continue to work as most users would expect.
2182 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002183 * @param {?number} scrollTop The zero-based top of the scroll region.
2184 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002185 * inclusive.
2186 */
2187hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002188 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002189 this.vtScrollTop_ = null;
2190 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002191 } else {
2192 this.vtScrollTop_ = scrollTop;
2193 this.vtScrollBottom_ = scrollBottom;
2194 }
rginda87b86462011-12-14 13:48:03 -08002195};
2196
2197/**
rginda8ba33642011-12-14 12:31:31 -08002198 * Return the top row index according to the VT.
2199 *
2200 * This will return 0 unless the terminal has been told to restrict scrolling
2201 * to some lower row. It is used for some VT cursor positioning and scrolling
2202 * commands.
2203 *
Joel Hockey0f933582019-08-27 18:01:51 -07002204 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002205 */
2206hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002207 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002208 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002209 }
rginda8ba33642011-12-14 12:31:31 -08002210
2211 return 0;
rginda87b86462011-12-14 13:48:03 -08002212};
rginda8ba33642011-12-14 12:31:31 -08002213
2214/**
2215 * Return the bottom row index according to the VT.
2216 *
2217 * This will return the height of the terminal unless the it has been told to
2218 * restrict scrolling to some higher row. It is used for some VT cursor
2219 * positioning and scrolling commands.
2220 *
Joel Hockey0f933582019-08-27 18:01:51 -07002221 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002222 */
2223hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002224 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002225 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002226 }
rginda8ba33642011-12-14 12:31:31 -08002227
rginda87b86462011-12-14 13:48:03 -08002228 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002229};
rginda8ba33642011-12-14 12:31:31 -08002230
2231/**
2232 * Process a '\n' character.
2233 *
2234 * If the cursor is on the final row of the terminal this will append a new
2235 * blank row to the screen and scroll the topmost row into the scrollback
2236 * buffer.
2237 *
2238 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002239 *
2240 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2241 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002242 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002243hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002244 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002245 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002246 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002247
Mike Frysingerdc727792020-04-10 01:41:13 -04002248 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2249 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002250
2251 if (this.vtScrollBottom_ != null) {
2252 // A VT Scroll region is active, we never append new rows.
2253 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2254 // We're at the end of the VT Scroll Region, perform a VT scroll.
2255 this.vtScrollUp(1);
2256 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2257 } else if (cursorAtEndOfScreen) {
2258 // We're at the end of the screen, the only thing to do is put the
2259 // cursor to column 0.
2260 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2261 } else {
2262 // Anywhere else, advance the cursor row, and reset the column.
2263 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2264 }
2265 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002266 // We're at the end of the screen. Append a new row to the terminal,
2267 // shifting the top row into the scrollback.
2268 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002269 } else {
rginda87b86462011-12-14 13:48:03 -08002270 // Anywhere else in the screen just moves the cursor.
2271 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002272 }
2273};
2274
2275/**
2276 * Like newLine(), except maintain the cursor column.
2277 */
2278hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002279 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002280 this.newLine();
2281 this.setCursorColumn(column);
2282};
2283
2284/**
rginda87b86462011-12-14 13:48:03 -08002285 * If autoCarriageReturn is set then newLine(), else lineFeed().
2286 */
2287hterm.Terminal.prototype.formFeed = function() {
2288 if (this.options_.autoCarriageReturn) {
2289 this.newLine();
2290 } else {
2291 this.lineFeed();
2292 }
2293};
2294
2295/**
2296 * Move the cursor up one row, possibly inserting a blank line.
2297 *
2298 * The cursor column is not changed.
2299 */
2300hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002301 const scrollTop = this.getVTScrollTop();
2302 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002303
2304 if (currentRow == scrollTop) {
2305 this.insertLines(1);
2306 } else {
2307 this.setAbsoluteCursorRow(currentRow - 1);
2308 }
2309};
2310
2311/**
rginda8ba33642011-12-14 12:31:31 -08002312 * Replace all characters to the left of the current cursor with the space
2313 * character.
2314 *
2315 * TODO(rginda): This should probably *remove* the characters (not just replace
2316 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002317 * position.
rginda8ba33642011-12-14 12:31:31 -08002318 */
2319hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002320 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002321 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002322 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002323 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002324 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002325};
2326
2327/**
David Benjamin684a9b72012-05-01 17:19:58 -04002328 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002329 *
2330 * The cursor position is unchanged.
2331 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002332 * If the current background color is not the default background color this
2333 * will insert spaces rather than delete. This is unfortunate because the
2334 * trailing space will affect text selection, but it's difficult to come up
2335 * with a way to style empty space that wouldn't trip up the hterm.Screen
2336 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002337 *
2338 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2339 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2340 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002341 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002342 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002343 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002344hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002345 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002346 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002347 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002348
Mike Frysingerdc727792020-04-10 01:41:13 -04002349 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002350 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002351
2352 if (this.screen_.textAttributes.background ===
2353 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002354 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002355 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002356 this.screen_.cursorPosition.column + count) {
2357 this.screen_.deleteChars(count);
2358 this.clearCursorOverflow();
2359 return;
2360 }
2361 }
2362
Mike Frysingerdc727792020-04-10 01:41:13 -04002363 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002364 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002365 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002366 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002367};
2368
2369/**
2370 * Erase the current line.
2371 *
2372 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002373 */
2374hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002375 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002376 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002377 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002378 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002379};
2380
2381/**
David Benjamina08d78f2012-05-05 00:28:49 -04002382 * Erase all characters from the start of the screen to the current cursor
2383 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002384 *
2385 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002386 */
2387hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002388 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002389
2390 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002391
Mike Frysingerdc727792020-04-10 01:41:13 -04002392 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002393 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002394 this.screen_.clearCursorRow();
2395 }
2396
rginda87b86462011-12-14 13:48:03 -08002397 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002398 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002399};
2400
2401/**
2402 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002403 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002404 *
2405 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002406 */
2407hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002408 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002409
2410 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002411
Mike Frysingerdc727792020-04-10 01:41:13 -04002412 const bottom = this.screenSize.height - 1;
2413 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002414 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002415 this.screen_.clearCursorRow();
2416 }
2417
rginda87b86462011-12-14 13:48:03 -08002418 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002419 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002420};
2421
2422/**
2423 * Fill the terminal with a given character.
2424 *
2425 * This methods does not respect the VT scroll region.
2426 *
2427 * @param {string} ch The character to use for the fill.
2428 */
2429hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002430 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002431
2432 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002433 for (let row = 0; row < this.screenSize.height; row++) {
2434 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002435 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002436 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002437 }
2438 }
2439
2440 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002441};
2442
2443/**
rginda9ea433c2012-03-16 11:57:00 -07002444 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002445 *
rginda9ea433c2012-03-16 11:57:00 -07002446 * This does not respect the scroll region.
2447 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002448 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002449 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002450 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002451hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2452 if (!screen) {
2453 screen = this.screen_;
2454 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002455 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002456
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002457 this.accessibilityReader_.clear();
2458
rginda11057d52012-04-25 12:29:56 -07002459 if (bottom == 0) {
2460 // Empty screen, nothing to do.
2461 return;
2462 }
2463
Mike Frysingerdc727792020-04-10 01:41:13 -04002464 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002465 screen.setCursorPosition(i, 0);
2466 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002467 }
2468
rginda9ea433c2012-03-16 11:57:00 -07002469 screen.setCursorPosition(0, 0);
2470};
2471
2472/**
2473 * Erase the entire display without changing the cursor position.
2474 *
2475 * The cursor position is unchanged. This does not respect the scroll
2476 * region.
2477 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002478 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002479 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002480 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002481hterm.Terminal.prototype.clear = function(screen = undefined) {
2482 if (!screen) {
2483 screen = this.screen_;
2484 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002485 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002486 this.clearHome(screen);
2487 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002488};
2489
2490/**
2491 * VT command to insert lines at the current cursor row.
2492 *
2493 * This respects the current scroll region. Rows pushed off the bottom are
2494 * lost (they won't show up in the scrollback buffer).
2495 *
Joel Hockey0f933582019-08-27 18:01:51 -07002496 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002497 */
2498hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002499 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002500
Mike Frysingerdc727792020-04-10 01:41:13 -04002501 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002502 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002503
Robert Ginda579186b2012-09-26 11:40:04 -07002504 // The moveCount is the number of rows we need to relocate to make room for
2505 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002506 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002507 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002508 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002509 }
rginda8ba33642011-12-14 12:31:31 -08002510
Mike Frysingerdc727792020-04-10 01:41:13 -04002511 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002512 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002513 this.screen_.clearCursorRow();
2514 }
rginda8ba33642011-12-14 12:31:31 -08002515};
2516
2517/**
2518 * VT command to delete lines at the current cursor row.
2519 *
2520 * New rows are added to the bottom of scroll region to take their place. New
2521 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002522 *
2523 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002524 */
2525hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002526 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002527
Mike Frysingerdc727792020-04-10 01:41:13 -04002528 const top = cursor.row;
2529 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002530
Mike Frysingerdc727792020-04-10 01:41:13 -04002531 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002532 count = Math.min(count, maxCount);
2533
Mike Frysingerdc727792020-04-10 01:41:13 -04002534 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002535 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002536 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002537 }
rginda8ba33642011-12-14 12:31:31 -08002538
Mike Frysingerdc727792020-04-10 01:41:13 -04002539 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002540 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002541 this.screen_.clearCursorRow();
2542 }
2543
rginda87b86462011-12-14 13:48:03 -08002544 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002545 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002546};
2547
2548/**
2549 * Inserts the given number of spaces at the current cursor position.
2550 *
rginda87b86462011-12-14 13:48:03 -08002551 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002552 *
2553 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002554 */
2555hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002556 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002557
Mike Frysinger73e56462019-07-17 00:23:46 -05002558 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002559 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002560 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002561
2562 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002563 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002564};
2565
2566/**
2567 * Forward-delete the specified number of characters starting at the cursor
2568 * position.
2569 *
Joel Hockey0f933582019-08-27 18:01:51 -07002570 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002571 */
2572hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002573 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002574 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002575 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002576 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002577 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002578 this.restoreCursor(cursor);
2579 }
2580
David Benjamin54e8bf62012-06-01 22:31:40 -04002581 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002582};
2583
2584/**
2585 * Shift rows in the scroll region upwards by a given number of lines.
2586 *
2587 * New rows are inserted at the bottom of the scroll region to fill the
2588 * vacated rows. The new rows not filled out with the current text attributes.
2589 *
2590 * This function does not affect the scrollback rows at all. Rows shifted
2591 * off the top are lost.
2592 *
rginda87b86462011-12-14 13:48:03 -08002593 * The cursor position is not altered.
2594 *
Joel Hockey0f933582019-08-27 18:01:51 -07002595 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002596 */
2597hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002598 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002599
rginda87b86462011-12-14 13:48:03 -08002600 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002601 this.deleteLines(count);
2602
rginda87b86462011-12-14 13:48:03 -08002603 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002604};
2605
2606/**
2607 * Shift rows below the cursor down by a given number of lines.
2608 *
2609 * This function respects the current scroll region.
2610 *
2611 * New rows are inserted at the top of the scroll region to fill the
2612 * vacated rows. The new rows not filled out with the current text attributes.
2613 *
2614 * This function does not affect the scrollback rows at all. Rows shifted
2615 * off the bottom are lost.
2616 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002617 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002618 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002619hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002620 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002621
rginda87b86462011-12-14 13:48:03 -08002622 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002623 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002624
rginda87b86462011-12-14 13:48:03 -08002625 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002626};
2627
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002628/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002629 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002630 *
2631 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002632 * cause Assitive Technology to announce the output of the terminal. It also
2633 * enables other features that aid assistive technology. All the features gated
2634 * behind this flag have a performance impact on the terminal which is why they
2635 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002636 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002637 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002638 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002639hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002640 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002641};
rginda87b86462011-12-14 13:48:03 -08002642
rginda8ba33642011-12-14 12:31:31 -08002643/**
2644 * Set the cursor position.
2645 *
2646 * The cursor row is relative to the scroll region if the terminal has
2647 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2648 *
Joel Hockey0f933582019-08-27 18:01:51 -07002649 * @param {number} row The new zero-based cursor row.
2650 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002651 */
2652hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2653 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002654 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002655 } else {
rginda87b86462011-12-14 13:48:03 -08002656 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002657 }
rginda87b86462011-12-14 13:48:03 -08002658};
rginda8ba33642011-12-14 12:31:31 -08002659
Evan Jones2600d4f2016-12-06 09:29:36 -05002660/**
2661 * Move the cursor relative to its current position.
2662 *
2663 * @param {number} row
2664 * @param {number} column
2665 */
rginda87b86462011-12-14 13:48:03 -08002666hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002667 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002668 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2669 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002670 this.screen_.setCursorPosition(row, column);
2671};
2672
Evan Jones2600d4f2016-12-06 09:29:36 -05002673/**
2674 * Move the cursor to the specified position.
2675 *
2676 * @param {number} row
2677 * @param {number} column
2678 */
rginda87b86462011-12-14 13:48:03 -08002679hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002680 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2681 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002682 this.screen_.setCursorPosition(row, column);
2683};
2684
2685/**
2686 * Set the cursor column.
2687 *
Joel Hockey0f933582019-08-27 18:01:51 -07002688 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002689 */
2690hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002691 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002692};
2693
2694/**
2695 * Return the cursor column.
2696 *
Joel Hockey0f933582019-08-27 18:01:51 -07002697 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002698 */
2699hterm.Terminal.prototype.getCursorColumn = function() {
2700 return this.screen_.cursorPosition.column;
2701};
2702
2703/**
2704 * Set the cursor row.
2705 *
2706 * The cursor row is relative to the scroll region if the terminal has
2707 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2708 *
Joel Hockey0f933582019-08-27 18:01:51 -07002709 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002710 */
rginda87b86462011-12-14 13:48:03 -08002711hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2712 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002713};
2714
2715/**
2716 * Return the cursor row.
2717 *
Joel Hockey0f933582019-08-27 18:01:51 -07002718 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002719 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002720hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002721 return this.screen_.cursorPosition.row;
2722};
2723
2724/**
2725 * Request that the ScrollPort redraw itself soon.
2726 *
2727 * The redraw will happen asynchronously, soon after the call stack winds down.
2728 * Multiple calls will be coalesced into a single redraw.
2729 */
2730hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002731 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002732 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002733 }
rginda8ba33642011-12-14 12:31:31 -08002734
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002735 this.timeouts_.redraw = setTimeout(() => {
2736 delete this.timeouts_.redraw;
2737 this.scrollPort_.redraw_();
2738 });
rginda8ba33642011-12-14 12:31:31 -08002739};
2740
2741/**
2742 * Request that the ScrollPort be scrolled to the bottom.
2743 *
2744 * The scroll will happen asynchronously, soon after the call stack winds down.
2745 * Multiple calls will be coalesced into a single scroll.
2746 *
2747 * This affects the scrollbar position of the ScrollPort, and has nothing to
2748 * do with the VT scroll commands.
2749 */
2750hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002751 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002752 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002753 }
rginda8ba33642011-12-14 12:31:31 -08002754
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002755 this.timeouts_.scrollDown = setTimeout(() => {
2756 delete this.timeouts_.scrollDown;
2757 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2758 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002759};
2760
2761/**
2762 * Move the cursor up a specified number of rows.
2763 *
Joel Hockey0f933582019-08-27 18:01:51 -07002764 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002765 */
2766hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002767 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002768};
2769
2770/**
2771 * Move the cursor down a specified number of rows.
2772 *
Joel Hockey0f933582019-08-27 18:01:51 -07002773 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002774 */
2775hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002776 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002777 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2778 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2779 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002780
Mike Frysingerdc727792020-04-10 01:41:13 -04002781 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2782 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002783 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002784};
2785
2786/**
2787 * Move the cursor left a specified number of columns.
2788 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002789 * If reverse wraparound mode is enabled and the previous row wrapped into
2790 * the current row then we back up through the wraparound as well.
2791 *
Joel Hockey0f933582019-08-27 18:01:51 -07002792 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002793 */
2794hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002795 count = count || 1;
2796
Mike Frysingerbdb34802020-04-07 03:47:32 -04002797 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002798 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002799 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002800
Mike Frysingerdc727792020-04-10 01:41:13 -04002801 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002802 if (this.options_.reverseWraparound) {
2803 if (this.screen_.cursorPosition.overflow) {
2804 // If this cursor is in the right margin, consume one count to get it
2805 // back to the last column. This only applies when we're in reverse
2806 // wraparound mode.
2807 count--;
2808 this.clearCursorOverflow();
2809
Mike Frysingerbdb34802020-04-07 03:47:32 -04002810 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002811 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002812 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002813 }
2814
Mike Frysingerdc727792020-04-10 01:41:13 -04002815 let newRow = this.screen_.cursorPosition.row;
2816 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002817 if (newColumn < 0) {
2818 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2819 if (newRow < 0) {
2820 // xterm also wraps from row 0 to the last row.
2821 newRow = this.screenSize.height + newRow % this.screenSize.height;
2822 }
2823 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2824 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002825
Robert Gindabfb32622014-07-17 13:20:27 -07002826 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2827
2828 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002829 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002830 this.setCursorColumn(newColumn);
2831 }
rginda8ba33642011-12-14 12:31:31 -08002832};
2833
2834/**
2835 * Move the cursor right a specified number of columns.
2836 *
Joel Hockey0f933582019-08-27 18:01:51 -07002837 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002838 */
2839hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002840 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002841
Mike Frysingerbdb34802020-04-07 03:47:32 -04002842 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002843 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002844 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002845
Mike Frysingerdc727792020-04-10 01:41:13 -04002846 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2847 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002848 this.setCursorColumn(column);
2849};
2850
2851/**
2852 * Reverse the foreground and background colors of the terminal.
2853 *
2854 * This only affects text that was drawn with no attributes.
2855 *
2856 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2857 * been drawn with attributes that happen to coincide with the default
2858 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002859 *
2860 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002861 */
2862hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002863 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002864 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002865 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2866 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002867 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002868 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2869 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002870 }
2871};
2872
2873/**
rginda87b86462011-12-14 13:48:03 -08002874 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002875 *
2876 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002877 */
2878hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002879 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002880
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002881 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002882
Michael Kelly485ecd12014-06-09 11:41:56 -04002883 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002884 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002885 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002886 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002887
Robert Ginda92e18102013-03-14 13:56:37 -07002888 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002889 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002890 this.bellSequelchTimeout_ = setTimeout(() => {
2891 this.bellSquelchTimeout_ = null;
2892 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002893 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002894 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002895 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002896
2897 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002898 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002899 this.bellNotificationList_.push(n);
2900 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002901 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002902 }
rginda87b86462011-12-14 13:48:03 -08002903};
2904
2905/**
rginda8ba33642011-12-14 12:31:31 -08002906 * Set the origin mode bit.
2907 *
2908 * If origin mode is on, certain VT cursor and scrolling commands measure their
2909 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2910 * to the top of the addressable screen.
2911 *
2912 * Defaults to off.
2913 *
2914 * @param {boolean} state True to set origin mode, false to unset.
2915 */
2916hterm.Terminal.prototype.setOriginMode = function(state) {
2917 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002918 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002919};
2920
2921/**
2922 * Set the insert mode bit.
2923 *
2924 * If insert mode is on, existing text beyond the cursor position will be
2925 * shifted right to make room for new text. Otherwise, new text overwrites
2926 * any existing text.
2927 *
2928 * Defaults to off.
2929 *
2930 * @param {boolean} state True to set insert mode, false to unset.
2931 */
2932hterm.Terminal.prototype.setInsertMode = function(state) {
2933 this.options_.insertMode = state;
2934};
2935
2936/**
rginda87b86462011-12-14 13:48:03 -08002937 * Set the auto carriage return bit.
2938 *
2939 * If auto carriage return is on then a formfeed character is interpreted
2940 * as a newline, otherwise it's the same as a linefeed. The difference boils
2941 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002942 *
2943 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002944 */
2945hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2946 this.options_.autoCarriageReturn = state;
2947};
2948
2949/**
rginda8ba33642011-12-14 12:31:31 -08002950 * Set the wraparound mode bit.
2951 *
2952 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2953 * to the start of the following row. Otherwise, the cursor is clamped to the
2954 * end of the screen and attempts to write past it are ignored.
2955 *
2956 * Defaults to on.
2957 *
2958 * @param {boolean} state True to set wraparound mode, false to unset.
2959 */
2960hterm.Terminal.prototype.setWraparound = function(state) {
2961 this.options_.wraparound = state;
2962};
2963
2964/**
2965 * Set the reverse-wraparound mode bit.
2966 *
2967 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2968 * to the end of the previous row. Otherwise, the cursor is clamped to column
2969 * 0.
2970 *
2971 * Defaults to off.
2972 *
2973 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2974 */
2975hterm.Terminal.prototype.setReverseWraparound = function(state) {
2976 this.options_.reverseWraparound = state;
2977};
2978
2979/**
2980 * Selects between the primary and alternate screens.
2981 *
2982 * If alternate mode is on, the alternate screen is active. Otherwise the
2983 * primary screen is active.
2984 *
2985 * Swapping screens has no effect on the scrollback buffer.
2986 *
2987 * Each screen maintains its own cursor position.
2988 *
2989 * Defaults to off.
2990 *
2991 * @param {boolean} state True to set alternate mode, false to unset.
2992 */
2993hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002994 if (state == (this.screen_ == this.alternateScreen_)) {
2995 return;
2996 }
2997 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2998 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002999 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
3000
Joel Hockey42dba8f2020-03-26 16:21:11 -07003001 // Swap color overrides.
3002 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
3003 oldOverrides.forEach((c, i) => {
3004 if (!newOverrides.hasOwnProperty(i)) {
3005 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
3006 }
3007 });
3008 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
3009
rginda35c456b2012-02-09 17:29:05 -08003010 if (this.screen_.rowsArray.length &&
3011 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3012 // If the screen changed sizes while we were away, our rowIndexes may
3013 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003014 const offset = this.scrollbackRows_.length;
3015 const ary = this.screen_.rowsArray;
3016 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003017 ary[i].rowIndex = offset + i;
3018 }
3019 }
rginda8ba33642011-12-14 12:31:31 -08003020
rginda35c456b2012-02-09 17:29:05 -08003021 this.realizeWidth_(this.screenSize.width);
3022 this.realizeHeight_(this.screenSize.height);
3023 this.scrollPort_.syncScrollHeight();
3024 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003025
rginda6d397402012-01-17 10:58:29 -08003026 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003027 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003028};
3029
3030/**
3031 * Set the cursor-blink mode bit.
3032 *
3033 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3034 * a visible cursor does not blink.
3035 *
3036 * You should make sure to turn blinking off if you're going to dispose of a
3037 * terminal, otherwise you'll leak a timeout.
3038 *
3039 * Defaults to on.
3040 *
3041 * @param {boolean} state True to set cursor-blink mode, false to unset.
3042 */
3043hterm.Terminal.prototype.setCursorBlink = function(state) {
3044 this.options_.cursorBlink = state;
3045
3046 if (!state && this.timeouts_.cursorBlink) {
3047 clearTimeout(this.timeouts_.cursorBlink);
3048 delete this.timeouts_.cursorBlink;
3049 }
3050
Mike Frysingerbdb34802020-04-07 03:47:32 -04003051 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003052 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003053 }
rginda8ba33642011-12-14 12:31:31 -08003054};
3055
3056/**
3057 * Set the cursor-visible mode bit.
3058 *
3059 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3060 *
3061 * Defaults to on.
3062 *
3063 * @param {boolean} state True to set cursor-visible mode, false to unset.
3064 */
3065hterm.Terminal.prototype.setCursorVisible = function(state) {
3066 this.options_.cursorVisible = state;
3067
3068 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003069 if (this.timeouts_.cursorBlink) {
3070 clearTimeout(this.timeouts_.cursorBlink);
3071 delete this.timeouts_.cursorBlink;
3072 }
rginda87b86462011-12-14 13:48:03 -08003073 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003074 return;
3075 }
3076
rginda87b86462011-12-14 13:48:03 -08003077 this.syncCursorPosition_();
3078
3079 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003080
3081 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003082 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003083 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003084 }
rginda8ba33642011-12-14 12:31:31 -08003085
Robert Gindaea2183e2014-07-17 09:51:51 -07003086 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003087 } else {
3088 if (this.timeouts_.cursorBlink) {
3089 clearTimeout(this.timeouts_.cursorBlink);
3090 delete this.timeouts_.cursorBlink;
3091 }
3092 }
3093};
3094
3095/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003096 * Pause blinking temporarily.
3097 *
3098 * When the cursor moves around, it can be helpful to momentarily pause the
3099 * blinking. This could be when the user is typing in things, or when they're
3100 * moving around with the arrow keys.
3101 */
3102hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3103 if (!this.options_.cursorBlink) {
3104 return;
3105 }
3106
3107 this.cursorBlinkPause_ = true;
3108
3109 // If a timeout is already pending, reset the clock due to the new input.
3110 if (this.timeouts_.cursorBlinkPause) {
3111 clearTimeout(this.timeouts_.cursorBlinkPause);
3112 }
3113 // After 500ms, resume blinking. That seems like a good balance between user
3114 // input timings & responsiveness to resume.
3115 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3116 delete this.timeouts_.cursorBlinkPause;
3117 this.cursorBlinkPause_ = false;
3118 }, 500);
3119};
3120
3121/**
rginda87b86462011-12-14 13:48:03 -08003122 * Synchronizes the visible cursor and document selection with the current
3123 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003124 *
3125 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003126 */
3127hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003128 const topRowIndex = this.scrollPort_.getTopRowIndex();
3129 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3130 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003131 this.screen_.cursorPosition.row;
3132
Raymes Khoury15697f42018-07-17 11:37:18 +10003133 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003134 if (this.accessibilityReader_.accessibilityEnabled) {
3135 // Report the new position of the cursor for accessibility purposes.
3136 const cursorColumnIndex = this.screen_.cursorPosition.column;
3137 const cursorLineText =
3138 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003139 // This will force the selection to be sync'd to the cursor position if the
3140 // user has pressed a key. Generally we would only sync the cursor position
3141 // when selection is collapsed so that if the user has selected something
3142 // we don't clear the selection by moving the selection. However when a
3143 // screen reader is used, it's intuitive for entering a key to move the
3144 // selection to the cursor.
3145 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003146 this.accessibilityReader_.afterCursorChange(
3147 cursorLineText, cursorRowIndex, cursorColumnIndex);
3148 }
3149
rginda8ba33642011-12-14 12:31:31 -08003150 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003151 // Cursor is scrolled off screen, hide it.
3152 this.cursorOffScreen_ = true;
3153 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003154 return false;
rginda8ba33642011-12-14 12:31:31 -08003155 }
3156
Joel Hockey3babf302020-04-22 15:00:06 -07003157 if (this.cursorNode_.style.display == 'none') {
3158 // Re-display the terminal cursor if it was hidden.
3159 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003160 this.cursorNode_.style.display = '';
3161 }
3162
Mike Frysinger44c32202017-08-05 01:13:09 -04003163 // Position the cursor using CSS variable math. If we do the math in JS,
3164 // the float math will end up being more precise than the CSS which will
3165 // cause the cursor tracking to be off.
3166 this.setCssVar(
3167 'cursor-offset-row',
3168 `${cursorRowIndex - topRowIndex} + ` +
3169 `${this.scrollPort_.visibleRowTopMargin}px`);
3170 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003171
3172 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003173 '(' + this.screen_.cursorPosition.column +
3174 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003175 ')');
3176
3177 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003178 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003179 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003180 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003181 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003182 return true;
rginda8ba33642011-12-14 12:31:31 -08003183};
3184
Robert Gindafb1be6a2013-12-11 11:56:22 -08003185/**
3186 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3187 * and character cell dimensions.
3188 */
Robert Ginda830583c2013-08-07 13:20:46 -07003189hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003190 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003191
3192 if (this.cursorNode_.getAttribute('focus') == 'false') {
3193 // Always show a block cursor when unfocused.
3194 shape = hterm.Terminal.cursorShape.BLOCK;
3195 }
3196
Mike Frysingerdc727792020-04-10 01:41:13 -04003197 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003198
3199 switch (shape) {
3200 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003201 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003202 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003203 style.borderLeftStyle = 'solid';
3204 break;
3205
3206 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003207 style.backgroundColor = 'transparent';
3208 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003209 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003210 break;
3211
3212 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003213 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003214 style.borderBottomStyle = '';
3215 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003216 break;
3217 }
3218};
3219
rginda8ba33642011-12-14 12:31:31 -08003220/**
3221 * Synchronizes the visible cursor with the current cursor coordinates.
3222 *
3223 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003224 * Multiple calls will be coalesced into a single sync. This should be called
3225 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003226 */
3227hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003228 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003229 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003230 }
rginda8ba33642011-12-14 12:31:31 -08003231
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003232 if (this.accessibilityReader_.accessibilityEnabled) {
3233 // Report the previous position of the cursor for accessibility purposes.
3234 const cursorRowIndex = this.scrollbackRows_.length +
3235 this.screen_.cursorPosition.row;
3236 const cursorColumnIndex = this.screen_.cursorPosition.column;
3237 const cursorLineText =
3238 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3239 this.accessibilityReader_.beforeCursorChange(
3240 cursorLineText, cursorRowIndex, cursorColumnIndex);
3241 }
3242
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003243 this.timeouts_.syncCursor = setTimeout(() => {
3244 this.syncCursorPosition_();
3245 delete this.timeouts_.syncCursor;
3246 });
rginda87b86462011-12-14 13:48:03 -08003247};
3248
rgindacc2996c2012-02-24 14:59:31 -08003249/**
rgindaf522ce02012-04-17 17:49:17 -07003250 * Show or hide the zoom warning.
3251 *
3252 * The zoom warning is a message warning the user that their browser zoom must
3253 * be set to 100% in order for hterm to function properly.
3254 *
3255 * @param {boolean} state True to show the message, false to hide it.
3256 */
3257hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3258 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003259 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003260 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003261 }
rgindaf522ce02012-04-17 17:49:17 -07003262
3263 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003264 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003265 this.zoomWarningNode_.style.cssText = (
3266 'color: black;' +
3267 'background-color: #ff2222;' +
3268 'font-size: large;' +
3269 'border-radius: 8px;' +
3270 'opacity: 0.75;' +
3271 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3272 'top: 0.5em;' +
3273 'right: 1.2em;' +
3274 'position: absolute;' +
3275 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003276 '-webkit-user-select: none;' +
3277 '-moz-text-size-adjust: none;' +
3278 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003279
3280 this.zoomWarningNode_.addEventListener('click', function(e) {
3281 this.parentNode.removeChild(this);
3282 });
rgindaf522ce02012-04-17 17:49:17 -07003283 }
3284
Mike Frysingerb7289952019-03-23 16:05:38 -07003285 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003286 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003287 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003288
rgindaf522ce02012-04-17 17:49:17 -07003289 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3290
3291 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003292 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003293 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003294 }
rgindaf522ce02012-04-17 17:49:17 -07003295 } else if (this.zoomWarningNode_.parentNode) {
3296 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3297 }
3298};
3299
3300/**
rgindacc2996c2012-02-24 14:59:31 -08003301 * Show the terminal overlay for a given amount of time.
3302 *
Jason Lin3d825782020-05-12 11:02:48 +10003303 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003304 *
3305 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003306 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003307 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3308 * stay up forever (or until the next overlay).
3309 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003310hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003311 this.showOverlayWithNode(new Text(msg), timeout);
3312};
3313
3314/**
3315 * Show the terminal overlay for a given amount of time.
3316 *
3317 * The terminal overlay appears in inverse video, centered over the terminal.
3318 *
3319 * @param {!Node} node The node to display in the overlay.
3320 * @param {?number=} timeout The amount of time to wait before fading out
3321 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3322 * stay up forever (or until the next overlay).
3323 */
3324hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003325 if (!this.ready_ || !this.div_) {
3326 return;
3327 }
rgindaf0090c92012-02-10 14:58:52 -08003328
Joel Hockeyedac0e72020-05-14 20:16:20 -07003329 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003330 this.overlayNode_ = this.document_.createElement('div');
3331 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003332 'color: rgb(var(--hterm-background-color));' +
3333 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003334 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003335 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003336 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003337 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003338 'position: absolute;' +
3339 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003340 '-webkit-transition: opacity 180ms ease-in;' +
3341 '-moz-user-select: none;' +
3342 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003343
3344 this.overlayNode_.addEventListener('mousedown', function(e) {
3345 e.preventDefault();
3346 e.stopPropagation();
3347 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003348 }
3349
Jason Lin34567412020-05-14 10:32:09 +10003350 this.overlayNode_.textContent = ''; // Remove all children first.
3351 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003352
Mike Frysingerbdb34802020-04-07 03:47:32 -04003353 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003354 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003355 }
rgindaf0090c92012-02-10 14:58:52 -08003356
Mike Frysingerdc727792020-04-10 01:41:13 -04003357 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3358 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003359
Robert Ginda8a59f762014-07-23 11:29:55 -07003360 this.overlayNode_.style.top =
3361 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003362 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003363 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003364
Mike Frysingerbdb34802020-04-07 03:47:32 -04003365 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003366 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003367 }
rgindaf0090c92012-02-10 14:58:52 -08003368
Jason Lin34567412020-05-14 10:32:09 +10003369 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003370
Mike Frysingerec4225d2020-04-07 05:00:01 -04003371 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003372 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003373 }
rgindacc2996c2012-02-24 14:59:31 -08003374
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003375 this.overlayTimeout_ = setTimeout(() => {
3376 this.overlayNode_.style.opacity = '0';
3377 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003378 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003379};
3380
3381/**
3382 * Hide the terminal overlay immediately.
3383 *
3384 * Useful when we show an overlay for an event with an unknown end time.
3385 */
3386hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003387 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003388 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003389 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003390 this.overlayTimeout_ = null;
3391
Mike Frysingerbdb34802020-04-07 03:47:32 -04003392 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003393 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003394 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003395 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003396};
3397
rginda4bba5e12012-06-20 16:15:30 -07003398/**
3399 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003400 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003401 * Note: In Chrome, this should work unless the user has rejected the permission
3402 * request. In Firefox extension environment, you'll need the "clipboardRead"
3403 * permission. In other environments, this might always fail as the browser
3404 * frequently blocks access for security reasons.
3405 *
3406 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3407 * value is always null. Otherwise, this function uses legacy pasting and
3408 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003409 */
3410hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003411 if (!this.alwaysUseLegacyPasting &&
3412 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003413 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3414 return null;
3415 } else {
3416 // Legacy pasting.
3417 try {
3418 return this.document_.execCommand('paste');
3419 } catch (firefoxException) {
3420 // Ignore this. FF 40 and older would incorrectly throw an exception if
3421 // there was an error instead of returning false.
3422 return false;
3423 }
3424 }
rginda4bba5e12012-06-20 16:15:30 -07003425};
3426
3427/**
3428 * Copy a string to the system clipboard.
3429 *
3430 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003431 *
3432 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003433 */
3434hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003435 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003436 if (!this.clipboardNotice_) {
3437 this.clipboardNotice_ = this.document_.createElement('div');
3438 this.clipboardNotice_.style.textAlign = 'center';
3439 const copyImage = lib.resource.getData('hterm/images/copy');
3440 this.clipboardNotice_.innerHTML =
3441 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3442 }
3443 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003444 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003445
Mike Frysinger96eacae2019-01-02 18:13:56 -05003446 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003447};
3448
Evan Jones2600d4f2016-12-06 09:29:36 -05003449/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003450 * Display an image.
3451 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003452 * Either URI or buffer or blob fields must be specified.
3453 *
Joel Hockey0f933582019-08-27 18:01:51 -07003454 * @param {{
3455 * name: (string|undefined),
3456 * size: (string|number|undefined),
3457 * preserveAspectRation: (boolean|undefined),
3458 * inline: (boolean|undefined),
3459 * width: (string|number|undefined),
3460 * height: (string|number|undefined),
3461 * align: (string|undefined),
3462 * url: (string|undefined),
3463 * buffer: (!ArrayBuffer|undefined),
3464 * blob: (!Blob|undefined),
3465 * type: (string|undefined),
3466 * }} options The image to display.
3467 * name A human readable string for the image
3468 * size The size (in bytes).
3469 * preserveAspectRatio Whether to preserve aspect.
3470 * inline Whether to display the image inline.
3471 * width The width of the image.
3472 * height The height of the image.
3473 * align Direction to align the image.
3474 * uri The source URI for the image.
3475 * buffer The ArrayBuffer image data.
3476 * blob The Blob image data.
3477 * type The MIME type of the image data.
3478 * @param {function()=} onLoad Callback when loading finishes.
3479 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003480 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003481hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003482 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003483 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003484 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003485 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003486 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003487
3488 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003489 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003490 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003491 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003492
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003493 // See if the mime type is available. If not, guess from the filename.
3494 // We don't list all possible mime types because the browser can usually
3495 // guess it correctly. So list the ones that need a bit more help.
3496 if (!options.type) {
3497 const ary = options.name.split('.');
3498 const ext = ary[ary.length - 1].trim();
3499 switch (ext) {
3500 case 'svg':
3501 case 'svgz':
3502 options.type = 'image/svg+xml';
3503 break;
3504 }
3505 }
3506
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003507 // Has the user approved image display yet?
3508 if (this.allowImagesInline !== true) {
3509 this.newLine();
3510 const row = this.getRowNode(this.scrollbackRows_.length +
3511 this.getCursorRow() - 1);
3512
3513 if (this.allowImagesInline === false) {
3514 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3515 'Inline Images Disabled');
3516 return;
3517 }
3518
3519 // Show a prompt.
3520 let button;
3521 const span = this.document_.createElement('span');
3522 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3523 span.style.fontWeight = 'bold';
3524 span.style.borderWidth = '1px';
3525 span.style.borderStyle = 'dashed';
3526 button = this.document_.createElement('span');
3527 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3528 button.style.marginLeft = '1em';
3529 button.style.borderWidth = '1px';
3530 button.style.borderStyle = 'solid';
3531 button.addEventListener('click', () => {
3532 this.prefs_.set('allow-images-inline', false);
3533 });
3534 span.appendChild(button);
3535 button = this.document_.createElement('span');
3536 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3537 'allow this session');
3538 button.style.marginLeft = '1em';
3539 button.style.borderWidth = '1px';
3540 button.style.borderStyle = 'solid';
3541 button.addEventListener('click', () => {
3542 this.allowImagesInline = true;
3543 });
3544 span.appendChild(button);
3545 button = this.document_.createElement('span');
3546 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3547 button.style.marginLeft = '1em';
3548 button.style.borderWidth = '1px';
3549 button.style.borderStyle = 'solid';
3550 button.addEventListener('click', () => {
3551 this.prefs_.set('allow-images-inline', true);
3552 });
3553 span.appendChild(button);
3554
3555 row.appendChild(span);
3556 return;
3557 }
3558
3559 // See if we should show this object directly, or download it.
3560 if (options.inline) {
3561 const io = this.io.push();
3562 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003563 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003564
3565 // While we're loading the image, eat all the user's input.
3566 io.onVTKeystroke = io.sendString = () => {};
3567
3568 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003569 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003570 if (options.uri !== undefined) {
3571 img.src = options.uri;
3572 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003573 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003574 img.src = URL.createObjectURL(blob);
3575 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003576 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003577 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003578 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003579 img.title = img.alt = options.name;
3580
3581 // Attach the image to the page to let it load/render. It won't stay here.
3582 // This is needed so it's visible and the DOM can calculate the height. If
3583 // the image is hidden or not in the DOM, the height is always 0.
3584 this.document_.body.appendChild(img);
3585
3586 // Wait for the image to finish loading before we try moving it to the
3587 // right place in the terminal.
3588 img.onload = () => {
3589 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003590 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003591 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003592 img.style.maxWidth = `${screenSize.width}px`;
3593 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003594
3595 // Parse a width/height specification.
3596 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003597 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003598 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003599 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003600
3601 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3602 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003603 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003604 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003605 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003606 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003607 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003608 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003609 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003610 }
3611
3612 return '';
3613 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003614 img.style.width = parseDim(
3615 options.width, screenSize.width, '--hterm-charsize-width');
3616 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003617 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003618
3619 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003620 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003621 const padRows = Math.ceil(img.clientHeight /
3622 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003623 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003624 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003625 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003626
3627 // Update the max height in case the user shrinks the character size.
3628 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3629
3630 // Move the image to the last row. This way when we scroll up, it doesn't
3631 // disappear when the first row gets clipped. It will disappear when we
3632 // scroll down and the last row is clipped ...
3633 this.document_.body.removeChild(img);
3634 // Create a wrapper node so we can do an absolute in a relative position.
3635 // This helps with rounding errors between JS & CSS counts.
3636 const div = this.document_.createElement('div');
3637 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003638 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003639 img.style.position = 'absolute';
3640 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3641 div.appendChild(img);
3642 const row = this.getRowNode(this.scrollbackRows_.length +
3643 this.getCursorRow() - 1);
3644 row.appendChild(div);
3645
Mike Frysinger2558ed52019-01-14 01:03:41 -05003646 // Now that the image has been read, we can revoke the source.
3647 if (options.uri === undefined) {
3648 URL.revokeObjectURL(img.src);
3649 }
3650
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003651 io.hideOverlay();
3652 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003653
Mike Frysingerbdb34802020-04-07 03:47:32 -04003654 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003655 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003656 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003657 };
3658
3659 // If we got a malformed image, give up.
3660 img.onerror = (e) => {
3661 this.document_.body.removeChild(img);
3662 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003663 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003664 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003665
Mike Frysingerbdb34802020-04-07 03:47:32 -04003666 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003667 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003668 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003669 };
3670 } else {
3671 // We can't use chrome.downloads.download as that requires "downloads"
3672 // permissions, and that works only in extensions, not apps.
3673 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003674 if (options.uri !== undefined) {
3675 a.href = options.uri;
3676 } else if (options.buffer !== undefined) {
3677 const blob = new Blob([options.buffer]);
3678 a.href = URL.createObjectURL(blob);
3679 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003680 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003681 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003682 a.download = options.name;
3683 this.document_.body.appendChild(a);
3684 a.click();
3685 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003686 if (options.uri === undefined) {
3687 URL.revokeObjectURL(a.href);
3688 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003689 }
3690};
3691
3692/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003693 * Returns the selected text, or null if no text is selected.
3694 *
3695 * @return {string|null}
3696 */
rgindaa09e7332012-08-17 12:49:51 -07003697hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003698 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003699 selection.sync();
3700
Mike Frysingerbdb34802020-04-07 03:47:32 -04003701 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003702 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003703 }
rgindaa09e7332012-08-17 12:49:51 -07003704
rgindaa09e7332012-08-17 12:49:51 -07003705 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003706 let startOffset = selection.startOffset;
3707 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003708
Raymes Khoury334625a2018-06-25 10:29:40 +10003709 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003710 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003711 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003712 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003713
Robert Gindafdbb3f22012-09-06 20:23:06 -07003714 if (node.nodeName != 'X-ROW') {
3715 // If the selection doesn't start on an x-row node, then it must be
3716 // somewhere inside the x-row. Add any characters from previous siblings
3717 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003718
3719 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3720 // If node is the text node in a styled span, move up to the span node.
3721 node = node.parentNode;
3722 }
3723
Robert Gindafdbb3f22012-09-06 20:23:06 -07003724 while (node.previousSibling) {
3725 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003726 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003727 }
rgindaa09e7332012-08-17 12:49:51 -07003728 }
3729
3730 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003731 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003732 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003733 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003734
Robert Gindafdbb3f22012-09-06 20:23:06 -07003735 if (node.nodeName != 'X-ROW') {
3736 // If the selection doesn't end on an x-row node, then it must be
3737 // somewhere inside the x-row. Add any characters from following siblings
3738 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003739
3740 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3741 // If node is the text node in a styled span, move up to the span node.
3742 node = node.parentNode;
3743 }
3744
Robert Gindafdbb3f22012-09-06 20:23:06 -07003745 while (node.nextSibling) {
3746 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003747 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003748 }
rgindaa09e7332012-08-17 12:49:51 -07003749 }
3750
Mike Frysingerdc727792020-04-10 01:41:13 -04003751 const rv = this.getRowsText(selection.startRow.rowIndex,
3752 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003753 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003754};
3755
rginda4bba5e12012-06-20 16:15:30 -07003756/**
3757 * Copy the current selection to the system clipboard, then clear it after a
3758 * short delay.
3759 */
3760hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003761 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003762 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003763 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003764 }
rginda4bba5e12012-06-20 16:15:30 -07003765};
3766
Joel Hockey0f933582019-08-27 18:01:51 -07003767/**
3768 * Show overlay with current terminal size.
3769 */
rgindaf0090c92012-02-10 14:58:52 -08003770hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003771 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003772 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003773 }
rgindaf0090c92012-02-10 14:58:52 -08003774};
3775
rginda87b86462011-12-14 13:48:03 -08003776/**
3777 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3778 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003779 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003780 */
3781hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003782 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003783 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003784 }
rginda87b86462011-12-14 13:48:03 -08003785
Mike Frysinger225c99d2019-10-20 14:02:37 -06003786 this.pauseCursorBlink_();
3787
Mike Frysinger79669762018-12-30 20:51:10 -05003788 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003789};
3790
3791/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003792 * Open the selected url.
3793 */
3794hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003795 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003796
3797 // If there is no selection, try and expand wherever they clicked.
3798 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003799 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003800 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003801
3802 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003803 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003804 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003805 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003806 }
3807
3808 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003809 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003810 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003811 }
Mike Frysinger43472622017-06-26 18:11:07 -04003812
3813 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003814 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003815 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3816 // We have to whitelist a few protocols that lack authorities and thus
3817 // never use the //. Like mailto.
3818 switch (str.split(':', 1)[0]) {
3819 case 'mailto':
3820 break;
3821 default:
3822 str = 'http://' + str;
3823 break;
3824 }
3825 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003826
Mike Frysinger720fa832017-10-23 01:15:52 -04003827 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003828};
Mike Frysinger70b94692017-01-26 18:57:50 -10003829
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003830/**
3831 * Manage the automatic mouse hiding behavior while typing.
3832 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003833 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003834 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003835hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003836 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3837 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003838 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003839 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003840 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003841
3842 this.mouseHideWhileTyping_ = !!v;
3843};
3844
3845/**
3846 * Handler for monitoring user keyboard activity.
3847 *
3848 * This isn't for processing the keystrokes directly, but for updating any
3849 * state that might toggle based on the user using the keyboard at all.
3850 *
Joel Hockey0f933582019-08-27 18:01:51 -07003851 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003852 */
3853hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3854 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003855 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003856 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003857 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003858};
Mike Frysinger70b94692017-01-26 18:57:50 -10003859
3860/**
rgindad5613292012-06-19 15:40:37 -07003861 * Add the terminalRow and terminalColumn properties to mouse events and
3862 * then forward on to onMouse().
3863 *
3864 * The terminalRow and terminalColumn properties contain the (row, column)
3865 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003866 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003867 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003868 */
3869hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003870 if (e.processedByTerminalHandler_) {
3871 // We register our event handlers on the document, as well as the cursor
3872 // and the scroll blocker. Mouse events that occur on the cursor or
3873 // scroll blocker will also appear on the document, but we don't want to
3874 // process them twice.
3875 //
3876 // We can't just prevent bubbling because that has other side effects, so
3877 // we decorate the event object with this property instead.
3878 return;
3879 }
3880
Mike Frysinger468966c2018-08-28 13:48:51 -04003881 // Consume navigation events. Button 3 is usually "browser back" and
3882 // button 4 is "browser forward" which we don't want to happen.
3883 if (e.button > 2) {
3884 e.preventDefault();
3885 // We don't return so click events can be passed to the remote below.
3886 }
3887
Mike Frysingerdc727792020-04-10 01:41:13 -04003888 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003889 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3890
rgindafaa74742012-08-21 13:34:03 -07003891 e.processedByTerminalHandler_ = true;
3892
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003893 // Handle auto hiding of mouse cursor while typing.
3894 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3895 // Make sure the mouse cursor is visible.
3896 this.syncMouseStyle();
3897 // This debounce isn't perfect, but should work well enough for such a
3898 // simple implementation. If the user moved the mouse, we enabled this
3899 // debounce, and then moved the mouse just before the timeout, we wouldn't
3900 // debounce that later movement.
3901 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3902 }
3903
Robert Gindaeda48db2014-07-17 09:25:30 -07003904 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003905 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003906 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003907 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003908 this.scrollPort_.characterSize.height) + 1;
3909 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003910 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003911
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003912 // Clamp row and column.
3913 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3914 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3915
3916 // Ignore mousedown in the scrollbar area.
3917 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003918 return;
3919 }
3920
Joel Hockey3babf302020-04-22 15:00:06 -07003921 if (this.options_.cursorVisible && !reportMouseEvents &&
3922 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003923 // If the cursor is visible and we're not sending mouse events to the
3924 // host app, then we want to hide the terminal cursor when the mouse
3925 // cursor is over top. This keeps the terminal cursor from interfering
3926 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003927 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3928 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3929 this.cursorNode_.style.display = 'none';
3930 } else if (this.cursorNode_.style.display == 'none') {
3931 this.cursorNode_.style.display = '';
3932 }
3933 }
rgindad5613292012-06-19 15:40:37 -07003934
Robert Ginda928cf632014-03-05 15:07:41 -08003935 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003936 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003937
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003938 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003939 // If VT mouse reporting is disabled, or has been defeated with
3940 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003941 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003942 this.setSelectionEnabled(true);
3943 } else {
3944 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003945 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003946 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003947 this.setSelectionEnabled(false);
3948 e.preventDefault();
3949 }
3950 }
3951
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003952 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003953 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003954 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003955 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003956 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003957 }
rgindad5613292012-06-19 15:40:37 -07003958 }
3959
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003960 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003961 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003962 // Ignore links created using OSC-8 as those will open by themselves, and
3963 // the visible text is most likely not the URI they want anyways.
3964 if (e.target.className === 'uri-node') {
3965 return;
3966 }
3967
Mike Frysinger70b94692017-01-26 18:57:50 -10003968 // Debounce this event with the dblclick event. If you try to doubleclick
3969 // a URL to open it, Chrome will fire click then dblclick, but we won't
3970 // have expanded the selection text at the first click event.
3971 clearTimeout(this.timeouts_.openUrl);
3972 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3973 500);
3974 return;
3975 }
3976
Mike Frysinger847577f2017-05-23 23:25:57 -04003977 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003978 if (e.ctrlKey && e.button == 2 /* right button */) {
3979 e.preventDefault();
3980 this.contextMenu.show(e, this);
3981 } else if (e.button == this.mousePasteButton ||
3982 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003983 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003984 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003985 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003986 }
3987 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003988
Mike Frysinger2edd3612017-05-24 00:54:39 -04003989 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003990 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003991 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003992 }
3993
3994 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3995 this.scrollBlockerNode_.engaged) {
3996 // Disengage the scroll-blocker after one of these events.
3997 this.scrollBlockerNode_.engaged = false;
3998 this.scrollBlockerNode_.style.top = '-99px';
3999 }
4000
Mike Frysinger3c9fa072017-07-13 10:21:13 -04004001 // Emulate arrow key presses via scroll wheel events.
4002 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
4003 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04004004 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07004005 const delta =
4006 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04004007
Mike Frysinger321063c2018-08-29 15:33:14 -04004008 // Helper to turn a wheel event delta into a series of key presses.
4009 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
4010 if (distance == 0) {
4011 return '';
4012 }
4013
4014 // Convert the scroll distance into a number of rows/cols.
4015 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4016 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4017 return data.repeat(cells);
4018 };
4019
4020 // The order between up/down and left/right doesn't really matter.
4021 this.io.sendString(
4022 // Up/down arrow keys.
4023 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4024 'A', 'B') +
4025 // Left/right arrow keys.
4026 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004027 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004028 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004029
4030 e.preventDefault();
4031 }
4032 }
Robert Ginda928cf632014-03-05 15:07:41 -08004033 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004034 if (!this.scrollBlockerNode_.engaged) {
4035 if (e.type == 'mousedown') {
4036 // Move the scroll-blocker into place if we want to keep the scrollport
4037 // from scrolling.
4038 this.scrollBlockerNode_.engaged = true;
4039 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4040 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4041 } else if (e.type == 'mousemove') {
4042 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4043 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004044 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004045 e.preventDefault();
4046 }
4047 }
Robert Ginda928cf632014-03-05 15:07:41 -08004048
4049 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004050 }
4051
Robert Ginda928cf632014-03-05 15:07:41 -08004052 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4053 // Restore this on mouseup in case it was temporarily defeated with a
4054 // alt-mousedown. Only do this when the selection is empty so that
4055 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004056 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004057 }
rgindad5613292012-06-19 15:40:37 -07004058};
4059
4060/**
4061 * Clients should override this if they care to know about mouse events.
4062 *
4063 * The event parameter will be a normal DOM mouse click event with additional
4064 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004065 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004066 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004067 */
4068hterm.Terminal.prototype.onMouse = function(e) { };
4069
4070/**
rginda8e92a692012-05-20 19:37:20 -07004071 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004072 *
4073 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004074 */
Rob Spies06533ba2014-04-24 11:20:37 -07004075hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4076 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004077 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004078
Mike Frysingerbdb34802020-04-07 03:47:32 -04004079 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004080 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004081 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004082
Mike Frysingerbdb34802020-04-07 03:47:32 -04004083 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004084 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004085 }
rginda8e92a692012-05-20 19:37:20 -07004086};
4087
4088/**
rginda8ba33642011-12-14 12:31:31 -08004089 * React when the ScrollPort is scrolled.
4090 */
4091hterm.Terminal.prototype.onScroll_ = function() {
4092 this.scheduleSyncCursorPosition_();
4093};
4094
4095/**
rginda9846e2f2012-01-27 13:53:33 -08004096 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004097 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004098 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004099 */
4100hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004101 this.onPasteData_(e.text);
4102};
4103
4104/**
4105 * Handle pasted data.
4106 *
4107 * @param {string} data The pasted data.
4108 */
4109hterm.Terminal.prototype.onPasteData_ = function(data) {
4110 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004111 if (this.options_.bracketedPaste) {
4112 // We strip out most escape sequences as they can cause issues (like
4113 // inserting an \x1b[201~ midstream). We pass through whitespace
4114 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4115 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004116 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004117 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4118 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4119 }
Robert Gindaa063b202014-07-21 11:08:25 -07004120
4121 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004122};
4123
4124/**
rgindaa09e7332012-08-17 12:49:51 -07004125 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004126 *
Joel Hockey0f933582019-08-27 18:01:51 -07004127 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004128 */
4129hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004130 if (!this.useDefaultWindowCopy) {
4131 e.preventDefault();
4132 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4133 }
rgindaa09e7332012-08-17 12:49:51 -07004134};
4135
4136/**
rginda8ba33642011-12-14 12:31:31 -08004137 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004138 *
4139 * Note: This function should not directly contain code that alters the internal
4140 * state of the terminal. That kind of code belongs in realizeWidth or
4141 * realizeHeight, so that it can be executed synchronously in the case of a
4142 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004143 */
4144hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004145 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4146 this.scrollPort_.characterSize.width) || 0;
4147 const rowCount = lib.f.smartFloorDivide(
4148 this.scrollPort_.getScreenHeight(),
4149 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004150
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004151 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004152 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004153 // gets removed from the document or during the initial load, and we can't
4154 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004155 // This can also happen if called before the scrollPort calculates the
4156 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004157 return;
4158 }
4159
Mike Frysingerdc727792020-04-10 01:41:13 -04004160 const isNewSize = (columnCount != this.screenSize.width ||
4161 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004162 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004163
4164 // We do this even if the size didn't change, just to be sure everything is
4165 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004166 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004167 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004168
Mike Frysingerbdb34802020-04-07 03:47:32 -04004169 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004170 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004171 }
rgindaa8ba17d2012-08-15 14:41:10 -07004172
Robert Gindafb1be6a2013-12-11 11:56:22 -08004173 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004174 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004175
4176 if (wasScrolledEnd) {
4177 this.scrollEnd();
4178 }
rginda8ba33642011-12-14 12:31:31 -08004179};
4180
4181/**
4182 * Service the cursor blink timeout.
4183 */
4184hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004185 if (!this.options_.cursorBlink) {
4186 delete this.timeouts_.cursorBlink;
4187 return;
4188 }
4189
Robert Ginda830583c2013-08-07 13:20:46 -07004190 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004191 this.cursorNode_.style.opacity == '0' ||
4192 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004193 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004194 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4195 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004196 } else {
rginda87b86462011-12-14 13:48:03 -08004197 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004198 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4199 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004200 }
4201};
David Reveman8f552492012-03-28 12:18:41 -04004202
4203/**
4204 * Set the scrollbar-visible mode bit.
4205 *
4206 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4207 * Otherwise it will not.
4208 *
4209 * Defaults to on.
4210 *
4211 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4212 */
4213hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4214 this.scrollPort_.setScrollbarVisible(state);
4215};
Michael Kelly485ecd12014-06-09 11:41:56 -04004216
4217/**
Rob Spies49039e52014-12-17 13:40:04 -08004218 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004219 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004220 *
4221 * Defaults to 1.
4222 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004223 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004224 */
4225hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4226 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4227};
4228
4229/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004230 * Close all web notifications created by terminal bells.
4231 */
4232hterm.Terminal.prototype.closeBellNotifications_ = function() {
4233 this.bellNotificationList_.forEach(function(n) {
4234 n.close();
4235 });
4236 this.bellNotificationList_.length = 0;
4237};
Raymes Khourye5d48982018-08-02 09:08:32 +10004238
4239/**
4240 * Syncs the cursor position when the scrollport gains focus.
4241 */
4242hterm.Terminal.prototype.onScrollportFocus_ = function() {
4243 // If the cursor is offscreen we set selection to the last row on the screen.
4244 const topRowIndex = this.scrollPort_.getTopRowIndex();
4245 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4246 const selection = this.document_.getSelection();
4247 if (!this.syncCursorPosition_() && selection) {
4248 selection.collapse(this.getRowNode(bottomRowIndex));
4249 }
4250};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004251
4252/**
4253 * Clients can override this if they want to provide an options page.
4254 */
4255hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4256
4257
4258/**
4259 * Called when user selects to open the options page.
4260 */
4261hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4262 this.onOpenOptionsPage();
4263};