Move GetOuputString()
and GetFormatStyleString()
to PSHostUserInterface
as public API (#16075)
This commit is contained in:
parent
72c8c9b24a
commit
171c7ffdc8
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -232,6 +232,161 @@ namespace System.Management.Automation.Host
|
|||
/// </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
|
||||
|
|
Loading…
Reference in a new issue