Skip to content

Commit d045b50

Browse files
authored
Choose the inline prediction color based on the environment (#3808)
1 parent d98e1e6 commit d045b50

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,14 @@ public class PSConsoleReadLineOptions
9595

9696
// Find the most suitable color using https://stackoverflow.com/a/33206814
9797
// Default prediction color settings:
98-
// - use FG color 'dim white italic' for the inline-view suggestion text
9998
// - use FG color 'yellow' for the list-view suggestion text
10099
// - use BG color 'dark black' for the selected list-view suggestion text
101-
public const string DefaultInlinePredictionColor = "\x1b[97;2;3m";
102100
public const string DefaultListPredictionColor = "\x1b[33m";
103101
public const string DefaultListPredictionSelectedColor = "\x1b[48;5;238m";
104-
public const string DefaultListPredictionTooltipColor = "\x1b[97;2;3m";
105102

106-
public static EditMode DefaultEditMode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
107-
? EditMode.Windows
108-
: EditMode.Emacs;
103+
public static readonly string DefaultInlinePredictionColor;
104+
public static readonly string DefaultListPredictionTooltipColor;
105+
public static readonly EditMode DefaultEditMode;
109106

110107
public const string DefaultContinuationPrompt = ">> ";
111108

@@ -166,6 +163,40 @@ public class PSConsoleReadLineOptions
166163
/// </summary>
167164
public const int DefaultAnsiEscapeTimeout = 100;
168165

166+
static PSConsoleReadLineOptions()
167+
{
168+
// For inline-view suggestion text, we use the new FG color 'dim white italic' when possible, because it provides
169+
// sufficient contrast in terminals that don't use a pure black background (like VSCode terminal).
170+
// However, on Windows 10 and Windows Server, the ConHost doesn't support font effect VT sequences, such as 'dim'
171+
// and 'italic', so we need to use the old FG color 'dark black' as in the v2.2.6.
172+
const string newInlinePredictionColor = "\x1b[97;2;3m";
173+
const string oldInlinePredictionColor = "\x1b[38;5;238m";
174+
175+
ColorSetters = null;
176+
DefaultEditMode = EditMode.Emacs;
177+
DefaultInlinePredictionColor = newInlinePredictionColor;
178+
179+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
180+
{
181+
DefaultEditMode = EditMode.Windows;
182+
183+
// Our tests expect that the default inline-view color is set to the new color, so we configure
184+
// the color based on system environment only if we are not in test runs.
185+
if (AppDomain.CurrentDomain.FriendlyName is not "PSReadLine.Tests")
186+
{
187+
DefaultInlinePredictionColor =
188+
Environment.OSVersion.Version.Build >= 22621 // on Windows 11 22H2 or newer versions
189+
|| Environment.GetEnvironmentVariable("WT_SESSION") is not null // in Windows Terminal
190+
? newInlinePredictionColor
191+
: oldInlinePredictionColor;
192+
}
193+
}
194+
195+
// Use the same color for the list prediction tooltips.
196+
DefaultListPredictionTooltipColor = DefaultInlinePredictionColor;
197+
DefaultAddToHistoryHandler = s => PSConsoleReadLine.GetDefaultAddToHistoryOption(s);
198+
}
199+
169200
public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)
170201
{
171202
ResetColors();
@@ -285,8 +316,7 @@ public object ContinuationPromptColor
285316
/// or added to memory only, or added to both memory and history file.
286317
/// </summary>
287318
public Func<string, object> AddToHistoryHandler { get; set; }
288-
public static readonly Func<string, object> DefaultAddToHistoryHandler =
289-
s => PSConsoleReadLine.GetDefaultAddToHistoryOption(s);
319+
public static readonly Func<string, object> DefaultAddToHistoryHandler;
290320

291321
/// <summary>
292322
/// This handler is called from ValidateAndAcceptLine.
@@ -305,7 +335,6 @@ public object ContinuationPromptColor
305335
/// odd things with script blocks, we create a white-list of commands
306336
/// that do invoke the script block - this covers the most useful cases.
307337
/// </summary>
308-
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
309338
public HashSet<string> CommandsToValidateScriptBlockArguments { get; set; }
310339

311340
/// <summary>
@@ -555,7 +584,7 @@ internal void ResetColors()
555584
SelectionColor = VTColorUtils.AsEscapeSequence(bg, fg);
556585
}
557586

558-
private static Dictionary<string, Action<PSConsoleReadLineOptions, object>> ColorSetters = null;
587+
private static Dictionary<string, Action<PSConsoleReadLineOptions, object>> ColorSetters;
559588

560589
internal void SetColor(string property, object value)
561590
{
@@ -830,7 +859,6 @@ public class ChangePSReadLineKeyHandlerCommandBase : PSCmdlet
830859
[Parameter(Position = 0, Mandatory = true)]
831860
[Alias("Key")]
832861
[ValidateNotNullOrEmpty]
833-
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
834862
public string[] Chord { get; set; }
835863

836864
[Parameter]
@@ -903,8 +931,7 @@ protected override void EndProcessing()
903931
}
904932
}
905933

906-
private readonly Lazy<RuntimeDefinedParameterDictionary> _dynamicParameters =
907-
new Lazy<RuntimeDefinedParameterDictionary>(CreateDynamicParametersResult);
934+
private readonly Lazy<RuntimeDefinedParameterDictionary> _dynamicParameters = new(CreateDynamicParametersResult);
908935

909936
private static RuntimeDefinedParameterDictionary CreateDynamicParametersResult()
910937
{
@@ -1027,7 +1054,7 @@ public static class VTColorUtils
10271054

10281055
public const ConsoleColor UnknownColor = (ConsoleColor) (-1);
10291056
private static readonly Dictionary<string, ConsoleColor> ConsoleColors =
1030-
new Dictionary<string, ConsoleColor>(StringComparer.OrdinalIgnoreCase)
1057+
new(StringComparer.OrdinalIgnoreCase)
10311058
{
10321059
{"Black", ConsoleColor.Black},
10331060
{"DarkBlue", ConsoleColor.DarkBlue},

PSReadLine/PlatformWindows.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,7 @@ internal static extern int NtQueryInformationProcess(
637637
internal static int GetParentPid(Process process)
638638
{
639639
// (This is how ProcessCodeMethods in pwsh does it.)
640-
PROCESS_BASIC_INFORMATION pbi;
641-
int size;
642-
var res = NtQueryInformationProcess(process.Handle, 0, out pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out size);
643-
640+
var res = NtQueryInformationProcess(process.Handle, 0, out PROCESS_BASIC_INFORMATION pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out _);
644641
return res != 0 ? InvalidProcessId : pbi.InheritedFromUniqueProcessId.ToInt32();
645642
}
646643

PSReadLine/ReadLine.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
namespace Microsoft.PowerShell
2727
{
28-
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors")]
29-
[SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable")]
3028
class ExitException : Exception { }
3129

3230
public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods

0 commit comments

Comments
 (0)