Skip to content

Commit 9b0c39c

Browse files
committed
Major cleanup and stablility improvement
1 parent 3d4aa08 commit 9b0c39c

File tree

9 files changed

+94
-85
lines changed

9 files changed

+94
-85
lines changed

CustomShell/CustomShell.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
<Compile Include="Processes.cs" />
9696
<Compile Include="Program.cs" />
9797
<Compile Include="Properties\AssemblyInfo.cs" />
98-
<Compile Include="ScriptFile.cs" />
9998
<Compile Include="ScriptInterpreter.cs" />
10099
<Compile Include="SSHClient.cs" />
101100
<Compile Include="SystemInformation.cs" />

CustomShell/FTPController.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public void StartFTPSConnection(string user, string pass)
2121
client.Credentials = new NetworkCredential(user, pass);
2222
client.Connect();
2323
main.AddTextToConsole("Successfully connected to the server...");
24-
main.inputBox.Text = main.InputPrefix();
25-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
24+
main.SetInputPrefix();
2625
}
2726
catch (Exception)
2827
{
@@ -36,9 +35,7 @@ public void StartFTPConnection()
3635
{
3736
client.Connect();
3837
main.AddTextToConsole("Successfully connected to the server...");
39-
main.inputBox.Text = main.InputPrefix();
40-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
41-
38+
main.SetInputPrefix();
4239
}
4340
catch (Exception)
4441
{
@@ -53,8 +50,7 @@ public void GetDir(string dir)
5350
foreach (FtpListItem item in client.GetListing(dir))
5451
main.AddFTPItemToConsole(item);
5552

56-
main.inputBox.Text = main.InputPrefix();
57-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
53+
main.SetInputPrefix();
5854
}
5955

6056
public void UploadFile(string from, string to)
@@ -63,8 +59,7 @@ public void UploadFile(string from, string to)
6359
{
6460
client.UploadFile(from, to);
6561
main.AddTextToConsole("Successfully uploaded file...");
66-
main.inputBox.Text = main.InputPrefix();
67-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
62+
main.SetInputPrefix();
6863
}
6964
catch (Exception)
7065
{
@@ -79,8 +74,7 @@ public void DownloadFile(string to, string from)
7974
{
8075
client.DownloadFile(to, from);
8176
main.AddTextToConsole("Successfully downloaded file...");
82-
main.inputBox.Text = main.InputPrefix();
83-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
77+
main.SetInputPrefix();
8478
}
8579
catch (Exception)
8680
{
@@ -95,8 +89,7 @@ public void UploadDirectory(string from, string to)
9589
{
9690
client.UploadDirectory(from, to);
9791
main.AddTextToConsole("Successfully uploaded directory...");
98-
main.inputBox.Text = main.InputPrefix();
99-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
92+
main.SetInputPrefix();
10093
}
10194
catch (Exception)
10295
{
@@ -111,8 +104,7 @@ public void DownloadDirectory(string to, string from)
111104
{
112105
client.DownloadDirectory(to, from);
113106
main.AddTextToConsole("Successfully downloaded directory...");
114-
main.inputBox.Text = main.InputPrefix();
115-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
107+
main.SetInputPrefix();
116108
}
117109
catch (Exception)
118110
{
@@ -127,8 +119,7 @@ public void DeleteFile(string path)
127119
{
128120
client.DeleteFile(path);
129121
main.AddTextToConsole("Successfully deleted the file...");
130-
main.inputBox.Text = main.InputPrefix();
131-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
122+
main.SetInputPrefix();
132123
}
133124
catch (Exception)
134125
{
@@ -143,8 +134,7 @@ public void DeleteDirectory(string path)
143134
{
144135
client.DeleteDirectory(path);
145136
main.AddTextToConsole("Successfully deleted the directory...");
146-
main.inputBox.Text = main.InputPrefix();
147-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
137+
main.SetInputPrefix();
148138
}
149139
catch (Exception)
150140
{
@@ -159,8 +149,7 @@ public void Terminate()
159149
{
160150
client.Disconnect();
161151
main.AddTextToConsole("Successfully terminated connection to the server...");
162-
main.inputBox.Text = main.InputPrefix();
163-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
152+
main.SetInputPrefix();
164153
}
165154
catch (Exception)
166155
{

CustomShell/MainController.cs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,25 @@ public MainController()
4848
InitConsole();
4949
}
5050

51-
public string InputPrefix()
51+
public string GetInputPrefix()
5252
{
5353
string text = string.Concat(Environment.UserName, "@", currentDir, " ~ ");
5454
return text;
5555
}
5656

57+
public void SetInputPrefix()
58+
{
59+
inputBox.Text = GetInputPrefix();
60+
LockInputPrefix();
61+
}
62+
63+
public void LockInputPrefix()
64+
{
65+
inputBox.Select(0, GetInputPrefix().Length);
66+
inputBox.SelectionProtected = true;
67+
inputBox.SelectionStart = inputBox.Text.Length;
68+
}
69+
5770
public string GetFullPathFromName(string path)
5871
{
5972
if (!currentDir.EndsWith("\\"))
@@ -75,8 +88,7 @@ public string GetPathType(string path)
7588

7689
public void InitConsole()
7790
{
78-
inputBox.Text = string.Concat(Environment.UserName, "@", currentDir, " ~ ");
79-
inputBox.SelectionStart = inputBox.Text.Length;
91+
SetInputPrefix();
8092
this.ActiveControl = inputBox;
8193
}
8294

@@ -91,8 +103,7 @@ public void AddCommandToConsole(string[] tokens)
91103
outputBox.SelectionStart = outputBox.TextLength;
92104
outputBox.ScrollToCaret();
93105
UpdateHistoryFile(command);//Update history file
94-
inputBox.Text = InputPrefix(); //Clear input area
95-
inputBox.SelectionStart = inputBox.Text.Length;//Set cursor to right position
106+
SetInputPrefix();
96107
}
97108

98109
public void UpdateHistoryFile(string command)
@@ -146,17 +157,22 @@ public string FormatBytes(long bytes)
146157
#region Commands
147158
public void ChangeDirectory(string[] tokens)
148159
{
149-
150-
string path = GetPathType(tokens[1]);
160+
string path = string.Empty;
151161

152-
if (tokens.Length == 1) //Go to root
162+
if (tokens.Length > 1)
163+
path = GetPathType(tokens[1]);
164+
else if(tokens.Length == 1 && tokens[0] == "cd")
153165
{
154166
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
155167
currentDir = @"C:\";
156168
else if (Environment.OSVersion.Platform == PlatformID.Unix)
157169
currentDir = @"~";
170+
171+
SetInputPrefix();
172+
return;
158173
}
159-
else if (tokens[1] == "..")//Go back one folder
174+
175+
if (tokens[1] == "..")//Go back one folder
160176
{
161177
string dir = currentDir;
162178
int index = dir.LastIndexOf(@"\");
@@ -238,7 +254,7 @@ public void Rename(string[] tokens)
238254

239255
AddCommandToConsole(tokens);
240256
}
241-
catch (Exception e)
257+
catch (Exception)
242258
{
243259
AddTextToConsole("Cannot find file path, did you enter the right name?");
244260
}
@@ -261,10 +277,7 @@ public void MakeFile(string[] tokens)
261277
{
262278
string path = string.Empty;
263279
if (tokens.Length == 2)
264-
{
265280
path = GetPathType(tokens[1]);
266-
267-
}
268281
else
269282
{
270283
AddTextToConsole("Wrong ammount of arguments...");
@@ -336,7 +349,6 @@ public void RemoveFolder(string[] tokens)
336349
return; //Hopefully never gets to this so don't display anything
337350
}
338351
}
339-
340352
}
341353
else
342354
AddTextToConsole("Folder doesn't exists");
@@ -365,7 +377,7 @@ public void CopyFile(string[] tokens)
365377
{
366378
if (!Directory.Exists(output))
367379
{
368-
//UNFINISHED, TO BE ADDED
380+
//TODO: COPY FOLDERS
369381
}
370382
else
371383
AddTextToConsole("Output directory already exists");
@@ -468,8 +480,7 @@ public void DisplayHelp()
468480

469481
AddTextToConsole(sb.ToString());
470482
AddCommandToConsole(com);
471-
inputBox.Text = InputPrefix();
472-
inputBox.SelectionStart = inputBox.Text.Length;
483+
SetInputPrefix();
473484
}
474485

475486
public void Execute(string[] tokens)
@@ -521,13 +532,13 @@ public void OpenFile(string[] tokens)
521532
public void ClearConsole()
522533
{
523534
outputBox.Text = string.Empty;
524-
inputBox.Text = InputPrefix();
525-
inputBox.SelectionStart = inputBox.Text.Length;
535+
SetInputPrefix();
526536
}
527537

528538
public void ClearHistory()
529539
{
530540
File.WriteAllText(historyFilePath, string.Empty);
541+
LoadHistoryFile();
531542
}
532543

533544
public void DirectorySize(string[] tokens)
@@ -792,16 +803,14 @@ public void RunCommand(string command, bool fromScript)
792803
coloring = new Coloring();
793804
coloring.ColorForeground(fcolor);
794805

795-
inputBox.Text = InputPrefix();
796-
inputBox.SelectionStart = inputBox.Text.Length;
806+
SetInputPrefix();
797807
break;
798808
case true when cmds[i].StartsWith("bcolor"):
799809
string bcolor = tokens[1].ToUpper();
800810
coloring = new Coloring();
801-
coloring.ColorForeground(bcolor);
811+
coloring.ColorBackground(bcolor);
802812

803-
inputBox.Text = InputPrefix();
804-
inputBox.SelectionStart = inputBox.Text.Length;
813+
SetInputPrefix();
805814
break;
806815
case true when cmds[i].StartsWith("ssh"):
807816
AddCommandToConsole(tokens);
@@ -874,15 +883,15 @@ go out of bounds or change index by 2
874883
{
875884
if(firstClick == false)
876885
--historyIndex;
877-
inputBox.Text = string.Concat(InputPrefix(), " ", cmdHistory[historyIndex]);
886+
inputBox.Text = string.Concat(GetInputPrefix(), " ", cmdHistory[historyIndex]);
878887
inputBox.SelectionStart = inputBox.Text.Length;//Set cursor to right position
879888

880889
if(firstClick == true)
881890
firstClick = false;
882891
}
883892
else if (historyIndex == 0) //This else if must be here so that you can press down to clear the history if you are at the end
884893
{
885-
inputBox.Text = string.Concat(InputPrefix(), " ", cmdHistory[historyIndex]);
894+
inputBox.Text = string.Concat(GetInputPrefix(), " ", cmdHistory[historyIndex]);
886895
inputBox.SelectionStart = inputBox.Text.Length;
887896
}
888897
}
@@ -896,22 +905,21 @@ go out of bounds or change index by 2
896905
{
897906
if (firstClick == false)
898907
++historyIndex;
899-
inputBox.Text = string.Concat(InputPrefix(), " ", cmdHistory[historyIndex]);
908+
inputBox.Text = string.Concat(GetInputPrefix(), " ", cmdHistory[historyIndex]);
900909
inputBox.SelectionStart = inputBox.Text.Length;//Set cursor to right position
901910
if (firstClick == true)
902911
firstClick = false;
903912
}
904913
else if(historyIndex == 0 && firstClick == false) //This else if must be here so that you can press down to clear the history if you are at the end
905914
{
906915
++historyIndex;
907-
inputBox.Text = string.Concat(InputPrefix(), " ", cmdHistory[historyIndex]);
916+
inputBox.Text = string.Concat(GetInputPrefix(), " ", cmdHistory[historyIndex]);
908917
inputBox.SelectionStart = inputBox.Text.Length;
909918
}
910919
else if (historyIndex == cmdHistory.Length - 1)
911920
{
912921
++historyIndex;
913-
inputBox.Text = InputPrefix();
914-
inputBox.SelectionStart = inputBox.Text.Length;//Set cursor to right position
922+
SetInputPrefix();
915923
}
916924
}
917925
#endregion

CustomShell/Processes.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public void ListProcesses()
1717
for (int i = 0; i < processes.Length; i++)
1818
main.AddTextToConsole(processes[i].ProcessName + " : " + processes[i].Id);
1919

20-
main.inputBox.Text = main.InputPrefix();
21-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
20+
main.SetInputPrefix();
2221
}
2322

2423
public void KillProcess(string[] tokens)

CustomShell/SSHClient.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public void EstablishConnection(string host, string username, string password)
2222
client = new SshClient(connInfo);
2323
client.Connect();
2424
main.AddTextToConsole("Successfully connected to the host...");
25-
main.inputBox.Text = main.InputPrefix();
26-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
25+
main.SetInputPrefix();
2726
}
2827
catch (Exception)
2928
{
@@ -45,8 +44,7 @@ public void SendCommand(string command)
4544
main.outputBox.SelectionColor = Color.Red;
4645
main.outputBox.SelectionStart = main.outputBox.Text.Length;
4746

48-
main.inputBox.Text = main.InputPrefix();
49-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
47+
main.SetInputPrefix();
5048
}
5149
catch (Exception)
5250
{
@@ -61,8 +59,7 @@ public void TerminateConnection()
6159
{
6260
client.Disconnect();
6361
main.AddTextToConsole("Successfully terminated the connection...");
64-
main.inputBox.Text = main.InputPrefix();
65-
main.inputBox.SelectionStart = main.inputBox.Text.Length;
62+
main.SetInputPrefix();
6663
}
6764
catch (Exception)
6865
{

CustomShell/ScriptFile.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)