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

58 lines
2.4 KiB
C#
Raw Normal View History

2017-10-14 13:42:11 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
2017-10-15 15:25:37 -04:00
using System.Windows.Forms;
2017-10-14 13:42:11 -04:00
2017-10-15 15:25:37 -04:00
namespace ShiftOS.Main.Terminal
2017-10-14 13:42:11 -04:00
{
public static class TerminalBackend
{
// The line below gets all the terminal commands in... well... the entire ShiftOS.Engine
2017-11-18 11:09:54 -05: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 13:42:11 -04:00
2017-10-15 15:25:37 -04:00
public static List<ShiftOS.Apps.Terminal> trm = new List<ShiftOS.Apps.Terminal>();
public static int trmTopID = 0;
2017-11-21 18:47:37 -05:00
public static List<string> commandBuffer = new List<string>();
2017-10-14 13:42:11 -04:00
/// <summary>
/// Runs a terminal command.
/// </summary>
/// <param name="command"></param>
2017-10-15 15:25:37 -04: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 13:42:11 -04: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 13:42:11 -04:00
foreach (TerminalCommand instance in instances)
{
2017-10-15 15:25:37 -04: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;
2017-11-21 18:47:37 -05:00
commandBuffer.Add(command);
return;
}
2017-10-14 13:42:11 -04:00
}
2017-11-22 12:46:09 -05:00
if (command.Length == 0) return;
if (!complete)
{
2017-11-21 17:53:24 -05:00
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"\n sbash: invalid command: {command.Split(' ').First()}");
return;
}
2017-10-15 15:25:37 -04:00
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.Text += " \n The command cannot be found. \n";
2017-10-14 13:42:11 -04:00
}
}
}