Skip to content

Commit 9fa13a2

Browse files
committed
Delete deprecated console operations
So much simpler.
1 parent 39ced5f commit 9fa13a2

9 files changed

+15
-204
lines changed

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/ConsoleProxy.cs

+2-69
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Runtime.InteropServices;
65
using System.Threading;
76

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

24-
private static readonly IConsoleOperations s_consoleProxy;
25-
26-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline", Justification = "Platform specific initialization")]
27-
static ConsoleProxy()
28-
{
29-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
30-
{
31-
s_consoleProxy = new WindowsConsoleOperations();
32-
return;
33-
}
34-
35-
s_consoleProxy = new UnixConsoleOperations();
36-
}
37-
38-
/// <summary>
39-
/// Obtains the next character or function key pressed by the user asynchronously.
40-
/// Does not block when other console API's are called.
41-
/// </summary>
42-
/// <param name="intercept">
43-
/// Determines whether to display the pressed key in the console window. <see langword="true" />
44-
/// to not display the pressed key; otherwise, <see langword="false" />.
45-
/// </param>
46-
/// <param name="cancellationToken">The CancellationToken to observe.</param>
47-
/// <returns>
48-
/// An object that describes the <see cref="ConsoleKey" /> constant and Unicode character, if any,
49-
/// that correspond to the pressed console key. The <see cref="ConsoleKeyInfo" /> object also
50-
/// describes, in a bitwise combination of <see cref="ConsoleModifiers" /> values, whether
51-
/// one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.
52-
/// </returns>
53-
public static ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToken) =>
54-
s_consoleProxy.ReadKey(intercept, cancellationToken);
55-
56-
/// <summary>
57-
/// Obtains the horizontal position of the console cursor. TODO: Is this still necessary?
58-
/// </summary>
59-
/// <returns>The horizontal position of the console cursor.</returns>
60-
public static int GetCursorLeft() => s_consoleProxy.GetCursorLeft();
61-
62-
/// <summary>
63-
/// Obtains the horizontal position of the console cursor. TODO: Is this still necessary?
64-
/// </summary>
65-
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
66-
/// <returns>The horizontal position of the console cursor.</returns>
67-
public static int GetCursorLeft(CancellationToken cancellationToken) =>
68-
s_consoleProxy.GetCursorLeft(cancellationToken);
69-
70-
/// <summary>
71-
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
72-
/// </summary>
73-
/// <returns>The vertical position of the console cursor.</returns>
74-
public static int GetCursorTop() => s_consoleProxy.GetCursorTop();
75-
76-
/// <summary>
77-
/// Obtains the vertical position of the console cursor. TODO: Is this still necessary?
78-
/// </summary>
79-
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
80-
/// <returns>The vertical position of the console cursor.</returns>
81-
public static int GetCursorTop(CancellationToken cancellationToken) =>
82-
s_consoleProxy.GetCursorTop(cancellationToken);
83-
8423
/// <summary>
8524
/// This method is sent to PSReadLine as a workaround for issues with the System.Console
8625
/// implementation. Functionally it is the same as System.Console.ReadKey,
@@ -101,14 +40,8 @@ public static int GetCursorTop(CancellationToken cancellationToken) =>
10140
/// </returns>
10241
internal static ConsoleKeyInfo SafeReadKey(bool intercept, CancellationToken cancellationToken)
10342
{
104-
try
105-
{
106-
return s_consoleProxy.ReadKey(intercept, cancellationToken);
107-
}
108-
catch (OperationCanceledException)
109-
{
110-
return s_nullKeyInfo;
111-
}
43+
ConsoleKeyInfo key = System.Console.ReadKey(intercept);
44+
return cancellationToken.IsCancellationRequested ? s_nullKeyInfo : key;
11245
}
11346
}
11447
}

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
5-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
64
using System.Collections.Generic;
75
using System.Linq;
86
using System.Management.Automation;
97
using System.Text;
108
using System.Threading;
119
using System.Threading.Tasks;
10+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
11+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
1212

1313
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
1414
{
@@ -46,8 +46,8 @@ public override string ReadLine(CancellationToken cancellationToken)
4646

4747
StringBuilder inputLine = new();
4848

49-
int initialCursorCol = ConsoleProxy.GetCursorLeft(cancellationToken);
50-
int initialCursorRow = ConsoleProxy.GetCursorTop(cancellationToken);
49+
int initialCursorCol = Console.CursorLeft;
50+
int initialCursorRow = Console.CursorTop;
5151

5252
int currentCursorIndex = 0;
5353

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/PsrlReadLine.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public PsrlReadLine(
4040

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

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

4545
#endregion
4646

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/TerminalReadLine.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public SecureString ReadSecureLine(CancellationToken cancellationToken)
6969
}
7070
else if (previousInputLength > 0 && currentInputLength < previousInputLength)
7171
{
72-
int row = ConsoleProxy.GetCursorTop(cancellationToken);
73-
int col = ConsoleProxy.GetCursorLeft(cancellationToken);
72+
int row = Console.CursorTop;
73+
int col = Console.CursorLeft;
7474

7575
// Back up the cursor before clearing the character
7676
col--;

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/UnixConsoleOperations.cs

-64
This file was deleted.

Diff for: src/PowerShellEditorServices/Services/PowerShell/Console/WindowsConsoleOperations.cs

-41
This file was deleted.

Diff for: src/PowerShellEditorServices/Services/PowerShell/Host/EditorServicesConsolePSHostRawUserInterface.cs

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.Extensions.Logging;
5-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
64
using System;
75
using System.Management.Automation;
86
using System.Management.Automation.Host;
9-
using System.Threading;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
109

1110
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
1211
{
@@ -26,8 +25,6 @@ internal class EditorServicesConsolePSHostRawUserInterface : PSHostRawUserInterf
2625
/// Creates a new instance of the TerminalPSHostRawUserInterface
2726
/// class with the given IConsoleHost implementation.
2827
/// </summary>
29-
/// <param name="logger">The ILogger implementation to use for this instance.</param>
30-
/// <param name="internalHost">The InternalHost instance from the origin runspace.</param>
3128
public EditorServicesConsolePSHostRawUserInterface(
3229
ILoggerFactory loggerFactory,
3330
PSHostRawUserInterface internalRawUI)
@@ -72,10 +69,7 @@ public override Size BufferSize
7269
/// </summary>
7370
public override Coordinates CursorPosition
7471
{
75-
get => new(
76-
ConsoleProxy.GetCursorLeft(),
77-
ConsoleProxy.GetCursorTop());
78-
72+
get => _internalRawUI.CursorPosition;
7973
set => _internalRawUI.CursorPosition = value;
8074
}
8175

@@ -137,7 +131,6 @@ public override string WindowTitle
137131
/// <returns>A KeyInfo struct with details about the current keypress.</returns>
138132
public override KeyInfo ReadKey(ReadKeyOptions options)
139133
{
140-
141134
bool includeUp = (options & ReadKeyOptions.IncludeKeyUp) != 0;
142135

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

172165
if (IsCtrlC(key))
173166
{
@@ -194,11 +187,7 @@ public override KeyInfo ReadKey(ReadKeyOptions options)
194187
/// <summary>
195188
/// Flushes the current input buffer.
196189
/// </summary>
197-
public override void FlushInputBuffer()
198-
{
199-
_logger.LogWarning(
200-
"PSHostRawUserInterface.FlushInputBuffer was called");
201-
}
190+
public override void FlushInputBuffer() => _logger.LogWarning("PSHostRawUserInterface.FlushInputBuffer was called");
202191

203192
/// <summary>
204193
/// Gets the contents of the console buffer in a rectangular area.

Diff for: src/PowerShellEditorServices/Services/PowerShell/Host/EditorServicesConsolePSHostUserInterface.cs

-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
using System.Management.Automation;
99
using System.Management.Automation.Host;
1010
using System.Security;
11-
using System.Threading;
1211
using Microsoft.Extensions.Logging;
13-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
1412

1513
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
1614
{
1715
internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface
1816
{
19-
private readonly IReadLineProvider _readLineProvider;
20-
2117
private readonly PSHostUserInterface _underlyingHostUI;
2218

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

2925
public EditorServicesConsolePSHostUserInterface(
3026
ILoggerFactory loggerFactory,
31-
IReadLineProvider readLineProvider,
3227
PSHostUserInterface underlyingHostUI)
3328
{
34-
_readLineProvider = readLineProvider;
3529
_underlyingHostUI = underlyingHostUI;
3630
RawUI = new EditorServicesConsolePSHostRawUserInterface(loggerFactory, underlyingHostUI.RawUI);
3731
}

Diff for: src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public PsesInternalHost(
116116

117117
DebugContext = new PowerShellDebugContext(loggerFactory, this);
118118
UI = hostInfo.ConsoleReplEnabled
119-
? new EditorServicesConsolePSHostUserInterface(loggerFactory, _readLineProvider, hostInfo.PSHost.UI)
119+
? new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI)
120120
: new NullPSHostUI();
121121
}
122122

0 commit comments

Comments
 (0)