forked from EDDYMENS/interactive-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.js
74 lines (65 loc) · 1.75 KB
/
frontend.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
const socket = new WebSocket("ws://localhost:6060");
var term = new window.Terminal({
cursorBlink: true
});
term.open(document.getElementById('terminal'));
function init() {
if (term._initialized) {
return;
}
term._initialized = true;
term.prompt = () => {
term.write('\r\n$ ');
};
prompt(term);
term.onData(e => {
switch (e) {
case '\u0003': // Ctrl+C
term.write('^C');
prompt(term);
break;
case '\r': // Enter
runCommand(term, command);
command = '';
break;
case '\u007F': // Backspace (DEL)
// Do not delete the prompt
if (term._core.buffer.x > 2) {
term.write('\b \b');
if (command.length > 0) {
command = command.substr(0, command.length - 1);
}
}
break;
case '\u0009':
console.log('tabbed', output, ["dd", "ls"]);
break;
default:
if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') {
command += e;
term.write(e);
}
}
});
}
function clearInput(command) {
var inputLengh = command.length;
for (var i = 0; i < inputLengh; i++) {
term.write('\b \b');
}
}
function prompt(term) {
command = '';
term.write('\r\n$ ');
}
socket.onmessage = (event) => {
term.write(event.data);
}
function runCommand(term, command) {
if (command.length > 0) {
clearInput(command);
socket.send(command + '\n');
return;
}
}
init();