|
1 | | -using System; |
| 1 | +using OutputColorizer.Format; |
| 2 | +using System; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Text; |
4 | 5 |
|
5 | 6 | namespace OutputColorizer |
6 | 7 | { |
7 | 8 | public static class Colorizer |
| 9 | + |
8 | 10 | { |
9 | 11 | private static IOutputWriter s_printer = new ConsoleWriter(); |
10 | 12 |
|
@@ -47,109 +49,108 @@ public static void SetupWriter(IOutputWriter newWriter) |
47 | 49 | private static void InternalWrite(string message, object[] args) |
48 | 50 | { |
49 | 51 | Stack<ConsoleColor> colors = new Stack<ConsoleColor>(); |
50 | | - Stack<int> parens = new Stack<int>(); |
51 | | - int unbalancedParens = 0; |
52 | | - // intial state |
53 | | - parens.Push(-1); |
54 | 52 |
|
55 | | - Dictionary<string, int> argMap = CreateArgumentMap(message); |
56 | | - for (int i = 0; i < message.Length; i++) |
| 53 | + Lexer lex = new Lexer(message); |
| 54 | + Token[] tokens = lex.Tokenize(); |
| 55 | + |
| 56 | + CheckFormat(lex); |
| 57 | + |
| 58 | + Dictionary<string, int> argMap = CreateArgumentMap(tokens, message); |
| 59 | + |
| 60 | + for (int currentTokenPosition = 0; currentTokenPosition < tokens.Length; currentTokenPosition++) |
57 | 61 | { |
58 | | - char currentChar = message[i]; |
| 62 | + Token currentToken = tokens[currentTokenPosition]; |
59 | 63 |
|
60 | | - switch (currentChar) |
| 64 | + switch (currentToken.Kind) |
61 | 65 | { |
62 | | - case '\\': |
| 66 | + case TokenKind.String: |
63 | 67 | { |
64 | | - // if we have an escaped character, continue. |
65 | | - i++; |
66 | | - continue; |
| 68 | + // write the text |
| 69 | + string content = RewriteString(argMap, lex.GetValue(currentToken), args); |
| 70 | + s_printer.Write(content); |
| 71 | + break; |
67 | 72 | } |
68 | | - case '[': |
| 73 | + case TokenKind.CloseBracket: |
69 | 74 | { |
70 | | - // When we encounter a '[' it means we are probably going to change the color |
71 | | - // so we need to write what we had up to this point. |
72 | | - |
73 | | - // pop the location of the last paren from the stack |
74 | | - // Write the message segment between the last paren and the current position |
75 | | - int previousParenIndex = parens.Pop(); |
76 | | - WriteMessageSegment(message, args, argMap, previousParenIndex, i); |
77 | | - |
78 | | - // Given a string that looks like [color! extracts the color and pushes it on the stack of colors |
79 | | - ParseColor(message, colors, ref i); |
80 | | - |
81 | | - // keep track of the latest parens that you saw |
82 | | - unbalancedParens++; |
83 | | - parens.Push(i); |
84 | | - |
85 | | - continue; |
| 75 | + s_printer.ForegroundColor = colors.Pop(); |
| 76 | + break; |
86 | 77 | } |
87 | | - case ']': |
| 78 | + case TokenKind.ColorDelimiter: |
88 | 79 | { |
89 | | - // at this point, we know where the color ended. |
90 | | - // Write the message segment between the last paren and the current position |
91 | | - int matchingbracket = parens.Pop(); |
92 | | - WriteMessageSegment(message, args, argMap, matchingbracket, i); |
| 80 | + s_printer.Write("!"); |
| 81 | + break; |
| 82 | + } |
| 83 | + case TokenKind.OpenBracket: |
| 84 | + { |
| 85 | + currentTokenPosition++; // move to the next token, which should be a string token. |
| 86 | + currentToken = tokens[currentTokenPosition]; |
93 | 87 |
|
94 | | - if (colors.Count == 0) |
| 88 | + // This case is [] |
| 89 | + if (currentToken.Kind == TokenKind.CloseBracket) |
95 | 90 | { |
96 | | - throw new FormatException($"Missing expected ']' "); |
| 91 | + string content = RewriteString(argMap, "\\[\\]", args); |
| 92 | + s_printer.Write(content); |
| 93 | + currentTokenPosition++; |
| 94 | + break; |
97 | 95 | } |
98 | | - s_printer.ForegroundColor = colors.Pop(); |
99 | 96 |
|
100 | | - parens.Push(i); |
101 | | - unbalancedParens--; |
102 | | - continue; |
| 97 | + // The token is not a close paren -- we should check and see what is the next parameter |
| 98 | + Token futureToken = tokens[currentTokenPosition + 1]; |
| 99 | + if (futureToken.Kind == TokenKind.ColorDelimiter) |
| 100 | + { |
| 101 | + string colorName = lex.GetValue(currentToken); |
| 102 | + if (!s_consoleColorMap.TryGetValue(colorName, out ConsoleColor color)) |
| 103 | + { |
| 104 | + throw new ArgumentException($"Unknown color: {colorName}"); |
| 105 | + } |
| 106 | + |
| 107 | + colors.Push(s_printer.ForegroundColor); |
| 108 | + s_printer.ForegroundColor = color; |
| 109 | + currentTokenPosition++; // skip over the ! token |
| 110 | + continue; |
| 111 | + } |
| 112 | + else if (futureToken.Kind == TokenKind.CloseBracket) |
| 113 | + { |
| 114 | + // check to see if we have a matching closing bracket (this can be covered by the check up-top) |
| 115 | + // the user wanted to write the actual text '[noColor]' |
| 116 | + |
| 117 | + // construct an escaped string |
| 118 | + string content = RewriteString(argMap, "\\[" + lex.GetValue(currentToken) + "\\]", args); |
| 119 | + s_printer.Write(content); |
| 120 | + currentTokenPosition++; // skip over the close bracket token |
| 121 | + } |
| 122 | + |
| 123 | + break; |
103 | 124 | } |
104 | 125 | } |
105 | 126 | } |
106 | | - |
107 | | - // at this point, the closing bracket might not have been found! |
108 | | - if (unbalancedParens != 0) |
109 | | - { |
110 | | - throw new FormatException($"Missing expected ']' "); |
111 | | - } |
112 | | - |
113 | | - // write the last part, if any |
114 | | - int finalParen = parens.Pop(); |
115 | | - WriteMessageSegment(message, args, argMap, finalParen, message.Length); |
116 | 127 | } |
117 | 128 |
|
118 | | - private static void WriteMessageSegment(string message, object[] args, Dictionary<string, int> argMap, int startIndex, int currentIndex) |
| 129 | + private static void CheckFormat(Lexer lex) |
119 | 130 | { |
120 | | - // do we have anything to print? |
121 | | - if (currentIndex - startIndex - 1 > 0) |
122 | | - { |
123 | | - string messageSegment = message.Substring(startIndex + 1, currentIndex - startIndex - 1); |
124 | | - string content = RewriteString(argMap, messageSegment, args); |
125 | | - s_printer.Write(content); |
126 | | - } |
127 | | - } |
| 131 | + Token[] tokens = lex.Tokenize(); |
128 | 132 |
|
129 | | - private static void ParseColor(string message, Stack<ConsoleColor> colors, ref int currPos) |
130 | | - { |
131 | | - int textLength = message.Length; |
132 | | - // find the color |
133 | | - for (int pos = currPos + 1; pos < textLength; pos++) |
| 133 | + int brackets = 0; |
| 134 | + // check to see if the parens are balanced. |
| 135 | + for (int i = 0; i < tokens.Length; i++) |
134 | 136 | { |
135 | | - if (message[pos] == '!') |
136 | | - { |
137 | | - string colorString = message.Substring(currPos + 1, pos - currPos - 1); |
138 | | - ConsoleColor color; |
| 137 | + if (tokens[i].Kind == TokenKind.OpenBracket) brackets++; |
| 138 | + if (tokens[i].Kind == TokenKind.CloseBracket) brackets--; |
139 | 139 |
|
140 | | - if (!s_consoleColorMap.TryGetValue(colorString, out color)) |
| 140 | + // To nest you need to specify a color |
| 141 | + if (i > 0 && tokens[i].Kind == TokenKind.OpenBracket) |
| 142 | + { |
| 143 | + if (tokens[i - 1].Kind == tokens[i].Kind) |
141 | 144 | { |
142 | | - throw new ArgumentException($"Unknown color {colorString}"); |
| 145 | + throw new FormatException($"Invalid format at position {tokens[i].Start}"); |
143 | 146 | } |
144 | | - |
145 | | - colors.Push(s_printer.ForegroundColor); |
146 | | - s_printer.ForegroundColor = color; |
147 | | - |
148 | | - // set the position of the last character |
149 | | - currPos = pos; |
150 | | - break; |
151 | 147 | } |
152 | 148 | } |
| 149 | + |
| 150 | + if (brackets != 0) |
| 151 | + { |
| 152 | + throw new FormatException("Invalid format, unbalanced paranthesis in the string"); |
| 153 | + } |
153 | 154 | } |
154 | 155 |
|
155 | 156 | private static string RewriteString(Dictionary<string, int> argMap, string content, params object[] args) |
@@ -234,36 +235,46 @@ private static string RewriteString(Dictionary<string, int> argMap, string conte |
234 | 235 | return string.Format(sb.ToString(), argument); |
235 | 236 | } |
236 | 237 |
|
237 | | - private static Dictionary<string, int> CreateArgumentMap(string content) |
| 238 | + private static Dictionary<string, int> CreateArgumentMap(Token[] tokens, string content) |
238 | 239 | { |
239 | 240 | Dictionary<string, int> map = new Dictionary<string, int>(); |
240 | 241 |
|
241 | 242 | int argCount = 0; |
242 | | - int textLength = content.Length; |
243 | | - for (int i = 0; i < textLength; i++) |
| 243 | + |
| 244 | + foreach (Token tok in tokens) |
244 | 245 | { |
245 | | - if (content[i] == '{') |
| 246 | + // Skip over tokens that can't contain replacements. |
| 247 | + if (tok.Kind != TokenKind.String) |
246 | 248 | { |
| 249 | + continue; |
| 250 | + } |
| 251 | + |
| 252 | + int tokenTextLength = tok.End + 1; |
| 253 | + |
| 254 | + for (int i = tok.Start; i < tokenTextLength; i++) |
| 255 | + { |
| 256 | + if (content[i] != '{') |
| 257 | + { |
| 258 | + continue; |
| 259 | + } |
| 260 | + |
247 | 261 | // '{' are escaped as '{{' |
248 | | - if (i + 1 < textLength && content[i + 1] == '{') |
| 262 | + if (i + 1 < tokenTextLength && content[i + 1] == '{') |
249 | 263 | { |
250 | 264 | i++; |
251 | 265 | continue; |
252 | 266 | } |
253 | 267 |
|
254 | 268 | // find the matching closing curly bracket |
255 | 269 | int pos = i; |
256 | | - while (pos < textLength && content[pos++] != '}') |
257 | | - { |
258 | | - } |
| 270 | + while (pos < tokenTextLength && content[pos++] != '}') ; |
259 | 271 |
|
260 | 272 | if (content[pos - 1] != '}') // did not find matching '}' |
261 | 273 | throw new ArgumentException(string.Format("Could not parse '{0}'", content)); |
262 | 274 |
|
263 | 275 | string arg = content.Substring(i + 1, pos - i - 2); |
264 | 276 |
|
265 | | - int x; |
266 | | - if (!int.TryParse(arg, out x)) |
| 277 | + if (!int.TryParse(arg, out int x)) |
267 | 278 | { |
268 | 279 | throw new ArgumentException(string.Format("Could not parse '{0}'", content)); |
269 | 280 | } |
|
0 commit comments