Skip to content

Commit 6cc8277

Browse files
author
Andrew Menagarishvili
committed
Merged PR 18394: Change default for $PSStyle.OutputRendering to Ansi
Cherry picked from !18333
1 parent 95a9747 commit 6cc8277

File tree

9 files changed

+45
-32
lines changed

9 files changed

+45
-32
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ private void WriteImpl(string value, bool newLine)
729729
}
730730

731731
TextWriter writer = Console.IsOutputRedirected ? Console.Out : _parent.ConsoleTextWriter;
732-
value = GetOutputString(value, SupportsVirtualTerminal, Console.IsOutputRedirected);
732+
value = GetOutputString(value, SupportsVirtualTerminal);
733733

734734
if (_parent.IsRunningAsync)
735735
{
@@ -1215,7 +1215,7 @@ public override void WriteDebugLine(string message)
12151215
{
12161216
if (SupportsVirtualTerminal)
12171217
{
1218-
WriteLine(GetFormatStyleString(FormatStyle.Debug, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset);
1218+
WriteLine(GetFormatStyleString(FormatStyle.Debug) + 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(GetFormatStyleString(FormatStyle.Verbose, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset);
1279+
WriteLine(GetFormatStyleString(FormatStyle.Verbose) + 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(GetFormatStyleString(FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
1323+
WriteLine(GetFormatStyleString(FormatStyle.Warning) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset);
13241324
}
13251325
else
13261326
{

src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public FileInfoFormatting()
562562
/// <summary>
563563
/// Gets or sets the rendering mode for output.
564564
/// </summary>
565-
public OutputRendering OutputRendering { get; set; } = OutputRendering.Host;
565+
public OutputRendering OutputRendering { get; set; } = OutputRendering.Ansi;
566566

567567
/// <summary>
568568
/// Gets value to turn off all attributes.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,6 @@ public static class InternalTestHooks
15741574
internal static bool BypassAppLockerPolicyCaching;
15751575
internal static bool BypassOnlineHelpRetrieval;
15761576
internal static bool ForcePromptForChoiceDefaultOption;
1577-
internal static bool BypassOutputRedirectionCheck;
15781577
internal static bool NoPromptForPassword;
15791578
internal static bool ForceFormatListFixedLabelWidth;
15801579

src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,12 @@ public enum FormatStyle
304304
/// <param name="formatStyle">
305305
/// The format style to get the escape sequence for.
306306
/// </param>
307-
/// <param name="isOutputRedirected">
308-
/// True if the output is redirected.
309-
/// </param>
310307
/// <returns>
311308
/// The ANSI escape sequence for the given format style.
312309
/// </returns>
313-
public static string GetFormatStyleString(FormatStyle formatStyle, bool isOutputRedirected)
310+
public static string GetFormatStyleString(FormatStyle formatStyle)
314311
{
315-
// redirected console gets plaintext output to preserve existing behavior
316-
if (!InternalTestHooks.BypassOutputRedirectionCheck &&
317-
(PSStyle.Instance.OutputRendering == OutputRendering.PlainText ||
318-
isOutputRedirected))
312+
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
319313
{
320314
return string.Empty;
321315
}
@@ -353,30 +347,22 @@ public static string GetFormatStyleString(FormatStyle formatStyle, bool isOutput
353347
/// <param name="supportsVirtualTerminal">
354348
/// True if the host supports virtual terminal.
355349
/// </param>
356-
/// <param name="isOutputRedirected">
357-
/// True if the output is redirected.
358-
/// </param>
359350
/// <returns>
360351
/// The formatted text.
361352
/// </returns>
362-
public static string GetOutputString(string text, bool supportsVirtualTerminal, bool isOutputRedirected)
353+
public static string GetOutputString(string text, bool supportsVirtualTerminal)
363354
{
364-
return GetOutputString(text, isHost: true, supportsVirtualTerminal: supportsVirtualTerminal, isOutputRedirected: isOutputRedirected);
355+
return GetOutputString(text, isHost: true, supportsVirtualTerminal: supportsVirtualTerminal);
365356
}
366357

367-
internal static string GetOutputString(string text, bool isHost, bool? supportsVirtualTerminal = null, bool isOutputRedirected = false)
358+
internal static string GetOutputString(string text, bool isHost, bool? supportsVirtualTerminal = null)
368359
{
369360
var sd = new ValueStringDecorated(text);
370361

371362
if (sd.IsDecorated)
372363
{
373364
var outputRendering = OutputRendering.Ansi;
374-
if (InternalTestHooks.BypassOutputRedirectionCheck)
375-
{
376-
isOutputRedirected = false;
377-
}
378-
379-
if (isOutputRedirected || ShouldOutputPlainText(isHost, supportsVirtualTerminal))
365+
if (ShouldOutputPlainText(isHost, supportsVirtualTerminal))
380366
{
381367
outputRendering = OutputRendering.PlainText;
382368
}

test/powershell/Host/ConsoleHost.Tests.ps1

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export $envVarName='$guid'
414414

415415
It "text output" {
416416
# Join (multiple lines) and remove whitespace (we don't care about spacing) to verify we converted to string (by generating a table)
417-
-join (& $powershell -noprofile -outputFormat text { [PSCustomObject]@{X=10;Y=20} }) -replace "\s","" | Should -Be "XY--1020"
417+
-join (& $powershell -noprofile -outputFormat text { $PSStyle.OutputRendering = 'PlainText'; [PSCustomObject]@{X=10;Y=20} }) -replace "\s","" | Should -Be "XY--1020"
418418
}
419419

420420
It "errors are in text if error is redirected, encoded command, non-interactive, and outputformat specified" {
@@ -491,7 +491,15 @@ export $envVarName='$guid'
491491
}
492492

493493
Context "Redirected standard input for 'interactive' use" {
494-
$nl = [Environment]::Newline
494+
BeforeAll {
495+
$nl = [Environment]::Newline
496+
$oldColor = $env:NO_COLOR
497+
$env:NO_COLOR = 1
498+
}
499+
500+
AfterAll {
501+
$env:NO_COLOR = $oldColor
502+
}
495503

496504
# All of the following tests replace the prompt (either via an initial command or interactively)
497505
# so that we can read StandardOutput and reliably know exactly what the prompt is.
@@ -586,6 +594,8 @@ foo
586594
It "Redirected input w/ nested prompt" -Pending:($IsWindows) {
587595
$si = NewProcessStartInfo "-noprofile -noexit -c ""`$function:prompt = { 'PS' + ('>'*(`$NestedPromptLevel+1)) + ' ' }""" -RedirectStdIn
588596
$process = RunPowerShell $si
597+
$process.StandardInput.Write("`$PSStyle.OutputRendering='plaintext'`n")
598+
$null = $process.StandardOutput.ReadLine()
589599
$process.StandardInput.Write("`$Host.EnterNestedPrompt()`n")
590600
$process.StandardOutput.ReadLine() | Should -Be "PS> `$Host.EnterNestedPrompt()"
591601
$process.StandardInput.Write("exit`n")

test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ Exit-PSHostProcess
6868
Describe "Enter-PSHostProcess tests" -Tag Feature {
6969
Context "By Process Id" {
7070

71+
BeforeAll {
72+
$oldColor = $env:NO_COLOR
73+
$env:NO_COLOR = 1
74+
}
75+
76+
AfterAll {
77+
$env:NO_COLOR = $oldColor
78+
}
79+
7180
BeforeEach {
7281
# Start a normal job where the first thing it does is return $PID. After that, spin forever.
7382
# We will use this job as the target process for Enter-PSHostProcess
@@ -181,6 +190,15 @@ Describe "Enter-PSHostProcess tests" -Tag Feature {
181190

182191
Context "By CustomPipeName" {
183192

193+
BeforeAll {
194+
$oldColor = $env:NO_COLOR
195+
$env:NO_COLOR = 1
196+
}
197+
198+
AfterAll {
199+
$env:NO_COLOR = $oldColor
200+
}
201+
184202
It "Can enter, exit, and re-enter using CustomPipeName" {
185203
$pipeName = [System.IO.Path]::GetRandomFileName()
186204
$pipePath = Get-PipePath -PipeName $pipeName

test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Describe 'Get-Error tests' -Tag CI {
136136
try {
137137
$originalRendering = $PSStyle.OutputRendering
138138
$PSStyle.OutputRendering = 'Ansi'
139-
$out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("BypassOutputRedirectionCheck", $true); try { 1/0 } catch { }; Get-Error' | Out-String
139+
$out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; try { 1/0 } catch { }; Get-Error' | Out-String
140140

141141
# need to escape the open square bracket so the regex works
142142
$resetColor = $PSStyle.Reset.Replace('[','\[')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Describe 'OutputRendering tests' {
2121
) {
2222
param($outputRendering, $ansi)
2323

24-
$out = pwsh -noprofile -command "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('BypassOutputRedirectionCheck', `$true); `$PSStyle.OutputRendering = '$outputRendering'; write-host '$($PSStyle.Foreground.Green)hello'"
24+
$out = pwsh -noprofile -command "`$PSStyle.OutputRendering = '$outputRendering'; write-host '$($PSStyle.Foreground.Green)hello'"
2525

2626
if ($ansi) {
2727
$out | Should -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)
@@ -62,7 +62,7 @@ Describe 'OutputRendering tests' {
6262
$switch = "-$stream"
6363
}
6464

65-
$out = pwsh -noprofile -command "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('BypassOutputRedirectionCheck', `$true); write-$stream $switch 'hello';'bye'"
65+
$out = pwsh -noprofile -command "write-$stream $switch 'hello';'bye'"
6666
$out.Count | Should -Be 2
6767
$out[0] | Should -BeExactly "$($PSStyle.Formatting.$stream)$($stream.ToUpper()): hello$($PSStyle.Reset)" -Because ($out[0] | Out-String | Format-hex)
6868
$out[1] | Should -BeExactly "bye"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Describe 'Tests for $PSStyle automatic variable' {
8383

8484
It '$PSStyle has correct default for OutputRendering' {
8585
$PSStyle | Should -Not -BeNullOrEmpty
86-
$PSStyle.OutputRendering | Should -BeExactly 'Host'
86+
$PSStyle.OutputRendering | Should -BeExactly 'Ansi'
8787
}
8888

8989
It '$PSStyle has correct defaults for style <key>' -TestCases (Get-TestCases $styleDefaults) {

0 commit comments

Comments
 (0)