Skip to content

Conversation

@gnodet
Copy link
Member

@gnodet gnodet commented Oct 26, 2025

Problem

JLine3's mouse functionality has a fundamental conflict where enabling one mouse feature disables the other:

  • Option 1: JLine Mouse Mode - Text selection works ✅, but cursor positioning doesn't ❌
  • Option 2: Terminal Mouse Tracking - Cursor positioning works ✅, but text selection doesn't ❌

This is because when Terminal.trackMouse() is enabled, it intercepts all mouse events at the terminal level, preventing the terminal from allowing standard text selection.

Solution

Implement a context-aware dynamic mouse tracking strategy:

  • During input (readLine): Disable terminal mouse tracking (Terminal.MouseTracking.Off)

    • Allows standard text selection (drag to select, copy/paste)
    • LineReader's mouse widget still works through key bindings
  • Between prompts (after readLine): Enable terminal mouse tracking (Terminal.MouseTracking.Button)

    • Allows users to click to position the cursor
    • Users can click anywhere in the terminal before the next prompt

Changes

LineReaderImpl.java

  • Line 686-688: Disable terminal mouse tracking when entering readLine

    • Allows text selection during input
    • Comment explains the behavior
  • Line 2668-2671: Re-enable terminal mouse tracking when exiting readLine

    • Allows cursor positioning between prompts
    • Comment explains the behavior

LineReaderMouseExample.java

  • Removed explicit terminal.trackMouse() call
  • Updated documentation to reflect new behavior
  • Added feature list showing both capabilities

Benefits

Both features work simultaneously: Users can select text AND position cursor with clicks
No breaking changes: Existing code continues to work
Backward compatible: LineReader.MOUSE option still controls the feature
All tests pass: Full build and test suite passes successfully

Usage

Simply enable the MOUSE option in LineReader:

LineReader reader = LineReaderBuilder.builder()
    .terminal(terminal)
    .variable(LineReader.MOUSE, true)  // Enable mouse support
    .build();

// Now users can:
// - Drag to select text for copy/paste
// - Click to position cursor between prompts

No need to manually call terminal.trackMouse() - the LineReader handles it automatically!

Testing

  • Full build and test suite passes: ./mvx build
  • All existing tests continue to pass
  • No new test failures introduced

Fixes #1480


Pull Request opened by Augment Code with guidance from the PR author

…sitioning

This fix resolves issue #1480 where enabling one mouse feature disabled the other.

The solution implements a context-aware dynamic mouse tracking strategy:
- During input (readLine): Disable terminal mouse tracking to allow text selection
- Between prompts (after readLine): Enable terminal mouse tracking to allow cursor positioning

This allows users to:
- Drag to select text for copy/paste operations
- Click to position the cursor between prompts

The LineReader's mouse widget continues to work through key bindings regardless
of terminal mouse tracking state.

Changes:
- LineReaderImpl: Disable mouse tracking on readLine entry, re-enable on exit
- LineReaderMouseExample: Updated example to reflect new behavior

All existing tests pass. No breaking changes.
@gnodet gnodet force-pushed the fix/mouse-tracking-conflict branch from a9a1908 to 95f5a4c Compare November 12, 2025 09:39
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.

Mouse tracking conflict: Cannot use both text selection and cursor positioning simultaneously

2 participants