Switched to dependency injection for console use.
Created interfaces for input and output for better structure and easier testing.
This commit is contained in:
68
RecrownedGTK.Tools/ConsoleInterface/ConsoleProgarm.cs
Normal file
68
RecrownedGTK.Tools/ConsoleInterface/ConsoleProgarm.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using RecrownedGTK.Tools.CommandProcessor;
|
||||
using RecrownedGTK.Tools.CommandProcessor.Commands;
|
||||
using RecrownedGTK.Tools.NinePatchTools;
|
||||
using RecrownedGTK.Tools.TextureAtlasTools;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace RecrownedGTK.Tools.ConsoleInterface
|
||||
{
|
||||
internal class ConsoleProgram
|
||||
{
|
||||
private class ConsoleInput : IUserInput {
|
||||
public string GetInput() {
|
||||
return Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
private class ConsoleOutput : IUserOutput {
|
||||
public void Output(string output) {
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
|
||||
public void WrappedOutput(string output) {
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth(output);
|
||||
}
|
||||
|
||||
public void ClearOutput() {
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
static void Main(string[] args)
|
||||
{
|
||||
ConsoleInput consoleInput = new ConsoleInput();
|
||||
ConsoleOutput consoleOutput = new ConsoleOutput();
|
||||
CommandEngine ce = new CommandEngine();
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth("Recrowned Athenaeum Console Tools version " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth("Type \"help\" for help.");
|
||||
|
||||
ce.commands.Add(new HelpCommand(ce));
|
||||
ce.commands.Add(new TexturePackerCommand());
|
||||
ce.commands.Add(new StopCommand(ce));
|
||||
ce.commands.Add(new ClearConsoleCommand());
|
||||
ce.commands.Add(new NinePatchCommand());
|
||||
|
||||
if (args.Length > 0)
|
||||
{
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth("Executing as one time use.");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < args.Length; i++) sb.Append(args[i] + ' ');
|
||||
string commandAndArgs = sb.ToString().TrimEnd();
|
||||
try
|
||||
{
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth("Command and argument received: " + commandAndArgs);
|
||||
ce.Process(consoleInput, consoleOutput, commandAndArgs);
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
ConsoleUtilities.WrapMessageToConsoleWidth("An error has occurred: " + e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ce.Run(consoleInput, consoleOutput);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
RecrownedGTK.Tools/ConsoleInterface/ConsoleUtilities.cs
Normal file
39
RecrownedGTK.Tools/ConsoleInterface/ConsoleUtilities.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace RecrownedGTK.Tools.ConsoleInterface
|
||||
{
|
||||
internal class ConsoleUtilities
|
||||
{
|
||||
public static string WrapMessageToConsoleWidth(string message)
|
||||
{
|
||||
string[] words = message.Split(' ');
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
int currentLineSize = 0;
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
if (currentLineSize + words[i].Length >= Console.BufferWidth - Console.CursorLeft -1)
|
||||
{
|
||||
currentLineSize = 0;
|
||||
stringBuilder.AppendLine();
|
||||
}
|
||||
if (words[i].Contains("\n"))
|
||||
{
|
||||
currentLineSize = 0;
|
||||
}
|
||||
currentLineSize += words[i].Length + 1;
|
||||
if (words[i].Contains("\n"))
|
||||
{
|
||||
currentLineSize -= 2;
|
||||
}
|
||||
stringBuilder.Append(words[i]);
|
||||
if (i + 1 < words.Length)
|
||||
{
|
||||
stringBuilder.Append(' ');
|
||||
}
|
||||
}
|
||||
stringBuilder.AppendLine();
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user