From f8854f0e4477f87ef68649e769b8126e7586865a Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 29 Jul 2017 11:01:32 -0400 Subject: [PATCH] subshells and shell-specific cmds --- ShiftOS.Frontend/Apps/Terminal.cs | 28 +++--- ShiftOS.Frontend/Commands.cs | 12 +-- ShiftOS_TheReturn/TerminalBackend.cs | 138 +++++++++++++++++++++------ 3 files changed, 124 insertions(+), 54 deletions(-) diff --git a/ShiftOS.Frontend/Apps/Terminal.cs b/ShiftOS.Frontend/Apps/Terminal.cs index 343bb5d..0621139 100644 --- a/ShiftOS.Frontend/Apps/Terminal.cs +++ b/ShiftOS.Frontend/Apps/Terminal.cs @@ -199,7 +199,7 @@ namespace ShiftOS.Frontend.Apps protected override void OnKeyEvent(KeyEvent a) { - if(a.ControlDown && (a.Key == Keys.OemPlus || a.Key == Keys.Add)) + if (a.ControlDown && (a.Key == Keys.OemPlus || a.Key == Keys.Add)) { _zoomFactor *= 2; RecalculateLayout(); @@ -209,7 +209,7 @@ namespace ShiftOS.Frontend.Apps if (a.ControlDown && (a.Key == Keys.OemMinus || a.Key == Keys.Subtract)) { - _zoomFactor = Math.Max(1, _zoomFactor/2); + _zoomFactor = Math.Max(1, _zoomFactor / 2); RecalculateLayout(); Invalidate(); return; @@ -221,7 +221,7 @@ namespace ShiftOS.Frontend.Apps if (!PerformTerminalBehaviours) { Text = Text.Insert(Index, Environment.NewLine); - Index+=2; + Index += 2; RecalculateLayout(); Invalidate(); return; @@ -239,12 +239,12 @@ namespace ShiftOS.Frontend.Apps var text2 = text[text.Length - 1]; var text3 = ""; var text4 = Regex.Replace(text2, @"\t|\n|\r", ""); - WriteLine(""); + WriteLine(""); - if (TerminalBackend.PrefixEnabled) - { - text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length); - } + if (TerminalBackend.PrefixEnabled) + { + text3 = text4.Remove(0, TerminalBackend.ShellOverride.Length); + } if (!string.IsNullOrWhiteSpace(text3)) { TerminalBackend.LastCommand = text3; @@ -288,7 +288,7 @@ namespace ShiftOS.Frontend.Apps { var tostring3 = Lines[Lines.Length - 1]; var tostringlen = tostring3.Length + 1; - var workaround = $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "; + var workaround = TerminalBackend.ShellOverride; var derp = workaround.Length + 1; if (tostringlen != derp) { @@ -310,9 +310,9 @@ namespace ShiftOS.Frontend.Apps Debug.WriteLine("Drunky alert in terminal."); } } - else if(a.Key == Keys.Right) + else if (a.Key == Keys.Right) { - if(Index < Text.Length) + if (Index < Text.Length) { Index++; AppearanceManager.CurrentPosition++; @@ -326,7 +326,7 @@ namespace ShiftOS.Frontend.Apps { var getstring = Lines[Lines.Length - 1]; var stringlen = getstring.Length + 1; - var header = $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "; + var header = TerminalBackend.ShellOverride; var headerlen = header.Length + 1; var selstart = Index; var remstrlen = Text.Length - stringlen; @@ -343,7 +343,7 @@ namespace ShiftOS.Frontend.Apps else if (a.Key == Keys.Up && PerformTerminalBehaviours) { var tostring3 = Lines[Lines.Length - 1]; - if (tostring3 == $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ") + if (tostring3 == TerminalBackend.ShellOverride) Console.Write(TerminalBackend.LastCommand); ConsoleEx.OnFlush?.Invoke(); return; @@ -360,7 +360,7 @@ namespace ShiftOS.Frontend.Apps Text = Text.Insert(Index, a.KeyChar.ToString()); Index++; AppearanceManager.CurrentPosition++; -// RecalculateLayout(); + // RecalculateLayout(); InvalidateTopLevel(); } } diff --git a/ShiftOS.Frontend/Commands.cs b/ShiftOS.Frontend/Commands.cs index 012d0b0..d21e8a9 100644 --- a/ShiftOS.Frontend/Commands.cs +++ b/ShiftOS.Frontend/Commands.cs @@ -152,7 +152,8 @@ namespace ShiftOS.Frontend } } - [Command("commands", "", "{DESC_COMMANDS}")] + [MetaCommand] + [Command("help", "", "{DESC_COMMANDS}")] public static bool Commands() { var sb = new StringBuilder(); @@ -160,7 +161,7 @@ namespace ShiftOS.Frontend sb.AppendLine("================="); sb.AppendLine(); //print all unique namespaces. - foreach (var n in TerminalBackend.Commands.Where(x => !(x is TerminalBackend.WinOpenCommand) && Shiftorium.UpgradeInstalled(x.Dependencies) && x.CommandInfo.hide == false).OrderBy(x => x.CommandInfo.name)) + foreach (var n in TerminalBackend.Commands.Where(x => !(x is TerminalBackend.WinOpenCommand) && Shiftorium.UpgradeInstalled(x.Dependencies) && x.CommandInfo.hide == false && x.MatchShell() == true).OrderBy(x => x.CommandInfo.name)) { sb.Append(" - " + n.CommandInfo.name); if (!string.IsNullOrWhiteSpace(n.CommandInfo.description)) @@ -174,13 +175,6 @@ namespace ShiftOS.Frontend return true; } - [Command("help", description = "{DESC_HELP}")] - public static bool Help() - { - Commands(); - WindowCommands.Programs(); - return true; - } [MultiplayerOnly] diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index 689465f..dcc4625 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -40,6 +40,29 @@ namespace ShiftOS.Engine /// public static class TerminalBackend { + private static string _shellOverrideString = ""; + + + /// + /// Gets the current shell prompt override string. + /// + public static string ShellOverride + { + get + { + return (string.IsNullOrWhiteSpace(_shellOverrideString) || SaveSystem.CurrentSave == null) ? $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ " : _shellOverrideString; + } + } + + /// + /// Sets the shell override string to the specified value. Empty string or to use the default ShiftOS string. + /// + /// The string to use as a shell prompt. + public static void SetShellOverride(string value) + { + _shellOverrideString = value; + } + /// /// Occurs when a command is processed. /// @@ -133,6 +156,17 @@ namespace ShiftOS.Engine public class TerminalCommand { + public virtual bool MatchShell() + { + if(ShellMatch != "metacmd") + { + return (ShellMatch == _shellOverrideString); + } + return true; + } + + public string ShellMatch { get; set; } + public override int GetHashCode() { int hash = 0; @@ -178,11 +212,19 @@ namespace ShiftOS.Engine public virtual void Invoke(Dictionary args) { List errors = new List(); + if (ShellMatch != "metacmd") + { + if (ShellMatch != TerminalBackend._shellOverrideString) + { + errors.Add("Command not found."); + } + } + if (errors.Count > 0) { foreach (var error in errors) { - Console.WriteLine("Command error: " + error); + Console.WriteLine(error); } return; } @@ -197,10 +239,28 @@ namespace ShiftOS.Engine } } + [MetaCommand] + [Command("exit")] + public static void Exit() + { + if (_shellOverrideString != "") + _shellOverrideString = ""; + else + { + Console.WriteLine("error: cannot exit system shell"); + } + } + + public class WinOpenCommand : TerminalCommand { public Type ShiftOSWindow { get; set; } + public override bool MatchShell() + { + return (_shellOverrideString == ""); + } + public override void Invoke(Dictionary args) { @@ -296,6 +356,13 @@ namespace ShiftOS.Engine var tc = new TerminalCommand(); tc.RequiresElevation = !(type.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); + var shellConstraint = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShellConstraintAttribute) as ShellConstraintAttribute; + tc.ShellMatch = (shellConstraint == null) ? "" : shellConstraint.Shell; + + if(mth.GetCustomAttributes(false).FirstOrDefault(x=>x is MetaCommandAttribute) != null) + { + tc.ShellMatch = "metacmd"; + } tc.CommandInfo = cmd as Command; tc.RequiresElevation = tc.RequiresElevation || !(mth.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); @@ -471,42 +538,23 @@ namespace ShiftOS.Engine return true; } +#if DEBUG + [Command("setshell", hide = true)] + [RequiresArgument("id")] + public static void Debug_SetShellOverrideCMD(Dictionary args) + { + SetShellOverride(args["id"].ToString()); + } +#endif + /// /// Prints the user prompt to the terminal. /// public static void PrintPrompt() { - if (SaveSystem.CurrentSave != null) + if (PrefixEnabled) { - ConsoleEx.BackgroundColor = SkinEngine.LoadedSkin.TerminalBackColorCC; - ConsoleEx.Italic = false; - ConsoleEx.Underline = false; - - ConsoleEx.ForegroundColor = ConsoleColor.Magenta; - ConsoleEx.Bold = true; - - Console.Write(SaveSystem.CurrentSave.Username); - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = ConsoleColor.Gray; - Console.Write("@"); - ConsoleEx.Italic = true; - ConsoleEx.Bold = true; - ConsoleEx.ForegroundColor = ConsoleColor.Yellow; - Console.Write(SaveSystem.CurrentSave.SystemName); - ConsoleEx.Italic = false; - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = ConsoleColor.Gray; - Console.Write(":~"); - Console.ForegroundColor = ConsoleColor.White; - ConsoleEx.Italic = true; - if (KernelWatchdog.InKernelMode == true) - Console.Write("#"); - else - Console.Write("$"); - ConsoleEx.Italic = false; - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC; - Console.Write(" "); + Console.Write(ShellOverride); ConsoleEx.Flush(); } } @@ -536,4 +584,32 @@ namespace ShiftOS.Engine } } + + /// + /// Marks this command so that it can be run in ANY shell. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class MetaCommandAttribute : Attribute + { + + } + + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class ShellConstraintAttribute : Attribute + { + /// + /// Instructs the terminal command interpreter to disallow running of this command unless the user shell override matches up with the value provided here. + /// + /// The required shell string. Null or whitespace to match with the default ShiftOS shell. + public ShellConstraintAttribute(string shell) + { + Shell = shell; + } + + + /// + /// Gets the required shell string for the command. + /// + public string Shell { get; private set; } + } }