Refactor CLI command system to support async execution and central command history

This commit is contained in:
Stone_Red
2026-06-11 20:40:26 +02:00
parent 2b091e6e7c
commit 160d0559e8
14 changed files with 114 additions and 63 deletions
+14 -2
View File
@@ -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;
}