blob: a5d6de901e61254a37f63e2068964d235834ae40 [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 Hockey0f933582019-08-27 18:01:51 -070024 * @param {string=} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080025 * provided, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
rginda8ba33642011-12-14 12:31:31 -080027 */
Robert Ginda57f03b42012-09-13 11:02:48 -070028hterm.Terminal = function(opt_profileId) {
29 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080030
rginda8ba33642011-12-14 12:31:31 -080031 // Two screen instances.
32 this.primaryScreen_ = new hterm.Screen();
33 this.alternateScreen_ = new hterm.Screen();
34
35 // The "current" screen.
36 this.screen_ = this.primaryScreen_;
37
rginda8ba33642011-12-14 12:31:31 -080038 // The local notion of the screen size. ScreenBuffers also have a size which
39 // indicates their present size. During size changes, the two may disagree.
40 // Also, the inactive screen's size is not altered until it is made the active
41 // screen.
42 this.screenSize = new hterm.Size(0, 0);
43
rginda8ba33642011-12-14 12:31:31 -080044 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080045 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080046 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
47 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080048 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100049 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070050 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080051
rginda87b86462011-12-14 13:48:03 -080052 // The div that contains this terminal.
53 this.div_ = null;
54
rgindac9bc5502012-01-18 11:48:44 -080055 // The document that contains the scrollPort. Defaulted to the global
56 // document here so that the terminal is functional even if it hasn't been
57 // inserted into a document yet, but re-set in decorate().
58 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080059
rginda8ba33642011-12-14 12:31:31 -080060 // The rows that have scrolled off screen and are no longer addressable.
61 this.scrollbackRows_ = [];
62
rgindac9bc5502012-01-18 11:48:44 -080063 // Saved tab stops.
64 this.tabStops_ = [];
65
David Benjamin66e954d2012-05-05 21:08:12 -040066 // Keep track of whether default tab stops have been erased; after a TBC
67 // clears all tab stops, defaults aren't restored on resize until a reset.
68 this.defaultTabStops = true;
69
rginda8ba33642011-12-14 12:31:31 -080070 // The VT's notion of the top and bottom rows. Used during some VT
71 // cursor positioning and scrolling commands.
72 this.vtScrollTop_ = null;
73 this.vtScrollBottom_ = null;
74
75 // The DIV element for the visible cursor.
76 this.cursorNode_ = null;
77
Robert Ginda830583c2013-08-07 13:20:46 -070078 // The current cursor shape of the terminal.
79 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
80
Robert Gindaea2183e2014-07-17 09:51:51 -070081 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
82 this.cursorBlinkCycle_ = [100, 100];
83
84 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
85 // cursor on/off servicing.
86 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
87
rginda9f5222b2012-03-05 11:53:28 -080088 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070089 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070090 this.backgroundColor_ = null;
91 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070092 this.scrollOnOutput_ = null;
93 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040094 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080095
Robert Ginda6aec7eb2015-06-16 10:31:30 -070096 // True if we should override mouse event reporting to allow local selection.
97 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -080098
Mike Frysinger02ded6d2018-06-21 14:25:20 -040099 // Whether to auto hide the mouse cursor when typing.
100 this.setAutomaticMouseHiding();
101 // Timer to keep mouse visible while it's being used.
102 this.mouseHideDelay_ = null;
103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400106 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800107 this.bellAudio_.setAttribute('preload', 'auto');
108
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000109 // The AccessibilityReader object for announcing command output.
110 this.accessibilityReader_ = null;
111
Mike Frysingercc114512017-09-11 21:39:17 -0400112 // The context menu object.
113 this.contextMenu = new hterm.ContextMenu();
114
Michael Kelly485ecd12014-06-09 11:41:56 -0400115 // All terminal bell notifications that have been generated (not necessarily
116 // shown).
117 this.bellNotificationList_ = [];
118
119 // Whether we have permission to display notifications.
120 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400121
rginda6d397402012-01-17 10:58:29 -0800122 // Cursor position and attributes saved with DECSC.
123 this.savedOptions_ = {};
124
rginda8ba33642011-12-14 12:31:31 -0800125 // The current mode bits for the terminal.
126 this.options_ = new hterm.Options();
127
128 // Timeouts we might need to clear.
129 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800130
131 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800132 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800133
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800134 this.saveCursorAndState(true);
135
Zhu Qunying30d40712017-03-14 16:27:00 -0700136 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800137 this.keyboard = new hterm.Keyboard(this);
138
rginda87b86462011-12-14 13:48:03 -0800139 // General IO interface that can be given to third parties without exposing
140 // the entire terminal object.
141 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800142
rgindad5613292012-06-19 15:40:37 -0700143 // True if mouse-click-drag should scroll the terminal.
144 this.enableMouseDragScroll = true;
145
Robert Ginda57f03b42012-09-13 11:02:48 -0700146 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400147 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700148 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700149
Zhu Qunying30d40712017-03-14 16:27:00 -0700150 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700151 this.useDefaultWindowCopy = false;
152
153 this.clearSelectionAfterCopy = true;
154
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400155 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800156 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700157
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400158 // Whether we allow images to be shown.
159 this.allowImagesInline = null;
160
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400161 this.reportFocus = false;
162
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500164 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800165};
166
167/**
Robert Ginda830583c2013-08-07 13:20:46 -0700168 * Possible cursor shapes.
169 */
170hterm.Terminal.cursorShape = {
171 BLOCK: 'BLOCK',
172 BEAM: 'BEAM',
173 UNDERLINE: 'UNDERLINE'
174};
175
176/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700177 * Clients should override this to be notified when the terminal is ready
178 * for use.
179 *
180 * The terminal initialization is asynchronous, and shouldn't be used before
181 * this method is called.
182 */
183hterm.Terminal.prototype.onTerminalReady = function() { };
184
185/**
rginda35c456b2012-02-09 17:29:05 -0800186 * Default tab with of 8 to match xterm.
187 */
188hterm.Terminal.prototype.tabWidth = 8;
189
190/**
rginda9f5222b2012-03-05 11:53:28 -0800191 * Select a preference profile.
192 *
193 * This will load the terminal preferences for the given profile name and
194 * associate subsequent preference changes with the new preference profile.
195 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500196 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800197 * characters will be removed from the name.
Joel Hockey0f933582019-08-27 18:01:51 -0700198 * @param {function()=} opt_callback Optional callback to invoke when the
199 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800200 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700201hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
202 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800205
Robert Ginda57f03b42012-09-13 11:02:48 -0700206 if (this.prefs_)
207 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800208
Robert Ginda57f03b42012-09-13 11:02:48 -0700209 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
210 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800211 'alt-gr-mode': function(v) {
212 if (v == null) {
213 if (navigator.language.toLowerCase() == 'en-us') {
214 v = 'none';
215 } else {
216 v = 'right-alt';
217 }
218 } else if (typeof v == 'string') {
219 v = v.toLowerCase();
220 } else {
221 v = 'none';
222 }
223
224 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
225 v = 'none';
226
227 terminal.keyboard.altGrMode = v;
228 },
229
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700230 'alt-backspace-is-meta-backspace': function(v) {
231 terminal.keyboard.altBackspaceIsMetaBackspace = v;
232 },
233
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 'alt-is-meta': function(v) {
235 terminal.keyboard.altIsMeta = v;
236 },
237
238 'alt-sends-what': function(v) {
239 if (!/^(escape|8-bit|browser-key)$/.test(v))
240 v = 'escape';
241
242 terminal.keyboard.altSendsWhat = v;
243 },
244
245 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800246 var ary = v.match(/^lib-resource:(\S+)/);
247 if (ary) {
248 terminal.bellAudio_.setAttribute('src',
249 lib.resource.getDataUrl(ary[1]));
250 } else {
251 terminal.bellAudio_.setAttribute('src', v);
252 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700253 },
254
Michael Kelly485ecd12014-06-09 11:41:56 -0400255 'desktop-notification-bell': function(v) {
256 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700257 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400258 Notification.permission === 'granted';
259 if (!terminal.desktopNotificationBell_) {
260 // Note: We don't call Notification.requestPermission here because
261 // Chrome requires the call be the result of a user action (such as an
262 // onclick handler), and pref listeners are run asynchronously.
263 //
264 // A way of working around this would be to display a dialog in the
265 // terminal with a "click-to-request-permission" button.
266 console.warn('desktop-notification-bell is true but we do not have ' +
267 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400268 }
269 } else {
270 terminal.desktopNotificationBell_ = false;
271 }
272 },
273
Robert Ginda57f03b42012-09-13 11:02:48 -0700274 'background-color': function(v) {
275 terminal.setBackgroundColor(v);
276 },
277
278 'background-image': function(v) {
279 terminal.scrollPort_.setBackgroundImage(v);
280 },
281
282 'background-size': function(v) {
283 terminal.scrollPort_.setBackgroundSize(v);
284 },
285
286 'background-position': function(v) {
287 terminal.scrollPort_.setBackgroundPosition(v);
288 },
289
290 'backspace-sends-backspace': function(v) {
291 terminal.keyboard.backspaceSendsBackspace = v;
292 },
293
Brad Town18654b62015-03-12 00:27:45 -0700294 'character-map-overrides': function(v) {
295 if (!(v == null || v instanceof Object)) {
296 console.warn('Preference character-map-modifications is not an ' +
297 'object: ' + v);
298 return;
299 }
300
Mike Frysinger095d4062017-06-14 00:29:48 -0700301 terminal.vt.characterMaps.reset();
302 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700303 },
304
Robert Ginda57f03b42012-09-13 11:02:48 -0700305 'cursor-blink': function(v) {
306 terminal.setCursorBlink(!!v);
307 },
308
Joel Hockey9d10ba12019-05-28 01:25:02 -0700309 'cursor-shape': function(v) {
310 terminal.setCursorShape(v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700384 'paste-on-drop': function(v) {
385 terminal.scrollPort_.setPasteOnDrop(v);
386 },
387
Masaya Suzuki273aa982014-05-31 07:25:55 +0900388 'east-asian-ambiguous-as-two-column': function(v) {
389 lib.wc.regardCjkAmbiguous = v;
390 },
391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-8-bit-control': function(v) {
393 terminal.vt.enable8BitControl = !!v;
394 },
rginda30f20f62012-04-05 16:36:19 -0700395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-bold': function(v) {
397 terminal.syncBoldSafeState();
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3e278d72014-03-25 13:18:51 -0700400 'enable-bold-as-bright': function(v) {
401 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
402 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
403 },
404
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400405 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500406 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400407 },
408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'enable-clipboard-write': function(v) {
410 terminal.vt.enableClipboardWrite = !!v;
411 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400412
Robert Ginda3755e752013-05-31 13:34:09 -0700413 'enable-dec12': function(v) {
414 terminal.vt.enableDec12 = !!v;
415 },
416
Mike Frysinger38f267d2018-09-07 02:50:59 -0400417 'enable-csi-j-3': function(v) {
418 terminal.vt.enableCsiJ3 = !!v;
419 },
420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 'font-family': function(v) {
422 terminal.syncFontFamily();
423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500426 v = parseInt(v);
427 if (v <= 0) {
428 console.error(`Invalid font size: ${v}`);
429 return;
430 }
431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 terminal.setFontSize(v);
433 },
rginda9875d902012-08-20 16:21:57 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'font-smoothing': function(v) {
436 terminal.syncFontFamily();
437 },
rgindade84e382012-04-20 15:39:31 -0700438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'foreground-color': function(v) {
440 terminal.setForegroundColor(v);
441 },
rginda30f20f62012-04-05 16:36:19 -0700442
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400443 'hide-mouse-while-typing': function(v) {
444 terminal.setAutomaticMouseHiding(v);
445 },
446
Robert Ginda57f03b42012-09-13 11:02:48 -0700447 'home-keys-scroll': function(v) {
448 terminal.keyboard.homeKeysScroll = v;
449 },
rginda4bba5e12012-06-20 16:15:30 -0700450
Robert Gindaa8165692015-06-15 14:46:31 -0700451 'keybindings': function(v) {
452 terminal.keyboard.bindings.clear();
453
454 if (!v)
455 return;
456
457 if (!(v instanceof Object)) {
458 console.error('Error in keybindings preference: Expected object');
459 return;
460 }
461
462 try {
463 terminal.keyboard.bindings.addBindings(v);
464 } catch (ex) {
465 console.error('Error in keybindings preference: ' + ex);
466 }
467 },
468
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700469 'media-keys-are-fkeys': function(v) {
470 terminal.keyboard.mediaKeysAreFKeys = v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'meta-sends-escape': function(v) {
474 terminal.keyboard.metaSendsEscape = v;
475 },
rginda30f20f62012-04-05 16:36:19 -0700476
Mike Frysinger847577f2017-05-23 23:25:57 -0400477 'mouse-right-click-paste': function(v) {
478 terminal.mouseRightClickPaste = v;
479 },
480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'mouse-paste-button': function(v) {
482 terminal.syncMousePasteButton();
483 },
rgindaa8ba17d2012-08-15 14:41:10 -0700484
Robert Gindae76aa9f2014-03-14 12:29:12 -0700485 'page-keys-scroll': function(v) {
486 terminal.keyboard.pageKeysScroll = v;
487 },
488
Robert Ginda40932892012-12-10 17:26:40 -0800489 'pass-alt-number': function(v) {
490 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800491 // Let Alt-1..9 pass to the browser (to control tab switching) on
492 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500493 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800494 }
495
496 terminal.passAltNumber = v;
497 },
498
499 'pass-ctrl-number': function(v) {
500 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800501 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
502 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500503 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800504 }
505
506 terminal.passCtrlNumber = v;
507 },
508
509 'pass-meta-number': function(v) {
510 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800511 // Let Meta-1..9 pass to the browser (to control tab switching) on
512 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500513 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800514 }
515
516 terminal.passMetaNumber = v;
517 },
518
Marius Schilder77857b32014-05-14 16:21:26 -0700519 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700520 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700521 },
522
Robert Ginda8cb7d902013-06-20 14:37:18 -0700523 'receive-encoding': function(v) {
524 if (!(/^(utf-8|raw)$/).test(v)) {
525 console.warn('Invalid value for "receive-encoding": ' + v);
526 v = 'utf-8';
527 }
528
529 terminal.vt.characterEncoding = v;
530 },
531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scroll-on-keystroke': function(v) {
533 terminal.scrollOnKeystroke_ = v;
534 },
rginda9f5222b2012-03-05 11:53:28 -0800535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'scroll-on-output': function(v) {
537 terminal.scrollOnOutput_ = v;
538 },
rginda30f20f62012-04-05 16:36:19 -0700539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'scrollbar-visible': function(v) {
541 terminal.setScrollbarVisible(v);
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400544 'scroll-wheel-may-send-arrow-keys': function(v) {
545 terminal.scrollWheelArrowKeys_ = v;
546 },
547
Rob Spies49039e52014-12-17 13:40:04 -0800548 'scroll-wheel-move-multiplier': function(v) {
549 terminal.setScrollWheelMoveMultipler(v);
550 },
551
Robert Ginda57f03b42012-09-13 11:02:48 -0700552 'shift-insert-paste': function(v) {
553 terminal.keyboard.shiftInsertPaste = v;
554 },
rginda9f5222b2012-03-05 11:53:28 -0800555
Mike Frysingera7768922017-07-28 15:00:12 -0400556 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400557 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400582
583 'allow-images-inline': function(v) {
584 terminal.allowImagesInline = v;
585 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 });
rginda30f20f62012-04-05 16:36:19 -0700587
Robert Ginda57f03b42012-09-13 11:02:48 -0700588 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800589 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700590
591 if (opt_callback)
592 opt_callback();
593 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800594};
595
Rob Spies56953412014-04-28 14:09:47 -0700596/**
597 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500598 *
Joel Hockey0f933582019-08-27 18:01:51 -0700599 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700600 */
601hterm.Terminal.prototype.getPrefs = function() {
602 return this.prefs_;
603};
604
Robert Gindaa063b202014-07-21 11:08:25 -0700605/**
606 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500607 *
608 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700609 */
610hterm.Terminal.prototype.setBracketedPaste = function(state) {
611 this.options_.bracketedPaste = state;
612};
Rob Spies56953412014-04-28 14:09:47 -0700613
rginda8e92a692012-05-20 19:37:20 -0700614/**
615 * Set the color for the cursor.
616 *
617 * If you want this setting to persist, set it through prefs_, rather than
618 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500619 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500620 * @param {string=} color The color to set. If not defined, we reset to the
621 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700622 */
623hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500624 if (color === undefined)
625 color = this.prefs_.get('cursor-color');
626
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400627 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700628};
629
630/**
631 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500632 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700633 */
634hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400635 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700636};
637
638/**
rgindad5613292012-06-19 15:40:37 -0700639 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500640 *
641 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700642 */
643hterm.Terminal.prototype.setSelectionEnabled = function(state) {
644 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700645};
646
647/**
rginda8e92a692012-05-20 19:37:20 -0700648 * Set the background color.
649 *
650 * If you want this setting to persist, set it through prefs_, rather than
651 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500652 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500653 * @param {string=} color The color to set. If not defined, we reset to the
654 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700655 */
656hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500657 if (color === undefined)
658 color = this.prefs_.get('background-color');
659
rgindacbbd7482012-06-13 15:06:16 -0700660 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700661 this.primaryScreen_.textAttributes.setDefaults(
662 this.foregroundColor_, this.backgroundColor_);
663 this.alternateScreen_.textAttributes.setDefaults(
664 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700665 this.scrollPort_.setBackgroundColor(color);
666};
667
rginda9f5222b2012-03-05 11:53:28 -0800668/**
669 * Return the current terminal background color.
670 *
671 * Intended for use by other classes, so we don't have to expose the entire
672 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500673 *
674 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800675 */
676hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700677 return this.backgroundColor_;
678};
679
680/**
681 * Set the foreground color.
682 *
683 * If you want this setting to persist, set it through prefs_, rather than
684 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500685 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500686 * @param {string=} color The color to set. If not defined, we reset to the
687 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700688 */
689hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500690 if (color === undefined)
691 color = this.prefs_.get('foreground-color');
692
rgindacbbd7482012-06-13 15:06:16 -0700693 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700694 this.primaryScreen_.textAttributes.setDefaults(
695 this.foregroundColor_, this.backgroundColor_);
696 this.alternateScreen_.textAttributes.setDefaults(
697 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700698 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800699};
700
701/**
702 * Return the current terminal foreground color.
703 *
704 * Intended for use by other classes, so we don't have to expose the entire
705 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500706 *
707 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800708 */
709hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700710 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800711};
712
713/**
rginda87b86462011-12-14 13:48:03 -0800714 * Create a new instance of a terminal command and run it with a given
715 * argument string.
716 *
Joel Hockey0f933582019-08-27 18:01:51 -0700717 * @param {function()} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700718 * @param {string} commandName The command to run for this terminal.
719 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800720 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700721hterm.Terminal.prototype.runCommandClass = function(
722 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700723 var environment = this.prefs_.get('environment');
724 if (typeof environment != 'object' || environment == null)
725 environment = {};
726
rginda87b86462011-12-14 13:48:03 -0800727 var self = this;
728 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700729 {
730 commandName: commandName,
731 args: args,
rginda87b86462011-12-14 13:48:03 -0800732 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700733 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800734 onExit: function(code) {
735 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800736 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700737 if (self.prefs_.get('close-on-exit'))
738 window.close();
rginda87b86462011-12-14 13:48:03 -0800739 }
740 });
741
rgindafeaf3142012-01-31 15:14:20 -0800742 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800743 this.command.run();
744};
745
746/**
rgindafeaf3142012-01-31 15:14:20 -0800747 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500748 *
749 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800750 */
751hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700752 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800753};
754
755/**
756 * Install the keyboard handler for this terminal.
757 *
758 * This will prevent the browser from seeing any keystrokes sent to the
759 * terminal.
760 */
761hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700762 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400763};
rgindafeaf3142012-01-31 15:14:20 -0800764
765/**
766 * Uninstall the keyboard handler for this terminal.
767 */
768hterm.Terminal.prototype.uninstallKeyboard = function() {
769 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400770};
rgindafeaf3142012-01-31 15:14:20 -0800771
772/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400773 * Set a CSS variable.
774 *
775 * Normally this is used to set variables in the hterm namespace.
776 *
777 * @param {string} name The variable to set.
778 * @param {string} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700779 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400780 */
781hterm.Terminal.prototype.setCssVar = function(name, value,
782 opt_prefix='--hterm-') {
783 this.document_.documentElement.style.setProperty(
784 `${opt_prefix}${name}`, value);
785};
786
787/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500788 * Get a CSS variable.
789 *
790 * Normally this is used to get variables in the hterm namespace.
791 *
792 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700793 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500794 * @return {string} The current setting for this variable.
795 */
796hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
797 return this.document_.documentElement.style.getPropertyValue(
798 `${opt_prefix}${name}`);
799};
800
801/**
rginda35c456b2012-02-09 17:29:05 -0800802 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800803 *
804 * Call setFontSize(0) to reset to the default font size.
805 *
806 * This function does not modify the font-size preference.
807 *
808 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800809 */
810hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500811 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800812 px = this.prefs_.get('font-size');
813
rginda35c456b2012-02-09 17:29:05 -0800814 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400815 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
816 this.setCssVar('charsize-height',
817 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800818};
819
820/**
821 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500822 *
823 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800824 */
825hterm.Terminal.prototype.getFontSize = function() {
826 return this.scrollPort_.getFontSize();
827};
828
829/**
rginda8e92a692012-05-20 19:37:20 -0700830 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500831 *
832 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700833 */
834hterm.Terminal.prototype.getFontFamily = function() {
835 return this.scrollPort_.getFontFamily();
836};
837
838/**
rginda35c456b2012-02-09 17:29:05 -0800839 * Set the CSS "font-family" for this terminal.
840 */
rginda9f5222b2012-03-05 11:53:28 -0800841hterm.Terminal.prototype.syncFontFamily = function() {
842 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
843 this.prefs_.get('font-smoothing'));
844 this.syncBoldSafeState();
845};
846
rginda4bba5e12012-06-20 16:15:30 -0700847/**
848 * Set this.mousePasteButton based on the mouse-paste-button pref,
849 * autodetecting if necessary.
850 */
851hterm.Terminal.prototype.syncMousePasteButton = function() {
852 var button = this.prefs_.get('mouse-paste-button');
853 if (typeof button == 'number') {
854 this.mousePasteButton = button;
855 return;
856 }
857
Mike Frysingeree81a002017-12-12 16:14:53 -0500858 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400859 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700860 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400861 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700862 }
863};
864
865/**
866 * Enable or disable bold based on the enable-bold pref, autodetecting if
867 * necessary.
868 */
rginda9f5222b2012-03-05 11:53:28 -0800869hterm.Terminal.prototype.syncBoldSafeState = function() {
870 var enableBold = this.prefs_.get('enable-bold');
871 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700872 this.primaryScreen_.textAttributes.enableBold = enableBold;
873 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800874 return;
875 }
876
rgindaf7521392012-02-28 17:20:34 -0800877 var normalSize = this.scrollPort_.measureCharacterSize();
878 var boldSize = this.scrollPort_.measureCharacterSize('bold');
879
880 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800881 if (!isBoldSafe) {
882 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700883 'from normal. Font family is: ' +
884 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800885 }
rginda9f5222b2012-03-05 11:53:28 -0800886
Robert Gindaed016262012-10-26 16:27:09 -0700887 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
888 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800889};
890
891/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500892 * Control text blinking behavior.
893 *
894 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400895 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500896hterm.Terminal.prototype.setTextBlink = function(state) {
897 if (state === undefined)
898 state = this.prefs_.get('enable-blink');
899 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400900};
901
902/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400903 * Set the mouse cursor style based on the current terminal mode.
904 */
905hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400906 this.setCssVar('mouse-cursor-style',
907 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
908 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500909 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400910};
911
912/**
rginda87b86462011-12-14 13:48:03 -0800913 * Return a copy of the current cursor position.
914 *
Joel Hockey0f933582019-08-27 18:01:51 -0700915 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800916 */
917hterm.Terminal.prototype.saveCursor = function() {
918 return this.screen_.cursorPosition.clone();
919};
920
Evan Jones2600d4f2016-12-06 09:29:36 -0500921/**
922 * Return the current text attributes.
923 *
924 * @return {string}
925 */
rgindaa19afe22012-01-25 15:40:22 -0800926hterm.Terminal.prototype.getTextAttributes = function() {
927 return this.screen_.textAttributes;
928};
929
Evan Jones2600d4f2016-12-06 09:29:36 -0500930/**
931 * Set the text attributes.
932 *
933 * @param {string} textAttributes The attributes to set.
934 */
rginda1a09aa02012-06-18 21:11:25 -0700935hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
936 this.screen_.textAttributes = textAttributes;
937};
938
rginda87b86462011-12-14 13:48:03 -0800939/**
rgindaf522ce02012-04-17 17:49:17 -0700940 * Return the current browser zoom factor applied to the terminal.
941 *
942 * @return {number} The current browser zoom factor.
943 */
944hterm.Terminal.prototype.getZoomFactor = function() {
945 return this.scrollPort_.characterSize.zoomFactor;
946};
947
948/**
rginda9846e2f2012-01-27 13:53:33 -0800949 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500950 *
951 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800952 */
953hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800954 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800955};
956
957/**
rginda87b86462011-12-14 13:48:03 -0800958 * Restore a previously saved cursor position.
959 *
Joel Hockey0f933582019-08-27 18:01:51 -0700960 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800961 */
962hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700963 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
964 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800965 this.screen_.setCursorPosition(row, column);
966 if (cursor.column > column ||
967 cursor.column == column && cursor.overflow) {
968 this.screen_.cursorPosition.overflow = true;
969 }
rginda87b86462011-12-14 13:48:03 -0800970};
971
972/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400973 * Clear the cursor's overflow flag.
974 */
975hterm.Terminal.prototype.clearCursorOverflow = function() {
976 this.screen_.cursorPosition.overflow = false;
977};
978
979/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800980 * Save the current cursor state to the corresponding screens.
981 *
982 * See the hterm.Screen.CursorState class for more details.
983 *
984 * @param {boolean=} both If true, update both screens, else only update the
985 * current screen.
986 */
987hterm.Terminal.prototype.saveCursorAndState = function(both) {
988 if (both) {
989 this.primaryScreen_.saveCursorAndState(this.vt);
990 this.alternateScreen_.saveCursorAndState(this.vt);
991 } else
992 this.screen_.saveCursorAndState(this.vt);
993};
994
995/**
996 * Restore the saved cursor state in the corresponding screens.
997 *
998 * See the hterm.Screen.CursorState class for more details.
999 *
1000 * @param {boolean=} both If true, update both screens, else only update the
1001 * current screen.
1002 */
1003hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1004 if (both) {
1005 this.primaryScreen_.restoreCursorAndState(this.vt);
1006 this.alternateScreen_.restoreCursorAndState(this.vt);
1007 } else
1008 this.screen_.restoreCursorAndState(this.vt);
1009};
1010
1011/**
Robert Ginda830583c2013-08-07 13:20:46 -07001012 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001013 *
1014 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001015 */
1016hterm.Terminal.prototype.setCursorShape = function(shape) {
1017 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001018 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001019};
Robert Ginda830583c2013-08-07 13:20:46 -07001020
1021/**
1022 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001023 *
1024 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001025 */
1026hterm.Terminal.prototype.getCursorShape = function() {
1027 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001028};
Robert Ginda830583c2013-08-07 13:20:46 -07001029
1030/**
rginda87b86462011-12-14 13:48:03 -08001031 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001032 *
1033 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001034 */
1035hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001036 if (columnCount == null) {
1037 this.div_.style.width = '100%';
1038 return;
1039 }
1040
Robert Ginda26806d12014-07-24 13:44:07 -07001041 this.div_.style.width = Math.ceil(
1042 this.scrollPort_.characterSize.width *
1043 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001044 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001045 this.scheduleSyncCursorPosition_();
1046};
rginda87b86462011-12-14 13:48:03 -08001047
rgindac9bc5502012-01-18 11:48:44 -08001048/**
rginda35c456b2012-02-09 17:29:05 -08001049 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001050 *
1051 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001052 */
1053hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001054 if (rowCount == null) {
1055 this.div_.style.height = '100%';
1056 return;
1057 }
1058
rginda35c456b2012-02-09 17:29:05 -08001059 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001060 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001061 this.realizeSize_(this.screenSize.width, rowCount);
1062 this.scheduleSyncCursorPosition_();
1063};
1064
1065/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001066 * Deal with terminal size changes.
1067 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001068 * @param {number} columnCount The number of columns.
1069 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001070 */
1071hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001072 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001073
Mike Frysinger0206e262019-06-13 10:18:19 -04001074 if (columnCount != this.screenSize.width) {
1075 notify = true;
1076 this.realizeWidth_(columnCount);
1077 }
1078
1079 if (rowCount != this.screenSize.height) {
1080 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001081 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001082 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001083
1084 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001085 if (notify) {
1086 this.io.onTerminalResize_(columnCount, rowCount);
1087 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001088};
1089
1090/**
rgindac9bc5502012-01-18 11:48:44 -08001091 * Deal with terminal width changes.
1092 *
1093 * This function does what needs to be done when the terminal width changes
1094 * out from under us. It happens here rather than in onResize_() because this
1095 * code may need to run synchronously to handle programmatic changes of
1096 * terminal width.
1097 *
1098 * Relying on the browser to send us an async resize event means we may not be
1099 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001100 *
1101 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001102 */
1103hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001104 if (columnCount <= 0)
1105 throw new Error('Attempt to realize bad width: ' + columnCount);
1106
rgindac9bc5502012-01-18 11:48:44 -08001107 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001108 if (deltaColumns == 0) {
1109 // No change, so don't bother recalculating things.
1110 return;
1111 }
rgindac9bc5502012-01-18 11:48:44 -08001112
rginda87b86462011-12-14 13:48:03 -08001113 this.screenSize.width = columnCount;
1114 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001115
1116 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001117 if (this.defaultTabStops)
1118 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001119 } else {
1120 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001121 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001122 break;
1123
1124 this.tabStops_.pop();
1125 }
1126 }
1127
1128 this.screen_.setColumnCount(this.screenSize.width);
1129};
1130
1131/**
1132 * Deal with terminal height changes.
1133 *
1134 * This function does what needs to be done when the terminal height changes
1135 * out from under us. It happens here rather than in onResize_() because this
1136 * code may need to run synchronously to handle programmatic changes of
1137 * terminal height.
1138 *
1139 * Relying on the browser to send us an async resize event means we may not be
1140 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001141 *
1142 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001143 */
1144hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001145 if (rowCount <= 0)
1146 throw new Error('Attempt to realize bad height: ' + rowCount);
1147
rgindac9bc5502012-01-18 11:48:44 -08001148 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001149 if (deltaRows == 0) {
1150 // No change, so don't bother recalculating things.
1151 return;
1152 }
rgindac9bc5502012-01-18 11:48:44 -08001153
1154 this.screenSize.height = rowCount;
1155
1156 var cursor = this.saveCursor();
1157
1158 if (deltaRows < 0) {
1159 // Screen got smaller.
1160 deltaRows *= -1;
1161 while (deltaRows) {
1162 var lastRow = this.getRowCount() - 1;
1163 if (lastRow - this.scrollbackRows_.length == cursor.row)
1164 break;
1165
1166 if (this.getRowText(lastRow))
1167 break;
1168
1169 this.screen_.popRow();
1170 deltaRows--;
1171 }
1172
1173 var ary = this.screen_.shiftRows(deltaRows);
1174 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1175
1176 // We just removed rows from the top of the screen, we need to update
1177 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001178 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001179 } else if (deltaRows > 0) {
1180 // Screen got larger.
1181
1182 if (deltaRows <= this.scrollbackRows_.length) {
1183 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1184 var rows = this.scrollbackRows_.splice(
1185 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1186 this.screen_.unshiftRows(rows);
1187 deltaRows -= scrollbackCount;
1188 cursor.row += scrollbackCount;
1189 }
1190
1191 if (deltaRows)
1192 this.appendRows_(deltaRows);
1193 }
1194
rginda35c456b2012-02-09 17:29:05 -08001195 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001196 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001197};
1198
1199/**
1200 * Scroll the terminal to the top of the scrollback buffer.
1201 */
1202hterm.Terminal.prototype.scrollHome = function() {
1203 this.scrollPort_.scrollRowToTop(0);
1204};
1205
1206/**
1207 * Scroll the terminal to the end.
1208 */
1209hterm.Terminal.prototype.scrollEnd = function() {
1210 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1211};
1212
1213/**
1214 * Scroll the terminal one page up (minus one line) relative to the current
1215 * position.
1216 */
1217hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001218 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001219};
1220
1221/**
1222 * Scroll the terminal one page down (minus one line) relative to the current
1223 * position.
1224 */
1225hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001226 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001227};
1228
rgindac9bc5502012-01-18 11:48:44 -08001229/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001230 * Scroll the terminal one line up relative to the current position.
1231 */
1232hterm.Terminal.prototype.scrollLineUp = function() {
1233 var i = this.scrollPort_.getTopRowIndex();
1234 this.scrollPort_.scrollRowToTop(i - 1);
1235};
1236
1237/**
1238 * Scroll the terminal one line down relative to the current position.
1239 */
1240hterm.Terminal.prototype.scrollLineDown = function() {
1241 var i = this.scrollPort_.getTopRowIndex();
1242 this.scrollPort_.scrollRowToTop(i + 1);
1243};
1244
1245/**
Robert Ginda40932892012-12-10 17:26:40 -08001246 * Clear primary screen, secondary screen, and the scrollback buffer.
1247 */
1248hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001249 this.clearHome(this.primaryScreen_);
1250 this.clearHome(this.alternateScreen_);
1251
1252 this.clearScrollback();
1253};
1254
1255/**
1256 * Clear scrollback buffer.
1257 */
1258hterm.Terminal.prototype.clearScrollback = function() {
1259 // Move to the end of the buffer in case the screen was scrolled back.
1260 // We're going to throw it away which would leave the display invalid.
1261 this.scrollEnd();
1262
Robert Ginda40932892012-12-10 17:26:40 -08001263 this.scrollbackRows_.length = 0;
1264 this.scrollPort_.resetCache();
1265
Mike Frysinger9c482b82018-09-07 02:49:36 -04001266 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1267 const bottom = screen.getHeight();
1268 this.renumberRows_(0, bottom, screen);
1269 });
Robert Ginda40932892012-12-10 17:26:40 -08001270
1271 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001272 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001273};
1274
1275/**
rgindac9bc5502012-01-18 11:48:44 -08001276 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001277 *
1278 * Perform a full reset to the default values listed in
1279 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001280 */
rginda87b86462011-12-14 13:48:03 -08001281hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001282 this.vt.reset();
1283
rgindac9bc5502012-01-18 11:48:44 -08001284 this.clearAllTabStops();
1285 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001286
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001287 const resetScreen = (screen) => {
1288 // We want to make sure to reset the attributes before we clear the screen.
1289 // The attributes might be used to initialize default/empty rows.
1290 screen.textAttributes.reset();
1291 screen.textAttributes.resetColorPalette();
1292 this.clearHome(screen);
1293 screen.saveCursorAndState(this.vt);
1294 };
1295 resetScreen(this.primaryScreen_);
1296 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001297
Mike Frysinger84301d02017-11-29 13:28:46 -08001298 // Reset terminal options to their default values.
1299 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001300 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1301
Mike Frysinger84301d02017-11-29 13:28:46 -08001302 this.setVTScrollRegion(null, null);
1303
1304 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001305};
1306
rgindac9bc5502012-01-18 11:48:44 -08001307/**
1308 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001309 *
1310 * Perform a soft reset to the default values listed in
1311 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001312 */
rginda0f5c0292012-01-13 11:00:13 -08001313hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001314 this.vt.reset();
1315
rgindab8bc8932012-04-27 12:45:03 -07001316 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001317 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001318
Brad Townb62dfdc2015-03-16 19:07:15 -07001319 // We show the cursor on soft reset but do not alter the blink state.
1320 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1321
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001322 const resetScreen = (screen) => {
1323 // Xterm also resets the color palette on soft reset, even though it doesn't
1324 // seem to be documented anywhere.
1325 screen.textAttributes.reset();
1326 screen.textAttributes.resetColorPalette();
1327 screen.saveCursorAndState(this.vt);
1328 };
1329 resetScreen(this.primaryScreen_);
1330 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001331
rgindab8bc8932012-04-27 12:45:03 -07001332 // The xterm man page explicitly says this will happen on soft reset.
1333 this.setVTScrollRegion(null, null);
1334
1335 // Xterm also shows the cursor on soft reset, but does not alter the blink
1336 // state.
rgindaa19afe22012-01-25 15:40:22 -08001337 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001338};
1339
rgindac9bc5502012-01-18 11:48:44 -08001340/**
1341 * Move the cursor forward to the next tab stop, or to the last column
1342 * if no more tab stops are set.
1343 */
1344hterm.Terminal.prototype.forwardTabStop = function() {
1345 var column = this.screen_.cursorPosition.column;
1346
1347 for (var i = 0; i < this.tabStops_.length; i++) {
1348 if (this.tabStops_[i] > column) {
1349 this.setCursorColumn(this.tabStops_[i]);
1350 return;
1351 }
1352 }
1353
David Benjamin66e954d2012-05-05 21:08:12 -04001354 // xterm does not clear the overflow flag on HT or CHT.
1355 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001356 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001357 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001358};
1359
rgindac9bc5502012-01-18 11:48:44 -08001360/**
1361 * Move the cursor backward to the previous tab stop, or to the first column
1362 * if no previous tab stops are set.
1363 */
1364hterm.Terminal.prototype.backwardTabStop = function() {
1365 var column = this.screen_.cursorPosition.column;
1366
1367 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1368 if (this.tabStops_[i] < column) {
1369 this.setCursorColumn(this.tabStops_[i]);
1370 return;
1371 }
1372 }
1373
1374 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001375};
1376
rgindac9bc5502012-01-18 11:48:44 -08001377/**
1378 * Set a tab stop at the given column.
1379 *
Joel Hockey0f933582019-08-27 18:01:51 -07001380 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001381 */
1382hterm.Terminal.prototype.setTabStop = function(column) {
1383 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1384 if (this.tabStops_[i] == column)
1385 return;
1386
1387 if (this.tabStops_[i] < column) {
1388 this.tabStops_.splice(i + 1, 0, column);
1389 return;
1390 }
1391 }
1392
1393 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001394};
1395
rgindac9bc5502012-01-18 11:48:44 -08001396/**
1397 * Clear the tab stop at the current cursor position.
1398 *
1399 * No effect if there is no tab stop at the current cursor position.
1400 */
1401hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1402 var column = this.screen_.cursorPosition.column;
1403
1404 var i = this.tabStops_.indexOf(column);
1405 if (i == -1)
1406 return;
1407
1408 this.tabStops_.splice(i, 1);
1409};
1410
1411/**
1412 * Clear all tab stops.
1413 */
1414hterm.Terminal.prototype.clearAllTabStops = function() {
1415 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001416 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001417};
1418
1419/**
1420 * Set up the default tab stops, starting from a given column.
1421 *
1422 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001423 * from the specified column, or 0 if no column is provided. It also flags
1424 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001425 *
1426 * This does not clear the existing tab stops first, use clearAllTabStops
1427 * for that.
1428 *
Joel Hockey0f933582019-08-27 18:01:51 -07001429 * @param {number=} opt_start Optional starting zero based starting column,
1430 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001431 */
1432hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1433 var start = opt_start || 0;
1434 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001435 // Round start up to a default tab stop.
1436 start = start - 1 - ((start - 1) % w) + w;
1437 for (var i = start; i < this.screenSize.width; i += w) {
1438 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001439 }
David Benjamin66e954d2012-05-05 21:08:12 -04001440
1441 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001442};
1443
rginda6d397402012-01-17 10:58:29 -08001444/**
rginda8ba33642011-12-14 12:31:31 -08001445 * Interpret a sequence of characters.
1446 *
1447 * Incomplete escape sequences are buffered until the next call.
1448 *
1449 * @param {string} str Sequence of characters to interpret or pass through.
1450 */
1451hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001452 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001453 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001454};
1455
1456/**
1457 * Take over the given DIV for use as the terminal display.
1458 *
Joel Hockey0f933582019-08-27 18:01:51 -07001459 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001460 */
1461hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001462 const charset = div.ownerDocument.characterSet.toLowerCase();
1463 if (charset != 'utf-8') {
1464 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1465 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1466 }
1467
rginda87b86462011-12-14 13:48:03 -08001468 this.div_ = div;
1469
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001470 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1471
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001472 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1473};
1474
1475/**
1476 * Initialisation of ScrollPort properties which need to be set after its DOM
1477 * has been initialised.
1478 * @private
1479 */
1480hterm.Terminal.prototype.setupScrollPort_ = function() {
rginda30f20f62012-04-05 16:36:19 -07001481 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001482 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1483 this.scrollPort_.setBackgroundPosition(
1484 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001485 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1486 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001487 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001488
rginda0918b652012-04-04 11:26:24 -07001489 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001490
rginda9f5222b2012-03-05 11:53:28 -08001491 this.setFontSize(this.prefs_.get('font-size'));
1492 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001493
David Reveman8f552492012-03-28 12:18:41 -04001494 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001495 this.setScrollWheelMoveMultipler(
1496 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001497
rginda8ba33642011-12-14 12:31:31 -08001498 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001499 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001500
Evan Jones5f9df812016-12-06 09:38:58 -05001501 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001502 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001503
1504 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001505 var screenNode = this.scrollPort_.getScreenNode();
1506 screenNode.addEventListener('mousedown', onMouse);
1507 screenNode.addEventListener('mouseup', onMouse);
1508 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001509 this.scrollPort_.onScrollWheel = onMouse;
1510
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001511 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1512
Toni Barzic0bfa8922013-11-22 11:18:35 -08001513 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001514 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001515 // Listen for mousedown events on the screenNode as in FF the focus
1516 // events don't bubble.
1517 screenNode.addEventListener('mousedown', function() {
1518 setTimeout(this.onFocusChange_.bind(this, true));
1519 }.bind(this));
1520
Toni Barzic0bfa8922013-11-22 11:18:35 -08001521 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001522 'blur', this.onFocusChange_.bind(this, false));
1523
1524 var style = this.document_.createElement('style');
1525 style.textContent =
1526 ('.cursor-node[focus="false"] {' +
1527 ' box-sizing: border-box;' +
1528 ' background-color: transparent !important;' +
1529 ' border-width: 2px;' +
1530 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001531 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001532 'menu {' +
1533 ' margin: 0;' +
1534 ' padding: 0;' +
1535 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1536 '}' +
1537 'menuitem {' +
1538 ' white-space: nowrap;' +
1539 ' border-bottom: 1px dashed;' +
1540 ' display: block;' +
1541 ' padding: 0.3em 0.3em 0 0.3em;' +
1542 '}' +
1543 'menuitem.separator {' +
1544 ' border-bottom: none;' +
1545 ' height: 0.5em;' +
1546 ' padding: 0;' +
1547 '}' +
1548 'menuitem:hover {' +
1549 ' color: var(--hterm-cursor-color);' +
1550 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001551 '.wc-node {' +
1552 ' display: inline-block;' +
1553 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001554 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001555 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001556 '}' +
1557 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001558 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1559 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001560 // Default position hides the cursor for when the window is initializing.
1561 ' --hterm-cursor-offset-col: -1;' +
1562 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001563 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001564 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001565 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001566 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001567 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001568 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001569 '.uri-node:hover {' +
1570 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001571 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001572 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001573 '@keyframes blink {' +
1574 ' from { opacity: 1.0; }' +
1575 ' to { opacity: 0.0; }' +
1576 '}' +
1577 '.blink-node {' +
1578 ' animation-name: blink;' +
1579 ' animation-duration: var(--hterm-blink-node-duration);' +
1580 ' animation-iteration-count: infinite;' +
1581 ' animation-timing-function: ease-in-out;' +
1582 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001583 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001584 // Insert this stock style as the first node so that any user styles will
1585 // override w/out having to use !important everywhere. The rules above mix
1586 // runtime variables with default ones designed to be overridden by the user,
1587 // but we can wait for a concrete case from the users to determine the best
1588 // way to split the sheet up to before & after the user-css settings.
1589 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001590
rginda8ba33642011-12-14 12:31:31 -08001591 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001592 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001593 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001594 this.cursorNode_.style.cssText =
1595 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001596 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1597 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001598 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001599 'width: var(--hterm-charsize-width);' +
1600 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001601 'background-color: var(--hterm-cursor-color);' +
1602 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001603 '-webkit-transition: opacity, background-color 100ms linear;' +
1604 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001605
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001606 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001607 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1608 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001609
rginda8ba33642011-12-14 12:31:31 -08001610 this.document_.body.appendChild(this.cursorNode_);
1611
rgindad5613292012-06-19 15:40:37 -07001612 // When 'enableMouseDragScroll' is off we reposition this element directly
1613 // under the mouse cursor after a click. This makes Chrome associate
1614 // subsequent mousemove events with the scroll-blocker. Since the
1615 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1616 // events do not cause the scrollport to scroll.
1617 //
1618 // It's a hack, but it's the cleanest way I could find.
1619 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001620 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001621 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001622 this.scrollBlockerNode_.style.cssText =
1623 ('position: absolute;' +
1624 'top: -99px;' +
1625 'display: block;' +
1626 'width: 10px;' +
1627 'height: 10px;');
1628 this.document_.body.appendChild(this.scrollBlockerNode_);
1629
rgindad5613292012-06-19 15:40:37 -07001630 this.scrollPort_.onScrollWheel = onMouse;
1631 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1632 ].forEach(function(event) {
1633 this.scrollBlockerNode_.addEventListener(event, onMouse);
1634 this.cursorNode_.addEventListener(event, onMouse);
1635 this.document_.addEventListener(event, onMouse);
1636 }.bind(this));
1637
1638 this.cursorNode_.addEventListener('mousedown', function() {
1639 setTimeout(this.focus.bind(this));
1640 }.bind(this));
1641
rginda8ba33642011-12-14 12:31:31 -08001642 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001643
rginda87b86462011-12-14 13:48:03 -08001644 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001645 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001646};
1647
rginda0918b652012-04-04 11:26:24 -07001648/**
1649 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001650 *
Joel Hockey0f933582019-08-27 18:01:51 -07001651 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001652 */
rginda87b86462011-12-14 13:48:03 -08001653hterm.Terminal.prototype.getDocument = function() {
1654 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001655};
1656
1657/**
rginda0918b652012-04-04 11:26:24 -07001658 * Focus the terminal.
1659 */
1660hterm.Terminal.prototype.focus = function() {
1661 this.scrollPort_.focus();
1662};
1663
1664/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001665 * Unfocus the terminal.
1666 */
1667hterm.Terminal.prototype.blur = function() {
1668 this.scrollPort_.blur();
1669};
1670
1671/**
rginda8ba33642011-12-14 12:31:31 -08001672 * Return the HTML Element for a given row index.
1673 *
1674 * This is a method from the RowProvider interface. The ScrollPort uses
1675 * it to fetch rows on demand as they are scrolled into view.
1676 *
1677 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1678 * pairs to conserve memory.
1679 *
Joel Hockey0f933582019-08-27 18:01:51 -07001680 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001681 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001682 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001683 * @return {!Element} The 'x-row' element containing for the requested row.
rginda8ba33642011-12-14 12:31:31 -08001684 */
1685hterm.Terminal.prototype.getRowNode = function(index) {
1686 if (index < this.scrollbackRows_.length)
1687 return this.scrollbackRows_[index];
1688
1689 var screenIndex = index - this.scrollbackRows_.length;
1690 return this.screen_.rowsArray[screenIndex];
1691};
1692
1693/**
1694 * Return the text content for a given range of rows.
1695 *
1696 * This is a method from the RowProvider interface. The ScrollPort uses
1697 * it to fetch text content on demand when the user attempts to copy their
1698 * selection to the clipboard.
1699 *
Joel Hockey0f933582019-08-27 18:01:51 -07001700 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001701 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001702 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001703 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001704 * relative to the start of the scrollback buffer.
1705 * @return {string} A single string containing the text value of the range of
1706 * rows. Lines will be newline delimited, with no trailing newline.
1707 */
1708hterm.Terminal.prototype.getRowsText = function(start, end) {
1709 var ary = [];
1710 for (var i = start; i < end; i++) {
1711 var node = this.getRowNode(i);
1712 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001713 if (i < end - 1 && !node.getAttribute('line-overflow'))
1714 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001715 }
1716
rgindaa09e7332012-08-17 12:49:51 -07001717 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001718};
1719
1720/**
1721 * Return the text content for a given row.
1722 *
1723 * This is a method from the RowProvider interface. The ScrollPort uses
1724 * it to fetch text content on demand when the user attempts to copy their
1725 * selection to the clipboard.
1726 *
Joel Hockey0f933582019-08-27 18:01:51 -07001727 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001728 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001729 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001730 * @return {string} A string containing the text value of the selected row.
1731 */
1732hterm.Terminal.prototype.getRowText = function(index) {
1733 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001734 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001735};
1736
1737/**
1738 * Return the total number of rows in the addressable screen and in the
1739 * scrollback buffer of this terminal.
1740 *
1741 * This is a method from the RowProvider interface. The ScrollPort uses
1742 * it to compute the size of the scrollbar.
1743 *
Joel Hockey0f933582019-08-27 18:01:51 -07001744 * @return {number} The number of rows in this terminal.
rginda8ba33642011-12-14 12:31:31 -08001745 */
1746hterm.Terminal.prototype.getRowCount = function() {
1747 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1748};
1749
1750/**
1751 * Create DOM nodes for new rows and append them to the end of the terminal.
1752 *
1753 * This is the only correct way to add a new DOM node for a row. Notice that
1754 * the new row is appended to the bottom of the list of rows, and does not
1755 * require renumbering (of the rowIndex property) of previous rows.
1756 *
1757 * If you think you want a new blank row somewhere in the middle of the
1758 * terminal, look into moveRows_().
1759 *
1760 * This method does not pay attention to vtScrollTop/Bottom, since you should
1761 * be using moveRows() in cases where they would matter.
1762 *
1763 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001764 *
1765 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001766 */
1767hterm.Terminal.prototype.appendRows_ = function(count) {
1768 var cursorRow = this.screen_.rowsArray.length;
1769 var offset = this.scrollbackRows_.length + cursorRow;
1770 for (var i = 0; i < count; i++) {
1771 var row = this.document_.createElement('x-row');
1772 row.appendChild(this.document_.createTextNode(''));
1773 row.rowIndex = offset + i;
1774 this.screen_.pushRow(row);
1775 }
1776
1777 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1778 if (extraRows > 0) {
1779 var ary = this.screen_.shiftRows(extraRows);
1780 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001781 if (this.scrollPort_.isScrolledEnd)
1782 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001783 }
1784
1785 if (cursorRow >= this.screen_.rowsArray.length)
1786 cursorRow = this.screen_.rowsArray.length - 1;
1787
rginda87b86462011-12-14 13:48:03 -08001788 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001789};
1790
1791/**
1792 * Relocate rows from one part of the addressable screen to another.
1793 *
1794 * This is used to recycle rows during VT scrolls (those which are driven
1795 * by VT commands, rather than by the user manipulating the scrollbar.)
1796 *
1797 * In this case, the blank lines scrolled into the scroll region are made of
1798 * the nodes we scrolled off. These have their rowIndex properties carefully
1799 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001800 *
1801 * @param {number} fromIndex The start index.
1802 * @param {number} count The number of rows to move.
1803 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001804 */
1805hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1806 var ary = this.screen_.removeRows(fromIndex, count);
1807 this.screen_.insertRows(toIndex, ary);
1808
1809 var start, end;
1810 if (fromIndex < toIndex) {
1811 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001812 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001813 } else {
1814 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001815 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001816 }
1817
1818 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001819 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001820};
1821
1822/**
1823 * Renumber the rowIndex property of the given range of rows.
1824 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001825 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001826 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001827 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001828 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001829 *
1830 * @param {number} start The start index.
1831 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001832 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001833 */
Robert Ginda40932892012-12-10 17:26:40 -08001834hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1835 var screen = opt_screen || this.screen_;
1836
rginda8ba33642011-12-14 12:31:31 -08001837 var offset = this.scrollbackRows_.length;
1838 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001839 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001840 }
1841};
1842
1843/**
1844 * Print a string to the terminal.
1845 *
1846 * This respects the current insert and wraparound modes. It will add new lines
1847 * to the end of the terminal, scrolling off the top into the scrollback buffer
1848 * if necessary.
1849 *
1850 * The string is *not* parsed for escape codes. Use the interpret() method if
1851 * that's what you're after.
1852 *
1853 * @param{string} str The string to print.
1854 */
1855hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001856 this.scheduleSyncCursorPosition_();
1857
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001858 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001859 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001860
rgindaa9abdd82012-08-06 18:05:09 -07001861 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001862
Ricky Liang48f05cb2013-12-31 23:35:29 +08001863 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001864 // Fun edge case: If the string only contains zero width codepoints (like
1865 // combining characters), we make sure to iterate at least once below.
1866 if (strWidth == 0 && str)
1867 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001868
1869 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001870 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1871 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001872 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001873 }
rgindaa19afe22012-01-25 15:40:22 -08001874
Ricky Liang48f05cb2013-12-31 23:35:29 +08001875 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001876 var didOverflow = false;
1877 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001878
rgindaa9abdd82012-08-06 18:05:09 -07001879 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1880 didOverflow = true;
1881 count = this.screenSize.width - this.screen_.cursorPosition.column;
1882 }
rgindaa19afe22012-01-25 15:40:22 -08001883
rgindaa9abdd82012-08-06 18:05:09 -07001884 if (didOverflow && !this.options_.wraparound) {
1885 // If the string overflowed the line but wraparound is off, then the
1886 // last printed character should be the last of the string.
1887 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001888 substr = lib.wc.substr(str, startOffset, count - 1) +
1889 lib.wc.substr(str, strWidth - 1);
1890 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001891 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001892 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001893 }
rgindaa19afe22012-01-25 15:40:22 -08001894
Ricky Liang48f05cb2013-12-31 23:35:29 +08001895 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1896 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001897 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1898 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001899
1900 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001901 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001902 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001903 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001904 }
1905 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001906 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001907 }
1908
1909 this.screen_.maybeClipCurrentRow();
1910 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001911 }
rginda8ba33642011-12-14 12:31:31 -08001912
rginda9f5222b2012-03-05 11:53:28 -08001913 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001914 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001915};
1916
1917/**
rginda87b86462011-12-14 13:48:03 -08001918 * Set the VT scroll region.
1919 *
rginda87b86462011-12-14 13:48:03 -08001920 * This also resets the cursor position to the absolute (0, 0) position, since
1921 * that's what xterm appears to do.
1922 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001923 * Setting the scroll region to the full height of the terminal will clear
1924 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1925 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1926 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1927 * continue to work as most users would expect.
1928 *
Joel Hockey0f933582019-08-27 18:01:51 -07001929 * @param {number} scrollTop The zero-based top of the scroll region.
1930 * @param {number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001931 * inclusive.
1932 */
1933hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001934 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001935 this.vtScrollTop_ = null;
1936 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001937 } else {
1938 this.vtScrollTop_ = scrollTop;
1939 this.vtScrollBottom_ = scrollBottom;
1940 }
rginda87b86462011-12-14 13:48:03 -08001941};
1942
1943/**
rginda8ba33642011-12-14 12:31:31 -08001944 * Return the top row index according to the VT.
1945 *
1946 * This will return 0 unless the terminal has been told to restrict scrolling
1947 * to some lower row. It is used for some VT cursor positioning and scrolling
1948 * commands.
1949 *
Joel Hockey0f933582019-08-27 18:01:51 -07001950 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001951 */
1952hterm.Terminal.prototype.getVTScrollTop = function() {
1953 if (this.vtScrollTop_ != null)
1954 return this.vtScrollTop_;
1955
1956 return 0;
rginda87b86462011-12-14 13:48:03 -08001957};
rginda8ba33642011-12-14 12:31:31 -08001958
1959/**
1960 * Return the bottom row index according to the VT.
1961 *
1962 * This will return the height of the terminal unless the it has been told to
1963 * restrict scrolling to some higher row. It is used for some VT cursor
1964 * positioning and scrolling commands.
1965 *
Joel Hockey0f933582019-08-27 18:01:51 -07001966 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001967 */
1968hterm.Terminal.prototype.getVTScrollBottom = function() {
1969 if (this.vtScrollBottom_ != null)
1970 return this.vtScrollBottom_;
1971
rginda87b86462011-12-14 13:48:03 -08001972 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001973};
rginda8ba33642011-12-14 12:31:31 -08001974
1975/**
1976 * Process a '\n' character.
1977 *
1978 * If the cursor is on the final row of the terminal this will append a new
1979 * blank row to the screen and scroll the topmost row into the scrollback
1980 * buffer.
1981 *
1982 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001983 *
1984 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1985 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001986 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001987hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1988 if (!dueToOverflow)
1989 this.accessibilityReader_.newLine();
1990
Robert Ginda9937abc2013-07-25 16:09:23 -07001991 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1992 this.screen_.rowsArray.length - 1);
1993
1994 if (this.vtScrollBottom_ != null) {
1995 // A VT Scroll region is active, we never append new rows.
1996 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1997 // We're at the end of the VT Scroll Region, perform a VT scroll.
1998 this.vtScrollUp(1);
1999 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2000 } else if (cursorAtEndOfScreen) {
2001 // We're at the end of the screen, the only thing to do is put the
2002 // cursor to column 0.
2003 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2004 } else {
2005 // Anywhere else, advance the cursor row, and reset the column.
2006 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2007 }
2008 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002009 // We're at the end of the screen. Append a new row to the terminal,
2010 // shifting the top row into the scrollback.
2011 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002012 } else {
rginda87b86462011-12-14 13:48:03 -08002013 // Anywhere else in the screen just moves the cursor.
2014 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002015 }
2016};
2017
2018/**
2019 * Like newLine(), except maintain the cursor column.
2020 */
2021hterm.Terminal.prototype.lineFeed = function() {
2022 var column = this.screen_.cursorPosition.column;
2023 this.newLine();
2024 this.setCursorColumn(column);
2025};
2026
2027/**
rginda87b86462011-12-14 13:48:03 -08002028 * If autoCarriageReturn is set then newLine(), else lineFeed().
2029 */
2030hterm.Terminal.prototype.formFeed = function() {
2031 if (this.options_.autoCarriageReturn) {
2032 this.newLine();
2033 } else {
2034 this.lineFeed();
2035 }
2036};
2037
2038/**
2039 * Move the cursor up one row, possibly inserting a blank line.
2040 *
2041 * The cursor column is not changed.
2042 */
2043hterm.Terminal.prototype.reverseLineFeed = function() {
2044 var scrollTop = this.getVTScrollTop();
2045 var currentRow = this.screen_.cursorPosition.row;
2046
2047 if (currentRow == scrollTop) {
2048 this.insertLines(1);
2049 } else {
2050 this.setAbsoluteCursorRow(currentRow - 1);
2051 }
2052};
2053
2054/**
rginda8ba33642011-12-14 12:31:31 -08002055 * Replace all characters to the left of the current cursor with the space
2056 * character.
2057 *
2058 * TODO(rginda): This should probably *remove* the characters (not just replace
2059 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002060 * position.
rginda8ba33642011-12-14 12:31:31 -08002061 */
2062hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002063 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002064 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002065 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002066 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002067 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002068};
2069
2070/**
David Benjamin684a9b72012-05-01 17:19:58 -04002071 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002072 *
2073 * The cursor position is unchanged.
2074 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002075 * If the current background color is not the default background color this
2076 * will insert spaces rather than delete. This is unfortunate because the
2077 * trailing space will affect text selection, but it's difficult to come up
2078 * with a way to style empty space that wouldn't trip up the hterm.Screen
2079 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002080 *
2081 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2082 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2083 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002084 *
Joel Hockey0f933582019-08-27 18:01:51 -07002085 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002086 */
2087hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002088 if (this.screen_.cursorPosition.overflow)
2089 return;
2090
Robert Ginda7fd57082012-09-25 14:41:47 -07002091 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2092 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002093
2094 if (this.screen_.textAttributes.background ===
2095 this.screen_.textAttributes.DEFAULT_COLOR) {
2096 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002097 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002098 this.screen_.cursorPosition.column + count) {
2099 this.screen_.deleteChars(count);
2100 this.clearCursorOverflow();
2101 return;
2102 }
2103 }
2104
rginda87b86462011-12-14 13:48:03 -08002105 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002106 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002107 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002108 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002109};
2110
2111/**
2112 * Erase the current line.
2113 *
2114 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002115 */
2116hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002117 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002118 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002119 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002120 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002121};
2122
2123/**
David Benjamina08d78f2012-05-05 00:28:49 -04002124 * Erase all characters from the start of the screen to the current cursor
2125 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002126 *
2127 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002128 */
2129hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002130 var cursor = this.saveCursor();
2131
2132 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002133
David Benjamina08d78f2012-05-05 00:28:49 -04002134 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002135 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002136 this.screen_.clearCursorRow();
2137 }
2138
rginda87b86462011-12-14 13:48:03 -08002139 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002140 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002141};
2142
2143/**
2144 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002145 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002146 *
2147 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002148 */
2149hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002150 var cursor = this.saveCursor();
2151
2152 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002153
David Benjamina08d78f2012-05-05 00:28:49 -04002154 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002155 for (var i = cursor.row + 1; i <= bottom; i++) {
2156 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002157 this.screen_.clearCursorRow();
2158 }
2159
rginda87b86462011-12-14 13:48:03 -08002160 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002161 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002162};
2163
2164/**
2165 * Fill the terminal with a given character.
2166 *
2167 * This methods does not respect the VT scroll region.
2168 *
2169 * @param {string} ch The character to use for the fill.
2170 */
2171hterm.Terminal.prototype.fill = function(ch) {
2172 var cursor = this.saveCursor();
2173
2174 this.setAbsoluteCursorPosition(0, 0);
2175 for (var row = 0; row < this.screenSize.height; row++) {
2176 for (var col = 0; col < this.screenSize.width; col++) {
2177 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002178 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002179 }
2180 }
2181
2182 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002183};
2184
2185/**
rginda9ea433c2012-03-16 11:57:00 -07002186 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002187 *
rginda9ea433c2012-03-16 11:57:00 -07002188 * This does not respect the scroll region.
2189 *
Joel Hockey0f933582019-08-27 18:01:51 -07002190 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002191 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002192 */
rginda9ea433c2012-03-16 11:57:00 -07002193hterm.Terminal.prototype.clearHome = function(opt_screen) {
2194 var screen = opt_screen || this.screen_;
2195 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002196
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002197 this.accessibilityReader_.clear();
2198
rginda11057d52012-04-25 12:29:56 -07002199 if (bottom == 0) {
2200 // Empty screen, nothing to do.
2201 return;
2202 }
2203
rgindae4d29232012-01-19 10:47:13 -08002204 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002205 screen.setCursorPosition(i, 0);
2206 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002207 }
2208
rginda9ea433c2012-03-16 11:57:00 -07002209 screen.setCursorPosition(0, 0);
2210};
2211
2212/**
2213 * Erase the entire display without changing the cursor position.
2214 *
2215 * The cursor position is unchanged. This does not respect the scroll
2216 * region.
2217 *
Joel Hockey0f933582019-08-27 18:01:51 -07002218 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002219 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002220 */
2221hterm.Terminal.prototype.clear = function(opt_screen) {
2222 var screen = opt_screen || this.screen_;
2223 var cursor = screen.cursorPosition.clone();
2224 this.clearHome(screen);
2225 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002226};
2227
2228/**
2229 * VT command to insert lines at the current cursor row.
2230 *
2231 * This respects the current scroll region. Rows pushed off the bottom are
2232 * lost (they won't show up in the scrollback buffer).
2233 *
Joel Hockey0f933582019-08-27 18:01:51 -07002234 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002235 */
2236hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002237 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002238
2239 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002240 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002241
Robert Ginda579186b2012-09-26 11:40:04 -07002242 // The moveCount is the number of rows we need to relocate to make room for
2243 // the new row(s). The count is the distance to move them.
2244 var moveCount = bottom - cursorRow - count + 1;
2245 if (moveCount)
2246 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002247
Robert Ginda579186b2012-09-26 11:40:04 -07002248 for (var i = count - 1; i >= 0; i--) {
2249 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002250 this.screen_.clearCursorRow();
2251 }
rginda8ba33642011-12-14 12:31:31 -08002252};
2253
2254/**
2255 * VT command to delete lines at the current cursor row.
2256 *
2257 * New rows are added to the bottom of scroll region to take their place. New
2258 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002259 *
2260 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002261 */
2262hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002263 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002264
rginda87b86462011-12-14 13:48:03 -08002265 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002266 var bottom = this.getVTScrollBottom();
2267
rginda87b86462011-12-14 13:48:03 -08002268 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002269 count = Math.min(count, maxCount);
2270
rginda87b86462011-12-14 13:48:03 -08002271 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002272 if (count != maxCount)
2273 this.moveRows_(top, count, moveStart);
2274
2275 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002276 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002277 this.screen_.clearCursorRow();
2278 }
2279
rginda87b86462011-12-14 13:48:03 -08002280 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002281 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002282};
2283
2284/**
2285 * Inserts the given number of spaces at the current cursor position.
2286 *
rginda87b86462011-12-14 13:48:03 -08002287 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002288 *
2289 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002290 */
2291hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002292 var cursor = this.saveCursor();
2293
Mike Frysinger73e56462019-07-17 00:23:46 -05002294 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002295 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002296 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002297
2298 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002299 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002300};
2301
2302/**
2303 * Forward-delete the specified number of characters starting at the cursor
2304 * position.
2305 *
Joel Hockey0f933582019-08-27 18:01:51 -07002306 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002307 */
2308hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002309 var deleted = this.screen_.deleteChars(count);
2310 if (deleted && !this.screen_.textAttributes.isDefault()) {
2311 var cursor = this.saveCursor();
2312 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002313 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002314 this.restoreCursor(cursor);
2315 }
2316
David Benjamin54e8bf62012-06-01 22:31:40 -04002317 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002318};
2319
2320/**
2321 * Shift rows in the scroll region upwards by a given number of lines.
2322 *
2323 * New rows are inserted at the bottom of the scroll region to fill the
2324 * vacated rows. The new rows not filled out with the current text attributes.
2325 *
2326 * This function does not affect the scrollback rows at all. Rows shifted
2327 * off the top are lost.
2328 *
rginda87b86462011-12-14 13:48:03 -08002329 * The cursor position is not altered.
2330 *
Joel Hockey0f933582019-08-27 18:01:51 -07002331 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002332 */
2333hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002334 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002335
rginda87b86462011-12-14 13:48:03 -08002336 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002337 this.deleteLines(count);
2338
rginda87b86462011-12-14 13:48:03 -08002339 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002340};
2341
2342/**
2343 * Shift rows below the cursor down by a given number of lines.
2344 *
2345 * This function respects the current scroll region.
2346 *
2347 * New rows are inserted at the top of the scroll region to fill the
2348 * vacated rows. The new rows not filled out with the current text attributes.
2349 *
2350 * This function does not affect the scrollback rows at all. Rows shifted
2351 * off the bottom are lost.
2352 *
Joel Hockey0f933582019-08-27 18:01:51 -07002353 * @param {number=} opt_count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002354 */
2355hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002356 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002357
rginda87b86462011-12-14 13:48:03 -08002358 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002359 this.insertLines(opt_count);
2360
rginda87b86462011-12-14 13:48:03 -08002361 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002362};
2363
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002364/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002365 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002366 *
2367 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002368 * cause Assitive Technology to announce the output of the terminal. It also
2369 * enables other features that aid assistive technology. All the features gated
2370 * behind this flag have a performance impact on the terminal which is why they
2371 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002372 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002373 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002374 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002375hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002376 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002377};
rginda87b86462011-12-14 13:48:03 -08002378
rginda8ba33642011-12-14 12:31:31 -08002379/**
2380 * Set the cursor position.
2381 *
2382 * The cursor row is relative to the scroll region if the terminal has
2383 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2384 *
Joel Hockey0f933582019-08-27 18:01:51 -07002385 * @param {number} row The new zero-based cursor row.
2386 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002387 */
2388hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2389 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002390 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002391 } else {
rginda87b86462011-12-14 13:48:03 -08002392 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002393 }
rginda87b86462011-12-14 13:48:03 -08002394};
rginda8ba33642011-12-14 12:31:31 -08002395
Evan Jones2600d4f2016-12-06 09:29:36 -05002396/**
2397 * Move the cursor relative to its current position.
2398 *
2399 * @param {number} row
2400 * @param {number} column
2401 */
rginda87b86462011-12-14 13:48:03 -08002402hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2403 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002404 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2405 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002406 this.screen_.setCursorPosition(row, column);
2407};
2408
Evan Jones2600d4f2016-12-06 09:29:36 -05002409/**
2410 * Move the cursor to the specified position.
2411 *
2412 * @param {number} row
2413 * @param {number} column
2414 */
rginda87b86462011-12-14 13:48:03 -08002415hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002416 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2417 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002418 this.screen_.setCursorPosition(row, column);
2419};
2420
2421/**
2422 * Set the cursor column.
2423 *
Joel Hockey0f933582019-08-27 18:01:51 -07002424 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002425 */
2426hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002427 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002428};
2429
2430/**
2431 * Return the cursor column.
2432 *
Joel Hockey0f933582019-08-27 18:01:51 -07002433 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002434 */
2435hterm.Terminal.prototype.getCursorColumn = function() {
2436 return this.screen_.cursorPosition.column;
2437};
2438
2439/**
2440 * Set the cursor row.
2441 *
2442 * The cursor row is relative to the scroll region if the terminal has
2443 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2444 *
Joel Hockey0f933582019-08-27 18:01:51 -07002445 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002446 */
rginda87b86462011-12-14 13:48:03 -08002447hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2448 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002449};
2450
2451/**
2452 * Return the cursor row.
2453 *
Joel Hockey0f933582019-08-27 18:01:51 -07002454 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002455 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002456hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002457 return this.screen_.cursorPosition.row;
2458};
2459
2460/**
2461 * Request that the ScrollPort redraw itself soon.
2462 *
2463 * The redraw will happen asynchronously, soon after the call stack winds down.
2464 * Multiple calls will be coalesced into a single redraw.
2465 */
2466hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002467 if (this.timeouts_.redraw)
2468 return;
rginda8ba33642011-12-14 12:31:31 -08002469
2470 var self = this;
rginda87b86462011-12-14 13:48:03 -08002471 this.timeouts_.redraw = setTimeout(function() {
2472 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002473 self.scrollPort_.redraw_();
2474 }, 0);
2475};
2476
2477/**
2478 * Request that the ScrollPort be scrolled to the bottom.
2479 *
2480 * The scroll will happen asynchronously, soon after the call stack winds down.
2481 * Multiple calls will be coalesced into a single scroll.
2482 *
2483 * This affects the scrollbar position of the ScrollPort, and has nothing to
2484 * do with the VT scroll commands.
2485 */
2486hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2487 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002488 return;
rginda8ba33642011-12-14 12:31:31 -08002489
2490 var self = this;
2491 this.timeouts_.scrollDown = setTimeout(function() {
2492 delete self.timeouts_.scrollDown;
2493 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2494 }, 10);
2495};
2496
2497/**
2498 * Move the cursor up a specified number of rows.
2499 *
Joel Hockey0f933582019-08-27 18:01:51 -07002500 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002501 */
2502hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002503 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002504};
2505
2506/**
2507 * Move the cursor down a specified number of rows.
2508 *
Joel Hockey0f933582019-08-27 18:01:51 -07002509 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002510 */
2511hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002512 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002513 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2514 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2515 this.screenSize.height - 1);
2516
rgindacbbd7482012-06-13 15:06:16 -07002517 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002518 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002519 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002520};
2521
2522/**
2523 * Move the cursor left a specified number of columns.
2524 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002525 * If reverse wraparound mode is enabled and the previous row wrapped into
2526 * the current row then we back up through the wraparound as well.
2527 *
Joel Hockey0f933582019-08-27 18:01:51 -07002528 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002529 */
2530hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002531 count = count || 1;
2532
2533 if (count < 1)
2534 return;
2535
2536 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002537 if (this.options_.reverseWraparound) {
2538 if (this.screen_.cursorPosition.overflow) {
2539 // If this cursor is in the right margin, consume one count to get it
2540 // back to the last column. This only applies when we're in reverse
2541 // wraparound mode.
2542 count--;
2543 this.clearCursorOverflow();
2544
2545 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002546 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002547 }
2548
Robert Gindabfb32622014-07-17 13:20:27 -07002549 var newRow = this.screen_.cursorPosition.row;
2550 var newColumn = currentColumn - count;
2551 if (newColumn < 0) {
2552 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2553 if (newRow < 0) {
2554 // xterm also wraps from row 0 to the last row.
2555 newRow = this.screenSize.height + newRow % this.screenSize.height;
2556 }
2557 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2558 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002559
Robert Gindabfb32622014-07-17 13:20:27 -07002560 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2561
2562 } else {
2563 var newColumn = Math.max(currentColumn - count, 0);
2564 this.setCursorColumn(newColumn);
2565 }
rginda8ba33642011-12-14 12:31:31 -08002566};
2567
2568/**
2569 * Move the cursor right a specified number of columns.
2570 *
Joel Hockey0f933582019-08-27 18:01:51 -07002571 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002572 */
2573hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002574 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002575
2576 if (count < 1)
2577 return;
2578
rgindacbbd7482012-06-13 15:06:16 -07002579 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002580 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002581 this.setCursorColumn(column);
2582};
2583
2584/**
2585 * Reverse the foreground and background colors of the terminal.
2586 *
2587 * This only affects text that was drawn with no attributes.
2588 *
2589 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2590 * been drawn with attributes that happen to coincide with the default
2591 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002592 *
2593 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002594 */
2595hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002596 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002597 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002598 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2599 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002600 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002601 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2602 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002603 }
2604};
2605
2606/**
rginda87b86462011-12-14 13:48:03 -08002607 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002608 *
2609 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002610 */
2611hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002612 this.cursorNode_.style.backgroundColor =
2613 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002614
2615 var self = this;
2616 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002617 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002618 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002619
Michael Kelly485ecd12014-06-09 11:41:56 -04002620 // bellSquelchTimeout_ affects both audio and notification bells.
2621 if (this.bellSquelchTimeout_)
2622 return;
2623
Robert Ginda92e18102013-03-14 13:56:37 -07002624 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002625 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002626 this.bellSequelchTimeout_ = setTimeout(function() {
2627 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002628 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002629 } else {
2630 delete this.bellSquelchTimeout_;
2631 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002632
2633 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002634 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002635 this.bellNotificationList_.push(n);
2636 // TODO: Should we try to raise the window here?
2637 n.onclick = function() { self.closeBellNotifications_(); };
2638 }
rginda87b86462011-12-14 13:48:03 -08002639};
2640
2641/**
rginda8ba33642011-12-14 12:31:31 -08002642 * Set the origin mode bit.
2643 *
2644 * If origin mode is on, certain VT cursor and scrolling commands measure their
2645 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2646 * to the top of the addressable screen.
2647 *
2648 * Defaults to off.
2649 *
2650 * @param {boolean} state True to set origin mode, false to unset.
2651 */
2652hterm.Terminal.prototype.setOriginMode = function(state) {
2653 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002654 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002655};
2656
2657/**
2658 * Set the insert mode bit.
2659 *
2660 * If insert mode is on, existing text beyond the cursor position will be
2661 * shifted right to make room for new text. Otherwise, new text overwrites
2662 * any existing text.
2663 *
2664 * Defaults to off.
2665 *
2666 * @param {boolean} state True to set insert mode, false to unset.
2667 */
2668hterm.Terminal.prototype.setInsertMode = function(state) {
2669 this.options_.insertMode = state;
2670};
2671
2672/**
rginda87b86462011-12-14 13:48:03 -08002673 * Set the auto carriage return bit.
2674 *
2675 * If auto carriage return is on then a formfeed character is interpreted
2676 * as a newline, otherwise it's the same as a linefeed. The difference boils
2677 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002678 *
2679 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002680 */
2681hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2682 this.options_.autoCarriageReturn = state;
2683};
2684
2685/**
rginda8ba33642011-12-14 12:31:31 -08002686 * Set the wraparound mode bit.
2687 *
2688 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2689 * to the start of the following row. Otherwise, the cursor is clamped to the
2690 * end of the screen and attempts to write past it are ignored.
2691 *
2692 * Defaults to on.
2693 *
2694 * @param {boolean} state True to set wraparound mode, false to unset.
2695 */
2696hterm.Terminal.prototype.setWraparound = function(state) {
2697 this.options_.wraparound = state;
2698};
2699
2700/**
2701 * Set the reverse-wraparound mode bit.
2702 *
2703 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2704 * to the end of the previous row. Otherwise, the cursor is clamped to column
2705 * 0.
2706 *
2707 * Defaults to off.
2708 *
2709 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2710 */
2711hterm.Terminal.prototype.setReverseWraparound = function(state) {
2712 this.options_.reverseWraparound = state;
2713};
2714
2715/**
2716 * Selects between the primary and alternate screens.
2717 *
2718 * If alternate mode is on, the alternate screen is active. Otherwise the
2719 * primary screen is active.
2720 *
2721 * Swapping screens has no effect on the scrollback buffer.
2722 *
2723 * Each screen maintains its own cursor position.
2724 *
2725 * Defaults to off.
2726 *
2727 * @param {boolean} state True to set alternate mode, false to unset.
2728 */
2729hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002730 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002731 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2732
rginda35c456b2012-02-09 17:29:05 -08002733 if (this.screen_.rowsArray.length &&
2734 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2735 // If the screen changed sizes while we were away, our rowIndexes may
2736 // be incorrect.
2737 var offset = this.scrollbackRows_.length;
2738 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002739 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002740 ary[i].rowIndex = offset + i;
2741 }
2742 }
rginda8ba33642011-12-14 12:31:31 -08002743
rginda35c456b2012-02-09 17:29:05 -08002744 this.realizeWidth_(this.screenSize.width);
2745 this.realizeHeight_(this.screenSize.height);
2746 this.scrollPort_.syncScrollHeight();
2747 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002748
rginda6d397402012-01-17 10:58:29 -08002749 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002750 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002751};
2752
2753/**
2754 * Set the cursor-blink mode bit.
2755 *
2756 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2757 * a visible cursor does not blink.
2758 *
2759 * You should make sure to turn blinking off if you're going to dispose of a
2760 * terminal, otherwise you'll leak a timeout.
2761 *
2762 * Defaults to on.
2763 *
2764 * @param {boolean} state True to set cursor-blink mode, false to unset.
2765 */
2766hterm.Terminal.prototype.setCursorBlink = function(state) {
2767 this.options_.cursorBlink = state;
2768
2769 if (!state && this.timeouts_.cursorBlink) {
2770 clearTimeout(this.timeouts_.cursorBlink);
2771 delete this.timeouts_.cursorBlink;
2772 }
2773
2774 if (this.options_.cursorVisible)
2775 this.setCursorVisible(true);
2776};
2777
2778/**
2779 * Set the cursor-visible mode bit.
2780 *
2781 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2782 *
2783 * Defaults to on.
2784 *
2785 * @param {boolean} state True to set cursor-visible mode, false to unset.
2786 */
2787hterm.Terminal.prototype.setCursorVisible = function(state) {
2788 this.options_.cursorVisible = state;
2789
2790 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002791 if (this.timeouts_.cursorBlink) {
2792 clearTimeout(this.timeouts_.cursorBlink);
2793 delete this.timeouts_.cursorBlink;
2794 }
rginda87b86462011-12-14 13:48:03 -08002795 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002796 return;
2797 }
2798
rginda87b86462011-12-14 13:48:03 -08002799 this.syncCursorPosition_();
2800
2801 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002802
2803 if (this.options_.cursorBlink) {
2804 if (this.timeouts_.cursorBlink)
2805 return;
2806
Robert Gindaea2183e2014-07-17 09:51:51 -07002807 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002808 } else {
2809 if (this.timeouts_.cursorBlink) {
2810 clearTimeout(this.timeouts_.cursorBlink);
2811 delete this.timeouts_.cursorBlink;
2812 }
2813 }
2814};
2815
2816/**
rginda87b86462011-12-14 13:48:03 -08002817 * Synchronizes the visible cursor and document selection with the current
2818 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002819 *
2820 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002821 */
2822hterm.Terminal.prototype.syncCursorPosition_ = function() {
2823 var topRowIndex = this.scrollPort_.getTopRowIndex();
2824 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2825 var cursorRowIndex = this.scrollbackRows_.length +
2826 this.screen_.cursorPosition.row;
2827
Raymes Khoury15697f42018-07-17 11:37:18 +10002828 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002829 if (this.accessibilityReader_.accessibilityEnabled) {
2830 // Report the new position of the cursor for accessibility purposes.
2831 const cursorColumnIndex = this.screen_.cursorPosition.column;
2832 const cursorLineText =
2833 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002834 // This will force the selection to be sync'd to the cursor position if the
2835 // user has pressed a key. Generally we would only sync the cursor position
2836 // when selection is collapsed so that if the user has selected something
2837 // we don't clear the selection by moving the selection. However when a
2838 // screen reader is used, it's intuitive for entering a key to move the
2839 // selection to the cursor.
2840 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002841 this.accessibilityReader_.afterCursorChange(
2842 cursorLineText, cursorRowIndex, cursorColumnIndex);
2843 }
2844
rginda8ba33642011-12-14 12:31:31 -08002845 if (cursorRowIndex > bottomRowIndex) {
2846 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002847 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002848 return false;
rginda8ba33642011-12-14 12:31:31 -08002849 }
2850
Robert Gindab837c052014-08-11 11:17:51 -07002851 if (this.options_.cursorVisible &&
2852 this.cursorNode_.style.display == 'none') {
2853 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2854 this.cursorNode_.style.display = '';
2855 }
2856
Mike Frysinger44c32202017-08-05 01:13:09 -04002857 // Position the cursor using CSS variable math. If we do the math in JS,
2858 // the float math will end up being more precise than the CSS which will
2859 // cause the cursor tracking to be off.
2860 this.setCssVar(
2861 'cursor-offset-row',
2862 `${cursorRowIndex - topRowIndex} + ` +
2863 `${this.scrollPort_.visibleRowTopMargin}px`);
2864 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002865
2866 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002867 '(' + this.screen_.cursorPosition.column +
2868 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002869 ')');
2870
2871 // Update the caret for a11y purposes.
2872 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002873 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002874 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002875 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002876 return true;
rginda8ba33642011-12-14 12:31:31 -08002877};
2878
Robert Gindafb1be6a2013-12-11 11:56:22 -08002879/**
2880 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2881 * and character cell dimensions.
2882 */
Robert Ginda830583c2013-08-07 13:20:46 -07002883hterm.Terminal.prototype.restyleCursor_ = function() {
2884 var shape = this.cursorShape_;
2885
2886 if (this.cursorNode_.getAttribute('focus') == 'false') {
2887 // Always show a block cursor when unfocused.
2888 shape = hterm.Terminal.cursorShape.BLOCK;
2889 }
2890
2891 var style = this.cursorNode_.style;
2892
2893 switch (shape) {
2894 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002895 style.backgroundColor = 'transparent';
2896 style.borderBottomStyle = null;
2897 style.borderLeftStyle = 'solid';
2898 break;
2899
2900 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002901 style.backgroundColor = 'transparent';
2902 style.borderBottomStyle = 'solid';
Robert Ginda830583c2013-08-07 13:20:46 -07002903 style.borderLeftStyle = null;
2904 break;
2905
2906 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002907 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002908 style.borderBottomStyle = null;
2909 style.borderLeftStyle = null;
2910 break;
2911 }
2912};
2913
rginda8ba33642011-12-14 12:31:31 -08002914/**
2915 * Synchronizes the visible cursor with the current cursor coordinates.
2916 *
2917 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002918 * Multiple calls will be coalesced into a single sync. This should be called
2919 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002920 */
2921hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2922 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002923 return;
rginda8ba33642011-12-14 12:31:31 -08002924
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002925 if (this.accessibilityReader_.accessibilityEnabled) {
2926 // Report the previous position of the cursor for accessibility purposes.
2927 const cursorRowIndex = this.scrollbackRows_.length +
2928 this.screen_.cursorPosition.row;
2929 const cursorColumnIndex = this.screen_.cursorPosition.column;
2930 const cursorLineText =
2931 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2932 this.accessibilityReader_.beforeCursorChange(
2933 cursorLineText, cursorRowIndex, cursorColumnIndex);
2934 }
2935
rginda8ba33642011-12-14 12:31:31 -08002936 var self = this;
2937 this.timeouts_.syncCursor = setTimeout(function() {
2938 self.syncCursorPosition_();
2939 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002940 }, 0);
2941};
2942
rgindacc2996c2012-02-24 14:59:31 -08002943/**
rgindaf522ce02012-04-17 17:49:17 -07002944 * Show or hide the zoom warning.
2945 *
2946 * The zoom warning is a message warning the user that their browser zoom must
2947 * be set to 100% in order for hterm to function properly.
2948 *
2949 * @param {boolean} state True to show the message, false to hide it.
2950 */
2951hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2952 if (!this.zoomWarningNode_) {
2953 if (!state)
2954 return;
2955
2956 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002957 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002958 this.zoomWarningNode_.style.cssText = (
2959 'color: black;' +
2960 'background-color: #ff2222;' +
2961 'font-size: large;' +
2962 'border-radius: 8px;' +
2963 'opacity: 0.75;' +
2964 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2965 'top: 0.5em;' +
2966 'right: 1.2em;' +
2967 'position: absolute;' +
2968 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002969 '-webkit-user-select: none;' +
2970 '-moz-text-size-adjust: none;' +
2971 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002972
2973 this.zoomWarningNode_.addEventListener('click', function(e) {
2974 this.parentNode.removeChild(this);
2975 });
rgindaf522ce02012-04-17 17:49:17 -07002976 }
2977
Mike Frysingerb7289952019-03-23 16:05:38 -07002978 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002979 hterm.zoomWarningMessage,
2980 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2981
rgindaf522ce02012-04-17 17:49:17 -07002982 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2983
2984 if (state) {
2985 if (!this.zoomWarningNode_.parentNode)
2986 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2987 } else if (this.zoomWarningNode_.parentNode) {
2988 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2989 }
2990};
2991
2992/**
rgindacc2996c2012-02-24 14:59:31 -08002993 * Show the terminal overlay for a given amount of time.
2994 *
2995 * The terminal overlay appears in inverse video in a large font, centered
2996 * over the terminal. You should probably keep the overlay message brief,
2997 * since it's in a large font and you probably aren't going to check the size
2998 * of the terminal first.
2999 *
3000 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003001 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003002 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3003 * stay up forever (or until the next overlay).
3004 */
3005hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003006 if (!this.overlayNode_) {
3007 if (!this.div_)
3008 return;
3009
3010 this.overlayNode_ = this.document_.createElement('div');
3011 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003012 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003013 'font-size: xx-large;' +
3014 'opacity: 0.75;' +
3015 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3016 'position: absolute;' +
3017 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003018 '-webkit-transition: opacity 180ms ease-in;' +
3019 '-moz-user-select: none;' +
3020 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003021
3022 this.overlayNode_.addEventListener('mousedown', function(e) {
3023 e.preventDefault();
3024 e.stopPropagation();
3025 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003026 }
3027
rginda9f5222b2012-03-05 11:53:28 -08003028 this.overlayNode_.style.color = this.prefs_.get('background-color');
3029 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3030 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3031
rgindaf0090c92012-02-10 14:58:52 -08003032 this.overlayNode_.textContent = msg;
3033 this.overlayNode_.style.opacity = '0.75';
3034
3035 if (!this.overlayNode_.parentNode)
3036 this.div_.appendChild(this.overlayNode_);
3037
Robert Ginda97769282013-02-01 15:30:30 -08003038 var divSize = hterm.getClientSize(this.div_);
3039 var overlaySize = hterm.getClientSize(this.overlayNode_);
3040
Robert Ginda8a59f762014-07-23 11:29:55 -07003041 this.overlayNode_.style.top =
3042 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003043 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003044 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003045
rgindaf0090c92012-02-10 14:58:52 -08003046 if (this.overlayTimeout_)
3047 clearTimeout(this.overlayTimeout_);
3048
Raymes Khouryc7a06382018-07-04 10:25:45 +10003049 this.accessibilityReader_.assertiveAnnounce(msg);
3050
rgindacc2996c2012-02-24 14:59:31 -08003051 if (opt_timeout === null)
3052 return;
3053
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003054 this.overlayTimeout_ = setTimeout(() => {
3055 this.overlayNode_.style.opacity = '0';
3056 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3057 }, opt_timeout || 1500);
3058};
3059
3060/**
3061 * Hide the terminal overlay immediately.
3062 *
3063 * Useful when we show an overlay for an event with an unknown end time.
3064 */
3065hterm.Terminal.prototype.hideOverlay = function() {
3066 if (this.overlayTimeout_)
3067 clearTimeout(this.overlayTimeout_);
3068 this.overlayTimeout_ = null;
3069
3070 if (this.overlayNode_.parentNode)
3071 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3072 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003073};
3074
rginda4bba5e12012-06-20 16:15:30 -07003075/**
3076 * Paste from the system clipboard to the terminal.
Joel Hockey0f933582019-08-27 18:01:51 -07003077 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003078 */
3079hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003080 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003081};
3082
3083/**
3084 * Copy a string to the system clipboard.
3085 *
3086 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003087 *
3088 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003089 */
3090hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003091 if (this.prefs_.get('enable-clipboard-notice'))
3092 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3093
Mike Frysinger96eacae2019-01-02 18:13:56 -05003094 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003095};
3096
Evan Jones2600d4f2016-12-06 09:29:36 -05003097/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003098 * Display an image.
3099 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003100 * Either URI or buffer or blob fields must be specified.
3101 *
Joel Hockey0f933582019-08-27 18:01:51 -07003102 * @param {{
3103 * name: (string|undefined),
3104 * size: (string|number|undefined),
3105 * preserveAspectRation: (boolean|undefined),
3106 * inline: (boolean|undefined),
3107 * width: (string|number|undefined),
3108 * height: (string|number|undefined),
3109 * align: (string|undefined),
3110 * url: (string|undefined),
3111 * buffer: (!ArrayBuffer|undefined),
3112 * blob: (!Blob|undefined),
3113 * type: (string|undefined),
3114 * }} options The image to display.
3115 * name A human readable string for the image
3116 * size The size (in bytes).
3117 * preserveAspectRatio Whether to preserve aspect.
3118 * inline Whether to display the image inline.
3119 * width The width of the image.
3120 * height The height of the image.
3121 * align Direction to align the image.
3122 * uri The source URI for the image.
3123 * buffer The ArrayBuffer image data.
3124 * blob The Blob image data.
3125 * type The MIME type of the image data.
3126 * @param {function()=} onLoad Callback when loading finishes.
3127 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003128 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003129hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003130 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003131 if (options.uri === undefined && options.buffer === undefined &&
3132 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003133 return;
3134
3135 // Set up the defaults to simplify code below.
3136 if (!options.name)
3137 options.name = '';
3138
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003139 // See if the mime type is available. If not, guess from the filename.
3140 // We don't list all possible mime types because the browser can usually
3141 // guess it correctly. So list the ones that need a bit more help.
3142 if (!options.type) {
3143 const ary = options.name.split('.');
3144 const ext = ary[ary.length - 1].trim();
3145 switch (ext) {
3146 case 'svg':
3147 case 'svgz':
3148 options.type = 'image/svg+xml';
3149 break;
3150 }
3151 }
3152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003153 // Has the user approved image display yet?
3154 if (this.allowImagesInline !== true) {
3155 this.newLine();
3156 const row = this.getRowNode(this.scrollbackRows_.length +
3157 this.getCursorRow() - 1);
3158
3159 if (this.allowImagesInline === false) {
3160 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3161 'Inline Images Disabled');
3162 return;
3163 }
3164
3165 // Show a prompt.
3166 let button;
3167 const span = this.document_.createElement('span');
3168 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3169 span.style.fontWeight = 'bold';
3170 span.style.borderWidth = '1px';
3171 span.style.borderStyle = 'dashed';
3172 button = this.document_.createElement('span');
3173 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3174 button.style.marginLeft = '1em';
3175 button.style.borderWidth = '1px';
3176 button.style.borderStyle = 'solid';
3177 button.addEventListener('click', () => {
3178 this.prefs_.set('allow-images-inline', false);
3179 });
3180 span.appendChild(button);
3181 button = this.document_.createElement('span');
3182 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3183 'allow this session');
3184 button.style.marginLeft = '1em';
3185 button.style.borderWidth = '1px';
3186 button.style.borderStyle = 'solid';
3187 button.addEventListener('click', () => {
3188 this.allowImagesInline = true;
3189 });
3190 span.appendChild(button);
3191 button = this.document_.createElement('span');
3192 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3193 button.style.marginLeft = '1em';
3194 button.style.borderWidth = '1px';
3195 button.style.borderStyle = 'solid';
3196 button.addEventListener('click', () => {
3197 this.prefs_.set('allow-images-inline', true);
3198 });
3199 span.appendChild(button);
3200
3201 row.appendChild(span);
3202 return;
3203 }
3204
3205 // See if we should show this object directly, or download it.
3206 if (options.inline) {
3207 const io = this.io.push();
3208 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3209 'Loading $1 ...'), null);
3210
3211 // While we're loading the image, eat all the user's input.
3212 io.onVTKeystroke = io.sendString = () => {};
3213
3214 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003215 const img =
Joel Hockey0f933582019-08-27 18:01:51 -07003216 /** @type {!ImageElement} */ (this.document_.createElement('img'));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003217 if (options.uri !== undefined) {
3218 img.src = options.uri;
3219 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003220 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003221 img.src = URL.createObjectURL(blob);
3222 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003223 const blob = new Blob([options.blob], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003224 img.src = URL.createObjectURL(options.blob);
3225 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003226 img.title = img.alt = options.name;
3227
3228 // Attach the image to the page to let it load/render. It won't stay here.
3229 // This is needed so it's visible and the DOM can calculate the height. If
3230 // the image is hidden or not in the DOM, the height is always 0.
3231 this.document_.body.appendChild(img);
3232
3233 // Wait for the image to finish loading before we try moving it to the
3234 // right place in the terminal.
3235 img.onload = () => {
3236 // Now that we have the image dimensions, figure out how to show it.
3237 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3238 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3239 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3240
3241 // Parse a width/height specification.
3242 const parseDim = (dim, maxDim, cssVar) => {
3243 if (!dim || dim == 'auto')
3244 return '';
3245
3246 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3247 if (ary) {
3248 if (ary[2] == '%')
3249 return maxDim * parseInt(ary[1]) / 100 + 'px';
3250 else if (ary[2] == 'px')
3251 return dim;
3252 else
3253 return `calc(${dim} * var(${cssVar}))`;
3254 }
3255
3256 return '';
3257 };
3258 img.style.width =
3259 parseDim(options.width, this.document_.body.clientWidth,
3260 '--hterm-charsize-width');
3261 img.style.height =
3262 parseDim(options.height, this.document_.body.clientHeight,
3263 '--hterm-charsize-height');
3264
3265 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003266 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003267 const padRows = Math.ceil(img.clientHeight /
3268 this.scrollPort_.characterSize.height);
3269 for (let i = 0; i < padRows; ++i)
3270 this.newLine();
3271
3272 // Update the max height in case the user shrinks the character size.
3273 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3274
3275 // Move the image to the last row. This way when we scroll up, it doesn't
3276 // disappear when the first row gets clipped. It will disappear when we
3277 // scroll down and the last row is clipped ...
3278 this.document_.body.removeChild(img);
3279 // Create a wrapper node so we can do an absolute in a relative position.
3280 // This helps with rounding errors between JS & CSS counts.
3281 const div = this.document_.createElement('div');
3282 div.style.position = 'relative';
3283 div.style.textAlign = options.align;
3284 img.style.position = 'absolute';
3285 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3286 div.appendChild(img);
3287 const row = this.getRowNode(this.scrollbackRows_.length +
3288 this.getCursorRow() - 1);
3289 row.appendChild(div);
3290
Mike Frysinger2558ed52019-01-14 01:03:41 -05003291 // Now that the image has been read, we can revoke the source.
3292 if (options.uri === undefined) {
3293 URL.revokeObjectURL(img.src);
3294 }
3295
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003296 io.hideOverlay();
3297 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003298
3299 if (onLoad)
3300 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003301 };
3302
3303 // If we got a malformed image, give up.
3304 img.onerror = (e) => {
3305 this.document_.body.removeChild(img);
3306 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003307 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003308 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003309
3310 if (onError)
3311 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003312 };
3313 } else {
3314 // We can't use chrome.downloads.download as that requires "downloads"
3315 // permissions, and that works only in extensions, not apps.
3316 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003317 if (options.uri !== undefined) {
3318 a.href = options.uri;
3319 } else if (options.buffer !== undefined) {
3320 const blob = new Blob([options.buffer]);
3321 a.href = URL.createObjectURL(blob);
3322 } else {
3323 a.href = URL.createObjectURL(options.blob);
3324 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003325 a.download = options.name;
3326 this.document_.body.appendChild(a);
3327 a.click();
3328 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003329 if (options.uri === undefined) {
3330 URL.revokeObjectURL(a.href);
3331 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003332 }
3333};
3334
3335/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003336 * Returns the selected text, or null if no text is selected.
3337 *
3338 * @return {string|null}
3339 */
rgindaa09e7332012-08-17 12:49:51 -07003340hterm.Terminal.prototype.getSelectionText = function() {
3341 var selection = this.scrollPort_.selection;
3342 selection.sync();
3343
3344 if (selection.isCollapsed)
3345 return null;
3346
rgindaa09e7332012-08-17 12:49:51 -07003347 // Start offset measures from the beginning of the line.
3348 var startOffset = selection.startOffset;
3349 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003350
Raymes Khoury334625a2018-06-25 10:29:40 +10003351 // If an x-row isn't selected, |node| will be null.
3352 if (!node)
3353 return null;
3354
Robert Gindafdbb3f22012-09-06 20:23:06 -07003355 if (node.nodeName != 'X-ROW') {
3356 // If the selection doesn't start on an x-row node, then it must be
3357 // somewhere inside the x-row. Add any characters from previous siblings
3358 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003359
3360 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3361 // If node is the text node in a styled span, move up to the span node.
3362 node = node.parentNode;
3363 }
3364
Robert Gindafdbb3f22012-09-06 20:23:06 -07003365 while (node.previousSibling) {
3366 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003367 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003368 }
rgindaa09e7332012-08-17 12:49:51 -07003369 }
3370
3371 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003372 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3373 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003374 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003375
Robert Gindafdbb3f22012-09-06 20:23:06 -07003376 if (node.nodeName != 'X-ROW') {
3377 // If the selection doesn't end on an x-row node, then it must be
3378 // somewhere inside the x-row. Add any characters from following siblings
3379 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003380
3381 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3382 // If node is the text node in a styled span, move up to the span node.
3383 node = node.parentNode;
3384 }
3385
Robert Gindafdbb3f22012-09-06 20:23:06 -07003386 while (node.nextSibling) {
3387 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003388 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003389 }
rgindaa09e7332012-08-17 12:49:51 -07003390 }
3391
3392 var rv = this.getRowsText(selection.startRow.rowIndex,
3393 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003394 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003395};
3396
rginda4bba5e12012-06-20 16:15:30 -07003397/**
3398 * Copy the current selection to the system clipboard, then clear it after a
3399 * short delay.
3400 */
3401hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003402 var text = this.getSelectionText();
3403 if (text != null)
3404 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003405};
3406
Joel Hockey0f933582019-08-27 18:01:51 -07003407/**
3408 * Show overlay with current terminal size.
3409 */
rgindaf0090c92012-02-10 14:58:52 -08003410hterm.Terminal.prototype.overlaySize = function() {
3411 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3412};
3413
rginda87b86462011-12-14 13:48:03 -08003414/**
3415 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3416 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003417 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003418 */
3419hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003420 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003421 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3422
Mike Frysinger79669762018-12-30 20:51:10 -05003423 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003424};
3425
3426/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003427 * Open the selected url.
3428 */
3429hterm.Terminal.prototype.openSelectedUrl_ = function() {
3430 var str = this.getSelectionText();
3431
3432 // If there is no selection, try and expand wherever they clicked.
3433 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003434 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003435 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003436
3437 // If clicking in empty space, return.
3438 if (str == null)
3439 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003440 }
3441
3442 // Make sure URL is valid before opening.
3443 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3444 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003445
3446 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003447 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003448 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3449 // We have to whitelist a few protocols that lack authorities and thus
3450 // never use the //. Like mailto.
3451 switch (str.split(':', 1)[0]) {
3452 case 'mailto':
3453 break;
3454 default:
3455 str = 'http://' + str;
3456 break;
3457 }
3458 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003459
Mike Frysinger720fa832017-10-23 01:15:52 -04003460 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003461};
Mike Frysinger70b94692017-01-26 18:57:50 -10003462
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003463/**
3464 * Manage the automatic mouse hiding behavior while typing.
3465 *
3466 * @param {boolean=} v Whether to enable automatic hiding.
3467 */
3468hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3469 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3470 // Linux & Windows seem to leave this to specific applications to manage.
3471 if (v === null)
3472 v = (hterm.os != 'cros' && hterm.os != 'mac');
3473
3474 this.mouseHideWhileTyping_ = !!v;
3475};
3476
3477/**
3478 * Handler for monitoring user keyboard activity.
3479 *
3480 * This isn't for processing the keystrokes directly, but for updating any
3481 * state that might toggle based on the user using the keyboard at all.
3482 *
Joel Hockey0f933582019-08-27 18:01:51 -07003483 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003484 */
3485hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3486 // When the user starts typing, hide the mouse cursor.
3487 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3488 this.setCssVar('mouse-cursor-style', 'none');
3489};
Mike Frysinger70b94692017-01-26 18:57:50 -10003490
3491/**
rgindad5613292012-06-19 15:40:37 -07003492 * Add the terminalRow and terminalColumn properties to mouse events and
3493 * then forward on to onMouse().
3494 *
3495 * The terminalRow and terminalColumn properties contain the (row, column)
3496 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003497 *
Joel Hockey0f933582019-08-27 18:01:51 -07003498 * @param {!Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003499 */
3500hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003501 if (e.processedByTerminalHandler_) {
3502 // We register our event handlers on the document, as well as the cursor
3503 // and the scroll blocker. Mouse events that occur on the cursor or
3504 // scroll blocker will also appear on the document, but we don't want to
3505 // process them twice.
3506 //
3507 // We can't just prevent bubbling because that has other side effects, so
3508 // we decorate the event object with this property instead.
3509 return;
3510 }
3511
Mike Frysinger468966c2018-08-28 13:48:51 -04003512 // Consume navigation events. Button 3 is usually "browser back" and
3513 // button 4 is "browser forward" which we don't want to happen.
3514 if (e.button > 2) {
3515 e.preventDefault();
3516 // We don't return so click events can be passed to the remote below.
3517 }
3518
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003519 var reportMouseEvents = (!this.defeatMouseReports_ &&
3520 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3521
rgindafaa74742012-08-21 13:34:03 -07003522 e.processedByTerminalHandler_ = true;
3523
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003524 // Handle auto hiding of mouse cursor while typing.
3525 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3526 // Make sure the mouse cursor is visible.
3527 this.syncMouseStyle();
3528 // This debounce isn't perfect, but should work well enough for such a
3529 // simple implementation. If the user moved the mouse, we enabled this
3530 // debounce, and then moved the mouse just before the timeout, we wouldn't
3531 // debounce that later movement.
3532 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3533 }
3534
Robert Gindaeda48db2014-07-17 09:25:30 -07003535 // One based row/column stored on the mouse event.
3536 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3537 this.scrollPort_.characterSize.height) + 1;
3538 e.terminalColumn = parseInt(e.clientX /
3539 this.scrollPort_.characterSize.width) + 1;
3540
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003541 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3542 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003543 return;
3544 }
3545
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003546 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003547 // If the cursor is visible and we're not sending mouse events to the
3548 // host app, then we want to hide the terminal cursor when the mouse
3549 // cursor is over top. This keeps the terminal cursor from interfering
3550 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003551 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3552 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3553 this.cursorNode_.style.display = 'none';
3554 } else if (this.cursorNode_.style.display == 'none') {
3555 this.cursorNode_.style.display = '';
3556 }
3557 }
rgindad5613292012-06-19 15:40:37 -07003558
Robert Ginda928cf632014-03-05 15:07:41 -08003559 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003560 this.contextMenu.hide(e);
3561
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003562 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003563 // If VT mouse reporting is disabled, or has been defeated with
3564 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003565 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003566 this.setSelectionEnabled(true);
3567 } else {
3568 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003569 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003570 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003571 this.setSelectionEnabled(false);
3572 e.preventDefault();
3573 }
3574 }
3575
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003576 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003577 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003578 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003579 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003580 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003581 }
3582
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003583 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003584 // Debounce this event with the dblclick event. If you try to doubleclick
3585 // a URL to open it, Chrome will fire click then dblclick, but we won't
3586 // have expanded the selection text at the first click event.
3587 clearTimeout(this.timeouts_.openUrl);
3588 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3589 500);
3590 return;
3591 }
3592
Mike Frysinger847577f2017-05-23 23:25:57 -04003593 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003594 if (e.ctrlKey && e.button == 2 /* right button */) {
3595 e.preventDefault();
3596 this.contextMenu.show(e, this);
3597 } else if (e.button == this.mousePasteButton ||
3598 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003599 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003600 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003601 }
3602 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003603
Mike Frysinger2edd3612017-05-24 00:54:39 -04003604 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003605 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003606 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003607 }
3608
3609 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3610 this.scrollBlockerNode_.engaged) {
3611 // Disengage the scroll-blocker after one of these events.
3612 this.scrollBlockerNode_.engaged = false;
3613 this.scrollBlockerNode_.style.top = '-99px';
3614 }
3615
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003616 // Emulate arrow key presses via scroll wheel events.
3617 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3618 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003619 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003620 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003621
Mike Frysinger321063c2018-08-29 15:33:14 -04003622 // Helper to turn a wheel event delta into a series of key presses.
3623 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3624 if (distance == 0) {
3625 return '';
3626 }
3627
3628 // Convert the scroll distance into a number of rows/cols.
3629 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3630 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3631 return data.repeat(cells);
3632 };
3633
3634 // The order between up/down and left/right doesn't really matter.
3635 this.io.sendString(
3636 // Up/down arrow keys.
3637 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3638 'A', 'B') +
3639 // Left/right arrow keys.
3640 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3641 'C', 'D')
3642 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003643
3644 e.preventDefault();
3645 }
3646 }
Robert Ginda928cf632014-03-05 15:07:41 -08003647 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003648 if (!this.scrollBlockerNode_.engaged) {
3649 if (e.type == 'mousedown') {
3650 // Move the scroll-blocker into place if we want to keep the scrollport
3651 // from scrolling.
3652 this.scrollBlockerNode_.engaged = true;
3653 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3654 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3655 } else if (e.type == 'mousemove') {
3656 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3657 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003658 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003659 e.preventDefault();
3660 }
3661 }
Robert Ginda928cf632014-03-05 15:07:41 -08003662
3663 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003664 }
3665
Robert Ginda928cf632014-03-05 15:07:41 -08003666 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3667 // Restore this on mouseup in case it was temporarily defeated with a
3668 // alt-mousedown. Only do this when the selection is empty so that
3669 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003670 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003671 }
rgindad5613292012-06-19 15:40:37 -07003672};
3673
3674/**
3675 * Clients should override this if they care to know about mouse events.
3676 *
3677 * The event parameter will be a normal DOM mouse click event with additional
3678 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003679 *
Joel Hockey0f933582019-08-27 18:01:51 -07003680 * @param {!Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003681 */
3682hterm.Terminal.prototype.onMouse = function(e) { };
3683
3684/**
rginda8e92a692012-05-20 19:37:20 -07003685 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003686 *
3687 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003688 */
Rob Spies06533ba2014-04-24 11:20:37 -07003689hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3690 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003691 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003692
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003693 if (this.reportFocus)
3694 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003695
Michael Kelly485ecd12014-06-09 11:41:56 -04003696 if (focused === true)
3697 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003698};
3699
3700/**
rginda8ba33642011-12-14 12:31:31 -08003701 * React when the ScrollPort is scrolled.
3702 */
3703hterm.Terminal.prototype.onScroll_ = function() {
3704 this.scheduleSyncCursorPosition_();
3705};
3706
3707/**
rginda9846e2f2012-01-27 13:53:33 -08003708 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003709 *
Joel Hockey0f933582019-08-27 18:01:51 -07003710 * @param {!Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003711 */
3712hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003713 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003714 if (this.options_.bracketedPaste) {
3715 // We strip out most escape sequences as they can cause issues (like
3716 // inserting an \x1b[201~ midstream). We pass through whitespace
3717 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3718 // This matches xterm behavior.
3719 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3720 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3721 }
Robert Gindaa063b202014-07-21 11:08:25 -07003722
3723 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003724};
3725
3726/**
rgindaa09e7332012-08-17 12:49:51 -07003727 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003728 *
Joel Hockey0f933582019-08-27 18:01:51 -07003729 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003730 */
3731hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003732 if (!this.useDefaultWindowCopy) {
3733 e.preventDefault();
3734 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3735 }
rgindaa09e7332012-08-17 12:49:51 -07003736};
3737
3738/**
rginda8ba33642011-12-14 12:31:31 -08003739 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003740 *
3741 * Note: This function should not directly contain code that alters the internal
3742 * state of the terminal. That kind of code belongs in realizeWidth or
3743 * realizeHeight, so that it can be executed synchronously in the case of a
3744 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003745 */
3746hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003747 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003748 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003749 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003750 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003751
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003752 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003753 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003754 // gets removed from the document or during the initial load, and we can't
3755 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003756 // This can also happen if called before the scrollPort calculates the
3757 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003758 return;
3759 }
3760
rgindaa8ba17d2012-08-15 14:41:10 -07003761 var isNewSize = (columnCount != this.screenSize.width ||
3762 rowCount != this.screenSize.height);
3763
3764 // We do this even if the size didn't change, just to be sure everything is
3765 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003766 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003767 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003768
3769 if (isNewSize)
3770 this.overlaySize();
3771
Robert Gindafb1be6a2013-12-11 11:56:22 -08003772 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003773 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003774};
3775
3776/**
3777 * Service the cursor blink timeout.
3778 */
3779hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003780 if (!this.options_.cursorBlink) {
3781 delete this.timeouts_.cursorBlink;
3782 return;
3783 }
3784
Robert Ginda830583c2013-08-07 13:20:46 -07003785 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3786 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003787 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003788 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3789 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003790 } else {
rginda87b86462011-12-14 13:48:03 -08003791 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003792 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3793 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003794 }
3795};
David Reveman8f552492012-03-28 12:18:41 -04003796
3797/**
3798 * Set the scrollbar-visible mode bit.
3799 *
3800 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3801 * Otherwise it will not.
3802 *
3803 * Defaults to on.
3804 *
3805 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3806 */
3807hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3808 this.scrollPort_.setScrollbarVisible(state);
3809};
Michael Kelly485ecd12014-06-09 11:41:56 -04003810
3811/**
Rob Spies49039e52014-12-17 13:40:04 -08003812 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003813 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003814 *
3815 * Defaults to 1.
3816 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003817 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003818 */
3819hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3820 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3821};
3822
3823/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003824 * Close all web notifications created by terminal bells.
3825 */
3826hterm.Terminal.prototype.closeBellNotifications_ = function() {
3827 this.bellNotificationList_.forEach(function(n) {
3828 n.close();
3829 });
3830 this.bellNotificationList_.length = 0;
3831};
Raymes Khourye5d48982018-08-02 09:08:32 +10003832
3833/**
3834 * Syncs the cursor position when the scrollport gains focus.
3835 */
3836hterm.Terminal.prototype.onScrollportFocus_ = function() {
3837 // If the cursor is offscreen we set selection to the last row on the screen.
3838 const topRowIndex = this.scrollPort_.getTopRowIndex();
3839 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3840 const selection = this.document_.getSelection();
3841 if (!this.syncCursorPosition_() && selection) {
3842 selection.collapse(this.getRowNode(bottomRowIndex));
3843 }
3844};