forked from TheCyaniteProject/exit_code_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.java
More file actions
362 lines (323 loc) · 11.6 KB
/
Copy pathConsole.java
File metadata and controls
362 lines (323 loc) · 11.6 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package exitcode;
import javafx.scene.control.Label;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import exitcode.FileNavigator;
// Internal dependancies: Command, FileNavigator
public class Console {
private static Map<String, Command> globalCommands = new TreeMap<>();
private FileNavigator fileNavigator;
private Map<String, Command> privateCommands = new TreeMap<>();
private boolean rootPriv = false;
private boolean debug = false;
private boolean lock = false;
private String localUserInstance = LoginScreen.USERNAME.toLowerCase();
public Console(String defaultDirectory, FileNavigator fileNavigator) { // Temporary
this.fileNavigator = fileNavigator;
this.fileNavigator.setDirectory(defaultDirectory);
}
public void setLocalUser(String username) {
this.localUserInstance = username;
}
public String getLocalUser() {
return this.localUserInstance;
}
public FileNavigator getNavigator() {
return this.fileNavigator;
}
/**
* Sets the value of this.lock
*
* @param mode Sets the lock state (true/false) in this console.
* @author Allison Smith (Cyanite)
*/
public void setLock(boolean mode) {
this.lock = mode;
}
/**
* Returns the value of this.lock
*
* @return true if console is locked, false if otherwise.
* @author Allison Smith (Cyanite)
*/
public boolean locked() {
return this.lock;
}
/**
* Sets the value of this.debug
*
* @param mode Sets the debug mode state (true/false) in this console.
* @author Allison Smith (Cyanite)
*/
public void setDebug(boolean mode) {
this.debug = mode;
}
/**
* Returns the value of this.debug
*
* @return true if console is in debug mode, false if otherwise.
* @author Allison Smith (Cyanite)
*/
public boolean isDebug() {
return this.debug;
}
/**
* Adds a new command to the Map of globalCommands.
*
* @param command The command to add.
* @author JuliusFreudenberger
*/
public static void addGlobalCommand(Command command) {
globalCommands.putIfAbsent(command.getCommandName(), command);
}
/**
* Checks if a command exists, and returns it's value.
*
* @param commandName The name of the command to check.
* @return Command if commandName is in the globalCommands Map
* @throws NullPointerException if the command does not exist, or if the passed value is null.
* @author Allison Smith (Cyanite)
*/
public static Command getGlobalCommand(String commandName) throws NullPointerException {
if (globalCommands.containsKey(commandName)) {
return globalCommands.get(commandName);
}
throw new NullPointerException("The given command does not exist!");
}
/**
* Returns a collection of all global commandNames.
*
* @return the values of the global commands
* @author JuliusFreudenberger
*/
public static Collection<Command> getGlobalCommands() {
return globalCommands.values();
}
/**
* Adds a new command to the Map of globalCommands.
*
* @param command Command type object.
* @throws NullPointerException if the command does not exist.
* @author JuliusFreudenberger
*/
public static void setGlobalCommand(Command command) throws NullPointerException {
globalCommands.replace(command.getCommandName(), command);
}
/**
* Removes a command from the globalCommands Map.
*
* @param commandName The name of the command to remove.
* @throws NullPointerException if the provided String is null.
* @author Allison Smith (Cyanite)
*/
public static void removeGlobalCommand(String commandName) throws NullPointerException {
globalCommands.remove(commandName);
}
/**
* Part of the function to execute a Command.
*
* @param input The command to run.
* @param termArea The JavaFX Label to output to.
* @param body The body of the command. (Everything originally after the first space)
* @return true if the Command ran successfully, false otherwise.
* @author Allison Smith (Cyanite)
* @see exitcode.Console#exeCommand(String, Label, String)
* @see exitcode.Console#exePrivate(String, Label, String)
*/
private boolean exeGlobal(String input, Label termArea, String body) {
if (globalCommands.containsKey(input)) {
Command command = globalCommands.get(input);
if (!(command == null)) {
command.runCommand(termArea, body, this);
return true;
}
}
return false;
}
/**
* A simple method for printing text to a terminal.
*
* @param termArea The Label to print to.
*
* @param text The String to print.
*/
public static void printText(Label termArea, String text) {
termArea.setText(String.format("%s%n%s", termArea.getText(), text));
}
/**
* Sets the value of this.rootPriv
*
* @param permission Sets the state of root privileges (true/false) in this console.
* @author Allison Smith (Cyanite)
*/
public void setRootPrivs(boolean permission) {
this.rootPriv = permission;
}
/**
* Returns the value of this.rootPriv
*
* @return true if console "has root privileges", false if otherwise.
* @author Allison Smith (Cyanite)
*/
public boolean hasRootPrivs() {
return this.rootPriv;
}
/**
* "Simply" runs a command. Yep.
*
* @param input The command String to parse and run. i.e: "echo Hello world!"
* @param ps1 The ps1 to be inserted when outputting to the console. i.e: "root@system:~# "
* @param termArea (Ohh, boy!) A JavaFX label to output to. This serves as the Terminal "log" per-say.
* @author JuliusFreudenberger
* @author Allison Smith (Cyanite)
* @see exitcode.Console#formatCommand(String)
* @see exitcode.Console#checkCommand(String)
* @see exitcode.Console#exeCommand(String, Label, String)
* @see exitcode.Console#printText(Label, String)
*/
public void runCommand(String input, String ps1, Label termArea) {
ArrayList<String> output = formatCommand(input);
String command = output.get(0);
String body = output.get(1);
printText(termArea, String.format("%n%s%s", ps1, input));
if (checkCommand(command)) {
exeCommand(command, termArea, body);
} else {
if (!(command.trim().equals(""))) {
printText(termArea, ("\"" + command + "\" is not a valid command."));
}
}
}
/**
* Executes the Command. First, checks if exePrivate() was able to run the command.
* If not, then runs exeGlobal(). This way, private commands overwrite global commands
* but private commands only exist in a single Console instance.
*
* @param input The command to run.
* @param termArea The JavaFX Label to output to.
* @param body The body of the command. (Everything originally after the first space)
* @author Allison Smith (Cyanite)
* @see exitcode.Console#exePrivate(String, Label, String)
* @see exitcode.Console#exeGlobal(String, Label, String)
*/
private void exeCommand(String input, Label termArea, String body) {
if (!exePrivate(input, termArea, body)) {
exeGlobal(input, termArea, body);
}
}
/**
* Part of the function to execute a Command.
*
* @param commandName The command to run.
* @param termArea The JavaFX Label to output to.
* @param body The body of the command. (Everything originally after the first space)
* @return true if the Command ran successfully, false otherwise.
* @author Allison Smith (Cyanite)
* @see exitcode.Console#exeCommand(String, Label, String)
* @see exitcode.Console#exeGlobal(String, Label, String)
*/
private boolean exePrivate(String commandName, Label termArea, String body) {
if (this.privateCommands.containsKey(commandName)) {
Command command = this.privateCommands.get(commandName);
if (!(command == null)) {
command.runCommand(termArea, body, this);
return true;
}
}
return false;
}
/**
* Adds a new command to the Map of privateCommands.
*
* @param command The command to add.
* @author JuliusFreudenberger
*/
public void addPrivateCommand(Command command) {
this.privateCommands.putIfAbsent(command.getCommandName(), command);
}
/**
* Checks if a command exists, and returns it's value.
*
* @param commandName The name of the command to check.
* @return Command if commandName is in the privateCommands Map
* @throws NullPointerException if the command does not exist, or if the passed value is null.
* @author Allison Smith (Cyanite)
*/
public Command getPrivateCommand(String commandName) throws NullPointerException {
if (this.privateCommands.containsKey(commandName)) {
return this.privateCommands.get(commandName);
}
throw new NullPointerException("The specified command does not exist!");
}
/**
* Returns a Set of all private commandNames.
*
* @return the keySet of the private commands
* @author JuliusFreudenberger
*/
public Collection<Command> getPrivateCommands() {
return privateCommands.values();
}
public void setPrivateCommand(Command command) throws NullPointerException {
this.privateCommands.replace(command.getCommandName(), command);
}
public void removePrivateCommand(String commandName) throws NullPointerException {
this.privateCommands.remove(commandName);
}
private boolean checkCommand(String commandName) {
if (checkPrivate(commandName)) {
return true;
} else {
return checkGlobal(commandName);
}
}
private boolean checkPrivate(String commandName) {
if (this.privateCommands.containsKey(commandName)) {
if (this.privateCommands.get(commandName).isDebug()) {
return this.isDebug();
} else {
return true;
}
} else {
return false;
}
}
/**
* Checks if a Global command exists.
*
* @param commandName The name of the command to check for.
* @return true if the command exists, false otherwise.
* @author Allison Smith (Cyanite)
*/
private boolean checkGlobal(String commandName) {
if (globalCommands.containsKey(commandName)) {
if (globalCommands.get(commandName).isDebug()) {
return this.isDebug();
} else {
return true;
}
} else {
return false;
}
}
private ArrayList<String> formatCommand(String commandName) {
String command;
String body;
ArrayList<String> output = new ArrayList<>();
if (!commandName.contains(" ")) {
command = commandName.trim();
body = "null";
} else {
command = commandName.split(" ", 2)[0];
body = commandName.split(" ", 2)[1];
if (body.trim().equals("")) {
body = "null";
}
}
output.add(command);
output.add(body);
return output;
}
}