using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Batnum
{
public static class ConsoleUtilities
{
///
/// 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
///
/// The question to ask
/// The minimum value expected
/// The maximum value expected
/// A pair of numbers representing the minimum and maximum of the range
public static (int min, int max) AskNumberRangeQuestion(string question, Func 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();
}
}
///
/// Ask the user a question and expects a number in response
///
/// The question to ask
/// A minimum value expected
/// A maximum value expected
/// The number the user entered
public static int AskNumberQuestion(string question, Func 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();
}
}
///
/// Align content to center of console.
///
/// Content to center
/// Center aligned text
public static string CenterText(string content)
{
int windowWidth = Console.WindowWidth;
return String.Format("{0," + ((windowWidth / 2) + (content.Length / 2)) + "}", content);
}
///
/// 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
///
/// The value to write.
/// The value that indicates the column width of tab characters.
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 wrapped = new List();
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);
}
}
}
}