diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs
index 87a65dfdc..ad20eadf3 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs
+++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs
@@ -729,7 +729,7 @@ namespace Microsoft.PowerShell
}
TextWriter writer = Console.IsOutputRedirected ? Console.Out : _parent.ConsoleTextWriter;
- value = Utils.GetOutputString(value, isHost: true, SupportsVirtualTerminal, Console.IsOutputRedirected);
+ value = GetOutputString(value, SupportsVirtualTerminal, Console.IsOutputRedirected);
if (_parent.IsRunningAsync)
{
@@ -1215,7 +1215,7 @@ namespace Microsoft.PowerShell
{
if (SupportsVirtualTerminal)
{
- WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
+ WriteLine(GetFormatStyleString(FormatStyle.Debug, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
}
else
{
@@ -1276,7 +1276,7 @@ namespace Microsoft.PowerShell
{
if (SupportsVirtualTerminal)
{
- WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Verbose, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
+ WriteLine(GetFormatStyleString(FormatStyle.Verbose, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
}
else
{
@@ -1320,7 +1320,7 @@ namespace Microsoft.PowerShell
{
if (SupportsVirtualTerminal)
{
- WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
+ WriteLine(GetFormatStyleString(FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
}
else
{
diff --git a/src/System.Management.Automation/FormatAndOutput/common/ILineOutput.cs b/src/System.Management.Automation/FormatAndOutput/common/ILineOutput.cs
index 32d4565da..8c672b620 100644
--- a/src/System.Management.Automation/FormatAndOutput/common/ILineOutput.cs
+++ b/src/System.Management.Automation/FormatAndOutput/common/ILineOutput.cs
@@ -4,6 +4,7 @@
using System.Globalization;
using System.IO;
using System.Management.Automation;
+using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Text;
@@ -413,7 +414,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
{
CheckStopProcessing();
- s = Utils.GetOutputString(s, isHost: false);
+ s = PSHostUserInterface.GetOutputString(s, isHost: false);
if (_suppressNewline)
{
diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs
index 91f608359..f277dcb1e 100644
--- a/src/System.Management.Automation/engine/Utils.cs
+++ b/src/System.Management.Automation/engine/Utils.cs
@@ -1559,99 +1559,6 @@ namespace System.Management.Automation
return oldMode;
}
-
- #region PSAnsiRendering
-
- internal static bool ShouldOutputPlainText(bool isHost, bool? supportsVirtualTerminal)
- {
- var outputRendering = OutputRendering.Ansi;
-
- if (supportsVirtualTerminal != false)
- {
- switch (PSStyle.Instance.OutputRendering)
- {
- case OutputRendering.Host:
- outputRendering = isHost ? OutputRendering.Ansi : OutputRendering.PlainText;
- break;
- default:
- outputRendering = PSStyle.Instance.OutputRendering;
- break;
- }
- }
-
- return outputRendering == OutputRendering.PlainText;
- }
-
- internal static string GetOutputString(string s, bool isHost, bool? supportsVirtualTerminal = null, bool isOutputRedirected = false)
- {
- var sd = new ValueStringDecorated(s);
-
- if (sd.IsDecorated)
- {
- var outputRendering = OutputRendering.Ansi;
- if (InternalTestHooks.BypassOutputRedirectionCheck)
- {
- isOutputRedirected = false;
- }
-
- if (isOutputRedirected || ShouldOutputPlainText(isHost, supportsVirtualTerminal))
- {
- outputRendering = OutputRendering.PlainText;
- }
-
- s = sd.ToString(outputRendering);
- }
-
- return s;
- }
-
- internal enum FormatStyle
- {
- Reset,
- FormatAccent,
- TableHeader,
- ErrorAccent,
- Error,
- Warning,
- Verbose,
- Debug,
- }
-
- internal static string GetFormatStyleString(FormatStyle formatStyle, bool isOutputRedirected)
- {
- // redirected console gets plaintext output to preserve existing behavior
- if (!InternalTestHooks.BypassOutputRedirectionCheck &&
- ((PSStyle.Instance.OutputRendering == OutputRendering.PlainText) ||
- isOutputRedirected))
- {
- return string.Empty;
- }
-
- PSStyle psstyle = PSStyle.Instance;
- switch (formatStyle)
- {
- case FormatStyle.Reset:
- return psstyle.Reset;
- case FormatStyle.FormatAccent:
- return psstyle.Formatting.FormatAccent;
- case FormatStyle.TableHeader:
- return psstyle.Formatting.TableHeader;
- case FormatStyle.ErrorAccent:
- return psstyle.Formatting.ErrorAccent;
- case FormatStyle.Error:
- return psstyle.Formatting.Error;
- case FormatStyle.Warning:
- return psstyle.Formatting.Warning;
- case FormatStyle.Verbose:
- return psstyle.Formatting.Verbose;
- case FormatStyle.Debug:
- return psstyle.Formatting.Debug;
- default:
- return string.Empty;
- }
- }
-
- #endregion
}
}
diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs
index 2d8c16f2b..cf2dea14a 100644
--- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs
+++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs
@@ -232,6 +232,161 @@ namespace System.Management.Automation.Host
///
public virtual void WriteInformation(InformationRecord record) { }
+ private static bool ShouldOutputPlainText(bool isHost, bool? supportsVirtualTerminal)
+ {
+ var outputRendering = OutputRendering.Ansi;
+
+ if (supportsVirtualTerminal != false)
+ {
+ switch (PSStyle.Instance.OutputRendering)
+ {
+ case OutputRendering.Host:
+ outputRendering = isHost ? OutputRendering.Ansi : OutputRendering.PlainText;
+ break;
+ default:
+ outputRendering = PSStyle.Instance.OutputRendering;
+ break;
+ }
+ }
+
+ return outputRendering == OutputRendering.PlainText;
+ }
+
+ ///
+ /// The format styles that are supported by the host.
+ ///
+ public enum FormatStyle
+ {
+ ///
+ /// Reset the formatting to the default.
+ ///
+ Reset,
+
+ ///
+ /// Highlight text used in output formatting.
+ ///
+ FormatAccent,
+
+ ///
+ /// Highlight for table headers.
+ ///
+ TableHeader,
+
+ ///
+ /// Highlight for detailed error view.
+ ///
+ ErrorAccent,
+
+ ///
+ /// Style for error messages.
+ ///
+ Error,
+
+ ///
+ /// Style for warning messages.
+ ///
+ Warning,
+
+ ///
+ /// Style for verbose messages.
+ ///
+ Verbose,
+
+ ///
+ /// Style for debug messages.
+ ///
+ Debug,
+ }
+
+ ///
+ /// Get the ANSI escape sequence for the given format style.
+ ///
+ ///
+ /// The format style to get the escape sequence for.
+ ///
+ ///
+ /// True if the output is redirected.
+ ///
+ ///
+ /// The ANSI escape sequence for the given format style.
+ ///
+ public static string GetFormatStyleString(FormatStyle formatStyle, bool isOutputRedirected)
+ {
+ // redirected console gets plaintext output to preserve existing behavior
+ if (!InternalTestHooks.BypassOutputRedirectionCheck &&
+ (PSStyle.Instance.OutputRendering == OutputRendering.PlainText ||
+ isOutputRedirected))
+ {
+ return string.Empty;
+ }
+
+ PSStyle psstyle = PSStyle.Instance;
+ switch (formatStyle)
+ {
+ case FormatStyle.Reset:
+ return psstyle.Reset;
+ case FormatStyle.FormatAccent:
+ return psstyle.Formatting.FormatAccent;
+ case FormatStyle.TableHeader:
+ return psstyle.Formatting.TableHeader;
+ case FormatStyle.ErrorAccent:
+ return psstyle.Formatting.ErrorAccent;
+ case FormatStyle.Error:
+ return psstyle.Formatting.Error;
+ case FormatStyle.Warning:
+ return psstyle.Formatting.Warning;
+ case FormatStyle.Verbose:
+ return psstyle.Formatting.Verbose;
+ case FormatStyle.Debug:
+ return psstyle.Formatting.Debug;
+ default:
+ return string.Empty;
+ }
+ }
+
+ ///
+ /// Get the appropriate output string based on different criteria.
+ ///
+ ///
+ /// The text to format.
+ ///
+ ///
+ /// True if the host supports virtual terminal.
+ ///
+ ///
+ /// True if the output is redirected.
+ ///
+ ///
+ /// The formatted text.
+ ///
+ public static string GetOutputString(string text, bool supportsVirtualTerminal, bool isOutputRedirected)
+ {
+ return GetOutputString(text, isHost: true, supportsVirtualTerminal: supportsVirtualTerminal, isOutputRedirected: isOutputRedirected);
+ }
+
+ internal static string GetOutputString(string text, bool isHost, bool? supportsVirtualTerminal = null, bool isOutputRedirected = false)
+ {
+ var sd = new ValueStringDecorated(text);
+
+ if (sd.IsDecorated)
+ {
+ var outputRendering = OutputRendering.Ansi;
+ if (InternalTestHooks.BypassOutputRedirectionCheck)
+ {
+ isOutputRedirected = false;
+ }
+
+ if (isOutputRedirected || ShouldOutputPlainText(isHost, supportsVirtualTerminal))
+ {
+ outputRendering = OutputRendering.PlainText;
+ }
+
+ text = sd.ToString(outputRendering);
+ }
+
+ return text;
+ }
+
// Gets the state associated with PowerShell transcription.
//
// Ideally, this would be associated with the host instance, but remoting recycles host instances