Skip to content

testing removing consoleecho #988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider future proofing this somewhat by making it IsPS7OrGreater. It doesn't really matter since we aren't using it, but could save us some trouble if it gets used somewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d like to keep this as IsPS7. I get the idea that PS8 will maybe be on .NET 5 which might have differences… not too mention that will be years out. Plus, if PS7 makes it back into Windows in a few years, maybe there will be a day where we drop IsPS5 so this file is certainly “living” anyway.

}

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;
}
}