Switched to dependency injection for console use.

Created interfaces for input and output for better structure and easier testing.
This commit is contained in:
2020-02-23 20:51:39 -05:00
parent 72a1ba903b
commit fa74bc9ae5
12 changed files with 119 additions and 105 deletions

View File

@@ -14,25 +14,25 @@ namespace RecrownedGTK.Tools.CommandProcessor
commands = new List<EngineCommand>();
}
public void Run(IUserInput input)
public void Run(IUserInput input, IUserOutput output)
{
while (running)
{
ConsoleUtilities.WriteWrappedLine("\nAwaiting command.");
output.WrappedOutput("\nAwaiting command.");
string command = input.GetInput();
try
{
Process(command);
Process(input, output, command);
}
catch (ArgumentException ae)
{
ConsoleUtilities.WriteWrappedLine("Error: " + ae.Message);
output.WrappedOutput("Error: " + ae.Message);
}
}
}
public void Process(string commandAndArguments)
public void Process(IUserInput userInput, IUserOutput userOutput, string commandAndArguments)
{
string command = commandAndArguments;
string[] arguments = null;
@@ -72,7 +72,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
arguments = argumentsList.ToArray();
}
if (!ContainsCommand(command)) throw new ArgumentException("Command not found. Type \"help\" for a list of commands.");
GetCommand(command).Run(arguments);
GetCommand(command).Run(userInput, userOutput, arguments);
}
public bool ContainsCommand(string command)

View File

@@ -9,9 +9,9 @@ namespace RecrownedGTK.Tools.CommandProcessor.Commands
help = "Clears the console.";
}
public override void Run(string[] arguments = null)
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
{
Console.Clear();
userOutput.ClearOutput();
}
}
}

View File

@@ -13,16 +13,16 @@ namespace RecrownedGTK.Tools.CommandProcessor.Commands
}
public override void Run(string[] arguments)
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments)
{
if (arguments != null)
{
if (commandEngine.ContainsCommand(arguments[0]))
{
if (arguments.Length < 2) ConsoleUtilities.WriteWrappedLine(commandEngine.GetCommand(arguments[0]).Help(null));
if (arguments.Length < 2) userOutput.WrappedOutput(commandEngine.GetCommand(arguments[0]).Help(null));
for (int i = 1; i < arguments.Length; i++)
{
ConsoleUtilities.WriteWrappedLine(commandEngine.GetCommand(arguments[0]).Help(arguments[i]));
userOutput.WrappedOutput(commandEngine.GetCommand(arguments[0]).Help(arguments[i]));
}
}
else
@@ -32,20 +32,21 @@ namespace RecrownedGTK.Tools.CommandProcessor.Commands
}
else
{
ConsoleUtilities.WriteWrappedLine("Tools for RecrownedGTK library. Possible commands are as follows:\n");
userOutput.WrappedOutput("Tools for RecrownedGTK library. Possible commands are as follows:\n");
foreach (EngineCommand engineCommand in commandEngine.commands)
{
for (int i = 0; i < engineCommand.InvokeStrings.Length; i++)
{
ConsoleUtilities.WriteWrapped(engineCommand.InvokeStrings[i]);
userOutput.WrappedOutput(engineCommand.InvokeStrings[i]);
if (i + 1 < engineCommand.InvokeStrings.Length)
{
ConsoleUtilities.WriteWrapped(", ");
userOutput.WrappedOutput(", ");
}
}
ConsoleUtilities.WriteWrapped(" : ");
ConsoleUtilities.WriteWrapped(engineCommand.Help().Replace("\n", "\n\t"), true);
Console.WriteLine("--------");
userOutput.WrappedOutput(" : ");
userOutput.WrappedOutput(engineCommand.Help().Replace("\n", "\n\t"));
userOutput.WrappedOutput("\n");
userOutput.Output("--------");
}
}
}

View File

@@ -10,7 +10,7 @@
help = "Exits the tool.";
}
public override void Run(string[] arguments = null)
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
{
commandEngine.running = false;
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Text;
using RecrownedGTK.Tools.CommandProcessor;
namespace RecrownedGTK.Tools.CommandProcessor
{
@@ -51,7 +52,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
/// Runs the command.
/// </summary>
/// <param name="arguments">arguments to be used. May be null.</param>
public abstract void Run(string[] arguments = null);
public abstract void Run(IUserInput userInput = null, IUserOutput userOutput = null, string[] arguments = null);
/// <summary>
/// Check if given argument is viable for command.

View File

@@ -0,0 +1,8 @@
namespace RecrownedGTK.Tools.CommandProcessor {
public interface IUserOutput
{
void Output(string output);
void WrappedOutput(string output);
void ClearOutput();
}
}