-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMenu.cs
79 lines (66 loc) · 1.68 KB
/
IMenu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Spectre.Console;
using Spectre.Console.Rendering;
public interface IMenuLayout {
IRenderable GetContent();
IPrompt<string> GetPrompt();
}
public interface IMenuBehavior {
IMenu? HandleInput(string input);
}
public interface IMenu : IMenuLayout, IMenuBehavior {}
public class MainMenu : IMenu
{
public IRenderable GetContent()
{
return new Table().AddColumn("Key").AddColumn("Value").AddRow(["myKey", "myValue"]);
}
public IPrompt<string> GetPrompt()
{
return new SelectionPrompt<string>().AddChoices(["SubMenu", "Exit"]);
}
public IMenu? HandleInput(string input)
{
return input switch {
"SubMenu" => new SubMenu(),
"Exit" => null,
_ => this
};
}
}
public class SubMenu : IMenu
{
public IRenderable GetContent()
{
return new Calendar(DateTime.Now);
}
public IPrompt<string> GetPrompt()
{
return new SelectionPrompt<string>().AddChoices(["Go back", "Do stuff"]);
}
public IMenu HandleInput(string input)
{
return input switch {
"Go back" => new MainMenu(),
_ => this
};
}
}
public class MenuManager {
private IMenu _currentState;
public MenuManager(IMenu initialState) {
_currentState = initialState;
}
public void Run() {
while (_currentState != null) {
// main app-loop
// clear the screen
AnsiConsole.Clear();
// render content
AnsiConsole.Write(_currentState.GetContent());
// gather user input
var userInput = AnsiConsole.Prompt(_currentState.GetPrompt());
// handle input
_currentState = _currentState.HandleInput(userInput);
}
}
}