YOY terminal

This commit is contained in:
Alex-TIMEHACK 2017-10-14 18:42:11 +01:00
parent 5d1004015d
commit 09215a05dd
5 changed files with 90 additions and 1 deletions

View file

@ -52,6 +52,9 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="Terminal\Commands\Hello.cs" />
<Compile Include="Terminal\TerminalBackend.cs" />
<Compile Include="Terminal\TerminalCommand.cs" />
<Compile Include="Tools.cs" /> <Compile Include="Tools.cs" />
<Compile Include="WindowManager\InfoboxTemplate.cs"> <Compile Include="WindowManager\InfoboxTemplate.cs">
<SubType>UserControl</SubType> <SubType>UserControl</SubType>
@ -93,5 +96,6 @@
<ItemGroup> <ItemGroup>
<None Include="Resources\iconInfoBox.fw.png" /> <None Include="Resources\iconInfoBox.fw.png" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Engine.Terminal.Commands
{
public class Hello : TerminalCommand
{
public override string GetName()
{
return "Hello";
}
public override string Run(params string[] parameters)
{
return "Oh, HELLO," + parameters[0];
}
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Engine.Terminal
{
public static class TerminalBackend
{
// The line below gets all the terminal commands in... well... the entire ShiftOS.Engine
public static IEnumerable<TerminalCommand> instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsSubclassOf(typeof(TerminalCommand))
&& t.GetConstructor(Type.EmptyTypes) != null
select Activator.CreateInstance(t) as TerminalCommand;
/// <summary>
/// Runs a terminal command.
/// </summary>
/// <param name="command"></param>
/// <returns>Returns all the output from that command.</returns>
public static string RunCommand(string command)
{
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);
foreach (TerminalCommand instance in instances)
{
if (instance.GetName() == name)
return instance.Run(theParams);
}
return "The command cannot be found.";
}
// An extra function ;)
private static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
}
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Engine.Terminal
{
public abstract class TerminalCommand
{
public abstract string GetName();
public abstract string Run(params string[] parameters);
}
}

View file

@ -7,6 +7,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using ShiftOS.Engine;
using ShiftOS.Engine.Terminal;
namespace ShiftOS.Main.ShiftOS.Apps namespace ShiftOS.Main.ShiftOS.Apps
{ {
@ -43,7 +45,8 @@ namespace ShiftOS.Main.ShiftOS.Apps
// termmain.Paste(DataFormats.GetFormat(DataFormats.Text)); // termmain.Paste(DataFormats.GetFormat(DataFormats.Text));
e.Handled = true; e.Handled = true;
} else if (e.KeyCode == Keys.Enter) { } else if (e.KeyCode == Keys.Enter) {
Print(RunCommand(termmain.Text.Substring(TrackingPosition, termmain.Text.Length - TrackingPosition))); // The most horrific line in the entire application! Print(TerminalBackend.RunCommand(termmain.Text.Substring(TrackingPosition, termmain.Text.Length - TrackingPosition))); // The most horrific line in the entire application!
e.Handled = true;
} }
} }