Repository navigation
Keep mouse and paste modes synchronized after terminal resets #1276
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
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
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,115 +1,59 @@ | ||||||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||||||
| import type { Terminal } from '@xterm/xterm'; | ||||||
| // @vitest-environment jsdom | ||||||
| import { afterEach, describe, expect, it } from 'vitest'; | ||||||
| import { Terminal } from '@xterm/xterm'; | ||||||
| import { attachMouseModeObserver } from './mouse-mode-observer'; | ||||||
| import { __resetMouseSelectionForTests, getMouseSelectionState } from './mouse-selection'; | ||||||
| import { __resetMouseSelectionForTests, getMouseSelectionState, setOverride, setSelection } from './mouse-selection'; | ||||||
|
|
||||||
| const live: Terminal[] = []; | ||||||
| afterEach(() => { | ||||||
| for (const terminal of live.splice(0)) terminal.dispose(); | ||||||
| __resetMouseSelectionForTests(); | ||||||
| vi.restoreAllMocks(); | ||||||
| }); | ||||||
|
|
||||||
| interface MockDisposables { | ||||||
| setHandlers: Array<() => boolean>; | ||||||
| resetHandlers: Array<() => boolean>; | ||||||
| } | ||||||
|
|
||||||
| function buildMockTerminal(): { terminal: Terminal; modes: { mouseTrackingMode: string; bracketedPasteMode: boolean }; handlers: MockDisposables } { | ||||||
| const handlers: MockDisposables = { setHandlers: [], resetHandlers: [] }; | ||||||
| const modes = { mouseTrackingMode: 'none' as const, bracketedPasteMode: false }; | ||||||
| const parser = { | ||||||
| registerCsiHandler(id: { prefix?: string; final?: string }, cb: () => boolean) { | ||||||
| if (id.prefix === '?' && id.final === 'h') { | ||||||
| handlers.setHandlers.push(cb); | ||||||
| } else if (id.prefix === '?' && id.final === 'l') { | ||||||
| handlers.resetHandlers.push(cb); | ||||||
| } | ||||||
| return { dispose: vi.fn() }; | ||||||
| }, | ||||||
| }; | ||||||
| const terminal = { parser, modes } as unknown as Terminal; | ||||||
| return { terminal, modes, handlers }; | ||||||
| } | ||||||
|
|
||||||
| async function flushMicrotasks(): Promise<void> { | ||||||
| await Promise.resolve(); | ||||||
| await Promise.resolve(); | ||||||
| function setup() { | ||||||
| const terminal = new Terminal({ allowProposedApi: true }); | ||||||
| live.push(terminal); | ||||||
| const observer = attachMouseModeObserver('a', terminal); | ||||||
| return { terminal, observer }; | ||||||
| } | ||||||
| const write = (terminal: Terminal, data: string) => new Promise<void>(resolve => terminal.write(data, resolve)); | ||||||
|
|
||||||
| describe('attachMouseModeObserver', () => { | ||||||
| it('registers one DECSET and one DECRST handler', () => { | ||||||
| const { terminal, handlers } = buildMockTerminal(); | ||||||
| attachMouseModeObserver('a', terminal); | ||||||
| expect(handlers.setHandlers).toHaveLength(1); | ||||||
| expect(handlers.resetHandlers).toHaveLength(1); | ||||||
| }); | ||||||
|
|
||||||
| it('DECSET handler returns false so xterm still processes the sequence', () => { | ||||||
| const { terminal, handlers } = buildMockTerminal(); | ||||||
| attachMouseModeObserver('a', terminal); | ||||||
| expect(handlers.setHandlers[0]()).toBe(false); | ||||||
| expect(handlers.resetHandlers[0]()).toBe(false); | ||||||
| }); | ||||||
|
|
||||||
| it('syncs mouseReporting after a DECSET fires (modes updated in the mock)', async () => { | ||||||
| const { terminal, modes, handlers } = buildMockTerminal(); | ||||||
| attachMouseModeObserver('a', terminal); | ||||||
|
|
||||||
| // Simulate xterm processing `\e[?1000h`: our handler fires, then xterm's | ||||||
| // builtin updates modes. We emulate by flipping the mode before the | ||||||
| // microtask runs. | ||||||
| handlers.setHandlers[0](); | ||||||
| modes.mouseTrackingMode = 'vt200'; | ||||||
|
|
||||||
| await flushMicrotasks(); | ||||||
| expect(getMouseSelectionState('a').mouseReporting).toBe('vt200'); | ||||||
| }); | ||||||
|
|
||||||
| it('syncs bracketedPaste after a DECSET fires', async () => { | ||||||
| const { terminal, modes, handlers } = buildMockTerminal(); | ||||||
| attachMouseModeObserver('a', terminal); | ||||||
|
|
||||||
| handlers.setHandlers[0](); | ||||||
| modes.bracketedPasteMode = true; | ||||||
|
|
||||||
| await flushMicrotasks(); | ||||||
| expect(getMouseSelectionState('a').bracketedPaste).toBe(true); | ||||||
| it.each([[9, 'x10'], [1000, 'vt200'], [1002, 'drag'], [1003, 'any']] as const)( | ||||||
| 'tracks mouse mode %i through real xterm parsing', async (mode, expected) => { | ||||||
| const { terminal } = setup(); | ||||||
| await write(terminal, `[?${mode}h[?2004h`); | ||||||
| expect(getMouseSelectionState('a')).toMatchObject({ mouseReporting: expected, bracketedPaste: true }); | ||||||
| await write(terminal, `[?${mode}l[?2004l`); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| expect(getMouseSelectionState('a')).toMatchObject({ mouseReporting: 'none', bracketedPaste: false }); | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| it('clears reporting, override, program selection, and paste mode after a hard reset', async () => { | ||||||
| const { terminal } = setup(); | ||||||
| await write(terminal, '\x1b[?1003h\x1b[?2004h'); | ||||||
| setOverride('a', 'permanent'); | ||||||
| setSelection('a', { startRow: 0, startCol: 0, endRow: 0, endCol: 4, shape: 'linewise', dragging: false, startedInScrollback: false, owner: 'program' }); | ||||||
| await write(terminal, '\x1bc'); | ||||||
| expect(terminal.modes.mouseTrackingMode).toBe('none'); | ||||||
| expect(terminal.modes.bracketedPasteMode).toBe(false); | ||||||
| expect(getMouseSelectionState('a')).toMatchObject({ mouseReporting: 'none', bracketedPaste: false, override: 'off', selection: null }); | ||||||
| }); | ||||||
|
|
||||||
| it('syncs mouseReporting to none after DECRST', async () => { | ||||||
| const { terminal, modes, handlers } = buildMockTerminal(); | ||||||
| attachMouseModeObserver('a', terminal); | ||||||
|
|
||||||
| // Enable first | ||||||
| handlers.setHandlers[0](); | ||||||
| modes.mouseTrackingMode = 'any'; | ||||||
| await flushMicrotasks(); | ||||||
| expect(getMouseSelectionState('a').mouseReporting).toBe('any'); | ||||||
|
|
||||||
| // Then disable | ||||||
| handlers.resetHandlers[0](); | ||||||
| modes.mouseTrackingMode = 'none'; | ||||||
| await flushMicrotasks(); | ||||||
| expect(getMouseSelectionState('a').mouseReporting).toBe('none'); | ||||||
| it('reads the resolved soft-reset modes instead of assuming mouse tracking ended', async () => { | ||||||
| const { terminal } = setup(); | ||||||
| await write(terminal, '\x1b[?1003h\x1b[?2004h'); | ||||||
| await write(terminal, '\x1b[!p'); | ||||||
| expect(terminal.modes.mouseTrackingMode).toBe('any'); | ||||||
| expect(terminal.modes.bracketedPasteMode).toBe(false); | ||||||
| expect(getMouseSelectionState('a')).toMatchObject({ mouseReporting: 'any', bracketedPaste: false }); | ||||||
| }); | ||||||
|
|
||||||
| it('dispose tears down both handlers', () => { | ||||||
| const disposables: Array<{ dispose: ReturnType<typeof vi.fn> }> = []; | ||||||
| const terminal = { | ||||||
| parser: { | ||||||
| registerCsiHandler() { | ||||||
| const d = { dispose: vi.fn() }; | ||||||
| disposables.push(d); | ||||||
| return d; | ||||||
| }, | ||||||
| }, | ||||||
| modes: { mouseTrackingMode: 'none', bracketedPasteMode: false }, | ||||||
| } as unknown as Terminal; | ||||||
|
|
||||||
| const observer = attachMouseModeObserver('a', terminal); | ||||||
| it('stops updating the store after disposal', async () => { | ||||||
| const { terminal, observer } = setup(); | ||||||
| await write(terminal, '\x1b[?1003h\x1b[?2004h'); | ||||||
| observer.dispose(); | ||||||
|
|
||||||
| expect(disposables).toHaveLength(2); | ||||||
| expect(disposables[0].dispose).toHaveBeenCalledOnce(); | ||||||
| expect(disposables[1].dispose).toHaveBeenCalledOnce(); | ||||||
| await write(terminal, '\x1b[?1003l\x1b[?2004l'); | ||||||
| expect(getMouseSelectionState('a')).toMatchObject({ mouseReporting: 'any', bracketedPaste: true }); | ||||||
| }); | ||||||
| }); | ||||||
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 |
|---|---|---|
| @@ -1,38 +1,12 @@ | ||
| import type { Terminal, IDisposable } from '@xterm/xterm'; | ||
| import { setBracketedPaste, setMouseReporting } from './mouse-selection'; | ||
|
|
||
| /** | ||
| * Wire an xterm terminal's mouse-tracking and bracketed-paste modes into the | ||
| * mouse-selection store. | ||
| * | ||
| * Installs CSI handlers for DECSET (`CSI ? ... h`) and DECRST (`CSI ? ... l`) | ||
| * that return false, letting xterm's built-in handler still process the | ||
| * sequence. After xterm updates its internal state we sync our store from | ||
| * the public `terminal.modes` getters in a microtask (the parser handler | ||
| * runs synchronously before xterm's, so `modes` isn't updated yet when our | ||
| * callback first fires). | ||
| */ | ||
| /** Read resolved modes after parsing, including hard and soft resets. Watching | ||
| * only DECSET/DECRST misses resets, and guessing their effects would duplicate | ||
| * xterm's mode semantics. The store ignores unchanged values. */ | ||
| export function attachMouseModeObserver(id: string, terminal: Terminal): IDisposable { | ||
| const sync = () => { | ||
| queueMicrotask(() => { | ||
| setMouseReporting(id, terminal.modes.mouseTrackingMode); | ||
| setBracketedPaste(id, terminal.modes.bracketedPasteMode); | ||
| }); | ||
| }; | ||
|
|
||
| const onSet = terminal.parser.registerCsiHandler({ prefix: '?', final: 'h' }, () => { | ||
| sync(); | ||
| return false; | ||
| }); | ||
| const onReset = terminal.parser.registerCsiHandler({ prefix: '?', final: 'l' }, () => { | ||
| sync(); | ||
| return false; | ||
| return terminal.onWriteParsed(() => { | ||
| setMouseReporting(id, terminal.modes.mouseTrackingMode); | ||
| setBracketedPaste(id, terminal.modes.bracketedPasteMode); | ||
| }); | ||
|
|
||
| return { | ||
| dispose() { | ||
| onSet.dispose(); | ||
| onReset.dispose(); | ||
| }, | ||
| }; | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raw ESC bytes here are invisible in editors and make
gh pr diffrefuse to print without--allow-escape-sequences; the other tests in this file spell them\x1b.