mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 00:56:19 +02:00
60 lines
1.5 KiB
C#
60 lines
1.5 KiB
C#
using System;
|
|
|
|
namespace RemSox.UI.GUI.CLI;
|
|
|
|
public static class CommandManager
|
|
{
|
|
private static readonly Dictionary<string, ICommand> commands = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public static void RegisterCommand(ICommand command)
|
|
{
|
|
commands[command.Name] = command;
|
|
}
|
|
|
|
public static void RegisterCommands(IEnumerable<ICommand> commandsToRegister)
|
|
{
|
|
foreach (ICommand command in commandsToRegister)
|
|
{
|
|
RegisterCommand(command);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<ICommand> GetCommands()
|
|
{
|
|
return commands.Values.OrderBy(command => command.Name);
|
|
}
|
|
|
|
public static bool TryExecute(string input)
|
|
{
|
|
string trimmedInput = input.Trim();
|
|
|
|
if (trimmedInput.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (KeyValuePair<string, ICommand> 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.Substring(commandName.Length).TrimStart();
|
|
}
|
|
|
|
entry.Value.Execute(arguments);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |