Add isOutputRedirected parameter to GetFormatStyleString() method (#14397)

This commit is contained in:
Steve Lee
2021-09-09 11:50:36 -07:00
committed by GitHub
parent 0d7ba2fe5a
commit 024f409763
3 changed files with 5 additions and 16 deletions
@@ -1215,7 +1215,7 @@ namespace Microsoft.PowerShell
{
if (SupportsVirtualTerminal)
{
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
WriteLine(Utils.GetFormatStyleString(Utils.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) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
WriteLine(Utils.GetFormatStyleString(Utils.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) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
}
else
{
@@ -1617,13 +1617,12 @@ namespace System.Management.Automation
Debug,
}
internal static string GetFormatStyleString(FormatStyle formatStyle)
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) ||
(formatStyle == FormatStyle.Error && Console.IsErrorRedirected) ||
(formatStyle != FormatStyle.Error && Console.IsOutputRedirected)))
isOutputRedirected))
{
return string.Empty;
}
@@ -2,14 +2,6 @@
# Licensed under the MIT License.
Describe 'OutputRendering tests' {
BeforeAll {
$th = New-TestHost
$rs = [runspacefactory]::Createrunspace($th)
$rs.open()
$ps = [powershell]::Create()
$ps.Runspace = $rs
}
BeforeEach {
if ($null -ne $PSStyle) {
$oldOutputRendering = $PSStyle.OutputRendering
@@ -20,8 +12,6 @@ Describe 'OutputRendering tests' {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $oldOutputRendering
}
$ps.Commands.Clear()
}
It 'OutputRendering works for "<outputRendering>" to the host' -TestCases @(