Skip to content

Commit f2c1159

Browse files
testing removing consoleecho (#988)
* testing removing consoleecho * conditional logic for PS6 * style nit Co-Authored-By: Patrick Meinecke <SeeminglyScience@users.noreply.github.com>
1 parent 7915b32 commit f2c1159

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/PowerShellEditorServices/Console/ConsoleProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal static ConsoleKeyInfo UnixReadKey(bool intercept, CancellationToken can
189189
}
190190
catch (OperationCanceledException)
191191
{
192-
return default(ConsoleKeyInfo);
192+
return default;
193193
}
194194
}
195195
}

src/PowerShellEditorServices/Console/UnixConsoleOperations.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToke
4949
// To work around this we wait for a key to be pressed before actually calling Console.ReadKey.
5050
// However, any pressed keys during this time will be echoed to the console. To get around
5151
// this we use the UnixConsoleEcho package to disable echo prior to waiting.
52-
InputEcho.Disable();
52+
if (Utils.IsPS6)
53+
{
54+
InputEcho.Disable();
55+
}
56+
5357
try
5458
{
5559
// The WaitForKeyAvailable delegate switches between a long delay between waits and
@@ -59,7 +63,10 @@ public ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToke
5963
}
6064
finally
6165
{
62-
InputEcho.Disable();
66+
if (Utils.IsPS6)
67+
{
68+
InputEcho.Disable();
69+
}
6370
s_readKeyHandle.Release();
6471
}
6572

@@ -82,14 +89,20 @@ public async Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken
8289

8390
// I tried to replace this library with a call to `stty -echo`, but unfortunately
8491
// the library also sets up allowing backspace to trigger `Console.KeyAvailable`.
85-
InputEcho.Disable();
92+
if (Utils.IsPS6)
93+
{
94+
InputEcho.Disable();
95+
}
8696
try
8797
{
8898
while (!await WaitForKeyAvailableAsync(cancellationToken));
8999
}
90100
finally
91101
{
92-
InputEcho.Enable();
102+
if (Utils.IsPS6)
103+
{
104+
InputEcho.Enable();
105+
}
93106
s_readKeyHandle.Release();
94107
}
95108

src/PowerShellEditorServices/Utility/Utils.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using System;
7+
using System.Reflection;
78
using System.Runtime.InteropServices;
89

910
namespace Microsoft.PowerShell.EditorServices
@@ -17,5 +18,38 @@ internal static class Utils
1718
/// True if we are running on .NET Core, false otherwise.
1819
/// </summary>
1920
public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal);
21+
22+
/// <summary>
23+
/// Get's the Version of PowerShell being used.
24+
/// </summary>
25+
public static Version PSVersion { get; } = PowerShellReflectionUtils.PSVersion;
26+
27+
/// <summary>
28+
/// True if we are running in Windows PowerShell, false otherwise.
29+
/// </summary>
30+
public static bool IsPS5 { get; } = PSVersion.Major == 5;
31+
32+
/// <summary>
33+
/// True if we are running in PowerShell Core 6, false otherwise.
34+
/// </summary>
35+
public static bool IsPS6 { get; } = PSVersion.Major == 6;
36+
37+
/// <summary>
38+
/// True if we are running in PowerShell 7, false otherwise.
39+
/// </summary>
40+
public static bool IsPS7 { get; } = PSVersion.Major == 7;
41+
}
42+
43+
internal static class PowerShellReflectionUtils
44+
{
45+
46+
private static readonly Assembly s_psRuntimeAssembly = typeof(System.Management.Automation.Runspaces.Runspace).Assembly;
47+
private static readonly PropertyInfo s_psVersionProperty = s_psRuntimeAssembly.GetType("System.Management.Automation.PSVersionInfo")
48+
.GetProperty("PSVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
49+
50+
/// <summary>
51+
/// Get's the Version of PowerShell being used.
52+
/// </summary>
53+
public static Version PSVersion { get; } = s_psVersionProperty.GetValue(null) as Version;
2054
}
2155
}

0 commit comments

Comments
 (0)