mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Make AnsiRegex able to capture Hyperlink ansi sequences (#17442)
* Add the hyperlink ansi regex * Add test * Add comment
This commit is contained in:
@@ -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' {
|
||||
|
||||
Reference in New Issue
Block a user