-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhterm_terminal.js
4240 lines (3678 loc) · 125 KB
/
hterm_terminal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {lib} from '../../libdot/index.js';
import {hterm} from '../index.js';
/**
* Constructor for the Terminal class.
*
* A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
* classes to provide the complete terminal functionality.
*
* There are a number of lower-level Terminal methods that can be called
* directly to manipulate the cursor, text, scroll region, and other terminal
* attributes. However, the primary method is interpret(), which parses VT
* escape sequences and invokes the appropriate Terminal methods.
*
* This class was heavily influenced by Cory Maccarrone's Framebuffer class.
*
* TODO(rginda): Eventually we're going to need to support characters which are
* displayed twice as wide as standard latin characters. This is to support
* CJK (and possibly other character sets).
*
* @param {{
* profileId: (?string|undefined),
* storage: (!lib.Storage|undefined),
* }=} options Various settings to control behavior.
* profileId: The preference profile name. Defaults to "default".
* storage: The backing storage for preferences. Defaults to local.
* @constructor
* @implements {hterm.RowProvider}
*/
hterm.Terminal = function({profileId, storage} = {}) {
// Set to true once terminal is initialized and onTerminalReady() is called.
this.ready_ = false;
this.profileId_ = null;
this.storage_ = storage || new lib.Storage.Local();
/** @type {?hterm.PreferenceManager} */
this.prefs_ = null;
// Two screen instances.
this.primaryScreen_ = new hterm.Screen();
this.alternateScreen_ = new hterm.Screen();
// The "current" screen.
this.screen_ = this.primaryScreen_;
// The local notion of the screen size. ScreenBuffers also have a size which
// indicates their present size. During size changes, the two may disagree.
// Also, the inactive screen's size is not altered until it is made the active
// screen.
this.screenSize = new hterm.Size(0, 0);
// The scroll port we'll be using to display the visible rows.
this.scrollPort_ = new hterm.ScrollPort(this);
this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
this.scrollPort_.onCopy = this.onCopy_.bind(this);
// The div that contains this terminal.
this.div_ = null;
// UI for showing info to the user in a privileged way.
this.notifications_ = null;
// The document that contains the scrollPort. Defaulted to the global
// document here so that the terminal is functional even if it hasn't been
// inserted into a document yet, but re-set in decorate().
this.document_ = globalThis.document;
// The rows that have scrolled off screen and are no longer addressable.
this.scrollbackRows_ = [];
// Saved tab stops.
this.tabStops_ = [];
// Keep track of whether default tab stops have been erased; after a TBC
// clears all tab stops, defaults aren't restored on resize until a reset.
this.defaultTabStops = true;
// The VT's notion of the top and bottom rows. Used during some VT
// cursor positioning and scrolling commands.
this.vtScrollTop_ = null;
this.vtScrollBottom_ = null;
// The DIV element for the visible cursor.
this.cursorNode_ = null;
// The current cursor shape of the terminal.
this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
// Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
this.cursorBlinkCycle_ = [100, 100];
// Whether to temporarily disable blinking.
this.cursorBlinkPause_ = false;
// Cursor is hidden when scrolling up pushes it off the bottom of the screen.
this.cursorOffScreen_ = false;
// Pre-bound onCursorBlink_ handler, so we don't have to do this for each
// cursor on/off servicing.
this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
// These prefs are cached so we don't have to read from local storage with
// each output and keystroke. They are initialized by the preference manager.
/** @type {?string} */
this.backgroundColor_ = null;
/** @type {?string} */
this.foregroundColor_ = null;
/** @type {!Map<number, string>} */
this.colorPaletteOverrides_ = new Map();
this.screenBorderSize_ = 0;
this.scrollOnOutput_ = null;
this.scrollOnKeystroke_ = null;
this.scrollWheelArrowKeys_ = null;
// True if we should override mouse event reporting to allow local selection.
this.defeatMouseReports_ = false;
// Whether to auto hide the mouse cursor when typing.
this.setAutomaticMouseHiding();
// Timer to keep mouse visible while it's being used.
this.mouseHideDelay_ = null;
// Terminal bell sound.
this.bellAudio_ = this.document_.createElement('audio');
this.bellAudio_.id = 'hterm:bell-audio';
this.bellAudio_.setAttribute('preload', 'auto');
// The AccessibilityReader object for announcing command output.
this.accessibilityReader_ = null;
// The context menu object.
this.contextMenu = new hterm.ContextMenu();
// All terminal bell notifications that have been generated (not necessarily
// shown).
this.bellNotificationList_ = [];
this.bellSquelchTimeout_ = null;
// Whether we have permission to display notifications.
this.desktopNotificationBell_ = false;
// Cursor position and attributes saved with DECSC.
this.savedOptions_ = {};
// The current mode bits for the terminal.
this.options_ = new hterm.Options();
// Timeouts we might need to clear.
this.timeouts_ = {};
// The VT escape sequence interpreter.
this.vt = new hterm.VT(this);
this.saveCursorAndState(true);
// The keyboard handler.
this.keyboard = new hterm.Keyboard(this);
// General IO interface that can be given to third parties without exposing
// the entire terminal object.
this.io = new hterm.Terminal.IO(this);
// True if mouse-click-drag should scroll the terminal.
this.enableMouseDragScroll = true;
this.copyOnSelect = null;
this.mouseRightClickPaste = null;
this.mousePasteButton = null;
// Whether to use the default window copy behavior.
this.useDefaultWindowCopy = false;
this.clearSelectionAfterCopy = true;
this.realizeSize_(80, 24);
this.setDefaultTabStops();
// Whether we allow images to be shown.
this.allowImagesInline = null;
this.reportFocus = false;
// TODO(crbug.com/1063219) Remove this once the bug is fixed.
this.alwaysUseLegacyPasting = false;
this.setProfile(profileId || hterm.Terminal.DEFAULT_PROFILE_ID, () => {
hterm.initPromise.then(() => this.onTerminalReady());
});
/** @const */
this.findBar = new hterm.FindBar(this);
};
/**
* Default Profile ID.
*
* @const {string}
*/
hterm.Terminal.DEFAULT_PROFILE_ID = 'default';
/**
* Possible cursor shapes.
*/
hterm.Terminal.cursorShape = {
BLOCK: 'BLOCK',
BEAM: 'BEAM',
UNDERLINE: 'UNDERLINE',
};
/**
* Clients should override this to be notified when the terminal is ready
* for use.
*
* The terminal initialization is asynchronous, and shouldn't be used before
* this method is called.
*/
hterm.Terminal.prototype.onTerminalReady = function() { };
/**
* Default tab with of 8 to match xterm.
*/
hterm.Terminal.prototype.tabWidth = 8;
/**
* Select a preference profile.
*
* This will load the terminal preferences for the given profile name and
* associate subsequent preference changes with the new preference profile.
*
* @param {string} profileId The name of the preference profile. Forward slash
* characters will be removed from the name.
* @param {function()=} callback Optional callback to invoke when the
* profile transition is complete.
*/
hterm.Terminal.prototype.setProfile = function(
profileId, callback = undefined) {
profileId = profileId.replace(/\//g, '');
if (this.profileId_ === profileId) {
if (callback) {
callback();
}
return;
}
this.profileId_ = profileId;
if (this.prefs_) {
this.prefs_.setProfile(profileId).then(callback);
return;
}
this.prefs_ = new hterm.PreferenceManager(this.storage_, this.profileId_);
/**
* Clears and reloads key bindings. Used by preferences
* 'keybindings' and 'keybindings-os-defaults'.
*
* @param {*?=} bindings
* @param {*?=} useOsDefaults
*/
const loadKeyBindings = (bindings = null, useOsDefaults = false) => {
this.keyboard.bindings.clear();
// Default to an empty object so we still handle OS defaults.
if (bindings === null) {
bindings = {};
}
if (!(bindings instanceof Object)) {
console.error('Error in keybindings preference: Expected object');
bindings = {};
// Fall through to handle OS defaults.
}
try {
this.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
} catch (ex) {
console.error('Error in keybindings preference: ' + ex);
}
};
this.prefs_.addObservers(null, {
'alt-gr-mode': (v) => {
if (v == null) {
if (navigator.language.toLowerCase() == 'en-us') {
v = 'none';
} else {
v = 'right-alt';
}
} else if (typeof v == 'string') {
v = v.toLowerCase();
} else {
v = 'none';
}
if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
v = 'none';
}
this.keyboard.altGrMode = v;
},
'alt-backspace-is-meta-backspace': (v) => {
this.keyboard.altBackspaceIsMetaBackspace = v;
},
'alt-is-meta': (v) => {
this.keyboard.altIsMeta = v;
},
'alt-sends-what': (v) => {
if (!/^(escape|8-bit|browser-key)$/.test(v)) {
v = 'escape';
}
this.keyboard.altSendsWhat = v;
},
'audible-bell-sound': (v) => {
const ary = v.match(/^lib-resource:(\S+)/);
if (ary) {
const name = ary[1];
if (lib.resource.get(name) === undefined) {
console.warn(`Invalid resource name '${name}'`);
this.prefs_.reset('audible-bell-sound');
return;
}
this.bellAudio_.setAttribute('src', lib.resource.getDataUrl(name));
} else {
this.bellAudio_.setAttribute('src', v);
}
},
'desktop-notification-bell': (v) => {
if (v && Notification) {
this.desktopNotificationBell_ = Notification.permission === 'granted';
if (!this.desktopNotificationBell_) {
// Note: We don't call Notification.requestPermission here because
// Chrome requires the call be the result of a user action (such as an
// onclick handler), and pref listeners are run asynchronously.
//
// A way of working around this would be to display a dialog in the
// terminal with a "click-to-request-permission" button.
console.warn('desktop-notification-bell is true but we do not have ' +
'permission to display notifications.');
}
} else {
this.desktopNotificationBell_ = false;
}
},
'background-color': (v) => {
this.setBackgroundColor(v);
},
'background-image': (v) => {
this.scrollPort_.setBackgroundImage(v);
},
'background-size': (v) => {
this.scrollPort_.setBackgroundSize(v);
},
'background-position': (v) => {
this.scrollPort_.setBackgroundPosition(v);
},
'backspace-sends-backspace': (v) => {
this.keyboard.backspaceSendsBackspace = v;
},
'character-map-overrides': (v) => {
if (!(v == null || v instanceof Object)) {
console.warn('Preference character-map-modifications is not an ' +
'object: ' + v);
return;
}
this.vt.characterMaps.reset();
this.vt.characterMaps.setOverrides(v);
},
'cursor-blink': (v) => {
this.setCursorBlink(!!v);
},
'cursor-shape': (v) => {
this.setCursorShape(v);
},
'cursor-blink-cycle': (v) => {
if (v instanceof Array &&
typeof v[0] == 'number' &&
typeof v[1] == 'number') {
this.cursorBlinkCycle_ = v;
} else if (typeof v == 'number') {
this.cursorBlinkCycle_ = [v, v];
} else {
// Fast blink indicates an error.
this.cursorBlinkCycle_ = [100, 100];
}
},
'cursor-color': (v) => {
this.setCursorColor(v);
},
'color-palette-overrides': (v) => {
if (!(v == null || v instanceof Object || v instanceof Array)) {
console.warn('Preference color-palette-overrides is not an array or ' +
'object: ' + v);
return;
}
// Reset all existing colors first as the new palette override might not
// have the same mappings. If the old one set colors the new one doesn't,
// those old mappings have to get cleared first.
lib.colors.stockPalette.forEach((c, i) => this.setColorPalette(i, c));
this.colorPaletteOverrides_.clear();
if (v) {
for (const key in v) {
const i = parseInt(key, 10);
if (isNaN(i) || i < 0 || i > 255) {
console.log('Invalid value in palette: ' + key + ': ' + v[key]);
continue;
}
if (v[i]) {
const rgb = lib.colors.normalizeCSS(v[i]);
if (rgb) {
this.setColorPalette(i, rgb);
this.colorPaletteOverrides_.set(i, rgb);
}
}
}
}
this.primaryScreen_.textAttributes.colorPaletteOverrides = [];
this.alternateScreen_.textAttributes.colorPaletteOverrides = [];
},
'copy-on-select': (v) => {
this.copyOnSelect = !!v;
},
'use-default-window-copy': (v) => {
this.useDefaultWindowCopy = !!v;
},
'clear-selection-after-copy': (v) => {
this.clearSelectionAfterCopy = !!v;
},
'ctrl-plus-minus-zero-zoom': (v) => {
this.keyboard.ctrlPlusMinusZeroZoom = v;
},
'ctrl-c-copy': (v) => {
this.keyboard.ctrlCCopy = v;
},
'ctrl-v-paste': (v) => {
this.keyboard.ctrlVPaste = v;
this.scrollPort_.setCtrlVPaste(v);
},
'paste-on-drop': (v) => {
this.scrollPort_.setPasteOnDrop(v);
},
'east-asian-ambiguous-as-two-column': (v) => {
hterm.wc.regardCjkAmbiguous = v;
},
'enable-8-bit-control': (v) => {
this.vt.enable8BitControl = !!v;
},
'enable-bold': (v) => {
this.syncBoldSafeState();
},
'enable-bold-as-bright': (v) => {
this.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
this.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
},
'enable-blink': (v) => {
this.setTextBlink(!!v);
},
'enable-clipboard-write': (v) => {
this.vt.enableClipboardWrite = !!v;
},
'enable-dec12': (v) => {
this.vt.enableDec12 = !!v;
},
'enable-csi-j-3': (v) => {
this.vt.enableCsiJ3 = !!v;
},
'find-result-color': (v) => {
this.findBar.setFindResultColor(v);
},
'find-result-selected-color': (v) => {
this.findBar.setFindResultSelectedColor(v);
},
'font-family': (v) => {
this.syncFontFamily();
},
'font-size': (v) => {
v = parseInt(v, 10);
if (isNaN(v) || v <= 0) {
console.error(`Invalid font size: ${v}`);
return;
}
this.setFontSize(v);
},
'font-smoothing': (v) => {
this.syncFontFamily();
},
'foreground-color': (v) => {
this.setForegroundColor(v);
},
'hide-mouse-while-typing': (v) => {
this.setAutomaticMouseHiding(v);
},
'home-keys-scroll': (v) => {
this.keyboard.homeKeysScroll = v;
},
'keybindings': (v) => {
loadKeyBindings(v, this.prefs_.get('keybindings-os-defaults'));
},
'keybindings-os-defaults': (v) => {
loadKeyBindings(this.prefs_.get('keybindings'), v);
},
'line-height-padding-size': (v) => {
v = parseFloat(v);
if (isNaN(v)) {
console.error(`Invalid line height padding size: ${v}`);
return;
}
this.setLineHeightPaddingSize(v);
},
'media-keys-are-fkeys': (v) => {
this.keyboard.mediaKeysAreFKeys = v;
},
'meta-sends-escape': (v) => {
this.keyboard.metaSendsEscape = v;
},
'mouse-right-click-paste': (v) => {
this.mouseRightClickPaste = v;
},
'mouse-paste-button': (v) => {
this.syncMousePasteButton();
},
'page-keys-scroll': (v) => {
this.keyboard.pageKeysScroll = v;
},
'pass-alt-number': (v) => {
if (v == null) {
// Let Alt+1..9 pass to the browser (to control tab switching) on
// non-OS X systems, or if hterm is not opened in an app window.
v = (hterm.os !== 'mac' &&
hterm.windowType !== 'popup' &&
hterm.windowType !== 'app');
}
this.passAltNumber = v;
},
'pass-ctrl-number': (v) => {
if (v == null) {
// Let Ctrl+1..9 pass to the browser (to control tab switching) on
// non-OS X systems, or if hterm is not opened in an app window.
v = (hterm.os !== 'mac' &&
hterm.windowType !== 'popup' &&
hterm.windowType !== 'app');
}
this.passCtrlNumber = v;
},
'pass-ctrl-n': (v) => {
this.passCtrlN = v;
},
'pass-ctrl-t': (v) => {
this.passCtrlT = v;
},
'pass-ctrl-tab': (v) => {
this.passCtrlTab = v;
},
'pass-ctrl-w': (v) => {
this.passCtrlW = v;
},
'pass-meta-number': (v) => {
if (v == null) {
// Let Meta+1..9 pass to the browser (to control tab switching) on
// OS X systems, or if hterm is not opened in an app window.
v = (hterm.os === 'mac' &&
hterm.windowType !== 'popup' &&
hterm.windowType !== 'app');
}
this.passMetaNumber = v;
},
'pass-meta-v': (v) => {
this.keyboard.passMetaV = v;
},
'screen-padding-size': (v) => {
v = parseInt(v, 10);
if (isNaN(v) || v < 0) {
console.error(`Invalid screen padding size: ${v}`);
return;
}
this.setScreenPaddingSize(v);
},
'screen-border-size': (v) => {
v = parseInt(v, 10);
if (isNaN(v) || v < 0) {
console.error(`Invalid screen border size: ${v}`);
return;
}
this.setScreenBorderSize(v);
},
'screen-border-color': (v) => {
this.div_.style.borderColor = v;
},
'scroll-on-keystroke': (v) => {
this.scrollOnKeystroke_ = v;
},
'scroll-on-output': (v) => {
this.scrollOnOutput_ = v;
},
'scrollbar-visible': (v) => {
this.setScrollbarVisible(v);
},
'scroll-wheel-may-send-arrow-keys': (v) => {
this.scrollWheelArrowKeys_ = v;
},
'scroll-wheel-move-multiplier': (v) => {
this.setScrollWheelMoveMultipler(v);
},
'shift-insert-paste': (v) => {
this.keyboard.shiftInsertPaste = v;
},
'terminal-encoding': (v) => {
this.vt.setEncoding(v);
},
'user-css': (v) => {
this.scrollPort_.setUserCssUrl(v);
},
'user-css-text': (v) => {
this.scrollPort_.setUserCssText(v);
},
'word-break-match-left': (v) => {
this.primaryScreen_.wordBreakMatchLeft = v;
this.alternateScreen_.wordBreakMatchLeft = v;
},
'word-break-match-right': (v) => {
this.primaryScreen_.wordBreakMatchRight = v;
this.alternateScreen_.wordBreakMatchRight = v;
},
'word-break-match-middle': (v) => {
this.primaryScreen_.wordBreakMatchMiddle = v;
this.alternateScreen_.wordBreakMatchMiddle = v;
},
'allow-images-inline': (v) => {
this.allowImagesInline = v;
},
});
this.prefs_.readStorage().then(() => {
this.prefs_.notifyAll();
if (callback) {
this.ready_ = true;
// TODO(vapier): Call this immediately. We have to put it into the queue
// so we run after other Terminal events that are event based instead of
// Promise based. Most notably, creating a new Terminal -> decorate ->
// scrollport -> scheduleRedraw -> setTimeout -> redraw_.
setTimeout(() => callback());
}
});
};
/**
* Returns the preferences manager used for configuring this terminal.
*
* @return {!hterm.PreferenceManager}
*/
hterm.Terminal.prototype.getPrefs = function() {
return lib.notNull(this.prefs_);
};
/**
* Enable or disable bracketed paste mode.
*
* @param {boolean} state The value to set.
*/
hterm.Terminal.prototype.setBracketedPaste = function(state) {
this.options_.bracketedPaste = state;
};
/**
* Set the color for the cursor.
*
* If you want this setting to persist, set it through prefs_, rather than
* with this method.
*
* @param {string=} color The color to set. If not defined, we reset to the
* saved user preference.
*/
hterm.Terminal.prototype.setCursorColor = function(color) {
if (color === undefined) {
color = this.prefs_.getString('cursor-color');
}
this.setCssVar('cursor-color', color);
};
/**
* Return the current cursor color as a string.
*
* @return {string}
*/
hterm.Terminal.prototype.getCursorColor = function() {
return this.getCssVar('cursor-color');
};
/**
* Enable or disable mouse based text selection in the terminal.
*
* @param {boolean} state The value to set.
*/
hterm.Terminal.prototype.setSelectionEnabled = function(state) {
this.enableMouseDragScroll = state;
};
/**
* Set the background image.
*
* If you want this setting to persist, set it through prefs_, rather than
* with this method.
*
* @param {string=} cssUrl The image to set as a css url. If not defined, we
* reset to the saved user preference.
*/
hterm.Terminal.prototype.setBackgroundImage = function(cssUrl) {
if (cssUrl === undefined) {
cssUrl = this.prefs_.getString('background-image');
}
this.scrollPort_.setBackgroundImage(cssUrl);
};
/**
* Set the background color.
*
* If you want this setting to persist, set it through prefs_, rather than
* with this method.
*
* @param {string=} color The color to set. If not defined, we reset to the
* saved user preference.
*/
hterm.Terminal.prototype.setBackgroundColor = function(color) {
if (color === undefined) {
color = this.prefs_.getString('background-color');
}
this.backgroundColor_ = lib.colors.normalizeCSS(color);
this.setRgbColorCssVar('background-color', this.backgroundColor_);
};
/**
* Return the current terminal background color.
*
* Intended for use by other classes, so we don't have to expose the entire
* prefs_ object.
*
* @return {?string}
*/
hterm.Terminal.prototype.getBackgroundColor = function() {
return this.backgroundColor_;
};
/**
* Set the foreground color.
*
* If you want this setting to persist, set it through prefs_, rather than
* with this method.
*
* @param {string=} color The color to set. If not defined, we reset to the
* saved user preference.
*/
hterm.Terminal.prototype.setForegroundColor = function(color) {
if (color === undefined) {
color = this.prefs_.getString('foreground-color');
}
this.foregroundColor_ = lib.colors.normalizeCSS(color);
this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
};
/**
* Return the current terminal foreground color.
*
* Intended for use by other classes, so we don't have to expose the entire
* prefs_ object.
*
* @return {?string}
*/
hterm.Terminal.prototype.getForegroundColor = function() {
return this.foregroundColor_;
};
/**
* Returns true if the current screen is the primary screen, false otherwise.
*
* @return {boolean}
*/
hterm.Terminal.prototype.isPrimaryScreen = function() {
return this.screen_ == this.primaryScreen_;
};
/**
* Install the keyboard handler for this terminal.
*
* This will prevent the browser from seeing any keystrokes sent to the
* terminal.
*/
hterm.Terminal.prototype.installKeyboard = function() {
this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
};
/**
* Uninstall the keyboard handler for this terminal.
*/
hterm.Terminal.prototype.uninstallKeyboard = function() {
this.keyboard.installKeyboard(null);
};
/**
* Set a CSS variable.
*
* Normally this is used to set variables in the hterm namespace.
*
* @param {string} name The variable to set.
* @param {string|number} value The value to assign to the variable.
* @param {string=} prefix The variable namespace/prefix to use.
*/
hterm.Terminal.prototype.setCssVar = function(name, value,
prefix = '--hterm-') {
this.document_.documentElement.style.setProperty(
`${prefix}${name}`, value.toString());
};
/**
* Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
* input is valid.
*
* @param {string} name The variable to set.
* @param {?string} rgb The rgb value to assign to the variable.
*/
hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
const ary = rgb ? lib.colors.crackRGB(rgb) : null;
if (ary) {
this.setCssVar(name, ary.slice(0, 3).join(','));
}
};
/**
* Sets the specified color for the active screen.
*
* @param {number} i The index into the 256 color palette to set.
* @param {?string} rgb The rgb value to assign to the variable.
*/
hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
this.setRgbColorCssVar(`color-${i}`, rgb);
this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
}
};
/**
* Returns the current value in the active screen of the specified color.
*
* @param {number} i Color palette index.
* @return {string} rgb color.
*/
hterm.Terminal.prototype.getColorPalette = function(i) {
return this.screen_.textAttributes.colorPaletteOverrides[i] ||
this.colorPaletteOverrides_.get(i) ||
lib.colors.stockPalette[i];
};
/**
* Reset the specified color in the active screen to its default value.
*
* @param {number} i Color to reset
*/
hterm.Terminal.prototype.resetColor = function(i) {
this.setColorPalette(
i, this.colorPaletteOverrides_.get(i) || lib.colors.stockPalette[i]);
delete this.screen_.textAttributes.colorPaletteOverrides[i];
};
/**
* Reset the current screen color palette to the default state.
*/
hterm.Terminal.prototype.resetColorPalette = function() {
this.screen_.textAttributes.colorPaletteOverrides.forEach(
(c, i) => this.resetColor(i));
};
/**
* Get a CSS variable.
*
* Normally this is used to get variables in the hterm namespace.
*
* @param {string} name The variable to read.
* @param {string=} prefix The variable namespace/prefix to use.
* @return {string} The current setting for this variable.
*/
hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
return this.document_.documentElement.style.getPropertyValue(
`${prefix}${name}`);
};
/**
* @return {!hterm.ScrollPort}
*/
hterm.Terminal.prototype.getScrollPort = function() {
return this.scrollPort_;
};
/**
* Update CSS character size variables to match the scrollport.
*/
hterm.Terminal.prototype.updateCssCharsize_ = function() {
this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
this.setCssVar('charsize-height',
this.scrollPort_.characterSize.height + 'px');
};
/**
* Set the font size for this terminal.