subshells and shell-specific cmds

This commit is contained in:
Michael 2017-07-29 11:01:32 -04:00
parent ffdcd39b9a
commit f8854f0e44
3 changed files with 124 additions and 54 deletions

View file

@ -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();
}
}

View file

@ -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]

View file

@ -40,6 +40,29 @@ namespace ShiftOS.Engine
/// </summary>
public static class TerminalBackend
{
private static string _shellOverrideString = "";
/// <summary>
/// Gets the current shell prompt override string.
/// </summary>
public static string ShellOverride
{
get
{
return (string.IsNullOrWhiteSpace(_shellOverrideString) || SaveSystem.CurrentSave == null) ? $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ " : _shellOverrideString;
}
}
/// <summary>
/// Sets the shell override string to the specified value. Empty string or <see cref="null"/> to use the default ShiftOS string.
/// </summary>
/// <param name="value">The string to use as a shell prompt.</param>
public static void SetShellOverride(string value)
{
_shellOverrideString = value;
}
/// <summary>
/// Occurs when a command is processed.
/// </summary>
@ -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<string, object> args)
{
List<string> errors = new List<string>();
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<string, object> 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<string, object> args)
{
SetShellOverride(args["id"].ToString());
}
#endif
/// <summary>
/// Prints the user prompt to the terminal.
/// </summary>
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
}
}
/// <summary>
/// Marks this command so that it can be run in ANY shell.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class MetaCommandAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ShellConstraintAttribute : Attribute
{
/// <summary>
/// Instructs the terminal command interpreter to disallow running of this command unless the user shell override matches up with the value provided here.
/// </summary>
/// <param name="shell">The required shell string. Null or whitespace to match with the default ShiftOS shell.</param>
public ShellConstraintAttribute(string shell)
{
Shell = shell;
}
/// <summary>
/// Gets the required shell string for the command.
/// </summary>
public string Shell { get; private set; }
}
}