-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCLI.js
executable file
·139 lines (129 loc) · 4.64 KB
/
CLI.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var CLI = {
currentInput: "",
oldInput: "",
textHeight: 0,
STDIn: "",
STDOut: "",
cursor: "_",
status: {
OK: 0,
BAD_COMMAND: 1
},
commandHistory: [],
commandPosition: 0,
getSTDIn: function () {
return this.STDIn.trim();
},
render: function () {
container.innerHTML = CLI.oldInput + CLI.currentInput + CLI.cursor;
},
lastCommand: "",
promptMode: false,
passwordMode: false,
passwordBuffer: "",
prompt: function (question) {
OS.display(question);
CLI.render();
CLI.promptMode = true;
},
promptResult: ""
}
var container;
window.onload = function () {
init_d();
setInterval(cursor, 500);
container = window.document.getElementById('container');
}
document.onkeypress = function (evt) {
evt = evt || window.event;
if (evt.charCode == 13) { // On enter
if (evt.shiftKey && CLI.promptMode == false) { //If shift if held, insert a newline instead
CLI.currentInput += "\n";
CLI.render();
window.scrollTo(0, document.body.scrollHeight); //Keep scrolling down
return;
}
if (CLI.promptMode == false) {
//Grab the function here
CLI.commandPosition = 0;
CLI.commandHistory.splice(1, 0, CLI.currentInput);
if (CLI.commandHistory.length > 21) // n+1 -- limit how back history goes
CLI.commandHistory.pop();
var cmdStatus = doCommand(CLI.currentInput);
} else {
if (CLI.passwordMode == false)
CLI.promptResult = CLI.currentInput;
else
CLI.promptResult = CLI.passwordBuffer;
CLI.passwordBuffer = "";
CLI.promptMode = false;
doCommand(CLI.lastCommand);
}
start();
//Prep terminal for new line
if (cmdStatus == CLI.status.BAD_COMMAND)
CLI.currentInput += "\nUnknown command";
CLI.STDIn = "";
CLI.STDOut = "";
if (CLI.promptMode == false) {
if (OS.FS.getPwd() == Directory.Files) {
CLI.currentInput += "\n\n" + CurrentUserSingleton.getInstance().getUserName() + ": <b>/</b>> ";
}
else {
CLI.currentInput += "\n\n" + CurrentUserSingleton.getInstance().getUserName() + ": <b>" + OS.FS.getPwdTopLevel() + "/</b>> ";
}
}
container.innerHTML = CLI.oldInput + CLI.currentInput;
CLI.oldInput = CLI.oldInput + CLI.currentInput;
CLI.currentInput = "";
} else if (evt.charCode != 60 && evt.charCode != 62) { // A character is typed (Not '<' or '>' for HTML reasons)
if (CLI.passwordMode == false) {
CLI.currentInput += String.fromCharCode(evt.charCode);
} else {
CLI.currentInput += "*";
CLI.passwordBuffer += String.fromCharCode(evt.charCode);
}
container.innerHTML = CLI.oldInput + CLI.currentInput;
}
window.scrollTo(0, document.body.scrollHeight); //Keep scrolling down
}
//Backspace works a little differently
document.onkeydown = function (evt) {
evt = evt || window.event;
if (evt.keyCode == 8) { // On backspace
evt.preventDefault(); //Don't go the previous webpage!!
if (CLI.currentInput.length > 0) { //To be safe
CLI.currentInput = CLI.currentInput.slice(0, CLI.currentInput.length - 1); //Remove a character
if (CLI.passwordMode == true) {
if (CLI.passwordBuffer.length > 0)
CLI.passwordBuffer = CLI.passwordBuffer.slice(0, CLI.passwordBuffer.length - 1);
}
CLI.render();
}
} else if (evt.keyCode == 38 && CLI.promptMode == false) { //Up key history
evt.preventDefault();
if (CLI.commandPosition + 1 < CLI.commandHistory.length) {
CLI.commandPosition++;
CLI.currentInput = CLI.commandHistory[CLI.commandPosition];
CLI.render();
}
} else if (evt.keyCode == 40 && CLI.promptMode == false) { //Down key history
evt.preventDefault();
if (CLI.commandPosition - 1 > -1) {
CLI.commandPosition--;
CLI.currentInput = CLI.commandHistory[CLI.commandPosition];
CLI.render();
}
}
window.scrollTo(0, document.body.scrollHeight); //Keep scrolling down
}
function doCommand(input) { //Commands are sent here to be parsed
return Processes.listOfDevices['keyboard'].main(input);
}
function cursor() {
if (CLI.cursor == " ")
CLI.cursor = "_";
else
CLI.cursor = " ";
container.innerHTML = CLI.oldInput + CLI.currentInput + CLI.cursor;
}