-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckbox.cs
executable file
·177 lines (145 loc) · 5.43 KB
/
Checkbox.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
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
// @author LarsVomMars
// https://github.com/LarsVomMars/Checkboxes
using System;
using System.Collections.Generic;
namespace Checkbox
{
public class Checkbox
{
private List<CheckboxOptions> _options;
private int _hoveredIndex;
private int _selectedIndex;
private string _displayText;
private bool _multiSelect;
private bool _required;
private bool _error;
private ConsoleKey _key;
private ConsoleKey _prevKey;
public Checkbox(string displayText, params string[] options)
{
_multiSelect = false;
_required = true;
Init(displayText, options);
}
public Checkbox(string displayText, bool multiMode, bool required, params string[] options)
{
_multiSelect = multiMode;
_required = required;
Init(displayText, options);
}
private void Init(string dt, string[] options)
{
_hoveredIndex = 0;
_selectedIndex = -1;
_error = false;
_displayText = dt;
_options = new List<CheckboxOptions>();
for (int i = 0; i < options.Length; i++)
_options.Add(new CheckboxOptions(options[i], false, i == _hoveredIndex, i));
}
private CheckboxReturn[] ReturnData()
{
List<CheckboxReturn> l = new List<CheckboxReturn>();
foreach (var option in _options)
{
if (option.Selected) l.Add(option.GetData());
}
return l.ToArray();
}
public void Show()
{
Console.Clear();
Console.WriteLine(_displayText);
Console.WriteLine("(Use Arrow keys to navigate up and down, Space bar to select and Enter to submit)");
foreach (var option in _options)
{
Console.ForegroundColor = option.Selected
? (option.Hovered ? ConsoleColor.Blue : ConsoleColor.DarkBlue)
: (option.Hovered ? ConsoleColor.White : ConsoleColor.DarkGray);
Console.WriteLine((option.Selected ? "[*]~ " : "[ ]~ ") + $"{option.Option}");
}
Console.ResetColor();
if (_error) Console.WriteLine("\nAt least one item has to be selected!");
}
public CheckboxReturn[] Select()
{
Show();
bool end = false;
while (!end)
{
_key = Console.KeyAvailable ? Console.ReadKey(true).Key : ConsoleKey.D9;
if (_key == _prevKey) continue;
_options[_hoveredIndex].Hovered = false;
switch (_key)
{
case ConsoleKey.UpArrow:
case ConsoleKey.W:
_hoveredIndex = _hoveredIndex - 1 >= 0 ? _hoveredIndex - 1 : _options.Count - 1;
break;
case ConsoleKey.DownArrow:
case ConsoleKey.S:
_hoveredIndex = _hoveredIndex + 1 < _options.Count ? _hoveredIndex + 1 : 0;
break;
case ConsoleKey.Spacebar:
_options[_hoveredIndex].Selected = !_options[_hoveredIndex].Selected;
if (!_multiSelect)
{
if (_selectedIndex > -1 && _hoveredIndex != _selectedIndex)
_options[_selectedIndex].Selected = false;
_selectedIndex = _hoveredIndex;
}
_error = false;
break;
case ConsoleKey.Enter:
if (_required)
{
foreach (var option in _options)
{
if (!option.Selected) continue;
end = true;
break;
}
if (!end) _error = true;
}
else end = true;
break;
}
_options[_hoveredIndex].Hovered = true;
Show();
_prevKey = _key;
}
return ReturnData();
}
}
public class CheckboxOptions
{
private readonly int _index;
private readonly string _option;
public CheckboxOptions(string option, bool selected, bool hovered, int index)
{
_option = option;
Selected = selected;
Hovered = hovered;
_index = index;
}
public bool Selected { get; set; }
public bool Hovered { get; set; }
public string Option => _option;
public CheckboxReturn GetData()
{
return new CheckboxReturn(_index, _option);
}
}
public class CheckboxReturn
{
private int _index;
private string _option;
public CheckboxReturn(int index, string option)
{
_index = index;
_option = option;
}
public int Index => _index;
public string Option => _option;
}
}