Make AnsiRegex able to capture Hyperlink ansi sequences (#17442)

* Add the hyperlink ansi regex

* Add test

* Add comment
This commit is contained in:
Dongbo Wang
2022-06-08 10:35:07 -07:00
committed by GitHub
parent a8cd40a0ac
commit 6a522d9c68
2 changed files with 13 additions and 1 deletions
@@ -109,8 +109,11 @@ namespace System.Management.Automation.Internal
// CSI escape sequences
private const string CsiRegex = @"(\x1b\[\?\d+[hl])";
// Hyperlink escape sequences. Note: '.*?' makes '.*' do non-greedy match.
private const string HyperlinkRegex = @"(\x1b\]8;;.*?\x1b\\)";
// replace regex with .NET 6 API once available
internal static readonly Regex AnsiRegex = new Regex($"{GraphicsRegex}|{CsiRegex}", RegexOptions.Compiled);
internal static readonly Regex AnsiRegex = new Regex($"{GraphicsRegex}|{CsiRegex}|{HyperlinkRegex}", RegexOptions.Compiled);
/// <summary>
/// Get the ranges of all escape sequences in the text.
@@ -277,6 +277,15 @@ Describe 'Tests for $PSStyle automatic variable' {
$result = & $pwsh -noprofile -Command { $PSStyle.Progress.UseOSCIndicator = $true; 'hello'} | Format-List
$result | Out-String -Stream | Should -BeExactly 'hello'
}
It 'Able to handle Hyperlink ansi sequences' {
$word = "This is a link"
$hyperlink = $PSStyle.FormatHyperlink($word, "some random text as a link")
$strDec = [System.Management.Automation.Internal.StringDecorated]::new($hyperlink)
$strDec.IsDecorated | Should -BeTrue
$strDec.ContentLength | Should -Be $word.Length
$strDec.ToString("PlainText") | Should -Be $word
}
}
Describe 'Handle strings with escape sequences in formatting' {