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 @@ -109,8 +109,11 @@ private string PlainText
// 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\\)";
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

// 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.
Expand Down
9 changes: 9 additions & 0 deletions test/powershell/engine/Formatting/PSStyle.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,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' {
Expand Down