Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ private void WriteImpl(string value, bool newLine)
}

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)
{
Expand Down Expand Up @@ -1215,7 +1215,7 @@ public override void WriteDebugLine(string message)
{
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
{
Expand Down Expand Up @@ -1276,7 +1276,7 @@ public override void WriteVerboseLine(string message)
{
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
{
Expand Down Expand Up @@ -1320,7 +1320,7 @@ public override void WriteWarningLine(string message)
{
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -413,7 +414,7 @@ internal override void WriteLine(string s)
{
CheckStopProcessing();

s = Utils.GetOutputString(s, isHost: false);
s = PSHostUserInterface.GetOutputString(s, isHost: false);

if (_suppressNewline)
{
Expand Down
93 changes: 0 additions & 93 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1559,99 +1559,6 @@ internal static bool IsComObject(object obj)

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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,161 @@ public virtual void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgro
/// </summary>
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;
}

/// <summary>
/// The format styles that are supported by the host.
/// </summary>
public enum FormatStyle
{
/// <summary>
/// Reset the formatting to the default.
/// </summary>
Reset,

/// <summary>
/// Highlight text used in output formatting.
/// </summary>
FormatAccent,

/// <summary>
/// Highlight for table headers.
/// </summary>
TableHeader,

/// <summary>
/// Highlight for detailed error view.
/// </summary>
ErrorAccent,

/// <summary>
/// Style for error messages.
/// </summary>
Error,

/// <summary>
/// Style for warning messages.
/// </summary>
Warning,

/// <summary>
/// Style for verbose messages.
/// </summary>
Verbose,

/// <summary>
/// Style for debug messages.
/// </summary>
Debug,
}

/// <summary>
/// Get the ANSI escape sequence for the given format style.
/// </summary>
/// <param name="formatStyle">
/// The format style to get the escape sequence for.
/// </param>
/// <param name="isOutputRedirected">
/// True if the output is redirected.
/// </param>
/// <returns>
/// The ANSI escape sequence for the given format style.
/// </returns>
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;
}
}

/// <summary>
/// Get the appropriate output string based on different criteria.
/// </summary>
/// <param name="text">
/// The text to format.
/// </param>
/// <param name="supportsVirtualTerminal">
/// True if the host supports virtual terminal.
/// </param>
/// <param name="isOutputRedirected">
/// True if the output is redirected.
/// </param>
/// <returns>
/// The formatted text.
/// </returns>
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
Expand Down