Repository navigation
Expand file tree
/
Copy pathterminal.js
More file actions
81 lines (67 loc) · 2.33 KB
/
Copy pathterminal.js
File metadata and controls
81 lines (67 loc) · 2.33 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
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
let activeResolver = null;
function print(text = "") {
const content = document.getElementById("Content");
const line = document.createElement("div");
line.textContent = text;
content.appendChild(line);
line.scrollIntoView({ behavior: "smooth", block: "end" });
}
function clearScreen() {
const content = document.getElementById("Content");
content.innerHTML = "";
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function readLine(promptText = "") {
return new Promise((resolve) => {
const content = document.getElementById("Content");
// Remove any existing blinking carets
document.querySelectorAll(".console-carrot").forEach(el => el.remove());
// Disable any previous inputs
document.querySelectorAll(".terminal-input").forEach(el => el.disabled = true);
const lineContainer = document.createElement("div");
lineContainer.className = "input-line";
const promptSpan = document.createElement("span");
promptSpan.className = "terminal-prompt";
promptSpan.textContent = promptText;
const input = document.createElement("input");
input.type = "text";
input.className = "terminal-input";
input.autocomplete = "off";
input.autocorrect = "off";
input.autocapitalize = "off";
input.spellcheck = false;
const carrot = document.createElement("div");
carrot.className = "console-carrot";
lineContainer.appendChild(promptSpan);
lineContainer.appendChild(input);
lineContainer.appendChild(carrot);
content.appendChild(lineContainer);
input.focus();
lineContainer.scrollIntoView({ behavior: "smooth", block: "end" });
activeResolver = (val) => {
input.disabled = true;
carrot.remove();
resolve(val);
};
});
}
document.addEventListener("keydown", (e) => {
if (e.key === "Enter" && activeResolver) {
const inputs = document.querySelectorAll(".terminal-input:not([disabled])");
if (inputs.length > 0) {
const activeInput = inputs[inputs.length - 1];
const val = activeInput.value;
const resolver = activeResolver;
activeResolver = null;
resolver(val);
}
}
});
document.addEventListener("click", () => {
const inputs = document.querySelectorAll(".terminal-input:not([disabled])");
if (inputs.length > 0) {
inputs[inputs.length - 1].focus();
}
});