-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spore.cs
142 lines (130 loc) · 4.56 KB
/
Spore.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Text.RegularExpressions;
using System.Diagnostics;
/// <summary>
/// This is a debug class.
/// </summary>
public static class Spore
{
private static readonly string _colorSplitPattern = @"(<Black>|<DarkBlue>|<DarkGreen>|<DarkCyan>|<DarkRed>|<DarkMagenta>|<DarkYellow>|<Gray>|<DarkGray>|<Blue>|<Green>|<Cyan>|<Red>|<Magenta>|<Yellow>|<White>|<Previous>|<Reset>)";
/// <summary>
/// Prints a line with color to print.
/// </summary>
/// <param name="value">The message to print.</param>
public static void ColorLine(string value)
{
Spore.Color(value+"\n");
}
/// <summary>
/// Prints a line with color to print.
/// </summary>
/// <param name="value">The message to print.</param>
public static void Color(string value)
{
List<(string Color, string Characters)> stringTextParts = new List<(string Color, string Characters)>();
string[] parts = Regex.Split(value, _colorSplitPattern, RegexOptions.Compiled);
SporeTraceColor baseColor = SporeTraceColor.White;
SporeTraceColor currentColor = baseColor;
Stack<SporeTraceColor> colorStack = new Stack<SporeTraceColor>();
colorStack.Push(currentColor);
int lineLength = 0;
int consoleWidth = Console.WindowWidth;
foreach(string s in parts){
SporeTraceColor? color = StringToSporeTraceColor(s);
if(color.HasValue)
{
if(color == SporeTraceColor.Reset)
{
currentColor = baseColor;
colorStack.Clear();
colorStack.Push(currentColor);
}
else if(color == SporeTraceColor.Previous)
{
if(colorStack.Count > 1)
{
colorStack.Pop();
currentColor = colorStack.Peek();
}
}
else
{
currentColor = (SporeTraceColor)color;
colorStack.Push(currentColor);
}
}
else
{
Console.ForegroundColor = (ConsoleColor)currentColor;
foreach(char c in s)
{
if(c=='\n')
{
Console.Write(new string(' ', consoleWidth - lineLength));
Console.Write(c);
lineLength = 0;
}
else
{
Console.Write(c);
lineLength++;
}
}
}
}
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// Convert a string to SporeTraceColor.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>A SporeTraceColor of the color or null.</returns>
private static SporeTraceColor? StringToSporeTraceColor(string color)
{
switch(color)
{
case "<Black>":
return SporeTraceColor.Black;
case "<DarkBlue>":
return SporeTraceColor.DarkBlue;
case "<DarkGreen>":
return SporeTraceColor.DarkGreen;
case "<DarkCyan>":
return SporeTraceColor.DarkCyan;
case "<DarkRed>":
return SporeTraceColor.DarkRed;
case "<DarkMagenta>":
return SporeTraceColor.DarkMagenta;
case "<DarkYellow>":
return SporeTraceColor.DarkYellow;
case "<Gray>":
return SporeTraceColor.Gray;
case "<DarkGray>":
return SporeTraceColor.DarkGray;
case "<Blue>":
return SporeTraceColor.Blue;
case "<Green>":
return SporeTraceColor.Green;
case "<Cyan>":
return SporeTraceColor.Cyan;
case "<Red>":
return SporeTraceColor.Red;
case "<Magenta>":
return SporeTraceColor.Magenta;
case "<Yellow>":
return SporeTraceColor.Yellow;
case "<White>":
return SporeTraceColor.White;
case "<Previous>":
return SporeTraceColor.Previous;
case "<Reset>":
return SporeTraceColor.Reset;
default:
return null;
}
}
public static void Clear()
{
Console.CursorVisible = false;
Console.Clear();
}
}