Skip to content

Commit c140137

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
Add isOutputRedirected parameter to GetFormatStyleString() method (#14397)
1 parent 1dcc873 commit c140137

3 files changed

Lines changed: 5 additions & 16 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ public override void WriteDebugLine(string message)
12151215
{
12161216
if (SupportsVirtualTerminal)
12171217
{
1218-
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
1218+
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
12191219
}
12201220
else
12211221
{
@@ -1276,7 +1276,7 @@ public override void WriteVerboseLine(string message)
12761276
{
12771277
if (SupportsVirtualTerminal)
12781278
{
1279-
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Verbose) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
1279+
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Verbose, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
12801280
}
12811281
else
12821282
{
@@ -1320,7 +1320,7 @@ public override void WriteWarningLine(string message)
13201320
{
13211321
if (SupportsVirtualTerminal)
13221322
{
1323-
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
1323+
WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
13241324
}
13251325
else
13261326
{

src/System.Management.Automation/engine/Utils.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,13 +1617,12 @@ internal enum FormatStyle
16171617
Debug,
16181618
}
16191619

1620-
internal static string GetFormatStyleString(FormatStyle formatStyle)
1620+
internal static string GetFormatStyleString(FormatStyle formatStyle, bool isOutputRedirected)
16211621
{
16221622
// redirected console gets plaintext output to preserve existing behavior
16231623
if (!InternalTestHooks.BypassOutputRedirectionCheck &&
16241624
((PSStyle.Instance.OutputRendering == OutputRendering.PlainText) ||
1625-
(formatStyle == FormatStyle.Error && Console.IsErrorRedirected) ||
1626-
(formatStyle != FormatStyle.Error && Console.IsOutputRedirected)))
1625+
isOutputRedirected))
16271626
{
16281627
return string.Empty;
16291628
}

test/powershell/engine/Formatting/OutputRendering.Tests.ps1

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
# Licensed under the MIT License.
33

44
Describe 'OutputRendering tests' {
5-
BeforeAll {
6-
$th = New-TestHost
7-
$rs = [runspacefactory]::Createrunspace($th)
8-
$rs.open()
9-
$ps = [powershell]::Create()
10-
$ps.Runspace = $rs
11-
}
12-
135
BeforeEach {
146
if ($null -ne $PSStyle) {
157
$oldOutputRendering = $PSStyle.OutputRendering
@@ -20,8 +12,6 @@ Describe 'OutputRendering tests' {
2012
if ($null -ne $PSStyle) {
2113
$PSStyle.OutputRendering = $oldOutputRendering
2214
}
23-
24-
$ps.Commands.Clear()
2515
}
2616

2717
It 'OutputRendering works for "<outputRendering>" to the host' -TestCases @(

0 commit comments

Comments
 (0)