Skip to content

Commit 7c97b9a

Browse files
committed
Delete ConsoleProxy.cs and further simplify ReadKey logic
1 parent 9fa13a2 commit 7c97b9a

File tree

8 files changed

+45
-342
lines changed

8 files changed

+45
-342
lines changed

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

-47
This file was deleted.

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

-57
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Security;
54
using System.Threading;
65

76
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
87
{
8+
// TODO: Do we really need a whole interface for this?
99
internal interface IReadLine
1010
{
1111
string ReadLine(CancellationToken cancellationToken);
12-
13-
SecureString ReadSecureLine(CancellationToken cancellationToken);
1412
}
1513
}

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

+12-17
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override string ReadLine(CancellationToken cancellationToken)
6868
switch (keyInfo.Key)
6969
{
7070
case ConsoleKey.Tab:
71-
if (currentCompletion == null)
71+
if (currentCompletion is null)
7272
{
7373
inputBeforeCompletion = inputLine.ToString();
7474
inputAfterCompletion = null;
@@ -115,14 +115,14 @@ public override string ReadLine(CancellationToken cancellationToken)
115115
currentCompletion?.GetNextResult(
116116
!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift));
117117

118-
if (completion != null)
118+
if (completion is not null)
119119
{
120120
currentCursorIndex =
121121
InsertInput(
122122
inputLine,
123123
promptStartCol,
124124
promptStartRow,
125-
$"{completion.CompletionText}{inputAfterCompletion}",
125+
completion.CompletionText + inputAfterCompletion,
126126
currentCursorIndex,
127127
insertIndex: currentCompletion.ReplacementIndex,
128128
replaceLength: inputLine.Length - currentCompletion.ReplacementIndex,
@@ -189,8 +189,7 @@ public override string ReadLine(CancellationToken cancellationToken)
189189
currentCompletion = null;
190190

191191
// TODO: Ctrl+Up should allow navigation in multi-line input
192-
193-
if (currentHistory == null)
192+
if (currentHistory is null)
194193
{
195194
historyIndex = -1;
196195

@@ -199,13 +198,13 @@ public override string ReadLine(CancellationToken cancellationToken)
199198

200199
currentHistory = _psesHost.InvokePSCommand<PSObject>(command, executionOptions: null, cancellationToken);
201200

202-
if (currentHistory != null)
201+
if (currentHistory is not null)
203202
{
204203
historyIndex = currentHistory.Count;
205204
}
206205
}
207206

208-
if (currentHistory != null && currentHistory.Count > 0 && historyIndex > 0)
207+
if (currentHistory?.Count > 0 && historyIndex > 0)
209208
{
210209
historyIndex--;
211210

@@ -227,9 +226,8 @@ public override string ReadLine(CancellationToken cancellationToken)
227226

228227
// The down arrow shouldn't cause history to be loaded,
229228
// it's only for navigating an active history array
230-
231229
if (historyIndex > -1 && historyIndex < currentHistory.Count &&
232-
currentHistory != null && currentHistory.Count > 0)
230+
currentHistory?.Count > 0)
233231
{
234232
historyIndex++;
235233

@@ -419,9 +417,7 @@ private ConsoleKeyInfo ReadKeyWithIdleSupport(CancellationToken cancellationToke
419417
}
420418
}
421419

422-
private ConsoleKeyInfo InvokeReadKeyFunc() =>
423-
// intercept = false means we display the key in the console
424-
_readKeyFunc(/* intercept */ false);
420+
private ConsoleKeyInfo InvokeReadKeyFunc() => _readKeyFunc(/* intercept */ false);
425421

426422
private static int InsertInput(
427423
StringBuilder inputLine,
@@ -496,10 +492,8 @@ private static int InsertInput(
496492
consoleWidth,
497493
finalCursorIndex);
498494
}
499-
else
500-
{
501-
return inputLine.Length;
502-
}
495+
496+
return inputLine.Length;
503497
}
504498

505499
private static int MoveCursorToIndex(
@@ -520,6 +514,7 @@ private static int MoveCursorToIndex(
520514

521515
return newCursorIndex;
522516
}
517+
523518
private static void CalculateCursorFromIndex(
524519
int promptStartCol,
525520
int promptStartRow,
@@ -529,7 +524,7 @@ private static void CalculateCursorFromIndex(
529524
out int cursorRow)
530525
{
531526
cursorCol = promptStartCol + inputIndex;
532-
cursorRow = promptStartRow + cursorCol / consoleWidth;
527+
cursorRow = promptStartRow + (cursorCol / consoleWidth);
533528
cursorCol %= consoleWidth;
534529
}
535530
}

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

+1-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ internal class PsrlReadLine : TerminalReadLine
1818

1919
private readonly EngineIntrinsics _engineIntrinsics;
2020

21-
#region Constructors
22-
2321
public PsrlReadLine(
2422
PSReadLineProxy psrlProxy,
2523
PsesInternalHost psesHost,
@@ -34,24 +32,14 @@ public PsrlReadLine(
3432
_psrlProxy.OverrideIdleHandler(onIdleAction);
3533
}
3634

37-
#endregion
38-
39-
#region Public Methods
40-
4135
public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken);
4236

43-
protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => ConsoleProxy.SafeReadKey(intercept: true, cancellationToken);
44-
45-
#endregion
46-
47-
#region Private Methods
37+
protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => _psesHost.ReadKey(intercept: true, cancellationToken);
4838

4939
private string InvokePSReadLine(CancellationToken cancellationToken)
5040
{
5141
EngineIntrinsics engineIntrinsics = _psesHost.IsRunspacePushed ? null : _engineIntrinsics;
5242
return _psrlProxy.ReadLine(_psesHost.Runspace, engineIntrinsics, cancellationToken, /* lastExecutionStatus */ null);
5343
}
54-
55-
#endregion
5644
}
5745
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
5-
using System.Management.Automation;
6-
using System.Security;
74
using System.Threading;
85

96
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
@@ -15,85 +12,5 @@ internal abstract class TerminalReadLine : IReadLine
1512
public abstract string ReadLine(CancellationToken cancellationToken);
1613

1714
protected abstract ConsoleKeyInfo ReadKey(CancellationToken cancellationToken);
18-
19-
public SecureString ReadSecureLine(CancellationToken cancellationToken)
20-
{
21-
Console.TreatControlCAsInput = true;
22-
int previousInputLength = 0;
23-
SecureString secureString = new();
24-
try
25-
{
26-
bool enterPressed = false;
27-
while (!enterPressed && !cancellationToken.IsCancellationRequested)
28-
{
29-
ConsoleKeyInfo keyInfo = ReadKey(cancellationToken);
30-
31-
if (keyInfo.IsCtrlC())
32-
{
33-
throw new PipelineStoppedException();
34-
}
35-
36-
switch (keyInfo.Key)
37-
{
38-
case ConsoleKey.Enter:
39-
// Stop the while loop so we can realign the cursor
40-
// and then return the entered string
41-
enterPressed = true;
42-
continue;
43-
44-
case ConsoleKey.Tab:
45-
break;
46-
47-
case ConsoleKey.Backspace:
48-
if (secureString.Length > 0)
49-
{
50-
secureString.RemoveAt(secureString.Length - 1);
51-
}
52-
break;
53-
54-
default:
55-
if (keyInfo.KeyChar != 0 && !char.IsControl(keyInfo.KeyChar))
56-
{
57-
secureString.AppendChar(keyInfo.KeyChar);
58-
}
59-
break;
60-
}
61-
62-
// Re-render the secure string characters
63-
int currentInputLength = secureString.Length;
64-
int consoleWidth = Console.WindowWidth;
65-
66-
if (currentInputLength > previousInputLength)
67-
{
68-
Console.Write('*');
69-
}
70-
else if (previousInputLength > 0 && currentInputLength < previousInputLength)
71-
{
72-
int row = Console.CursorTop;
73-
int col = Console.CursorLeft;
74-
75-
// Back up the cursor before clearing the character
76-
col--;
77-
if (col < 0)
78-
{
79-
col = consoleWidth - 1;
80-
row--;
81-
}
82-
83-
Console.SetCursorPosition(col, row);
84-
Console.Write(' ');
85-
Console.SetCursorPosition(col, row);
86-
}
87-
88-
previousInputLength = currentInputLength;
89-
}
90-
}
91-
finally
92-
{
93-
Console.TreatControlCAsInput = false;
94-
}
95-
96-
return secureString;
97-
}
9815
}
9916
}

0 commit comments

Comments
 (0)