@@ -178,6 +178,9 @@ public virtual int WindowHeight
178178
179179 public virtual int WindowTop { get ; set ; }
180180
181+ // Track 256-color/RGB escape sequences emitted by PSReadLine
182+ public List < ( int Position , bool IsForeground , int ColorIndex ) > Emitted256Colors { get ; } = new ( ) ;
183+
181184 public virtual ConsoleColor BackgroundColor
182185 {
183186 get => _backgroundColor ;
@@ -233,16 +236,48 @@ public virtual void Write(string s)
233236 var endSequence = s . IndexOfAny ( endEscapeChars , i ) ;
234237 var len = endSequence - i - ( s [ endSequence ] != 'm' ? 1 : 2 ) ;
235238 var escapeSequence = s . Substring ( i + 2 , len ) ;
236- foreach ( var subsequence in escapeSequence . Split ( ';' ) )
239+ var parts = escapeSequence . Split ( ';' ) ;
240+
241+ for ( int j = 0 ; j < parts . Length ; j ++ )
237242 {
243+ var subsequence = parts [ j ] ;
244+
238245 if ( subsequence is "1" or "2" or "3" )
239246 {
240247 // Ignore the font effect sequence: 1 - bold; 2 - dimmed color; 3 - italics
241248 // They are used in the metadata line of the list view.
242249 continue ;
243250 }
244251
245- EscapeSequenceActions [ subsequence ] ( this ) ;
252+ // Handle 256-color/RGB sequences: 38 (FG) or 48 (BG)
253+ // PSReadLine uses \x1b[38;5;238m and \x1b[48;5;238m for predictions
254+ // Track these sequences without changing color state
255+ if ( subsequence is "38" or "48" )
256+ {
257+ bool isForeground = subsequence == "38" ;
258+ if ( j + 2 < parts . Length )
259+ {
260+ var mode = parts [ j + 1 ] ;
261+ if ( mode == "5" )
262+ {
263+ if ( int . TryParse ( parts [ j + 2 ] , out var colorIndex ) )
264+ {
265+ Emitted256Colors . Add ( ( writePos , isForeground , colorIndex ) ) ;
266+ }
267+ j += 2 ; // Skip mode and color index
268+ }
269+ else if ( mode == "2" && j + 4 < parts . Length )
270+ {
271+ j += 4 ; // Skip mode and RGB values (r, g, b)
272+ }
273+ }
274+ continue ;
275+ }
276+
277+ if ( EscapeSequenceActions . ContainsKey ( subsequence ) )
278+ {
279+ EscapeSequenceActions [ subsequence ] ( this ) ;
280+ }
246281 }
247282 i = endSequence ;
248283 continue ;
@@ -462,16 +497,48 @@ public override void Write(string s)
462497 var endSequence = s . IndexOfAny ( endEscapeChars , i ) ;
463498 var len = endSequence - i - ( s [ endSequence ] != 'm' ? 1 : 2 ) ;
464499 var escapeSequence = s . Substring ( i + 2 , len ) ;
465- foreach ( var subsequence in escapeSequence . Split ( ';' ) )
500+ var parts = escapeSequence . Split ( ';' ) ;
501+
502+ for ( int j = 0 ; j < parts . Length ; j ++ )
466503 {
504+ var subsequence = parts [ j ] ;
505+
467506 if ( subsequence is "1" or "2" or "3" )
468507 {
469508 // Ignore the font effect sequence: 1 - bold; 2 - dimmed color; 3 - italics
470509 // They are used in the metadata line of the list view.
471510 continue ;
472511 }
473512
474- EscapeSequenceActions [ subsequence ] ( this ) ;
513+ // Handle 256-color/RGB sequences: 38 (FG) or 48 (BG)
514+ // PSReadLine uses \x1b[38;5;238m and \x1b[48;5;238m for predictions
515+ // Track these sequences without changing color state
516+ if ( subsequence is "38" or "48" )
517+ {
518+ bool isForeground = subsequence == "38" ;
519+ if ( j + 2 < parts . Length )
520+ {
521+ var mode = parts [ j + 1 ] ;
522+ if ( mode == "5" )
523+ {
524+ if ( int . TryParse ( parts [ j + 2 ] , out var colorIndex ) )
525+ {
526+ Emitted256Colors . Add ( ( writePos , isForeground , colorIndex ) ) ;
527+ }
528+ j += 2 ; // Skip mode and color index
529+ }
530+ else if ( mode == "2" && j + 4 < parts . Length )
531+ {
532+ j += 4 ; // Skip mode and RGB values (r, g, b)
533+ }
534+ }
535+ continue ;
536+ }
537+
538+ if ( EscapeSequenceActions . ContainsKey ( subsequence ) )
539+ {
540+ EscapeSequenceActions [ subsequence ] ( this ) ;
541+ }
475542 }
476543 i = endSequence ;
477544 continue ;
0 commit comments