Skip to content

Interrupt integrated console command prompt when commands are executed #412

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
merged 3 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ protected override void Initialize()

protected Task LaunchScript(RequestContext<object> requestContext)
{
// Ensure the read loop is stopped
this.editorSession.ConsoleService.CancelReadLoop();

// Is this an untitled script?
Task launchTask = null;

Expand Down Expand Up @@ -144,9 +141,6 @@ private async Task OnExecutionCompleted(Task executeTask)

if (this.isAttachSession)
{
// Ensure the read loop is stopped
this.editorSession.ConsoleService.CancelReadLoop();

// Pop the sessions
if (this.editorSession.PowerShellContext.CurrentRunspace.Context == RunspaceContext.EnteredProcess)
{
Expand All @@ -165,12 +159,6 @@ private async Task OnExecutionCompleted(Task executeTask)
Logger.WriteException("Caught exception while popping attached process after debugging", e);
}
}

}

if (!this.ownsEditorSession)
{
this.editorSession.ConsoleService.StartReadLoop();
}

if (this.disconnectRequestContext != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,9 +1108,6 @@ protected Task HandleEvaluateRequest(
DebugAdapterMessages.EvaluateRequestArguments evaluateParams,
RequestContext<DebugAdapterMessages.EvaluateResponseBody> requestContext)
{
// Cancel the read loop before executing
this.editorSession.ConsoleService.CancelReadLoop();

// We don't await the result of the execution here because we want
// to be able to receive further messages while the current script
// is executing. This important in cases where the pipeline thread
Expand Down
9 changes: 8 additions & 1 deletion src/PowerShellEditorServices/Console/ChoicePromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,15 @@ private async Task<int[]> StartPromptLoop(
break;
}

// If the handler returns null it means we should prompt again
choiceIndexes = this.HandleResponse(responseString);

// Return the default choice values if no choices were entered
if (choiceIndexes == null && string.IsNullOrEmpty(responseString))
{
choiceIndexes = this.DefaultChoices;
}

// If the user provided no choices, we should prompt again
if (choiceIndexes != null)
{
break;
Expand Down
72 changes: 42 additions & 30 deletions src/PowerShellEditorServices/Console/ConsoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public ConsoleService(
this.powerShellContext.ConsoleHost = this;
this.powerShellContext.DebuggerStop += PowerShellContext_DebuggerStop;
this.powerShellContext.DebuggerResumed += PowerShellContext_DebuggerResumed;
this.powerShellContext.ExecutionStatusChanged += PowerShellContext_ExecutionStatusChanged;

// Set the default prompt handler factory or create
// a default if one is not provided
Expand Down Expand Up @@ -140,31 +141,6 @@ public void CancelReadLoop()
}
}

/// <summary>
/// Called when a command string is received from the user.
/// If a prompt is currently active, the prompt handler is
/// asked to handle the string. Otherwise the string is
/// executed in the PowerShellContext.
/// </summary>
/// <param name="inputString">The input string to evaluate.</param>
/// <param name="echoToConsole">If true, the input will be echoed to the console.</param>
public void ExecuteCommand(string inputString, bool echoToConsole)
{
this.CancelReadLoop();

if (this.activePromptHandler == null)
{
// Execute the script string but don't wait for completion
var executeTask =
this.powerShellContext
.ExecuteScriptString(
inputString,
echoToConsole,
true)
.ConfigureAwait(false);
}
}

/// <summary>
/// Executes a script file at the specified path.
/// </summary>
Expand Down Expand Up @@ -338,11 +314,16 @@ await this.consoleReadLine.ReadCommandLine(
{
Console.Write(Environment.NewLine);

await this.powerShellContext.ExecuteScriptString(
commandString,
false,
true,
true);
var unusedTask =
this.powerShellContext
.ExecuteScriptString(
commandString,
false,
true,
true)
.ConfigureAwait(false);

break;
}
}
while (!cancellationToken.IsCancellationRequested);
Expand Down Expand Up @@ -459,6 +440,37 @@ private void PowerShellContext_DebuggerResumed(object sender, System.Management.
this.CancelReadLoop();
}

private void PowerShellContext_ExecutionStatusChanged(object sender, ExecutionStatusChangedEventArgs eventArgs)
{
if (this.EnableConsoleRepl)
{
// Any command which writes output to the host will affect
// the display of the prompt
if (eventArgs.ExecutionOptions.WriteOutputToHost ||
eventArgs.ExecutionOptions.InterruptCommandPrompt)
{
if (eventArgs.ExecutionStatus != ExecutionStatus.Running)
{
// Execution has completed, start the input prompt
this.StartReadLoop();
}
else
{
// A new command was started, cancel the input prompt
this.CancelReadLoop();
}
}
else if (
eventArgs.ExecutionOptions.WriteErrorsToHost &&
(eventArgs.ExecutionStatus == ExecutionStatus.Failed ||
eventArgs.HadErrors))
{
this.CancelReadLoop();
this.StartReadLoop();
}
}
}

#endregion
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/PowerShellEditorServices/Session/ExecutionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// Defines options for the execution of a command.
/// </summary>
public class ExecutionOptions
{
#region Properties

/// <summary>
/// Gets or sets a boolean that determines whether command output
/// should be written to the host.
/// </summary>
public bool WriteOutputToHost { get; set; }

/// <summary>
/// Gets or sets a boolean that determines whether command errors
/// should be written to the host.
/// </summary>
public bool WriteErrorsToHost { get; set; }

/// <summary>
/// Gets or sets a boolean that determines whether the executed
/// command should be added to the command history.
/// </summary>
public bool AddToHistory { get; set; }

/// <summary>
/// Gets or sets a boolean that determines whether the execution
/// of the command should interrupt the command prompt. Should
/// only be set if WriteOutputToHost is false but the command
/// should still interrupt the command prompt.
/// </summary>
public bool InterruptCommandPrompt { get; set; }

#endregion

#region Constructors

/// <summary>
/// Creates an instance of the ExecutionOptions class with
/// default settings configured.
/// </summary>
public ExecutionOptions()
{
this.WriteOutputToHost = true;
this.WriteErrorsToHost = true;
this.AddToHistory = false;
this.InterruptCommandPrompt = false;
}

#endregion
}
}
39 changes: 39 additions & 0 deletions src/PowerShellEditorServices/Session/ExecutionStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// Enumerates the possible execution results that can occur after
/// executing a command or script.
/// </summary>
public enum ExecutionStatus
{
/// <summary>
/// Indicates that execution has not yet started.
/// </summary>
Pending,

/// <summary>
/// Indicates that the command is executing.
/// </summary>
Running,

/// <summary>
/// Indicates that execution has failed.
/// </summary>
Failed,

/// <summary>
/// Indicates that execution was aborted by the user.
/// </summary>
Aborted,

/// <summary>
/// Indicates that execution completed successfully.
/// </summary>
Completed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// Contains details about an executed
/// </summary>
public class ExecutionStatusChangedEventArgs
{
#region Properties

/// <summary>
/// Gets the options used when the command was executed.
/// </summary>
public ExecutionOptions ExecutionOptions { get; private set; }

/// <summary>
/// Gets the command execution's current status.
/// </summary>
public ExecutionStatus ExecutionStatus { get; private set; }

/// <summary>
/// If true, the command execution had errors.
/// </summary>
public bool HadErrors { get; private set; }

#endregion

#region Constructors

/// <summary>
/// Creates an instance of the ExecutionStatusChangedEventArgs class.
/// </summary>
/// <param name="executionStatus">The command execution's current status.</param>
/// <param name="executionOptions">The options used when the command was executed.</param>
/// <param name="hadErrors">If execution has completed, indicates whether there were errors.</param>
public ExecutionStatusChangedEventArgs(
ExecutionStatus executionStatus,
ExecutionOptions executionOptions,
bool hadErrors)
{
this.ExecutionStatus = executionStatus;
this.ExecutionOptions = executionOptions;
this.HadErrors = hadErrors || (executionStatus == ExecutionStatus.Failed);
}

#endregion
}
}
Loading