Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Hephaestus.Common/Classes/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public static void Launch(Game game)
{
try
{
ConsoleUtility.Info($"Starting {Path.GetFileName(game.GameExecutable)}");
ConsoleUtility.PrintInfo($"Starting {Path.GetFileName(game.GameExecutable)}");

Process.Start(new ProcessStartInfo(game.GameExecutable, game.GameExecutableArguments));
}
catch (Exception e)
{
ConsoleUtility.Error($"Failed to start {game.GameExecutable} because {e.Message}");
ConsoleUtility.PrintError($"Failed to start {game.GameExecutable} because {e.Message}");
}
}

Expand All @@ -55,7 +55,7 @@ public static void Shutdown(string processToShutdown)
continue;
}

ConsoleUtility.Info($"Found {processName}. Terminating process...");
ConsoleUtility.PrintInfo($"Found {processName}. Terminating process...");

try
{
Expand All @@ -71,7 +71,7 @@ public static void Shutdown(string processToShutdown)
}
catch (Exception e)
{
ConsoleUtility.Error($"error: Failed to shutdown {process} because {e.Message}");
ConsoleUtility.PrintError($"error: Failed to shutdown {process} because {e.Message}");
}
}
}
Expand Down
105 changes: 43 additions & 62 deletions Hephaestus.Common/Utilities/ConsoleUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,96 @@

namespace Hephaestus.Common.Utilities
{
/// <summary>
/// Provides a set of faculties to handle various console related tasks.
/// </summary>
public static class ConsoleUtility
{
/// <summary>
/// Loops till the user gives a Y or N answer which then is converted into a boolean.
/// Prints a given message to the console which acts as a question to the user from which they respond with either Y for yes or N for no.
/// </summary>
/// <param name="question">Question to ask the user. Question is transformed into 'questionHere (Y/N): '.</param>
/// <returns>Returns true if user selected yes and returns false if user selected no.</returns>
/// <param name="question">String to output to the console and act as a question to the user. The provided string is outputed in the following format: 'questionHere (Y/N): '.</param>
/// <returns>Returns true if the user entered Y for yes or false if the user entered N for no.</returns>
public static bool AskYesNoQuestion(string question)
{
bool result;

string response;
Console.Write($"{question} (Y/N): ");

while (true)
{
Console.Write($"{question} (Y/N): ");
string key = Console.ReadLine()?.ToLower();

if (key == "y")
{
result = true;
break;
}
response = Console.ReadLine()?.ToLower();

if (key == "n")
if (response != "y" || response != "yes" || response != "n" || response != "no")
{
result = false;
break;
Console.WriteLine($"{response} is not a valid anwser to the given question: {question}. Please enter either Y for yes or N for no.");
continue;
}

Console.WriteLine("Incorrect selection. Try again.");
break;
}

return result;
return response == "y" || response == "yes" ? true : false;
}

/// <summary>
/// Ask the user to enter a string.
/// Prints a given message to the console and then prompts the user to enter a string.
/// </summary>
/// <param name="message">Message displayed to the user. Message is transformed into 'messageHere: '.</param>
/// <returns></returns>
/// <param name="message">String to output to the console and act as a message to the user. The provided string is outputed in the following format: 'messageHere: '.</param>
/// <returns>Returns the entered string</returns>
public static string AskToEnterString(string message)
{
Console.Write($"{message}: ");

string enteredString = Console.ReadLine();

return enteredString;
return Console.ReadLine();
}

/// <summary>
/// Ask the user to enter either a directory or a file path.
/// Prints a given message to the console and then prompts the user to enter a filesystem path corresponding to either a directory or a file.
/// </summary>
/// <param name="message">Message displayed to the user. Message is transformed into 'messageHere: '.</param>
/// <param name="pathType">Select what type of path should the user enter.</param>
/// <param name="message">String to output to the console and act as a message to the user. The provided string is outputed in the following format: 'messageHere: '.</param>
/// <param name="pathType">The type of path the user needs to enter. Either a directory or an individual file.</param>
/// <returns>Path to the directory or file.</returns>
public static string AskToEnterPath(string message, PathType pathType)
public static string AskToEnterFileSystemPath(string message, PathType pathType)
{
string enteredString;
string enteredPath;
Console.Write($"{message}: ");

while (true)
{
Console.Write($"{message}: ");

enteredString = Console.ReadLine();

if (pathType == PathType.Directory)
enteredPath = Console.ReadLine();
if ((Directory.Exists(enteredPath) && pathType == PathType.Directory) || (File.Exists(enteredPath) && pathType == PathType.File))
{
if (Directory.Exists(enteredString))
{
break;
}

Console.WriteLine("That is not a valid directory. Try again.");
break;
}
else
{
if (File.Exists(enteredString))
{
break;
}

Console.WriteLine("That is not a valid file. Try again.");
}
string invalidPathMessage = pathType == PathType.Directory ?
$"{enteredPath} does not exist or does not represent a valid directory path. Please try again and enter a valid directory path." :
$"{enteredPath} does not exist or does not represent a valid file path. Please try again and enter a valid file path.";
Console.WriteLine(invalidPathMessage);
}

return enteredString;
return enteredPath;
}

/// <summary>
/// Logs an error message to the console.
/// Prints a given message to the console acting as a error message to the user.
/// </summary>
/// <param name="message">Message to the user. Message is transformed into 'error: messageHere'.</param>
public static void Error(string message)
/// <param name="message">String to output to the console and act as a error message to the user. The provided string is outputed in the following format: 'error: messageHere'.</param>
public static void PrintError(string message)
{
Console.Error.WriteLine($"error: {message}");
}

/// <summary>
/// Logs a warning to the console.
/// Prints a given message to the console acting as a warning message to the user.
/// </summary>
/// <param name="message">Message to the user. Message is transformed into 'warning: messageHere'.</param>
public static void Warning(string message)
/// <param name="message">String to output to the console and act as a warning message to the user. The provided string is outputed in the following format: 'warning: messageHere'.</param>
public static void PrintWarning(string message)
{
Console.WriteLine($"warning: {message}");
}

/// <summary>
/// Logs an info message to the console.
/// Prints a given message to the console as a informative message to the user.
/// </summary>
/// <param name="message">Message to the user. Message is transformed into 'info: messageHere'.</param>
public static void Info(string message)
/// <param name="message">String to output to the console and act as a informative message to the user. The provided string is outputed in the following format: 'info: messageHere'.</param>
public static void PrintInfo(string message)
{
Console.WriteLine($"info: {message}");
}
Expand Down