-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorConsole.cs
238 lines (208 loc) · 6.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System.Text.RegularExpressions;
/// <summary>
/// Console Color Helper class that provides coloring to individual commands
/// </summary>
public static class ColorConsole
{
static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// WriteLine with color
/// </summary>
/// <param name="text"></param>
/// <param name="color"></param>
public static void WriteLine(string text, ConsoleColor? color = null)
{
if (color.HasValue)
{
var oldColor = System.Console.ForegroundColor;
if (color == oldColor)
Console.WriteLine(text);
else
{
Console.ForegroundColor = color.Value;
Console.WriteLine(text);
Console.ForegroundColor = oldColor;
}
}
else
Console.WriteLine(text);
Logger.Info(text);
}
/// <summary>
/// Writes out a line with a specific color as a string
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="color">A console color. Must match ConsoleColors collection names (case insensitive)</param>
public static void WriteLine(string text, string color)
{
if (string.IsNullOrEmpty(color))
{
WriteLine(text);
return;
}
if (!Enum.TryParse(color, true, out ConsoleColor col))
{
WriteLine(text);
}
else
{
WriteLine(text, col);
}
Logger.Info(text);
}
/// <summary>
/// Write with color
/// </summary>
/// <param name="text"></param>
/// <param name="color"></param>
public static void Write(string text, ConsoleColor? color = null)
{
if (color.HasValue)
{
var oldColor = System.Console.ForegroundColor;
if (color == oldColor)
{
Console.Write(text);
Console.CursorLeft = 0;
}
else
{
Console.ForegroundColor = color.Value;
Console.Write(text);
Console.ForegroundColor = oldColor;
}
}
else
Console.Write(text);
Logger.Info(text);
}
/// <summary>
/// Writes out a line with color specified as a string
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="color">A console color. Must match ConsoleColors collection names (case insensitive)</param>
public static void Write(string text, string color)
{
if (string.IsNullOrEmpty(color))
{
Write(text);
return;
}
if (!ConsoleColor.TryParse(color, true, out ConsoleColor col))
{
Write(text);
}
else
{
Write(text, col);
}
Logger.Info(text);
}
#region Wrappers and Templates
/// <summary>
/// Writes a line of header text wrapped in a in a pair of lines of dashes:
/// -----------
/// Header Text
/// -----------
/// and allows you to specify a color for the header. The dashes are colored
/// </summary>
/// <param name="headerText">Header text to display</param>
/// <param name="wrapperChar">wrapper character (-)</param>
/// <param name="headerColor">Color for header text (yellow)</param>
/// <param name="dashColor">Color for dashes (gray)</param>
public static void WriteWrappedHeader(string headerText,
char wrapperChar = '-',
ConsoleColor headerColor = ConsoleColor.Yellow,
ConsoleColor dashColor = ConsoleColor.DarkGray)
{
if (string.IsNullOrEmpty(headerText))
return;
string line = new string(wrapperChar, headerText.Length);
WriteLine(line, dashColor);
WriteLine(headerText, headerColor);
WriteLine(line, dashColor);
}
private static Lazy<Regex> colorBlockRegEx = new Lazy<Regex>(
() => new Regex("\\[(?<color>.*?)\\](?<text>[^[]*)\\[/\\k<color>\\]", RegexOptions.IgnoreCase),
isThreadSafe: true);
/// <summary>
/// Allows a string to be written with embedded color values using:
/// This is [red]Red[/red] text and this is [cyan]Blue[/blue] text
/// </summary>
/// <param name="text">Text to display</param>
/// <param name="baseTextColor">Base text color</param>
public static void WriteEmbeddedColorLine(string text, ConsoleColor? baseTextColor = null)
{
if (baseTextColor == null)
baseTextColor = Console.ForegroundColor;
if (string.IsNullOrEmpty(text))
{
WriteLine(string.Empty);
return;
}
int at = text.IndexOf("[");
int at2 = text.IndexOf("]");
if (at == -1 || at2 <= at)
{
WriteLine(text, baseTextColor);
return;
}
while (true)
{
var match = colorBlockRegEx.Value.Match(text);
if (match.Length < 1)
{
Write(text, baseTextColor);
break;
}
// write up to expression
Write(text.Substring(0, match.Index), baseTextColor);
// strip out the expression
string highlightText = match.Groups["text"].Value;
string colorVal = match.Groups["color"].Value;
Write(highlightText, colorVal);
// remainder of string
text = text.Substring(match.Index + match.Value.Length);
}
Console.WriteLine();
}
#endregion
#region Success, Error, Info, Warning Wrappers
/// <summary>
/// Write a Success Line - green
/// </summary>
/// <param name="text">Text to write out</param>
public static void WriteSuccess(string text)
{
WriteLine(text, ConsoleColor.Green);
Logger.Info(text);
}
/// <summary>
/// Write a Error Line - Red
/// </summary>
/// <param name="text">Text to write out</param>
public static void WriteError(string text)
{
WriteLine(text, ConsoleColor.Red);
Logger.Error(text);
}
/// <summary>
/// Write a Warning Line - Yellow
/// </summary>
/// <param name="text">Text to Write out</param>
public static void WriteWarning(string text)
{
WriteLine(text, ConsoleColor.DarkYellow);
Logger.Warn(text);
}
/// <summary>
/// Write a Info Line - dark cyan
/// </summary>
/// <param name="text">Text to write out</param>
public static void WriteInfo(string text)
{
WriteLine(text, ConsoleColor.DarkCyan);
Logger.Info(text);
}
#endregion
}