-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
97 lines (86 loc) · 2.5 KB
/
utility.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var prompt;
var currentDirectory;
var io = {};
var commands = {};
var argumentCommands = {};
var commandList = [];
//is called when the page loads
function init() {
io.input = document.getElementById("input");
io.output = document.getElementById("output");
io.prompt = document.getElementById("prompt");
io.print(welcomeMessage);
setPrompt();
focusInput();
}
//is called when user presses enter, this is where the main stuff happens
function handleInput(aEvent) {
if (aEvent.keyCode === 13) {
aEvent.preventDefault();
io.print(prompt + io.input.value);
const input = io.input.value.toLowerCase();
io.input.value = "";
if (input !== "") {
if (input in commands) {
commands[input]();
} else {
let spaceIndex = input.indexOf(" ");
let command = input;
if (spaceIndex > 0) {
command = input.substring(0, spaceIndex);
}
if (command in argumentCommands) {
argumentCommands[command](input);
} else {
io.print("Command not found\nType 'help' for a list of commands");
}
}
}
window.scrollTo(0, document.body.scrollHeight);
}
}
//creates a command
function createCommand(aName, aDescription, aFunction) {
commands[aName] = aFunction;
if (aDescription) {
commandList.push(aName + " - " + aDescription);
}
}
//creates a command that takes an argument
function createArgumentCommand(aName, aDescription, aFunction) {
argumentCommands[aName] = aFunction;
if (aDescription) {
commandList.push(aName + " - " + aDescription);
}
}
//prints a string and makes sure newline characters are handled correctly
io.print = function(aString) {
var output = document.createElement("p");
var text;
var newlineIndex = aString.indexOf("\n");
while (newlineIndex >= 0) {
text = document.createTextNode(aString.substring(0, newlineIndex));
output.appendChild(text);
var newline = document.createElement("br");
output.appendChild(newline);
aString = aString.substring(newlineIndex + 1, aString.length);
newlineIndex = aString.indexOf("\n");
}
text = document.createTextNode(aString);
output.appendChild(text);
io.output.appendChild(output);
}
//gets a random number in the range [0, aMax)
function random(aMax) {
return Math.floor(Math.random() * aMax);
}
//gives focus to the input field
function focusInput() {
io.input.focus();
}
//sets the prompt to show the current directory
function setPrompt() {
prompt = currentDirectory.getName() + "> ";
io.prompt.innerHTML = prompt;
io.input.style.width = (document.body.offsetWidth - io.prompt.offsetWidth - 64) + "px";
}