-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
66 lines (53 loc) · 1.68 KB
/
script.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
function stdin(input) {
// If enter is pressed:
if (event.keyCode == 13) {
var command = input.value.trim();
// create placeholder from duplicated "stdin" node:
var parent = input.parentNode.parentNode;
var clone = parent.cloneNode(true);
// write output from input command:
var output = exec(command);
clone.appendChild(output)
// clear current prompt:
input.value = "";
// insert placeholder before prompt:
parent.parentNode.insertBefore(clone, parent);
// Handle cleaning:
if (command == "clear") {
var terminal = parent.parentNode;
while (terminal.childElementCount > 1) {
terminal.removeChild(terminal.firstChild);
}
}
}
}
function exec(command) {
switch (command) {
case "":
case "clear":
case "bash":
case "/bin/bash":
return getElement("text-output")
case "output0":
return getElement(command)
default:
var out = getElement("text-output")
// if command contains /
if (command.indexOf("/") !== -1) {
out.value = "bash: " + command + ": No such file or directory";
return out;
}
out.value = "bash: command not found: ".concat(command);
return out;
}
}
function getElement(id) {
var out = document.getElementById(id).cloneNode(true);
out.removeAttribute("id")
out.style.display = "flex";
return out
}
function promptFocus() {
var inputs = document.getElementsByTagName('input');
inputs[inputs.length - 1].focus();
}