Skip to content

Commit d1fba04

Browse files
committed
Write custom full prompt each time it is displayed
This change fixes an issue where the user's custom prompt function is only written out fully (including any Write-Host or Write-Output behavior) the first time the prompt appears after a command is executed. This is because the return value of the prompt function was being cached and re-printed each time the user hit Enter without running a new command. Resolves PowerShell/vscode-powershell#774.
1 parent beea563 commit d1fba04

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/PowerShellEditorServices/Console/ConsoleService.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Microsoft.PowerShell.EditorServices.Console
1313
using Microsoft.PowerShell.EditorServices.Session;
1414
using System;
1515
using System.Globalization;
16+
using System.Linq;
1617
using System.Management.Automation;
1718
using System.Security;
1819

@@ -218,9 +219,24 @@ public async Task<SecureString> ReadSecureLine(CancellationToken cancellationTok
218219

219220
#region Private Methods
220221

221-
private void WritePromptStringToHost()
222+
private async Task WritePromptStringToHost()
222223
{
223-
string promptString = this.powerShellContext.PromptString;
224+
PSCommand promptCommand = new PSCommand().AddScript("prompt");
225+
226+
string promptString =
227+
(await this.powerShellContext.ExecuteCommand<object>(promptCommand, false, false))
228+
.OfType<string>()
229+
.FirstOrDefault() ?? "PS> ";
230+
231+
// Add the [DBG] prefix if we're stopped in the debugger
232+
if (this.powerShellContext.IsDebuggerStopped)
233+
{
234+
promptString =
235+
string.Format(
236+
CultureInfo.InvariantCulture,
237+
"[DBG]: {0}",
238+
promptString);
239+
}
224240

225241
// Update the stored prompt string if the session is remote
226242
if (this.powerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote)
@@ -295,7 +311,7 @@ private async Task StartReplLoop(CancellationToken cancellationToken)
295311
{
296312
string commandString = null;
297313

298-
this.WritePromptStringToHost();
314+
await this.WritePromptStringToHost();
299315

300316
try
301317
{

src/PowerShellEditorServices/Session/PowerShellContext.cs

-8
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ public RunspaceDetails CurrentRunspace
103103
private set;
104104
}
105105

106-
/// <summary>
107-
/// Gets the prompt string for the current runspace.
108-
/// </summary>
109-
public string PromptString
110-
{
111-
get { return this.mostRecentSessionDetails.PromptString; }
112-
}
113-
114106
#endregion
115107

116108
#region Constructors

src/PowerShellEditorServices/Session/SessionDetails.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ namespace Microsoft.PowerShell.EditorServices.Session
1515
/// </summary>
1616
public class SessionDetails
1717
{
18-
/// <summary>
19-
/// Gets the current prompt string.
20-
/// </summary>
21-
public string PromptString { get; internal set; }
22-
2318
/// <summary>
2419
/// Gets the process ID of the current process.
2520
/// </summary>
@@ -47,7 +42,6 @@ public SessionDetails(PSObject detailsObject)
4742

4843
Hashtable innerHashtable = detailsObject.BaseObject as Hashtable;
4944

50-
this.PromptString = innerHashtable["prompt"] as string ?? "PS> ";
5145
this.ProcessId = (int)innerHashtable["processId"] as int?;
5246
this.ComputerName = innerHashtable["computerName"] as string;
5347
this.InstanceId = innerHashtable["instanceId"] as Guid?;
@@ -62,7 +56,7 @@ public static PSCommand GetDetailsCommand()
6256
{
6357
PSCommand infoCommand = new PSCommand();
6458
infoCommand.AddScript(
65-
"@{ 'prompt' = prompt; 'computerName' = $env:ComputerName; 'processId' = $PID; 'instanceId' = $host.InstanceId }");
59+
"@{ 'computerName' = $env:ComputerName; 'processId' = $PID; 'instanceId' = $host.InstanceId }");
6660

6761
return infoCommand;
6862
}

0 commit comments

Comments
 (0)