namespace RemSox.UI.CLI; public static class CommandManager { private static readonly Dictionary commands = new(StringComparer.OrdinalIgnoreCase); private static readonly List commandHistory = []; public static void RegisterCommand(ICommand command) { commands[command.Name] = command; } public static void RegisterCommands(IEnumerable commandsToRegister) { foreach (ICommand command in commandsToRegister) { RegisterCommand(command); } } public static IEnumerable GetCommands() { return commands.Values.OrderBy(command => command.Name); } public static IList GetCommandHistory() { return commandHistory; } public static async Task TryExecuteAsync(string input, Action printLine, CancellationToken cancellationToken = default) { string trimmedInput = input.Trim(); if (trimmedInput.Length == 0) { return false; } if (commandHistory.Count == 0 || commandHistory[^1] != trimmedInput) { commandHistory.Add(trimmedInput); } foreach (KeyValuePair entry in commands.OrderByDescending(entry => entry.Key.Length)) { string commandName = entry.Key; if (!trimmedInput.Equals(commandName, StringComparison.OrdinalIgnoreCase) && !trimmedInput.StartsWith(commandName + " ", StringComparison.OrdinalIgnoreCase)) { continue; } string? arguments = null; if (trimmedInput.Length > commandName.Length) { arguments = trimmedInput[commandName.Length..].TrimStart(); } CancellationTokenRegistration unused = cancellationToken.Register(async () => await entry.Value.StopAsync()); await entry.Value.ExecuteAsync(arguments, printLine); return true; } return false; } }