X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -565,7 +562,7 @@ public FileInfoFormatting()
/// <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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public StringDecorated(string text)
/// 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.
Expand All @@ -64,28 +67,17 @@ public StringDecorated(string text)
/// <returns>Rendered string based on outputRendering.</returns>
public string ToString(OutputRendering outputRendering)
{
if (!_isDecorated)
if (outputRendering == OutputRendering.Host)
{
return _text;
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not good to throw in public ToString().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take a closer look, you will find it won't throw in ToString(). The exception could happen only when user calling ToString(OutputRendering.Host).

}

if (outputRendering == OutputRendering.Automatic)
{
outputRendering = OutputRendering.Ansi;
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
{
outputRendering = OutputRendering.PlainText;
}
}

if (outputRendering == OutputRendering.PlainText)
{
return PlainText;
}
else
if (!_isDecorated)
{
return _text;
}

return outputRendering == OutputRendering.PlainText ? PlainText : _text;
}
}

Expand Down Expand Up @@ -139,7 +131,10 @@ public ValueStringDecorated(string text)
/// 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.
Expand All @@ -148,28 +143,17 @@ public ValueStringDecorated(string text)
/// <returns>Rendered string based on outputRendering.</returns>
public string ToString(OutputRendering outputRendering)
{
if (!_isDecorated)
if (outputRendering == OutputRendering.Host)
{
return _text;
throw new ArgumentException(StringDecoratedStrings.RequireExplicitRendering);
}

if (outputRendering == OutputRendering.Automatic)
{
outputRendering = OutputRendering.Ansi;
if (PSStyle.Instance.OutputRendering == OutputRendering.PlainText)
{
outputRendering = OutputRendering.PlainText;
}
}

if (outputRendering == OutputRendering.PlainText)
{
return PlainText;
}
else
if (!_isDecorated)
{
return _text;
}

return outputRendering == OutputRendering.PlainText ? PlainText : _text;
}
}
}
3 changes: 0 additions & 3 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,9 +1795,6 @@ internal static bool ShouldOutputPlainText(bool isHost, bool? supportsVirtualTer
{
switch (PSStyle.Instance.OutputRendering)
{
case OutputRendering.Automatic:
outputRendering = OutputRendering.Ansi;
break;
case OutputRendering.Host:
outputRendering = isHost ? OutputRendering.Ansi : OutputRendering.PlainText;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -145,6 +147,7 @@ Describe 'Get-Error tests' -Tag CI {
}
finally
{
$PSStyle.OutputRendering = $originalRendering
if ($suppressVT) {
$env:__SuppressAnsiEscapeSequences = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 9 additions & 2 deletions test/powershell/engine/Formatting/OutputRendering.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down Expand Up @@ -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'
}
}
8 changes: 7 additions & 1 deletion test/powershell/engine/Formatting/PSStyle.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
Expand Down
X Tutup