Skip to content

Commit 2d61847

Browse files
committed
Code upgrade
* Add method print() * Add method printDebug() * Fix FileManagement logic to make features to work better * removed _defaultPath variable * Add features such as ls, cd, cd.., delete * Link to anvil to provide anvil features * Convert a few System.out.println() to IOStreams.println()
1 parent 8737096 commit 2d61847

File tree

2 files changed

+121
-32
lines changed

2 files changed

+121
-32
lines changed

Source/Cataphract/API/IOStreams.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ public static void printAttention(String message)
8282
println(5, 8, "[ ATTENTION ] " + message);
8383
}
8484

85+
/**
86+
* Prints the text specified, prefixed with a Debug tag
87+
*
88+
* @param message The text specified to be printed onto the console.
89+
*/
90+
public static void printDebug(String message)
91+
{
92+
println(1, 7, "[ DEBUG ] " + message);
93+
}
94+
95+
/**
96+
* Prints the text specified without newline character at the end. Does not include any formatting.
97+
*
98+
* @param message The text specified to be printed onto the console.
99+
*/
100+
public static void print(String message)
101+
{
102+
System.out.print(message);
103+
}
104+
85105
/**
86106
* Prints the text specified. Does not include any formatting.
87107
*

Source/Cataphract/API/Wraith/FileManagement.java

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import java.nio.file.Files;
77
import java.nio.file.StandardCopyOption;
88

9+
import Cataphract.API.Anvil;
910
import Cataphract.API.IOStreams;
1011
import Cataphract.API.Minotaur.Cryptography;
1112

1213
public class FileManagement
1314
{
1415
private String _username = "";
1516
private String _name = "";
16-
private String _defaultPath = "";
1717
private String _presentWorkingDirectory = "";
1818

1919
private Console console = System.console();
@@ -22,7 +22,7 @@ public FileManagement(String username)throws Exception
2222
{
2323
_username = username;
2424
_name = new Cataphract.API.Dragon.Login(username).getNameLogic();
25-
_defaultPath = "./Users/Cataphract/" + _username + "/ ";
25+
_presentWorkingDirectory = "./Users/Cataphract/" + _username + "/";
2626
}
2727

2828
/*****************************************
@@ -31,8 +31,8 @@ public FileManagement(String username)throws Exception
3131

3232
private final boolean login()throws Exception
3333
{
34-
IOStreams.println("Username: " + _username);
35-
return new Cataphract.API.Dragon.Login(_username).authenticationLogic(Cryptography.stringToSHA3_256(console.readLine("Password: ")), Cryptography.stringToSHA3_256(console.readLine("Security Key: ")));
34+
IOStreams.println("> Username: " + _name);
35+
return new Cataphract.API.Dragon.Login(_username).authenticationLogic(Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Password: "))), Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Security Key: ")))) ;
3636
}
3737

3838
/*****************************************
@@ -44,27 +44,43 @@ private boolean checkEntityExistence(String fileName)throws Exception
4444
return new File(fileName).exists();
4545
}
4646

47-
private void deleteDirectoryFile(String fileName)throws Exception
47+
private final void deleteEntity(String delFile)throws Exception
4848
{
49-
fileName = _defaultPath + _presentWorkingDirectory + fileName;
50-
File deletionEntity = new File(fileName);
51-
52-
if (! checkEntityExistence(fileName))
49+
try
5350
{
54-
IOStreams.printError("The specified file or directory does not exist.");
51+
delFile = _presentWorkingDirectory+delFile;
52+
if(checkEntityExistence(delFile))
53+
{
54+
File f=new File(delFile);
55+
if(f.isDirectory())
56+
deleteEntityHelper(f);
57+
else
58+
f.delete();
59+
}
60+
else
61+
IOStreams.printError("The Specified File/Directory Does Not Exist.");
62+
System.gc();
5563
}
56-
else
64+
catch (Exception E)
65+
{
66+
//troubleshooting tips here
67+
E.printStackTrace();
68+
}
69+
}
70+
71+
private final void deleteEntityHelper(File delfile)throws Exception
72+
{
73+
if (delfile.listFiles() != null)
5774
{
58-
if(deletionEntity.isDirectory())
59-
for (File filesInDirectory : deletionEntity.listFiles())
60-
deleteDirectoryFile(filesInDirectory.getName());
61-
deletionEntity.delete();
75+
for (File fileInDirectory : delfile.listFiles())
76+
deleteEntityHelper(fileInDirectory);
6277
}
78+
delfile.delete();
6379
}
6480

6581
private void viewDirectoryTree()throws Exception
6682
{
67-
File treeView = new File(_defaultPath + _presentWorkingDirectory);
83+
File treeView = new File(_presentWorkingDirectory);
6884
IOStreams.println("\n--- [ TREE VIEW ] ---\n");
6985
viewDirTreeHelper(0, treeView);
7086
IOStreams.println("");
@@ -74,9 +90,9 @@ private final void viewDirTreeHelper(int indent, File file) {
7490
System.out.print("|");
7591

7692
for (int i = 0; i < indent; ++i)
77-
System.out.print('-');
93+
IOStreams.print("-");
7894

79-
System.out.println(file.getName().replace(_username, _name + " [ USER ROOT DIRECTORY ]"));
95+
IOStreams.println(file.getName().replace(_username, _name + " [ USER HOME DIRECTORY ]"));
8096

8197
if (file.isDirectory())
8298
{
@@ -88,12 +104,9 @@ private final void viewDirTreeHelper(int indent, File file) {
88104

89105
private final void navPreviousDirectory()throws Exception
90106
{
91-
int lastSlashIndex = _presentWorkingDirectory.lastIndexOf('/');
92-
if (lastSlashIndex != -1)
93-
{
94-
_presentWorkingDirectory = _presentWorkingDirectory.substring(0, lastSlashIndex);
95-
}
96-
else
107+
_presentWorkingDirectory = _presentWorkingDirectory.substring(0, _presentWorkingDirectory.length() - 1);
108+
_presentWorkingDirectory = _presentWorkingDirectory.replace(_presentWorkingDirectory.substring(_presentWorkingDirectory.lastIndexOf('/'), _presentWorkingDirectory.length()), "/");
109+
if (_presentWorkingDirectory.equals("./Users/Cataphract/"))
97110
{
98111
IOStreams.printError("Permission Denied.");
99112
resetToHomeDirectory();
@@ -102,18 +115,18 @@ private final void navPreviousDirectory()throws Exception
102115

103116
private final void resetToHomeDirectory()
104117
{
105-
_presentWorkingDirectory = "/";
118+
_presentWorkingDirectory = "./Users/Cataphract/" + _username + '/' ;
106119
}
107120

108121
private final void makeDirectory(String fileName) throws Exception
109122
{
110-
new File(_defaultPath + _presentWorkingDirectory + fileName).mkdirs();
123+
new File(_presentWorkingDirectory + fileName).mkdirs();
111124
}
112125

113126
private final void renameEntity(String fileName, String newFileName) throws Exception
114127
{
115-
fileName = _defaultPath + _presentWorkingDirectory + fileName;
116-
newFileName = _defaultPath + _presentWorkingDirectory + newFileName;
128+
fileName = _presentWorkingDirectory + fileName;
129+
newFileName = _presentWorkingDirectory + newFileName;
117130

118131
if(checkEntityExistence(newFileName))
119132
new File(fileName).renameTo(new File(newFileName));
@@ -126,7 +139,7 @@ private final void copyMoveEntity(String fileName, String destination, boolean m
126139
if(!checkEntityExistence(fileName) && !checkEntityExistence(destination))
127140
IOStreams.printError("Invalid file name or destination. Permission Denied.");
128141

129-
copyMoveHelper(new File(_defaultPath + _presentWorkingDirectory + fileName), new File(_defaultPath + _presentWorkingDirectory + destination), move);
142+
copyMoveHelper(new File(_presentWorkingDirectory + fileName), new File(_presentWorkingDirectory + destination), move);
130143
}
131144

132145
private final void copyMoveHelper(File source, File destination, boolean move)throws Exception
@@ -150,6 +163,49 @@ private final void copyMoveHelper(File source, File destination, boolean move)th
150163
}
151164
}
152165

166+
private final void listEntities()throws Exception
167+
{
168+
//String format = "%1$-60s|%2$-50s|%3$-20s\n";
169+
String format = "%1$-32s| %2$-24s| %3$-10s\n";
170+
String c = "-";
171+
if(checkEntityExistence(_presentWorkingDirectory))
172+
{
173+
File dPath=new File(_presentWorkingDirectory);
174+
System.out.println("\n");
175+
String disp = (String.format(format, "Directory/File Name", "File Size [In KB]","Type"));
176+
System.out.println(disp + c.repeat(disp.length()) + "\n");
177+
for(File file : dPath.listFiles())
178+
{
179+
//System.out.format(String.format(format, file.getPath().replace(User,Name), file.getName().replace(User,Name), file.length()/1024+" KB"));
180+
System.out.format(String.format(format, file.getName().replace(_username, _name), file.length()/1024+" KB", file.isDirectory()?"Directory":"File"));
181+
}
182+
System.out.println();
183+
}
184+
else
185+
IOStreams.printError("The Specified File/Directory Does Not Exist.");
186+
System.gc();
187+
}
188+
189+
private final void changeDirectory(String destination)throws Exception
190+
{
191+
if(destination.equals(".."))
192+
{
193+
navPreviousDirectory();
194+
System.gc();
195+
}
196+
else
197+
{
198+
if(checkEntityExistence(_presentWorkingDirectory + destination))
199+
{
200+
_presentWorkingDirectory = _presentWorkingDirectory + destination + "/";
201+
}
202+
else
203+
{
204+
IOStreams.printError("\'" + destination + "\' does not exist");
205+
}
206+
}
207+
}
208+
153209
/*****************************************
154210
* GRINCH FILE MANAGEMENT & SCRIPT LOGIC *
155211
*****************************************/
@@ -200,7 +256,7 @@ private void grinchInterpreter(String command)throws Exception
200256
if(commandArray.length < 2)
201257
IOStreams.printError("Invalid Syntax.");
202258
else
203-
deleteDirectoryFile(commandArray[1]);
259+
deleteEntity(commandArray[1]);
204260
break;
205261

206262
case "rename":
@@ -221,10 +277,18 @@ private void grinchInterpreter(String command)throws Exception
221277
break;
222278

223279
case "pwd":
224-
IOStreams.println(_defaultPath + _presentWorkingDirectory);
280+
IOStreams.println((_presentWorkingDirectory).replace(_username, _name));
225281
break;
226282

227283
case "cd":
284+
if(commandArray.length < 2)
285+
IOStreams.printError("Invalid Syntax.");
286+
else
287+
changeDirectory(commandArray[1]);
288+
break;
289+
290+
case "cd..":
291+
navPreviousDirectory();
228292
break;
229293

230294
case "tree":
@@ -233,17 +297,22 @@ private void grinchInterpreter(String command)throws Exception
233297

234298
case "dir":
235299
case "ls":
300+
listEntities();
236301
break;
237302

238303
case "download":
239304
break;
240305

306+
case "home":
307+
resetToHomeDirectory();
308+
break;
309+
241310
case "exit":
242311
case "":
243312
break;
244313

245314
default:
246-
IOStreams.printError("Command Not Found.");
315+
Anvil.anvilInterpreter(commandArray);
247316
}
248317
}
249318

0 commit comments

Comments
 (0)