ShiftOS-Rewind/ShiftOS.Main/Terminal/TerminalBackend.cs

58 lines
2.2 KiB
C#
Raw Normal View History

2017-10-14 17:42:11 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
2017-10-15 19:25:37 +00:00
using System.Windows.Forms;
2017-10-14 17:42:11 +00:00
2017-10-15 19:25:37 +00:00
namespace ShiftOS.Main.Terminal
2017-10-14 17:42:11 +00:00
{
public static class TerminalBackend
{
// The line below gets all the terminal commands in... well... the entire ShiftOS.Engine
2017-11-18 16:09:54 +00:00
public static IEnumerable<TerminalCommand> instances = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsSubclassOf(typeof(TerminalCommand)) && t.GetConstructor(Type.EmptyTypes) != null)
.Select(t => Activator.CreateInstance(t) as TerminalCommand);
2017-10-14 17:42:11 +00:00
2017-10-15 19:25:37 +00:00
public static List<ShiftOS.Apps.Terminal> trm = new List<ShiftOS.Apps.Terminal>();
public static int trmTopID = 0;
2017-10-14 17:42:11 +00:00
/// <summary>
/// Runs a terminal command.
/// </summary>
/// <param name="command"></param>
2017-10-15 19:25:37 +00:00
/// <param name="rtb"><summary>The rich text box that the text will be written to.</summary></param>
public static void RunCommand(string command, int TermID)
2017-10-14 17:42:11 +00:00
{
string name;
try { name = command.Split(' ')[0]; } catch { name = command; }
var theParams = new string[command.Split(' ').Length - 1];
Array.Copy(command.Split(' '), 1, theParams, 0, command.Split(' ').Length - 1);
bool complete = false;
2017-10-14 17:42:11 +00:00
foreach (TerminalCommand instance in instances)
{
2017-10-15 19:25:37 +00:00
if (instance.Name.ToLower() == name.ToLower())
{
instance.TermID = TermID;
// Add a new line!
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText("\n");
instance.Run(theParams);
complete = true;
return;
}
2017-10-14 17:42:11 +00:00
}
if(!complete)
{
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"\nsbash: {command.Split(' ').First()}: invalid command");
return;
}
2017-10-14 17:42:11 +00:00
2017-10-15 19:25:37 +00:00
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.Text += " \n The command cannot be found. \n";
2017-10-14 17:42:11 +00:00
}
}
}