-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleMenu.cs
More file actions
188 lines (167 loc) · 5.93 KB
/
ConsoleMenu.cs
File metadata and controls
188 lines (167 loc) · 5.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using ConsoleAddonLibary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAddonLibrary
{
public class ConsoleMenu
{
private List<MenuElement> menuElements = new List<MenuElement>();
public string MenuName { get; set; }
private ConsoleColor _boxColor = ConsoleColor.Gray;
private int _selectedElement = 0;
private int _verticalCursorPos = 0;
private bool _isShown = false;
private bool CloseMenu = false;
public void AddMenuElement(MenuElement element) { menuElements.Add(element); }
public void RemoveMenuElement(int index) { menuElements.Remove(menuElements[index]); }
public void ShowMenu(bool ShowExitButton = false)
{
if (ShowExitButton) { AddMenuElement(new MenuElement("Close menu", () => Exit())); }
PrintMenu();
while (true)
{
ConsoleKey key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.Enter:
menuElements[_selectedElement].Method.Invoke();
if (!CloseMenu)
{
ConsoleLogs.SystemLog("Press any key for continue");
Console.ReadKey();
Console.Clear();
_isShown = false;
PrintMenu();
}
else Console.Clear();
break;
case ConsoleKey.S:
if (_selectedElement < menuElements.Count - 1)
{
_selectedElement++;
PrintMenu();
}
break;
case ConsoleKey.DownArrow:
if (_selectedElement < menuElements.Count - 1)
{
_selectedElement++;
PrintMenu();
}
break;
case ConsoleKey.W:
if (_selectedElement > 0)
{
_selectedElement--;
PrintMenu();
}
break;
case ConsoleKey.UpArrow:
if (_selectedElement > 0)
{
_selectedElement--;
PrintMenu();
}
break;
}
if (CloseMenu)
{
RemoveMenuElement(menuElements.Count - 1);
CloseMenu = false;
_isShown = false;
return;
}
}
}
private void PrintMenu()
{
Console.ForegroundColor = _boxColor;
string line = "";
for (int i = 0; i < FindMaxLenght() + 4; i++)
{
line += "█";
}
if (!_isShown)
{
Console.WriteLine(line);
if (MenuName != null)
{
Console.BackgroundColor = ConsoleColor.DarkYellow;
PrintLine(MenuName);
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(line);
_verticalCursorPos = Console.CursorTop;
}
_isShown = true;
}
Console.CursorTop = _verticalCursorPos;
if (menuElements != null)
{
for (int i = 0; i < menuElements.Count; i++)
{
if (i == _selectedElement)
{
ConsoleLogs.PrintSelected(() =>
{
PrintLine(menuElements[i].ElementName);
});
}
else
{
PrintLine(menuElements[i].ElementName);
}
}
Console.WriteLine(line);
}
}
private void PrintLine(string elmentName)
{
int maxLenght = FindMaxLenght();
Console.Write("█ ");
Console.ForegroundColor= ConsoleColor.Yellow;
Console.Write(elmentName);
Console.ForegroundColor = _boxColor;
string next = "";
for (int i = 0; i < maxLenght - elmentName.Length; i++)
{
next += " ";
}
next += " █";
Console.Write(next);
Console.WriteLine();
}
private int FindMaxLenght()
{
int maxLenght = 0;
foreach (MenuElement element in menuElements)
{
if (element.ElementName.Length > maxLenght)
{
maxLenght = element.ElementName.Length;
}
}
return maxLenght;
}
private void Exit()
{
CloseMenu = true;
}
}
public class MenuElement
{
public MenuElement(string elementName, Action method)
{
if (string.IsNullOrEmpty(elementName))
{
throw new ArgumentException("Element name cannot be null or empty");
}
ElementName = elementName;
Method = method;
}
public string ElementName { get; }
public Action Method { get; }
}
}