Merge pull request #26 from That-One-Nerd/shell-systems

Update main with some shell bug fixes.
This commit is contained in:
That_One_Nerd 2023-03-29 09:10:59 -04:00 committed by GitHub
commit b5cfa6f0cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 29 deletions

View File

@ -100,9 +100,6 @@ public static class BaseModule
}); });
} }
[Command("cut")]
public static void CutFile(string source, string destination) => MoveFile(source, destination);
[Command("del")] [Command("del")]
public static void Delete(string path) public static void Delete(string path)
{ {
@ -189,9 +186,6 @@ public static class BaseModule
[Command("echo")] [Command("echo")]
public static void Echo(string msg) => Write(msg); public static void Echo(string msg) => Write(msg);
[Command("exit")]
public static void ExitShell(int code = 0) => QuitShell(code);
[Command("explorer")] [Command("explorer")]
public static void OpenExplorer(string path = ".") => Process.Start("explorer.exe", Path.GetFullPath(path)); public static void OpenExplorer(string path = ".") => Process.Start("explorer.exe", Path.GetFullPath(path));
@ -210,6 +204,7 @@ public static class BaseModule
DisplayWithPages(lines); DisplayWithPages(lines);
} }
[Command("cut")]
[Command("move")] [Command("move")]
public static void MoveFile(string source, string destination) public static void MoveFile(string source, string destination)
{ {
@ -287,6 +282,7 @@ public static class BaseModule
} }
[Command("print")] [Command("print")]
[Command("type")]
public static void Print(string file) public static void Print(string file)
{ {
if (!File.Exists(file)) throw new($"No file exists at \"{file}\""); if (!File.Exists(file)) throw new($"No file exists at \"{file}\"");
@ -346,15 +342,13 @@ public static class BaseModule
}); });
} }
[Command("exit")]
[Command("quit")] [Command("quit")]
public static void QuitShell(int code = 0) public static void QuitShell(int code = 0)
{ {
Environment.Exit(code); Environment.Exit(code);
} }
[Command("type")]
public static void Type(string file) => Print(file);
[Command("undo")] [Command("undo")]
public static void UndoCommand(int amount = 1) public static void UndoCommand(int amount = 1)
{ {

View File

@ -1,6 +1,6 @@
namespace SrcMod.Shell.Modules.ObjectModels; namespace SrcMod.Shell.Modules.ObjectModels;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class CommandAttribute : Attribute public class CommandAttribute : Attribute
{ {
public readonly string NameId; public readonly string NameId;

View File

@ -17,26 +17,37 @@ public class CommandInfo
RequiredParameters = 0; RequiredParameters = 0;
} }
public static CommandInfo? FromMethod(ModuleInfo parentModule, MethodInfo info) public static CommandInfo[] FromMethod(ModuleInfo parentModule, MethodInfo info)
{ {
CommandAttribute? attribute = info.GetCustomAttribute<CommandAttribute>(); // This is a little redundant as we're duplicating a bunch of info,
if (attribute is null) return null; // but honestly, it isn't too bad. Maybe there will be an improvement
// in the future, maybe not. But not for a while because this works.
if (info.ReturnType != typeof(void)) return null; if (info.ReturnType != typeof(void)) return Array.Empty<CommandInfo>();
ParameterInfo[] param = info.GetParameters(); ParameterInfo[] param = info.GetParameters();
int required = 0; int required = 0;
while (required < param.Length && !param[required].IsOptional) required++; while (required < param.Length && !param[required].IsOptional) required++;
return new() List<CommandInfo> commands = new();
CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray();
if (attributes.Length <= 0) return Array.Empty<CommandInfo>();
foreach (CommandAttribute attribute in attributes)
{ {
Method = info, commands.Add(new()
Module = parentModule, {
Name = info.Name, Method = info,
NameId = attribute.NameId, Module = parentModule,
Parameters = param, Name = info.Name,
RequiredParameters = required NameId = attribute.NameId,
}; Parameters = param,
RequiredParameters = required
});
}
return commands.ToArray();
} }
public void Invoke(params string[] args) public void Invoke(params string[] args)

View File

@ -18,7 +18,7 @@ public class ModuleInfo
NameIsPrefix = true; NameIsPrefix = true;
} }
public static ModuleInfo? FromModule(Type info) public static ModuleInfo? FromType(Type info)
{ {
ModuleAttribute? attribute = info.GetCustomAttribute<ModuleAttribute>(); ModuleAttribute? attribute = info.GetCustomAttribute<ModuleAttribute>();
if (attribute is null) return null; if (attribute is null) return null;
@ -37,9 +37,9 @@ public class ModuleInfo
List<CommandInfo> commands = new(); List<CommandInfo> commands = new();
foreach (MethodInfo method in info.GetMethods()) foreach (MethodInfo method in info.GetMethods())
{ {
CommandInfo? cmd = CommandInfo.FromMethod(module, method); CommandInfo[] cmds = CommandInfo.FromMethod(module, method);
if (cmd is null) continue; if (cmds.Length <= 0) continue;
commands.Add(cmd); commands.AddRange(cmds);
} }
module.Commands.AddRange(commands); module.Commands.AddRange(commands);

View File

@ -4,7 +4,7 @@ public class Shell
{ {
public const string Author = "That_One_Nerd"; public const string Author = "That_One_Nerd";
public const string Name = "SrcMod"; public const string Name = "SrcMod";
public const string Version = "Alpha 0.1.0"; public const string Version = "Alpha 0.2.2";
public readonly string? ShellDirectory; public readonly string? ShellDirectory;
@ -62,7 +62,7 @@ public class Shell
} }
foreach (Type t in possibleModules) foreach (Type t in possibleModules)
{ {
ModuleInfo? module = ModuleInfo.FromModule(t); ModuleInfo? module = ModuleInfo.FromType(t);
if (module is not null) if (module is not null)
{ {
LoadedModules.Add(module); LoadedModules.Add(module);

View File

@ -7,6 +7,8 @@ public static class Tools
private static int loadingPosition = -1; private static int loadingPosition = -1;
private static int lastLoadingBufferSize = 0; private static int lastLoadingBufferSize = 0;
private static int lastLoadingValue = -1; private static int lastLoadingValue = -1;
private static float loadingBarValue = 0;
private static ConsoleColor loadingBarColor = Console.ForegroundColor;
public static bool LoadingBarEnabled { get; private set; } public static bool LoadingBarEnabled { get; private set; }
@ -101,6 +103,9 @@ public static class Tools
Int2 oldPos = (Console.CursorLeft, Console.CursorTop); Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
loadingBarValue = value;
loadingBarColor = color ?? Console.ForegroundColor;
// Erase last bar. // Erase last bar.
Console.SetCursorPosition(0, loadingPosition); Console.SetCursorPosition(0, loadingPosition);
Console.Write(new string(' ', lastLoadingBufferSize)); Console.Write(new string(' ', lastLoadingBufferSize));
@ -119,14 +124,20 @@ public static class Tools
Write(right, newLine: false); Write(right, newLine: false);
if (oldPos.y == Console.CursorTop) oldPos.y++; if (oldPos.y == Console.CursorTop) oldPos.y++;
while (oldPos.y >= Console.BufferHeight)
{
Console.WriteLine();
oldPos.y--;
loadingPosition--;
}
Console.SetCursorPosition(oldPos.x, oldPos.y); Console.SetCursorPosition(oldPos.x, oldPos.y);
} }
public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null) public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null)
{ {
if (loadingPosition != -1) throw new("The loading bar has already been enabled."); if (loadingPosition != -1) throw new("The loading bar has already been enabled.");
loadingPosition = position ?? Console.CursorTop; loadingPosition = position ?? Console.CursorTop;
LoadingBarSet(value, color);
LoadingBarEnabled = true; LoadingBarEnabled = true;
LoadingBarSet(value, color);
} }
public static void Write(object? message, ConsoleColor? col = null, bool newLine = true) public static void Write(object? message, ConsoleColor? col = null, bool newLine = true)
@ -138,6 +149,12 @@ public static class Tools
else Console.Write(message); else Console.Write(message);
Console.ForegroundColor = prevCol; Console.ForegroundColor = prevCol;
if (newLine && LoadingBarEnabled && Console.CursorTop >= Console.BufferHeight - 1)
{
loadingPosition--;
LoadingBarSet(loadingBarValue, loadingBarColor);
}
} }
public static bool ValidateUnsafe() public static bool ValidateUnsafe()