From c3b5935fbdc03ef183f5946d1e5e23c4eb8564d5 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 15 Apr 2022 11:48:12 -0700 Subject: [PATCH] Don't print extra prompt on cancellation of `ReadLine` --- .../Execution/SynchronousPowerShellTask.cs | 2 +- .../PowerShell/Host/PsesInternalHost.cs | 34 +++++++------------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs index 71accf212..52ea70efe 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs @@ -62,7 +62,7 @@ public override IReadOnlyList Run(CancellationToken cancellationToken) if (PowerShellExecutionOptions.WriteInputToHost) { - _psesHost.WriteWithPrompt(_psCommand, cancellationToken); + _psesHost.UI.WriteLine(_psCommand.GetInvocationText()); } return _pwsh.Runspace.Debugger.InBreakpoint diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index 8697f45ab..18bf753b6 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -753,17 +753,20 @@ 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. - if (string.IsNullOrEmpty(userInput)) + // 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) && LastKeyWasCtrlC()) + { + UI.WriteLine(); + return; + } + + if (cancellationToken.IsCancellationRequested) { - if (cancellationToken.IsCancellationRequested || LastKeyWasCtrlC()) - { - UI.WriteLine(); - } return; } @@ -825,19 +828,6 @@ private string GetPrompt(CancellationToken cancellationToken) return prompt; } - /// - /// 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. - /// - /// The PSCommand we'll print after the prompt. - /// - public void WriteWithPrompt(PSCommand command, CancellationToken cancellationToken) - { - UI.Write(GetPrompt(cancellationToken)); - UI.WriteLine(command.GetInvocationText()); - } - private string InvokeReadLine(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested();