-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutput.cs
More file actions
131 lines (126 loc) · 4.81 KB
/
Output.cs
File metadata and controls
131 lines (126 loc) · 4.81 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Ultz.Extensions.Logging
{
/// <summary>
/// Utility class, used to split a message with colour formatting up into "sub-messages" of one solid colour.
/// </summary>
public static class Output
{
/// <summary>
/// The character used to denote a colour change.
/// </summary>
/// <remarks>
/// The character that follows this character in a message denotes what colour the next part of the message
/// will be. Possible values are:
/// <list type="bullet">
/// <item>
/// <term>0</term><description>Black</description>
/// </item>
/// <item>
/// <term>1</term><description>Dark Blue</description>
/// </item>
/// <item>
/// <term>2</term><description>Dark Green</description>
/// </item>
/// <item>
/// <term>3</term><description>Dark Aqua</description>
/// </item>
/// <item>
/// <term>4</term><description>Dark Red</description>
/// </item>
/// <item>
/// <term>5</term><description>Dark Purple</description>
/// </item>
/// <item>
/// <term>6</term><description>Gold</description>
/// </item>
/// <item>
/// <term>7</term><description>Grey</description>
/// </item>
/// <item>
/// <term>8</term><description>Dark Grey</description>
/// </item>
/// <item>
/// <term>9</term><description>Blue</description>
/// </item>
/// <item>
/// <term>a</term><description>Green</description>
/// </item>
/// <item>
/// <term>b</term><description>Aqua</description>
/// </item>
/// <item>
/// <term>c</term><description>Red</description>
/// </item>
/// <item>
/// <term>d</term><description>Light Purple</description>
/// </item>
/// <item>
/// <term>e</term><description>Yellow</description>
/// </item>
/// <item>
/// <term>f</term><description>White</description>
/// </item>
/// </list>
/// If the colour character appears once again, the colour character is considered "escaped" and, as a result,
/// no colour change will occur and the colour character will be written to the buffer.<br />
/// If the character that follows the colour character is not one of the possible values denoted above and is
/// not the colour character once again (to escape it), then the character is treated as a "reset character" and
/// will reset the output buffer colour back to its original state. You will often see <c>§r</c> used to reset
/// the colour, however any character that is not a valid value can be used.
/// </remarks>
public const char ColourCharacter = '§';
/// <summary>
/// Gets all coloured sub-messages in the given colour formatted message.
/// </summary>
/// <param name="msg">The message to split into sub-messages.</param>
/// <param name="colourChar">The colour character to use. By default, this is <see cref="ColourCharacter" /></param>
/// <returns>All sub-messages found within this message.</returns>
public static IEnumerable<(string, ConsoleColor?)> EnumerateSubMessages(string msg,
char colourChar = ColourCharacter)
{
var subMsg = string.Empty;
var gotColourChar = false;
ConsoleColor? colour = null;
for (var i = 0; i < msg.Length; i++)
{
var c = msg[i];
if (c == colourChar)
{
if (gotColourChar)
{
// escaped
subMsg += colourChar;
gotColourChar = false;
continue;
}
gotColourChar = true;
}
else if (gotColourChar)
{
if (subMsg != string.Empty)
{
yield return (subMsg, colour);
subMsg = string.Empty;
gotColourChar = false;
}
colour = byte.TryParse(c.ToString(), NumberStyles.HexNumber, null, out var val)
? (ConsoleColor?) val
: null;
gotColourChar = false;
}
else
{
gotColourChar = false;
subMsg += c;
}
}
if (subMsg != string.Empty)
{
yield return (subMsg, colour);
}
}
}
}