-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb23fbe
commit 0de3e45
Showing
16 changed files
with
253 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Spectre.Console; | ||
|
||
namespace BitzArt.Console; | ||
|
||
internal class FruitsMenu : ConsoleSelectionMenu | ||
{ | ||
public override string Title => "Fruits"; | ||
|
||
public FruitsMenu() | ||
{ | ||
AddSelection("apple", pauseOnComplete: true); | ||
AddSelection("pear", pauseOnComplete: true); | ||
AddSelection("banana", pauseOnComplete: true); | ||
} | ||
|
||
public override void OnSelection(ConsoleSelectionMenuItem selection) | ||
{ | ||
AnsiConsole.WriteLine($"You selected '{selection.Name}'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
namespace BitzArt.Console; | ||
|
||
internal class MainMenu(IConsoleAppNavigationManager navigation) : ConsoleMenuBase | ||
internal class MainMenu : ConsoleSelectionMenu | ||
{ | ||
public override string Title => "Main Menu"; | ||
protected override bool IsMainMenu => true; | ||
|
||
public IConsoleAppNavigationManager Navigation { get; } = navigation; | ||
public MainMenu() | ||
{ | ||
AddSubmenu<FruitsMenu>("Fruits"); | ||
AddSubmenu<VeggiesMenu>("Veggies"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Spectre.Console; | ||
|
||
namespace BitzArt.Console; | ||
|
||
internal class VeggiesMenu : ConsoleSelectionMenu | ||
{ | ||
public override string Title => "Veggies"; | ||
|
||
private bool _selectedPotato = false; | ||
|
||
public VeggiesMenu() | ||
{ | ||
AddSelection("tomato", pauseOnComplete: true); | ||
AddSelection("cucumber", pauseOnComplete: true); | ||
AddSelection("is potato a vegetable?", () => _selectedPotato = true, pauseOnComplete: true); | ||
} | ||
|
||
public override void OnSelection(ConsoleSelectionMenuItem selection) | ||
{ | ||
AnsiConsole.WriteLine($"You selected '{selection.Name}'"); | ||
} | ||
|
||
public override void OnSelectionInvoked(ConsoleSelectionMenuItem selection) | ||
{ | ||
if (_selectedPotato) | ||
{ | ||
AnsiConsole.WriteLine($"Potato is a vegetable."); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/BitzArt.Console/Exceptions/SelectionItemDuplicateException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace BitzArt.Console; | ||
|
||
internal class SelectionItemDuplicateException(string actionName) : Exception($"Duplicate selection item '{actionName}'") | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Spectre.Console; | ||
|
||
namespace BitzArt.Console; | ||
|
||
public abstract class ConsoleMenu : IConsoleMenu | ||
{ | ||
public ConsoleApp? App { get; set; } | ||
|
||
public virtual string Title => "Menu"; | ||
|
||
public Task RunAsync() | ||
{ | ||
Render(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public virtual void Render() | ||
{ | ||
AnsiConsole.Clear(); | ||
AnsiConsoleMenu.WriteTitle(Title); | ||
} | ||
|
||
public async Task RunAsync<TConsoleMenu>() where TConsoleMenu : IConsoleMenu | ||
{ | ||
var navigationManager = App!.Services.GetRequiredService<IConsoleAppNavigationManager>(); | ||
await navigationManager.NavigateAsync<TConsoleMenu>(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Spectre.Console; | ||
using static System.Collections.Specialized.BitVector32; | ||
|
||
namespace BitzArt.Console; | ||
|
||
public abstract class ConsoleSelectionMenu : ConsoleMenu | ||
{ | ||
/// <summary> | ||
/// Identifies whether this is a main menu of the app or not. | ||
/// </summary> | ||
protected virtual bool IsMainMenu => false; | ||
|
||
public override string Title => "Selection Menu"; | ||
|
||
protected virtual List<ConsoleSelectionMenuItem> SelectionItems { get; set; } = []; | ||
|
||
public void AddSubmenu<TMenu>(string? selectionName = null) | ||
where TMenu : IConsoleMenu | ||
{ | ||
selectionName ??= typeof(TMenu).Name; | ||
AddSelection(selectionName, async () => await RunAsync<TMenu>()); | ||
} | ||
|
||
public void AddSelection(string name, Action? action = null, bool pauseOnComplete = false) | ||
{ | ||
SelectionItems.Add(new ConsoleSelectionMenuItem(name, action, pauseOnComplete)); | ||
} | ||
|
||
public override void Render() | ||
{ | ||
AnsiConsole.Clear(); | ||
|
||
var selectionPrompt = new SelectionPrompt<ConsoleSelectionMenuItem>().Title($"[green]{Title}[/]"); | ||
|
||
selectionPrompt.AddChoices(SelectionItems); | ||
|
||
var backSelectionOption = IsMainMenu ? ConsoleSelectionMenuItem.ExitItem : ConsoleSelectionMenuItem.BackItem; | ||
selectionPrompt.AddChoice(backSelectionOption); | ||
|
||
var selected = AnsiConsole.Prompt(selectionPrompt); | ||
|
||
if (selected.IsExit) | ||
{ | ||
OnExit(); | ||
return; | ||
} | ||
|
||
OnSelection(selected); | ||
InvokeSelection(selected); | ||
OnSelectionInvoked(selected); | ||
OnAfterSelectionInvoked(selected); | ||
|
||
Render(); | ||
} | ||
|
||
public virtual void OnSelection(ConsoleSelectionMenuItem selection) | ||
{ | ||
} | ||
|
||
public virtual void InvokeSelection(ConsoleSelectionMenuItem selection) | ||
{ | ||
selection.Action?.Invoke(); | ||
} | ||
|
||
public virtual void OnSelectionInvoked(ConsoleSelectionMenuItem selection) | ||
{ | ||
} | ||
|
||
public virtual void OnAfterSelectionInvoked(ConsoleSelectionMenuItem selection) | ||
{ | ||
if (selection.PauseOnComplete) | ||
{ | ||
AnsiConsole.WriteLine("Press any key to continue..."); | ||
System.Console.ReadKey(); | ||
} | ||
} | ||
|
||
public virtual void OnExit() | ||
{ | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.