Skip to content

fix: fix regexes for mouse escape sequences#5943

Merged
willmcgugan merged 1 commit into
Textualize:mainfrom
TomJGooding:fix-mouse-escape-sequence-regexes
Jul 10, 2025
Merged

fix: fix regexes for mouse escape sequences#5943
willmcgugan merged 1 commit into
Textualize:mainfrom
TomJGooding:fix-mouse-escape-sequence-regexes

Conversation

@TomJGooding

@TomJGooding TomJGooding commented Jul 9, 2025

Copy link
Copy Markdown
Contributor

Fix potential issues with the regexes for mouse escape sequences, which were recently updated to allow negative coordinates.

  • Fix the updated character class [-?\d;]. This will match the literal character ? which I assume was not the intention.
  • Fix the regex for SGR mouse sequences, where the first parameter is the button value and should not be negative.

(Note for reviewers: These regexes are the same as my original PR #5688 which was superseded, so if I've just misunderstood why the updated regexes are more correct please let me know!)

Please review the following checklist.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

Fix potential issues with the regexes for mouse escape sequences,
which were recently updated to allow negative coordinates.

- Fix the updated character class `[-?\d;]`. This will match the literal
  character `?` which I assume was not the intention.
- Fix the regex for SGR mouse sequences, where the first parameter is
  the button value and should not be negative.

class XTermParser(Parser[Message]):
_re_sgr_mouse = re.compile(r"\x1b\[<(-?\d+);(-?\d+);(-?\d+)([Mm])")
_re_sgr_mouse = re.compile(r"\x1b\[<(\d+);(-?\d+);(-?\d+)([Mm])")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The irony is that the terminal shouldn't send hyphens in any of those fields. But I guess we haven't seen negative mouse buttons in the wild, so this makes sense.

@willmcgugan willmcgugan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suck at regexes

@willmcgugan willmcgugan merged commit 64c2c2e into Textualize:main Jul 10, 2025
23 checks passed
@TomJGooding TomJGooding deleted the fix-mouse-escape-sequence-regexes branch July 11, 2025 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants