-
Notifications
You must be signed in to change notification settings - Fork 0
Enable SGR mouse tracking and parse mouse escape sequences #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shouze
merged 2 commits into
feat/mouse-ui-checkbox
from
feat/mouse-tracking-foundation
Aug 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, it, expect } from "bun:test"; | ||
| import { parseMouseEvent } from "./mouse.ts"; | ||
|
|
||
| describe("parseMouseEvent", () => { | ||
| it("parses a valid left click (button 0) press", () => { | ||
| const event = parseMouseEvent("\x1b[<0;12;5M"); | ||
| expect(event).toEqual({ button: 0, x: 12, y: 5, isRelease: false }); | ||
| }); | ||
|
|
||
| it("parses a valid left click (button 0) release", () => { | ||
| const event = parseMouseEvent("\x1b[<0;12;5m"); | ||
| expect(event).toEqual({ button: 0, x: 12, y: 5, isRelease: true }); | ||
| }); | ||
|
|
||
| it("parses a valid middle click (button 1)", () => { | ||
| const event = parseMouseEvent("\x1b[<1;100;50M"); | ||
| expect(event).toEqual({ button: 1, x: 100, y: 50, isRelease: false }); | ||
| }); | ||
|
|
||
| it("parses a valid right click (button 2)", () => { | ||
| const event = parseMouseEvent("\x1b[<2;5;1M"); | ||
| expect(event).toEqual({ button: 2, x: 5, y: 1, isRelease: false }); | ||
| }); | ||
|
|
||
| it("parses wheel up (button 64)", () => { | ||
| const event = parseMouseEvent("\x1b[<64;10;10M"); | ||
| expect(event).toEqual({ button: 64, x: 10, y: 10, isRelease: false }); | ||
| }); | ||
|
|
||
| it("parses wheel down (button 65)", () => { | ||
| const event = parseMouseEvent("\x1b[<65;10;10M"); | ||
| expect(event).toEqual({ button: 65, x: 10, y: 10, isRelease: false }); | ||
| }); | ||
|
|
||
| it("returns null for malformed sequence (missing bracket)", () => { | ||
| const event = parseMouseEvent("\x1b[0;12;5M"); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for malformed sequence (non-numeric button)", () => { | ||
| const event = parseMouseEvent("\x1b[<a;12;5M"); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for malformed sequence (missing semicolon)", () => { | ||
| const event = parseMouseEvent("\x1b[<0,12;5M"); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for empty string", () => { | ||
| const event = parseMouseEvent(""); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for partial/truncated sequence", () => { | ||
| const event = parseMouseEvent("\x1b[<0;12"); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for unrelated escape sequence", () => { | ||
| const event = parseMouseEvent("\x1b[A"); // arrow up | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for non-escape sequence", () => { | ||
| const event = parseMouseEvent("hello"); | ||
| expect(event).toBeNull(); | ||
| }); | ||
|
|
||
| it("handles edge-case coordinates (0, 0)", () => { | ||
| const event = parseMouseEvent("\x1b[<0;0;0M"); | ||
| expect(event).toEqual({ button: 0, x: 0, y: 0, isRelease: false }); | ||
| }); | ||
|
|
||
| it("handles large coordinates (999, 999)", () => { | ||
| const event = parseMouseEvent("\x1b[<0;999;999M"); | ||
| expect(event).toEqual({ button: 0, x: 999, y: 999, isRelease: false }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Mouse event parsing for SGR-mode terminal mouse reporting. | ||
| // SGR format: CSI [< button ; x ; y M/m where M=press, m=release | ||
|
|
||
| export interface MouseEvent { | ||
| button: number; | ||
| x: number; | ||
| y: number; | ||
| isRelease: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Parses an SGR-format mouse event sequence. | ||
| * Returns a structured event or null if the sequence doesn't match or is malformed. | ||
| * | ||
| * SGR format: \x1b[<button;x;yM (press) or \x1b[<button;x;ym (release) | ||
| * Button codes: | ||
| * 0 = left click | ||
| * 1 = middle click | ||
| * 2 = right click | ||
| * 64 = wheel up | ||
| * 65 = wheel down | ||
| */ | ||
| const SGR_MOUSE_PREFIX = "\x1b[<"; | ||
|
|
||
| export function parseMouseEvent(sequence: string): MouseEvent | null { | ||
| if (!sequence.startsWith(SGR_MOUSE_PREFIX)) return null; | ||
| const match = sequence.slice(SGR_MOUSE_PREFIX.length).match(/^(\d+);(\d+);(\d+)([Mm])$/); | ||
| if (!match) return null; | ||
|
|
||
| const button = parseInt(match[1], 10); | ||
| const x = parseInt(match[2], 10); | ||
| const y = parseInt(match[3], 10); | ||
| const isRelease = match[4] === "m"; // lowercase m = release, uppercase M = press | ||
|
|
||
| return { button, x, y, isRelease }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.