@@ -11,6 +11,16 @@ public class PradBuffer
1111 private char [ ] buffer ;
1212 private int BufferSize ;
1313
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+
1424 /// <summary>
1525 /// <para> Stores the current index of the input buffer. </para>
1626 /// <para> It is the position of the cursor in the string (buffer array). </para>
@@ -181,7 +191,7 @@ public void GetInput()
181191 Console . Write ( " " ) ;
182192 }
183193 Console . Write ( "\r " ) ;
184- Console . Write ( GetBufferAsString ( ) + " " ) ;
194+ WriteHighlight ( GetBufferAsString ( ) + " " ) ;
185195
186196 Console . CursorLeft = BufferIndex ;
187197 }
@@ -223,7 +233,7 @@ public void GetInput()
223233 Console . Write ( " " ) ;
224234 }
225235 Console . Write ( "\r " ) ;
226- Console . Write ( GetBufferAsString ( ) ) ;
236+ WriteHighlight ( GetBufferAsString ( ) ) ;
227237
228238 BufferIndex = Console . CursorLeft ;
229239 break ;
@@ -232,6 +242,45 @@ public void GetInput()
232242 }
233243 }
234244
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+
235284 /// <summary>
236285 /// 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.
237286 /// </summary>
@@ -346,7 +395,7 @@ public void GetInput(string prefix)
346395 Console . Write ( " " ) ;
347396 }
348397 Console . Write ( "\r " ) ;
349- Console . Write ( prefix + GetBufferAsString ( ) + " " ) ;
398+ WriteHighlight ( prefix + GetBufferAsString ( ) + " " ) ;
350399
351400 Console . CursorLeft = BufferIndex + prefix . Length ;
352401 }
@@ -389,7 +438,7 @@ public void GetInput(string prefix)
389438 Console . Write ( " " ) ;
390439 }
391440 Console . Write ( "\r " ) ;
392- Console . Write ( prefix + GetBufferAsString ( ) ) ;
441+ WriteHighlight ( prefix + GetBufferAsString ( ) ) ;
393442
394443 BufferIndex = Console . CursorLeft - prefix . Length ;
395444 break ;
0 commit comments