Skip to content

Commit

Permalink
Bugfixes and minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbin44 committed Oct 30, 2021
1 parent 4dbf715 commit 275b839
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 67 deletions.
3 changes: 2 additions & 1 deletion CustomShell/CustomShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.3.0.%2a</ApplicationVersion>
<ApplicationVersion>1.4.1.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down Expand Up @@ -84,6 +84,7 @@
<Compile Include="Coloring.cs" />
<Compile Include="Compression.cs" />
<Compile Include="FTPController.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Interpreter.cs" />
<Compile Include="LocalDirectory.cs" />
<Compile Include="MainController.cs">
Expand Down
69 changes: 69 additions & 0 deletions CustomShell/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomShell
{
public class Helper
{
MainController main = MainController.controller;
public Helper()
{

}

public void DisplayHelp()
{
string[] com = { "help" };
StringBuilder sb = new StringBuilder();
sb.Append("Not everything can be displayed here, please refer to https://github.com/Abbin44/CMD-Plus/wiki for more help\n");
sb.Append("() means optional parameter\n");
sb.Append("------------------------------------------------------------------------------------------\n");
sb.Append("cd [Path] | Change directory\n");
sb.Append("ls (Path) | List all files and folders in a directory\n");
sb.Append("mkdir [Path] | Creates a folder\n");
sb.Append("mkfile [Path] | Creates a file\n");
sb.Append("cp [InputPath] (OutputPath) | Copies a file\n");
sb.Append("mv [InputPath] [OutputPath] | Moves a file or folder\n");
sb.Append("rm (-r) [Path] | Removes a file or folder, -r means recursive removal\n");
sb.Append("system | Displays system information in a nice way\n");
sb.Append("script [ScriptPath] | Runs a script file with the .rls extention\n");
sb.Append("exec [PathToExe] | Executes an .EXE file\n");
sb.Append("open [PathToFile] | Opens a file with the standard app\n");
sb.Append("extr [PathToZip] (OutputFolder) | Extracts a zip file\n");
sb.Append("compr [Path] (OutputArchive) | Compresses a foler or file into a zip\n");
sb.Append("calc [Equation] | Calculates the given equation\n");
sb.Append("size [Path] | Gets the size of a folder\n");
sb.Append("peek [Path] | Prints all the text in a file\n");
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");
sb.Append("listproc | Lists all running processes\n");
sb.Append("killproc [ID] | Lets you kill a process\n");
sb.Append("batch [CommandOrBatFile] | Lets you run any batch command or script file\n");
sb.Append("clear | Clears the console\n");
sb.Append("clear history | Clears the command history\n");
sb.Append("history | Displays command history with indexes\n");
sb.Append("!! | Runs the last command from the history file\n");
sb.Append("![number] | Runs the command from the history file with the corresponding index that is passed in number\n");
sb.Append("fcolor [Color] | Changes the text color of the console\n");
sb.Append("bcolor [Color] | Changes the back color of the console\n");
sb.Append("ftp [ip] [true/false] (username) (Password) | Starts an FTP/FTPS Connection\n");
sb.Append("ftp uploadFile [local path] [remote path] | Uploads a file to the FTP server\n");
sb.Append("ftp downloadFile [local path] [remote path] | Downloads a file from the FTP server\n");
sb.Append("ftp uploadDirectory [local path] [remote path] | Uploads a directory to the FTP server\n");
sb.Append("ftp downloadDirectory [local path] [remote path] | Downloads a directory from the FTP server\n");
sb.Append("ftp close | Terminates the connection to the FTP server\n");
sb.Append("ssh connect [host] [user] [password] | Opens a connection to the SSH host\n");
sb.Append("sshCom [SSH Command] | Executes an SSH command an returns the result\n");
sb.Append("ssh close | Terminates the connection to the SSH host\n");
sb.Append("help | Display help\n");
sb.Append("shutdown | Shuts down the computer\n");
sb.Append("exit | Exits the shell");

main.AddCommandToConsole(com);
main.AddTextToConsole(sb.ToString());
main.SetInputPrefix();
}
}
}
113 changes: 59 additions & 54 deletions CustomShell/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class MainController : Form
int historyLen;
WandEditor wand;
Processes proc;
Helper helper;
Compression comp;
BatchInterpreter batch;
FTPController ftpController;
Expand Down Expand Up @@ -108,6 +109,7 @@ public void AddCommandToConsole(string[] tokens)

public void UpdateHistoryFile(string command)
{
command = command.Trim();
if(cmdHistory.Length > 0)
{
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
Expand Down Expand Up @@ -154,6 +156,16 @@ public string FormatBytes(long bytes)
return string.Format("{0:0.##} {1}", dblSByte, suffix[i]);
}

private string GetLastCommand()
{
return cmdHistory[cmdHistory.Length - 1];
}

private string GetCommandFromIndex(int index)
{
return cmdHistory[cmdHistory.Length - index];
}

#region Commands
public void ChangeDirectory(string[] tokens)
{
Expand Down Expand Up @@ -434,55 +446,6 @@ public void CopyFile(string[] tokens)
}
}

public void DisplayHelp()
{
string[] com = { "help" };
StringBuilder sb = new StringBuilder();
sb.Append("Not everything can be displayed here, please refer to https://github.com/Abbin44/CMD-Plus/wiki for more help\n");
sb.Append("() means optional parameter\n");
sb.Append("------------------------------------------------------------------------------------------\n");
sb.Append("cd [Path] | Change directory\n");
sb.Append("ls (Path) | List all files and folders in a directory\n");
sb.Append("mkdir [Path] | Creates a folder\n");
sb.Append("mkfile [Path] | Creates a file\n");
sb.Append("cp [InputPath] (OutputPath) | Copies a file\n");
sb.Append("mv [InputPath] [OutputPath] | Moves a file or folder\n");
sb.Append("rm (-r) [Path] | Removes a file or folder\n");
sb.Append("system | Displays system information in a nice way\n");
sb.Append("script [ScriptPath] | Runs a script file\n");
sb.Append("exec [PathToExe] | Executes an .EXE file\n");
sb.Append("open [PathToFile] | Opens a file with the standard app\n");
sb.Append("extr [PathToZip] (OutputFolder) | Extracts a zip file\n");
sb.Append("compr [Path] (OutputArchive) | Compresses a foler or file into a zip\n");
sb.Append("calc [Equation] | Calculates the given equation\n");
sb.Append("size [Path] | Gets the size of a folder\n");
sb.Append("peek [Path] | Prints all the text in a file\n");
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");
sb.Append("listproc | Lists all running processes\n");
sb.Append("killproc [ID] | Lets you kill a process\n");
sb.Append("batch [CommandOrBatFile] | Lets you run any batch command or script file\n");
sb.Append("clear | Clears the console\n");
sb.Append("clear history | Clears the command history\n");
sb.Append("fcolor [Color] | Changes the text color of the console\n");
sb.Append("bcolor [Color] | Changes the back color of the console\n");
sb.Append("ftp [ip] [true/false] (username) (Password) | Starts an FTP/FTPS Connection\n");
sb.Append("ftp uploadFile [local path] [remote path] | Uploads a file to the FTP server\n");
sb.Append("ftp downloadFile [local path] [remote path] | Downloads a file from the FTP server\n");
sb.Append("ftp uploadDirectory [local path] [remote path] | Uploads a directory to the FTP server\n");
sb.Append("ftp downloadDirectory [local path] [remote path] | Downloads a directory from the FTP server\n");
sb.Append("ftp close | Terminates the connection to the FTP server\n");
sb.Append("ssh connect [host] [user] [password] | Opens a connection to the SSH host\n");
sb.Append("sshCom [SSH Command] | Executes an SSH command an returns the result\n");
sb.Append("ssh close | Terminates the connection to the SSH host\n");
sb.Append("help | Display help\n");
sb.Append("shutdown | Shuts down the computer\n");
sb.Append("exit | Exits the shell");

AddTextToConsole(sb.ToString());
AddCommandToConsole(com);
SetInputPrefix();
}

public void Execute(string[] tokens)
{
string path = GetPathType(tokens[1]);
Expand Down Expand Up @@ -529,19 +492,35 @@ public void OpenFile(string[] tokens)
}
}

public void ClearConsole()
private void ClearConsole()
{
outputBox.Text = string.Empty;
SetInputPrefix();
}

public void ClearHistory()
private void ClearHistory()
{
File.WriteAllText(historyFilePath, string.Empty);
LoadHistoryFile();
SetInputPrefix();
}

private void PrintHistory()
{
int counter = cmdHistory.Length - 1;
string text = string.Empty;
if (coloring == null)
coloring = new Coloring();
for (int i = 0; i < cmdHistory.Length; ++i)
{
text = counter.ToString() + ") " + cmdHistory[i];
AddTextToConsole(text);
coloring.FindAndColorString(text, Color.DarkOrange, outputBox);
--counter;
}
}

public void DirectorySize(string[] tokens)
private void DirectorySize(string[] tokens)
{
string path = GetPathType(tokens[1]);
long size = 0;
Expand Down Expand Up @@ -611,7 +590,7 @@ public void Shutdown()
int historyIndex;
public void RunCommand(string command, bool fromScript)
{
historyIndex = cmdHistory.Length - 1;
historyIndex = historyLen;
int commands = 1; //Default is one but will be incresed if there are any && in the input line
string[] cmds;
string input = string.Empty;
Expand All @@ -629,6 +608,24 @@ public void RunCommand(string command, bool fromScript)

tokens = command.Split(' ');

for (int i = 0; i < tokens.Length; ++i)
{
if (tokens[i] == "!!")
{
string temp = tokens[i];
tokens[i] = GetLastCommand();
UpdateHistoryFile(temp);//Update history file
}

if (tokens[i].StartsWith("!") && tokens[i] != "!!")
{
string temp = tokens[i];
int x = Convert.ToInt32(tokens[i].Substring(1));
tokens[i] = GetCommandFromIndex(x + 1);
UpdateHistoryFile(temp);//Update history file
}
}

if (command.Contains("&&"))
{
string[] cmd = command.Split(new string[] { "&&" }, StringSplitOptions.None); //Split input line into seperate commands
Expand Down Expand Up @@ -672,7 +669,10 @@ public void RunCommand(string command, bool fromScript)
RemoveFolder(tokens);
break;
case true when cmds[i].StartsWith("help"):
DisplayHelp();
if (helper == null)
helper = new Helper();

helper.DisplayHelp();
break;
case true when cmds[i].StartsWith("exec"):
Execute(tokens);
Expand Down Expand Up @@ -715,6 +715,11 @@ public void RunCommand(string command, bool fromScript)
else if (tokens.Length == 2 && tokens[1] == "history")
ClearHistory();
break;
case true when cmds[i].StartsWith("history"):
AddCommandToConsole(tokens);
if (tokens.Length == 1)
PrintHistory();
break;
case true when cmds[i].StartsWith("exit"):
ExitShell();
break;
Expand Down
6 changes: 6 additions & 0 deletions CustomShell/ScriptInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ private void InterpretTokens()
tokens[j] = nums[l].value.ToString(); //Replace variable names with the appropriate number
break;
}

if (tokens[j].Equals(nums[l].name + ".value") && lines[i].StartsWith("print"))
{
tokens[j] = nums[l].value.ToString();
break;
}
}
}

Expand Down
19 changes: 7 additions & 12 deletions CustomShell/SystemInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private string GetResolution()
string screenWidth = Screen.PrimaryScreen.Bounds.Width.ToString();
string screenHeight = Screen.PrimaryScreen.Bounds.Height.ToString();

return string.Concat("Resolution: " ,screenWidth, "x", screenHeight);
return string.Concat("Resolution: " ,screenWidth, "x", screenHeight, "px");
}

private string GetSystemUpTimeInfo()
Expand All @@ -79,7 +79,6 @@ private string GetSystemUpTimeInfo()

private string GetMobo()
{
string prefix = "Motherboard: ";
string company = string.Empty;
string model = string.Empty;
searcher = new ManagementObjectSearcher("select Manufacturer, Product from Win32_BaseBoard");
Expand All @@ -99,12 +98,11 @@ private string GetMobo()
}
}
}
return string.Concat(prefix, company, model);
return string.Concat("Motherboard: ", company, model);
}

private string GetOS()
{
string prefix = "OS: ";
string name = string.Empty;
string arch = string.Empty;
string version = string.Empty;
Expand All @@ -130,12 +128,11 @@ private string GetOS()
}
}
}
return string.Concat(prefix, name, ", ", arch, " v", version);
return string.Concat("OS: ", name, ", ", arch, " v", version);
}

private string GetCPU()
{
string prefix = "CPU: ";
string name = string.Empty;
string speed = string.Empty;
searcher = new ManagementObjectSearcher("select Name, MaxClockSpeed from Win32_Processor");
Expand All @@ -157,12 +154,11 @@ private string GetCPU()
}
}

return string.Concat(prefix, name, " ", "@ ", speed, "GHz");
return string.Concat("CPU: ", name, " ", "@ ", speed, "GHz");
}

private string GetGPU()
{
string prefix = "GPU: ";
string gpu = string.Empty;
searcher = new ManagementObjectSearcher("select Caption from Win32_VideoController");
foreach (ManagementObject share in searcher.Get())
Expand All @@ -176,12 +172,11 @@ private string GetGPU()
}
}
}
return string.Concat(prefix, gpu);
return string.Concat("GPU: ", gpu);
}

private string GetRAM()
{
string prefix = "RAM: ";
string maxCapacity= string.Empty;
string currentUse= string.Empty;
searcher = new ManagementObjectSearcher("select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OperatingSystem");
Expand All @@ -203,7 +198,7 @@ private string GetRAM()
}
}
}
return string.Concat(prefix, currentUse, "/", maxCapacity);
return string.Concat("RAM: ", currentUse, "/", maxCapacity);
}

private void DrawIcon()
Expand All @@ -226,7 +221,7 @@ @@@@@@@@ @@@@@@@@
-: .*@@@@@@@@@@@@@@@@@@*. :-
.%@@@@@@@@@@@@@@@@@@@:
-*@@%. -@@@@=. .%@@*-
: :@@@@: : " + "\n";
: :@@@@: : " + "\n\n";

MainController.controller.outputBox.AppendText(icon);
}
Expand Down

0 comments on commit 275b839

Please sign in to comment.