Skip to content

Commit

Permalink
testing removing consoleecho (#988)
Browse files Browse the repository at this point in the history
* testing removing consoleecho

* conditional logic for PS6

* style nit

Co-Authored-By: Patrick Meinecke <SeeminglyScience@users.noreply.github.com>
  • Loading branch information
TylerLeonhardt and SeeminglyScience authored Jul 10, 2019
1 parent 7915b32 commit f2c1159
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/PowerShellEditorServices/Console/ConsoleProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ internal static ConsoleKeyInfo UnixReadKey(bool intercept, CancellationToken can
}
catch (OperationCanceledException)
{
return default(ConsoleKeyInfo);
return default;
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/PowerShellEditorServices/Console/UnixConsoleOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,7 +63,10 @@ public ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToke
}
finally
{
InputEcho.Disable();
if (Utils.IsPS6)
{
InputEcho.Disable();
}
s_readKeyHandle.Release();
}

Expand All @@ -82,14 +89,20 @@ public async Task<ConsoleKeyInfo> 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();
}

Expand Down
34 changes: 34 additions & 0 deletions src/PowerShellEditorServices/Utility/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Microsoft.PowerShell.EditorServices
Expand All @@ -17,5 +18,38 @@ internal static class Utils
/// True if we are running on .NET Core, false otherwise.
/// </summary>
public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal);

/// <summary>
/// Get's the Version of PowerShell being used.
/// </summary>
public static Version PSVersion { get; } = PowerShellReflectionUtils.PSVersion;

/// <summary>
/// True if we are running in Windows PowerShell, false otherwise.
/// </summary>
public static bool IsPS5 { get; } = PSVersion.Major == 5;

/// <summary>
/// True if we are running in PowerShell Core 6, false otherwise.
/// </summary>
public static bool IsPS6 { get; } = PSVersion.Major == 6;

/// <summary>
/// True if we are running in PowerShell 7, false otherwise.
/// </summary>
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);

/// <summary>
/// Get's the Version of PowerShell being used.
/// </summary>
public static Version PSVersion { get; } = s_psVersionProperty.GetValue(null) as Version;
}
}

0 comments on commit f2c1159

Please sign in to comment.