Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Delete deprecated console operations
So much simpler.
  • Loading branch information
andyleejordan committed Apr 7, 2022
commit 9fa13a2c8df2b9b1fb09a5c3e9f86eff9d5a5d96
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
Expand All @@ -21,66 +20,6 @@ internal static class ConsoleProxy
alt: false,
control: false);

private static readonly IConsoleOperations s_consoleProxy;

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline", Justification = "Platform specific initialization")]
static ConsoleProxy()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
s_consoleProxy = new WindowsConsoleOperations();
return;
}

s_consoleProxy = new UnixConsoleOperations();
}

/// <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>
/// An object that describes the <see cref="ConsoleKey" /> constant and Unicode character, if any,
/// that correspond to the pressed console key. The <see cref="ConsoleKeyInfo" /> object also
/// describes, in a bitwise combination of <see cref="ConsoleModifiers" /> values, whether
/// one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.
/// </returns>
public static ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToken) =>
s_consoleProxy.ReadKey(intercept, cancellationToken);

/// <summary>
/// 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();

/// <summary>
/// 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 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();

/// <summary>
/// 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>
/// 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 All @@ -101,14 +40,8 @@ public static int GetCursorTop(CancellationToken cancellationToken) =>
/// </returns>
internal static ConsoleKeyInfo SafeReadKey(bool intercept, CancellationToken cancellationToken)
{
try
{
return s_consoleProxy.ReadKey(intercept, cancellationToken);
}
catch (OperationCanceledException)
{
return s_nullKeyInfo;
}
ConsoleKeyInfo key = System.Console.ReadKey(intercept);
return cancellationToken.IsCancellationRequested ? s_nullKeyInfo : key;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
{
Expand Down Expand Up @@ -46,8 +46,8 @@ public override string ReadLine(CancellationToken cancellationToken)

StringBuilder inputLine = new();

int initialCursorCol = ConsoleProxy.GetCursorLeft(cancellationToken);
int initialCursorRow = ConsoleProxy.GetCursorTop(cancellationToken);
int initialCursorCol = Console.CursorLeft;
int initialCursorRow = Console.CursorTop;

int currentCursorIndex = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PsrlReadLine(

public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken);

protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => ConsoleProxy.ReadKey(intercept: true, cancellationToken);
protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => ConsoleProxy.SafeReadKey(intercept: true, cancellationToken);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public SecureString ReadSecureLine(CancellationToken cancellationToken)
}
else if (previousInputLength > 0 && currentInputLength < previousInputLength)
{
int row = ConsoleProxy.GetCursorTop(cancellationToken);
int col = ConsoleProxy.GetCursorLeft(cancellationToken);
int row = Console.CursorTop;
int col = Console.CursorLeft;

// Back up the cursor before clearing the character
col--;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
using System;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
{
Expand All @@ -26,8 +25,6 @@ internal class EditorServicesConsolePSHostRawUserInterface : PSHostRawUserInterf
/// Creates a new instance of the TerminalPSHostRawUserInterface
/// class with the given IConsoleHost implementation.
/// </summary>
/// <param name="logger">The ILogger implementation to use for this instance.</param>
/// <param name="internalHost">The InternalHost instance from the origin runspace.</param>
public EditorServicesConsolePSHostRawUserInterface(
ILoggerFactory loggerFactory,
PSHostRawUserInterface internalRawUI)
Expand Down Expand Up @@ -72,10 +69,7 @@ public override Size BufferSize
/// </summary>
public override Coordinates CursorPosition
{
get => new(
ConsoleProxy.GetCursorLeft(),
ConsoleProxy.GetCursorTop());

get => _internalRawUI.CursorPosition;
set => _internalRawUI.CursorPosition = value;
}

Expand Down Expand Up @@ -137,7 +131,6 @@ public override string WindowTitle
/// <returns>A KeyInfo struct with details about the current keypress.</returns>
public override KeyInfo ReadKey(ReadKeyOptions options)
{

bool includeUp = (options & ReadKeyOptions.IncludeKeyUp) != 0;

// Key Up was requested and we have a cached key down we can return.
Expand Down Expand Up @@ -167,7 +160,7 @@ public override KeyInfo ReadKey(ReadKeyOptions options)
try
{
System.Console.TreatControlCAsInput = true;
ConsoleKeyInfo key = ConsoleProxy.ReadKey(intercept, default);
ConsoleKeyInfo key = ConsoleProxy.SafeReadKey(intercept, default);

if (IsCtrlC(key))
{
Expand All @@ -194,11 +187,7 @@ public override KeyInfo ReadKey(ReadKeyOptions options)
/// <summary>
/// Flushes the current input buffer.
/// </summary>
public override void FlushInputBuffer()
{
_logger.LogWarning(
"PSHostRawUserInterface.FlushInputBuffer was called");
}
public override void FlushInputBuffer() => _logger.LogWarning("PSHostRawUserInterface.FlushInputBuffer was called");

/// <summary>
/// Gets the contents of the console buffer in a rectangular area.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Security;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
{
internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface
{
private readonly IReadLineProvider _readLineProvider;

private readonly PSHostUserInterface _underlyingHostUI;

/// <summary>
Expand All @@ -28,10 +24,8 @@ internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface

public EditorServicesConsolePSHostUserInterface(
ILoggerFactory loggerFactory,
IReadLineProvider readLineProvider,
PSHostUserInterface underlyingHostUI)
{
_readLineProvider = readLineProvider;
_underlyingHostUI = underlyingHostUI;
RawUI = new EditorServicesConsolePSHostRawUserInterface(loggerFactory, underlyingHostUI.RawUI);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public PsesInternalHost(

DebugContext = new PowerShellDebugContext(loggerFactory, this);
UI = hostInfo.ConsoleReplEnabled
? new EditorServicesConsolePSHostUserInterface(loggerFactory, _readLineProvider, hostInfo.PSHost.UI)
? new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI)
: new NullPSHostUI();
}

Expand Down