-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
65 lines (62 loc) · 1.92 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HueLightDimmer
{
internal static class Utils
{
internal static string AskQuestion(string question)
{
Console.WriteLine(question);
return Console.ReadLine()?.Trim() ?? string.Empty;
}
internal static bool AskQuestionBool(string question)
{
bool? answer = null;
while (answer is null)
{
var str = AskQuestion($"{question} [y = yes, n = no]");
if (str.Length > 0)
{
answer = str.ToLower()[0] switch
{
'y' => true,
'n' => false,
_ => null
};
}
if (answer is null)
{
Console.WriteLine("Not a valid bool!");
}
}
return answer.Value;
}
internal static int AskQuestionInt(string question, int minInclusive, int maxInclusive)
{
int? number = null;
while (number is null)
{
var str = AskQuestion($"{question} [min {minInclusive}, max {maxInclusive}]");
if (int.TryParse(str, out var parsed))
{
if (parsed < minInclusive || parsed > maxInclusive)
{
Console.WriteLine($"Number must be between {minInclusive} and {maxInclusive} inclusive");
}
else
{
number = parsed;
}
}
else
{
Console.WriteLine("Not a valid integer!");
}
}
return number.Value;
}
}
}