-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleUI.cs
128 lines (119 loc) · 4.73 KB
/
ConsoleUI.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
using OrnaLibs.DataTypes;
using System.Globalization;
namespace OrnaLibs
{
public static class ConsoleUI
{
public static string TextBox(string title, string defaultValue = "", Predicate<string>? validate = null)
{
string res;
do
{
Console.Clear();
Console.Write($"{title}{(defaultValue == "" ? "" : $" (По-умолчанию: {defaultValue})")}: ");
res = Console.ReadLine() ?? defaultValue;
} while (validate is { } && !validate.Invoke(res));
return res;
}
public static int NumberBox(string title, int defaultValue = 0, int min = 0, int max = 100)
{
Console.Clear();
Console.Write($"{title} [{min}{(defaultValue == 0?"":$"..{defaultValue}")}..{max}]: ");
var res = Console.ReadLine();
if (string.IsNullOrWhiteSpace(res) && defaultValue != 0) return defaultValue;
if (int.TryParse(res, CultureInfo.InvariantCulture, out var i) && i >= min && i <= max) return i;
return NumberBox(title, defaultValue, min, max);
}
public static void Error(string text)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
public static int Select(string title, string[] items)
{
if(items.Length == 0) throw new ArgumentNullException("items");
Console.CursorVisible = false;
int index = 0;
ConsoleKey key;
Console.Clear();
Console.WriteLine($"{title}: ");
for (var i = 0; i < items.Length; i++)
Console.WriteLine($"{(index == i ? ">" : " ")} {items[i]}");
do
{
key = Console.ReadKey(true).Key;
if (key == ConsoleKey.DownArrow && index < items.Length - 1)
SelectItem(index, ++index);
else if (key == ConsoleKey.UpArrow && index > 0)
SelectItem(index, --index);
else if (key == ConsoleKey.PageDown)
{
SelectItem(index, items.Length - 1);
index = items.Length - 1;
}
else if (key == ConsoleKey.PageUp)
{
SelectItem(index, 0);
index = 0;
}
} while (key != ConsoleKey.Enter);
return index;
}
private static void SelectItem(int clearIndex, int placeIndex)
{
Console.SetCursorPosition(0, clearIndex+1);
Console.Write(' ');
Console.SetCursorPosition(0, placeIndex+1);
Console.Write('>');
}
public static DateOnly DateBox(string title, string format = "yyyy'/'MM'/'dd")
{
DateOnly d;
string? res;
do
{
Console.Clear();
Console.Write($"{title} (Формат: {format.Replace("'", "").ToUpper()}): ");
res = Console.ReadLine();
if (res is null) return DateOnly.FromDateTime(DateTime.Today);
} while (!DateOnly.TryParseExact(res, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out d));
return d;
}
public static TimeOnly TimeBox(string title, string format = "HH:mm")
{
TimeOnly t;
string? res;
do
{
Console.Clear();
Console.Write($"{title} (Формат: {format.Replace("'", "").ToUpper()}): ");
res = Console.ReadLine();
if (res is null) return TimeOnly.FromDateTime(DateTime.Today);
} while (!TimeOnly.TryParseExact(res, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out t));
return t;
}
public static bool Ask(string title, bool defaultValue)
{
ConsoleKey key;
Console.Clear();
ConsoleKey[] waitKeys = [ConsoleKey.Enter, ConsoleKey.L, ConsoleKey.Y];
Console.Write($"{title}? [{(defaultValue ? "Д" : "д")}/{(defaultValue ? "н" : "Н")}]");
var pos = Console.GetCursorPosition().Left;
do
{
key = Console.ReadKey().Key;
Console.SetCursorPosition(pos, 0);
Console.Write(' ');
Console.SetCursorPosition(pos, 0);
} while (!waitKeys.Contains(key));
return key switch
{
(ConsoleKey.L) => true,
(ConsoleKey.Y) => false,
_ => defaultValue,
};
}
}
}