Skip to content

Commit a6d48ba

Browse files
SteveL-MSFTxtqqczze
authored andcommitted
Change default to OutputRendering.Host and remove OutputRendering.Automatic (PowerShell#15882)
1 parent 6b88ed8 commit a6d48ba

11 files changed

Lines changed: 122 additions & 46 deletions

File tree

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ namespace System.Management.Automation
1313
/// </summary>
1414
public enum OutputRendering
1515
{
16-
/// <summary>Automatic by PowerShell.</summary>
17-
Automatic = 0,
16+
/// <summary>Render ANSI only to host.</summary>
17+
Host = 0,
1818

1919
/// <summary>Render as plaintext.</summary>
2020
PlainText = 1,
2121

2222
/// <summary>Render as ANSI.</summary>
2323
Ansi = 2,
24-
25-
/// <summary>Render ANSI only to host.</summary>
26-
Host = 3,
2724
}
2825
#endregion OutputRendering
2926

@@ -565,7 +562,7 @@ public FileInfoFormatting()
565562
/// <summary>
566563
/// Gets or sets the rendering mode for output.
567564
/// </summary>
568-
public OutputRendering OutputRendering { get; set; } = OutputRendering.Automatic;
565+
public OutputRendering OutputRendering { get; set; } = OutputRendering.Host;
569566

570567
/// <summary>
571568
/// Gets value to turn off all attributes.

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public StringDecorated(string text)
5555
/// Render the decorarted string using automatic output rendering.
5656
/// </summary>
5757
/// <returns>Rendered string based on automatic output rendering.</returns>
58-
public override string ToString() => _isDecorated ? ToString(OutputRendering.Automatic) : _text;
58+
public override string ToString() => ToString(
59+
PSStyle.Instance.OutputRendering == OutputRendering.PlainText
60+
? OutputRendering.PlainText
61+
: OutputRendering.Ansi);
5962

6063
/// <summary>
6164
/// Return string representation of content depending on output rendering mode.
@@ -64,28 +67,17 @@ public StringDecorated(string text)
6467
/// <returns>Rendered string based on outputRendering.</returns>
6568
public string ToString(OutputRendering outputRendering)
6669
{
67-
if (!_isDecorated)
70+
if (outputRendering == OutputRendering.Host)
6871
{
69-
return _text;
72+
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
7073
}
7174

72-
if (outputRendering == OutputRendering.Automatic)
73-
{
74-
outputRendering = OutputRendering.Ansi;
75-
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
76-
{
77-
outputRendering = OutputRendering.PlainText;
78-
}
79-
}
80-
81-
if (outputRendering == OutputRendering.PlainText)
82-
{
83-
return PlainText;
84-
}
85-
else
75+
if (!_isDecorated)
8676
{
8777
return _text;
8878
}
79+
80+
return outputRendering == OutputRendering.PlainText ? PlainText : _text;
8981
}
9082
}
9183

@@ -139,7 +131,10 @@ public ValueStringDecorated(string text)
139131
/// Render the decorarted string using automatic output rendering.
140132
/// </summary>
141133
/// <returns>Rendered string based on automatic output rendering.</returns>
142-
public override string ToString() => _isDecorated ? ToString(OutputRendering.Automatic) : _text;
134+
public override string ToString() => ToString(
135+
PSStyle.Instance.OutputRendering == OutputRendering.PlainText
136+
? OutputRendering.PlainText
137+
: OutputRendering.Ansi);
143138

144139
/// <summary>
145140
/// Return string representation of content depending on output rendering mode.
@@ -148,28 +143,17 @@ public ValueStringDecorated(string text)
148143
/// <returns>Rendered string based on outputRendering.</returns>
149144
public string ToString(OutputRendering outputRendering)
150145
{
151-
if (!_isDecorated)
146+
if (outputRendering == OutputRendering.Host)
152147
{
153-
return _text;
148+
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
154149
}
155150

156-
if (outputRendering == OutputRendering.Automatic)
157-
{
158-
outputRendering = OutputRendering.Ansi;
159-
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
160-
{
161-
outputRendering = OutputRendering.PlainText;
162-
}
163-
}
164-
165-
if (outputRendering == OutputRendering.PlainText)
166-
{
167-
return PlainText;
168-
}
169-
else
151+
if (!_isDecorated)
170152
{
171153
return _text;
172154
}
155+
156+
return outputRendering == OutputRendering.PlainText ? PlainText : _text;
173157
}
174158
}
175159
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,9 +1570,6 @@ internal static bool ShouldOutputPlainText(bool isHost, bool? supportsVirtualTer
15701570
{
15711571
switch (PSStyle.Instance.OutputRendering)
15721572
{
1573-
case OutputRendering.Automatic:
1574-
outputRendering = OutputRendering.Ansi;
1575-
break;
15761573
case OutputRendering.Host:
15771574
outputRendering = isHost ? OutputRendering.Ansi : OutputRendering.PlainText;
15781575
break;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<xsd:schema id="root" xmlns="" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
4+
<xsd:import namespace="https://www.w3.org/XML/1998/namespace" />
5+
<xsd:element name="root" msdata:IsDataSet="true">
6+
<xsd:complexType>
7+
<xsd:choice maxOccurs="unbounded">
8+
<xsd:element name="metadata">
9+
<xsd:complexType>
10+
<xsd:sequence>
11+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
12+
</xsd:sequence>
13+
<xsd:attribute name="name" use="required" type="xsd:string" />
14+
<xsd:attribute name="type" type="xsd:string" />
15+
<xsd:attribute name="mimetype" type="xsd:string" />
16+
<xsd:attribute ref="xml:space" />
17+
</xsd:complexType>
18+
</xsd:element>
19+
<xsd:element name="assembly">
20+
<xsd:complexType>
21+
<xsd:attribute name="alias" type="xsd:string" />
22+
<xsd:attribute name="name" type="xsd:string" />
23+
</xsd:complexType>
24+
</xsd:element>
25+
<xsd:element name="data">
26+
<xsd:complexType>
27+
<xsd:sequence>
28+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
29+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
30+
</xsd:sequence>
31+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
32+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
33+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
34+
<xsd:attribute ref="xml:space" />
35+
</xsd:complexType>
36+
</xsd:element>
37+
<xsd:element name="resheader">
38+
<xsd:complexType>
39+
<xsd:sequence>
40+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
41+
</xsd:sequence>
42+
<xsd:attribute name="name" type="xsd:string" use="required" />
43+
</xsd:complexType>
44+
</xsd:element>
45+
</xsd:choice>
46+
</xsd:complexType>
47+
</xsd:element>
48+
</xsd:schema>
49+
<resheader name="resmimetype">
50+
<value>text/microsoft-resx</value>
51+
</resheader>
52+
<resheader name="version">
53+
<value>2.0</value>
54+
</resheader>
55+
<resheader name="reader">
56+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
57+
</resheader>
58+
<resheader name="writer">
59+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
60+
</resheader>
61+
<data name="RequireExplicitRendering" xml:space="preserve">
62+
<value>Only 'ANSI' or 'PlainText' is supported for this method.</value>
63+
</data>
64+
</root>

test/powershell/Language/Scripting/SuppressAnsiEscapeSequence.Tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
Describe '$env:__SuppressAnsiEscapeSequences tests' -Tag CI {
55
BeforeAll {
66
$originalSuppressPref = $env:__SuppressAnsiEscapeSequences
7+
$originalRendering = $PSStyle.OutputRendering
8+
$PSStyle.OutputRendering = 'Ansi'
79
}
810

911
AfterAll {
1012
$env:__SuppressAnsiEscapeSequences = $originalSuppressPref
13+
$PSStyle.OutputRendering = $originalRendering
1114
}
1215

1316

test/powershell/Modules/Microsoft.PowerShell.Utility/Format-List.Tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,13 @@ dbda : KM
213213

214214
Describe 'Format-List color tests' {
215215
BeforeAll {
216+
$originalRendering = $PSStyle.OutputRendering
217+
$PSStyle.OutputRendering = 'Ansi'
216218
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $true)
217219
}
218220

219221
AfterAll {
222+
$PSStyle.OutputRendering = $originalRendering
220223
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $false)
221224
}
222225

test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,15 @@ A Name B
848848
}
849849

850850
Describe 'Table color tests' {
851+
BeforeAll {
852+
$originalRendering = $PSStyle.OutputRendering
853+
$PSStyle.OutputRendering = 'Ansi'
854+
}
855+
856+
AfterAll {
857+
$PSStyle.OutputRendering = $originalRendering
858+
}
859+
851860
It 'Table header should use FormatAccent' {
852861
([pscustomobject]@{foo = 1} | Format-Table | Out-String).Trim() | Should -BeExactly @"
853862
$($PSStyle.Formatting.FormatAccent)foo$($PSStyle.Reset)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Describe 'Get-Error tests' -Tag CI {
134134
}
135135

136136
try {
137+
$originalRendering = $PSStyle.OutputRendering
138+
$PSStyle.OutputRendering = 'Ansi'
137139
$out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("BypassOutputRedirectionCheck", $true); try { 1/0 } catch { }; Get-Error' | Out-String
138140

139141
# need to escape the open square bracket so the regex works
@@ -145,6 +147,7 @@ Describe 'Get-Error tests' -Tag CI {
145147
}
146148
finally
147149
{
150+
$PSStyle.OutputRendering = $originalRendering
148151
if ($suppressVT) {
149152
$env:__SuppressAnsiEscapeSequences = 1
150153
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ Describe "Select-String" -Tags "CI" {
55
BeforeAll {
66
$nl = [Environment]::NewLine
77
$currentDirectory = $PWD.Path
8+
$originalRendering = $PSStyle.OutputRendering
9+
$PSStyle.OutputRendering = 'Ansi'
810
}
911

1012
AfterAll {
13+
$PSStyle.OutputRendering = $originalRendering
1114
Push-Location $currentDirectory
1215
}
1316

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Describe 'OutputRendering tests' {
2525
}
2626

2727
It 'OutputRendering works for "<outputRendering>" to the host' -TestCases @(
28-
@{ outputRendering = 'automatic'; ansi = $true }
2928
@{ outputRendering = 'host' ; ansi = $true }
3029
@{ outputRendering = 'ansi' ; ansi = $true }
3130
@{ outputRendering = 'plaintext'; ansi = $false }
@@ -43,7 +42,6 @@ Describe 'OutputRendering tests' {
4342
}
4443

4544
It 'OutputRendering works for "<outputRendering>" to the pipeline' -TestCases @(
46-
@{ outputRendering = 'automatic'; ansi = $true }
4745
@{ outputRendering = 'host' ; ansi = $false }
4846
@{ outputRendering = 'ansi' ; ansi = $true }
4947
@{ outputRendering = 'plaintext'; ansi = $false }
@@ -79,4 +77,13 @@ Describe 'OutputRendering tests' {
7977
$out[0] | Should -BeExactly "$($PSStyle.Formatting.$stream)$($stream.ToUpper()): hello$($PSStyle.Reset)" -Because ($out[0] | Out-String | Format-hex)
8078
$out[1] | Should -BeExactly "bye"
8179
}
80+
81+
It 'ToString(OutputRendering) works correctly' {
82+
$s = [System.Management.Automation.Internal.StringDecorated]::new($PSStyle.Foreground.Red + 'Hello')
83+
$s.IsDecorated | Should -BeTrue
84+
$s.ToString() | Should -BeExactly "$($PSStyle.Foreground.Red)Hello"
85+
$s.ToString([System.Management.Automation.OutputRendering]::ANSI) | Should -BeExactly "$($PSStyle.Foreground.Red)Hello"
86+
$s.ToString([System.Management.Automation.OutputRendering]::PlainText) | Should -BeExactly 'Hello'
87+
{ $s.ToString([System.Management.Automation.OutputRendering]::Host) } | Should -Throw -ErrorId 'ArgumentException'
88+
}
8289
}

0 commit comments

Comments
 (0)