-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.java
More file actions
172 lines (139 loc) · 4.49 KB
/
Terminal.java
File metadata and controls
172 lines (139 loc) · 4.49 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Terminal extends Commands {
private Parser parser;
private List<String> commandList;
public Terminal() {
this.parser = new Parser();
this.commandList = new ArrayList<>();
}
public void run() {
while (true) {
System.out.print("Enter a command: ");
String input = System.console().readLine();
if (input.equals("exit")) {
break;
}
if (parser.parse(input)) {
int index = Arrays.asList(parser.getArgs()).indexOf(">");
int index2 = Arrays.asList(parser.getArgs()).indexOf(">>");
if (index != -1) {
String[] copy = Arrays.copyOfRange(parser.getArgs(), index + 1, parser.getArgs().length);
String[] main = Arrays.copyOfRange(parser.getArgs(), 0, index);
System.out.println(copy[0]);
String commandAns = chooseCommandAction(main, input);
operator1Command(commandAns, copy);
} else if (index2 != -1) {
String[] copy = Arrays.copyOfRange(parser.getArgs(), index2 + 1, parser.getArgs().length);
String[] main = Arrays.copyOfRange(parser.getArgs(), 0, index2);
System.out.println(copy[0]);
String commandAns = chooseCommandAction(main, input);
operator2Command(commandAns, copy);
} else {
String commandAns = chooseCommandAction(parser.getArgs(), input);
System.out.println(commandAns);
}
}
else {
System.out.println("Invalid command.");
}
}
}
public void operator1Command(String commandAns, String[] copy) {
if (copy.length == 1) {
try {
FileWriter file = new FileWriter(copy[0]);
file.write(commandAns);
file.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Invalid usage ! Usage : command > <filename>");
}
}
public void operator2Command(String commandAns, String[] copy) {
if (copy.length == 1) {
try {
File file = new File(copy[0]);
if ((file.exists())) {
FileWriter file2 = new FileWriter(copy[0]);
file2.write(commandAns);
file2.close();
} else {
System.out.println("No Such File Exists!");
}
}
catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Invalid usage ! Usage : command >> <filename>");
}
}
public String chooseCommandAction(String[] args, String input) {
String commandName = parser.getCommandName();
// String[] args = parser.getArgs();
switch (commandName) {
case "echo":
if (args.length == 1) {
commandList.add(input);
return args[0];
} else
return ("Invalid usage. Usage: echo <text>");
case "pwd":
commandList.add(input);
return (System.getProperty("user.dir"));
case "cd":
commandList.add(input);
return cdCommand(args);
case "ls":
commandList.add(input);
return lsCommand(args);
case "touch":
commandList.add(input);
if (args.length == 0){return("Invalid usage. Usage: touch <file_name>");}
return touchCommand(parser.getArgs()[0]);
case "cp":
if (args.length == 2) {
commandList.add(input);
cpCommand(args[0], args[1]);
}
else if (args.length == 3 && args[0].equals("-r")) {
commandList.add(input);
cprCommand(new File(args[1]), new File(args[2]));
}
else {
System.out.println("Invalid usage. Usage: cp <file_name> <file_name> || cp -r <folder_name> <folder_name>");
}
case "rm":
commandList.add(input);
return rmCommand(args);
case "cat":
commandList.add(input);
return catCommand(args);
case "wc":
commandList.add(input);
return wcCommand(args);
case "mkdir":
commandList.add(input);
return mkdir(args);
case "history":
commandList.add(input);
return history();
default:
return ("Command Not Found !" + commandName);
}
}
public String history() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < commandList.size(); i++) {
result.append((i + 1)).append(" ").append(commandList.get(i)).append("\n");
}
return result.toString();
}
}