feat(tui): preserve modifiers on mouse wheel events

Decode Shift, Meta, and Ctrl bits from SGR and legacy X10 wheel event button bytes so TUI input handlers can distinguish modified wheel gestures from plain scrolling.
This commit is contained in:
Brooklyn Nicholson 2026-04-29 20:39:39 -05:00
parent 9fc9c15b4a
commit b978fd8b26
2 changed files with 79 additions and 5 deletions

View file

@ -39,3 +39,60 @@ describe('parseMultipleKeypresses bracketed paste recovery', () => {
expect(state.pasteBuffer).toBe('')
})
})
describe('mouse wheel modifier decoding', () => {
// SGR mouse format: ESC [ < button ; col ; row M
// Wheel up = 64 (0x40), wheel down = 65 (0x41).
// Modifier bits: shift = 0x04, meta = 0x08, ctrl = 0x10.
const sgrWheel = (button: number) => `\x1b[<${button};10;10M`
it('plain wheel up has no modifiers', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x40))
expect(key).toMatchObject({ name: 'wheelup', ctrl: false, meta: false, shift: false })
})
it('plain wheel down has no modifiers', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x41))
expect(key).toMatchObject({ name: 'wheeldown', ctrl: false, meta: false, shift: false })
})
it('decodes meta (Alt/Option) on wheel up', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x40 | 0x08))
expect(key).toMatchObject({ name: 'wheelup', ctrl: false, meta: true, shift: false })
})
it('decodes meta (Alt/Option) on wheel down', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x41 | 0x08))
expect(key).toMatchObject({ name: 'wheeldown', ctrl: false, meta: true, shift: false })
})
it('decodes ctrl on wheel events', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x40 | 0x10))
expect(key).toMatchObject({ name: 'wheelup', ctrl: true, meta: false, shift: false })
})
it('decodes shift on wheel events', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x41 | 0x04))
expect(key).toMatchObject({ name: 'wheeldown', ctrl: false, meta: false, shift: true })
})
it('decodes combined modifiers', () => {
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, sgrWheel(0x40 | 0x08 | 0x10))
expect(key).toMatchObject({ name: 'wheelup', ctrl: true, meta: true, shift: false })
})
it('decodes meta on legacy X10 wheel encoding', () => {
// X10: ESC [ M Cb Cx Cy where each byte is value+32.
const x10 = `\x1b[M${String.fromCharCode(0x40 + 0x08 + 32)}${String.fromCharCode(10 + 32)}${String.fromCharCode(10 + 32)}`
const [[key]] = parseMultipleKeypresses(INITIAL_STATE, x10)
expect(key).toMatchObject({ name: 'wheelup', meta: true })
})
})