-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.js
72 lines (60 loc) · 2.29 KB
/
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
// not published with regular exports - will access via window
import 'https://unpkg.com/[email protected]/index.js';
import 'https://unpkg.com/[email protected]/lib/xterm.js';
import 'https://unpkg.com/[email protected]/lib/xterm-addon-web-links.js';
import 'https://unpkg.com/[email protected]/lib/xterm-addon-fit.js';
import 'https://unpkg.com/[email protected]/lib/xterm-addon-unicode11.js';
import { theme } from './theme.js';
const delay = ms => new Promise(r => setTimeout(r, ms));
const { WebLinksAddon } = window.WebLinksAddon;
const { FitAddon } = window.FitAddon;
const { Unicode11Addon } = window.Unicode11Addon;
const termEl = document.getElementById('terminal');
const termLinks = new WebLinksAddon();
const termFit = new FitAddon();
const termUnicode = new Unicode11Addon();
const getFontSize = () => {
const w = document.getElementById('terminal').clientWidth;
if (w > 1285) return 32;
if (w > 970) return 24;
if (w > 650) return 16;
if (w >= 572) return 14;
return 12;
}
export const initializeTerm = () => {
const w = document.documentElement.clientWidth;
const term = window.term = new Terminal({
fontFamily: `"CP437", "Cascadia Code", "Fira Code", monospace`,
fontSize: getFontSize(),
convertEol: true, // treat \n like \r\n
disableStdin: true, // view only
cols: 80,
// cursorBlink: true,
// cursorStyle: 'underline',
theme: { ...theme, cursor: 'rgba(0,0,0,0)', cursorAccent: 'rgba(0,0,0,0)' }, // hide cursor
});
term.loadAddon(termLinks);
term.loadAddon(termFit);
term.loadAddon(termUnicode);
term.unicode.activeVersion = '11';
term.fit = termFit.fit.bind(termFit);
term.open(termEl);
// have terminal fit the viewport - handle devtools open/close event(s)
const onResize = () => {
term.setOption('fontSize', getFontSize());
term.fit();
}
term.fit();
window.addEventListener('resize', onResize);
window.addEventListener('devtoolschange', onResize);
const disposeFn = term.dispose;
term.dispose = (...args) => {
try {
disposeFn(...args);
} catch(_) {}
term.dispose = () => {};
window.removeEventListener('resize', onResize);
window.removeEventListener('devtoolschange', onResize);
}
return term;
};