From f128324830eee3d274fb5f7b4c5669d5400af126 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Dec 2020 11:26:36 -0800 Subject: [PATCH 1/4] Add support to `$PSStyle` for strikethrough and hyperlinks --- .../FormatAndOutput/common/PSStyle.cs | 18 +++++++++++++ .../engine/Formatting/PSStyle.Tests.ps1 | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index f27a5b89da9..a60815114ea 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -365,6 +365,24 @@ public class FormattingData /// public string Underline { get; } = "\x1b[4m"; + /// + /// Gets value to turn off strikethrough. + /// + public string StrikethroughOff { get; } = "\x1b[29m"; + + /// + /// Gets value to turn on strikethrough. + /// + public string Strikethrough { get; } = "\x1b[9m"; + + /// + /// Gets ANSI representation of a hyperlink. + /// + public string ToHyperlink(string text, Uri link) + { + return $"\x1b]8;;{link}\x1b\\{text}\x1b]8;;\x1b\\"; + } + /// /// Gets the formatting rendering settings. /// diff --git a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 index 5e02e2e231c..a150e8c613d 100644 --- a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 +++ b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 @@ -18,6 +18,8 @@ Describe 'Tests for $PSStyle automatic variable' { Italic = "`e[3m" UnderlineOff = "`e[24m" Underline = "`e[4m" + StrikethroughOff = "`e[29m" + Strikethrough = "`e[9m" } $formattingDefaults = @{ @@ -111,4 +113,29 @@ Describe 'Tests for $PSStyle automatic variable' { $PSStyle.Background.$key | Should -BeExactly $value } + + It '$PSStyle.Foreground.FromRGB(r, g, b) works' { + $o = $PSStyle.Foreground.FromRGB(11,22,33) + $o | Should -BeExactly "`e[38;2;11;22;33m" -Because ($o | Format-Hex | Out-String) + } + + It '$PSStyle.Foreground.FromRGB(rgb) works' { + $o = $PSStyle.Foreground.FromRGB(0x223344) + $o | Should -BeExactly "`e[38;2;34;51;68m" -Because ($o | Format-Hex | Out-String) + } + + It '$PSStyle.Background.FromRGB(r, g, b) works' { + $o = $PSStyle.Background.FromRGB(33,44,55) + $o | Should -BeExactly "`e[48;2;33;44;55m" -Because ($o | Format-Hex | Out-String) + } + + It '$PSStyle.Background.FromRGB(rgb) works' { + $o = $PSStyle.Background.FromRGB(0x445566) + $o | Should -BeExactly "`e[48;2;68;85;102m" -Because ($o | Format-Hex | Out-String) + } + + It '$PSStyle.ToHyperlink() works' { + $o = $PSStyle.ToHyperlink('PSBlog','https://aka.ms/psblog') + $o | Should -BeExactly "`e]8;;https://aka.ms/psblog`e\PSBlog`e]8;;`e\" -Because ($o | Format-Hex | Out-String) + } } From 6272e9839789cbe1640701bec3bb9f88643effab Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Dec 2020 12:17:47 -0800 Subject: [PATCH 2/4] update formatting for new property and change to `FormatHyperlink()` --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 ++ .../FormatAndOutput/common/PSStyle.cs | 2 +- test/powershell/engine/Formatting/PSStyle.Tests.ps1 | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index dbb8d3a2085..f96eb3c4478 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -2039,6 +2039,8 @@ private static IEnumerable ViewsOf_System_Management_Autom .AddItemScriptBlock(@"""$($_.Italic)$($_.Italic.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Italic") .AddItemScriptBlock(@"""$($_.UnderlineOff)$($_.UnderlineOff.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "UnderlineOff") .AddItemScriptBlock(@"""$($_.Underline)$($_.Underline.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Underline") + .AddItemScriptBlock(@"""$($_.StrikethroughOff)$($_.UnderlineOff.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "StrikethroughOff") + .AddItemScriptBlock(@"""$($_.Strikethrough)$($_.Underline.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Strikethrough") .AddItemProperty(@"OutputRendering") .AddItemScriptBlock(@"""$($_.Formatting.FormatAccent)$($_.Formatting.FormatAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.FormatAccent") .AddItemScriptBlock(@"""$($_.Formatting.ErrorAccent)$($_.Formatting.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.ErrorAccent") diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index a60815114ea..9cd8d05ebef 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -378,7 +378,7 @@ public class FormattingData /// /// Gets ANSI representation of a hyperlink. /// - public string ToHyperlink(string text, Uri link) + public string FormatHyperlink(string text, Uri link) { return $"\x1b]8;;{link}\x1b\\{text}\x1b]8;;\x1b\\"; } diff --git a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 index a150e8c613d..a55c2392a8d 100644 --- a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 +++ b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 @@ -134,8 +134,8 @@ Describe 'Tests for $PSStyle automatic variable' { $o | Should -BeExactly "`e[48;2;68;85;102m" -Because ($o | Format-Hex | Out-String) } - It '$PSStyle.ToHyperlink() works' { - $o = $PSStyle.ToHyperlink('PSBlog','https://aka.ms/psblog') + It '$PSStyle.FormatHyperlink() works' { + $o = $PSStyle.FormatHyperlink('PSBlog','https://aka.ms/psblog') $o | Should -BeExactly "`e]8;;https://aka.ms/psblog`e\PSBlog`e]8;;`e\" -Because ($o | Format-Hex | Out-String) } } From b2a3b44fbba9dac5b54d0764f4ec7bf28c19a848 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Dec 2020 12:20:28 -0800 Subject: [PATCH 3/4] fix codefactor issue --- .../FormatAndOutput/common/PSStyle.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index 9cd8d05ebef..e45899c7f7d 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -378,6 +378,9 @@ public class FormattingData /// /// Gets ANSI representation of a hyperlink. /// + /// Text describing the link. + /// A valid hyperlink. + /// String representing ANSI code for the hyperlink. public string FormatHyperlink(string text, Uri link) { return $"\x1b]8;;{link}\x1b\\{text}\x1b]8;;\x1b\\"; From 518c735af6856724bc71ab3a399b6d495d38071e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Dec 2020 16:54:51 -0800 Subject: [PATCH 4/4] fix cut and paste error with formatting --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index f96eb3c4478..d881f551b31 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -2039,8 +2039,8 @@ private static IEnumerable ViewsOf_System_Management_Autom .AddItemScriptBlock(@"""$($_.Italic)$($_.Italic.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Italic") .AddItemScriptBlock(@"""$($_.UnderlineOff)$($_.UnderlineOff.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "UnderlineOff") .AddItemScriptBlock(@"""$($_.Underline)$($_.Underline.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Underline") - .AddItemScriptBlock(@"""$($_.StrikethroughOff)$($_.UnderlineOff.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "StrikethroughOff") - .AddItemScriptBlock(@"""$($_.Strikethrough)$($_.Underline.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Strikethrough") + .AddItemScriptBlock(@"""$($_.StrikethroughOff)$($_.StrikethroughOff.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "StrikethroughOff") + .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.ErrorAccent)$($_.Formatting.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.ErrorAccent")