bmux_tui_components 0.0.1-alpha.1

Reusable terminal UI components for bmux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Higher-level text-input control with opt-in behavior policies.

use std::time::{Duration, Instant};

use bmux_keyboard::{KeyCode, KeyStroke, Modifiers};
use bmux_text_edit::keyboard::TextKeymap;
use bmux_text_edit::{SelectionMode, TextEditBuffer, TextMotion};
use bmux_tui::event::{MouseButton, MouseEvent, MouseEventKind};
use bmux_tui::geometry::Rect;
use unicode_segmentation::UnicodeSegmentation;

const DEFAULT_MULTI_CLICK_WINDOW: Duration = Duration::from_millis(500);
const DEFAULT_MULTI_CLICK_DISTANCE: u16 = 2;

/// Stateful text input data used by [`TextInputControl`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextInputState {
    buffer: TextEditBuffer,
    content_area: Rect,
    vertical_scroll: usize,
    mouse_selection: MouseSelectionState,
}

impl Default for TextInputState {
    fn default() -> Self {
        Self::new(TextEditBuffer::new())
    }
}

impl TextInputState {
    /// Create state around an edit buffer.
    #[must_use]
    pub fn new(buffer: TextEditBuffer) -> Self {
        Self {
            buffer,
            content_area: Rect::new(0, 0, 1, 1),
            vertical_scroll: 0,
            mouse_selection: MouseSelectionState::default(),
        }
    }

    /// Return the edit buffer.
    #[must_use]
    pub const fn buffer(&self) -> &TextEditBuffer {
        &self.buffer
    }

    /// Return the mutable edit buffer.
    pub const fn buffer_mut(&mut self) -> &mut TextEditBuffer {
        &mut self.buffer
    }

    /// Return the latest content area.
    #[must_use]
    pub const fn content_area(&self) -> Rect {
        self.content_area
    }

    /// Return the vertical viewport scroll in wrapped rows.
    #[must_use]
    pub const fn vertical_scroll(&self) -> usize {
        self.vertical_scroll
    }

    /// Store the latest content area.
    pub fn set_content_area(&mut self, area: Rect, policy: &TextInputPolicy) {
        self.content_area = area;
        self.sync_scroll_to_cursor(policy);
    }

    /// Synchronize vertical scroll so the cursor is visible if policy allows.
    pub fn sync_scroll_to_cursor(&mut self, policy: &TextInputPolicy) {
        if !policy.viewport.auto_scroll_to_cursor || self.content_area.height == 0 {
            return;
        }
        self.vertical_scroll = cursor_scroll_offset(&self.buffer, self.content_area);
    }

    /// Return whether a mouse selection drag is active.
    #[must_use]
    pub const fn mouse_selection_active(&self) -> bool {
        !matches!(self.mouse_selection.active, SelectionGranularity::Disabled)
    }
}

/// Stateless text-input event controller.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextInputControl<'policy> {
    policy: &'policy TextInputPolicy,
}

impl<'policy> TextInputControl<'policy> {
    /// Create a control using `policy`.
    #[must_use]
    pub const fn new(policy: &'policy TextInputPolicy) -> Self {
        Self { policy }
    }

    /// Return the configured policy.
    #[must_use]
    pub const fn policy(&self) -> &TextInputPolicy {
        self.policy
    }

    /// Return visible content rows for a terminal width.
    #[must_use]
    pub fn visible_rows_for_width(&self, state: &TextInputState, width: u16) -> u16 {
        let wrapped_rows = state
            .buffer
            .wrapped_layout(usize::from(width.max(1)))
            .lines
            .len()
            .max(1);
        usize_to_u16_saturating(wrapped_rows)
            .max(self.policy.viewport.min_rows.max(1))
            .min(self.policy.viewport.max_rows.unwrap_or(u16::MAX))
    }

    /// Handle one keyboard stroke.
    pub fn handle_key(&self, state: &mut TextInputState, stroke: KeyStroke) -> TextInputOutcome {
        if !self.policy.keyboard.enabled {
            return TextInputOutcome::Ignored;
        }
        if let Some(outcome) = self.handle_enter(state, stroke) {
            return outcome;
        }
        if self.policy.keyboard.selection_keys
            && let Some(motion) = selection_motion(stroke)
        {
            extend_selection(&mut state.buffer, state.content_area, motion);
            state.sync_scroll_to_cursor(self.policy);
            return TextInputOutcome::Edited;
        }
        if let Some(outcome) = self.handle_edge_key(state, stroke) {
            return outcome;
        }
        let Some(command) = self.policy.keyboard.keymap.command_for_key(stroke) else {
            return TextInputOutcome::Ignored;
        };
        state.buffer.apply_command(command);
        state.sync_scroll_to_cursor(self.policy);
        TextInputOutcome::Edited
    }

    /// Handle one mouse event.
    pub fn handle_mouse(&self, state: &mut TextInputState, mouse: MouseEvent) -> TextInputOutcome {
        if !self.policy.mouse.enabled {
            return TextInputOutcome::Ignored;
        }
        match mouse.kind {
            MouseEventKind::Down(MouseButton::Left) if self.policy.mouse.click_to_cursor => {
                self.handle_mouse_down(state, mouse)
            }
            MouseEventKind::Drag(MouseButton::Left) if self.policy.mouse.drag_selection => {
                self.handle_mouse_drag(state, mouse)
            }
            MouseEventKind::Up(MouseButton::Left) if state.mouse_selection_active() => {
                state.mouse_selection.active = SelectionGranularity::Disabled;
                TextInputOutcome::Redraw
            }
            MouseEventKind::Down(
                MouseButton::Left
                | MouseButton::Right
                | MouseButton::Middle
                | MouseButton::Other(_),
            )
            | MouseEventKind::Up(_)
            | MouseEventKind::Drag(_)
            | MouseEventKind::Move
            | MouseEventKind::ScrollUp
            | MouseEventKind::ScrollDown
            | MouseEventKind::ScrollLeft
            | MouseEventKind::ScrollRight => TextInputOutcome::Ignored,
        }
    }

    fn handle_enter(
        &self,
        state: &mut TextInputState,
        stroke: KeyStroke,
    ) -> Option<TextInputOutcome> {
        if stroke.key != KeyCode::Enter {
            return None;
        }
        let behavior = if stroke.modifiers.shift {
            self.policy
                .keyboard
                .shift_enter
                .unwrap_or(self.policy.keyboard.enter)
        } else if stroke.modifiers.is_empty() {
            self.policy.keyboard.enter
        } else {
            return None;
        };
        Some(apply_enter_behavior(state, self.policy, behavior))
    }

    fn handle_edge_key(
        &self,
        state: &TextInputState,
        stroke: KeyStroke,
    ) -> Option<TextInputOutcome> {
        if !stroke.modifiers.is_empty() {
            return None;
        }
        let width = usize::from(state.content_area.width.max(1));
        let layout = state.buffer.wrapped_layout(width);
        match stroke.key {
            KeyCode::Up if layout.cursor.row == 0 && self.policy.edge.up_at_first_row => {
                Some(TextInputOutcome::EdgeUp)
            }
            KeyCode::Down
                if layout.cursor.row.saturating_add(1) >= layout.lines.len()
                    && self.policy.edge.down_at_last_row =>
            {
                Some(TextInputOutcome::EdgeDown)
            }
            KeyCode::Char(_)
            | KeyCode::Enter
            | KeyCode::Tab
            | KeyCode::Backspace
            | KeyCode::Delete
            | KeyCode::Escape
            | KeyCode::Space
            | KeyCode::Up
            | KeyCode::Down
            | KeyCode::Left
            | KeyCode::Right
            | KeyCode::Home
            | KeyCode::End
            | KeyCode::PageUp
            | KeyCode::PageDown
            | KeyCode::Insert
            | KeyCode::F(_) => None,
        }
    }

    fn handle_mouse_down(&self, state: &mut TextInputState, mouse: MouseEvent) -> TextInputOutcome {
        let Some((row, col)) = mouse_wrapped_position(state, mouse) else {
            state.mouse_selection.active = SelectionGranularity::Disabled;
            return TextInputOutcome::Ignored;
        };
        let count = state
            .mouse_selection
            .click_count(mouse.position.x, mouse.position.y);
        let granularity = self.policy.mouse.granularity_for_click_count(count);
        state.mouse_selection.active = if self.policy.mouse.drag_selection {
            granularity
        } else {
            SelectionGranularity::Disabled
        };
        apply_selection_granularity(&mut state.buffer, state.content_area, row, col, granularity);
        state.sync_scroll_to_cursor(self.policy);
        TextInputOutcome::Redraw
    }

    fn handle_mouse_drag(&self, state: &mut TextInputState, mouse: MouseEvent) -> TextInputOutcome {
        let Some((row, col)) = mouse_wrapped_position(state, mouse) else {
            return TextInputOutcome::Ignored;
        };
        extend_selection_to_granularity(
            &mut state.buffer,
            state.content_area,
            row,
            col,
            state.mouse_selection.active,
        );
        state.sync_scroll_to_cursor(self.policy);
        TextInputOutcome::Redraw
    }
}

/// Configurable text-input behavior policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextInputPolicy {
    /// Keyboard behavior.
    pub keyboard: KeyboardPolicy,
    /// Mouse behavior.
    pub mouse: MousePolicy,
    /// Viewport behavior.
    pub viewport: ViewportPolicy,
    /// Edge signal behavior.
    pub edge: EdgePolicy,
}

impl Default for TextInputPolicy {
    fn default() -> Self {
        Self::raw()
    }
}

impl TextInputPolicy {
    /// Raw policy with all higher-level handling disabled.
    #[must_use]
    pub const fn raw() -> Self {
        Self {
            keyboard: KeyboardPolicy::disabled(),
            mouse: MousePolicy::disabled(),
            viewport: ViewportPolicy::raw(),
            edge: EdgePolicy::disabled(),
        }
    }

    /// Common chat-composer policy.
    #[must_use]
    pub const fn chat_composer() -> Self {
        Self {
            keyboard: KeyboardPolicy::chat_composer(),
            mouse: MousePolicy::text_selection(),
            viewport: ViewportPolicy {
                auto_scroll_to_cursor: true,
                min_rows: 1,
                max_rows: Some(6),
            },
            edge: EdgePolicy {
                up_at_first_row: true,
                down_at_last_row: true,
            },
        }
    }
}

/// Keyboard behavior policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyboardPolicy {
    /// Whether keyboard handling is enabled.
    pub enabled: bool,
    /// Standard edit keymap.
    pub keymap: TextKeymap,
    /// Enter key behavior.
    pub enter: EnterBehavior,
    /// Shift+Enter behavior.
    pub shift_enter: Option<EnterBehavior>,
    /// Whether shift-selection bindings are handled.
    pub selection_keys: bool,
}

impl KeyboardPolicy {
    /// Disabled keyboard handling.
    #[must_use]
    pub const fn disabled() -> Self {
        Self {
            enabled: false,
            keymap: TextKeymap {
                profile: bmux_text_edit::keyboard::TextInputProfile::Readline,
                boundary_policy: bmux_text_edit::TextBoundaryPolicy::Buffer,
            },
            enter: EnterBehavior::Ignore,
            shift_enter: None,
            selection_keys: false,
        }
    }

    /// Common chat-composer keyboard handling.
    #[must_use]
    pub const fn chat_composer() -> Self {
        Self {
            enabled: true,
            keymap: TextKeymap {
                profile: bmux_text_edit::keyboard::TextInputProfile::Readline,
                boundary_policy: bmux_text_edit::TextBoundaryPolicy::Buffer,
            },
            enter: EnterBehavior::Submit,
            shift_enter: Some(EnterBehavior::InsertNewline),
            selection_keys: true,
        }
    }
}

/// Enter-key behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnterBehavior {
    /// Do not handle enter.
    Ignore,
    /// Insert a newline into the buffer.
    InsertNewline,
    /// Emit a submit outcome.
    Submit,
}

/// Mouse behavior policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MousePolicy {
    /// Whether mouse handling is enabled.
    pub enabled: bool,
    /// Whether clicks place the cursor/select text.
    pub click_to_cursor: bool,
    /// Whether dragging extends selection.
    pub drag_selection: bool,
    /// Double-click selection behavior.
    pub double_click: Option<SelectionGranularity>,
    /// Triple-click selection behavior.
    pub triple_click: Option<SelectionGranularity>,
}

impl MousePolicy {
    /// Disabled mouse handling.
    #[must_use]
    pub const fn disabled() -> Self {
        Self {
            enabled: false,
            click_to_cursor: false,
            drag_selection: false,
            double_click: None,
            triple_click: None,
        }
    }

    /// Text-selection mouse behavior.
    #[must_use]
    pub const fn text_selection() -> Self {
        Self {
            enabled: true,
            click_to_cursor: true,
            drag_selection: true,
            double_click: Some(SelectionGranularity::Word),
            triple_click: Some(SelectionGranularity::All),
        }
    }

    const fn granularity_for_click_count(self, count: u8) -> SelectionGranularity {
        match count {
            3.. => option_granularity_or(self.triple_click, SelectionGranularity::Character),
            2 => option_granularity_or(self.double_click, SelectionGranularity::Character),
            _ => SelectionGranularity::Character,
        }
    }
}

/// Viewport behavior policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ViewportPolicy {
    /// Whether viewport scroll follows the cursor.
    pub auto_scroll_to_cursor: bool,
    /// Minimum visible rows.
    pub min_rows: u16,
    /// Maximum visible rows.
    pub max_rows: Option<u16>,
}

impl ViewportPolicy {
    /// Raw viewport behavior.
    #[must_use]
    pub const fn raw() -> Self {
        Self {
            auto_scroll_to_cursor: false,
            min_rows: 1,
            max_rows: None,
        }
    }
}

/// Edge signal behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EdgePolicy {
    /// Emit [`TextInputOutcome::EdgeUp`] when up is pressed on the first row.
    pub up_at_first_row: bool,
    /// Emit [`TextInputOutcome::EdgeDown`] when down is pressed on the last row.
    pub down_at_last_row: bool,
}

impl EdgePolicy {
    /// Disabled edge signals.
    #[must_use]
    pub const fn disabled() -> Self {
        Self {
            up_at_first_row: false,
            down_at_last_row: false,
        }
    }
}

/// Selection granularity for mouse actions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionGranularity {
    /// Select by character/cell hit target.
    Character,
    /// Select whole words.
    Word,
    /// Select the entire buffer.
    All,
    /// Disable active selection extension.
    Disabled,
}

/// Outcome from handling input.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextInputOutcome {
    /// Event was ignored.
    Ignored,
    /// Buffer/cursor/selection changed.
    Edited,
    /// Redraw requested without a text edit.
    Redraw,
    /// Submit was requested.
    Submitted,
    /// Up was pressed at the first visual row.
    EdgeUp,
    /// Down was pressed at the last visual row.
    EdgeDown,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct MouseSelectionState {
    last_click: Option<MouseClickState>,
    active: SelectionGranularity,
}

impl Default for MouseSelectionState {
    fn default() -> Self {
        Self {
            last_click: None,
            active: SelectionGranularity::Disabled,
        }
    }
}

impl MouseSelectionState {
    fn click_count(&mut self, x: u16, y: u16) -> u8 {
        let now = Instant::now();
        let count = self.last_click.map_or(1, |last| {
            let near = last.x.abs_diff(x) <= DEFAULT_MULTI_CLICK_DISTANCE
                && last.y.abs_diff(y) <= DEFAULT_MULTI_CLICK_DISTANCE;
            let quick = now.saturating_duration_since(last.at) <= DEFAULT_MULTI_CLICK_WINDOW;
            if near && quick {
                last.count.saturating_add(1)
            } else {
                1
            }
        });
        let capped = count.min(3);
        self.last_click = Some(MouseClickState {
            x,
            y,
            at: now,
            count: capped,
        });
        capped
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct MouseClickState {
    x: u16,
    y: u16,
    at: Instant,
    count: u8,
}

const fn option_granularity_or(
    value: Option<SelectionGranularity>,
    fallback: SelectionGranularity,
) -> SelectionGranularity {
    match value {
        Some(value) => value,
        None => fallback,
    }
}

fn apply_enter_behavior(
    state: &mut TextInputState,
    policy: &TextInputPolicy,
    behavior: EnterBehavior,
) -> TextInputOutcome {
    match behavior {
        EnterBehavior::Ignore => TextInputOutcome::Ignored,
        EnterBehavior::InsertNewline => {
            state.buffer.insert_newline();
            state.sync_scroll_to_cursor(policy);
            TextInputOutcome::Edited
        }
        EnterBehavior::Submit => TextInputOutcome::Submitted,
    }
}

const fn selection_motion(stroke: KeyStroke) -> Option<TextMotion> {
    let Modifiers {
        ctrl,
        alt,
        shift,
        super_key,
        hyper,
        meta,
    } = stroke.modifiers;
    if !shift || super_key || hyper || meta {
        return None;
    }
    match stroke.key {
        KeyCode::Left if ctrl || alt => Some(TextMotion::WordLeft),
        KeyCode::Right if ctrl || alt => Some(TextMotion::WordRight),
        KeyCode::Left => Some(TextMotion::Left),
        KeyCode::Right => Some(TextMotion::Right),
        KeyCode::Up => Some(TextMotion::VisualUp),
        KeyCode::Down => Some(TextMotion::VisualDown),
        KeyCode::Char(_)
        | KeyCode::Enter
        | KeyCode::Tab
        | KeyCode::Backspace
        | KeyCode::Delete
        | KeyCode::Escape
        | KeyCode::Space
        | KeyCode::Home
        | KeyCode::End
        | KeyCode::PageUp
        | KeyCode::PageDown
        | KeyCode::Insert
        | KeyCode::F(_) => None,
    }
}

fn extend_selection(buffer: &mut TextEditBuffer, area: Rect, motion: TextMotion) {
    match motion {
        TextMotion::VisualUp => extend_visual_selection(buffer, area, -1),
        TextMotion::VisualDown => extend_visual_selection(buffer, area, 1),
        motion => buffer.move_cursor_with_selection(motion, SelectionMode::Extend),
    }
}

fn extend_visual_selection(buffer: &mut TextEditBuffer, area: Rect, delta: isize) {
    let width = usize::from(area.width.max(1));
    let layout = buffer.wrapped_layout(width);
    let target_row = if delta.is_negative() {
        layout.cursor.row.saturating_sub(delta.unsigned_abs())
    } else {
        layout
            .cursor
            .row
            .saturating_add(delta.unsigned_abs())
            .min(layout.lines.len().saturating_sub(1))
    };
    buffer.select_to_wrapped_position(width, target_row, layout.cursor.col);
}

fn cursor_scroll_offset(buffer: &TextEditBuffer, area: Rect) -> usize {
    if area.height == 0 {
        return 0;
    }
    let layout = buffer.wrapped_layout(usize::from(area.width.max(1)));
    layout
        .cursor
        .row
        .saturating_add(1)
        .saturating_sub(usize::from(area.height))
}

fn mouse_wrapped_position(state: &TextInputState, mouse: MouseEvent) -> Option<(usize, usize)> {
    let area = state.content_area;
    if mouse.position.y < area.y || mouse.position.y >= area.bottom() {
        return None;
    }
    if mouse.position.x < area.x || mouse.position.x >= area.right() {
        return None;
    }
    Some((
        usize::from(mouse.position.y.saturating_sub(area.y)).saturating_add(state.vertical_scroll),
        usize::from(mouse.position.x.saturating_sub(area.x)),
    ))
}

fn apply_selection_granularity(
    buffer: &mut TextEditBuffer,
    area: Rect,
    row: usize,
    col: usize,
    granularity: SelectionGranularity,
) {
    let width = usize::from(area.width.max(1));
    let byte_index = buffer.byte_index_for_wrapped_position(width, row, col);
    match granularity {
        SelectionGranularity::Character | SelectionGranularity::Disabled => {
            buffer.move_cursor(TextMotion::Absolute(byte_index));
        }
        SelectionGranularity::Word => select_word_at(buffer, byte_index),
        SelectionGranularity::All => buffer.select_all(),
    }
}

fn extend_selection_to_granularity(
    buffer: &mut TextEditBuffer,
    area: Rect,
    row: usize,
    col: usize,
    granularity: SelectionGranularity,
) {
    let width = usize::from(area.width.max(1));
    let byte_index = buffer.byte_index_for_wrapped_position(width, row, col);
    match granularity {
        SelectionGranularity::Character => {
            buffer.move_cursor_with_selection(
                TextMotion::Absolute(byte_index),
                SelectionMode::Extend,
            );
        }
        SelectionGranularity::Word => {
            let target =
                word_range_at(buffer.text(), byte_index).map_or(byte_index, |(_, end)| end);
            buffer.move_cursor_with_selection(TextMotion::Absolute(target), SelectionMode::Extend);
        }
        SelectionGranularity::All => buffer.select_all(),
        SelectionGranularity::Disabled => {}
    }
}

fn select_word_at(buffer: &mut TextEditBuffer, byte_index: usize) {
    if let Some((start, end)) = word_range_at(buffer.text(), byte_index) {
        buffer.move_cursor(TextMotion::Absolute(start));
        buffer.move_cursor_with_selection(TextMotion::Absolute(end), SelectionMode::Extend);
    } else {
        buffer.move_cursor(TextMotion::Absolute(byte_index));
    }
}

fn word_range_at(text: &str, byte_index: usize) -> Option<(usize, usize)> {
    if text.is_empty() {
        return None;
    }
    let index = byte_index.min(text.len());
    let ranges = text
        .grapheme_indices(true)
        .map(|(start, grapheme)| (start, start.saturating_add(grapheme.len()), grapheme))
        .collect::<Vec<_>>();
    if ranges.is_empty() {
        return None;
    }
    let mut position = ranges
        .iter()
        .position(|(start, end, _)| index >= *start && index < *end)
        .unwrap_or_else(|| ranges.len().saturating_sub(1));
    if ranges[position].2.chars().all(char::is_whitespace) {
        position = ranges
            .iter()
            .enumerate()
            .skip(position)
            .find(|(_, (_, _, grapheme))| !grapheme.chars().all(char::is_whitespace))
            .map_or(position, |(index, _)| index);
    }
    if ranges[position].2.chars().all(char::is_whitespace) {
        return None;
    }
    let mut start_position = position;
    while start_position > 0 && is_word_grapheme(ranges[start_position - 1].2) {
        start_position -= 1;
    }
    let mut end_position = position;
    while end_position + 1 < ranges.len() && is_word_grapheme(ranges[end_position + 1].2) {
        end_position += 1;
    }
    Some((ranges[start_position].0, ranges[end_position].1))
}

fn is_word_grapheme(grapheme: &str) -> bool {
    grapheme.chars().any(|ch| ch.is_alphanumeric() || ch == '_')
}

fn usize_to_u16_saturating(value: usize) -> u16 {
    u16::try_from(value).unwrap_or(u16::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use bmux_tui::geometry::Point;

    fn key(key: KeyCode) -> KeyStroke {
        KeyStroke::simple(key)
    }

    fn shift_key(key: KeyCode) -> KeyStroke {
        KeyStroke::with_modifiers(
            key,
            Modifiers {
                shift: true,
                ..Modifiers::NONE
            },
        )
    }

    fn mouse(kind: MouseEventKind, x: u16, y: u16) -> MouseEvent {
        MouseEvent::new(kind, Point::new(x, y))
    }

    #[test]
    fn raw_policy_ignores_keyboard_and_mouse() {
        let policy = TextInputPolicy::raw();
        let control = TextInputControl::new(&policy);
        let mut state = TextInputState::new(TextEditBuffer::from_text("hello"));
        state.set_content_area(Rect::new(0, 0, 20, 1), &policy);

        assert_eq!(
            control.handle_key(&mut state, key(KeyCode::Left)),
            TextInputOutcome::Ignored
        );
        assert_eq!(
            control.handle_mouse(
                &mut state,
                mouse(MouseEventKind::Down(MouseButton::Left), 1, 0)
            ),
            TextInputOutcome::Ignored
        );
        assert_eq!(state.buffer().cursor_byte_index(), "hello".len());
    }

    #[test]
    fn shift_selection_extends_buffer_selection() {
        let policy = TextInputPolicy::chat_composer();
        let control = TextInputControl::new(&policy);
        let mut state = TextInputState::new(TextEditBuffer::from_text("hello"));
        state.set_content_area(Rect::new(0, 0, 20, 1), &policy);

        assert_eq!(
            control.handle_key(&mut state, shift_key(KeyCode::Left)),
            TextInputOutcome::Edited
        );
        assert_eq!(state.buffer().selected_text(), Some("o".to_string()));
    }

    #[test]
    fn edge_keys_emit_history_outcomes() {
        let policy = TextInputPolicy::chat_composer();
        let control = TextInputControl::new(&policy);
        let mut state = TextInputState::new(TextEditBuffer::from_text("hello"));
        state.set_content_area(Rect::new(0, 0, 20, 1), &policy);

        assert_eq!(
            control.handle_key(&mut state, key(KeyCode::Down)),
            TextInputOutcome::EdgeDown
        );
        state.buffer_mut().move_cursor(TextMotion::Start);
        assert_eq!(
            control.handle_key(&mut state, key(KeyCode::Up)),
            TextInputOutcome::EdgeUp
        );
    }

    #[test]
    fn double_click_selects_word_and_triple_click_selects_all() {
        let policy = TextInputPolicy::chat_composer();
        let control = TextInputControl::new(&policy);
        let mut state = TextInputState::new(TextEditBuffer::from_text("hello world"));
        state.set_content_area(Rect::new(0, 0, 20, 1), &policy);

        let _ = control.handle_mouse(
            &mut state,
            mouse(MouseEventKind::Down(MouseButton::Left), 1, 0),
        );
        let _ = control.handle_mouse(
            &mut state,
            mouse(MouseEventKind::Down(MouseButton::Left), 1, 0),
        );
        assert_eq!(state.buffer().selected_text(), Some("hello".to_string()));

        let _ = control.handle_mouse(
            &mut state,
            mouse(MouseEventKind::Down(MouseButton::Left), 1, 0),
        );
        assert_eq!(
            state.buffer().selected_text(),
            Some("hello world".to_string())
        );
    }

    #[test]
    fn drag_extends_selection() {
        let policy = TextInputPolicy::chat_composer();
        let control = TextInputControl::new(&policy);
        let mut state = TextInputState::new(TextEditBuffer::from_text("hello world"));
        state.set_content_area(Rect::new(0, 0, 20, 1), &policy);

        let _ = control.handle_mouse(
            &mut state,
            mouse(MouseEventKind::Down(MouseButton::Left), 0, 0),
        );
        let _ = control.handle_mouse(
            &mut state,
            mouse(MouseEventKind::Drag(MouseButton::Left), 5, 0),
        );
        assert_eq!(state.buffer().selected_text(), Some("hello".to_string()));
    }
}