Skip to content

Commit

Permalink
Many bugfixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbin44 committed Sep 24, 2021
1 parent 237c78c commit 39c7e4f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 47 deletions.
39 changes: 39 additions & 0 deletions CustomShell/Coloring.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

}

public void FindAndColor(string[] strings, Color color)
{
string word;

for (int i = 0; i < strings.Length; ++i)
{
int s_start = main.wandTextBox.SelectionStart, startIndex = 0, index;
word = strings[i];
while ((index = main.wandTextBox.Text.IndexOf(word, startIndex)) != -1)
{
main.wandTextBox.Select(index, word.Length);
main.wandTextBox.SelectionColor = color;

startIndex = index + word.Length;
}

main.wandTextBox.SelectionStart = s_start;
main.wandTextBox.SelectionLength = 0;
}
}
}
}
1 change: 1 addition & 0 deletions CustomShell/CustomShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<ItemGroup>
<Compile Include="BatchInterpreter.cs" />
<Compile Include="Calculator.cs" />
<Compile Include="Coloring.cs" />
<Compile Include="Compression.cs" />
<Compile Include="FTPController.cs" />
<Compile Include="Interpreter.cs" />
Expand Down
9 changes: 5 additions & 4 deletions CustomShell/MainController.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions CustomShell/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ public void AddCommandToConsole(string[] tokens)
public void UpdateHistoryFile(string command)
{
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
File.AppendAllText(historyFilePath, command + "\n");
}
else
File.AppendAllText(historyFilePath, command + "\n");

LoadHistoryFile();//Update history array
}
Expand Down Expand Up @@ -700,6 +704,7 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
wand.PeekFile(tokens);
break;
case true when cmds[i].StartsWith("wand"):
AddCommandToConsole(tokens);
if (wand == null)
wand = new WandEditor();
wand.LoadFile(tokens);
Expand All @@ -714,33 +719,35 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
Shutdown();
break;
case true when cmds[i].StartsWith("listproc"):
AddCommandToConsole(tokens);
if (proc == null)
proc = new Processes();
proc.ListProcesses();
AddCommandToConsole(tokens);
break;
case true when cmds[i].StartsWith("killproc"):
if (proc == null)
proc = new Processes();
proc.KillProcess(tokens);
break;
case true when cmds[i].StartsWith("calc")://Broken fucking calculator, someone please fix it.
CreateTokens(tokens);
AddCommandToConsole(tokens);
CreateTokens(tokens);
break;
case true when cmds[i].StartsWith("batch"):
if (batch == null)
batch = new BatchInterpreter();
batch.ExecuteCommand(tokens);
break;
case true when cmds[i].StartsWith("system"):
AddCommandToConsole(tokens);
if (systemInfo == null)
systemInfo = new SystemInformation();
AddCommandToConsole(tokens);

systemInfo = null;
break;
case true when cmds[i].StartsWith("ftp"):
AddCommandToConsole(tokens);

if (ftpController == null)
{
ftpController = new FTPController(tokens[1]);
Expand Down Expand Up @@ -773,7 +780,6 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
else if (tokens[1] == "close")
ftpController.Terminate();

AddCommandToConsole(tokens);
break;
case true when cmds[i].StartsWith("fcolor"):
string fcolor = tokens[1].ToUpper();
Expand Down Expand Up @@ -870,7 +876,9 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
inputBox.SelectionStart = inputBox.Text.Length;
break;
case true when cmds[i].StartsWith("ssh"):
if(sshClient == null)
AddCommandToConsole(tokens);

if (sshClient == null)
sshClient = new SSHClient();

if (tokens[0] == "sshCom" || tokens[0] == "sshcom")
Expand All @@ -889,7 +897,6 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e)
else if(tokens[0] == "ssh" && tokens[1] == "connect")
sshClient.EstablishConnection(tokens[2], tokens[3], tokens[4]);

AddCommandToConsole(tokens);
break;
default:
AddTextToConsole("Command does not exist");
Expand Down Expand Up @@ -929,6 +936,11 @@ go out of bounds or change index by 2
if(firstClick == true)
firstClick = false;
}
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
{
inputBox.Text = string.Concat(InputPrefix(), " ", cmdHistory[historyIndex]);
inputBox.SelectionStart = inputBox.Text.Length;
}
}

if (e.KeyCode == Keys.Down)
Expand Down Expand Up @@ -972,6 +984,8 @@ private void wandTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.S)//Save and quit from Wand
wand.SaveAndExit();
else if (e.Control && e.KeyCode == Keys.D)
wand.DuplicateLine();
else if (e.Control && e.KeyCode == Keys.H)//Toggle syntax highlighting
{
if (syntaxHighlight == true)
Expand Down
50 changes: 42 additions & 8 deletions CustomShell/SystemInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ namespace CustomShell
class SystemInformation
{
ManagementObjectSearcher searcher;
/* The ascii art SHOULD be a specific size such as 32x32 or close to that
*
*
*
*
*
*/
Coloring color;
// The ascii art SHOULD be a specific size such as 32x32 or close to that
public SystemInformation()
{
GetIcon();
Expand All @@ -34,13 +29,46 @@ private void CompileInformation()
GetCPU();
GetGPU();
GetRAM();
GetIcon();
GetUptime();
GetMobo();
}

private string GetMobo()
{
string mobo = string.Empty;

return mobo;
}

private string GetOS()
{
string os = string.Empty;
string arch = string.Empty;
string version = string.Empty;

searcher = new ManagementObjectSearcher("select Caption, OSArchitecture, Version from Win32_OperatingSystem");
foreach (ManagementObject share in searcher.Get())
{
foreach (PropertyData PC in share.Properties)
{
if (PC.Name == "Caption")
{
os = PC.Value.ToString();
os = os.Trim();
}
else if (PC.Name == "OSArchitecture")
{
arch = PC.Value.ToString();
arch = arch.Trim();
}
else if(PC.Name == "Version")
{
version = PC.Value.ToString();
version = version.Trim();
}
}
}
return os;
}

Expand Down Expand Up @@ -103,12 +131,18 @@ private string GetRAM()
{
ram = PC.Value.ToString();
ram = ram.Trim();
ram = MainController.controller.FormatBytes(Convert.ToInt32(ram));
ram = MainController.controller.FormatBytes(Convert.ToInt32(ram));//It is highly unclear what size MaxCapacity returns
}
}
}
return ram;
}
private string GetUptime()
{
string uptime = string.Empty;

return uptime;
}

private void GetIcon()
{
Expand Down
59 changes: 30 additions & 29 deletions CustomShell/WandEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace CustomShell
class WandEditor
{
MainController main = MainController.controller;
Coloring color;
public WandEditor()
{

}

string path;
public bool hasFileLoaded = false;
bool syntaxHighlighted = false;
public void AddTextToConsole(string text)
{
main.wandTextBox.AppendText(text + "\n");
Expand All @@ -26,7 +28,6 @@ public void PeekFile(string[] tokens)
main.AddTextToConsole("Invalid command format");
return;
}
main.AddCommandToConsole(tokens);
path = main.CheckInputType(tokens);

string[] lines = File.ReadAllLines(path);
Expand All @@ -48,7 +49,6 @@ public void LoadFile(string[] tokens)
}

path = main.CheckInputType(tokens);
main.AddCommandToConsole(tokens);

main.wandTextBox.Clear();
main.wandTextBox.Visible = true;// Swap text box to be able to preseve coloring in previous commands
Expand Down Expand Up @@ -77,47 +77,48 @@ public void LoadFile(string[] tokens)
}
}

public void DuplicateLine()
{
int index = main.wandTextBox.GetLineFromCharIndex(main.wandTextBox.SelectionStart);
int lineLength = main.wandTextBox.Lines[index].Length;
int newLineLength = main.wandTextBox.Lines[index + 1].Length;
int newLineIndex = main.wandTextBox.GetFirstCharIndexFromLine(index + 1);

main.wandTextBox.SelectionStart = main.wandTextBox.GetFirstCharIndexFromLine(index);
main.wandTextBox.SelectionLength = lineLength;
string lineText = main.wandTextBox.SelectedText;

main.wandTextBox.Text = main.wandTextBox.Text.Insert(newLineIndex, "\n");
main.wandTextBox.Text = main.wandTextBox.Text.Insert(newLineIndex, lineText);
main.wandTextBox.SelectionStart = newLineIndex + newLineLength;

if (syntaxHighlighted == true)
ApplySyntaxHighlight();
}

public void RemoveSyntaxHighlight()
{
syntaxHighlighted = false;
main.wandTextBox.SelectAll();
main.wandTextBox.SelectionColor = Color.LightSteelBlue;
main.wandTextBox.SelectionStart = main.wandTextBox.Text.Length;
}

public void ApplySyntaxHighlight()
{
color = new Coloring();
syntaxHighlighted = true;
string[] types = new string[] {"int ", "integer ", "float ", "single ", "double ", "decimal ", "bool ", "boolean ", "string "};
string[] operators = new string[] {"!", "=", ">", "<", "|", "@", "%", "+", "-", "*", "/", "\\", "?"};
string[] operators = new string[] {"!", "=", ">", "<", "|", "@", "%", "+", "-", "*", "/", "\\", "?", ";", ":"};
string[] statements = new string[] {"if", "else if", "elif", "if else", "else", "true", "false", "try", "catch", "finally", "public", "private", "protected", "static", "using", "import", "include", "define", "void", "while", "for", "return", "continue", "break"};
string[] misc = new string[] {"#", "$", "\"", "'", "region", "endregion"};
string[] numbers = new string[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

FindAndColor(types, Color.Blue);
FindAndColor(operators, Color.Red);
FindAndColor(statements, Color.Purple);
FindAndColor(misc, Color.Khaki);
FindAndColor(numbers, Color.DarkViolet);
}

private void FindAndColor(string[] strings, Color color)
{
string word;

for (int i = 0; i < strings.Length; ++i)
{
int s_start = main.wandTextBox.SelectionStart, startIndex = 0, index;
word = strings[i];
while ((index = main.wandTextBox.Text.IndexOf(word, startIndex)) != -1)
{
main.wandTextBox.Select(index, word.Length);
main.wandTextBox.SelectionColor = color;

startIndex = index + word.Length;
}

main.wandTextBox.SelectionStart = s_start;
main.wandTextBox.SelectionLength = 0;
}
color.FindAndColor(types, Color.Blue);
color.FindAndColor(operators, Color.Red);
color.FindAndColor(statements, Color.Purple);
color.FindAndColor(misc, Color.Khaki);
color.FindAndColor(numbers, Color.DarkViolet);
}

public void SaveAndExit()
Expand Down

0 comments on commit 39c7e4f

Please sign in to comment.