-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathConsoleUtilities.cs
111 lines (102 loc) · 4.37 KB
/
ConsoleUtilities.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Batnum
{
public static class ConsoleUtilities
{
/// <summary>
/// Ask the user a question and expects a comma separated pair of numbers representing a number range in response
/// the range provided must have a maximum which is greater than the minimum
/// </summary>
/// <param name="question">The question to ask</param>
/// <param name="minimum">The minimum value expected</param>
/// <param name="maximum">The maximum value expected</param>
/// <returns>A pair of numbers representing the minimum and maximum of the range</returns>
public static (int min, int max) AskNumberRangeQuestion(string question, Func<int, int, bool> Validate)
{
while (true)
{
Console.Write(question);
Console.Write(" ");
string[] rawInput = Console.ReadLine().Split(',');
if (rawInput.Length == 2)
{
if (int.TryParse(rawInput[0], out int min) && int.TryParse(rawInput[1], out int max))
{
if (Validate(min, max))
{
return (min, max);
}
}
}
Console.WriteLine();
}
}
/// <summary>
/// Ask the user a question and expects a number in response
/// </summary>
/// <param name="question">The question to ask</param>
/// <param name="minimum">A minimum value expected</param>
/// <param name="maximum">A maximum value expected</param>
/// <returns>The number the user entered</returns>
public static int AskNumberQuestion(string question, Func<int, bool> Validate)
{
while (true)
{
Console.Write(question);
Console.Write(" ");
string rawInput = Console.ReadLine();
if (int.TryParse(rawInput, out int number))
{
if (Validate(number))
{
return number;
}
}
Console.WriteLine();
}
}
/// <summary>
/// Align content to center of console.
/// </summary>
/// <param name="content">Content to center</param>
/// <returns>Center aligned text</returns>
public static string CenterText(string content)
{
int windowWidth = Console.WindowWidth;
return String.Format("{0," + ((windowWidth / 2) + (content.Length / 2)) + "}", content);
}
/// <summary>
/// Writes the specified data, followed by the current line terminator, to the standard output stream, while wrapping lines that would otherwise break words.
/// source: https://stackoverflow.com/questions/20534318/make-console-writeline-wrap-words-instead-of-letters
/// </summary>
/// <param name="paragraph">The value to write.</param>
/// <param name="tabSize">The value that indicates the column width of tab characters.</param>
public static void WriteLineWordWrap(string paragraph, int tabSize = 4)
{
string[] lines = paragraph
.Replace("\t", new String(' ', tabSize))
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++)
{
string process = lines[i];
List<String> wrapped = new List<string>();
while (process.Length > Console.WindowWidth)
{
int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1, process.Length));
if (wrapAt <= 0) break;
wrapped.Add(process.Substring(0, wrapAt));
process = process.Remove(0, wrapAt + 1);
}
foreach (string wrap in wrapped)
{
Console.WriteLine(wrap);
}
Console.WriteLine(process);
}
}
}
}