diff options
| author | Alex-TIMEHACK <[email protected]> | 2017-10-15 20:25:37 +0100 |
|---|---|---|
| committer | Alex-TIMEHACK <[email protected]> | 2017-10-15 20:25:37 +0100 |
| commit | b2f7322f3072ff90c8e6039ec4b3219d3c3fb919 (patch) | |
| tree | c9bfc21c4cc20df9eab5dbaa1c12772a90ef0f74 /ShiftOS.Main/Terminal | |
| parent | 7efdde5832b517153363621aaec982211ff25f61 (diff) | |
| download | shiftos-rewind-b2f7322f3072ff90c8e6039ec4b3219d3c3fb919.tar.gz shiftos-rewind-b2f7322f3072ff90c8e6039ec4b3219d3c3fb919.tar.bz2 shiftos-rewind-b2f7322f3072ff90c8e6039ec4b3219d3c3fb919.zip | |
It's broken.
Diffstat (limited to 'ShiftOS.Main/Terminal')
| -rw-r--r-- | ShiftOS.Main/Terminal/Commands/Hello.cs | 25 | ||||
| -rw-r--r-- | ShiftOS.Main/Terminal/TerminalBackend.cs | 49 | ||||
| -rw-r--r-- | ShiftOS.Main/Terminal/TerminalCommand.cs | 92 |
3 files changed, 166 insertions, 0 deletions
diff --git a/ShiftOS.Main/Terminal/Commands/Hello.cs b/ShiftOS.Main/Terminal/Commands/Hello.cs new file mode 100644 index 0000000..efc9a9a --- /dev/null +++ b/ShiftOS.Main/Terminal/Commands/Hello.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Main.Terminal.Commands +{ + public class Hello : TerminalCommand + { + public override string Name { get; } = "Hello"; + public override string Summary { get; } = "Just an example command."; + public override string Usage { get; } = "Hello <NAME>."; + public override bool Unlocked { get; set; } = false; + + public override async void Run(params string[] parameters) + { + string name = string.Join(" ", parameters); + WriteLine($"Oh, hello, {name}.", Color.Red); + string age = await Input("Hey, What's your age?"); + WriteLine($"Ok, so your name is {name} and your age {age}."); + } + } +} diff --git a/ShiftOS.Main/Terminal/TerminalBackend.cs b/ShiftOS.Main/Terminal/TerminalBackend.cs new file mode 100644 index 0000000..9a5785f --- /dev/null +++ b/ShiftOS.Main/Terminal/TerminalBackend.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ShiftOS.Main.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; + + public static List<ShiftOS.Apps.Terminal> trm = new List<ShiftOS.Apps.Terminal>(); + public static int trmTopID = 0; + /// <summary> + /// Runs a terminal command. + /// </summary> + /// <param name="command"></param> + /// <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) + { + 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.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); + return; + } + } + + Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.Text += " \n The command cannot be found. \n"; + } + } +} diff --git a/ShiftOS.Main/Terminal/TerminalCommand.cs b/ShiftOS.Main/Terminal/TerminalCommand.cs new file mode 100644 index 0000000..4746e6c --- /dev/null +++ b/ShiftOS.Main/Terminal/TerminalCommand.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ShiftOS.Main.Terminal +{ + public class TerminalCommand + { + public int TermID { get; set; } + + public virtual string Name { get; } + public virtual string Summary { get; } + public virtual string Usage { get; } + public virtual bool Unlocked { get; set; } + + public virtual void Run(params string[] parameters) { } + + /// <summary> + /// Writes a blank line in the terminal. + /// </summary> + public virtual void WriteLine() + { + WriteLine(""); + } + + /// <summary> + /// Writes specified text in the terminal and starts a new line. + /// </summary> + /// <param name="value"><summary>The text to write before the new line is made.</summary></param> + public virtual void WriteLine(string value) + { + Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"{value} \n"); + } + + /// <summary> + /// Writes specified text in the terminal in the specified color and starts a new line. + /// </summary> + /// <param name="value"><summary>The text to write before the new line is made.</summary></param> + /// <param name="textClr"><summary>The color the text is written in.</summary></param> + public virtual void WriteLine(string value, Color textClr) + { + ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID); + + int startPoint = trm.termmain.Text.Length; + trm.termmain.AppendText($"{value} \n"); + trm.termmain.Select(startPoint, $"{value} \n".Length); + trm.termmain.SelectionColor = textClr; + } + + /// <summary> + /// Writes specified text in the terminal. + /// </summary> + /// <param name="value"><summary>The text to write.</summary></param> + /// <param name="textClr"><summary>The color the text is written in.</summary></param> + public virtual void Write(string value, Color textClr) + { + + ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID); + + int startPoint = trm.termmain.Text.Length; + trm.termmain.AppendText($"{value}"); + trm.termmain.Select(startPoint, $"{value}".Length); + trm.termmain.SelectionColor = textClr; + } + + /// <summary> + /// Writes specified text in the terminal. + /// </summary> + /// <param name="value"><summary>The text to say before requesting text. </summary></param> + public virtual Task<string> Input(string value = "") + { + ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID); + trm.Input(value); + + Task<string> Input = new Task<string>(() => + { + while (true) + if (trm.InputReturnText != "") break; + + // The terminal has finally decided! + + return trm.InputReturnText; + }); + Input.Start(); + return Input; + } + } +} |
