-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorConsole.cs
105 lines (88 loc) · 2.73 KB
/
ColorConsole.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
using System.Text;
namespace DSBatchDownloader
{
public static class ColorConsole
{
//Supported colors
private static Dictionary<string, ConsoleColor> colorMap = new Dictionary<string, ConsoleColor>()
{
{"blue", ConsoleColor.Blue },
{"darkcyan", ConsoleColor.DarkCyan},
{"cyan", ConsoleColor.Cyan},
{"green", ConsoleColor.Green},
{"red", ConsoleColor.Red},
{"yellow", ConsoleColor.Yellow}
};
public static void WriteMarkedUpString(string message, bool addNewLineAfter = true)
{
Console.ResetColor();
var colorStrings = Parse(message);
foreach (var colorString in colorStrings)
{
if(colorString.Color == null)
Console.Write(colorString.Value);
else
{
Console.ResetColor();
Console.ForegroundColor = (ConsoleColor)colorString.Color;
Console.Write(colorString.Value);
Console.ResetColor();
}
}
if (addNewLineAfter)
Console.WriteLine();
}
private static List<ColorString> Parse(string message)
{
var colorStrings = new List<ColorString>();
var workingSb = new StringBuilder();
for(int i = 0; i < message.Length; i++)
{
var c = message[i];
if(c != '<') workingSb.Append(c);
else
{
i++;
colorStrings.Add(new ColorString(null, workingSb.ToString()));
workingSb.Clear();
var colorSb = new StringBuilder();
while (message[i] != '>')
{
if (i + 1 >= message.Length)
throw new Exception("Invalid color string format");
colorSb.Append(message[i]);
i++;
}
i++;
var strColor = colorSb.ToString();
if (!colorMap.ContainsKey(strColor))
throw new Exception($"Unsupported color: {strColor}");
var closerIndex = message.IndexOf($"</{strColor}>", i);
if(closerIndex < i)
throw new Exception($"No closing tag for color: {strColor}");
var phraseSb = new StringBuilder();
while (i < closerIndex)
{
phraseSb.Append(message[i]);
i++;
}
colorStrings.Add(new ColorString(colorMap[strColor], phraseSb.ToString()));
i = closerIndex + 2 + strColor.Length;
}
}
if (workingSb.Length > 0)
colorStrings.Add(new ColorString(null, workingSb.ToString()));
return colorStrings;
}
private class ColorString
{
public ConsoleColor? Color { get; set; }
public string Value { get; set; }
public ColorString(ConsoleColor? color, string value)
{
Color = color;
Value = value;
}
}
}
}