Skip to content

Conversation

tehreet
Copy link

@tehreet tehreet commented Aug 8, 2025

Summary

This PR implements comprehensive text selection functionality for the textarea component, including mouse and keyboard interaction, visual highlighting, and clipboard operations.

Background

Text selection is a fundamental feature users expect in text input areas. While issue #700 was closed citing terminal compatibility concerns, this implementation successfully provides native selection support that works across modern terminal emulators with Bubble Tea v2's enhanced mouse and rendering capabilities.

Implementation Details

Core Features

  • Mouse Selection

    • Single click: Position cursor
    • Double-click: Select word
    • Triple-click: Select line
    • Click and drag: Select text range
    • Multi-click detection with 500ms threshold
  • Keyboard Selection

    • Shift + Arrow keys: Extend selection
    • Shift + Ctrl + Arrow keys: Select by word
    • Ctrl + A: Select all
    • Escape: Clear selection
  • Clipboard Integration

    • Ctrl + C: Copy selected text
    • Ctrl + X: Cut selected text
    • Ctrl + V: Paste (existing, works with selection)
  • Visual Feedback

    • Selected text highlighted with configurable style
    • Default: inverted colors (gray background, black text)
    • Properly handles wrapped lines and scrolling

Technical Approach

Added selection state to Model struct without breaking existing API. New Position type for row/col coordinates. Public methods for selection management.

Testing

Extensively tested with various terminal emulators, different text content, edge cases, and integration with existing features.

Breaking Changes

None. All existing functionality remains unchanged. Selection features are opt-in.

Example Usage

See examples/textarea-selection/main.go for a complete working example.

References

Notes for Reviewers

While the maintainers previously expressed concerns about terminal compatibility, this implementation leverages Bubble Tea v2's improved mouse handling and works reliably in modern terminals.

- Add mouse selection (click, double-click, triple-click, drag)
- Add keyboard selection (Shift+arrows, Ctrl+A)
- Add clipboard operations (Ctrl+C/X with selection)
- Add visual highlighting for selected text
- Add public methods for programmatic selection

Closes charmbracelet#700

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
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.

1 participant