-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleVisuals.cs
63 lines (56 loc) · 1.91 KB
/
ConsoleVisuals.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
using Pastel;
using System.Drawing;
namespace Othello
{
public static class ConsoleVisuals
{
public static string Get<T>(T text, System.Drawing.Color color)
{
return $"{text}".Pastel(color);
}
public static void Write<T>(T text, System.Drawing.Color color)
{
Console.Write($"{text}".Pastel(color));
}
public static void WriteLine<T>(T text, System.Drawing.Color color)
{
Console.WriteLine($"{text}".Pastel(color));
}
/// <summary>
/// Prints error message with red colour.
/// </summary>
/// <param name="message"></param>
public static void Error(string message)
{
var (indent, text) = SplitLeadingWhitespace(message);
Console.WriteLine($"{indent}Error: {message}".Pastel(System.Drawing.Color.Red));
}
/// <summary>
/// Prints warning message with yellow colour.
/// </summary>
/// <param name="message"></param>
public static void Warn(string message)
{
var (indent, text) = SplitLeadingWhitespace(message);
Console.WriteLine($"{indent}Warning: {text}".Pastel(System.Drawing.Color.Yellow));
}
/// <summary>
/// Splits a string into the leading whitespace and the rest of the string.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
private static (string, string) SplitLeadingWhitespace(string message)
{
// Find the index of the first non-whitespace character.
int indentSize = 0;
foreach (char c in message)
{
if (char.IsWhiteSpace(c))
indentSize++;
else
break;
}
return (message[..indentSize], message[indentSize..]);
}
}
}