Add support to $PSStyle for strikethrough and hyperlinks (#14461)

This commit is contained in:
Steve Lee
2020-12-19 11:54:51 +05:00
committed by GitHub
parent 10ef7f5161
commit bc080921bf
3 changed files with 50 additions and 0 deletions
@@ -2039,6 +2039,8 @@ namespace System.Management.Automation.Runspaces
.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)$($_.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")
@@ -365,6 +365,27 @@ namespace System.Management.Automation
/// </summary>
public string Underline { get; } = "\x1b[4m";
/// <summary>
/// Gets value to turn off strikethrough.
/// </summary>
public string StrikethroughOff { get; } = "\x1b[29m";
/// <summary>
/// Gets value to turn on strikethrough.
/// </summary>
public string Strikethrough { get; } = "\x1b[9m";
/// <summary>
/// Gets ANSI representation of a hyperlink.
/// </summary>
/// <param name="text">Text describing the link.</param>
/// <param name="link">A valid hyperlink.</param>
/// <returns>String representing ANSI code for the hyperlink.</returns>
public string FormatHyperlink(string text, Uri link)
{
return $"\x1b]8;;{link}\x1b\\{text}\x1b]8;;\x1b\\";
}
/// <summary>
/// Gets the formatting rendering settings.
/// </summary>
@@ -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.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)
}
}