Skip to content

Commit

Permalink
Remove WriteWithPrompt workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Apr 27, 2022
1 parent 87b3c9c commit e968d8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)

if (PowerShellExecutionOptions.WriteInputToHost)
{
_psesHost.WriteWithPrompt(_psCommand, cancellationToken);
_psesHost.UI.WriteLine(_psCommand.GetInvocationText());
}

return _pwsh.Runspace.Debugger.InBreakpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,18 +757,22 @@ private void DoOneRepl(CancellationToken cancellationToken)

// If the user input was empty it's because:
// - the user provided no input
// - the readline task was canceled
// - CtrlC was sent to readline (which does not propagate a cancellation)
// - the ReadLine task was canceled
// - CtrlC was sent to ReadLine (which does not propagate a cancellation)
//
// In any event there's nothing to run in PowerShell, so we just loop back to the prompt again.
// However, we must distinguish the last two scenarios, since PSRL will not print a new line in those cases.
// In any event there's nothing to run in PowerShell, so we just loop back to the
// prompt again. However, PSReadLine will not print a newline for CtrlC, so we print
// one, but we do not want to print one if the ReadLine task was canceled.
if (string.IsNullOrEmpty(userInput))
{
if (cancellationToken.IsCancellationRequested || LastKeyWasCtrlC())
if (LastKeyWasCtrlC())
{
UI.WriteLine();
}
return;
// Propogate cancellation if that's what happened, since ReadLine won't.
// TODO: We may not need to do this at all.
cancellationToken.ThrowIfCancellationRequested();
return; // Task wasn't canceled but there was no input.
}

InvokeInput(userInput, cancellationToken);
Expand All @@ -782,10 +786,8 @@ private void DoOneRepl(CancellationToken cancellationToken)
{
throw;
}
catch (FlowControlException)
{
// Do nothing, a break or continue statement was used outside of a loop.
}
// Do nothing, a break or continue statement was used outside of a loop.
catch (FlowControlException) { }
catch (Exception e)
{
UI.WriteErrorLine($"An error occurred while running the REPL loop:{Environment.NewLine}{e}");
Expand Down Expand Up @@ -829,25 +831,15 @@ private string GetPrompt(CancellationToken cancellationToken)
return prompt;
}

/// <summary>
/// This is used to write the invocation text of a command with the user's prompt so that,
/// for example, F8 (evaluate selection) appears as if the user typed it. Used when
/// 'WriteInputToHost' is true.
/// </summary>
/// <param name="command">The PSCommand we'll print after the prompt.</param>
/// <param name="cancellationToken"></param>
public void WriteWithPrompt(PSCommand command, CancellationToken cancellationToken)
{
UI.Write(GetPrompt(cancellationToken));
UI.WriteLine(command.GetInvocationText());
}

private string InvokeReadLine(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
// TODO: If we can pass the cancellation token to ReadKey directly in PSReadLine, we
// can remove this logic.
_readKeyCancellationToken = cancellationToken;
cancellationToken.ThrowIfCancellationRequested();
return _readLineProvider.ReadLine.ReadLine(cancellationToken);
}
finally
Expand Down

0 comments on commit e968d8b

Please sign in to comment.