Skip to content

Fix formatting to properly handle the Reset VT sequences that appear in the middle of a string#26424

Merged
daxian-dbw merged 3 commits into
PowerShell:masterfrom
daxian-dbw:sls
Nov 24, 2025
Merged

Fix formatting to properly handle the Reset VT sequences that appear in the middle of a string#26424
daxian-dbw merged 3 commits into
PowerShell:masterfrom
daxian-dbw:sls

Conversation

@daxian-dbw
Copy link
Copy Markdown
Member

PR Summary

Fix #25833
Also, this is a follow-up fix to #17316

When dealing with word wrapping with a string that contains VT sequences in the formatting system, the Reset VT sequences appear in the middle of the string are not handled properly, which caused the resulted string to be poorly decorated with VT sequences.

The Reset VT sequences in a string essentially void the previous VT sequences. So, any VT sequences appeared before the Reset should be cleared before processing the rest of the string.

For this script, the formatting results are as follows:

driverquery.exe /V > driverqueryV.txt # generate a searchable file
sls mouse .\driverqueryV.txt

Before the fix:

image

After the fix:

image

PR Checklist

Copilot AI review requested due to automatic review settings November 11, 2025 20:06
@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 11, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes improper handling of VT Reset sequences (\e[0m) that appear in the middle of strings during formatting operations. Reset sequences void all previous VT sequences, so the formatting system now clears any accumulated VT sequences when a Reset is encountered, ensuring proper text decoration and word wrapping behavior.

Key Changes

  • Modified GetWords and SplitLines methods in ComplexWriter.cs to detect Reset sequences and clear accumulated VT sequences when found
  • Added VtResetAdded flag to GetWordsResult struct to track whether a Reset was automatically added, enabling proper suffix insertion
  • Updated ListWriter.cs to use TrimEnd() before checking for Reset sequences at line endings
  • Added comprehensive test coverage for various scenarios involving Reset sequences

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs Implements Reset sequence detection in GetWords and SplitLines methods; adds VtResetAdded field to track automatic Reset additions; removes unused System.Text.RegularExpressions import
src/System.Management.Automation/FormatAndOutput/common/ListWriter.cs Improves Reset sequence detection by trimming trailing whitespace before checking line endings
test/powershell/engine/Formatting/PSStyle.Tests.ps1 Adds 7 new test cases covering word wrapping, multi-line splitting, and MatchInfo formatting with various Reset sequence combinations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Nov 19, 2025
Copy link
Copy Markdown
Contributor

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

One little note of something that might be worth checking for, but maybe not worth it

vtSeqs.Append(vtSpan);

wordHasVtSeqs = true;
if (vtSpan.SequenceEqual(PSStyle.Instance.Reset))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth checking for `e[m as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. There are many other places where PSStyle.Instance.Reset is checked but not "\x1b[m". I will open an issue tracking this, and maybe do it in a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking issue opened: #26526

@daxian-dbw daxian-dbw added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log and removed CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Nov 24, 2025
@daxian-dbw daxian-dbw merged commit eb2ead4 into PowerShell:master Nov 24, 2025
45 of 48 checks passed
@daxian-dbw daxian-dbw deleted the sls branch November 24, 2025 22:38
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
@daxian-dbw daxian-dbw removed the Review - Needed The PR is being reviewed label Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Select-String emphasis improperly highlights spaces in matching lines as wide or wider than [console]::WindowWidth

4 participants