From f2c1159cc914d6fb4ff3c60bc27d826166866a2c Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 10 Jul 2019 09:04:27 -0700 Subject: [PATCH] testing removing consoleecho (#988) * testing removing consoleecho * conditional logic for PS6 * style nit Co-Authored-By: Patrick Meinecke --- .../Console/ConsoleProxy.cs | 2 +- .../Console/UnixConsoleOperations.cs | 21 +++++++++--- src/PowerShellEditorServices/Utility/Utils.cs | 34 +++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/PowerShellEditorServices/Console/ConsoleProxy.cs b/src/PowerShellEditorServices/Console/ConsoleProxy.cs index b9312ca7c..c38c81724 100644 --- a/src/PowerShellEditorServices/Console/ConsoleProxy.cs +++ b/src/PowerShellEditorServices/Console/ConsoleProxy.cs @@ -189,7 +189,7 @@ internal static ConsoleKeyInfo UnixReadKey(bool intercept, CancellationToken can } catch (OperationCanceledException) { - return default(ConsoleKeyInfo); + return default; } } } diff --git a/src/PowerShellEditorServices/Console/UnixConsoleOperations.cs b/src/PowerShellEditorServices/Console/UnixConsoleOperations.cs index 199f312f2..e0ef73b65 100644 --- a/src/PowerShellEditorServices/Console/UnixConsoleOperations.cs +++ b/src/PowerShellEditorServices/Console/UnixConsoleOperations.cs @@ -49,7 +49,11 @@ public ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToke // To work around this we wait for a key to be pressed before actually calling Console.ReadKey. // However, any pressed keys during this time will be echoed to the console. To get around // this we use the UnixConsoleEcho package to disable echo prior to waiting. - InputEcho.Disable(); + if (Utils.IsPS6) + { + InputEcho.Disable(); + } + try { // The WaitForKeyAvailable delegate switches between a long delay between waits and @@ -59,7 +63,10 @@ public ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToke } finally { - InputEcho.Disable(); + if (Utils.IsPS6) + { + InputEcho.Disable(); + } s_readKeyHandle.Release(); } @@ -82,14 +89,20 @@ public async Task ReadKeyAsync(bool intercept, CancellationToken // I tried to replace this library with a call to `stty -echo`, but unfortunately // the library also sets up allowing backspace to trigger `Console.KeyAvailable`. - InputEcho.Disable(); + if (Utils.IsPS6) + { + InputEcho.Disable(); + } try { while (!await WaitForKeyAvailableAsync(cancellationToken)); } finally { - InputEcho.Enable(); + if (Utils.IsPS6) + { + InputEcho.Enable(); + } s_readKeyHandle.Release(); } diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs index 05787be43..cbac80891 100644 --- a/src/PowerShellEditorServices/Utility/Utils.cs +++ b/src/PowerShellEditorServices/Utility/Utils.cs @@ -4,6 +4,7 @@ // using System; +using System.Reflection; using System.Runtime.InteropServices; namespace Microsoft.PowerShell.EditorServices @@ -17,5 +18,38 @@ internal static class Utils /// True if we are running on .NET Core, false otherwise. /// public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal); + + /// + /// Get's the Version of PowerShell being used. + /// + public static Version PSVersion { get; } = PowerShellReflectionUtils.PSVersion; + + /// + /// True if we are running in Windows PowerShell, false otherwise. + /// + public static bool IsPS5 { get; } = PSVersion.Major == 5; + + /// + /// True if we are running in PowerShell Core 6, false otherwise. + /// + public static bool IsPS6 { get; } = PSVersion.Major == 6; + + /// + /// True if we are running in PowerShell 7, false otherwise. + /// + public static bool IsPS7 { get; } = PSVersion.Major == 7; + } + + internal static class PowerShellReflectionUtils + { + + private static readonly Assembly s_psRuntimeAssembly = typeof(System.Management.Automation.Runspaces.Runspace).Assembly; + private static readonly PropertyInfo s_psVersionProperty = s_psRuntimeAssembly.GetType("System.Management.Automation.PSVersionInfo") + .GetProperty("PSVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); + + /// + /// Get's the Version of PowerShell being used. + /// + public static Version PSVersion { get; } = s_psVersionProperty.GetValue(null) as Version; } }