From d34ecb503fed4b7cc3cea08c201d380fc8bed2d5 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 2 Jul 2019 08:58:55 -0700 Subject: [PATCH 1/3] testing removing consoleecho --- .../Console/ConsoleProxy.cs | 10 +++++----- .../Session/PSReadLinePromptContext.cs | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/PowerShellEditorServices/Console/ConsoleProxy.cs b/src/PowerShellEditorServices/Console/ConsoleProxy.cs index b9312ca7c..478dd52f2 100644 --- a/src/PowerShellEditorServices/Console/ConsoleProxy.cs +++ b/src/PowerShellEditorServices/Console/ConsoleProxy.cs @@ -20,13 +20,13 @@ internal static class ConsoleProxy static ConsoleProxy() { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { + // if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + // { s_consoleProxy = new WindowsConsoleOperations(); - return; - } + // return; + // } - s_consoleProxy = new UnixConsoleOperations(); + // s_consoleProxy = new UnixConsoleOperations(); } /// diff --git a/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs index a71e32ed9..526765e65 100644 --- a/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs @@ -67,15 +67,15 @@ internal PSReadLinePromptContext( _consoleReadLine = new ConsoleReadLine(powerShellContext); _readLineProxy = readLineProxy; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return; - } - - _readLineProxy.OverrideReadKey( - intercept => ConsoleProxy.UnixReadKey( - intercept, - _readLineCancellationSource.Token)); + // if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + // { + // return; + // } + + // _readLineProxy.OverrideReadKey( + // intercept => ConsoleProxy.UnixReadKey( + // intercept, + // _readLineCancellationSource.Token)); } internal static bool TryGetPSReadLineProxy( From c473f13a0a0f42cea1dfb8aa6928a4a2239dce1b Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 2 Jul 2019 12:02:29 -0700 Subject: [PATCH 2/3] conditional logic for PS6 --- .../Console/ConsoleProxy.cs | 12 +++---- .../Console/UnixConsoleOperations.cs | 21 +++++++++--- .../Session/PSReadLinePromptContext.cs | 18 +++++----- src/PowerShellEditorServices/Utility/Utils.cs | 34 +++++++++++++++++++ 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/PowerShellEditorServices/Console/ConsoleProxy.cs b/src/PowerShellEditorServices/Console/ConsoleProxy.cs index 478dd52f2..c38c81724 100644 --- a/src/PowerShellEditorServices/Console/ConsoleProxy.cs +++ b/src/PowerShellEditorServices/Console/ConsoleProxy.cs @@ -20,13 +20,13 @@ internal static class ConsoleProxy static ConsoleProxy() { - // if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - // { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { s_consoleProxy = new WindowsConsoleOperations(); - // return; - // } + return; + } - // s_consoleProxy = new UnixConsoleOperations(); + s_consoleProxy = new UnixConsoleOperations(); } /// @@ -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/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs index 526765e65..a71e32ed9 100644 --- a/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Session/PSReadLinePromptContext.cs @@ -67,15 +67,15 @@ internal PSReadLinePromptContext( _consoleReadLine = new ConsoleReadLine(powerShellContext); _readLineProxy = readLineProxy; - // if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - // { - // return; - // } - - // _readLineProxy.OverrideReadKey( - // intercept => ConsoleProxy.UnixReadKey( - // intercept, - // _readLineCancellationSource.Token)); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return; + } + + _readLineProxy.OverrideReadKey( + intercept => ConsoleProxy.UnixReadKey( + intercept, + _readLineCancellationSource.Token)); } internal static bool TryGetPSReadLineProxy( diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs index 05787be43..2ea87ee69 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 _psRuntimeAssembly = typeof(System.Management.Automation.Runspaces.Runspace).Assembly; + private static readonly PropertyInfo _psVersionProperty = _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; } = _psVersionProperty.GetValue(null) as Version; } } From 611635072ebbf2fa5a9e4edef6c49d22ee503fe6 Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Tue, 9 Jul 2019 08:05:58 -0700 Subject: [PATCH 3/3] style nit Co-Authored-By: Patrick Meinecke --- src/PowerShellEditorServices/Utility/Utils.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs index 2ea87ee69..cbac80891 100644 --- a/src/PowerShellEditorServices/Utility/Utils.cs +++ b/src/PowerShellEditorServices/Utility/Utils.cs @@ -43,13 +43,13 @@ internal static class Utils internal static class PowerShellReflectionUtils { - private static readonly Assembly _psRuntimeAssembly = typeof(System.Management.Automation.Runspaces.Runspace).Assembly; - private static readonly PropertyInfo _psVersionProperty = _psRuntimeAssembly.GetType("System.Management.Automation.PSVersionInfo") + 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; } = _psVersionProperty.GetValue(null) as Version; + public static Version PSVersion { get; } = s_psVersionProperty.GetValue(null) as Version; } }