@@ -384,6 +384,12 @@ public object ErrorColor
384384 set => _errorColor = VTColorUtils . AsEscapeSequence ( value ) ;
385385 }
386386
387+ public object SelectionColor
388+ {
389+ get => _selectionColor ;
390+ set => _selectionColor = VTColorUtils . AsEscapeSequence ( value ) ;
391+ }
392+
387393 internal string _defaultTokenColor ;
388394 internal string _commentColor ;
389395 internal string _keywordColor ;
@@ -397,10 +403,12 @@ public object ErrorColor
397403 internal string _memberColor ;
398404 internal string _emphasisColor ;
399405 internal string _errorColor ;
406+ internal string _selectionColor ;
400407
401408 internal void ResetColors ( )
402409 {
403- DefaultTokenColor = Console . ForegroundColor ;
410+ var fg = Console . ForegroundColor ;
411+ DefaultTokenColor = fg ;
404412 CommentColor = DefaultCommentColor ;
405413 KeywordColor = DefaultKeywordColor ;
406414 StringColor = DefaultStringColor ;
@@ -413,6 +421,16 @@ internal void ResetColors()
413421 MemberColor = DefaultNumberColor ;
414422 EmphasisColor = DefaultEmphasisColor ;
415423 ErrorColor = DefaultErrorColor ;
424+
425+ var bg = Console . BackgroundColor ;
426+ if ( fg == VTColorUtils . UnknownColor || bg == VTColorUtils . UnknownColor )
427+ {
428+ // TODO: light vs. dark
429+ fg = ConsoleColor . Black ;
430+ bg = ConsoleColor . Gray ;
431+ }
432+
433+ SelectionColor = VTColorUtils . AsEscapeSequence ( bg , fg ) ;
416434 }
417435
418436 private static Dictionary < string , Action < PSConsoleReadLineOptions , object > > ColorSetters = null ;
@@ -438,6 +456,7 @@ internal void SetColor(string property, object value)
438456 { "Type" , ( o , v ) => o . TypeColor = v } ,
439457 { "Number" , ( o , v ) => o . NumberColor = v } ,
440458 { "Member" , ( o , v ) => o . MemberColor = v } ,
459+ { "Selection" , ( o , v ) => o . SelectionColor = v } ,
441460 } ;
442461
443462 Interlocked . CompareExchange ( ref ColorSetters , setters , null ) ;
@@ -649,16 +668,6 @@ protected override void EndProcessing()
649668 }
650669 }
651670
652- [ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Property ) ]
653- internal class ValidateColorAttribute : ValidateArgumentsAttribute
654- {
655- protected override void Validate ( object arguments , EngineIntrinsics engineIntrinsics )
656- {
657- if ( ! VTColorUtils . IsValidColor ( arguments ) )
658- throw new ValidationMetadataException ( PSReadLineResources . InvalidColorParameter ) ;
659- }
660- }
661-
662671 public class ChangePSReadLineKeyHandlerCommandBase : PSCmdlet
663672 {
664673 [ Parameter ( Position = 0 , Mandatory = true ) ]
@@ -837,9 +846,7 @@ protected override void EndProcessing()
837846
838847 public static class VTColorUtils
839848 {
840- internal static bool IsValidColorImpl ( ConsoleColor c ) => true ;
841-
842- internal static bool IsValidColorImpl ( string s ) => true ;
849+ public const ConsoleColor UnknownColor = ( ConsoleColor ) ( - 1 ) ;
843850
844851 public static bool IsValidColor ( object o )
845852 {
@@ -922,6 +929,20 @@ public static string AsEscapeSequence(object o, bool isBackground)
922929 throw new ArgumentException ( "o" ) ;
923930 }
924931
932+ public static string AsEscapeSequence ( ConsoleColor fg , ConsoleColor bg )
933+ {
934+ if ( fg < 0 || fg >= ( ConsoleColor ) ForegroundColorMap . Length )
935+ throw new ArgumentOutOfRangeException ( nameof ( fg ) ) ;
936+ if ( bg < 0 || bg >= ( ConsoleColor ) ForegroundColorMap . Length )
937+ throw new ArgumentOutOfRangeException ( nameof ( bg ) ) ;
938+
939+ string ExtractCode ( string s )
940+ {
941+ return s . Substring ( 2 ) . TrimEnd ( new [ ] { 'm' } ) ;
942+ }
943+ return "\x1b [" + ExtractCode ( ForegroundColorMap [ ( int ) fg ] ) + ";" + ExtractCode ( BackgroundColorMap [ ( int ) bg ] ) + "m" ;
944+ }
945+
925946 private static readonly string [ ] BackgroundColorMap = {
926947 "\x1b [40m" , // Black
927948 "\x1b [44m" , // DarkBlue
0 commit comments