Skip to content

Commit

Permalink
Use Spectre.Console
Browse files Browse the repository at this point in the history
Use Spectre.Console for selecting the demo to run.
  • Loading branch information
martincostello committed Sep 18, 2024
1 parent d4c14d1 commit 4368ec7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
50 changes: 17 additions & 33 deletions PollyTestClientConsole/Menu/ConsoleMenu.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Spectre.Console;

namespace PollyTestClientConsole.Menu;

public static class ConsoleMenu
Expand Down Expand Up @@ -53,43 +55,25 @@ public static void PrintSplashScreen()

public static void Run(List<ConsoleMenuItem> items)
{
int index = 0;
WriteMenu(items, items[index]);

bool isRunning = true;
while (isRunning)
while (true)
{
Action nextAction = Console.ReadKey().Key switch
{
ConsoleKey.DownArrow when index + 1 < items.Count => () => WriteMenu(items, items[++index]),
ConsoleKey.UpArrow when index - 1 >= 0 => () => WriteMenu(items, items[--index]),
ConsoleKey.Enter => () =>
{
Console.Clear();
items[index].Handler();
Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press any key to return to menu");
Console.ReadKey();
WriteMenu(items, items[index]);
},
ConsoleKey.Escape => () => isRunning = false,
_ => () => { }
};
nextAction();
}
Console.Clear();

Console.ReadKey();
}
var demo = AnsiConsole.Prompt(
new SelectionPrompt<ConsoleMenuItem>()
.Title("Select a demo to run.")
.PageSize(items.Count)
.AddChoices(items));

private static void WriteMenu(List<ConsoleMenuItem> items, ConsoleMenuItem selectedItem)
{
Console.Clear();
Console.WriteLine("Press Escape to return to menu.");

foreach (var item in items)
{
Console.Write(item == selectedItem ? "> " : " ");
Console.WriteLine(item.Name);
demo.Handler();

do
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
}
while (Console.ReadKey() is not { Key: ConsoleKey.Escape });
}
}
}
5 changes: 4 additions & 1 deletion PollyTestClientConsole/Menu/ConsoleMenuItem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
namespace PollyTestClientConsole.Menu;

public record ConsoleMenuItem(string Name, Action Handler);
public record ConsoleMenuItem(string Name, Action Handler)
{
public override string ToString() => Name;
}
3 changes: 3 additions & 0 deletions PollyTestClientConsole/PollyTestClientConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\PollyDemos\PollyDemos.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>
</Project>
13 changes: 7 additions & 6 deletions PollyTestClientConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using PollyDemos.OutputHelpers;
using PollyTestClientConsole;
using PollyTestClientConsole.Menu;
using Spectre.Console;
using Color = PollyDemos.OutputHelpers.Color;

Statistic[] statistics = [];
Progress<DemoProgress> progress = new();
Expand All @@ -11,7 +13,7 @@
{
foreach (var message in args.Messages)
{
WriteLineInColor(message.Message, message.Color.ToConsoleColor());
WriteLineInColor(message.Message, message.Color);
}

statistics = args.Statistics;
Expand Down Expand Up @@ -85,15 +87,14 @@ void PrintStatisticsThenClear()

foreach (Statistic stat in statistics)
{
WriteLineInColor($"{stat.Description.PadRight(longestDescription)}: {stat.Value}", stat.Color.ToConsoleColor());
WriteLineInColor($"{stat.Description.PadRight(longestDescription)}: {stat.Value}", stat.Color);
}

statistics = [];
}

void WriteLineInColor(string message, ConsoleColor color)
void WriteLineInColor(string message, Color color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
var consoleColor = Spectre.Console.Color.FromConsoleColor(color.ToConsoleColor());
AnsiConsole.MarkupLineInterpolated($"[{consoleColor}]{message}[/]");
}

0 comments on commit 4368ec7

Please sign in to comment.