Skip to content

Commit cae6b99

Browse files
Replace _consoleHostUI with _underlyingHostUI (#1746)
And stop using reflection to find `_externalUI`. This is not necessary in order to reuse PowerShell's built-in prompt logic (which was its original goal). Unfortunately this doesn't fix the issues it was written in an attempt to fix, but it does remove unnecessary reflection logic.
1 parent 7cb4761 commit cae6b99

File tree

1 file changed

+4
-60
lines changed

1 file changed

+4
-60
lines changed

src/PowerShellEditorServices/Services/PowerShell/Host/EditorServicesConsolePSHostUserInterface.cs

+4-60
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Collections.ObjectModel;
88
using System.Management.Automation;
99
using System.Management.Automation.Host;
10-
using System.Reflection;
1110
using System.Security;
1211
using System.Threading;
1312
using Microsoft.Extensions.Logging;
@@ -21,8 +20,6 @@ internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface
2120

2221
private readonly PSHostUserInterface _underlyingHostUI;
2322

24-
private readonly PSHostUserInterface _consoleHostUI;
25-
2623
/// <summary>
2724
/// We use a ConcurrentDictionary because ConcurrentHashSet does not exist, hence the value
2825
/// is never actually used, and `WriteProgress` must be thread-safe.
@@ -37,58 +34,19 @@ public EditorServicesConsolePSHostUserInterface(
3734
_readLineProvider = readLineProvider;
3835
_underlyingHostUI = underlyingHostUI;
3936
RawUI = new EditorServicesConsolePSHostRawUserInterface(loggerFactory, underlyingHostUI.RawUI);
40-
41-
_consoleHostUI = GetConsoleHostUI(_underlyingHostUI);
42-
43-
if (_consoleHostUI != null)
44-
{
45-
SetConsoleHostUIToInteractive(_consoleHostUI);
46-
}
4737
}
4838

4939
public override bool SupportsVirtualTerminal => _underlyingHostUI.SupportsVirtualTerminal;
5040

5141
public override PSHostRawUserInterface RawUI { get; }
5242

53-
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
54-
{
55-
if (_consoleHostUI != null)
56-
{
57-
return _consoleHostUI.Prompt(caption, message, descriptions);
58-
}
43+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions) => _underlyingHostUI.Prompt(caption, message, descriptions);
5944

60-
return _underlyingHostUI.Prompt(caption, message, descriptions);
61-
}
45+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice) => _underlyingHostUI.PromptForChoice(caption, message, choices, defaultChoice);
6246

63-
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
64-
{
65-
if (_consoleHostUI != null)
66-
{
67-
return _consoleHostUI.PromptForChoice(caption, message, choices, defaultChoice);
68-
}
69-
70-
return _underlyingHostUI.PromptForChoice(caption, message, choices, defaultChoice);
71-
}
47+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) => _underlyingHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
7248

73-
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
74-
{
75-
if (_consoleHostUI != null)
76-
{
77-
return _consoleHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
78-
}
79-
80-
return _underlyingHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
81-
}
82-
83-
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
84-
{
85-
if (_consoleHostUI is not null)
86-
{
87-
return _consoleHostUI.PromptForCredential(caption, message, userName, targetName);
88-
}
89-
90-
return _underlyingHostUI.PromptForCredential(caption, message, userName, targetName);
91-
}
49+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) => _underlyingHostUI.PromptForCredential(caption, message, userName, targetName);
9250

9351
public override string ReadLine() => _readLineProvider.ReadLine.ReadLine(CancellationToken.None);
9452

@@ -138,19 +96,5 @@ public void ResetProgress()
13896
public override void WriteVerboseLine(string message) => _underlyingHostUI.WriteVerboseLine(message);
13997

14098
public override void WriteWarningLine(string message) => _underlyingHostUI.WriteWarningLine(message);
141-
142-
private static PSHostUserInterface GetConsoleHostUI(PSHostUserInterface ui)
143-
{
144-
FieldInfo externalUIField = ui.GetType().GetField("_externalUI", BindingFlags.NonPublic | BindingFlags.Instance);
145-
146-
if (externalUIField is null)
147-
{
148-
return null;
149-
}
150-
151-
return (PSHostUserInterface)externalUIField.GetValue(ui);
152-
}
153-
154-
private static void SetConsoleHostUIToInteractive(PSHostUserInterface ui) => ui.GetType().GetProperty("ThrowOnReadAndPrompt", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(ui, false);
15599
}
156100
}

0 commit comments

Comments
 (0)