Skip to content

Commit 03ac8ce

Browse files
committed
Separate CLI helpers into static class
1 parent 86e5b8d commit 03ac8ce

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Riverside.JsonBinder.Console.Helpers;
2+
3+
public static class ConsoleHelpers
4+
{
5+
/// <summary>
6+
/// Displays an error message with the specified title and details.
7+
/// </summary>
8+
/// <param name="title">The title of the error.</param>
9+
/// <param name="details">The details of the error.</param>
10+
public static void DisplayError(string title, string details)
11+
{
12+
DisplayRedMessage($"\nError: {title}", false);
13+
DisplayRedMessage($"Details: {details}\n");
14+
}
15+
16+
/// <summary>
17+
/// Displays a message in red color.
18+
/// </summary>
19+
/// <param name="message">The message to display.</param>
20+
/// <param name="colorResets">Indicates whether to reset the color after displaying the message.</param>
21+
public static void DisplayRedMessage(string message, bool? colorResets = true)
22+
{
23+
System.Console.ForegroundColor = ConsoleColor.Red;
24+
System.Console.WriteLine(message);
25+
if (colorResets is true)
26+
System.Console.ResetColor();
27+
}
28+
29+
/// <summary>
30+
/// Displays a "Press any key to continue" message and waits for input.
31+
/// </summary>
32+
public static void PressAnyKey()
33+
{
34+
System.Console.WriteLine("\nPress any key to continue...");
35+
System.Console.ReadKey();
36+
}
37+
}

Diff for: src/Riverside.JsonBinder.Console/Program.cs

+12-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.CommandLine;
22
using System.CommandLine.NamingConventionBinder;
33
using System.Text.Json;
4+
using Riverside.JsonBinder.Console.Helpers;
45

56
namespace Riverside.JsonBinder.Console;
67

@@ -59,7 +60,7 @@ private static void RunInteractiveMode()
5960
return;
6061
break;
6162
default:
62-
DisplayRedMessage("Invalid option. Please select a valid menu option.\n");
63+
ConsoleHelpers.DisplayRedMessage("Invalid option. Please select a valid menu option.\n");
6364
break;
6465
}
6566
}
@@ -99,8 +100,8 @@ private static void ConvertJsonToClassesInteractive()
99100
var selectedLanguages = SelectLanguagesInteractive();
100101
if (selectedLanguages.Count == 0)
101102
{
102-
DisplayRedMessage("\nNo languages selected. Please try again.\n");
103-
PressAnyKey();
103+
ConsoleHelpers.DisplayRedMessage("\nNo languages selected. Please try again.\n");
104+
ConsoleHelpers.PressAnyKey();
104105
return;
105106
}
106107

@@ -121,15 +122,15 @@ private static void ConvertJsonToClassesInteractive()
121122
}
122123
catch (JsonException ex)
123124
{
124-
DisplayError("Invalid JSON format.", ex.Message);
125+
ConsoleHelpers.DisplayError("Invalid JSON format.", ex.Message);
125126
}
126127
catch (Exception ex)
127128
{
128-
DisplayError("An unexpected error occurred.", ex.Message);
129+
ConsoleHelpers.DisplayError("An unexpected error occurred.", ex.Message);
129130
}
130131
}
131132

132-
PressAnyKey();
133+
ConsoleHelpers.PressAnyKey();
133134
}
134135

135136
/// <summary>
@@ -192,13 +193,13 @@ private static void ConvertJsonToClasses(string json, string[] languages)
192193
{
193194
if (string.IsNullOrWhiteSpace(json))
194195
{
195-
DisplayRedMessage("No JSON provided. Please try again.\n");
196+
ConsoleHelpers.DisplayRedMessage("No JSON provided. Please try again.\n");
196197
return;
197198
}
198199

199200
if (languages == null || languages.Length == 0)
200201
{
201-
DisplayRedMessage("No languages selected. Please try again.\n");
202+
ConsoleHelpers.DisplayRedMessage("No languages selected. Please try again.\n");
202203
return;
203204
}
204205

@@ -214,16 +215,16 @@ private static void ConvertJsonToClasses(string json, string[] languages)
214215
}
215216
catch (JsonException ex)
216217
{
217-
DisplayError("Invalid JSON format.", ex.Message);
218+
ConsoleHelpers.DisplayError("Invalid JSON format.", ex.Message);
218219
}
219220
catch (Exception ex)
220221
{
221-
DisplayError("An unexpected error occurred.", ex.Message);
222+
ConsoleHelpers.DisplayError("An unexpected error occurred.", ex.Message);
222223
}
223224
}
224225
else
225226
{
226-
DisplayRedMessage($"\nInvalid language choice: {choice.Trim()}\n");
227+
ConsoleHelpers.DisplayRedMessage($"\nInvalid language choice: {choice.Trim()}\n");
227228
}
228229
}
229230
}
@@ -260,37 +261,4 @@ private static bool ConfirmExit()
260261
string? confirmation = System.Console.ReadLine()?.Trim().ToLower();
261262
return confirmation == "y" || confirmation == "yes";
262263
}
263-
264-
/// <summary>
265-
/// Displays an error message with the specified title and details.
266-
/// </summary>
267-
/// <param name="title">The title of the error.</param>
268-
/// <param name="details">The details of the error.</param>
269-
private static void DisplayError(string title, string details)
270-
{
271-
DisplayRedMessage($"\nError: {title}", false);
272-
DisplayRedMessage($"Details: {details}\n");
273-
}
274-
275-
/// <summary>
276-
/// Displays a message in red color.
277-
/// </summary>
278-
/// <param name="message">The message to display.</param>
279-
/// <param name="colorResets">Indicates whether to reset the color after displaying the message.</param>
280-
private static void DisplayRedMessage(string message, bool? colorResets = true)
281-
{
282-
System.Console.ForegroundColor = ConsoleColor.Red;
283-
System.Console.WriteLine(message);
284-
if (colorResets is true)
285-
System.Console.ResetColor();
286-
}
287-
288-
/// <summary>
289-
/// Displays a "Press any key to continue" message and waits for input.
290-
/// </summary>
291-
private static void PressAnyKey()
292-
{
293-
System.Console.WriteLine("\nPress any key to continue...");
294-
System.Console.ReadKey();
295-
}
296264
}

0 commit comments

Comments
 (0)