-
Notifications
You must be signed in to change notification settings - Fork 236
Re-implement legacy readline support #1584
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
rjmholt
merged 12 commits into
PowerShell:async-ps-consumer
from
rjmholt:pt-legacy-readline
Oct 15, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0f8dd05
Add initial ReadLine implementation
rjmholt ee874e4
Add common secure string read functionality
rjmholt 20b3ed3
Implement shared Ctrl-C test implementation
rjmholt 8fbfca7
Avoid unused parser call
rjmholt 7337696
Add idle support for legacy readline
rjmholt 2140120
Add comment about unimplemented line continuation support
rjmholt 9787b58
Hook up legacy readline support in the host
rjmholt 30f58b0
Remove stale TODOs
rjmholt 20bd69b
Remove commented out code
rjmholt b68c7ac
Remove unused usings
rjmholt 43bddb1
Remove unneeded readline interface methods
rjmholt ff77a68
Update comment
rjmholt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
631 changes: 0 additions & 631 deletions
631
src/PowerShellEditorServices/Services/PowerShell/Console/ConsoleReadLine.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
539 changes: 539 additions & 0 deletions
539
src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs
Large diffs are not rendered by default.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/PowerShellEditorServices/Services/PowerShell/Console/PsrlReadLine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution; | ||
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host; | ||
using System.Management.Automation; | ||
using System.Threading; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console | ||
{ | ||
using System; | ||
|
||
internal class PsrlReadLine : TerminalReadLine | ||
{ | ||
private readonly PSReadLineProxy _psrlProxy; | ||
|
||
private readonly PsesInternalHost _psesHost; | ||
|
||
private readonly EngineIntrinsics _engineIntrinsics; | ||
|
||
#region Constructors | ||
|
||
public PsrlReadLine( | ||
PSReadLineProxy psrlProxy, | ||
PsesInternalHost psesHost, | ||
EngineIntrinsics engineIntrinsics, | ||
Func<bool, ConsoleKeyInfo> readKeyFunc, | ||
Action<CancellationToken> onIdleAction) | ||
{ | ||
_psrlProxy = psrlProxy; | ||
_psesHost = psesHost; | ||
_engineIntrinsics = engineIntrinsics; | ||
_psrlProxy.OverrideReadKey(readKeyFunc); | ||
_psrlProxy.OverrideIdleHandler(onIdleAction); | ||
} | ||
|
||
#endregion | ||
|
||
#region Public Methods | ||
|
||
public override string ReadLine(CancellationToken cancellationToken) | ||
{ | ||
return _psesHost.InvokeDelegate<string>(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken); | ||
} | ||
|
||
protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) | ||
{ | ||
return ConsoleProxy.ReadKey(intercept: true, cancellationToken); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
|
||
private string InvokePSReadLine(CancellationToken cancellationToken) | ||
{ | ||
EngineIntrinsics engineIntrinsics = _psesHost.IsRunspacePushed ? null : _engineIntrinsics; | ||
return _psrlProxy.ReadLine(_psesHost.Runspace, engineIntrinsics, cancellationToken, /* lastExecutionStatus */ null); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/PowerShellEditorServices/Services/PowerShell/Console/TerminalReadLine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility; | ||
using System.Management.Automation; | ||
using System.Security; | ||
using System.Threading; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console | ||
{ | ||
using System; | ||
|
||
internal abstract class TerminalReadLine : IReadLine | ||
{ | ||
public abstract string ReadLine(CancellationToken cancellationToken); | ||
|
||
protected abstract ConsoleKeyInfo ReadKey(CancellationToken cancellationToken); | ||
|
||
public SecureString ReadSecureLine(CancellationToken cancellationToken) | ||
{ | ||
Console.TreatControlCAsInput = true; | ||
int previousInputLength = 0; | ||
SecureString secureString = new SecureString(); | ||
try | ||
{ | ||
bool enterPressed = false; | ||
while (!enterPressed && !cancellationToken.IsCancellationRequested) | ||
{ | ||
ConsoleKeyInfo keyInfo = ReadKey(cancellationToken); | ||
|
||
if (keyInfo.IsCtrlC()) | ||
{ | ||
throw new PipelineStoppedException(); | ||
} | ||
|
||
switch (keyInfo.Key) | ||
{ | ||
case ConsoleKey.Enter: | ||
// Stop the while loop so we can realign the cursor | ||
// and then return the entered string | ||
enterPressed = true; | ||
continue; | ||
|
||
case ConsoleKey.Tab: | ||
break; | ||
|
||
case ConsoleKey.Backspace: | ||
if (secureString.Length > 0) | ||
{ | ||
secureString.RemoveAt(secureString.Length - 1); | ||
} | ||
break; | ||
|
||
default: | ||
if (keyInfo.KeyChar != 0 && !char.IsControl(keyInfo.KeyChar)) | ||
{ | ||
secureString.AppendChar(keyInfo.KeyChar); | ||
} | ||
break; | ||
} | ||
|
||
// Re-render the secure string characters | ||
int currentInputLength = secureString.Length; | ||
int consoleWidth = Console.WindowWidth; | ||
|
||
if (currentInputLength > previousInputLength) | ||
{ | ||
Console.Write('*'); | ||
} | ||
else if (previousInputLength > 0 && currentInputLength < previousInputLength) | ||
{ | ||
int row = ConsoleProxy.GetCursorTop(cancellationToken); | ||
int col = ConsoleProxy.GetCursorLeft(cancellationToken); | ||
|
||
// Back up the cursor before clearing the character | ||
col--; | ||
if (col < 0) | ||
{ | ||
col = consoleWidth - 1; | ||
row--; | ||
} | ||
|
||
Console.SetCursorPosition(col, row); | ||
Console.Write(' '); | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Console.SetCursorPosition(col, row); | ||
} | ||
|
||
previousInputLength = currentInputLength; | ||
} | ||
} | ||
finally | ||
{ | ||
Console.TreatControlCAsInput = false; | ||
} | ||
|
||
return secureString; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/PowerShellEditorServices/Services/PowerShell/Utility/ConsoleKeyInfoExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility | ||
{ | ||
internal static class ConsoleKeyInfoExtensions | ||
{ | ||
public static bool IsCtrlC(this ConsoleKeyInfo keyInfo) | ||
{ | ||
if ((int)keyInfo.Key == 3) | ||
{ | ||
return true; | ||
} | ||
|
||
return keyInfo.Key == ConsoleKey.C | ||
&& (keyInfo.Modifiers & ConsoleModifiers.Control) != 0 | ||
&& (keyInfo.Modifiers & ConsoleModifiers.Shift) == 0 | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&& (keyInfo.Modifiers & ConsoleModifiers.Alt) == 0; | ||
SeeminglyScience marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.