Skip to content

Commit 3d4aa08

Browse files
committed
Big code cleanup
1 parent 398b078 commit 3d4aa08

File tree

5 files changed

+49
-85
lines changed

5 files changed

+49
-85
lines changed

CustomShell/Compression.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ public void CompressFolder(string[] tokens)
4646
}
4747
else if (tokens.Length == 2)
4848
{
49-
string path;
50-
51-
if (!main.IsFilePath(tokens[1]))
52-
path = main.GetFullPathFromName(tokens[1]);
53-
else
54-
path = tokens[1];
55-
49+
string path = main.GetPathType(tokens[1]);
5650
ZipFile.CreateFromDirectory(path, path + ".zip");
5751
}
5852
main.AddCommandToConsole(tokens);

CustomShell/MainController.Designer.cs

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomShell/MainController.cs

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ public string InputPrefix()
5454
return text;
5555
}
5656

57-
//This is used to get a full file path if only a file or folder is entered
58-
//and the user expects the command to understand that it's the file inside of the current directory
59-
public string GetFullPathFromArray(string[] tokens)
60-
{
61-
return string.Concat(currentDir, @"\", tokens[tokens.Length - 1]);
62-
}
63-
6457
public string GetFullPathFromName(string path)
6558
{
6659
if (!currentDir.EndsWith("\\"))
@@ -69,27 +62,6 @@ public string GetFullPathFromName(string path)
6962
return string.Concat(currentDir, path);
7063
}
7164

72-
public bool IsFilePath(string path)
73-
{
74-
if (!path.Contains(@":\"))
75-
return false;
76-
else
77-
return true;
78-
}
79-
80-
81-
//Check if the user has entered a complete file path or only a file or folder within the current directory
82-
public string CheckInputType(string[] tokens) //This should be replaced as soon as possible since it only checks the last token
83-
{
84-
string path;
85-
if (!tokens[tokens.Length - 1].Contains(@":\"))
86-
path = GetFullPathFromArray(tokens);
87-
else
88-
path = tokens[1];
89-
90-
return path;
91-
}
92-
9365
public string GetPathType(string path)
9466
{
9567
string input;
@@ -174,7 +146,8 @@ public string FormatBytes(long bytes)
174146
#region Commands
175147
public void ChangeDirectory(string[] tokens)
176148
{
177-
string path = CheckInputType(tokens);
149+
150+
string path = GetPathType(tokens[1]);
178151

179152
if (tokens.Length == 1) //Go to root
180153
{
@@ -223,7 +196,7 @@ public void ListFiles(string[] tokens)
223196
{
224197
AddCommandToConsole(tokens);
225198

226-
string path = CheckInputType(tokens);
199+
string path = GetPathType(tokens[1]);
227200
IEnumerable entries = Directory.EnumerateFileSystemEntries(path);
228201
foreach (string item in entries)
229202
{
@@ -253,18 +226,8 @@ public void ListFiles(string[] tokens)
253226
}
254227
public void Rename(string[] tokens)
255228
{
256-
string path = string.Empty;
257-
string name = string.Empty;
258-
259-
if (!tokens[1].Contains(@":\"))
260-
path = GetFullPathFromName(tokens[1]);
261-
else
262-
path = tokens[1];
263-
264-
if (!tokens[2].Contains(@":\"))
265-
name = GetFullPathFromName(tokens[2]);
266-
else
267-
name = tokens[2];
229+
string path = GetPathType(tokens[1]);
230+
string name = GetPathType(tokens[2]);
268231

269232
try
270233
{
@@ -283,11 +246,11 @@ public void Rename(string[] tokens)
283246

284247
public void MakeDirectory(string[] tokens)
285248
{
286-
string dir = CheckInputType(tokens);
249+
string path = GetPathType(tokens[1]);
287250

288-
if (!Directory.Exists(dir))
251+
if (!Directory.Exists(path))
289252
{
290-
Directory.CreateDirectory(dir);
253+
Directory.CreateDirectory(path);
291254
AddCommandToConsole(tokens);
292255
}
293256
else
@@ -296,7 +259,17 @@ public void MakeDirectory(string[] tokens)
296259

297260
public void MakeFile(string[] tokens)
298261
{
299-
string path = CheckInputType(tokens);
262+
string path = string.Empty;
263+
if (tokens.Length == 2)
264+
{
265+
path = GetPathType(tokens[1]);
266+
267+
}
268+
else
269+
{
270+
AddTextToConsole("Wrong ammount of arguments...");
271+
return;
272+
}
300273

301274
if (!File.Exists(path))
302275
{
@@ -322,14 +295,18 @@ public void MakeFile(string[] tokens)
322295

323296
public void RemoveFolder(string[] tokens)
324297
{
325-
string path = CheckInputType(tokens);
298+
string path = string.Empty;
299+
if (!tokens[1].Equals("-r"))
300+
path = GetPathType(tokens[1]);
301+
else
302+
path = GetPathType(tokens[2]);
326303

327-
if (tokens[1].Contains("."))
304+
if (path.Contains("."))
328305
{
329306
if (File.Exists(path))
330307
File.Delete(path);
331308
else
332-
AddTextToConsole("File doesn't exists");
309+
AddTextToConsole("File doesn't exist...");
333310
}
334311
else
335312
{
@@ -458,8 +435,9 @@ public void DisplayHelp()
458435
sb.Append("mkfile [Path] | Creates a file\n");
459436
sb.Append("cp [InputPath] (OutputPath) | Copies a file\n");
460437
sb.Append("mv [InputPath] [OutputPath] | Moves a file or folder\n");
461-
sb.Append("rm [Path] | Removes a file or folder\n");
438+
sb.Append("rm (-r) [Path] | Removes a file or folder\n");
462439
sb.Append("system | Displays system information in a nice way\n");
440+
sb.Append("script [ScriptPath] | Runs a script file\n");
463441
sb.Append("exec [PathToExe] | Executes an .EXE file\n");
464442
sb.Append("open [PathToFile] | Opens a file with the standard app\n");
465443
sb.Append("extr [PathToZip] (OutputFolder) | Extracts a zip file\n");
@@ -496,8 +474,9 @@ public void DisplayHelp()
496474

497475
public void Execute(string[] tokens)
498476
{
499-
string path = CheckInputType(tokens);
500-
if (!tokens[1].EndsWith(".exe"))
477+
string path = GetPathType(tokens[1]);
478+
479+
if (path.EndsWith(".exe"))
501480
{
502481
AddTextToConsole("You have not entered a valid file to execute");
503482
return;
@@ -518,8 +497,9 @@ public void Execute(string[] tokens)
518497

519498
public void OpenFile(string[] tokens)
520499
{
521-
string path = CheckInputType(tokens);
522-
if (!tokens[1].Contains("."))
500+
string path = GetPathType(tokens[1]);
501+
502+
if (!path.Contains("."))
523503
{
524504
AddTextToConsole("You have not entered a valid file to open");
525505
return;
@@ -552,7 +532,7 @@ public void ClearHistory()
552532

553533
public void DirectorySize(string[] tokens)
554534
{
555-
string path = CheckInputType(tokens);
535+
string path = GetPathType(tokens[1]);
556536
long size = 0;
557537
DirectoryInfo di = new DirectoryInfo(path);
558538

@@ -577,17 +557,8 @@ public void DirectorySize(string[] tokens)
577557
{
578558
if (tokens.Length == 3)
579559
{
580-
string input;
581-
if (!tokens[1].Contains(@":\"))
582-
input = GetFullPathFromName(tokens[1]);
583-
else
584-
input = tokens[1];
585-
586-
string output;
587-
if (!tokens[2].Contains(@":\"))
588-
output = GetFullPathFromName(tokens[2]);
589-
else
590-
output = tokens[2];
560+
string input = GetPathType(tokens[1]);
561+
string output = GetPathType(tokens[2]);
591562

592563
try
593564
{
@@ -712,6 +683,7 @@ public void RunCommand(string command, bool fromScript)
712683
DirectorySize(tokens);
713684
break;
714685
case true when cmds[i].StartsWith("peek"):
686+
AddCommandToConsole(tokens);
715687
if (wand == null)
716688
wand = new WandEditor();
717689
wand.PeekFile(tokens);
@@ -870,7 +842,6 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
870842
historyIndex = historyLen - 1;
871843
}
872844

873-
//When command is entered
874845
if (e.KeyCode == Keys.Enter)
875846
{
876847
e.Handled = true;
@@ -882,6 +853,7 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
882853
e.Handled = true;
883854
if (localDirectory == null)
884855
localDirectory = new LocalDirectory();
856+
885857
localDirectory.GetAllCurrentDir(inputBox.Text, currentDir);
886858
}
887859

CustomShell/ScriptInterpreter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public ScriptInterpreter(string filePath)
6464

6565
private void ReadScriptFile(string filePath)
6666
{
67-
if (!main.IsFilePath(filePath))
68-
filePath = main.GetFullPathFromName(filePath);
67+
filePath = main.GetPathType(filePath);
6968

7069
if (!filePath.EndsWith(".srp"))
7170
return;

CustomShell/WandEditor.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ public void PeekFile(string[] tokens)
2929
return;
3030
}
3131

32-
if (!main.IsFilePath(tokens[1]))
33-
path = main.GetFullPathFromName(tokens[1]);
34-
32+
path = main.GetPathType(tokens[1]);
3533
string[] lines = File.ReadAllLines(path);
3634
for (int i = 0; i < lines.Length; ++i)
3735
{
@@ -50,8 +48,7 @@ public void LoadFile(string[] tokens)
5048
return;
5149
}
5250

53-
if (!main.IsFilePath(tokens[1]))
54-
path = main.GetFullPathFromName(tokens[1]);
51+
path = main.GetPathType(tokens[1]);
5552

5653
main.wandTextBox.Clear();
5754
main.wandTextBox.Visible = true;// Swap text box to be able to preseve coloring in previous commands

0 commit comments

Comments
 (0)