Skip to content

Commit

Permalink
Separate CLI helpers into static class
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamparter committed Jan 18, 2025
1 parent 86e5b8d commit 03ac8ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
37 changes: 37 additions & 0 deletions src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Riverside.JsonBinder.Console.Helpers;

public static class ConsoleHelpers

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'

Check warning on line 3 in src/Riverside.JsonBinder.Console/Helpers/ConsoleHelpers.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'ConsoleHelpers'
{
/// <summary>
/// Displays an error message with the specified title and details.
/// </summary>
/// <param name="title">The title of the error.</param>
/// <param name="details">The details of the error.</param>
public static void DisplayError(string title, string details)
{
DisplayRedMessage($"\nError: {title}", false);
DisplayRedMessage($"Details: {details}\n");
}

/// <summary>
/// Displays a message in red color.
/// </summary>
/// <param name="message">The message to display.</param>
/// <param name="colorResets">Indicates whether to reset the color after displaying the message.</param>
public static void DisplayRedMessage(string message, bool? colorResets = true)
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine(message);
if (colorResets is true)
System.Console.ResetColor();
}

/// <summary>
/// Displays a "Press any key to continue" message and waits for input.
/// </summary>
public static void PressAnyKey()
{
System.Console.WriteLine("\nPress any key to continue...");
System.Console.ReadKey();
}
}
56 changes: 12 additions & 44 deletions src/Riverside.JsonBinder.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.Text.Json;
using Riverside.JsonBinder.Console.Helpers;

namespace Riverside.JsonBinder.Console;

Expand Down Expand Up @@ -59,7 +60,7 @@ private static void RunInteractiveMode()
return;
break;
default:
DisplayRedMessage("Invalid option. Please select a valid menu option.\n");
ConsoleHelpers.DisplayRedMessage("Invalid option. Please select a valid menu option.\n");
break;
}
}
Expand Down Expand Up @@ -99,8 +100,8 @@ private static void ConvertJsonToClassesInteractive()
var selectedLanguages = SelectLanguagesInteractive();
if (selectedLanguages.Count == 0)
{
DisplayRedMessage("\nNo languages selected. Please try again.\n");
PressAnyKey();
ConsoleHelpers.DisplayRedMessage("\nNo languages selected. Please try again.\n");
ConsoleHelpers.PressAnyKey();
return;
}

Expand All @@ -121,15 +122,15 @@ private static void ConvertJsonToClassesInteractive()
}
catch (JsonException ex)
{
DisplayError("Invalid JSON format.", ex.Message);
ConsoleHelpers.DisplayError("Invalid JSON format.", ex.Message);
}
catch (Exception ex)
{
DisplayError("An unexpected error occurred.", ex.Message);
ConsoleHelpers.DisplayError("An unexpected error occurred.", ex.Message);
}
}

PressAnyKey();
ConsoleHelpers.PressAnyKey();
}

/// <summary>
Expand Down Expand Up @@ -192,13 +193,13 @@ private static void ConvertJsonToClasses(string json, string[] languages)
{
if (string.IsNullOrWhiteSpace(json))
{
DisplayRedMessage("No JSON provided. Please try again.\n");
ConsoleHelpers.DisplayRedMessage("No JSON provided. Please try again.\n");
return;
}

if (languages == null || languages.Length == 0)
{
DisplayRedMessage("No languages selected. Please try again.\n");
ConsoleHelpers.DisplayRedMessage("No languages selected. Please try again.\n");
return;
}

Expand All @@ -214,16 +215,16 @@ private static void ConvertJsonToClasses(string json, string[] languages)
}
catch (JsonException ex)
{
DisplayError("Invalid JSON format.", ex.Message);
ConsoleHelpers.DisplayError("Invalid JSON format.", ex.Message);
}
catch (Exception ex)
{
DisplayError("An unexpected error occurred.", ex.Message);
ConsoleHelpers.DisplayError("An unexpected error occurred.", ex.Message);
}
}
else
{
DisplayRedMessage($"\nInvalid language choice: {choice.Trim()}\n");
ConsoleHelpers.DisplayRedMessage($"\nInvalid language choice: {choice.Trim()}\n");
}
}
}
Expand Down Expand Up @@ -260,37 +261,4 @@ private static bool ConfirmExit()
string? confirmation = System.Console.ReadLine()?.Trim().ToLower();
return confirmation == "y" || confirmation == "yes";
}

/// <summary>
/// Displays an error message with the specified title and details.
/// </summary>
/// <param name="title">The title of the error.</param>
/// <param name="details">The details of the error.</param>
private static void DisplayError(string title, string details)
{
DisplayRedMessage($"\nError: {title}", false);
DisplayRedMessage($"Details: {details}\n");
}

/// <summary>
/// Displays a message in red color.
/// </summary>
/// <param name="message">The message to display.</param>
/// <param name="colorResets">Indicates whether to reset the color after displaying the message.</param>
private static void DisplayRedMessage(string message, bool? colorResets = true)
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine(message);
if (colorResets is true)
System.Console.ResetColor();
}

/// <summary>
/// Displays a "Press any key to continue" message and waits for input.
/// </summary>
private static void PressAnyKey()
{
System.Console.WriteLine("\nPress any key to continue...");
System.Console.ReadKey();
}
}

0 comments on commit 03ac8ce

Please sign in to comment.