Change default to OutputRendering.Host and remove OutputRendering.Automatic (#15882)

This commit is contained in:
Steve Lee 2021-08-16 14:17:07 -07:00 committed by GitHub
parent b3a12b4faa
commit a162856af5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 124 additions and 48 deletions

View file

@ -13,17 +13,14 @@ namespace System.Management.Automation
/// </summary>
public enum OutputRendering
{
/// <summary>Automatic by PowerShell.</summary>
Automatic = 0,
/// <summary>Render ANSI only to host.</summary>
Host = 0,
/// <summary>Render as plaintext.</summary>
PlainText = 1,
/// <summary>Render as ANSI.</summary>
Ansi = 2,
/// <summary>Render ANSI only to host.</summary>
Host = 3,
}
#endregion OutputRendering
@ -565,7 +562,7 @@ namespace System.Management.Automation
/// <summary>
/// Gets or sets the rendering mode for output.
/// </summary>
public OutputRendering OutputRendering { get; set; } = OutputRendering.Automatic;
public OutputRendering OutputRendering { get; set; } = OutputRendering.Host;
/// <summary>
/// Gets value to turn off all attributes.

View file

@ -55,7 +55,10 @@ namespace System.Management.Automation.Internal
/// Render the decorarted string using automatic output rendering.
/// </summary>
/// <returns>Rendered string based on automatic output rendering.</returns>
public override string ToString() => _isDecorated ? ToString(OutputRendering.Automatic) : _text;
public override string ToString() => ToString(
PSStyle.Instance.OutputRendering == OutputRendering.PlainText
? OutputRendering.PlainText
: OutputRendering.Ansi);
/// <summary>
/// Return string representation of content depending on output rendering mode.
@ -64,28 +67,17 @@ namespace System.Management.Automation.Internal
/// <returns>Rendered string based on outputRendering.</returns>
public string ToString(OutputRendering outputRendering)
{
if (outputRendering == OutputRendering.Host)
{
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
}
if (!_isDecorated)
{
return _text;
}
if (outputRendering == OutputRendering.Automatic)
{
outputRendering = OutputRendering.Ansi;
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
{
outputRendering = OutputRendering.PlainText;
}
}
if (outputRendering == OutputRendering.PlainText)
{
return PlainText;
}
else
{
return _text;
}
return outputRendering == OutputRendering.PlainText ? PlainText : _text;
}
}
@ -139,7 +131,10 @@ namespace System.Management.Automation.Internal
/// Render the decorarted string using automatic output rendering.
/// </summary>
/// <returns>Rendered string based on automatic output rendering.</returns>
public override string ToString() => _isDecorated ? ToString(OutputRendering.Automatic) : _text;
public override string ToString() => ToString(
PSStyle.Instance.OutputRendering == OutputRendering.PlainText
? OutputRendering.PlainText
: OutputRendering.Ansi);
/// <summary>
/// Return string representation of content depending on output rendering mode.
@ -148,28 +143,17 @@ namespace System.Management.Automation.Internal
/// <returns>Rendered string based on outputRendering.</returns>
public string ToString(OutputRendering outputRendering)
{
if (outputRendering == OutputRendering.Host)
{
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
}
if (!_isDecorated)
{
return _text;
}
if (outputRendering == OutputRendering.Automatic)
{
outputRendering = OutputRendering.Ansi;
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
{
outputRendering = OutputRendering.PlainText;
}
}
if (outputRendering == OutputRendering.PlainText)
{
return PlainText;
}
else
{
return _text;
}
return outputRendering == OutputRendering.PlainText ? PlainText : _text;
}
}
}

View file

@ -1570,9 +1570,6 @@ namespace System.Management.Automation
{
switch (PSStyle.Instance.OutputRendering)
{
case OutputRendering.Automatic:
outputRendering = OutputRendering.Ansi;
break;
case OutputRendering.Host:
outputRendering = isHost ? OutputRendering.Ansi : OutputRendering.PlainText;
break;

View file

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="https://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="RequireExplicitRendering" xml:space="preserve">
<value>Only 'ANSI' or 'PlainText' is supported for this method.</value>
</data>
</root>

View file

@ -4,10 +4,13 @@
Describe '$env:__SuppressAnsiEscapeSequences tests' -Tag CI {
BeforeAll {
$originalSuppressPref = $env:__SuppressAnsiEscapeSequences
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
}
AfterAll {
$env:__SuppressAnsiEscapeSequences = $originalSuppressPref
$PSStyle.OutputRendering = $originalRendering
}

View file

@ -213,10 +213,13 @@ dbda : KM
Describe 'Format-List color tests' {
BeforeAll {
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $true)
}
AfterAll {
$PSStyle.OutputRendering = $originalRendering
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $false)
}

View file

@ -848,6 +848,15 @@ A Name B
}
Describe 'Table color tests' {
BeforeAll {
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
}
AfterAll {
$PSStyle.OutputRendering = $originalRendering
}
It 'Table header should use FormatAccent' {
([pscustomobject]@{foo = 1} | Format-Table | Out-String).Trim() | Should -BeExactly @"
$($PSStyle.Formatting.FormatAccent)foo$($PSStyle.Reset)

View file

@ -134,6 +134,8 @@ Describe 'Get-Error tests' -Tag CI {
}
try {
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
$out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("BypassOutputRedirectionCheck", $true); try { 1/0 } catch { }; Get-Error' | Out-String
# need to escape the open square bracket so the regex works
@ -145,6 +147,7 @@ Describe 'Get-Error tests' -Tag CI {
}
finally
{
$PSStyle.OutputRendering = $originalRendering
if ($suppressVT) {
$env:__SuppressAnsiEscapeSequences = 1
}

View file

@ -5,9 +5,12 @@ Describe "Select-String" -Tags "CI" {
BeforeAll {
$nl = [Environment]::NewLine
$currentDirectory = $PWD.Path
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
}
AfterAll {
$PSStyle.OutputRendering = $originalRendering
Push-Location $currentDirectory
}

View file

@ -25,7 +25,6 @@ Describe 'OutputRendering tests' {
}
It 'OutputRendering works for "<outputRendering>" to the host' -TestCases @(
@{ outputRendering = 'automatic'; ansi = $true }
@{ outputRendering = 'host' ; ansi = $true }
@{ outputRendering = 'ansi' ; ansi = $true }
@{ outputRendering = 'plaintext'; ansi = $false }
@ -43,7 +42,6 @@ Describe 'OutputRendering tests' {
}
It 'OutputRendering works for "<outputRendering>" to the pipeline' -TestCases @(
@{ outputRendering = 'automatic'; ansi = $true }
@{ outputRendering = 'host' ; ansi = $false }
@{ outputRendering = 'ansi' ; ansi = $true }
@{ outputRendering = 'plaintext'; ansi = $false }
@ -79,4 +77,13 @@ Describe 'OutputRendering tests' {
$out[0] | Should -BeExactly "$($PSStyle.Formatting.$stream)$($stream.ToUpper()): hello$($PSStyle.Reset)" -Because ($out[0] | Out-String | Format-hex)
$out[1] | Should -BeExactly "bye"
}
It 'ToString(OutputRendering) works correctly' {
$s = [System.Management.Automation.Internal.StringDecorated]::new($PSStyle.Foreground.Red + 'Hello')
$s.IsDecorated | Should -BeTrue
$s.ToString() | Should -BeExactly "$($PSStyle.Foreground.Red)Hello"
$s.ToString([System.Management.Automation.OutputRendering]::ANSI) | Should -BeExactly "$($PSStyle.Foreground.Red)Hello"
$s.ToString([System.Management.Automation.OutputRendering]::PlainText) | Should -BeExactly 'Hello'
{ $s.ToString([System.Management.Automation.OutputRendering]::Host) } | Should -Throw -ErrorId 'ArgumentException'
}
}

View file

@ -83,7 +83,7 @@ Describe 'Tests for $PSStyle automatic variable' {
It '$PSStyle has correct default for OutputRendering' {
$PSStyle | Should -Not -BeNullOrEmpty
$PSStyle.OutputRendering | Should -BeExactly 'Automatic'
$PSStyle.OutputRendering | Should -BeExactly 'Host'
}
It '$PSStyle has correct defaults for style <key>' -TestCases (Get-TestCases $styleDefaults) {
@ -137,26 +137,32 @@ Describe 'Tests for $PSStyle automatic variable' {
It '$PSStyle.Formatting.FormatAccent is applied to Format-List' {
$old = $PSStyle.Formatting.FormatAccent
$oldRender = $PSStyle.OutputRendering
try {
$PSStyle.OutputRendering = 'Ansi'
$PSStyle.Formatting.FormatAccent = $PSStyle.Foreground.Yellow + $PSStyle.Background.Red + $PSStyle.Italic
$out = $PSVersionTable | Format-List | Out-String
$out | Should -BeLike "*$($PSStyle.Formatting.FormatAccent.Replace('[',"``["))*"
}
finally {
$PSStyle.OutputRendering = $oldRender
$PSStyle.Formatting.FormatAccent = $old
}
}
It '$PSStyle.Formatting.TableHeader is applied to Format-Table' {
$old = $PSStyle.Formatting.TableHeader
$oldRender = $PSStyle.OutputRendering
try {
$PSStyle.OutputRendering = 'Ansi'
$PSStyle.Formatting.TableHeader = $PSStyle.Foreground.Blue + $PSStyle.Background.White + $PSStyle.Bold
$out = $PSVersionTable | Format-Table | Out-String
$out | Should -BeLike "*$($PSStyle.Formatting.TableHeader.Replace('[',"``["))*"
}
finally {
$PSStyle.OutputRendering = $oldRender
$PSStyle.Formatting.TableHeader = $old
}
}