Skip to content

Replace _consoleHostUI with _underlyingHostUI #1746

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 1 commit into from
Mar 24, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Reflection;
using System.Security;
using System.Threading;
using Microsoft.Extensions.Logging;
Expand All @@ -21,8 +20,6 @@ internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface

private readonly PSHostUserInterface _underlyingHostUI;

private readonly PSHostUserInterface _consoleHostUI;

/// <summary>
/// We use a ConcurrentDictionary because ConcurrentHashSet does not exist, hence the value
/// is never actually used, and `WriteProgress` must be thread-safe.
Expand All @@ -37,58 +34,19 @@ public EditorServicesConsolePSHostUserInterface(
_readLineProvider = readLineProvider;
_underlyingHostUI = underlyingHostUI;
RawUI = new EditorServicesConsolePSHostRawUserInterface(loggerFactory, underlyingHostUI.RawUI);

_consoleHostUI = GetConsoleHostUI(_underlyingHostUI);

if (_consoleHostUI != null)
{
SetConsoleHostUIToInteractive(_consoleHostUI);
}
}

public override bool SupportsVirtualTerminal => _underlyingHostUI.SupportsVirtualTerminal;

public override PSHostRawUserInterface RawUI { get; }

public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
if (_consoleHostUI != null)
{
return _consoleHostUI.Prompt(caption, message, descriptions);
}
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions) => _underlyingHostUI.Prompt(caption, message, descriptions);

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

public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
if (_consoleHostUI != null)
{
return _consoleHostUI.PromptForChoice(caption, message, choices, defaultChoice);
}

return _underlyingHostUI.PromptForChoice(caption, message, choices, defaultChoice);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) => _underlyingHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
if (_consoleHostUI != null)
{
return _consoleHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
}

return _underlyingHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
}

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
if (_consoleHostUI is not null)
{
return _consoleHostUI.PromptForCredential(caption, message, userName, targetName);
}

return _underlyingHostUI.PromptForCredential(caption, message, userName, targetName);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) => _underlyingHostUI.PromptForCredential(caption, message, userName, targetName);

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

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

public override void WriteWarningLine(string message) => _underlyingHostUI.WriteWarningLine(message);

private static PSHostUserInterface GetConsoleHostUI(PSHostUserInterface ui)
{
FieldInfo externalUIField = ui.GetType().GetField("_externalUI", BindingFlags.NonPublic | BindingFlags.Instance);

if (externalUIField is null)
{
return null;
}

return (PSHostUserInterface)externalUIField.GetValue(ui);
}

private static void SetConsoleHostUIToInteractive(PSHostUserInterface ui) => ui.GetType().GetProperty("ThrowOnReadAndPrompt", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(ui, false);
}
}