blob: fca1f12d1676438220bf67bd98aaeee7b516b37b [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Joel Hockeyedac0e72020-05-14 20:16:20 -070030 // Set to true once terminal is initialized and onTerminalReady() is called.
31 this.ready_ = false;
32
Robert Ginda57f03b42012-09-13 11:02:48 -070033 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
Joel Hockeyd4fca732019-09-20 16:57:03 -070035 /** @type {?hterm.PreferenceManager} */
36 this.prefs_ = null;
37
rginda8ba33642011-12-14 12:31:31 -080038 // Two screen instances.
39 this.primaryScreen_ = new hterm.Screen();
40 this.alternateScreen_ = new hterm.Screen();
41
42 // The "current" screen.
43 this.screen_ = this.primaryScreen_;
44
rginda8ba33642011-12-14 12:31:31 -080045 // The local notion of the screen size. ScreenBuffers also have a size which
46 // indicates their present size. During size changes, the two may disagree.
47 // Also, the inactive screen's size is not altered until it is made the active
48 // screen.
49 this.screenSize = new hterm.Size(0, 0);
50
rginda8ba33642011-12-14 12:31:31 -080051 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080052 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080053 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
54 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080055 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100056 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070057 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070058 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080059
rginda87b86462011-12-14 13:48:03 -080060 // The div that contains this terminal.
61 this.div_ = null;
62
rgindac9bc5502012-01-18 11:48:44 -080063 // The document that contains the scrollPort. Defaulted to the global
64 // document here so that the terminal is functional even if it hasn't been
65 // inserted into a document yet, but re-set in decorate().
66 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080067
rginda8ba33642011-12-14 12:31:31 -080068 // The rows that have scrolled off screen and are no longer addressable.
69 this.scrollbackRows_ = [];
70
rgindac9bc5502012-01-18 11:48:44 -080071 // Saved tab stops.
72 this.tabStops_ = [];
73
David Benjamin66e954d2012-05-05 21:08:12 -040074 // Keep track of whether default tab stops have been erased; after a TBC
75 // clears all tab stops, defaults aren't restored on resize until a reset.
76 this.defaultTabStops = true;
77
rginda8ba33642011-12-14 12:31:31 -080078 // The VT's notion of the top and bottom rows. Used during some VT
79 // cursor positioning and scrolling commands.
80 this.vtScrollTop_ = null;
81 this.vtScrollBottom_ = null;
82
83 // The DIV element for the visible cursor.
84 this.cursorNode_ = null;
85
Robert Ginda830583c2013-08-07 13:20:46 -070086 // The current cursor shape of the terminal.
87 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
88
Robert Gindaea2183e2014-07-17 09:51:51 -070089 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
90 this.cursorBlinkCycle_ = [100, 100];
91
Mike Frysinger225c99d2019-10-20 14:02:37 -060092 // Whether to temporarily disable blinking.
93 this.cursorBlinkPause_ = false;
94
Joel Hockey3babf302020-04-22 15:00:06 -070095 // Cursor is hidden when scrolling up pushes it off the bottom of the screen.
96 this.cursorOffScreen_ = false;
97
Robert Gindaea2183e2014-07-17 09:51:51 -070098 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
99 // cursor on/off servicing.
100 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
101
rginda9f5222b2012-03-05 11:53:28 -0800102 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -0700104 /** @type {?string} */
105 this.backgroundColor_ = null;
106 /** @type {?string} */
107 this.foregroundColor_ = null;
108
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700109 this.screenBorderSize_ = 0;
110
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.scrollOnOutput_ = null;
112 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400113 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800114
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700115 // True if we should override mouse event reporting to allow local selection.
116 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800117
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400118 // Whether to auto hide the mouse cursor when typing.
119 this.setAutomaticMouseHiding();
120 // Timer to keep mouse visible while it's being used.
121 this.mouseHideDelay_ = null;
122
rgindaf0090c92012-02-10 14:58:52 -0800123 // Terminal bell sound.
124 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400125 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800126 this.bellAudio_.setAttribute('preload', 'auto');
127
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000128 // The AccessibilityReader object for announcing command output.
129 this.accessibilityReader_ = null;
130
Mike Frysingercc114512017-09-11 21:39:17 -0400131 // The context menu object.
132 this.contextMenu = new hterm.ContextMenu();
133
Michael Kelly485ecd12014-06-09 11:41:56 -0400134 // All terminal bell notifications that have been generated (not necessarily
135 // shown).
136 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700137 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400138
139 // Whether we have permission to display notifications.
140 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400141
rginda6d397402012-01-17 10:58:29 -0800142 // Cursor position and attributes saved with DECSC.
143 this.savedOptions_ = {};
144
rginda8ba33642011-12-14 12:31:31 -0800145 // The current mode bits for the terminal.
146 this.options_ = new hterm.Options();
147
148 // Timeouts we might need to clear.
149 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800150
151 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800152 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800153
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800154 this.saveCursorAndState(true);
155
Zhu Qunying30d40712017-03-14 16:27:00 -0700156 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800157 this.keyboard = new hterm.Keyboard(this);
158
rginda87b86462011-12-14 13:48:03 -0800159 // General IO interface that can be given to third parties without exposing
160 // the entire terminal object.
161 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800162
rgindad5613292012-06-19 15:40:37 -0700163 // True if mouse-click-drag should scroll the terminal.
164 this.enableMouseDragScroll = true;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400167 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700168 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700169
Zhu Qunying30d40712017-03-14 16:27:00 -0700170 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700171 this.useDefaultWindowCopy = false;
172
173 this.clearSelectionAfterCopy = true;
174
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400175 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800176 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700177
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400178 // Whether we allow images to be shown.
179 this.allowImagesInline = null;
180
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400181 this.reportFocus = false;
182
Jason Linf129f3c2020-03-23 11:52:08 +1100183 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
184 this.alwaysUseLegacyPasting = false;
185
Joel Hockey3a44a442019-10-14 16:22:56 -0700186 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500187 function() { this.onTerminalReady(); }.bind(this));
shivanggargde5387e2020-06-10 01:31:16 +0530188
189 /** @const */
190 this.findBar = new hterm.FindBar(this);
rginda87b86462011-12-14 13:48:03 -0800191};
192
193/**
Robert Ginda830583c2013-08-07 13:20:46 -0700194 * Possible cursor shapes.
195 */
196hterm.Terminal.cursorShape = {
197 BLOCK: 'BLOCK',
198 BEAM: 'BEAM',
Mike Frysinger989f34b2020-04-08 00:53:43 -0400199 UNDERLINE: 'UNDERLINE',
Robert Ginda830583c2013-08-07 13:20:46 -0700200};
201
202/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700203 * Clients should override this to be notified when the terminal is ready
204 * for use.
205 *
206 * The terminal initialization is asynchronous, and shouldn't be used before
207 * this method is called.
208 */
209hterm.Terminal.prototype.onTerminalReady = function() { };
210
211/**
rginda35c456b2012-02-09 17:29:05 -0800212 * Default tab with of 8 to match xterm.
213 */
214hterm.Terminal.prototype.tabWidth = 8;
215
216/**
rginda9f5222b2012-03-05 11:53:28 -0800217 * Select a preference profile.
218 *
219 * This will load the terminal preferences for the given profile name and
220 * associate subsequent preference changes with the new preference profile.
221 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500222 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800223 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400224 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700225 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800226 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400227hterm.Terminal.prototype.setProfile = function(
228 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800230
Mike Frysingerdc727792020-04-10 01:41:13 -0400231 const terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800232
Mike Frysingerbdb34802020-04-07 03:47:32 -0400233 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400235 }
rginda9f5222b2012-03-05 11:53:28 -0800236
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700238
239 /**
240 * Clears and reloads key bindings. Used by preferences
241 * 'keybindings' and 'keybindings-os-defaults'.
242 *
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400243 * @param {*?=} bindings
244 * @param {*?=} useOsDefaults
Joel Hockey95a9e272020-03-16 21:19:53 -0700245 */
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400246 function loadKeyBindings(bindings = null, useOsDefaults = false) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700247 terminal.keyboard.bindings.clear();
248
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400249 // Default to an empty object so we still handle OS defaults.
250 if (bindings === null) {
251 bindings = {};
Joel Hockey95a9e272020-03-16 21:19:53 -0700252 }
253
254 if (!(bindings instanceof Object)) {
255 console.error('Error in keybindings preference: Expected object');
Mike Frysinger5e29dc02020-05-09 18:47:30 -0400256 bindings = {};
257 // Fall through to handle OS defaults.
Joel Hockey95a9e272020-03-16 21:19:53 -0700258 }
259
260 try {
261 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
262 } catch (ex) {
263 console.error('Error in keybindings preference: ' + ex);
264 }
265 }
266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800268 'alt-gr-mode': function(v) {
269 if (v == null) {
270 if (navigator.language.toLowerCase() == 'en-us') {
271 v = 'none';
272 } else {
273 v = 'right-alt';
274 }
275 } else if (typeof v == 'string') {
276 v = v.toLowerCase();
277 } else {
278 v = 'none';
279 }
280
Mike Frysingerbdb34802020-04-07 03:47:32 -0400281 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800282 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400283 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800284
285 terminal.keyboard.altGrMode = v;
286 },
287
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700288 'alt-backspace-is-meta-backspace': function(v) {
289 terminal.keyboard.altBackspaceIsMetaBackspace = v;
290 },
291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'alt-is-meta': function(v) {
293 terminal.keyboard.altIsMeta = v;
294 },
295
296 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400297 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700298 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400299 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700300
301 terminal.keyboard.altSendsWhat = v;
302 },
303
304 'audible-bell-sound': function(v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400305 const ary = v.match(/^lib-resource:(\S+)/);
Robert Gindab4839c22013-02-28 16:52:10 -0800306 if (ary) {
307 terminal.bellAudio_.setAttribute('src',
308 lib.resource.getDataUrl(ary[1]));
309 } else {
310 terminal.bellAudio_.setAttribute('src', v);
311 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 },
313
Michael Kelly485ecd12014-06-09 11:41:56 -0400314 'desktop-notification-bell': function(v) {
315 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700316 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400317 Notification.permission === 'granted';
318 if (!terminal.desktopNotificationBell_) {
319 // Note: We don't call Notification.requestPermission here because
320 // Chrome requires the call be the result of a user action (such as an
321 // onclick handler), and pref listeners are run asynchronously.
322 //
323 // A way of working around this would be to display a dialog in the
324 // terminal with a "click-to-request-permission" button.
325 console.warn('desktop-notification-bell is true but we do not have ' +
326 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400327 }
328 } else {
329 terminal.desktopNotificationBell_ = false;
330 }
331 },
332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'background-color': function(v) {
334 terminal.setBackgroundColor(v);
335 },
336
337 'background-image': function(v) {
338 terminal.scrollPort_.setBackgroundImage(v);
339 },
340
341 'background-size': function(v) {
342 terminal.scrollPort_.setBackgroundSize(v);
343 },
344
345 'background-position': function(v) {
346 terminal.scrollPort_.setBackgroundPosition(v);
347 },
348
349 'backspace-sends-backspace': function(v) {
350 terminal.keyboard.backspaceSendsBackspace = v;
351 },
352
Brad Town18654b62015-03-12 00:27:45 -0700353 'character-map-overrides': function(v) {
354 if (!(v == null || v instanceof Object)) {
355 console.warn('Preference character-map-modifications is not an ' +
356 'object: ' + v);
357 return;
358 }
359
Mike Frysinger095d4062017-06-14 00:29:48 -0700360 terminal.vt.characterMaps.reset();
361 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700362 },
363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'cursor-blink': function(v) {
365 terminal.setCursorBlink(!!v);
366 },
367
Joel Hockey9d10ba12019-05-28 01:25:02 -0700368 'cursor-shape': function(v) {
369 terminal.setCursorShape(v);
370 },
371
Robert Gindaea2183e2014-07-17 09:51:51 -0700372 'cursor-blink-cycle': function(v) {
373 if (v instanceof Array &&
374 typeof v[0] == 'number' &&
375 typeof v[1] == 'number') {
376 terminal.cursorBlinkCycle_ = v;
377 } else if (typeof v == 'number') {
378 terminal.cursorBlinkCycle_ = [v, v];
379 } else {
380 // Fast blink indicates an error.
381 terminal.cursorBlinkCycle_ = [100, 100];
382 }
383 },
384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'cursor-color': function(v) {
386 terminal.setCursorColor(v);
387 },
388
389 'color-palette-overrides': function(v) {
390 if (!(v == null || v instanceof Object || v instanceof Array)) {
391 console.warn('Preference color-palette-overrides is not an array or ' +
392 'object: ' + v);
393 return;
rginda9f5222b2012-03-05 11:53:28 -0800394 }
rginda9f5222b2012-03-05 11:53:28 -0800395
Joel Hockey42dba8f2020-03-26 16:21:11 -0700396 // Call terminal.setColorPalette here and below with the new default
397 // value before changing it in lib.colors.colorPalette to ensure that
398 // CSS vars are updated.
399 lib.colors.stockColorPalette.forEach(
400 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 if (v) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400404 for (const key in v) {
405 const i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700406 if (isNaN(i) || i < 0 || i > 255) {
407 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
408 continue;
409 }
410
411 if (v[i]) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400412 const rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700413 if (rgb) {
414 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700416 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 }
418 }
rginda30f20f62012-04-05 16:36:19 -0700419 }
rginda30f20f62012-04-05 16:36:19 -0700420
Joel Hockey42dba8f2020-03-26 16:21:11 -0700421 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
422 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'copy-on-select': function(v) {
426 terminal.copyOnSelect = !!v;
427 },
rginda9f5222b2012-03-05 11:53:28 -0800428
Rob Spies0bec09b2014-06-06 15:58:09 -0700429 'use-default-window-copy': function(v) {
430 terminal.useDefaultWindowCopy = !!v;
431 },
432
433 'clear-selection-after-copy': function(v) {
434 terminal.clearSelectionAfterCopy = !!v;
435 },
436
Robert Ginda7e5e9522014-03-14 12:23:58 -0700437 'ctrl-plus-minus-zero-zoom': function(v) {
438 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
439 },
440
Robert Gindafb5a3f92014-05-13 14:12:00 -0700441 'ctrl-c-copy': function(v) {
442 terminal.keyboard.ctrlCCopy = v;
443 },
444
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100445 'ctrl-v-paste': function(v) {
446 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700447 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100448 },
449
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700450 'paste-on-drop': function(v) {
451 terminal.scrollPort_.setPasteOnDrop(v);
452 },
453
Masaya Suzuki273aa982014-05-31 07:25:55 +0900454 'east-asian-ambiguous-as-two-column': function(v) {
455 lib.wc.regardCjkAmbiguous = v;
456 },
457
Robert Ginda57f03b42012-09-13 11:02:48 -0700458 'enable-8-bit-control': function(v) {
459 terminal.vt.enable8BitControl = !!v;
460 },
rginda30f20f62012-04-05 16:36:19 -0700461
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 'enable-bold': function(v) {
463 terminal.syncBoldSafeState();
464 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400465
Robert Ginda3e278d72014-03-25 13:18:51 -0700466 'enable-bold-as-bright': function(v) {
467 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
468 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
469 },
470
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400471 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500472 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400473 },
474
Robert Ginda57f03b42012-09-13 11:02:48 -0700475 'enable-clipboard-write': function(v) {
476 terminal.vt.enableClipboardWrite = !!v;
477 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400478
Robert Ginda3755e752013-05-31 13:34:09 -0700479 'enable-dec12': function(v) {
480 terminal.vt.enableDec12 = !!v;
481 },
482
Mike Frysinger38f267d2018-09-07 02:50:59 -0400483 'enable-csi-j-3': function(v) {
484 terminal.vt.enableCsiJ3 = !!v;
485 },
486
shivanggarg2b7b0d52020-07-10 11:01:34 +0530487 'find-result-color': function(v) {
488 terminal.findBar.setFindResultColor(v);
489 },
490
Robert Ginda57f03b42012-09-13 11:02:48 -0700491 'font-family': function(v) {
492 terminal.syncFontFamily();
493 },
rginda30f20f62012-04-05 16:36:19 -0700494
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700496 v = parseInt(v, 10);
Joel Hockey139d82d2020-04-07 23:04:29 -0700497 if (isNaN(v) || v <= 0) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500498 console.error(`Invalid font size: ${v}`);
499 return;
500 }
501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 terminal.setFontSize(v);
503 },
rginda9875d902012-08-20 16:21:57 -0700504
Robert Ginda57f03b42012-09-13 11:02:48 -0700505 'font-smoothing': function(v) {
506 terminal.syncFontFamily();
507 },
rgindade84e382012-04-20 15:39:31 -0700508
Robert Ginda57f03b42012-09-13 11:02:48 -0700509 'foreground-color': function(v) {
510 terminal.setForegroundColor(v);
511 },
rginda30f20f62012-04-05 16:36:19 -0700512
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400513 'hide-mouse-while-typing': function(v) {
514 terminal.setAutomaticMouseHiding(v);
515 },
516
Robert Ginda57f03b42012-09-13 11:02:48 -0700517 'home-keys-scroll': function(v) {
518 terminal.keyboard.homeKeysScroll = v;
519 },
rginda4bba5e12012-06-20 16:15:30 -0700520
Robert Gindaa8165692015-06-15 14:46:31 -0700521 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700522 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
523 },
Robert Gindaa8165692015-06-15 14:46:31 -0700524
Joel Hockey95a9e272020-03-16 21:19:53 -0700525 'keybindings-os-defaults': function(v) {
526 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700527 },
528
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700529 'media-keys-are-fkeys': function(v) {
530 terminal.keyboard.mediaKeysAreFKeys = v;
531 },
532
Robert Ginda57f03b42012-09-13 11:02:48 -0700533 'meta-sends-escape': function(v) {
534 terminal.keyboard.metaSendsEscape = v;
535 },
rginda30f20f62012-04-05 16:36:19 -0700536
Mike Frysinger847577f2017-05-23 23:25:57 -0400537 'mouse-right-click-paste': function(v) {
538 terminal.mouseRightClickPaste = v;
539 },
540
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 'mouse-paste-button': function(v) {
542 terminal.syncMousePasteButton();
543 },
rgindaa8ba17d2012-08-15 14:41:10 -0700544
Robert Gindae76aa9f2014-03-14 12:29:12 -0700545 'page-keys-scroll': function(v) {
546 terminal.keyboard.pageKeysScroll = v;
547 },
548
Robert Ginda40932892012-12-10 17:26:40 -0800549 'pass-alt-number': function(v) {
550 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700551 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800552 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500553 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800554 }
555
556 terminal.passAltNumber = v;
557 },
558
559 'pass-ctrl-number': function(v) {
560 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700561 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800562 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500563 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800564 }
565
566 terminal.passCtrlNumber = v;
567 },
568
Joel Hockey0e052042020-02-19 05:37:19 -0800569 'pass-ctrl-n': function(v) {
570 terminal.passCtrlN = v;
571 },
572
573 'pass-ctrl-t': function(v) {
574 terminal.passCtrlT = v;
575 },
576
577 'pass-ctrl-tab': function(v) {
578 terminal.passCtrlTab = v;
579 },
580
581 'pass-ctrl-w': function(v) {
582 terminal.passCtrlW = v;
583 },
584
Robert Ginda40932892012-12-10 17:26:40 -0800585 'pass-meta-number': function(v) {
586 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700587 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800588 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500589 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800590 }
591
592 terminal.passMetaNumber = v;
593 },
594
Marius Schilder77857b32014-05-14 16:21:26 -0700595 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700596 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700597 },
598
Robert Ginda8cb7d902013-06-20 14:37:18 -0700599 'receive-encoding': function(v) {
600 if (!(/^(utf-8|raw)$/).test(v)) {
601 console.warn('Invalid value for "receive-encoding": ' + v);
602 v = 'utf-8';
603 }
604
605 terminal.vt.characterEncoding = v;
606 },
607
Joel Hockey139d82d2020-04-07 23:04:29 -0700608 'screen-padding-size': function(v) {
609 v = parseInt(v, 10);
610 if (isNaN(v) || v < 0) {
611 console.error(`Invalid screen padding size: ${v}`);
612 return;
613 }
Joel Hockey139d82d2020-04-07 23:04:29 -0700614 terminal.setScreenPaddingSize(v);
615 },
616
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -0700617 'screen-border-size': function(v) {
618 v = parseInt(v, 10);
619 if (isNaN(v) || v < 0) {
620 console.error(`Invalid screen border size: ${v}`);
621 return;
622 }
623 terminal.setScreenBorderSize(v);
624 },
625
626 'screen-border-color': function(v) {
627 terminal.div_.style.borderColor = v;
628 },
629
Robert Ginda57f03b42012-09-13 11:02:48 -0700630 'scroll-on-keystroke': function(v) {
631 terminal.scrollOnKeystroke_ = v;
632 },
rginda9f5222b2012-03-05 11:53:28 -0800633
Robert Ginda57f03b42012-09-13 11:02:48 -0700634 'scroll-on-output': function(v) {
635 terminal.scrollOnOutput_ = v;
636 },
rginda30f20f62012-04-05 16:36:19 -0700637
Robert Ginda57f03b42012-09-13 11:02:48 -0700638 'scrollbar-visible': function(v) {
639 terminal.setScrollbarVisible(v);
640 },
rginda9f5222b2012-03-05 11:53:28 -0800641
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400642 'scroll-wheel-may-send-arrow-keys': function(v) {
643 terminal.scrollWheelArrowKeys_ = v;
644 },
645
Rob Spies49039e52014-12-17 13:40:04 -0800646 'scroll-wheel-move-multiplier': function(v) {
647 terminal.setScrollWheelMoveMultipler(v);
648 },
649
Robert Ginda57f03b42012-09-13 11:02:48 -0700650 'shift-insert-paste': function(v) {
651 terminal.keyboard.shiftInsertPaste = v;
652 },
rginda9f5222b2012-03-05 11:53:28 -0800653
Mike Frysingera7768922017-07-28 15:00:12 -0400654 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400655 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400656 },
657
Robert Gindae76aa9f2014-03-14 12:29:12 -0700658 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400659 terminal.scrollPort_.setUserCssUrl(v);
660 },
661
662 'user-css-text': function(v) {
663 terminal.scrollPort_.setUserCssText(v);
664 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400665
666 'word-break-match-left': function(v) {
667 terminal.primaryScreen_.wordBreakMatchLeft = v;
668 terminal.alternateScreen_.wordBreakMatchLeft = v;
669 },
670
671 'word-break-match-right': function(v) {
672 terminal.primaryScreen_.wordBreakMatchRight = v;
673 terminal.alternateScreen_.wordBreakMatchRight = v;
674 },
675
676 'word-break-match-middle': function(v) {
677 terminal.primaryScreen_.wordBreakMatchMiddle = v;
678 terminal.alternateScreen_.wordBreakMatchMiddle = v;
679 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400680
681 'allow-images-inline': function(v) {
682 terminal.allowImagesInline = v;
683 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700684 });
rginda30f20f62012-04-05 16:36:19 -0700685
Robert Ginda57f03b42012-09-13 11:02:48 -0700686 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800687 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700688
Mike Frysingerec4225d2020-04-07 05:00:01 -0400689 if (callback) {
Joel Hockeyedac0e72020-05-14 20:16:20 -0700690 this.ready_ = true;
Mike Frysingerec4225d2020-04-07 05:00:01 -0400691 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400692 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700693 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800694};
695
Rob Spies56953412014-04-28 14:09:47 -0700696/**
697 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500698 *
Joel Hockey0f933582019-08-27 18:01:51 -0700699 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700700 */
701hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700702 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700703};
704
Robert Gindaa063b202014-07-21 11:08:25 -0700705/**
706 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500707 *
708 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700709 */
710hterm.Terminal.prototype.setBracketedPaste = function(state) {
711 this.options_.bracketedPaste = state;
712};
Rob Spies56953412014-04-28 14:09:47 -0700713
rginda8e92a692012-05-20 19:37:20 -0700714/**
715 * Set the color for the cursor.
716 *
717 * If you want this setting to persist, set it through prefs_, rather than
718 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500719 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500720 * @param {string=} color The color to set. If not defined, we reset to the
721 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700722 */
723hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400724 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700725 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400726 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500727
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400728 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700729};
730
731/**
732 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400733 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500734 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700735 */
736hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400737 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700738};
739
740/**
rgindad5613292012-06-19 15:40:37 -0700741 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500742 *
743 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700744 */
745hterm.Terminal.prototype.setSelectionEnabled = function(state) {
746 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700747};
748
749/**
rginda8e92a692012-05-20 19:37:20 -0700750 * Set the background color.
751 *
752 * If you want this setting to persist, set it through prefs_, rather than
753 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500754 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500755 * @param {string=} color The color to set. If not defined, we reset to the
756 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700757 */
758hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400759 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700760 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400761 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500762
Joel Hockey42dba8f2020-03-26 16:21:11 -0700763 this.backgroundColor_ = lib.colors.normalizeCSS(color);
764 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700765};
766
rginda9f5222b2012-03-05 11:53:28 -0800767/**
768 * Return the current terminal background color.
769 *
770 * Intended for use by other classes, so we don't have to expose the entire
771 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500772 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700773 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800774 */
775hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700776 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700777};
778
779/**
780 * Set the foreground color.
781 *
782 * If you want this setting to persist, set it through prefs_, rather than
783 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500784 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500785 * @param {string=} color The color to set. If not defined, we reset to the
786 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700787 */
788hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400789 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700790 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400791 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500792
Joel Hockey42dba8f2020-03-26 16:21:11 -0700793 this.foregroundColor_ = lib.colors.normalizeCSS(color);
794 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800795};
796
797/**
798 * Return the current terminal foreground color.
799 *
800 * Intended for use by other classes, so we don't have to expose the entire
801 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500802 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700803 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800804 */
805hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700806 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800807};
808
809/**
rginda87b86462011-12-14 13:48:03 -0800810 * Create a new instance of a terminal command and run it with a given
811 * argument string.
812 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700813 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700814 * @param {string} commandName The command to run for this terminal.
815 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800816 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700817hterm.Terminal.prototype.runCommandClass = function(
818 commandClass, commandName, args) {
Mike Frysingerdc727792020-04-10 01:41:13 -0400819 let environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400820 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700821 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400822 }
rgindaf522ce02012-04-17 17:49:17 -0700823
rginda87b86462011-12-14 13:48:03 -0800824 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700825 {
826 commandName: commandName,
827 args: args,
rginda87b86462011-12-14 13:48:03 -0800828 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700829 environment: environment,
Mike Frysinger2acd3a52020-04-10 02:20:57 -0400830 onExit: (code) => {
831 this.io.pop();
832 this.uninstallKeyboard();
833 this.div_.dispatchEvent(new CustomEvent('terminal-closing'));
834 if (this.prefs_.get('close-on-exit')) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400835 window.close();
836 }
Mike Frysinger989f34b2020-04-08 00:53:43 -0400837 },
rginda87b86462011-12-14 13:48:03 -0800838 });
839
rgindafeaf3142012-01-31 15:14:20 -0800840 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800841 this.command.run();
842};
843
844/**
rgindafeaf3142012-01-31 15:14:20 -0800845 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500846 *
847 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800848 */
849hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700850 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800851};
852
853/**
854 * Install the keyboard handler for this terminal.
855 *
856 * This will prevent the browser from seeing any keystrokes sent to the
857 * terminal.
858 */
859hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700860 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400861};
rgindafeaf3142012-01-31 15:14:20 -0800862
863/**
864 * Uninstall the keyboard handler for this terminal.
865 */
866hterm.Terminal.prototype.uninstallKeyboard = function() {
867 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400868};
rgindafeaf3142012-01-31 15:14:20 -0800869
870/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400871 * Set a CSS variable.
872 *
873 * Normally this is used to set variables in the hterm namespace.
874 *
875 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700876 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400877 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400878 */
879hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400880 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400881 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400882 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400883};
884
885/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700886 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
887 * input is valid.
888 *
889 * @param {string} name The variable to set.
890 * @param {?string} rgb The rgb value to assign to the variable.
891 */
892hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
893 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
894 if (ary) {
895 this.setCssVar(name, ary.slice(0, 3).join(','));
896 }
897};
898
899/**
900 * Sets the specified color for the active screen.
901 *
902 * @param {number} i The index into the 256 color palette to set.
903 * @param {?string} rgb The rgb value to assign to the variable.
904 */
905hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
906 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
907 this.setRgbColorCssVar(`color-${i}`, rgb);
908 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
909 }
910};
911
912/**
913 * Returns the current value in the active screen of the specified color.
914 *
915 * @param {number} i Color palette index.
916 * @return {string} rgb color.
917 */
918hterm.Terminal.prototype.getColorPalette = function(i) {
919 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
920 lib.colors.colorPalette[i];
921};
922
923/**
924 * Reset the specified color in the active screen to its default value.
925 *
926 * @param {number} i Color to reset
927 */
928hterm.Terminal.prototype.resetColor = function(i) {
929 this.setColorPalette(i, lib.colors.colorPalette[i]);
930 delete this.screen_.textAttributes.colorPaletteOverrides[i];
931};
932
933/**
934 * Reset the current screen color palette to the default state.
935 */
936hterm.Terminal.prototype.resetColorPalette = function() {
937 this.screen_.textAttributes.colorPaletteOverrides.forEach(
938 (c, i) => this.resetColor(i));
939};
940
941/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500942 * Get a CSS variable.
943 *
944 * Normally this is used to get variables in the hterm namespace.
945 *
946 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400947 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500948 * @return {string} The current setting for this variable.
949 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400950hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500951 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400952 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500953};
954
955/**
shivanggargf3b362a2020-07-10 11:06:35 +0530956 * @return {!hterm.ScrollPort}
957 */
958hterm.Terminal.prototype.getScrollPort = function() {
959 return this.scrollPort_;
960};
961
962/**
Jason Linbbbdb752020-03-06 16:26:59 +1100963 * Update CSS character size variables to match the scrollport.
964 */
965hterm.Terminal.prototype.updateCssCharsize_ = function() {
966 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
967 this.setCssVar('charsize-height',
968 this.scrollPort_.characterSize.height + 'px');
969};
970
971/**
rginda35c456b2012-02-09 17:29:05 -0800972 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800973 *
974 * Call setFontSize(0) to reset to the default font size.
975 *
976 * This function does not modify the font-size preference.
977 *
978 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800979 */
980hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400981 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700982 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400983 }
rginda9f5222b2012-03-05 11:53:28 -0800984
rginda35c456b2012-02-09 17:29:05 -0800985 this.scrollPort_.setFontSize(px);
Joel Hockeyedac0e72020-05-14 20:16:20 -0700986 this.setCssVar('font-size', `${px}px`);
Jason Linbbbdb752020-03-06 16:26:59 +1100987 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800988};
989
990/**
991 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500992 *
993 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800994 */
995hterm.Terminal.prototype.getFontSize = function() {
996 return this.scrollPort_.getFontSize();
997};
998
999/**
rginda8e92a692012-05-20 19:37:20 -07001000 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -05001001 *
1002 * @return {string}
rginda8e92a692012-05-20 19:37:20 -07001003 */
1004hterm.Terminal.prototype.getFontFamily = function() {
1005 return this.scrollPort_.getFontFamily();
1006};
1007
1008/**
rginda35c456b2012-02-09 17:29:05 -08001009 * Set the CSS "font-family" for this terminal.
1010 */
rginda9f5222b2012-03-05 11:53:28 -08001011hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001012 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
1013 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +11001014 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -08001015 this.syncBoldSafeState();
1016};
1017
rginda4bba5e12012-06-20 16:15:30 -07001018/**
1019 * Set this.mousePasteButton based on the mouse-paste-button pref,
1020 * autodetecting if necessary.
1021 */
1022hterm.Terminal.prototype.syncMousePasteButton = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001023 const button = this.prefs_.get('mouse-paste-button');
rginda4bba5e12012-06-20 16:15:30 -07001024 if (typeof button == 'number') {
1025 this.mousePasteButton = button;
1026 return;
1027 }
1028
Mike Frysingeree81a002017-12-12 16:14:53 -05001029 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001030 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001031 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -04001032 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -07001033 }
1034};
1035
1036/**
1037 * Enable or disable bold based on the enable-bold pref, autodetecting if
1038 * necessary.
1039 */
rginda9f5222b2012-03-05 11:53:28 -08001040hterm.Terminal.prototype.syncBoldSafeState = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001041 const enableBold = this.prefs_.get('enable-bold');
rginda9f5222b2012-03-05 11:53:28 -08001042 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -07001043 this.primaryScreen_.textAttributes.enableBold = enableBold;
1044 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -08001045 return;
1046 }
1047
Mike Frysingerdc727792020-04-10 01:41:13 -04001048 const normalSize = this.scrollPort_.measureCharacterSize();
1049 const boldSize = this.scrollPort_.measureCharacterSize('bold');
rgindaf7521392012-02-28 17:20:34 -08001050
Mike Frysingerdc727792020-04-10 01:41:13 -04001051 const isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001052 if (!isBoldSafe) {
1053 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001054 'from normal. Font family is: ' +
1055 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001056 }
rginda9f5222b2012-03-05 11:53:28 -08001057
Robert Gindaed016262012-10-26 16:27:09 -07001058 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1059 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001060};
1061
1062/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001063 * Control text blinking behavior.
1064 *
1065 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001066 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001067hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001068 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001069 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001070 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001071 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001072};
1073
1074/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001075 * Set the mouse cursor style based on the current terminal mode.
1076 */
1077hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001078 this.setCssVar('mouse-cursor-style',
1079 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1080 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001081 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001082};
1083
1084/**
rginda87b86462011-12-14 13:48:03 -08001085 * Return a copy of the current cursor position.
1086 *
Joel Hockey0f933582019-08-27 18:01:51 -07001087 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001088 */
1089hterm.Terminal.prototype.saveCursor = function() {
1090 return this.screen_.cursorPosition.clone();
1091};
1092
Evan Jones2600d4f2016-12-06 09:29:36 -05001093/**
1094 * Return the current text attributes.
1095 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001096 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001097 */
rgindaa19afe22012-01-25 15:40:22 -08001098hterm.Terminal.prototype.getTextAttributes = function() {
1099 return this.screen_.textAttributes;
1100};
1101
Evan Jones2600d4f2016-12-06 09:29:36 -05001102/**
1103 * Set the text attributes.
1104 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001105 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001106 */
rginda1a09aa02012-06-18 21:11:25 -07001107hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1108 this.screen_.textAttributes = textAttributes;
1109};
1110
rginda87b86462011-12-14 13:48:03 -08001111/**
rgindaf522ce02012-04-17 17:49:17 -07001112 * Return the current browser zoom factor applied to the terminal.
1113 *
1114 * @return {number} The current browser zoom factor.
1115 */
1116hterm.Terminal.prototype.getZoomFactor = function() {
1117 return this.scrollPort_.characterSize.zoomFactor;
1118};
1119
1120/**
rginda9846e2f2012-01-27 13:53:33 -08001121 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001122 *
1123 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001124 */
1125hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001126 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001127};
1128
1129/**
rginda87b86462011-12-14 13:48:03 -08001130 * Restore a previously saved cursor position.
1131 *
Joel Hockey0f933582019-08-27 18:01:51 -07001132 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001133 */
1134hterm.Terminal.prototype.restoreCursor = function(cursor) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001135 const row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1136 const column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001137 this.screen_.setCursorPosition(row, column);
1138 if (cursor.column > column ||
1139 cursor.column == column && cursor.overflow) {
1140 this.screen_.cursorPosition.overflow = true;
1141 }
rginda87b86462011-12-14 13:48:03 -08001142};
1143
1144/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001145 * Clear the cursor's overflow flag.
1146 */
1147hterm.Terminal.prototype.clearCursorOverflow = function() {
1148 this.screen_.cursorPosition.overflow = false;
1149};
1150
1151/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001152 * Save the current cursor state to the corresponding screens.
1153 *
1154 * See the hterm.Screen.CursorState class for more details.
1155 *
1156 * @param {boolean=} both If true, update both screens, else only update the
1157 * current screen.
1158 */
1159hterm.Terminal.prototype.saveCursorAndState = function(both) {
1160 if (both) {
1161 this.primaryScreen_.saveCursorAndState(this.vt);
1162 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001163 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001164 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001165 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001166};
1167
1168/**
1169 * Restore the saved cursor state in the corresponding screens.
1170 *
1171 * See the hterm.Screen.CursorState class for more details.
1172 *
1173 * @param {boolean=} both If true, update both screens, else only update the
1174 * current screen.
1175 */
1176hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1177 if (both) {
1178 this.primaryScreen_.restoreCursorAndState(this.vt);
1179 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001180 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001181 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001182 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001183};
1184
1185/**
Robert Ginda830583c2013-08-07 13:20:46 -07001186 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001187 *
1188 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001189 */
1190hterm.Terminal.prototype.setCursorShape = function(shape) {
1191 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001192 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001193};
Robert Ginda830583c2013-08-07 13:20:46 -07001194
1195/**
1196 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001197 *
1198 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001199 */
1200hterm.Terminal.prototype.getCursorShape = function() {
1201 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001202};
Robert Ginda830583c2013-08-07 13:20:46 -07001203
1204/**
Joel Hockey139d82d2020-04-07 23:04:29 -07001205 * Set the screen padding size in pixels.
1206 *
1207 * @param {number} size
1208 */
1209hterm.Terminal.prototype.setScreenPaddingSize = function(size) {
Joel Hockeyaaabfba2020-05-01 16:10:28 -07001210 this.setCssVar('screen-padding-size', `${size}px`);
Joel Hockey139d82d2020-04-07 23:04:29 -07001211 this.scrollPort_.setScreenPaddingSize(size);
1212};
1213
1214/**
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001215 * Set the screen border size in pixels.
1216 *
1217 * @param {number} size
1218 */
1219hterm.Terminal.prototype.setScreenBorderSize = function(size) {
1220 this.div_.style.borderWidth = `${size}px`;
1221 this.screenBorderSize_ = size;
1222 this.scrollPort_.resize();
1223};
1224
1225/**
rginda87b86462011-12-14 13:48:03 -08001226 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001227 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001228 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001229 */
1230hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001231 if (columnCount == null) {
1232 this.div_.style.width = '100%';
1233 return;
1234 }
1235
Joel Hockey139d82d2020-04-07 23:04:29 -07001236 const rightPadding = Math.max(
1237 this.scrollPort_.screenPaddingSize,
1238 this.scrollPort_.currentScrollbarWidthPx);
Robert Ginda26806d12014-07-24 13:44:07 -07001239 this.div_.style.width = Math.ceil(
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001240 (this.scrollPort_.characterSize.width * columnCount) +
1241 this.scrollPort_.screenPaddingSize + rightPadding +
1242 (2 * this.screenBorderSize_)) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001243 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001244 this.scheduleSyncCursorPosition_();
1245};
rginda87b86462011-12-14 13:48:03 -08001246
rgindac9bc5502012-01-18 11:48:44 -08001247/**
rginda35c456b2012-02-09 17:29:05 -08001248 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001249 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001250 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001251 */
1252hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001253 if (rowCount == null) {
1254 this.div_.style.height = '100%';
1255 return;
1256 }
1257
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001258 this.div_.style.height = (this.scrollPort_.characterSize.height * rowCount) +
1259 (2 * this.scrollPort_.screenPaddingSize) +
1260 (2 * this.screenBorderSize_) + 'px';
rginda35c456b2012-02-09 17:29:05 -08001261 this.realizeSize_(this.screenSize.width, rowCount);
1262 this.scheduleSyncCursorPosition_();
1263};
1264
1265/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001266 * Deal with terminal size changes.
1267 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001268 * @param {number} columnCount The number of columns.
1269 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001270 */
1271hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001272 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001273
Mike Frysinger0206e262019-06-13 10:18:19 -04001274 if (columnCount != this.screenSize.width) {
1275 notify = true;
1276 this.realizeWidth_(columnCount);
1277 }
1278
1279 if (rowCount != this.screenSize.height) {
1280 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001281 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001282 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001283
1284 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001285 if (notify) {
1286 this.io.onTerminalResize_(columnCount, rowCount);
1287 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001288};
1289
1290/**
rgindac9bc5502012-01-18 11:48:44 -08001291 * Deal with terminal width changes.
1292 *
1293 * This function does what needs to be done when the terminal width changes
1294 * out from under us. It happens here rather than in onResize_() because this
1295 * code may need to run synchronously to handle programmatic changes of
1296 * terminal width.
1297 *
1298 * Relying on the browser to send us an async resize event means we may not be
1299 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001300 *
1301 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001302 */
1303hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001304 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001305 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001306 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001307
Mike Frysingerdc727792020-04-10 01:41:13 -04001308 const deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001309 if (deltaColumns == 0) {
1310 // No change, so don't bother recalculating things.
1311 return;
1312 }
rgindac9bc5502012-01-18 11:48:44 -08001313
rginda87b86462011-12-14 13:48:03 -08001314 this.screenSize.width = columnCount;
1315 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001316
1317 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001318 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001319 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001320 }
rgindac9bc5502012-01-18 11:48:44 -08001321 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04001322 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001323 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001324 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001325 }
rgindac9bc5502012-01-18 11:48:44 -08001326
1327 this.tabStops_.pop();
1328 }
1329 }
1330
1331 this.screen_.setColumnCount(this.screenSize.width);
1332};
1333
1334/**
1335 * Deal with terminal height changes.
1336 *
1337 * This function does what needs to be done when the terminal height changes
1338 * out from under us. It happens here rather than in onResize_() because this
1339 * code may need to run synchronously to handle programmatic changes of
1340 * terminal height.
1341 *
1342 * Relying on the browser to send us an async resize event means we may not be
1343 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001344 *
1345 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001346 */
1347hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001348 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001349 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001350 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001351
Mike Frysingerdc727792020-04-10 01:41:13 -04001352 let deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001353 if (deltaRows == 0) {
1354 // No change, so don't bother recalculating things.
1355 return;
1356 }
rgindac9bc5502012-01-18 11:48:44 -08001357
1358 this.screenSize.height = rowCount;
1359
Mike Frysingerdc727792020-04-10 01:41:13 -04001360 const cursor = this.saveCursor();
rgindac9bc5502012-01-18 11:48:44 -08001361
1362 if (deltaRows < 0) {
1363 // Screen got smaller.
1364 deltaRows *= -1;
1365 while (deltaRows) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001366 const lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001367 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001368 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001369 }
rgindac9bc5502012-01-18 11:48:44 -08001370
Mike Frysingerbdb34802020-04-07 03:47:32 -04001371 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001372 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001373 }
rgindac9bc5502012-01-18 11:48:44 -08001374
1375 this.screen_.popRow();
1376 deltaRows--;
1377 }
1378
Mike Frysingerdc727792020-04-10 01:41:13 -04001379 const ary = this.screen_.shiftRows(deltaRows);
rgindac9bc5502012-01-18 11:48:44 -08001380 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1381
1382 // We just removed rows from the top of the screen, we need to update
1383 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001384 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001385 } else if (deltaRows > 0) {
1386 // Screen got larger.
1387
1388 if (deltaRows <= this.scrollbackRows_.length) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001389 const scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1390 const rows = this.scrollbackRows_.splice(
rgindac9bc5502012-01-18 11:48:44 -08001391 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1392 this.screen_.unshiftRows(rows);
1393 deltaRows -= scrollbackCount;
1394 cursor.row += scrollbackCount;
1395 }
1396
Mike Frysingerbdb34802020-04-07 03:47:32 -04001397 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001398 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001399 }
rgindac9bc5502012-01-18 11:48:44 -08001400 }
1401
rginda35c456b2012-02-09 17:29:05 -08001402 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001403 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001404};
1405
1406/**
1407 * Scroll the terminal to the top of the scrollback buffer.
1408 */
1409hterm.Terminal.prototype.scrollHome = function() {
1410 this.scrollPort_.scrollRowToTop(0);
1411};
1412
1413/**
1414 * Scroll the terminal to the end.
1415 */
1416hterm.Terminal.prototype.scrollEnd = function() {
1417 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1418};
1419
1420/**
1421 * Scroll the terminal one page up (minus one line) relative to the current
1422 * position.
1423 */
1424hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001425 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001426};
1427
1428/**
1429 * Scroll the terminal one page down (minus one line) relative to the current
1430 * position.
1431 */
1432hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001433 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001434};
1435
rgindac9bc5502012-01-18 11:48:44 -08001436/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001437 * Scroll the terminal one line up relative to the current position.
1438 */
1439hterm.Terminal.prototype.scrollLineUp = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001440 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001441 this.scrollPort_.scrollRowToTop(i - 1);
1442};
1443
1444/**
1445 * Scroll the terminal one line down relative to the current position.
1446 */
1447hterm.Terminal.prototype.scrollLineDown = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001448 const i = this.scrollPort_.getTopRowIndex();
Mike Frysingercd56a632017-05-10 14:45:28 -04001449 this.scrollPort_.scrollRowToTop(i + 1);
1450};
1451
1452/**
Robert Ginda40932892012-12-10 17:26:40 -08001453 * Clear primary screen, secondary screen, and the scrollback buffer.
1454 */
1455hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001456 this.clearHome(this.primaryScreen_);
1457 this.clearHome(this.alternateScreen_);
1458
1459 this.clearScrollback();
1460};
1461
1462/**
1463 * Clear scrollback buffer.
1464 */
1465hterm.Terminal.prototype.clearScrollback = function() {
1466 // Move to the end of the buffer in case the screen was scrolled back.
1467 // We're going to throw it away which would leave the display invalid.
1468 this.scrollEnd();
1469
Robert Ginda40932892012-12-10 17:26:40 -08001470 this.scrollbackRows_.length = 0;
1471 this.scrollPort_.resetCache();
1472
Mike Frysinger9c482b82018-09-07 02:49:36 -04001473 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1474 const bottom = screen.getHeight();
1475 this.renumberRows_(0, bottom, screen);
1476 });
Robert Ginda40932892012-12-10 17:26:40 -08001477
1478 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001479 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001480};
1481
1482/**
rgindac9bc5502012-01-18 11:48:44 -08001483 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001484 *
1485 * Perform a full reset to the default values listed in
1486 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001487 */
rginda87b86462011-12-14 13:48:03 -08001488hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001489 this.vt.reset();
1490
rgindac9bc5502012-01-18 11:48:44 -08001491 this.clearAllTabStops();
1492 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001493
Joel Hockey42dba8f2020-03-26 16:21:11 -07001494 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001495 const resetScreen = (screen) => {
1496 // We want to make sure to reset the attributes before we clear the screen.
1497 // The attributes might be used to initialize default/empty rows.
1498 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001499 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001500 this.clearHome(screen);
1501 screen.saveCursorAndState(this.vt);
1502 };
1503 resetScreen(this.primaryScreen_);
1504 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001505
Mike Frysinger84301d02017-11-29 13:28:46 -08001506 // Reset terminal options to their default values.
1507 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001508 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1509
Mike Frysinger84301d02017-11-29 13:28:46 -08001510 this.setVTScrollRegion(null, null);
1511
1512 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001513};
1514
rgindac9bc5502012-01-18 11:48:44 -08001515/**
1516 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001517 *
1518 * Perform a soft reset to the default values listed in
1519 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001520 */
rginda0f5c0292012-01-13 11:00:13 -08001521hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001522 this.vt.reset();
1523
rgindab8bc8932012-04-27 12:45:03 -07001524 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001525 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001526
Brad Townb62dfdc2015-03-16 19:07:15 -07001527 // We show the cursor on soft reset but do not alter the blink state.
1528 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1529
Joel Hockey42dba8f2020-03-26 16:21:11 -07001530 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001531 const resetScreen = (screen) => {
1532 // Xterm also resets the color palette on soft reset, even though it doesn't
1533 // seem to be documented anywhere.
1534 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001535 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001536 screen.saveCursorAndState(this.vt);
1537 };
1538 resetScreen(this.primaryScreen_);
1539 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001540
rgindab8bc8932012-04-27 12:45:03 -07001541 // The xterm man page explicitly says this will happen on soft reset.
1542 this.setVTScrollRegion(null, null);
1543
1544 // Xterm also shows the cursor on soft reset, but does not alter the blink
1545 // state.
rgindaa19afe22012-01-25 15:40:22 -08001546 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001547};
1548
rgindac9bc5502012-01-18 11:48:44 -08001549/**
1550 * Move the cursor forward to the next tab stop, or to the last column
1551 * if no more tab stops are set.
1552 */
1553hterm.Terminal.prototype.forwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001554 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001555
Mike Frysingerdc727792020-04-10 01:41:13 -04001556 for (let i = 0; i < this.tabStops_.length; i++) {
rgindac9bc5502012-01-18 11:48:44 -08001557 if (this.tabStops_[i] > column) {
1558 this.setCursorColumn(this.tabStops_[i]);
1559 return;
1560 }
1561 }
1562
David Benjamin66e954d2012-05-05 21:08:12 -04001563 // xterm does not clear the overflow flag on HT or CHT.
Mike Frysingerdc727792020-04-10 01:41:13 -04001564 const overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001565 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001566 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001567};
1568
rgindac9bc5502012-01-18 11:48:44 -08001569/**
1570 * Move the cursor backward to the previous tab stop, or to the first column
1571 * if no previous tab stops are set.
1572 */
1573hterm.Terminal.prototype.backwardTabStop = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001574 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001575
Mike Frysingerdc727792020-04-10 01:41:13 -04001576 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
rgindac9bc5502012-01-18 11:48:44 -08001577 if (this.tabStops_[i] < column) {
1578 this.setCursorColumn(this.tabStops_[i]);
1579 return;
1580 }
1581 }
1582
1583 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001584};
1585
rgindac9bc5502012-01-18 11:48:44 -08001586/**
1587 * Set a tab stop at the given column.
1588 *
Joel Hockey0f933582019-08-27 18:01:51 -07001589 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001590 */
1591hterm.Terminal.prototype.setTabStop = function(column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001592 for (let i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001593 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001594 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001595 }
rgindac9bc5502012-01-18 11:48:44 -08001596
1597 if (this.tabStops_[i] < column) {
1598 this.tabStops_.splice(i + 1, 0, column);
1599 return;
1600 }
1601 }
1602
1603 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001604};
1605
rgindac9bc5502012-01-18 11:48:44 -08001606/**
1607 * Clear the tab stop at the current cursor position.
1608 *
1609 * No effect if there is no tab stop at the current cursor position.
1610 */
1611hterm.Terminal.prototype.clearTabStopAtCursor = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04001612 const column = this.screen_.cursorPosition.column;
rgindac9bc5502012-01-18 11:48:44 -08001613
Mike Frysingerdc727792020-04-10 01:41:13 -04001614 const i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001615 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001616 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001617 }
rgindac9bc5502012-01-18 11:48:44 -08001618
1619 this.tabStops_.splice(i, 1);
1620};
1621
1622/**
1623 * Clear all tab stops.
1624 */
1625hterm.Terminal.prototype.clearAllTabStops = function() {
1626 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001627 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001628};
1629
1630/**
1631 * Set up the default tab stops, starting from a given column.
1632 *
1633 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001634 * from the specified column, or 0 if no column is provided. It also flags
1635 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001636 *
1637 * This does not clear the existing tab stops first, use clearAllTabStops
1638 * for that.
1639 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001640 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001641 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001642 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001643hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001644 const w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001645 // Round start up to a default tab stop.
1646 start = start - 1 - ((start - 1) % w) + w;
Mike Frysingerdc727792020-04-10 01:41:13 -04001647 for (let i = start; i < this.screenSize.width; i += w) {
David Benjamin66e954d2012-05-05 21:08:12 -04001648 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001649 }
David Benjamin66e954d2012-05-05 21:08:12 -04001650
1651 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001652};
1653
rginda6d397402012-01-17 10:58:29 -08001654/**
rginda8ba33642011-12-14 12:31:31 -08001655 * Interpret a sequence of characters.
1656 *
1657 * Incomplete escape sequences are buffered until the next call.
1658 *
1659 * @param {string} str Sequence of characters to interpret or pass through.
1660 */
1661hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001662 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001663 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001664};
1665
1666/**
1667 * Take over the given DIV for use as the terminal display.
1668 *
Joel Hockey0f933582019-08-27 18:01:51 -07001669 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001670 */
1671hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001672 const charset = div.ownerDocument.characterSet.toLowerCase();
1673 if (charset != 'utf-8') {
1674 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1675 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1676 }
1677
rginda87b86462011-12-14 13:48:03 -08001678 this.div_ = div;
Jean-Marc Eurinad1731f2020-05-12 10:09:05 -07001679 this.div_.style.borderStyle = 'solid';
1680 this.div_.style.borderWidth = 0;
1681 this.div_.style.boxSizing = 'border-box';
rginda87b86462011-12-14 13:48:03 -08001682
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001683 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1684
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001685 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1686};
1687
1688/**
1689 * Initialisation of ScrollPort properties which need to be set after its DOM
1690 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001691 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001692 * @private
1693 */
1694hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001695 this.scrollPort_.setBackgroundImage(
1696 this.prefs_.getString('background-image'));
1697 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001698 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001699 this.prefs_.getString('background-position'));
1700 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1701 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1702 this.scrollPort_.setAccessibilityReader(
1703 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001704
rginda0918b652012-04-04 11:26:24 -07001705 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001706
Joel Hockeyd4fca732019-09-20 16:57:03 -07001707 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001708 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001709
Joel Hockeyd4fca732019-09-20 16:57:03 -07001710 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001711 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001712 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001713
rginda8ba33642011-12-14 12:31:31 -08001714 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001715 this.accessibilityReader_.decorate(this.document_);
shivanggargde5387e2020-06-10 01:31:16 +05301716 this.findBar.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001717
Evan Jones5f9df812016-12-06 09:38:58 -05001718 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001719 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001720
Mike Frysingerdc727792020-04-10 01:41:13 -04001721 const onMouse = this.onMouse_.bind(this);
1722 const screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001723 screenNode.addEventListener(
1724 'mousedown', /** @type {!EventListener} */ (onMouse));
1725 screenNode.addEventListener(
1726 'mouseup', /** @type {!EventListener} */ (onMouse));
1727 screenNode.addEventListener(
1728 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001729 this.scrollPort_.onScrollWheel = onMouse;
1730
Joel Hockeyd4fca732019-09-20 16:57:03 -07001731 screenNode.addEventListener(
1732 'keydown',
1733 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001734
Toni Barzic0bfa8922013-11-22 11:18:35 -08001735 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001736 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001737 // Listen for mousedown events on the screenNode as in FF the focus
1738 // events don't bubble.
1739 screenNode.addEventListener('mousedown', function() {
1740 setTimeout(this.onFocusChange_.bind(this, true));
1741 }.bind(this));
1742
Toni Barzic0bfa8922013-11-22 11:18:35 -08001743 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001744 'blur', this.onFocusChange_.bind(this, false));
1745
Mike Frysingerdc727792020-04-10 01:41:13 -04001746 const style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001747 style.textContent = `
1748.cursor-node[focus="false"] {
1749 box-sizing: border-box;
1750 background-color: transparent !important;
1751 border-width: 2px;
1752 border-style: solid;
1753}
1754menu {
Joel Hockey500c6102020-05-14 19:24:02 -07001755 background: #fff;
1756 border-radius: 4px;
1757 color: #202124;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001758 cursor: var(--hterm-mouse-cursor-pointer);
Joel Hockey500c6102020-05-14 19:24:02 -07001759 display: none;
1760 filter: drop-shadow(0 1px 3px #3C40434D) drop-shadow(0 4px 8px #3C404326);
1761 margin: 0;
1762 padding: 8px 0;
1763 position: absolute;
1764 transition-duration: 200ms;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001765}
1766menuitem {
Joel Hockeyd36efd62019-09-30 14:16:20 -07001767 display: block;
Joel Hockey500c6102020-05-14 19:24:02 -07001768 font: var(--hterm-font-size) 'Roboto', 'Noto Sans', sans-serif;
1769 padding: 0.5em 1em;
1770 white-space: nowrap;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001771}
1772menuitem.separator {
1773 border-bottom: none;
1774 height: 0.5em;
1775 padding: 0;
1776}
1777menuitem:hover {
Joel Hockey500c6102020-05-14 19:24:02 -07001778 background-color: #e2e4e6;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001779}
1780.wc-node {
1781 display: inline-block;
1782 text-align: center;
1783 width: calc(var(--hterm-charsize-width) * 2);
1784 line-height: var(--hterm-charsize-height);
1785}
1786:root {
1787 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1788 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
Joel Hockeyd36efd62019-09-30 14:16:20 -07001789 --hterm-blink-node-duration: 0.7s;
1790 --hterm-mouse-cursor-default: default;
1791 --hterm-mouse-cursor-text: text;
1792 --hterm-mouse-cursor-pointer: pointer;
1793 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey139d82d2020-04-07 23:04:29 -07001794 --hterm-screen-padding-size: 0;
Joel Hockey42dba8f2020-03-26 16:21:11 -07001795
Joel Hockey42dba8f2020-03-26 16:21:11 -07001796${lib.colors.stockColorPalette.map((c, i) => `
1797 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1798`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001799}
1800.uri-node:hover {
1801 text-decoration: underline;
1802 cursor: var(--hterm-mouse-cursor-pointer);
1803}
1804@keyframes blink {
1805 from { opacity: 1.0; }
1806 to { opacity: 0.0; }
1807}
1808.blink-node {
1809 animation-name: blink;
1810 animation-duration: var(--hterm-blink-node-duration);
1811 animation-iteration-count: infinite;
1812 animation-timing-function: ease-in-out;
1813 animation-direction: alternate;
1814}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001815 // Insert this stock style as the first node so that any user styles will
1816 // override w/out having to use !important everywhere. The rules above mix
1817 // runtime variables with default ones designed to be overridden by the user,
1818 // but we can wait for a concrete case from the users to determine the best
1819 // way to split the sheet up to before & after the user-css settings.
1820 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001821
rginda8ba33642011-12-14 12:31:31 -08001822 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001823 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001824 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001825 this.cursorNode_.style.cssText = `
1826position: absolute;
Joel Hockey139d82d2020-04-07 23:04:29 -07001827left: calc(var(--hterm-screen-padding-size) +
1828 var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1829top: calc(var(--hterm-screen-padding-size) +
1830 var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
Joel Hockeyd36efd62019-09-30 14:16:20 -07001831display: ${this.options_.cursorVisible ? '' : 'none'};
1832width: var(--hterm-charsize-width);
1833height: var(--hterm-charsize-height);
1834background-color: var(--hterm-cursor-color);
1835border-color: var(--hterm-cursor-color);
1836-webkit-transition: opacity, background-color 100ms linear;
1837-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001838
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001839 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001840 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1841 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001842
rginda8ba33642011-12-14 12:31:31 -08001843 this.document_.body.appendChild(this.cursorNode_);
1844
rgindad5613292012-06-19 15:40:37 -07001845 // When 'enableMouseDragScroll' is off we reposition this element directly
1846 // under the mouse cursor after a click. This makes Chrome associate
1847 // subsequent mousemove events with the scroll-blocker. Since the
1848 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1849 // events do not cause the scrollport to scroll.
1850 //
1851 // It's a hack, but it's the cleanest way I could find.
1852 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001853 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001854 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001855 this.scrollBlockerNode_.style.cssText =
1856 ('position: absolute;' +
1857 'top: -99px;' +
1858 'display: block;' +
1859 'width: 10px;' +
1860 'height: 10px;');
1861 this.document_.body.appendChild(this.scrollBlockerNode_);
1862
rgindad5613292012-06-19 15:40:37 -07001863 this.scrollPort_.onScrollWheel = onMouse;
1864 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1865 ].forEach(function(event) {
1866 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001867 this.cursorNode_.addEventListener(
1868 event, /** @type {!EventListener} */ (onMouse));
1869 this.document_.addEventListener(
1870 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001871 }.bind(this));
1872
1873 this.cursorNode_.addEventListener('mousedown', function() {
1874 setTimeout(this.focus.bind(this));
1875 }.bind(this));
1876
rginda8ba33642011-12-14 12:31:31 -08001877 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001878
Joel Hockeyd8bfa3e2020-06-12 14:41:11 -07001879 // Re-sync fonts whenever a web font loads.
1880 this.document_.fonts.addEventListener(
1881 'loadingdone', () => this.syncFontFamily());
1882
rginda87b86462011-12-14 13:48:03 -08001883 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001884 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001885};
1886
rginda0918b652012-04-04 11:26:24 -07001887/**
1888 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001889 *
Joel Hockey0f933582019-08-27 18:01:51 -07001890 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001891 */
rginda87b86462011-12-14 13:48:03 -08001892hterm.Terminal.prototype.getDocument = function() {
1893 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001894};
1895
1896/**
rginda0918b652012-04-04 11:26:24 -07001897 * Focus the terminal.
1898 */
1899hterm.Terminal.prototype.focus = function() {
1900 this.scrollPort_.focus();
1901};
1902
1903/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001904 * Unfocus the terminal.
1905 */
1906hterm.Terminal.prototype.blur = function() {
1907 this.scrollPort_.blur();
1908};
1909
1910/**
rginda8ba33642011-12-14 12:31:31 -08001911 * Return the HTML Element for a given row index.
1912 *
1913 * This is a method from the RowProvider interface. The ScrollPort uses
1914 * it to fetch rows on demand as they are scrolled into view.
1915 *
1916 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1917 * pairs to conserve memory.
1918 *
Joel Hockey0f933582019-08-27 18:01:51 -07001919 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001920 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001921 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001922 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001923 * @override
rginda8ba33642011-12-14 12:31:31 -08001924 */
1925hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001926 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001927 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001928 }
rginda8ba33642011-12-14 12:31:31 -08001929
Mike Frysingerdc727792020-04-10 01:41:13 -04001930 const screenIndex = index - this.scrollbackRows_.length;
rginda8ba33642011-12-14 12:31:31 -08001931 return this.screen_.rowsArray[screenIndex];
1932};
1933
1934/**
1935 * Return the text content for a given range of rows.
1936 *
1937 * This is a method from the RowProvider interface. The ScrollPort uses
1938 * it to fetch text content on demand when the user attempts to copy their
1939 * selection to the clipboard.
1940 *
Joel Hockey0f933582019-08-27 18:01:51 -07001941 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001942 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001943 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001944 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001945 * relative to the start of the scrollback buffer.
1946 * @return {string} A single string containing the text value of the range of
1947 * rows. Lines will be newline delimited, with no trailing newline.
1948 */
1949hterm.Terminal.prototype.getRowsText = function(start, end) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001950 const ary = [];
1951 for (let i = start; i < end; i++) {
1952 const node = this.getRowNode(i);
rginda8ba33642011-12-14 12:31:31 -08001953 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001954 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001955 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001956 }
rginda8ba33642011-12-14 12:31:31 -08001957 }
1958
rgindaa09e7332012-08-17 12:49:51 -07001959 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001960};
1961
1962/**
1963 * Return the text content for a given row.
1964 *
1965 * This is a method from the RowProvider interface. The ScrollPort uses
1966 * it to fetch text content on demand when the user attempts to copy their
1967 * selection to the clipboard.
1968 *
Joel Hockey0f933582019-08-27 18:01:51 -07001969 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001970 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001971 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001972 * @return {string} A string containing the text value of the selected row.
1973 */
1974hterm.Terminal.prototype.getRowText = function(index) {
Mike Frysingerdc727792020-04-10 01:41:13 -04001975 const node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001976 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001977};
1978
1979/**
1980 * Return the total number of rows in the addressable screen and in the
1981 * scrollback buffer of this terminal.
1982 *
1983 * This is a method from the RowProvider interface. The ScrollPort uses
1984 * it to compute the size of the scrollbar.
1985 *
Joel Hockey0f933582019-08-27 18:01:51 -07001986 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001987 * @override
rginda8ba33642011-12-14 12:31:31 -08001988 */
1989hterm.Terminal.prototype.getRowCount = function() {
1990 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1991};
1992
1993/**
1994 * Create DOM nodes for new rows and append them to the end of the terminal.
1995 *
1996 * This is the only correct way to add a new DOM node for a row. Notice that
1997 * the new row is appended to the bottom of the list of rows, and does not
1998 * require renumbering (of the rowIndex property) of previous rows.
1999 *
2000 * If you think you want a new blank row somewhere in the middle of the
2001 * terminal, look into moveRows_().
2002 *
2003 * This method does not pay attention to vtScrollTop/Bottom, since you should
2004 * be using moveRows() in cases where they would matter.
2005 *
2006 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05002007 *
2008 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08002009 */
2010hterm.Terminal.prototype.appendRows_ = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002011 let cursorRow = this.screen_.rowsArray.length;
2012 const offset = this.scrollbackRows_.length + cursorRow;
2013 for (let i = 0; i < count; i++) {
2014 const row = this.document_.createElement('x-row');
rginda8ba33642011-12-14 12:31:31 -08002015 row.appendChild(this.document_.createTextNode(''));
2016 row.rowIndex = offset + i;
2017 this.screen_.pushRow(row);
2018 }
2019
Mike Frysingerdc727792020-04-10 01:41:13 -04002020 const extraRows = this.screen_.rowsArray.length - this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08002021 if (extraRows > 0) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002022 const ary = this.screen_.shiftRows(extraRows);
rginda8ba33642011-12-14 12:31:31 -08002023 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002024 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07002025 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002026 }
rginda8ba33642011-12-14 12:31:31 -08002027 }
2028
Mike Frysingerbdb34802020-04-07 03:47:32 -04002029 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08002030 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002031 }
rginda8ba33642011-12-14 12:31:31 -08002032
rginda87b86462011-12-14 13:48:03 -08002033 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08002034};
2035
2036/**
2037 * Relocate rows from one part of the addressable screen to another.
2038 *
2039 * This is used to recycle rows during VT scrolls (those which are driven
2040 * by VT commands, rather than by the user manipulating the scrollbar.)
2041 *
2042 * In this case, the blank lines scrolled into the scroll region are made of
2043 * the nodes we scrolled off. These have their rowIndex properties carefully
2044 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002045 *
2046 * @param {number} fromIndex The start index.
2047 * @param {number} count The number of rows to move.
2048 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08002049 */
2050hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002051 const ary = this.screen_.removeRows(fromIndex, count);
rginda8ba33642011-12-14 12:31:31 -08002052 this.screen_.insertRows(toIndex, ary);
2053
Mike Frysingerdc727792020-04-10 01:41:13 -04002054 let start, end;
rginda8ba33642011-12-14 12:31:31 -08002055 if (fromIndex < toIndex) {
2056 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08002057 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002058 } else {
2059 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08002060 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08002061 }
2062
2063 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08002064 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08002065};
2066
2067/**
2068 * Renumber the rowIndex property of the given range of rows.
2069 *
Zhu Qunying30d40712017-03-14 16:27:00 -07002070 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08002071 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08002072 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08002073 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05002074 *
2075 * @param {number} start The start index.
2076 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04002077 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08002078 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002079hterm.Terminal.prototype.renumberRows_ = function(
2080 start, end, screen = undefined) {
2081 if (!screen) {
2082 screen = this.screen_;
2083 }
Robert Ginda40932892012-12-10 17:26:40 -08002084
Mike Frysingerdc727792020-04-10 01:41:13 -04002085 const offset = this.scrollbackRows_.length;
2086 for (let i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08002087 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002088 }
2089};
2090
2091/**
2092 * Print a string to the terminal.
2093 *
2094 * This respects the current insert and wraparound modes. It will add new lines
2095 * to the end of the terminal, scrolling off the top into the scrollback buffer
2096 * if necessary.
2097 *
2098 * The string is *not* parsed for escape codes. Use the interpret() method if
2099 * that's what you're after.
2100 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002101 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002102 */
2103hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002104 this.scheduleSyncCursorPosition_();
2105
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002106 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002107 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002108
Mike Frysingerdc727792020-04-10 01:41:13 -04002109 let startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002110
Mike Frysingerdc727792020-04-10 01:41:13 -04002111 let strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002112 // Fun edge case: If the string only contains zero width codepoints (like
2113 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002114 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002115 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002116 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002117
2118 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002119 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2120 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002121 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002122 }
rgindaa19afe22012-01-25 15:40:22 -08002123
Mike Frysingerdc727792020-04-10 01:41:13 -04002124 let count = strWidth - startOffset;
2125 let didOverflow = false;
2126 let substr;
rgindaa19afe22012-01-25 15:40:22 -08002127
rgindaa9abdd82012-08-06 18:05:09 -07002128 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2129 didOverflow = true;
2130 count = this.screenSize.width - this.screen_.cursorPosition.column;
2131 }
rgindaa19afe22012-01-25 15:40:22 -08002132
rgindaa9abdd82012-08-06 18:05:09 -07002133 if (didOverflow && !this.options_.wraparound) {
2134 // If the string overflowed the line but wraparound is off, then the
2135 // last printed character should be the last of the string.
2136 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002137 substr = lib.wc.substr(str, startOffset, count - 1) +
2138 lib.wc.substr(str, strWidth - 1);
2139 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002140 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002141 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002142 }
rgindaa19afe22012-01-25 15:40:22 -08002143
Mike Frysingerdc727792020-04-10 01:41:13 -04002144 const tokens = hterm.TextAttributes.splitWidecharString(substr);
2145 for (let i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002146 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2147 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002148
2149 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002150 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002151 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002152 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002153 }
2154 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002155 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002156 }
2157
2158 this.screen_.maybeClipCurrentRow();
2159 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002160 }
rginda8ba33642011-12-14 12:31:31 -08002161
Mike Frysingerbdb34802020-04-07 03:47:32 -04002162 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002163 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002164 }
rginda8ba33642011-12-14 12:31:31 -08002165};
2166
2167/**
rginda87b86462011-12-14 13:48:03 -08002168 * Set the VT scroll region.
2169 *
rginda87b86462011-12-14 13:48:03 -08002170 * This also resets the cursor position to the absolute (0, 0) position, since
2171 * that's what xterm appears to do.
2172 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002173 * Setting the scroll region to the full height of the terminal will clear
2174 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2175 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2176 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2177 * continue to work as most users would expect.
2178 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002179 * @param {?number} scrollTop The zero-based top of the scroll region.
2180 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002181 * inclusive.
2182 */
2183hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002184 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002185 this.vtScrollTop_ = null;
2186 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002187 } else {
2188 this.vtScrollTop_ = scrollTop;
2189 this.vtScrollBottom_ = scrollBottom;
2190 }
rginda87b86462011-12-14 13:48:03 -08002191};
2192
2193/**
rginda8ba33642011-12-14 12:31:31 -08002194 * Return the top row index according to the VT.
2195 *
2196 * This will return 0 unless the terminal has been told to restrict scrolling
2197 * to some lower row. It is used for some VT cursor positioning and scrolling
2198 * commands.
2199 *
Joel Hockey0f933582019-08-27 18:01:51 -07002200 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002201 */
2202hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002203 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002204 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002205 }
rginda8ba33642011-12-14 12:31:31 -08002206
2207 return 0;
rginda87b86462011-12-14 13:48:03 -08002208};
rginda8ba33642011-12-14 12:31:31 -08002209
2210/**
2211 * Return the bottom row index according to the VT.
2212 *
2213 * This will return the height of the terminal unless the it has been told to
2214 * restrict scrolling to some higher row. It is used for some VT cursor
2215 * positioning and scrolling commands.
2216 *
Joel Hockey0f933582019-08-27 18:01:51 -07002217 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002218 */
2219hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002220 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002221 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002222 }
rginda8ba33642011-12-14 12:31:31 -08002223
rginda87b86462011-12-14 13:48:03 -08002224 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002225};
rginda8ba33642011-12-14 12:31:31 -08002226
2227/**
2228 * Process a '\n' character.
2229 *
2230 * If the cursor is on the final row of the terminal this will append a new
2231 * blank row to the screen and scroll the topmost row into the scrollback
2232 * buffer.
2233 *
2234 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002235 *
2236 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2237 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002238 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002239hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002240 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002241 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002242 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002243
Mike Frysingerdc727792020-04-10 01:41:13 -04002244 const cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2245 this.screen_.rowsArray.length - 1);
Robert Ginda9937abc2013-07-25 16:09:23 -07002246
2247 if (this.vtScrollBottom_ != null) {
2248 // A VT Scroll region is active, we never append new rows.
2249 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2250 // We're at the end of the VT Scroll Region, perform a VT scroll.
2251 this.vtScrollUp(1);
2252 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2253 } else if (cursorAtEndOfScreen) {
2254 // We're at the end of the screen, the only thing to do is put the
2255 // cursor to column 0.
2256 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2257 } else {
2258 // Anywhere else, advance the cursor row, and reset the column.
2259 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2260 }
2261 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002262 // We're at the end of the screen. Append a new row to the terminal,
2263 // shifting the top row into the scrollback.
2264 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002265 } else {
rginda87b86462011-12-14 13:48:03 -08002266 // Anywhere else in the screen just moves the cursor.
2267 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002268 }
2269};
2270
2271/**
2272 * Like newLine(), except maintain the cursor column.
2273 */
2274hterm.Terminal.prototype.lineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002275 const column = this.screen_.cursorPosition.column;
rginda8ba33642011-12-14 12:31:31 -08002276 this.newLine();
2277 this.setCursorColumn(column);
2278};
2279
2280/**
rginda87b86462011-12-14 13:48:03 -08002281 * If autoCarriageReturn is set then newLine(), else lineFeed().
2282 */
2283hterm.Terminal.prototype.formFeed = function() {
2284 if (this.options_.autoCarriageReturn) {
2285 this.newLine();
2286 } else {
2287 this.lineFeed();
2288 }
2289};
2290
2291/**
2292 * Move the cursor up one row, possibly inserting a blank line.
2293 *
2294 * The cursor column is not changed.
2295 */
2296hterm.Terminal.prototype.reverseLineFeed = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002297 const scrollTop = this.getVTScrollTop();
2298 const currentRow = this.screen_.cursorPosition.row;
rginda87b86462011-12-14 13:48:03 -08002299
2300 if (currentRow == scrollTop) {
2301 this.insertLines(1);
2302 } else {
2303 this.setAbsoluteCursorRow(currentRow - 1);
2304 }
2305};
2306
2307/**
rginda8ba33642011-12-14 12:31:31 -08002308 * Replace all characters to the left of the current cursor with the space
2309 * character.
2310 *
2311 * TODO(rginda): This should probably *remove* the characters (not just replace
2312 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002313 * position.
rginda8ba33642011-12-14 12:31:31 -08002314 */
2315hterm.Terminal.prototype.eraseToLeft = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002316 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002317 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002318 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002319 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002320 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002321};
2322
2323/**
David Benjamin684a9b72012-05-01 17:19:58 -04002324 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002325 *
2326 * The cursor position is unchanged.
2327 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002328 * If the current background color is not the default background color this
2329 * will insert spaces rather than delete. This is unfortunate because the
2330 * trailing space will affect text selection, but it's difficult to come up
2331 * with a way to style empty space that wouldn't trip up the hterm.Screen
2332 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002333 *
2334 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2335 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2336 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002337 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002338 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002339 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002340hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002341 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002342 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002343 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002344
Mike Frysingerdc727792020-04-10 01:41:13 -04002345 const maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002346 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002347
2348 if (this.screen_.textAttributes.background ===
2349 this.screen_.textAttributes.DEFAULT_COLOR) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002350 const cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002351 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002352 this.screen_.cursorPosition.column + count) {
2353 this.screen_.deleteChars(count);
2354 this.clearCursorOverflow();
2355 return;
2356 }
2357 }
2358
Mike Frysingerdc727792020-04-10 01:41:13 -04002359 const cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002360 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002361 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002362 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002363};
2364
2365/**
2366 * Erase the current line.
2367 *
2368 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002369 */
2370hterm.Terminal.prototype.eraseLine = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002371 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002372 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002373 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002374 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002375};
2376
2377/**
David Benjamina08d78f2012-05-05 00:28:49 -04002378 * Erase all characters from the start of the screen to the current cursor
2379 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002380 *
2381 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002382 */
2383hterm.Terminal.prototype.eraseAbove = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002384 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002385
2386 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002387
Mike Frysingerdc727792020-04-10 01:41:13 -04002388 for (let i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002389 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002390 this.screen_.clearCursorRow();
2391 }
2392
rginda87b86462011-12-14 13:48:03 -08002393 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002394 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002395};
2396
2397/**
2398 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002399 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002400 *
2401 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002402 */
2403hterm.Terminal.prototype.eraseBelow = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04002404 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002405
2406 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002407
Mike Frysingerdc727792020-04-10 01:41:13 -04002408 const bottom = this.screenSize.height - 1;
2409 for (let i = cursor.row + 1; i <= bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08002410 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002411 this.screen_.clearCursorRow();
2412 }
2413
rginda87b86462011-12-14 13:48:03 -08002414 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002415 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002416};
2417
2418/**
2419 * Fill the terminal with a given character.
2420 *
2421 * This methods does not respect the VT scroll region.
2422 *
2423 * @param {string} ch The character to use for the fill.
2424 */
2425hterm.Terminal.prototype.fill = function(ch) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002426 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002427
2428 this.setAbsoluteCursorPosition(0, 0);
Mike Frysingerdc727792020-04-10 01:41:13 -04002429 for (let row = 0; row < this.screenSize.height; row++) {
2430 for (let col = 0; col < this.screenSize.width; col++) {
rginda87b86462011-12-14 13:48:03 -08002431 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002432 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002433 }
2434 }
2435
2436 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002437};
2438
2439/**
rginda9ea433c2012-03-16 11:57:00 -07002440 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002441 *
rginda9ea433c2012-03-16 11:57:00 -07002442 * This does not respect the scroll region.
2443 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002444 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002445 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002446 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002447hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2448 if (!screen) {
2449 screen = this.screen_;
2450 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002451 const bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002452
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002453 this.accessibilityReader_.clear();
2454
rginda11057d52012-04-25 12:29:56 -07002455 if (bottom == 0) {
2456 // Empty screen, nothing to do.
2457 return;
2458 }
2459
Mike Frysingerdc727792020-04-10 01:41:13 -04002460 for (let i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002461 screen.setCursorPosition(i, 0);
2462 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002463 }
2464
rginda9ea433c2012-03-16 11:57:00 -07002465 screen.setCursorPosition(0, 0);
2466};
2467
2468/**
2469 * Erase the entire display without changing the cursor position.
2470 *
2471 * The cursor position is unchanged. This does not respect the scroll
2472 * region.
2473 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002474 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002475 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002476 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002477hterm.Terminal.prototype.clear = function(screen = undefined) {
2478 if (!screen) {
2479 screen = this.screen_;
2480 }
Mike Frysingerdc727792020-04-10 01:41:13 -04002481 const cursor = screen.cursorPosition.clone();
rginda9ea433c2012-03-16 11:57:00 -07002482 this.clearHome(screen);
2483 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002484};
2485
2486/**
2487 * VT command to insert lines at the current cursor row.
2488 *
2489 * This respects the current scroll region. Rows pushed off the bottom are
2490 * lost (they won't show up in the scrollback buffer).
2491 *
Joel Hockey0f933582019-08-27 18:01:51 -07002492 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002493 */
2494hterm.Terminal.prototype.insertLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002495 const cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002496
Mike Frysingerdc727792020-04-10 01:41:13 -04002497 const bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002498 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002499
Robert Ginda579186b2012-09-26 11:40:04 -07002500 // The moveCount is the number of rows we need to relocate to make room for
2501 // the new row(s). The count is the distance to move them.
Mike Frysingerdc727792020-04-10 01:41:13 -04002502 const moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002503 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002504 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002505 }
rginda8ba33642011-12-14 12:31:31 -08002506
Mike Frysingerdc727792020-04-10 01:41:13 -04002507 for (let i = count - 1; i >= 0; i--) {
Robert Ginda579186b2012-09-26 11:40:04 -07002508 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002509 this.screen_.clearCursorRow();
2510 }
rginda8ba33642011-12-14 12:31:31 -08002511};
2512
2513/**
2514 * VT command to delete lines at the current cursor row.
2515 *
2516 * New rows are added to the bottom of scroll region to take their place. New
2517 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002518 *
2519 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002520 */
2521hterm.Terminal.prototype.deleteLines = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002522 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002523
Mike Frysingerdc727792020-04-10 01:41:13 -04002524 const top = cursor.row;
2525 const bottom = this.getVTScrollBottom();
rginda8ba33642011-12-14 12:31:31 -08002526
Mike Frysingerdc727792020-04-10 01:41:13 -04002527 const maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002528 count = Math.min(count, maxCount);
2529
Mike Frysingerdc727792020-04-10 01:41:13 -04002530 const moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002531 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002532 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002533 }
rginda8ba33642011-12-14 12:31:31 -08002534
Mike Frysingerdc727792020-04-10 01:41:13 -04002535 for (let i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002536 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002537 this.screen_.clearCursorRow();
2538 }
2539
rginda87b86462011-12-14 13:48:03 -08002540 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002541 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002542};
2543
2544/**
2545 * Inserts the given number of spaces at the current cursor position.
2546 *
rginda87b86462011-12-14 13:48:03 -08002547 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002548 *
2549 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002550 */
2551hterm.Terminal.prototype.insertSpace = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002552 const cursor = this.saveCursor();
rginda87b86462011-12-14 13:48:03 -08002553
Mike Frysinger73e56462019-07-17 00:23:46 -05002554 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002555 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002556 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002557
2558 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002559 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002560};
2561
2562/**
2563 * Forward-delete the specified number of characters starting at the cursor
2564 * position.
2565 *
Joel Hockey0f933582019-08-27 18:01:51 -07002566 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002567 */
2568hterm.Terminal.prototype.deleteChars = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002569 const deleted = this.screen_.deleteChars(count);
Robert Ginda7fd57082012-09-25 14:41:47 -07002570 if (deleted && !this.screen_.textAttributes.isDefault()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002571 const cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07002572 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002573 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002574 this.restoreCursor(cursor);
2575 }
2576
David Benjamin54e8bf62012-06-01 22:31:40 -04002577 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002578};
2579
2580/**
2581 * Shift rows in the scroll region upwards by a given number of lines.
2582 *
2583 * New rows are inserted at the bottom of the scroll region to fill the
2584 * vacated rows. The new rows not filled out with the current text attributes.
2585 *
2586 * This function does not affect the scrollback rows at all. Rows shifted
2587 * off the top are lost.
2588 *
rginda87b86462011-12-14 13:48:03 -08002589 * The cursor position is not altered.
2590 *
Joel Hockey0f933582019-08-27 18:01:51 -07002591 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002592 */
2593hterm.Terminal.prototype.vtScrollUp = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002594 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002595
rginda87b86462011-12-14 13:48:03 -08002596 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002597 this.deleteLines(count);
2598
rginda87b86462011-12-14 13:48:03 -08002599 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002600};
2601
2602/**
2603 * Shift rows below the cursor down by a given number of lines.
2604 *
2605 * This function respects the current scroll region.
2606 *
2607 * New rows are inserted at the top of the scroll region to fill the
2608 * vacated rows. The new rows not filled out with the current text attributes.
2609 *
2610 * This function does not affect the scrollback rows at all. Rows shifted
2611 * off the bottom are lost.
2612 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002613 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002614 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002615hterm.Terminal.prototype.vtScrollDown = function(count) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002616 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002617
rginda87b86462011-12-14 13:48:03 -08002618 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002619 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002620
rginda87b86462011-12-14 13:48:03 -08002621 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002622};
2623
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002624/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002625 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002626 *
2627 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002628 * cause Assitive Technology to announce the output of the terminal. It also
2629 * enables other features that aid assistive technology. All the features gated
2630 * behind this flag have a performance impact on the terminal which is why they
2631 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002632 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002633 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002634 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002635hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002636 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002637};
rginda87b86462011-12-14 13:48:03 -08002638
rginda8ba33642011-12-14 12:31:31 -08002639/**
2640 * Set the cursor position.
2641 *
2642 * The cursor row is relative to the scroll region if the terminal has
2643 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2644 *
Joel Hockey0f933582019-08-27 18:01:51 -07002645 * @param {number} row The new zero-based cursor row.
2646 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002647 */
2648hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2649 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002650 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002651 } else {
rginda87b86462011-12-14 13:48:03 -08002652 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002653 }
rginda87b86462011-12-14 13:48:03 -08002654};
rginda8ba33642011-12-14 12:31:31 -08002655
Evan Jones2600d4f2016-12-06 09:29:36 -05002656/**
2657 * Move the cursor relative to its current position.
2658 *
2659 * @param {number} row
2660 * @param {number} column
2661 */
rginda87b86462011-12-14 13:48:03 -08002662hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002663 const scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002664 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2665 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002666 this.screen_.setCursorPosition(row, column);
2667};
2668
Evan Jones2600d4f2016-12-06 09:29:36 -05002669/**
2670 * Move the cursor to the specified position.
2671 *
2672 * @param {number} row
2673 * @param {number} column
2674 */
rginda87b86462011-12-14 13:48:03 -08002675hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002676 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2677 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002678 this.screen_.setCursorPosition(row, column);
2679};
2680
2681/**
2682 * Set the cursor column.
2683 *
Joel Hockey0f933582019-08-27 18:01:51 -07002684 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002685 */
2686hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002687 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002688};
2689
2690/**
2691 * Return the cursor column.
2692 *
Joel Hockey0f933582019-08-27 18:01:51 -07002693 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002694 */
2695hterm.Terminal.prototype.getCursorColumn = function() {
2696 return this.screen_.cursorPosition.column;
2697};
2698
2699/**
2700 * Set the cursor row.
2701 *
2702 * The cursor row is relative to the scroll region if the terminal has
2703 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2704 *
Joel Hockey0f933582019-08-27 18:01:51 -07002705 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002706 */
rginda87b86462011-12-14 13:48:03 -08002707hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2708 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002709};
2710
2711/**
2712 * Return the cursor row.
2713 *
Joel Hockey0f933582019-08-27 18:01:51 -07002714 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002715 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002716hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002717 return this.screen_.cursorPosition.row;
2718};
2719
2720/**
2721 * Request that the ScrollPort redraw itself soon.
2722 *
2723 * The redraw will happen asynchronously, soon after the call stack winds down.
2724 * Multiple calls will be coalesced into a single redraw.
2725 */
2726hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002727 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002728 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002729 }
rginda8ba33642011-12-14 12:31:31 -08002730
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002731 this.timeouts_.redraw = setTimeout(() => {
2732 delete this.timeouts_.redraw;
2733 this.scrollPort_.redraw_();
2734 });
rginda8ba33642011-12-14 12:31:31 -08002735};
2736
2737/**
2738 * Request that the ScrollPort be scrolled to the bottom.
2739 *
2740 * The scroll will happen asynchronously, soon after the call stack winds down.
2741 * Multiple calls will be coalesced into a single scroll.
2742 *
2743 * This affects the scrollbar position of the ScrollPort, and has nothing to
2744 * do with the VT scroll commands.
2745 */
2746hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002747 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002748 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002749 }
rginda8ba33642011-12-14 12:31:31 -08002750
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002751 this.timeouts_.scrollDown = setTimeout(() => {
2752 delete this.timeouts_.scrollDown;
2753 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2754 }, 10);
rginda8ba33642011-12-14 12:31:31 -08002755};
2756
2757/**
2758 * Move the cursor up a specified number of rows.
2759 *
Joel Hockey0f933582019-08-27 18:01:51 -07002760 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002761 */
2762hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002763 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002764};
2765
2766/**
2767 * Move the cursor down a specified number of rows.
2768 *
Joel Hockey0f933582019-08-27 18:01:51 -07002769 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002770 */
2771hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002772 count = count || 1;
Mike Frysingerdc727792020-04-10 01:41:13 -04002773 const minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2774 const maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2775 this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08002776
Mike Frysingerdc727792020-04-10 01:41:13 -04002777 const row = lib.f.clamp(this.screen_.cursorPosition.row + count,
2778 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002779 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002780};
2781
2782/**
2783 * Move the cursor left a specified number of columns.
2784 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002785 * If reverse wraparound mode is enabled and the previous row wrapped into
2786 * the current row then we back up through the wraparound as well.
2787 *
Joel Hockey0f933582019-08-27 18:01:51 -07002788 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002789 */
2790hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002791 count = count || 1;
2792
Mike Frysingerbdb34802020-04-07 03:47:32 -04002793 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002794 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002795 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002796
Mike Frysingerdc727792020-04-10 01:41:13 -04002797 const currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002798 if (this.options_.reverseWraparound) {
2799 if (this.screen_.cursorPosition.overflow) {
2800 // If this cursor is in the right margin, consume one count to get it
2801 // back to the last column. This only applies when we're in reverse
2802 // wraparound mode.
2803 count--;
2804 this.clearCursorOverflow();
2805
Mike Frysingerbdb34802020-04-07 03:47:32 -04002806 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002807 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002808 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002809 }
2810
Mike Frysingerdc727792020-04-10 01:41:13 -04002811 let newRow = this.screen_.cursorPosition.row;
2812 let newColumn = currentColumn - count;
Robert Gindabfb32622014-07-17 13:20:27 -07002813 if (newColumn < 0) {
2814 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2815 if (newRow < 0) {
2816 // xterm also wraps from row 0 to the last row.
2817 newRow = this.screenSize.height + newRow % this.screenSize.height;
2818 }
2819 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2820 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002821
Robert Gindabfb32622014-07-17 13:20:27 -07002822 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2823
2824 } else {
Mike Frysingerdc727792020-04-10 01:41:13 -04002825 const newColumn = Math.max(currentColumn - count, 0);
Robert Gindabfb32622014-07-17 13:20:27 -07002826 this.setCursorColumn(newColumn);
2827 }
rginda8ba33642011-12-14 12:31:31 -08002828};
2829
2830/**
2831 * Move the cursor right a specified number of columns.
2832 *
Joel Hockey0f933582019-08-27 18:01:51 -07002833 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002834 */
2835hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002836 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002837
Mike Frysingerbdb34802020-04-07 03:47:32 -04002838 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002839 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002840 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002841
Mike Frysingerdc727792020-04-10 01:41:13 -04002842 const column = lib.f.clamp(this.screen_.cursorPosition.column + count,
2843 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002844 this.setCursorColumn(column);
2845};
2846
2847/**
2848 * Reverse the foreground and background colors of the terminal.
2849 *
2850 * This only affects text that was drawn with no attributes.
2851 *
2852 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2853 * been drawn with attributes that happen to coincide with the default
2854 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002855 *
2856 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002857 */
2858hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002859 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002860 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002861 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2862 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002863 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002864 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2865 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002866 }
2867};
2868
2869/**
rginda87b86462011-12-14 13:48:03 -08002870 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002871 *
2872 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002873 */
2874hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002875 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002876
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002877 setTimeout(() => this.restyleCursor_(), 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002878
Michael Kelly485ecd12014-06-09 11:41:56 -04002879 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002880 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002881 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002882 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002883
Robert Ginda92e18102013-03-14 13:56:37 -07002884 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002885 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002886 this.bellSequelchTimeout_ = setTimeout(() => {
2887 this.bellSquelchTimeout_ = null;
2888 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002889 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002890 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002891 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002892
2893 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingerdc727792020-04-10 01:41:13 -04002894 const n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002895 this.bellNotificationList_.push(n);
2896 // TODO: Should we try to raise the window here?
Mike Frysinger2acd3a52020-04-10 02:20:57 -04002897 n.onclick = () => this.closeBellNotifications_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002898 }
rginda87b86462011-12-14 13:48:03 -08002899};
2900
2901/**
rginda8ba33642011-12-14 12:31:31 -08002902 * Set the origin mode bit.
2903 *
2904 * If origin mode is on, certain VT cursor and scrolling commands measure their
2905 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2906 * to the top of the addressable screen.
2907 *
2908 * Defaults to off.
2909 *
2910 * @param {boolean} state True to set origin mode, false to unset.
2911 */
2912hterm.Terminal.prototype.setOriginMode = function(state) {
2913 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002914 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002915};
2916
2917/**
2918 * Set the insert mode bit.
2919 *
2920 * If insert mode is on, existing text beyond the cursor position will be
2921 * shifted right to make room for new text. Otherwise, new text overwrites
2922 * any existing text.
2923 *
2924 * Defaults to off.
2925 *
2926 * @param {boolean} state True to set insert mode, false to unset.
2927 */
2928hterm.Terminal.prototype.setInsertMode = function(state) {
2929 this.options_.insertMode = state;
2930};
2931
2932/**
rginda87b86462011-12-14 13:48:03 -08002933 * Set the auto carriage return bit.
2934 *
2935 * If auto carriage return is on then a formfeed character is interpreted
2936 * as a newline, otherwise it's the same as a linefeed. The difference boils
2937 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002938 *
2939 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002940 */
2941hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2942 this.options_.autoCarriageReturn = state;
2943};
2944
2945/**
rginda8ba33642011-12-14 12:31:31 -08002946 * Set the wraparound mode bit.
2947 *
2948 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2949 * to the start of the following row. Otherwise, the cursor is clamped to the
2950 * end of the screen and attempts to write past it are ignored.
2951 *
2952 * Defaults to on.
2953 *
2954 * @param {boolean} state True to set wraparound mode, false to unset.
2955 */
2956hterm.Terminal.prototype.setWraparound = function(state) {
2957 this.options_.wraparound = state;
2958};
2959
2960/**
2961 * Set the reverse-wraparound mode bit.
2962 *
2963 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2964 * to the end of the previous row. Otherwise, the cursor is clamped to column
2965 * 0.
2966 *
2967 * Defaults to off.
2968 *
2969 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2970 */
2971hterm.Terminal.prototype.setReverseWraparound = function(state) {
2972 this.options_.reverseWraparound = state;
2973};
2974
2975/**
2976 * Selects between the primary and alternate screens.
2977 *
2978 * If alternate mode is on, the alternate screen is active. Otherwise the
2979 * primary screen is active.
2980 *
2981 * Swapping screens has no effect on the scrollback buffer.
2982 *
2983 * Each screen maintains its own cursor position.
2984 *
2985 * Defaults to off.
2986 *
2987 * @param {boolean} state True to set alternate mode, false to unset.
2988 */
2989hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002990 if (state == (this.screen_ == this.alternateScreen_)) {
2991 return;
2992 }
2993 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2994 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002995 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2996
Joel Hockey42dba8f2020-03-26 16:21:11 -07002997 // Swap color overrides.
2998 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2999 oldOverrides.forEach((c, i) => {
3000 if (!newOverrides.hasOwnProperty(i)) {
3001 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
3002 }
3003 });
3004 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
3005
rginda35c456b2012-02-09 17:29:05 -08003006 if (this.screen_.rowsArray.length &&
3007 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
3008 // If the screen changed sizes while we were away, our rowIndexes may
3009 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07003010 const offset = this.scrollbackRows_.length;
3011 const ary = this.screen_.rowsArray;
3012 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08003013 ary[i].rowIndex = offset + i;
3014 }
3015 }
rginda8ba33642011-12-14 12:31:31 -08003016
rginda35c456b2012-02-09 17:29:05 -08003017 this.realizeWidth_(this.screenSize.width);
3018 this.realizeHeight_(this.screenSize.height);
3019 this.scrollPort_.syncScrollHeight();
3020 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08003021
rginda6d397402012-01-17 10:58:29 -08003022 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08003023 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08003024};
3025
3026/**
3027 * Set the cursor-blink mode bit.
3028 *
3029 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
3030 * a visible cursor does not blink.
3031 *
3032 * You should make sure to turn blinking off if you're going to dispose of a
3033 * terminal, otherwise you'll leak a timeout.
3034 *
3035 * Defaults to on.
3036 *
3037 * @param {boolean} state True to set cursor-blink mode, false to unset.
3038 */
3039hterm.Terminal.prototype.setCursorBlink = function(state) {
3040 this.options_.cursorBlink = state;
3041
3042 if (!state && this.timeouts_.cursorBlink) {
3043 clearTimeout(this.timeouts_.cursorBlink);
3044 delete this.timeouts_.cursorBlink;
3045 }
3046
Mike Frysingerbdb34802020-04-07 03:47:32 -04003047 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08003048 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003049 }
rginda8ba33642011-12-14 12:31:31 -08003050};
3051
3052/**
3053 * Set the cursor-visible mode bit.
3054 *
3055 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
3056 *
3057 * Defaults to on.
3058 *
3059 * @param {boolean} state True to set cursor-visible mode, false to unset.
3060 */
3061hterm.Terminal.prototype.setCursorVisible = function(state) {
3062 this.options_.cursorVisible = state;
3063
3064 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07003065 if (this.timeouts_.cursorBlink) {
3066 clearTimeout(this.timeouts_.cursorBlink);
3067 delete this.timeouts_.cursorBlink;
3068 }
rginda87b86462011-12-14 13:48:03 -08003069 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08003070 return;
3071 }
3072
rginda87b86462011-12-14 13:48:03 -08003073 this.syncCursorPosition_();
3074
3075 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08003076
3077 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003078 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08003079 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003080 }
rginda8ba33642011-12-14 12:31:31 -08003081
Robert Gindaea2183e2014-07-17 09:51:51 -07003082 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003083 } else {
3084 if (this.timeouts_.cursorBlink) {
3085 clearTimeout(this.timeouts_.cursorBlink);
3086 delete this.timeouts_.cursorBlink;
3087 }
3088 }
3089};
3090
3091/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003092 * Pause blinking temporarily.
3093 *
3094 * When the cursor moves around, it can be helpful to momentarily pause the
3095 * blinking. This could be when the user is typing in things, or when they're
3096 * moving around with the arrow keys.
3097 */
3098hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3099 if (!this.options_.cursorBlink) {
3100 return;
3101 }
3102
3103 this.cursorBlinkPause_ = true;
3104
3105 // If a timeout is already pending, reset the clock due to the new input.
3106 if (this.timeouts_.cursorBlinkPause) {
3107 clearTimeout(this.timeouts_.cursorBlinkPause);
3108 }
3109 // After 500ms, resume blinking. That seems like a good balance between user
3110 // input timings & responsiveness to resume.
3111 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3112 delete this.timeouts_.cursorBlinkPause;
3113 this.cursorBlinkPause_ = false;
3114 }, 500);
3115};
3116
3117/**
rginda87b86462011-12-14 13:48:03 -08003118 * Synchronizes the visible cursor and document selection with the current
3119 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003120 *
3121 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003122 */
3123hterm.Terminal.prototype.syncCursorPosition_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003124 const topRowIndex = this.scrollPort_.getTopRowIndex();
3125 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3126 const cursorRowIndex = this.scrollbackRows_.length +
rginda8ba33642011-12-14 12:31:31 -08003127 this.screen_.cursorPosition.row;
3128
Raymes Khoury15697f42018-07-17 11:37:18 +10003129 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003130 if (this.accessibilityReader_.accessibilityEnabled) {
3131 // Report the new position of the cursor for accessibility purposes.
3132 const cursorColumnIndex = this.screen_.cursorPosition.column;
3133 const cursorLineText =
3134 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003135 // This will force the selection to be sync'd to the cursor position if the
3136 // user has pressed a key. Generally we would only sync the cursor position
3137 // when selection is collapsed so that if the user has selected something
3138 // we don't clear the selection by moving the selection. However when a
3139 // screen reader is used, it's intuitive for entering a key to move the
3140 // selection to the cursor.
3141 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003142 this.accessibilityReader_.afterCursorChange(
3143 cursorLineText, cursorRowIndex, cursorColumnIndex);
3144 }
3145
rginda8ba33642011-12-14 12:31:31 -08003146 if (cursorRowIndex > bottomRowIndex) {
Joel Hockey3babf302020-04-22 15:00:06 -07003147 // Cursor is scrolled off screen, hide it.
3148 this.cursorOffScreen_ = true;
3149 this.cursorNode_.style.display = 'none';
Raymes Khourye5d48982018-08-02 09:08:32 +10003150 return false;
rginda8ba33642011-12-14 12:31:31 -08003151 }
3152
Joel Hockey3babf302020-04-22 15:00:06 -07003153 if (this.cursorNode_.style.display == 'none') {
3154 // Re-display the terminal cursor if it was hidden.
3155 this.cursorOffScreen_ = false;
Robert Gindab837c052014-08-11 11:17:51 -07003156 this.cursorNode_.style.display = '';
3157 }
3158
Mike Frysinger44c32202017-08-05 01:13:09 -04003159 // Position the cursor using CSS variable math. If we do the math in JS,
3160 // the float math will end up being more precise than the CSS which will
3161 // cause the cursor tracking to be off.
3162 this.setCssVar(
3163 'cursor-offset-row',
3164 `${cursorRowIndex - topRowIndex} + ` +
3165 `${this.scrollPort_.visibleRowTopMargin}px`);
3166 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003167
3168 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003169 '(' + this.screen_.cursorPosition.column +
3170 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003171 ')');
3172
3173 // Update the caret for a11y purposes.
Mike Frysingerdc727792020-04-10 01:41:13 -04003174 const selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003175 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003176 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003177 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003178 return true;
rginda8ba33642011-12-14 12:31:31 -08003179};
3180
Robert Gindafb1be6a2013-12-11 11:56:22 -08003181/**
3182 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3183 * and character cell dimensions.
3184 */
Robert Ginda830583c2013-08-07 13:20:46 -07003185hterm.Terminal.prototype.restyleCursor_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003186 let shape = this.cursorShape_;
Robert Ginda830583c2013-08-07 13:20:46 -07003187
3188 if (this.cursorNode_.getAttribute('focus') == 'false') {
3189 // Always show a block cursor when unfocused.
3190 shape = hterm.Terminal.cursorShape.BLOCK;
3191 }
3192
Mike Frysingerdc727792020-04-10 01:41:13 -04003193 const style = this.cursorNode_.style;
Robert Ginda830583c2013-08-07 13:20:46 -07003194
3195 switch (shape) {
3196 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003197 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003198 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003199 style.borderLeftStyle = 'solid';
3200 break;
3201
3202 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003203 style.backgroundColor = 'transparent';
3204 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003205 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003206 break;
3207
3208 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003209 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003210 style.borderBottomStyle = '';
3211 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003212 break;
3213 }
3214};
3215
rginda8ba33642011-12-14 12:31:31 -08003216/**
3217 * Synchronizes the visible cursor with the current cursor coordinates.
3218 *
3219 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003220 * Multiple calls will be coalesced into a single sync. This should be called
3221 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003222 */
3223hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003224 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003225 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003226 }
rginda8ba33642011-12-14 12:31:31 -08003227
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003228 if (this.accessibilityReader_.accessibilityEnabled) {
3229 // Report the previous position of the cursor for accessibility purposes.
3230 const cursorRowIndex = this.scrollbackRows_.length +
3231 this.screen_.cursorPosition.row;
3232 const cursorColumnIndex = this.screen_.cursorPosition.column;
3233 const cursorLineText =
3234 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3235 this.accessibilityReader_.beforeCursorChange(
3236 cursorLineText, cursorRowIndex, cursorColumnIndex);
3237 }
3238
Mike Frysinger2acd3a52020-04-10 02:20:57 -04003239 this.timeouts_.syncCursor = setTimeout(() => {
3240 this.syncCursorPosition_();
3241 delete this.timeouts_.syncCursor;
3242 });
rginda87b86462011-12-14 13:48:03 -08003243};
3244
rgindacc2996c2012-02-24 14:59:31 -08003245/**
rgindaf522ce02012-04-17 17:49:17 -07003246 * Show or hide the zoom warning.
3247 *
3248 * The zoom warning is a message warning the user that their browser zoom must
3249 * be set to 100% in order for hterm to function properly.
3250 *
3251 * @param {boolean} state True to show the message, false to hide it.
3252 */
3253hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3254 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003255 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003256 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003257 }
rgindaf522ce02012-04-17 17:49:17 -07003258
3259 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003260 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003261 this.zoomWarningNode_.style.cssText = (
3262 'color: black;' +
3263 'background-color: #ff2222;' +
3264 'font-size: large;' +
3265 'border-radius: 8px;' +
3266 'opacity: 0.75;' +
3267 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3268 'top: 0.5em;' +
3269 'right: 1.2em;' +
3270 'position: absolute;' +
3271 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003272 '-webkit-user-select: none;' +
3273 '-moz-text-size-adjust: none;' +
3274 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003275
3276 this.zoomWarningNode_.addEventListener('click', function(e) {
3277 this.parentNode.removeChild(this);
3278 });
rgindaf522ce02012-04-17 17:49:17 -07003279 }
3280
Mike Frysingerb7289952019-03-23 16:05:38 -07003281 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003282 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003283 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003284
rgindaf522ce02012-04-17 17:49:17 -07003285 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3286
3287 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003288 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003289 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003290 }
rgindaf522ce02012-04-17 17:49:17 -07003291 } else if (this.zoomWarningNode_.parentNode) {
3292 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3293 }
3294};
3295
3296/**
rgindacc2996c2012-02-24 14:59:31 -08003297 * Show the terminal overlay for a given amount of time.
3298 *
Jason Lin3d825782020-05-12 11:02:48 +10003299 * The terminal overlay appears in inverse video, centered over the terminal.
rgindacc2996c2012-02-24 14:59:31 -08003300 *
3301 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003302 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003303 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3304 * stay up forever (or until the next overlay).
3305 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003306hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
Jason Lin34567412020-05-14 10:32:09 +10003307 this.showOverlayWithNode(new Text(msg), timeout);
3308};
3309
3310/**
3311 * Show the terminal overlay for a given amount of time.
3312 *
3313 * The terminal overlay appears in inverse video, centered over the terminal.
3314 *
3315 * @param {!Node} node The node to display in the overlay.
3316 * @param {?number=} timeout The amount of time to wait before fading out
3317 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3318 * stay up forever (or until the next overlay).
3319 */
3320hterm.Terminal.prototype.showOverlayWithNode = function(node, timeout = 1500) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003321 if (!this.ready_ || !this.div_) {
3322 return;
3323 }
rgindaf0090c92012-02-10 14:58:52 -08003324
Joel Hockeyedac0e72020-05-14 20:16:20 -07003325 if (!this.overlayNode_) {
rgindaf0090c92012-02-10 14:58:52 -08003326 this.overlayNode_ = this.document_.createElement('div');
3327 this.overlayNode_.style.cssText = (
Joel Hockeyedac0e72020-05-14 20:16:20 -07003328 'color: rgb(var(--hterm-background-color));' +
3329 'background-color: rgb(var(--hterm-foreground-color));' +
Jason Lin3d825782020-05-12 11:02:48 +10003330 'border-radius: 12px;' +
Joel Hockeyedac0e72020-05-14 20:16:20 -07003331 'font: 500 var(--hterm-font-size) "Noto Sans", sans-serif;' +
rgindaf0090c92012-02-10 14:58:52 -08003332 'opacity: 0.75;' +
Jason Lin3d825782020-05-12 11:02:48 +10003333 'padding: 0.923em 1.846em;' +
rgindaf0090c92012-02-10 14:58:52 -08003334 'position: absolute;' +
3335 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003336 '-webkit-transition: opacity 180ms ease-in;' +
3337 '-moz-user-select: none;' +
3338 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003339
3340 this.overlayNode_.addEventListener('mousedown', function(e) {
3341 e.preventDefault();
3342 e.stopPropagation();
3343 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003344 }
3345
Jason Lin34567412020-05-14 10:32:09 +10003346 this.overlayNode_.textContent = ''; // Remove all children first.
3347 this.overlayNode_.appendChild(node);
rgindaf0090c92012-02-10 14:58:52 -08003348
Mike Frysingerbdb34802020-04-07 03:47:32 -04003349 if (!this.overlayNode_.parentNode) {
Joel Hockeyedac0e72020-05-14 20:16:20 -07003350 this.document_.body.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003351 }
rgindaf0090c92012-02-10 14:58:52 -08003352
Mike Frysingerdc727792020-04-10 01:41:13 -04003353 const divSize = hterm.getClientSize(lib.notNull(this.div_));
3354 const overlaySize = hterm.getClientSize(this.overlayNode_);
Robert Ginda97769282013-02-01 15:30:30 -08003355
Robert Ginda8a59f762014-07-23 11:29:55 -07003356 this.overlayNode_.style.top =
3357 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003358 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003359 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003360
Mike Frysingerbdb34802020-04-07 03:47:32 -04003361 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003362 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003363 }
rgindaf0090c92012-02-10 14:58:52 -08003364
Jason Lin34567412020-05-14 10:32:09 +10003365 this.accessibilityReader_.assertiveAnnounce(this.overlayNode_.textContent);
Raymes Khouryc7a06382018-07-04 10:25:45 +10003366
Mike Frysingerec4225d2020-04-07 05:00:01 -04003367 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003368 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003369 }
rgindacc2996c2012-02-24 14:59:31 -08003370
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003371 this.overlayTimeout_ = setTimeout(() => {
3372 this.overlayNode_.style.opacity = '0';
3373 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003374 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003375};
3376
3377/**
3378 * Hide the terminal overlay immediately.
3379 *
3380 * Useful when we show an overlay for an event with an unknown end time.
3381 */
3382hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003383 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003384 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003385 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003386 this.overlayTimeout_ = null;
3387
Mike Frysingerbdb34802020-04-07 03:47:32 -04003388 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003389 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003390 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003391 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003392};
3393
rginda4bba5e12012-06-20 16:15:30 -07003394/**
3395 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003396 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003397 * Note: In Chrome, this should work unless the user has rejected the permission
3398 * request. In Firefox extension environment, you'll need the "clipboardRead"
3399 * permission. In other environments, this might always fail as the browser
3400 * frequently blocks access for security reasons.
3401 *
3402 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3403 * value is always null. Otherwise, this function uses legacy pasting and
3404 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003405 */
3406hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003407 if (!this.alwaysUseLegacyPasting &&
3408 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003409 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3410 return null;
3411 } else {
3412 // Legacy pasting.
3413 try {
3414 return this.document_.execCommand('paste');
3415 } catch (firefoxException) {
3416 // Ignore this. FF 40 and older would incorrectly throw an exception if
3417 // there was an error instead of returning false.
3418 return false;
3419 }
3420 }
rginda4bba5e12012-06-20 16:15:30 -07003421};
3422
3423/**
3424 * Copy a string to the system clipboard.
3425 *
3426 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003427 *
3428 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003429 */
3430hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003431 if (this.prefs_.get('enable-clipboard-notice')) {
Jason Lin34567412020-05-14 10:32:09 +10003432 if (!this.clipboardNotice_) {
3433 this.clipboardNotice_ = this.document_.createElement('div');
3434 this.clipboardNotice_.style.textAlign = 'center';
3435 const copyImage = lib.resource.getData('hterm/images/copy');
3436 this.clipboardNotice_.innerHTML =
3437 `${copyImage}<div>${hterm.msg('NOTIFY_COPY')}</div>`;
3438 }
3439 setTimeout(() => this.showOverlayWithNode(this.clipboardNotice_, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003440 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003441
Mike Frysinger96eacae2019-01-02 18:13:56 -05003442 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003443};
3444
Evan Jones2600d4f2016-12-06 09:29:36 -05003445/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003446 * Display an image.
3447 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003448 * Either URI or buffer or blob fields must be specified.
3449 *
Joel Hockey0f933582019-08-27 18:01:51 -07003450 * @param {{
3451 * name: (string|undefined),
3452 * size: (string|number|undefined),
3453 * preserveAspectRation: (boolean|undefined),
3454 * inline: (boolean|undefined),
3455 * width: (string|number|undefined),
3456 * height: (string|number|undefined),
3457 * align: (string|undefined),
3458 * url: (string|undefined),
3459 * buffer: (!ArrayBuffer|undefined),
3460 * blob: (!Blob|undefined),
3461 * type: (string|undefined),
3462 * }} options The image to display.
3463 * name A human readable string for the image
3464 * size The size (in bytes).
3465 * preserveAspectRatio Whether to preserve aspect.
3466 * inline Whether to display the image inline.
3467 * width The width of the image.
3468 * height The height of the image.
3469 * align Direction to align the image.
3470 * uri The source URI for the image.
3471 * buffer The ArrayBuffer image data.
3472 * blob The Blob image data.
3473 * type The MIME type of the image data.
3474 * @param {function()=} onLoad Callback when loading finishes.
3475 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003476 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003477hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003478 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003479 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003480 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003481 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003482 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003483
3484 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003485 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003486 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003487 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003488
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003489 // See if the mime type is available. If not, guess from the filename.
3490 // We don't list all possible mime types because the browser can usually
3491 // guess it correctly. So list the ones that need a bit more help.
3492 if (!options.type) {
3493 const ary = options.name.split('.');
3494 const ext = ary[ary.length - 1].trim();
3495 switch (ext) {
3496 case 'svg':
3497 case 'svgz':
3498 options.type = 'image/svg+xml';
3499 break;
3500 }
3501 }
3502
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003503 // Has the user approved image display yet?
3504 if (this.allowImagesInline !== true) {
3505 this.newLine();
3506 const row = this.getRowNode(this.scrollbackRows_.length +
3507 this.getCursorRow() - 1);
3508
3509 if (this.allowImagesInline === false) {
3510 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3511 'Inline Images Disabled');
3512 return;
3513 }
3514
3515 // Show a prompt.
3516 let button;
3517 const span = this.document_.createElement('span');
3518 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3519 span.style.fontWeight = 'bold';
3520 span.style.borderWidth = '1px';
3521 span.style.borderStyle = 'dashed';
3522 button = this.document_.createElement('span');
3523 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3524 button.style.marginLeft = '1em';
3525 button.style.borderWidth = '1px';
3526 button.style.borderStyle = 'solid';
3527 button.addEventListener('click', () => {
3528 this.prefs_.set('allow-images-inline', false);
3529 });
3530 span.appendChild(button);
3531 button = this.document_.createElement('span');
3532 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3533 'allow this session');
3534 button.style.marginLeft = '1em';
3535 button.style.borderWidth = '1px';
3536 button.style.borderStyle = 'solid';
3537 button.addEventListener('click', () => {
3538 this.allowImagesInline = true;
3539 });
3540 span.appendChild(button);
3541 button = this.document_.createElement('span');
3542 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3543 button.style.marginLeft = '1em';
3544 button.style.borderWidth = '1px';
3545 button.style.borderStyle = 'solid';
3546 button.addEventListener('click', () => {
3547 this.prefs_.set('allow-images-inline', true);
3548 });
3549 span.appendChild(button);
3550
3551 row.appendChild(span);
3552 return;
3553 }
3554
3555 // See if we should show this object directly, or download it.
3556 if (options.inline) {
3557 const io = this.io.push();
3558 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003559 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003560
3561 // While we're loading the image, eat all the user's input.
3562 io.onVTKeystroke = io.sendString = () => {};
3563
3564 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003565 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003566 if (options.uri !== undefined) {
3567 img.src = options.uri;
3568 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003569 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003570 img.src = URL.createObjectURL(blob);
3571 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003572 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003573 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003574 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003575 img.title = img.alt = options.name;
3576
3577 // Attach the image to the page to let it load/render. It won't stay here.
3578 // This is needed so it's visible and the DOM can calculate the height. If
3579 // the image is hidden or not in the DOM, the height is always 0.
3580 this.document_.body.appendChild(img);
3581
3582 // Wait for the image to finish loading before we try moving it to the
3583 // right place in the terminal.
3584 img.onload = () => {
3585 // Now that we have the image dimensions, figure out how to show it.
Joel Hockey370a9ce2020-04-22 15:06:54 -07003586 const screenSize = this.scrollPort_.getScreenSize();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
Joel Hockey370a9ce2020-04-22 15:06:54 -07003588 img.style.maxWidth = `${screenSize.width}px`;
3589 img.style.maxHeight = `${screenSize.height}px`;
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003590
3591 // Parse a width/height specification.
3592 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003593 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003594 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003595 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003596
3597 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3598 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003599 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003600 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003601 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003602 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003603 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003604 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003605 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003606 }
3607
3608 return '';
3609 };
Joel Hockey370a9ce2020-04-22 15:06:54 -07003610 img.style.width = parseDim(
3611 options.width, screenSize.width, '--hterm-charsize-width');
3612 img.style.height = parseDim(
Mike Frysinger58f023d2020-04-07 19:56:11 -04003613 options.height, screenSize.height, '--hterm-charsize-height');
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003614
3615 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003616 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003617 const padRows = Math.ceil(img.clientHeight /
3618 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003619 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003620 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003621 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003622
3623 // Update the max height in case the user shrinks the character size.
3624 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3625
3626 // Move the image to the last row. This way when we scroll up, it doesn't
3627 // disappear when the first row gets clipped. It will disappear when we
3628 // scroll down and the last row is clipped ...
3629 this.document_.body.removeChild(img);
3630 // Create a wrapper node so we can do an absolute in a relative position.
3631 // This helps with rounding errors between JS & CSS counts.
3632 const div = this.document_.createElement('div');
3633 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003634 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003635 img.style.position = 'absolute';
3636 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3637 div.appendChild(img);
3638 const row = this.getRowNode(this.scrollbackRows_.length +
3639 this.getCursorRow() - 1);
3640 row.appendChild(div);
3641
Mike Frysinger2558ed52019-01-14 01:03:41 -05003642 // Now that the image has been read, we can revoke the source.
3643 if (options.uri === undefined) {
3644 URL.revokeObjectURL(img.src);
3645 }
3646
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003647 io.hideOverlay();
3648 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003649
Mike Frysingerbdb34802020-04-07 03:47:32 -04003650 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003651 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003652 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003653 };
3654
3655 // If we got a malformed image, give up.
3656 img.onerror = (e) => {
3657 this.document_.body.removeChild(img);
3658 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003659 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003660 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003661
Mike Frysingerbdb34802020-04-07 03:47:32 -04003662 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003663 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003664 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003665 };
3666 } else {
3667 // We can't use chrome.downloads.download as that requires "downloads"
3668 // permissions, and that works only in extensions, not apps.
3669 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003670 if (options.uri !== undefined) {
3671 a.href = options.uri;
3672 } else if (options.buffer !== undefined) {
3673 const blob = new Blob([options.buffer]);
3674 a.href = URL.createObjectURL(blob);
3675 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003676 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003677 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003678 a.download = options.name;
3679 this.document_.body.appendChild(a);
3680 a.click();
3681 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003682 if (options.uri === undefined) {
3683 URL.revokeObjectURL(a.href);
3684 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003685 }
3686};
3687
3688/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003689 * Returns the selected text, or null if no text is selected.
3690 *
3691 * @return {string|null}
3692 */
rgindaa09e7332012-08-17 12:49:51 -07003693hterm.Terminal.prototype.getSelectionText = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003694 const selection = this.scrollPort_.selection;
rgindaa09e7332012-08-17 12:49:51 -07003695 selection.sync();
3696
Mike Frysingerbdb34802020-04-07 03:47:32 -04003697 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003698 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003699 }
rgindaa09e7332012-08-17 12:49:51 -07003700
rgindaa09e7332012-08-17 12:49:51 -07003701 // Start offset measures from the beginning of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003702 let startOffset = selection.startOffset;
3703 let node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003704
Raymes Khoury334625a2018-06-25 10:29:40 +10003705 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003706 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003707 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003708 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003709
Robert Gindafdbb3f22012-09-06 20:23:06 -07003710 if (node.nodeName != 'X-ROW') {
3711 // If the selection doesn't start on an x-row node, then it must be
3712 // somewhere inside the x-row. Add any characters from previous siblings
3713 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003714
3715 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3716 // If node is the text node in a styled span, move up to the span node.
3717 node = node.parentNode;
3718 }
3719
Robert Gindafdbb3f22012-09-06 20:23:06 -07003720 while (node.previousSibling) {
3721 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003722 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003723 }
rgindaa09e7332012-08-17 12:49:51 -07003724 }
3725
3726 // End offset measures from the end of the line.
Mike Frysingerdc727792020-04-10 01:41:13 -04003727 let endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
Ricky Liang48f05cb2013-12-31 23:35:29 +08003728 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003729 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003730
Robert Gindafdbb3f22012-09-06 20:23:06 -07003731 if (node.nodeName != 'X-ROW') {
3732 // If the selection doesn't end on an x-row node, then it must be
3733 // somewhere inside the x-row. Add any characters from following siblings
3734 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003735
3736 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3737 // If node is the text node in a styled span, move up to the span node.
3738 node = node.parentNode;
3739 }
3740
Robert Gindafdbb3f22012-09-06 20:23:06 -07003741 while (node.nextSibling) {
3742 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003743 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003744 }
rgindaa09e7332012-08-17 12:49:51 -07003745 }
3746
Mike Frysingerdc727792020-04-10 01:41:13 -04003747 const rv = this.getRowsText(selection.startRow.rowIndex,
3748 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003749 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003750};
3751
rginda4bba5e12012-06-20 16:15:30 -07003752/**
3753 * Copy the current selection to the system clipboard, then clear it after a
3754 * short delay.
3755 */
3756hterm.Terminal.prototype.copySelectionToClipboard = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003757 const text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003758 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003759 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003760 }
rginda4bba5e12012-06-20 16:15:30 -07003761};
3762
Joel Hockey0f933582019-08-27 18:01:51 -07003763/**
3764 * Show overlay with current terminal size.
3765 */
rgindaf0090c92012-02-10 14:58:52 -08003766hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003767 if (this.prefs_.get('enable-resize-status')) {
Jason Lin3d825782020-05-12 11:02:48 +10003768 this.showOverlay(`${this.screenSize.width} x ${this.screenSize.height}`);
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003769 }
rgindaf0090c92012-02-10 14:58:52 -08003770};
3771
rginda87b86462011-12-14 13:48:03 -08003772/**
3773 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3774 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003775 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003776 */
3777hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003778 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003779 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003780 }
rginda87b86462011-12-14 13:48:03 -08003781
Mike Frysinger225c99d2019-10-20 14:02:37 -06003782 this.pauseCursorBlink_();
3783
Mike Frysinger79669762018-12-30 20:51:10 -05003784 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003785};
3786
3787/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003788 * Open the selected url.
3789 */
3790hterm.Terminal.prototype.openSelectedUrl_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04003791 let str = this.getSelectionText();
Mike Frysinger70b94692017-01-26 18:57:50 -10003792
3793 // If there is no selection, try and expand wherever they clicked.
3794 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003795 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003796 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003797
3798 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003799 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003800 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003801 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003802 }
3803
3804 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003805 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003806 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003807 }
Mike Frysinger43472622017-06-26 18:11:07 -04003808
3809 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003810 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003811 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3812 // We have to whitelist a few protocols that lack authorities and thus
3813 // never use the //. Like mailto.
3814 switch (str.split(':', 1)[0]) {
3815 case 'mailto':
3816 break;
3817 default:
3818 str = 'http://' + str;
3819 break;
3820 }
3821 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003822
Mike Frysinger720fa832017-10-23 01:15:52 -04003823 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003824};
Mike Frysinger70b94692017-01-26 18:57:50 -10003825
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003826/**
3827 * Manage the automatic mouse hiding behavior while typing.
3828 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003829 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003830 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003831hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003832 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3833 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003834 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003835 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003836 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003837
3838 this.mouseHideWhileTyping_ = !!v;
3839};
3840
3841/**
3842 * Handler for monitoring user keyboard activity.
3843 *
3844 * This isn't for processing the keystrokes directly, but for updating any
3845 * state that might toggle based on the user using the keyboard at all.
3846 *
Joel Hockey0f933582019-08-27 18:01:51 -07003847 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003848 */
3849hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3850 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003851 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003852 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003853 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003854};
Mike Frysinger70b94692017-01-26 18:57:50 -10003855
3856/**
rgindad5613292012-06-19 15:40:37 -07003857 * Add the terminalRow and terminalColumn properties to mouse events and
3858 * then forward on to onMouse().
3859 *
3860 * The terminalRow and terminalColumn properties contain the (row, column)
3861 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003862 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003863 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003864 */
3865hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003866 if (e.processedByTerminalHandler_) {
3867 // We register our event handlers on the document, as well as the cursor
3868 // and the scroll blocker. Mouse events that occur on the cursor or
3869 // scroll blocker will also appear on the document, but we don't want to
3870 // process them twice.
3871 //
3872 // We can't just prevent bubbling because that has other side effects, so
3873 // we decorate the event object with this property instead.
3874 return;
3875 }
3876
Mike Frysinger468966c2018-08-28 13:48:51 -04003877 // Consume navigation events. Button 3 is usually "browser back" and
3878 // button 4 is "browser forward" which we don't want to happen.
3879 if (e.button > 2) {
3880 e.preventDefault();
3881 // We don't return so click events can be passed to the remote below.
3882 }
3883
Mike Frysingerdc727792020-04-10 01:41:13 -04003884 const reportMouseEvents = (!this.defeatMouseReports_ &&
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003885 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3886
rgindafaa74742012-08-21 13:34:03 -07003887 e.processedByTerminalHandler_ = true;
3888
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003889 // Handle auto hiding of mouse cursor while typing.
3890 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3891 // Make sure the mouse cursor is visible.
3892 this.syncMouseStyle();
3893 // This debounce isn't perfect, but should work well enough for such a
3894 // simple implementation. If the user moved the mouse, we enabled this
3895 // debounce, and then moved the mouse just before the timeout, we wouldn't
3896 // debounce that later movement.
3897 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3898 }
3899
Robert Gindaeda48db2014-07-17 09:25:30 -07003900 // One based row/column stored on the mouse event.
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003901 const padding = this.scrollPort_.screenPaddingSize;
Joel Hockeyd4fca732019-09-20 16:57:03 -07003902 e.terminalRow = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003903 (e.clientY - this.scrollPort_.visibleRowTopMargin - padding) /
Joel Hockeyd4fca732019-09-20 16:57:03 -07003904 this.scrollPort_.characterSize.height) + 1;
3905 e.terminalColumn = Math.floor(
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003906 (e.clientX - padding) / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003907
Joel Hockeyaaabfba2020-05-01 16:10:28 -07003908 // Clamp row and column.
3909 e.terminalRow = lib.f.clamp(e.terminalRow, 1, this.screenSize.height);
3910 e.terminalColumn = lib.f.clamp(e.terminalColumn, 1, this.screenSize.width);
3911
3912 // Ignore mousedown in the scrollbar area.
3913 if (e.type == 'mousedown' && e.clientX >= this.scrollPort_.getScrollbarX()) {
rginda4bba5e12012-06-20 16:15:30 -07003914 return;
3915 }
3916
Joel Hockey3babf302020-04-22 15:00:06 -07003917 if (this.options_.cursorVisible && !reportMouseEvents &&
3918 !this.cursorOffScreen_) {
Robert Gindab837c052014-08-11 11:17:51 -07003919 // If the cursor is visible and we're not sending mouse events to the
3920 // host app, then we want to hide the terminal cursor when the mouse
3921 // cursor is over top. This keeps the terminal cursor from interfering
3922 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003923 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3924 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3925 this.cursorNode_.style.display = 'none';
3926 } else if (this.cursorNode_.style.display == 'none') {
3927 this.cursorNode_.style.display = '';
3928 }
3929 }
rgindad5613292012-06-19 15:40:37 -07003930
Robert Ginda928cf632014-03-05 15:07:41 -08003931 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003932 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003933
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003934 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003935 // If VT mouse reporting is disabled, or has been defeated with
3936 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003937 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003938 this.setSelectionEnabled(true);
3939 } else {
3940 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003941 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003942 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003943 this.setSelectionEnabled(false);
3944 e.preventDefault();
3945 }
3946 }
3947
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003948 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003949 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003950 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003951 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003952 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003953 }
rgindad5613292012-06-19 15:40:37 -07003954 }
3955
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003956 // Handle clicks to open links automatically.
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003957 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysingerda2e84f2020-06-01 17:53:21 -04003958 // Ignore links created using OSC-8 as those will open by themselves, and
3959 // the visible text is most likely not the URI they want anyways.
3960 if (e.target.className === 'uri-node') {
3961 return;
3962 }
3963
Mike Frysinger70b94692017-01-26 18:57:50 -10003964 // Debounce this event with the dblclick event. If you try to doubleclick
3965 // a URL to open it, Chrome will fire click then dblclick, but we won't
3966 // have expanded the selection text at the first click event.
3967 clearTimeout(this.timeouts_.openUrl);
3968 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3969 500);
3970 return;
3971 }
3972
Mike Frysinger847577f2017-05-23 23:25:57 -04003973 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003974 if (e.ctrlKey && e.button == 2 /* right button */) {
3975 e.preventDefault();
3976 this.contextMenu.show(e, this);
3977 } else if (e.button == this.mousePasteButton ||
3978 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003979 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003980 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003981 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003982 }
3983 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003984
Mike Frysinger2edd3612017-05-24 00:54:39 -04003985 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003986 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003987 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003988 }
3989
3990 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3991 this.scrollBlockerNode_.engaged) {
3992 // Disengage the scroll-blocker after one of these events.
3993 this.scrollBlockerNode_.engaged = false;
3994 this.scrollBlockerNode_.style.top = '-99px';
3995 }
3996
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003997 // Emulate arrow key presses via scroll wheel events.
3998 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3999 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04004000 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07004001 const delta =
4002 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04004003
Mike Frysinger321063c2018-08-29 15:33:14 -04004004 // Helper to turn a wheel event delta into a series of key presses.
4005 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
4006 if (distance == 0) {
4007 return '';
4008 }
4009
4010 // Convert the scroll distance into a number of rows/cols.
4011 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
4012 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
4013 return data.repeat(cells);
4014 };
4015
4016 // The order between up/down and left/right doesn't really matter.
4017 this.io.sendString(
4018 // Up/down arrow keys.
4019 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
4020 'A', 'B') +
4021 // Left/right arrow keys.
4022 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
Jason Lin9a627462020-04-20 18:03:53 +10004023 'C', 'D'),
Mike Frysinger321063c2018-08-29 15:33:14 -04004024 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04004025
4026 e.preventDefault();
4027 }
4028 }
Robert Ginda928cf632014-03-05 15:07:41 -08004029 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004030 if (!this.scrollBlockerNode_.engaged) {
4031 if (e.type == 'mousedown') {
4032 // Move the scroll-blocker into place if we want to keep the scrollport
4033 // from scrolling.
4034 this.scrollBlockerNode_.engaged = true;
4035 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
4036 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
4037 } else if (e.type == 'mousemove') {
4038 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
4039 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07004040 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08004041 e.preventDefault();
4042 }
4043 }
Robert Ginda928cf632014-03-05 15:07:41 -08004044
4045 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07004046 }
4047
Robert Ginda928cf632014-03-05 15:07:41 -08004048 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
4049 // Restore this on mouseup in case it was temporarily defeated with a
4050 // alt-mousedown. Only do this when the selection is empty so that
4051 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07004052 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08004053 }
rgindad5613292012-06-19 15:40:37 -07004054};
4055
4056/**
4057 * Clients should override this if they care to know about mouse events.
4058 *
4059 * The event parameter will be a normal DOM mouse click event with additional
4060 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05004061 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07004062 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07004063 */
4064hterm.Terminal.prototype.onMouse = function(e) { };
4065
4066/**
rginda8e92a692012-05-20 19:37:20 -07004067 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05004068 *
4069 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07004070 */
Rob Spies06533ba2014-04-24 11:20:37 -07004071hterm.Terminal.prototype.onFocusChange_ = function(focused) {
4072 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07004073 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004074
Mike Frysingerbdb34802020-04-07 03:47:32 -04004075 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04004076 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04004077 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04004078
Mike Frysingerbdb34802020-04-07 03:47:32 -04004079 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04004080 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004081 }
rginda8e92a692012-05-20 19:37:20 -07004082};
4083
4084/**
rginda8ba33642011-12-14 12:31:31 -08004085 * React when the ScrollPort is scrolled.
4086 */
4087hterm.Terminal.prototype.onScroll_ = function() {
4088 this.scheduleSyncCursorPosition_();
4089};
4090
4091/**
rginda9846e2f2012-01-27 13:53:33 -08004092 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004093 *
Joel Hockeye25ce432019-09-25 19:12:28 -07004094 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08004095 */
4096hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11004097 this.onPasteData_(e.text);
4098};
4099
4100/**
4101 * Handle pasted data.
4102 *
4103 * @param {string} data The pasted data.
4104 */
4105hterm.Terminal.prototype.onPasteData_ = function(data) {
4106 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07004107 if (this.options_.bracketedPaste) {
4108 // We strip out most escape sequences as they can cause issues (like
4109 // inserting an \x1b[201~ midstream). We pass through whitespace
4110 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4111 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004112 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004113 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4114 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4115 }
Robert Gindaa063b202014-07-21 11:08:25 -07004116
4117 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004118};
4119
4120/**
rgindaa09e7332012-08-17 12:49:51 -07004121 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004122 *
Joel Hockey0f933582019-08-27 18:01:51 -07004123 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004124 */
4125hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004126 if (!this.useDefaultWindowCopy) {
4127 e.preventDefault();
4128 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4129 }
rgindaa09e7332012-08-17 12:49:51 -07004130};
4131
4132/**
rginda8ba33642011-12-14 12:31:31 -08004133 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004134 *
4135 * Note: This function should not directly contain code that alters the internal
4136 * state of the terminal. That kind of code belongs in realizeWidth or
4137 * realizeHeight, so that it can be executed synchronously in the case of a
4138 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004139 */
4140hterm.Terminal.prototype.onResize_ = function() {
Mike Frysingerdc727792020-04-10 01:41:13 -04004141 const columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
4142 this.scrollPort_.characterSize.width) || 0;
4143 const rowCount = lib.f.smartFloorDivide(
4144 this.scrollPort_.getScreenHeight(),
4145 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004146
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004147 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004148 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004149 // gets removed from the document or during the initial load, and we can't
4150 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004151 // This can also happen if called before the scrollPort calculates the
4152 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004153 return;
4154 }
4155
Mike Frysingerdc727792020-04-10 01:41:13 -04004156 const isNewSize = (columnCount != this.screenSize.width ||
4157 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004158 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004159
4160 // We do this even if the size didn't change, just to be sure everything is
4161 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004162 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004163 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004164
Mike Frysingerbdb34802020-04-07 03:47:32 -04004165 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004166 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004167 }
rgindaa8ba17d2012-08-15 14:41:10 -07004168
Robert Gindafb1be6a2013-12-11 11:56:22 -08004169 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004170 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004171
4172 if (wasScrolledEnd) {
4173 this.scrollEnd();
4174 }
rginda8ba33642011-12-14 12:31:31 -08004175};
4176
4177/**
4178 * Service the cursor blink timeout.
4179 */
4180hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004181 if (!this.options_.cursorBlink) {
4182 delete this.timeouts_.cursorBlink;
4183 return;
4184 }
4185
Robert Ginda830583c2013-08-07 13:20:46 -07004186 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004187 this.cursorNode_.style.opacity == '0' ||
4188 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004189 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004190 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4191 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004192 } else {
rginda87b86462011-12-14 13:48:03 -08004193 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004194 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4195 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004196 }
4197};
David Reveman8f552492012-03-28 12:18:41 -04004198
4199/**
4200 * Set the scrollbar-visible mode bit.
4201 *
4202 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4203 * Otherwise it will not.
4204 *
4205 * Defaults to on.
4206 *
4207 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4208 */
4209hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4210 this.scrollPort_.setScrollbarVisible(state);
4211};
Michael Kelly485ecd12014-06-09 11:41:56 -04004212
4213/**
Rob Spies49039e52014-12-17 13:40:04 -08004214 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004215 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004216 *
4217 * Defaults to 1.
4218 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004219 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004220 */
4221hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4222 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4223};
4224
4225/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004226 * Close all web notifications created by terminal bells.
4227 */
4228hterm.Terminal.prototype.closeBellNotifications_ = function() {
4229 this.bellNotificationList_.forEach(function(n) {
4230 n.close();
4231 });
4232 this.bellNotificationList_.length = 0;
4233};
Raymes Khourye5d48982018-08-02 09:08:32 +10004234
4235/**
4236 * Syncs the cursor position when the scrollport gains focus.
4237 */
4238hterm.Terminal.prototype.onScrollportFocus_ = function() {
4239 // If the cursor is offscreen we set selection to the last row on the screen.
4240 const topRowIndex = this.scrollPort_.getTopRowIndex();
4241 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4242 const selection = this.document_.getSelection();
4243 if (!this.syncCursorPosition_() && selection) {
4244 selection.collapse(this.getRowNode(bottomRowIndex));
4245 }
4246};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004247
4248/**
4249 * Clients can override this if they want to provide an options page.
4250 */
4251hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4252
4253
4254/**
4255 * Called when user selects to open the options page.
4256 */
4257hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4258 this.onOpenOptionsPage();
4259};