A NuGet package. It aim is to help you write extendable and command-based console programs in C# and .Net. https://www.craigoates.net/Software/Project/7
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
4.7 KiB

using Console.Waterworks.Assistants;
using Console.Waterworks.Constants;
using Console.Waterworks.Loggers;
using Console.Waterworks.Models;
using Console.Waterworks.Specialists;
using System;
namespace Console.Waterworks.CoOrdinators
{
class CoOrdinator
{
CoOrdinatorAssistant Assistant = new CoOrdinatorAssistant();
ProgramInfoSpecialist ProgInfoSpec = new ProgramInfoSpecialist();
ConsoleIOSpecialist ConsoleSpec = new ConsoleIOSpecialist();
CommandsSpecialist CommSpec = new CommandsSpecialist();
HelpSpecialist HelpSpec = new HelpSpecialist();
CW_Logger Logger = new CW_Logger();
internal void PrepareConsoleEnvironment()
{
Logger.LogInfoMessage($"Preparing console..");
ConsoleSpec.PrepareConsoleEnvironment();
ConsoleSpec.SetInputPrompt($"{ProgInfoSpec.GetProductName()}> ");
Logger.LogSuccessMessage("Console environment is now setup");
}
internal void DisplayProgramInfo()
{
Logger.LogInfoMessage($"Displaying program information..");
Assistant.SetConsoleTitle(Logger, ProgInfoSpec, ConsoleSpec);
Assistant.OutputProgramInfo(Logger, ProgInfoSpec, ConsoleSpec);
}
internal void RunProgram(string commandsNamespace)
{
Logger.LogInfoMessage("Attempting to building commands library..");
var commandClasses = CommSpec.GetCommandClasses(commandsNamespace);
var commandLibraries = CommSpec.GetCommandLibraries(commandClasses);
Assistant.LogCommandGatheringAttempt(commandClasses, commandLibraries, Logger);
while (true)
{
var consoleInput = ConsoleSpec.GetInputFromUser();
if (string.IsNullOrEmpty(consoleInput)) continue;
try
{
Logger.LogInfoMessage("Parsing input from user..");
var command = new Command(consoleInput, commandsNamespace, CW_Constants.COMMAND_CLASS_NAME);
Logger.LogInfoMessage("Attempting to execute command..");
var result = CommSpec.ExecuteCommand(commandsNamespace, command, commandClasses, commandLibraries, ConsoleSpec);
ConsoleSpec.WriteOutputToConsole(result);
Logger.LogSuccessMessage("Command has been executed.");
Logger.LogNoteMessage("An error message does not mean the command did not execute properly or sucessfully.");
}
catch (Exception ex)
{
Logger.LogErrorMessage("Command was not successfully executed. See the error message in console for further details");
ConsoleSpec.WriteErrorMessage(ex.Message);
}
Logger.LogInfoMessage("Resetting the console's formatting..");
Logger.LogNoteMessage("This is to make sure no error messages or one-off formating change corrupts the console environment.");
ConsoleSpec.ResetConsoleColour();
Logger.LogSuccessMessage("Console's formating has been reset");
}
}
internal string DisplayHelpSection(string commandsNamespace)
{
Logger.LogInfoMessage("Attempting to display help section..");
var commandClasses = CommSpec.GetCommandClasses(commandsNamespace);
if (commandClasses.Count == 0)
{
Logger.LogErrorMessage("Unable to find any help information. Make sure the commands hace the correct atrributes applied");
ConsoleSpec.WriteErrorSuffix();
return "Unable to find help information";
}
Logger.LogSuccessMessage("Found help information");
Logger.LogInfoMessage("Attempting to display the help information..");
ConsoleSpec.WriteOutputToConsole("Displaying Help section.");
ConsoleSpec.LineBreak();
var commandMembers = HelpSpec.GetCommandMembers(commandClasses);
foreach (var command in commandMembers)
{
if (HelpSpec.ListCommand(command) == true)
{
ConsoleSpec.WriteOutputToConsole($"Command Name: {command.Name}");
ConsoleSpec.WriteOutputToConsole($"Parametres: {HelpSpec.GetParametres(command)}");
ConsoleSpec.WriteOutputToConsole($"Description: {HelpSpec.GetDescription(command)}");
ConsoleSpec.WriteOutputToConsole($"Example: {HelpSpec.GetUsageExamples(command)}");
ConsoleSpec.LineBreak();
}
}
Logger.LogSuccessMessage("Help section displayed in the console");
return "End of Help section.";
}
}
}