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 @@ -2051,6 +2051,7 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
.AddItemScriptBlock(@"""$($_.Strikethrough)$($_.Strikethrough.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Strikethrough")
.AddItemProperty(@"OutputRendering")
.AddItemScriptBlock(@"""$($_.Formatting.FormatAccent)$($_.Formatting.FormatAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.FormatAccent")
.AddItemScriptBlock(@"""$($_.Formatting.TableHeaader)$($_.Formatting.TableHeaader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.TableHeaader")
.AddItemScriptBlock(@"""$($_.Formatting.ErrorAccent)$($_.Formatting.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.ErrorAccent")
.AddItemScriptBlock(@"""$($_.Formatting.Error)$($_.Formatting.Error.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Error")
.AddItemScriptBlock(@"""$($_.Formatting.Warning)$($_.Formatting.Warning.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Warning")
Expand Down Expand Up @@ -2102,6 +2103,7 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
ListControl.Create()
.StartEntry()
.AddItemScriptBlock(@"""$($_.FormatAccent)$($_.FormatAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "FormatAccent")
.AddItemScriptBlock(@"""$($_.TableHeader)$($_.TableHeader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "TableHeader")
.AddItemScriptBlock(@"""$($_.ErrorAccent)$($_.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "ErrorAccent")
.AddItemScriptBlock(@"""$($_.Error)$($_.Error.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Error")
.AddItemScriptBlock(@"""$($_.Warning)$($_.Warning.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Warning")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands.Internal.Format
Expand Down Expand Up @@ -59,6 +60,10 @@ internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayC

// check if we have to truncate the labels
int maxAllowableLabelLength = screenColumnWidth - Separator.Length - MinFieldWidth;
if (InternalTestHooks.ForceFormatListFixedLabelWidth)
{
maxAllowableLabelLength = 10;
}

// find out the max display length (cell count) of the property names
_propertyLabelsDisplayLength = 0; // reset max
Expand Down Expand Up @@ -217,11 +222,25 @@ private void WriteSingleLineHelper(string prependString, string line, LineOutput
{
if (k == 0)
{
lo.WriteLine(prependString + sc[k]);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering"))
{
lo.WriteLine(PSStyle.Instance.Formatting.FormatAccent + prependString + PSStyle.Instance.Reset + sc[k]);
}
else
{
lo.WriteLine(prependString + sc[k]);
}
}
else
{
lo.WriteLine(padding + sc[k]);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering"))
{
lo.WriteLine(padding + PSStyle.Instance.Formatting.FormatAccent + PSStyle.Instance.Reset + sc[k]);
}
else
{
lo.WriteLine(padding + sc[k]);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ public sealed class FormattingData
/// </summary>
public string FormatAccent { get; set; } = "\x1b[32;1m";

/// <summary>
/// Gets or sets the style for table headers.
/// </summary>
public string TableHeader { get; set; } = "\x1b[32;1m";

/// <summary>
/// Gets or sets the accent style for errors.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Text;

Expand Down Expand Up @@ -155,7 +156,14 @@ internal int GenerateHeader(string[] values, LineOutput lo)
{
foreach (string line in _header)
{
lo.WriteLine(line);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering"))
{
lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset);
}
else
{
lo.WriteLine(line);
}
}

return _header.Count;
Expand All @@ -164,7 +172,7 @@ internal int GenerateHeader(string[] values, LineOutput lo)
_header = new List<string>();

// generate the row with the header labels
GenerateRow(values, lo, true, null, lo.DisplayCells, _header);
GenerateRow(values, lo, true, null, lo.DisplayCells, _header, isHeader: true);

// generate an array of "--" as header markers below
// the column header labels
Expand All @@ -191,14 +199,16 @@ internal int GenerateHeader(string[] values, LineOutput lo)
breakLine[k] = StringUtil.DashPadding(count);
}

GenerateRow(breakLine, lo, false, null, lo.DisplayCells, _header);
GenerateRow(breakLine, lo, false, null, lo.DisplayCells, _header, isHeader: true);
return _header.Count;
}

internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOnlySpan<int> alignment, DisplayCells dc, List<string> generatedRows)
internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOnlySpan<int> alignment, DisplayCells dc, List<string> generatedRows, bool isHeader = false)
{
if (_disabled)
{
return;
}

// build the current row alignment settings
int cols = _si.columnInfo.Length;
Expand All @@ -216,9 +226,13 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn
for (int i = 0; i < currentAlignment.Length; i++)
{
if (alignment[i] == TextAlignment.Undefined)
{
currentAlignment[i] = _si.columnInfo[i].alignment;
}
else
{
currentAlignment[i] = alignment[i];
}
}
}

Expand All @@ -227,14 +241,28 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn
foreach (string line in GenerateTableRow(values, currentAlignment, lo.DisplayCells))
{
generatedRows?.Add(line);
lo.WriteLine(line);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering") && isHeader)
{
lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset);
}
else
{
lo.WriteLine(line);
}
}
}
else
{
string line = GenerateRow(values, currentAlignment, dc);
generatedRows?.Add(line);
lo.WriteLine(line);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering") && isHeader)
{
lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset);
}
else
{
lo.WriteLine(line);
}
}
}

Expand Down Expand Up @@ -441,7 +469,15 @@ private string GenerateRow(string[] values, ReadOnlySpan<int> alignment, Display
if (values[k].Contains(ESC))
{
// Reset the console output if the content of this column contains ESC
sb.Append(ResetConsoleVt100Code);
if (ExperimentalFeature.IsEnabled("PSAnsiRendering"))
{
// Remove definition of `ResetConsoleVt10Code` when PSAnsiRendering is not experimental
sb.Append(PSStyle.Instance.Reset);
}
else
{
sb.Append(ResetConsoleVt100Code);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,7 @@ internal enum FormatStyle
{
Reset,
FormatAccent,
TableHeader,
ErrorAccent,
Error,
Warning,
Expand Down Expand Up @@ -1870,6 +1871,8 @@ internal static string GetFormatStyleString(FormatStyle formatStyle)
return psstyle.Reset;
case FormatStyle.FormatAccent:
return psstyle.Formatting.FormatAccent;
case FormatStyle.TableHeader:
return psstyle.Formatting.TableHeader;
case FormatStyle.ErrorAccent:
return psstyle.Formatting.ErrorAccent;
case FormatStyle.Error:
Expand Down Expand Up @@ -2073,6 +2076,7 @@ public static class InternalTestHooks
internal static bool ForcePromptForChoiceDefaultOption;
internal static bool BypassOutputRedirectionCheck;
internal static bool NoPromptForPassword;
internal static bool ForceFormatListFixedLabelWidth;

// Stop/Restart/Rename Computer tests
internal static bool TestStopComputer;
Expand Down
19 changes: 16 additions & 3 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export $envVarName='$guid'
}

It "Doesn't run the login profile when -Login not used" {
$result = & $powershell -Command "`$env:$envVarName"
$result = & $powershell -noprofile -Command "`$env:$envVarName"
$result | Should -BeNullOrEmpty
$LASTEXITCODE | Should -Be 0
}
Expand Down Expand Up @@ -374,7 +374,20 @@ export $envVarName='$guid'
}

Context "Pipe to/from powershell" {
$p = [PSCustomObject]@{X=10;Y=20}
BeforeAll {
if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}

$p = [PSCustomObject]@{X=10;Y=20}
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}
}

It "xml input" {
$p | & $powershell -noprofile { $input | ForEach-Object {$a = 0} { $a += $_.X + $_.Y } { $a } } | Should -Be 30
Expand Down Expand Up @@ -651,7 +664,7 @@ namespace StackTest {
}

It "powershell starts if PATH is not set" -Skip:($IsWindows) {
bash -c "unset PATH;$powershell -c '1+1'" | Should -BeExactly 2
bash -c "unset PATH;$powershell -nop -c '1+1'" | Should -BeExactly 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,19 @@ Describe 'ScriptScopeAccessFromClassMethod' -Tags "CI" {
}

Describe 'Hidden Members Test ' -Tags "CI" {
BeforeAll {
if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}
}

class C1
{
[int]$visibleX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ Describe "Format-Custom" -Tags "CI" {
}

Describe "Format-Custom DRT basic functionality" -Tags "CI" {
Add-Type -TypeDefinition @"
BeforeAll {
if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}
}

Add-Type -TypeDefinition @"
public abstract class NamedItem
{
public string name;
Expand Down Expand Up @@ -310,6 +323,11 @@ class MyLeaf2

Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" {
BeforeAll {
if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}

$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
$xmlContent = @'
<?xml version="1.0" encoding="UTF-8" ?>
Expand Down Expand Up @@ -389,6 +407,10 @@ Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}

$rs.Close()
$ps.Dispose()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Format-List" -Tags "CI" {
$nl = [Environment]::NewLine
BeforeAll {
$nl = [Environment]::NewLine

if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}
}

BeforeEach {
$in = New-Object PSObject
Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue
Expand Down Expand Up @@ -70,6 +84,19 @@ Describe "Format-List" -Tags "CI" {
}

Describe "Format-List DRT basic functionality" -Tags "CI" {
BeforeAll {
if ($null -ne $PSStyle) {
$outputRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'plaintext'
}
}

AfterAll {
if ($null -ne $PSStyle) {
$PSStyle.OutputRendering = $outputRendering
}
}

It "Format-List with array should work" {
$al = (0..255)
$info = @{}
Expand Down Expand Up @@ -183,3 +210,22 @@ dbda : KM
$actual | Should -BeExactly $expected
}
}

Describe 'Format-List color tests' {
BeforeAll {
$PSDefaultParameterValues.Add('It:Skip', (-not $EnabledExperimentalFeatures.Contains('PSAnsiRendering')))
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $true)
}

AfterAll {
$PSDefaultParameterValues.Remove('It:Skip')
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $false)
}

It 'Property names should use FormatAccent' {
$out = ([pscustomobject]@{Short=1;LongLabelName=2} | fl | out-string).Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
$out.Count | Should -Be 2
$out[0] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)Short : $($PSStyle.Reset)1" -Because ($out[0] | Format-Hex)
$out[1] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)LongLabelN : $($PSStyle.Reset)2"
}
}
Loading