Skip to content

Commit 275b839

Browse files
committed
Bugfixes and minor improvements
1 parent 4dbf715 commit 275b839

File tree

5 files changed

+143
-67
lines changed

5 files changed

+143
-67
lines changed

CustomShell/CustomShell.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<UpdateRequired>false</UpdateRequired>
2525
<MapFileExtensions>true</MapFileExtensions>
2626
<ApplicationRevision>0</ApplicationRevision>
27-
<ApplicationVersion>1.3.0.%2a</ApplicationVersion>
27+
<ApplicationVersion>1.4.1.%2a</ApplicationVersion>
2828
<UseApplicationTrust>false</UseApplicationTrust>
2929
<BootstrapperEnabled>true</BootstrapperEnabled>
3030
</PropertyGroup>
@@ -84,6 +84,7 @@
8484
<Compile Include="Coloring.cs" />
8585
<Compile Include="Compression.cs" />
8686
<Compile Include="FTPController.cs" />
87+
<Compile Include="Helper.cs" />
8788
<Compile Include="Interpreter.cs" />
8889
<Compile Include="LocalDirectory.cs" />
8990
<Compile Include="MainController.cs">

CustomShell/Helper.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CustomShell
8+
{
9+
public class Helper
10+
{
11+
MainController main = MainController.controller;
12+
public Helper()
13+
{
14+
15+
}
16+
17+
public void DisplayHelp()
18+
{
19+
string[] com = { "help" };
20+
StringBuilder sb = new StringBuilder();
21+
sb.Append("Not everything can be displayed here, please refer to https://github.com/Abbin44/CMD-Plus/wiki for more help\n");
22+
sb.Append("() means optional parameter\n");
23+
sb.Append("------------------------------------------------------------------------------------------\n");
24+
sb.Append("cd [Path] | Change directory\n");
25+
sb.Append("ls (Path) | List all files and folders in a directory\n");
26+
sb.Append("mkdir [Path] | Creates a folder\n");
27+
sb.Append("mkfile [Path] | Creates a file\n");
28+
sb.Append("cp [InputPath] (OutputPath) | Copies a file\n");
29+
sb.Append("mv [InputPath] [OutputPath] | Moves a file or folder\n");
30+
sb.Append("rm (-r) [Path] | Removes a file or folder, -r means recursive removal\n");
31+
sb.Append("system | Displays system information in a nice way\n");
32+
sb.Append("script [ScriptPath] | Runs a script file with the .rls extention\n");
33+
sb.Append("exec [PathToExe] | Executes an .EXE file\n");
34+
sb.Append("open [PathToFile] | Opens a file with the standard app\n");
35+
sb.Append("extr [PathToZip] (OutputFolder) | Extracts a zip file\n");
36+
sb.Append("compr [Path] (OutputArchive) | Compresses a foler or file into a zip\n");
37+
sb.Append("calc [Equation] | Calculates the given equation\n");
38+
sb.Append("size [Path] | Gets the size of a folder\n");
39+
sb.Append("peek [Path] | Prints all the text in a file\n");
40+
sb.Append("wand [Path] | Lets you edit a file. CTRL + S to save. CTRL + Q to quit. CTRL + H to toggle syntax highlight. CTRL + D to duplicate line. \n");
41+
sb.Append("listproc | Lists all running processes\n");
42+
sb.Append("killproc [ID] | Lets you kill a process\n");
43+
sb.Append("batch [CommandOrBatFile] | Lets you run any batch command or script file\n");
44+
sb.Append("clear | Clears the console\n");
45+
sb.Append("clear history | Clears the command history\n");
46+
sb.Append("history | Displays command history with indexes\n");
47+
sb.Append("!! | Runs the last command from the history file\n");
48+
sb.Append("![number] | Runs the command from the history file with the corresponding index that is passed in number\n");
49+
sb.Append("fcolor [Color] | Changes the text color of the console\n");
50+
sb.Append("bcolor [Color] | Changes the back color of the console\n");
51+
sb.Append("ftp [ip] [true/false] (username) (Password) | Starts an FTP/FTPS Connection\n");
52+
sb.Append("ftp uploadFile [local path] [remote path] | Uploads a file to the FTP server\n");
53+
sb.Append("ftp downloadFile [local path] [remote path] | Downloads a file from the FTP server\n");
54+
sb.Append("ftp uploadDirectory [local path] [remote path] | Uploads a directory to the FTP server\n");
55+
sb.Append("ftp downloadDirectory [local path] [remote path] | Downloads a directory from the FTP server\n");
56+
sb.Append("ftp close | Terminates the connection to the FTP server\n");
57+
sb.Append("ssh connect [host] [user] [password] | Opens a connection to the SSH host\n");
58+
sb.Append("sshCom [SSH Command] | Executes an SSH command an returns the result\n");
59+
sb.Append("ssh close | Terminates the connection to the SSH host\n");
60+
sb.Append("help | Display help\n");
61+
sb.Append("shutdown | Shuts down the computer\n");
62+
sb.Append("exit | Exits the shell");
63+
64+
main.AddCommandToConsole(com);
65+
main.AddTextToConsole(sb.ToString());
66+
main.SetInputPrefix();
67+
}
68+
}
69+
}

CustomShell/MainController.cs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public partial class MainController : Form
2020
int historyLen;
2121
WandEditor wand;
2222
Processes proc;
23+
Helper helper;
2324
Compression comp;
2425
BatchInterpreter batch;
2526
FTPController ftpController;
@@ -108,6 +109,7 @@ public void AddCommandToConsole(string[] tokens)
108109

109110
public void UpdateHistoryFile(string command)
110111
{
112+
command = command.Trim();
111113
if(cmdHistory.Length > 0)
112114
{
113115
if(cmdHistory[cmdHistory.Length - 1] != command)//Check if the last command is the same as the current one so that there are no doubles in the history
@@ -154,6 +156,16 @@ public string FormatBytes(long bytes)
154156
return string.Format("{0:0.##} {1}", dblSByte, suffix[i]);
155157
}
156158

159+
private string GetLastCommand()
160+
{
161+
return cmdHistory[cmdHistory.Length - 1];
162+
}
163+
164+
private string GetCommandFromIndex(int index)
165+
{
166+
return cmdHistory[cmdHistory.Length - index];
167+
}
168+
157169
#region Commands
158170
public void ChangeDirectory(string[] tokens)
159171
{
@@ -434,55 +446,6 @@ public void CopyFile(string[] tokens)
434446
}
435447
}
436448

437-
public void DisplayHelp()
438-
{
439-
string[] com = { "help" };
440-
StringBuilder sb = new StringBuilder();
441-
sb.Append("Not everything can be displayed here, please refer to https://github.com/Abbin44/CMD-Plus/wiki for more help\n");
442-
sb.Append("() means optional parameter\n");
443-
sb.Append("------------------------------------------------------------------------------------------\n");
444-
sb.Append("cd [Path] | Change directory\n");
445-
sb.Append("ls (Path) | List all files and folders in a directory\n");
446-
sb.Append("mkdir [Path] | Creates a folder\n");
447-
sb.Append("mkfile [Path] | Creates a file\n");
448-
sb.Append("cp [InputPath] (OutputPath) | Copies a file\n");
449-
sb.Append("mv [InputPath] [OutputPath] | Moves a file or folder\n");
450-
sb.Append("rm (-r) [Path] | Removes a file or folder\n");
451-
sb.Append("system | Displays system information in a nice way\n");
452-
sb.Append("script [ScriptPath] | Runs a script file\n");
453-
sb.Append("exec [PathToExe] | Executes an .EXE file\n");
454-
sb.Append("open [PathToFile] | Opens a file with the standard app\n");
455-
sb.Append("extr [PathToZip] (OutputFolder) | Extracts a zip file\n");
456-
sb.Append("compr [Path] (OutputArchive) | Compresses a foler or file into a zip\n");
457-
sb.Append("calc [Equation] | Calculates the given equation\n");
458-
sb.Append("size [Path] | Gets the size of a folder\n");
459-
sb.Append("peek [Path] | Prints all the text in a file\n");
460-
sb.Append("wand [Path] | Lets you edit a file. CTRL + S to save. CTRL + Q to quit. CTRL + H to toggle syntax highlight. CTRL + D to duplicate line. \n");
461-
sb.Append("listproc | Lists all running processes\n");
462-
sb.Append("killproc [ID] | Lets you kill a process\n");
463-
sb.Append("batch [CommandOrBatFile] | Lets you run any batch command or script file\n");
464-
sb.Append("clear | Clears the console\n");
465-
sb.Append("clear history | Clears the command history\n");
466-
sb.Append("fcolor [Color] | Changes the text color of the console\n");
467-
sb.Append("bcolor [Color] | Changes the back color of the console\n");
468-
sb.Append("ftp [ip] [true/false] (username) (Password) | Starts an FTP/FTPS Connection\n");
469-
sb.Append("ftp uploadFile [local path] [remote path] | Uploads a file to the FTP server\n");
470-
sb.Append("ftp downloadFile [local path] [remote path] | Downloads a file from the FTP server\n");
471-
sb.Append("ftp uploadDirectory [local path] [remote path] | Uploads a directory to the FTP server\n");
472-
sb.Append("ftp downloadDirectory [local path] [remote path] | Downloads a directory from the FTP server\n");
473-
sb.Append("ftp close | Terminates the connection to the FTP server\n");
474-
sb.Append("ssh connect [host] [user] [password] | Opens a connection to the SSH host\n");
475-
sb.Append("sshCom [SSH Command] | Executes an SSH command an returns the result\n");
476-
sb.Append("ssh close | Terminates the connection to the SSH host\n");
477-
sb.Append("help | Display help\n");
478-
sb.Append("shutdown | Shuts down the computer\n");
479-
sb.Append("exit | Exits the shell");
480-
481-
AddTextToConsole(sb.ToString());
482-
AddCommandToConsole(com);
483-
SetInputPrefix();
484-
}
485-
486449
public void Execute(string[] tokens)
487450
{
488451
string path = GetPathType(tokens[1]);
@@ -529,19 +492,35 @@ public void OpenFile(string[] tokens)
529492
}
530493
}
531494

532-
public void ClearConsole()
495+
private void ClearConsole()
533496
{
534497
outputBox.Text = string.Empty;
535498
SetInputPrefix();
536499
}
537500

538-
public void ClearHistory()
501+
private void ClearHistory()
539502
{
540503
File.WriteAllText(historyFilePath, string.Empty);
541504
LoadHistoryFile();
505+
SetInputPrefix();
506+
}
507+
508+
private void PrintHistory()
509+
{
510+
int counter = cmdHistory.Length - 1;
511+
string text = string.Empty;
512+
if (coloring == null)
513+
coloring = new Coloring();
514+
for (int i = 0; i < cmdHistory.Length; ++i)
515+
{
516+
text = counter.ToString() + ") " + cmdHistory[i];
517+
AddTextToConsole(text);
518+
coloring.FindAndColorString(text, Color.DarkOrange, outputBox);
519+
--counter;
520+
}
542521
}
543522

544-
public void DirectorySize(string[] tokens)
523+
private void DirectorySize(string[] tokens)
545524
{
546525
string path = GetPathType(tokens[1]);
547526
long size = 0;
@@ -611,7 +590,7 @@ public void Shutdown()
611590
int historyIndex;
612591
public void RunCommand(string command, bool fromScript)
613592
{
614-
historyIndex = cmdHistory.Length - 1;
593+
historyIndex = historyLen;
615594
int commands = 1; //Default is one but will be incresed if there are any && in the input line
616595
string[] cmds;
617596
string input = string.Empty;
@@ -629,6 +608,24 @@ public void RunCommand(string command, bool fromScript)
629608

630609
tokens = command.Split(' ');
631610

611+
for (int i = 0; i < tokens.Length; ++i)
612+
{
613+
if (tokens[i] == "!!")
614+
{
615+
string temp = tokens[i];
616+
tokens[i] = GetLastCommand();
617+
UpdateHistoryFile(temp);//Update history file
618+
}
619+
620+
if (tokens[i].StartsWith("!") && tokens[i] != "!!")
621+
{
622+
string temp = tokens[i];
623+
int x = Convert.ToInt32(tokens[i].Substring(1));
624+
tokens[i] = GetCommandFromIndex(x + 1);
625+
UpdateHistoryFile(temp);//Update history file
626+
}
627+
}
628+
632629
if (command.Contains("&&"))
633630
{
634631
string[] cmd = command.Split(new string[] { "&&" }, StringSplitOptions.None); //Split input line into seperate commands
@@ -672,7 +669,10 @@ public void RunCommand(string command, bool fromScript)
672669
RemoveFolder(tokens);
673670
break;
674671
case true when cmds[i].StartsWith("help"):
675-
DisplayHelp();
672+
if (helper == null)
673+
helper = new Helper();
674+
675+
helper.DisplayHelp();
676676
break;
677677
case true when cmds[i].StartsWith("exec"):
678678
Execute(tokens);
@@ -715,6 +715,11 @@ public void RunCommand(string command, bool fromScript)
715715
else if (tokens.Length == 2 && tokens[1] == "history")
716716
ClearHistory();
717717
break;
718+
case true when cmds[i].StartsWith("history"):
719+
AddCommandToConsole(tokens);
720+
if (tokens.Length == 1)
721+
PrintHistory();
722+
break;
718723
case true when cmds[i].StartsWith("exit"):
719724
ExitShell();
720725
break;

CustomShell/ScriptInterpreter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ private void InterpretTokens()
178178
tokens[j] = nums[l].value.ToString(); //Replace variable names with the appropriate number
179179
break;
180180
}
181+
182+
if (tokens[j].Equals(nums[l].name + ".value") && lines[i].StartsWith("print"))
183+
{
184+
tokens[j] = nums[l].value.ToString();
185+
break;
186+
}
181187
}
182188
}
183189

CustomShell/SystemInformation.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private string GetResolution()
5959
string screenWidth = Screen.PrimaryScreen.Bounds.Width.ToString();
6060
string screenHeight = Screen.PrimaryScreen.Bounds.Height.ToString();
6161

62-
return string.Concat("Resolution: " ,screenWidth, "x", screenHeight);
62+
return string.Concat("Resolution: " ,screenWidth, "x", screenHeight, "px");
6363
}
6464

6565
private string GetSystemUpTimeInfo()
@@ -79,7 +79,6 @@ private string GetSystemUpTimeInfo()
7979

8080
private string GetMobo()
8181
{
82-
string prefix = "Motherboard: ";
8382
string company = string.Empty;
8483
string model = string.Empty;
8584
searcher = new ManagementObjectSearcher("select Manufacturer, Product from Win32_BaseBoard");
@@ -99,12 +98,11 @@ private string GetMobo()
9998
}
10099
}
101100
}
102-
return string.Concat(prefix, company, model);
101+
return string.Concat("Motherboard: ", company, model);
103102
}
104103

105104
private string GetOS()
106105
{
107-
string prefix = "OS: ";
108106
string name = string.Empty;
109107
string arch = string.Empty;
110108
string version = string.Empty;
@@ -130,12 +128,11 @@ private string GetOS()
130128
}
131129
}
132130
}
133-
return string.Concat(prefix, name, ", ", arch, " v", version);
131+
return string.Concat("OS: ", name, ", ", arch, " v", version);
134132
}
135133

136134
private string GetCPU()
137135
{
138-
string prefix = "CPU: ";
139136
string name = string.Empty;
140137
string speed = string.Empty;
141138
searcher = new ManagementObjectSearcher("select Name, MaxClockSpeed from Win32_Processor");
@@ -157,12 +154,11 @@ private string GetCPU()
157154
}
158155
}
159156

160-
return string.Concat(prefix, name, " ", "@ ", speed, "GHz");
157+
return string.Concat("CPU: ", name, " ", "@ ", speed, "GHz");
161158
}
162159

163160
private string GetGPU()
164161
{
165-
string prefix = "GPU: ";
166162
string gpu = string.Empty;
167163
searcher = new ManagementObjectSearcher("select Caption from Win32_VideoController");
168164
foreach (ManagementObject share in searcher.Get())
@@ -176,12 +172,11 @@ private string GetGPU()
176172
}
177173
}
178174
}
179-
return string.Concat(prefix, gpu);
175+
return string.Concat("GPU: ", gpu);
180176
}
181177

182178
private string GetRAM()
183179
{
184-
string prefix = "RAM: ";
185180
string maxCapacity= string.Empty;
186181
string currentUse= string.Empty;
187182
searcher = new ManagementObjectSearcher("select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OperatingSystem");
@@ -203,7 +198,7 @@ private string GetRAM()
203198
}
204199
}
205200
}
206-
return string.Concat(prefix, currentUse, "/", maxCapacity);
201+
return string.Concat("RAM: ", currentUse, "/", maxCapacity);
207202
}
208203

209204
private void DrawIcon()
@@ -226,7 +221,7 @@ @@@@@@@@ @@@@@@@@
226221
-: .*@@@@@@@@@@@@@@@@@@*. :-
227222
.%@@@@@@@@@@@@@@@@@@@:
228223
-*@@%. -@@@@=. .%@@*-
229-
: :@@@@: : " + "\n";
224+
: :@@@@: : " + "\n\n";
230225

231226
MainController.controller.outputBox.AppendText(icon);
232227
}

0 commit comments

Comments
 (0)