Repository navigation
Expand file tree
/
Copy pathmain.mjs
More file actions
51 lines (48 loc) · 2.04 KB
/
Copy pathmain.mjs
File metadata and controls
51 lines (48 loc) · 2.04 KB
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
import { Terminal } from '@xterm/xterm';
import { FitAddon } from '@xterm/addon-fit';
import '@xterm/xterm/css/xterm.css';
import { mount } from './terminal.mjs';
document.documentElement.lang = 'en';
document.body.style.margin = '0';
const element = document.createElement('main');
element.style.height = 'calc(100dvh - 2rem)';
const close = document.createElement('button');
close.textContent = 'Hang up';
const status = document.createElement('output');
status.setAttribute('aria-live', 'polite');
document.body.replaceChildren(element, close, status);
close.disabled = true;
let terminal;
async function start(restart = false) {
close.disabled = true;
close.textContent = 'Hang up';
status.textContent = '';
terminal?.dispose();
try {
terminal = new Terminal({ cursorBlink: true, screenReaderMode: true });
// Tab is the program's; Shift+Tab is left to the browser, so that the keyboard can leave the terminal.
terminal.attachCustomKeyEventHandler(event => !(event.key === 'Tab' && event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey));
const session = await mount(element, {
terminal, fit: new FitAddon(), restart,
onError: error => { status.textContent = error.message; },
onExit: code => {
close.disabled = false;
close.textContent = 'Start again';
close.onclick = () => { void start(true); };
status.textContent = ` Process exited (${code})`;
},
onTruncated: () => { status.textContent = ' Showing the latest terminal output'; },
});
close.disabled = false;
close.onclick = async () => {
close.disabled = true;
try { await session.hangUp(); }
catch (error) { status.textContent = error.message; }
};
terminal.textarea?.setAttribute('aria-label', 'Terminal input. Shift+Tab leaves the terminal.');
terminal.focus();
} catch (error) {
status.textContent = error.message;
}
}
void start();