mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-08 15:56:29 +02:00
Refactor CLI command system to support async execution and central command history
This commit is contained in:
@@ -4,6 +4,8 @@ public static class CommandManager
|
||||
{
|
||||
private static readonly Dictionary<string, ICommand> commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private static readonly List<string> commandHistory = [];
|
||||
|
||||
public static void RegisterCommand(ICommand command)
|
||||
{
|
||||
commands[command.Name] = command;
|
||||
@@ -22,7 +24,12 @@ public static class CommandManager
|
||||
return commands.Values.OrderBy(command => command.Name);
|
||||
}
|
||||
|
||||
public static bool TryExecute(string input, Action<string> printLine)
|
||||
public static IList<string> GetCommandHistory()
|
||||
{
|
||||
return commandHistory;
|
||||
}
|
||||
|
||||
public static async Task<bool> TryExecute(string input, Action<string> printLine)
|
||||
{
|
||||
string trimmedInput = input.Trim();
|
||||
|
||||
@@ -31,6 +38,11 @@ public static class CommandManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (commandHistory.Count == 0 || commandHistory[^1] != trimmedInput)
|
||||
{
|
||||
commandHistory.Add(trimmedInput);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands
|
||||
.OrderByDescending(entry => entry.Key.Length))
|
||||
{
|
||||
@@ -49,7 +61,7 @@ public static class CommandManager
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
entry.Value.Execute(arguments, printLine);
|
||||
await entry.Value.ExecuteAsync(arguments, printLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user