Skip to content
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

Re-workaround uncancellable Console.ReadKey #1751

Merged
merged 7 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion .vsts-ci/templates/release-general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ steps:
**/Newtonsoft.Json.dll
**/OmniSharp*.dll
**/Serilog*.dll
**/UnixConsoleEcho.dll

# The SBOM generation requires our original sources with the `dotnet restore`
# produced `project.assets.json` files.
Expand Down
15 changes: 0 additions & 15 deletions Third Party Notices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ This file is based on or incorporates material from the projects listed below (T

---

UnixConsoleEcho

Copyright (c) 2017 Patrick Meinecke
Provided for Informational Purposes Only

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---

Serilog

Copyright 2013-2015 Serilog Contributors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.EditorServices.Hosting;
using System.Globalization;
using System.Collections;

// TODO: Remove this when we drop support for PS6.
#if CoreCLR
using System.Runtime.InteropServices;
#endif

#if DEBUG
using System.Diagnostics;
Expand All @@ -35,14 +29,6 @@ namespace Microsoft.PowerShell.EditorServices.Commands
[Cmdlet(VerbsLifecycle.Start, "EditorServices", DefaultParameterSetName = "NamedPipe")]
public sealed class StartEditorServicesCommand : PSCmdlet
{
// TODO: Remove this when we drop support for PS6.
private static readonly bool s_isWindows =
#if CoreCLR
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#else
true;
#endif

private readonly List<IDisposable> _disposableResources;

private readonly List<IDisposable> _loggerUnsubscribers;
Expand Down Expand Up @@ -420,12 +406,7 @@ private ConsoleReplKind GetReplKind()
return ConsoleReplKind.None;
}

// TODO: Remove this when we drop support for PS6.
Hashtable psVersionTable = (Hashtable)SessionState.PSVariable.GetValue("PSVersionTable");
dynamic version = psVersionTable["PSVersion"];
int majorVersion = (int)version.Major;

if (UseLegacyReadLine || (!s_isWindows && majorVersion == 6))
if (UseLegacyReadLine)
{
_logger.Log(PsesLogLevel.Diagnostic, "REPL configured as Legacy");
return ConsoleReplKind.LegacyReadLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<PackageReference Include="System.IO.Pipes.AccessControl" Version="5.0.0" />
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
<PackageReference Include="System.Security.Principal.Windows" Version="5.0.0" />
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,120 +47,33 @@ public static ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellat
s_consoleProxy.ReadKey(intercept, cancellationToken);

/// <summary>
/// Obtains the next character or function key pressed by the user asynchronously.
/// Does not block when other console API's are called.
/// </summary>
/// <param name="intercept">
/// Determines whether to display the pressed key in the console window. <see langword="true" />
/// to not display the pressed key; otherwise, <see langword="false" />.
/// </param>
/// <param name="cancellationToken">The CancellationToken to observe.</param>
/// <returns>
/// A task that will complete with a result of the key pressed by the user.
/// </returns>
public static Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken cancellationToken) =>
s_consoleProxy.ReadKeyAsync(intercept, cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the horizontal position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <returns>The horizontal position of the console cursor.</returns>
public static int GetCursorLeft() =>
s_consoleProxy.GetCursorLeft();
public static int GetCursorLeft() => s_consoleProxy.GetCursorLeft();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the horizontal position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The horizontal position of the console cursor.</returns>
public static int GetCursorLeft(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorLeft(cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorLeftAsync() =>
s_consoleProxy.GetCursorLeftAsync();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorLeftAsync(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorLeftAsync(cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <returns>The vertical position of the console cursor.</returns>
public static int GetCursorTop() =>
s_consoleProxy.GetCursorTop();
public static int GetCursorTop() => s_consoleProxy.GetCursorTop();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The vertical position of the console cursor.</returns>
public static int GetCursorTop(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorTop(cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorTopAsync() =>
s_consoleProxy.GetCursorTopAsync();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorTopAsync(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorTopAsync(cancellationToken);

/// <summary>
/// This method is sent to PSReadLine as a workaround for issues with the System.Console
/// implementation. Functionally it is the same as System.Console.ReadKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
{
Expand All @@ -30,109 +29,29 @@ internal interface IConsoleOperations
ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToken);

/// <summary>
/// Obtains the next character or function key pressed by the user asynchronously.
/// Does not block when other console API's are called.
/// </summary>
/// <param name="intercept">
/// Determines whether to display the pressed key in the console window. <see langword="true" />
/// to not display the pressed key; otherwise, <see langword="false" />.
/// </param>
/// <param name="cancellationToken">The CancellationToken to observe.</param>
/// <returns>
/// A task that will complete with a result of the key pressed by the user.
/// </returns>
Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the horizontal position of the console cursor.
/// </summary>
/// <returns>The horizontal position of the console cursor.</returns>
int GetCursorLeft();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the horizontal position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The horizontal position of the console cursor.</returns>
int GetCursorLeft(CancellationToken cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
Task<int> GetCursorLeftAsync();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
Task<int> GetCursorLeftAsync(CancellationToken cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <returns>The vertical position of the console cursor.</returns>
int GetCursorTop();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The vertical position of the console cursor.</returns>
int GetCursorTop(CancellationToken cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
Task<int> GetCursorTopAsync();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
Task<int> GetCursorTopAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Security;
using System.Threading;

Expand Down
Loading