@@ -11,6 +11,16 @@ public class PradBuffer
11
11
private char [ ] buffer ;
12
12
private int BufferSize ;
13
13
14
+ /// <summary>
15
+ /// Stores the syntax highlighting data for the input buffer
16
+ /// </summary>
17
+ public Dictionary < string , ConsoleColor > SyntaxHighlights { get ; set ; } = new Dictionary < string , ConsoleColor > ( ) ;
18
+
19
+ /// <summary>
20
+ /// Variable to enable syntax highlights during input.
21
+ /// </summary>
22
+ public bool EnableSyntaxHighlighting { get ; set ; } = false ;
23
+
14
24
/// <summary>
15
25
/// <para> Stores the current index of the input buffer. </para>
16
26
/// <para> It is the position of the cursor in the string (buffer array). </para>
@@ -181,7 +191,7 @@ public void GetInput()
181
191
Console . Write ( " " ) ;
182
192
}
183
193
Console . Write ( "\r " ) ;
184
- Console . Write ( GetBufferAsString ( ) + " " ) ;
194
+ WriteHighlight ( GetBufferAsString ( ) + " " ) ;
185
195
186
196
Console . CursorLeft = BufferIndex ;
187
197
}
@@ -223,7 +233,7 @@ public void GetInput()
223
233
Console . Write ( " " ) ;
224
234
}
225
235
Console . Write ( "\r " ) ;
226
- Console . Write ( GetBufferAsString ( ) ) ;
236
+ WriteHighlight ( GetBufferAsString ( ) ) ;
227
237
228
238
BufferIndex = Console . CursorLeft ;
229
239
break ;
@@ -232,6 +242,45 @@ public void GetInput()
232
242
}
233
243
}
234
244
245
+ private void WriteHighlight ( string str )
246
+ {
247
+ if ( ! EnableSyntaxHighlighting )
248
+ {
249
+ Console . Write ( str ) ;
250
+ return ;
251
+ }
252
+
253
+ int currentIndex = 0 ;
254
+
255
+ while ( currentIndex < str . Length )
256
+ {
257
+ bool matched = false ;
258
+
259
+ foreach ( var highlight in SyntaxHighlights )
260
+ {
261
+ string keyword = highlight . Key ;
262
+ ConsoleColor color = highlight . Value ;
263
+
264
+ if ( str . Substring ( currentIndex ) . StartsWith ( keyword ) )
265
+ {
266
+ Console . ForegroundColor = color ;
267
+ Console . Write ( keyword ) ;
268
+ Console . ResetColor ( ) ;
269
+
270
+ currentIndex += keyword . Length ;
271
+ matched = true ;
272
+ break ;
273
+ }
274
+ }
275
+
276
+ if ( ! matched )
277
+ {
278
+ Console . Write ( str [ currentIndex ] ) ;
279
+ currentIndex ++ ;
280
+ }
281
+ }
282
+ }
283
+
235
284
/// <summary>
236
285
/// Gets the input and stores it in the input buffer array cleanly. It does <b>NOT</b> return the buffer. We can add a prefix to the input.
237
286
/// </summary>
@@ -346,7 +395,7 @@ public void GetInput(string prefix)
346
395
Console . Write ( " " ) ;
347
396
}
348
397
Console . Write ( "\r " ) ;
349
- Console . Write ( prefix + GetBufferAsString ( ) + " " ) ;
398
+ WriteHighlight ( prefix + GetBufferAsString ( ) + " " ) ;
350
399
351
400
Console . CursorLeft = BufferIndex + prefix . Length ;
352
401
}
@@ -389,7 +438,7 @@ public void GetInput(string prefix)
389
438
Console . Write ( " " ) ;
390
439
}
391
440
Console . Write ( "\r " ) ;
392
- Console . Write ( prefix + GetBufferAsString ( ) ) ;
441
+ WriteHighlight ( prefix + GetBufferAsString ( ) ) ;
393
442
394
443
BufferIndex = Console . CursorLeft - prefix . Length ;
395
444
break ;
0 commit comments