rpi-tui 0.3.2

Terminal User Interface for pi-rust
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
//! Input component - single-line text input with horizontal scrolling.
//!
//! Provides a text input field with cursor positioning, selection,
//! undo/redo, and Emacs-style editing.

use std::any::Any;
use std::sync::{Arc, Mutex};

use super::component::{Component, Focusable};
use super::keybindings::Keybindings;
use super::kill_ring::KillRing;
use super::undo_stack::UndoStack;
use super::word_navigation::{find_word_backward, find_word_forward};
use crate::ansi::CURSOR_MARKER;
use crate::utils::{slice_by_column, visible_width};

/// Byte offset of the `char_index`-th character, clamped to the end of the
/// string.
///
/// The cursor is a *character* index (matching `word_navigation`, which also
/// indexes by char) but `&str` can only be sliced at byte offsets, so every
/// splice has to go through this mapping. Indexing a `&str` directly by the
/// char index both panicked on non-ASCII input and desynced the caret.
fn byte_offset(value: &str, char_index: usize) -> usize {
    value
        .char_indices()
        .nth(char_index)
        .map(|(offset, _)| offset)
        .unwrap_or(value.len())
}

/// Number of characters in `value`.
fn char_count(value: &str) -> usize {
    value.chars().count()
}

/// Input state for undo.
#[derive(Debug, Clone)]
struct InputState {
    value: String,
    cursor: usize,
}

impl Default for InputState {
    fn default() -> Self {
        Self {
            value: String::new(),
            cursor: 0,
        }
    }
}

/// Last action type for kill ring coalescing.
#[derive(Debug, Clone, Copy, PartialEq)]
enum LastAction {
    None,
    Kill,
    Yank,
    TypeWord,
}

/// Input component - single-line text input.
pub struct Input {
    state: Mutex<InputState>,
    prompt: String,
    placeholder: Option<String>,
    #[allow(dead_code)]
    keybindings: Arc<Keybindings>,
    kill_ring: Mutex<KillRing>,
    undo_stack: Mutex<UndoStack<InputState>>,
    last_action: Mutex<LastAction>,
    focused: Mutex<bool>,
    on_submit: Mutex<Option<Arc<dyn Fn(&str) + Send + Sync>>>,
    on_escape: Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
    on_change: Mutex<Option<Arc<dyn Fn(&str) + Send + Sync>>>,
}

impl Input {
    /// Create a new input component.
    pub fn new() -> Self {
        Self {
            state: Mutex::new(InputState::default()),
            prompt: "> ".to_string(),
            placeholder: None,
            keybindings: Arc::new(Keybindings::new()),
            kill_ring: Mutex::new(KillRing::new()),
            undo_stack: Mutex::new(UndoStack::new()),
            last_action: Mutex::new(LastAction::None),
            focused: Mutex::new(false),
            on_submit: Mutex::new(None),
            on_escape: Mutex::new(None),
            on_change: Mutex::new(None),
        }
    }

    /// Create an input with a custom prompt.
    pub fn with_prompt(prompt: &str) -> Self {
        let mut input = Self::new();
        input.prompt = prompt.to_string();
        input
    }

    /// Create an input with a placeholder.
    pub fn with_placeholder(placeholder: &str) -> Self {
        let mut input = Self::new();
        input.placeholder = Some(placeholder.to_string());
        input
    }

    /// Get the current value.
    pub fn get_value(&self) -> String {
        self.state
            .lock()
            .map(|s| s.value.clone())
            .unwrap_or_default()
    }

    /// Set the value.
    pub fn set_value(&self, value: &str) {
        if let Ok(mut state) = self.state.lock() {
            state.value = value.to_string();
            // Character index, not byte length: the cursor is char-counted.
            state.cursor = char_count(value);
        }
    }

    /// Clear the input.
    pub fn clear(&self) {
        if let Ok(mut state) = self.state.lock() {
            state.value.clear();
            state.cursor = 0;
        }
    }

    /// Set callback for when input is submitted (Enter pressed).
    pub fn on_submit(&self, callback: Arc<dyn Fn(&str) + Send + Sync>) {
        if let Ok(mut cb) = self.on_submit.lock() {
            *cb = Some(callback);
        }
    }

    /// Set callback for when escape is pressed.
    pub fn on_escape(&self, callback: Arc<dyn Fn() + Send + Sync>) {
        if let Ok(mut cb) = self.on_escape.lock() {
            *cb = Some(callback);
        }
    }

    /// Set callback for when value changes.
    pub fn on_change(&self, callback: Arc<dyn Fn(&str) + Send + Sync>) {
        if let Ok(mut cb) = self.on_change.lock() {
            *cb = Some(callback);
        }
    }

    /// Handle bracketed paste.
    #[allow(dead_code)]
    fn handle_paste(&self, pasted_text: &str) {
        self.set_last_action(LastAction::None);

        // Clean the pasted text - remove newlines and tabs
        let clean_text = pasted_text
            .replace("\r\n", "")
            .replace("\r", "")
            .replace("\n", "")
            .replace("\t", "    ");

        self.edit(false, |state| {
            if clean_text.is_empty() {
                return false;
            }
            let at = byte_offset(&state.value, state.cursor);
            state.value.insert_str(at, &clean_text);
            state.cursor += clean_text.chars().count();
            true
        });
    }

    /// Insert a character at the cursor.
    fn insert_character(&self, char: &str) {
        // Undo coalescing: consecutive word chars coalesce into one undo unit
        let whitespace = char
            .chars()
            .next()
            .map(|c| c.is_whitespace())
            .unwrap_or(true);
        let coalesce = !whitespace && self.get_last_action() == LastAction::TypeWord;
        self.set_last_action(LastAction::TypeWord);

        self.edit(coalesce, |state| {
            if char.is_empty() {
                return false;
            }
            let at = byte_offset(&state.value, state.cursor);
            state.value.insert_str(at, char);
            state.cursor += char.chars().count();
            true
        });
    }

    /// Handle backspace (delete character before cursor).
    fn handle_backspace(&self) {
        self.set_last_action(LastAction::None);
        self.edit(false, |state| {
            if state.cursor == 0 {
                return false;
            }
            let start = byte_offset(&state.value, state.cursor - 1);
            let end = byte_offset(&state.value, state.cursor);
            state.value.replace_range(start..end, "");
            state.cursor -= 1;
            true
        });
    }

    /// Handle forward delete (delete character after cursor).
    fn handle_forward_delete(&self) {
        self.set_last_action(LastAction::None);
        self.edit(false, |state| {
            if state.cursor >= char_count(&state.value) {
                return false;
            }
            let start = byte_offset(&state.value, state.cursor);
            let end = byte_offset(&state.value, state.cursor + 1);
            state.value.replace_range(start..end, "");
            true
        });
    }

    /// Delete to the start of the line.
    fn delete_to_line_start(&self) {
        self.edit(false, |state| {
            if state.cursor == 0 {
                return false;
            }
            let at = byte_offset(&state.value, state.cursor);
            let deleted = state.value[..at].to_string();
            if let Ok(mut kill_ring) = self.kill_ring.lock() {
                use super::kill_ring::PushOptions;
                let accumulate = self.get_last_action() == LastAction::Kill;
                kill_ring.push(
                    &deleted,
                    PushOptions {
                        prepend: true,
                        accumulate,
                    },
                );
            }
            self.set_last_action(LastAction::Kill);
            state.value.replace_range(..at, "");
            state.cursor = 0;
            true
        });
    }

    /// Delete to the end of the line.
    fn delete_to_line_end(&self) {
        self.edit(false, |state| {
            if state.cursor >= char_count(&state.value) {
                return false;
            }
            let at = byte_offset(&state.value, state.cursor);
            let deleted = state.value[at..].to_string();
            if let Ok(mut kill_ring) = self.kill_ring.lock() {
                use super::kill_ring::PushOptions;
                let accumulate = self.get_last_action() == LastAction::Kill;
                kill_ring.push(
                    &deleted,
                    PushOptions {
                        prepend: false,
                        accumulate,
                    },
                );
            }
            self.set_last_action(LastAction::Kill);
            state.value.truncate(at);
            true
        });
    }

    /// Delete word backwards.
    fn delete_word_backwards(&self) {
        self.edit(false, |state| {
            if state.cursor == 0 {
                return false;
            }
            let was_kill = self.get_last_action() == LastAction::Kill;
            let old_cursor = state.cursor;
            let new_cursor = find_word_backward(&state.value, old_cursor);
            let start = byte_offset(&state.value, new_cursor);
            let end = byte_offset(&state.value, old_cursor);
            let deleted = state.value[start..end].to_string();
            if let Ok(mut kill_ring) = self.kill_ring.lock() {
                use super::kill_ring::PushOptions;
                kill_ring.push(
                    &deleted,
                    PushOptions {
                        prepend: true,
                        accumulate: was_kill,
                    },
                );
            }
            self.set_last_action(LastAction::Kill);
            state.value.replace_range(start..end, "");
            state.cursor = new_cursor;
            true
        });
    }

    /// Delete word forward.
    fn delete_word_forward(&self) {
        self.edit(false, |state| {
            if state.cursor >= char_count(&state.value) {
                return false;
            }
            let was_kill = self.get_last_action() == LastAction::Kill;
            let old_cursor = state.cursor;
            let new_cursor = find_word_forward(&state.value, old_cursor);
            let start = byte_offset(&state.value, old_cursor);
            let end = byte_offset(&state.value, new_cursor);
            let deleted = state.value[start..end].to_string();
            if let Ok(mut kill_ring) = self.kill_ring.lock() {
                use super::kill_ring::PushOptions;
                kill_ring.push(
                    &deleted,
                    PushOptions {
                        prepend: false,
                        accumulate: was_kill,
                    },
                );
            }
            self.set_last_action(LastAction::Kill);
            state.value.replace_range(start..end, "");
            state.cursor = old_cursor;
            true
        });
    }

    /// Yank from kill ring.
    fn yank(&self) {
        let text = match self.kill_ring.lock() {
            Ok(kill_ring) => kill_ring.peek().map(|s| s.to_string()),
            Err(_) => None,
        };

        if let Some(text) = text {
            self.set_last_action(LastAction::Yank);
            self.edit(false, |state| {
                let at = byte_offset(&state.value, state.cursor);
                state.value.insert_str(at, &text);
                state.cursor += text.chars().count();
                true
            });
        }
    }

    /// Yank pop (cycle through kill ring).
    #[allow(dead_code)]
    fn yank_pop(&self) {
        if self.get_last_action() != LastAction::Yank {
            return;
        }

        // Rotate the kill ring before touching the state lock so the ring is
        // never held across an edit.
        let (previous, replacement) = {
            let Ok(mut kill_ring) = self.kill_ring.lock() else {
                return;
            };
            if kill_ring.len() <= 1 {
                return;
            }
            let previous = kill_ring.peek().unwrap_or("").to_string();
            kill_ring.rotate();
            let replacement = kill_ring.peek().unwrap_or("").to_string();
            (previous, replacement)
        };

        self.edit(false, |state| {
            let previous_len = previous.chars().count();
            if state.cursor < previous_len {
                return false;
            }
            // Remove the previously yanked text, then insert the next candidate.
            let start = byte_offset(&state.value, state.cursor - previous_len);
            let end = byte_offset(&state.value, state.cursor);
            state.value.replace_range(start..end, &replacement);
            state.cursor = state.cursor - previous_len + replacement.chars().count();
            true
        });
        self.set_last_action(LastAction::Yank);
    }

    /// Move cursor left.
    fn move_cursor_left(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            if state.cursor > 0 {
                state.cursor -= 1;
            }
        }
    }

    /// Move cursor right.
    fn move_cursor_right(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            if state.cursor < char_count(&state.value) {
                state.cursor += 1;
            }
        }
    }

    /// Move cursor to line start.
    fn move_to_line_start(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            state.cursor = 0;
        }
    }

    /// Move cursor to line end.
    fn move_to_line_end(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            state.cursor = char_count(&state.value);
        }
    }

    /// Move cursor word left.
    fn move_word_left(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            state.cursor = find_word_backward(&state.value, state.cursor);
        }
    }

    /// Move cursor word right.
    fn move_word_right(&self) {
        self.set_last_action(LastAction::None);
        if let Ok(mut state) = self.state.lock() {
            state.cursor = find_word_forward(&state.value, state.cursor);
        }
    }

    /// Undo.
    fn undo(&self) {
        if let Ok(mut undo_stack) = self.undo_stack.lock() {
            if let Some(snapshot) = undo_stack.pop() {
                drop(undo_stack);
                if let Ok(mut state) = self.state.lock() {
                    *state = snapshot;
                }
                self.set_last_action(LastAction::None);
                self.notify_change();
            }
        }
    }

    /// Apply a mutation to the input state, record an undo snapshot, and notify
    /// listeners.
    ///
    /// The undo snapshot is taken while the state lock is held but pushed to the
    /// undo stack *after* releasing it, and `notify_change` (which calls
    /// `get_value` ⇒ `state.lock()`) runs last. Both used to run nested inside the
    /// state lock, and `std::sync::Mutex` is not reentrant — so the first
    /// backspace (every deletion path, actually) deadlocked the whole TUI and
    /// wedged the `ask_user` freeform input.
    ///
    /// `coalesce` skips the snapshot so consecutive keystrokes of a word merge
    /// into a single undo unit.
    fn edit(&self, coalesce: bool, mutate: impl FnOnce(&mut InputState) -> bool) {
        let snapshot = {
            let Ok(mut state) = self.state.lock() else {
                return;
            };
            let snapshot = if coalesce {
                None
            } else {
                Some(InputState {
                    value: state.value.clone(),
                    cursor: state.cursor,
                })
            };
            if !mutate(&mut state) {
                return;
            }
            snapshot
        };
        if let Some(snapshot) = snapshot {
            if let Ok(mut undo_stack) = self.undo_stack.lock() {
                undo_stack.push(snapshot);
            }
        }
        self.notify_change();
    }

    /// Get the last action.
    fn get_last_action(&self) -> LastAction {
        self.last_action
            .lock()
            .map(|a| *a)
            .unwrap_or(LastAction::None)
    }

    /// Set the last action.
    fn set_last_action(&self, action: LastAction) {
        if let Ok(mut a) = self.last_action.lock() {
            *a = action;
        }
    }

    /// Notify change callback.
    fn notify_change(&self) {
        if let Ok(cb) = self.on_change.lock() {
            if let Some(callback) = cb.as_ref() {
                let value = self.get_value();
                callback(&value);
            }
        }
    }

    /// Handle key input.
    pub fn handle_key(&self, key: crossterm::event::KeyEvent) {
        use crossterm::event::{KeyCode, KeyModifiers};

        match (key.code, key.modifiers) {
            // Submit
            (KeyCode::Enter, _) | (KeyCode::Char('\n'), _) => {
                if let Ok(cb) = self.on_submit.lock() {
                    if let Some(callback) = cb.as_ref() {
                        let value = self.get_value();
                        callback(&value);
                    }
                }
            }
            // Escape
            (KeyCode::Esc, _) => {
                if let Ok(cb) = self.on_escape.lock() {
                    if let Some(callback) = cb.as_ref() {
                        callback();
                    }
                }
            }
            // Navigation
            (KeyCode::Left, KeyModifiers::CONTROL) => self.move_word_left(),
            (KeyCode::Right, KeyModifiers::CONTROL) => self.move_word_right(),
            (KeyCode::Left, _) => self.move_cursor_left(),
            (KeyCode::Right, _) => self.move_cursor_right(),
            (KeyCode::Home, _) => self.move_to_line_start(),
            (KeyCode::End, _) => self.move_to_line_end(),
            // Deletion
            (KeyCode::Backspace, KeyModifiers::CONTROL) => self.delete_word_backwards(),
            (KeyCode::Delete, KeyModifiers::CONTROL) => self.delete_word_forward(),
            (KeyCode::Backspace, _) => self.handle_backspace(),
            (KeyCode::Delete, _) => self.handle_forward_delete(),
            // Character input
            (KeyCode::Char(c), KeyModifiers::CONTROL) => match c {
                'a' => self.move_to_line_start(),
                'e' => self.move_to_line_end(),
                'b' => self.move_cursor_left(),
                'f' => self.move_cursor_right(),
                'w' => self.delete_word_backwards(),
                'k' => self.delete_to_line_end(),
                'u' => self.delete_to_line_start(),
                'y' => self.yank(),
                '_' | '/' => self.undo(),
                _ => {}
            },
            (KeyCode::Char(c), KeyModifiers::ALT) => match c {
                'b' => self.move_word_left(),
                'f' => self.move_word_right(),
                'd' => self.delete_word_forward(),
                _ => {}
            },
            (KeyCode::Char(c), _) => {
                self.insert_character(&c.to_string());
            }
            _ => {}
        }
    }
}

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

impl Component for Input {
    fn render(&self, width: usize) -> Vec<String> {
        let state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        let focused = *self.focused.lock().unwrap_or_else(|e| e.into_inner());

        let prompt_len = visible_width(&self.prompt);
        let available_width = width.saturating_sub(prompt_len);

        if available_width == 0 {
            return vec![self.prompt.clone()];
        }

        let value = &state.value;
        let cursor = state.cursor.min(char_count(value));

        // Show placeholder if empty and not focused
        if value.is_empty() && !focused {
            if let Some(ref placeholder) = self.placeholder {
                let dimmed = format!("\x1b[90m{}\x1b[0m", placeholder);
                return vec![format!("{}{}", self.prompt, dimmed)];
            }
        }

        let total_width = visible_width(value);
        let cursor_byte = byte_offset(value, cursor);
        let cursor_col = visible_width(&value[..cursor_byte]);

        // Horizontal scrolling. The window is chosen in display columns, but the
        // caret offset inside it must be counted in *characters* — the previous
        // implementation reused the byte cursor as an index into the visible
        // `chars` vector and re-derived it with a quadratic
        // `chars().position()` scan (which finds the FIRST occurrence of a char
        // and feeds that index back in as a byte offset). That desynced the
        // caret as soon as the text scrolled, and panicked on non-ASCII text.
        let (visible_text, cursor_display) = if total_width < available_width {
            (value.clone(), cursor)
        } else {
            let scroll_width = if cursor == char_count(value) {
                available_width.saturating_sub(1)
            } else {
                available_width
            };

            if scroll_width == 0 {
                (String::new(), 0)
            } else {
                let half_width = scroll_width / 2;
                let start_col = if cursor_col < half_width {
                    0
                } else if cursor_col > total_width.saturating_sub(half_width) {
                    total_width.saturating_sub(scroll_width)
                } else {
                    cursor_col.saturating_sub(half_width)
                };
                let visible = slice_by_column(value, start_col, scroll_width, true);
                let caret_cols = cursor_col.saturating_sub(start_col).min(scroll_width);
                let before_caret = slice_by_column(value, start_col, caret_cols, true);
                let caret = before_caret.chars().count().min(char_count(&visible));
                (visible, caret)
            }
        };

        // Insert cursor marker and reverse video for cursor character
        let chars: Vec<char> = visible_text.chars().collect();
        let cursor_display = cursor_display.min(chars.len());
        let before_cursor: String = chars.iter().take(cursor_display).collect();
        let at_cursor = chars.get(cursor_display).copied().unwrap_or(' ');
        let after_cursor: String = chars.iter().skip(cursor_display + 1).collect();

        let marker = if focused { CURSOR_MARKER } else { "" };
        let cursor_char = if focused {
            format!("\x1b[7m{}\x1b[27m", at_cursor)
        } else {
            at_cursor.to_string()
        };

        // The marker is zero-width and `extract_cursor_position` derives the
        // hardware cursor column from the visible text *before* it, so it has to
        // sit exactly at the caret. Emitting it right after the prompt (as this
        // used to) pinned the real terminal caret to the start of the input and
        // it never followed the text being typed.
        let line = format!(
            "{}{}{}{}{}{}",
            self.prompt, before_cursor, marker, cursor_char, after_cursor, "\x1b[0m"
        );

        vec![line]
    }

    fn invalidate(&self) {
        // No cached state to invalidate
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl Focusable for Input {
    fn set_focused(&self, focused: bool) {
        if let Ok(mut f) = self.focused.lock() {
            *f = focused;
        }
    }

    fn is_focused(&self) -> bool {
        *self.focused.lock().unwrap_or_else(|e| e.into_inner())
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::ansi::strip_ansi;
    use crossterm::event::{KeyCode, KeyModifiers};

    /// Press one key with no modifiers.
    fn key(input: &Input, code: KeyCode) {
        input.handle_key(crossterm::event::KeyEvent::new(code, KeyModifiers::NONE));
    }

    #[test]
    fn test_input_value() {
        let input = Input::new();
        input.set_value("hello");
        assert_eq!(input.get_value(), "hello");
    }

    #[test]
    fn test_input_clear() {
        let input = Input::new();
        input.set_value("hello");
        input.clear();
        assert_eq!(input.get_value(), "");
    }

    #[test]
    fn test_input_render() {
        let input = Input::new();
        input.set_value("hello");
        let lines = input.render(20);
        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("hello"));
    }

    #[test]
    fn test_input_placeholder() {
        let input = Input::with_placeholder("Enter text...");
        let lines = input.render(20);
        assert!(lines[0].contains("Enter text..."));
    }

    /// Regression: every deletion path used to call `push_undo()` and
    /// `notify_change()` *while already holding* the `state` mutex. Both re-lock
    /// it and `std::sync::Mutex` is not reentrant, so the first backspace
    /// deadlocked the whole TUI and wedged the `ask_user` freeform input.
    #[test]
    fn deletion_keys_do_not_deadlock() {
        use std::sync::mpsc;
        use std::time::Duration;

        let press = |code: KeyCode, mods: KeyModifiers| {
            let input = Arc::new(Input::new());
            input.set_value("hello world");
            let (tx, rx) = mpsc::channel();
            let handle = input.clone();
            std::thread::spawn(move || {
                handle.handle_key(crossterm::event::KeyEvent::new(code, mods));
                let _ = tx.send(handle.get_value());
            });
            match rx.recv_timeout(Duration::from_secs(5)) {
                Ok(value) => value,
                Err(_) => panic!("{code:?} deadlocked"),
            }
        };

        assert_eq!(press(KeyCode::Backspace, KeyModifiers::NONE), "hello worl");
        assert_eq!(press(KeyCode::Delete, KeyModifiers::NONE), "hello world");
        assert_eq!(press(KeyCode::Backspace, KeyModifiers::CONTROL), "hello ");
        // Ctrl+K sits at the end of the line, so it is a no-op; Ctrl+U kills
        // back to the start.
        assert_eq!(
            press(KeyCode::Char('k'), KeyModifiers::CONTROL),
            "hello world"
        );
        assert_eq!(press(KeyCode::Char('u'), KeyModifiers::CONTROL), "");
    }

    /// Regression: `render` emitted `CURSOR_MARKER` right after the prompt, but
    /// `extract_cursor_position` measures the hardware cursor column from the
    /// text *before* the marker — so the real terminal caret stayed pinned at
    /// the start of the input instead of following the document.
    #[test]
    fn cursor_marker_sits_at_the_caret() {
        let input = Input::new();
        input.set_focused(true);
        input.set_value("hello world");
        let line = input.render(40).remove(0);
        let marker_at = line.find(CURSOR_MARKER).expect("marker present");
        // Caret is at the end of "hello world", not at column 0 / after "> ".
        assert_eq!(
            visible_width(&line[..marker_at]),
            visible_width("> hello world"),
            "caret not at end: {line:?}"
        );
    }

    /// Same, but with a value wider than the field so the input has to scroll,
    /// and with multibyte text (the old caret math reused a byte cursor as a
    /// char index plus a quadratic `chars().position()` rescan).
    #[test]
    fn cursor_marker_tracks_scrolled_multibyte_text() {
        let input = Input::new();
        input.set_focused(true);

        let long: String = "0123456789".repeat(6);
        input.set_value(&long);
        let line = input.render(20).remove(0);
        let marker_at = line.find(CURSOR_MARKER).expect("marker present");
        let caret_col = visible_width(&line[..marker_at]);
        let plain = strip_ansi(&line.replace(CURSOR_MARKER, ""));
        assert!(
            caret_col > visible_width("> "),
            "caret stuck at the prompt: col={caret_col}"
        );
        assert!(plain.contains("89"), "window lost the tail: {plain:?}");

        // Multibyte value: must not panic and must keep the caret at the end.
        input.set_value("\u{4e2d}\u{6587}\u{6d4b}\u{8bd5}");
        let line = input.render(40).remove(0);
        let marker_at = line.find(CURSOR_MARKER).expect("marker present");
        assert_eq!(
            visible_width(&line[..marker_at]),
            visible_width("> \u{4e2d}\u{6587}\u{6d4b}\u{8bd5}")
        );
        assert!(strip_ansi(&line).contains("\u{4e2d}\u{6587}\u{6d4b}\u{8bd5}"));
    }

    /// Regression: the cursor is a character index while `&str` is sliced by
    /// byte offset, so `&value[..cursor]` panicked on non-ASCII input and
    /// `set_value` left the cursor past the end of the text.
    #[test]
    fn deletions_are_char_aware() {
        let input = Input::new();

        // set_value must put the cursor at the end, so Backspace deletes.
        input.set_value("ab\u{4e2d}");
        key(&input, KeyCode::Backspace);
        assert_eq!(input.get_value(), "ab");
        key(&input, KeyCode::Backspace);
        assert_eq!(input.get_value(), "a");

        // Forward delete across a multibyte boundary.
        input.set_value("\u{4e2d}b");
        key(&input, KeyCode::Left);
        key(&input, KeyCode::Delete);
        assert_eq!(input.get_value(), "\u{4e2d}");
        key(&input, KeyCode::Left);
        key(&input, KeyCode::Delete);
        assert_eq!(input.get_value(), "");

        // Insertion at the end must not land mid-character.
        input.set_value("\u{4e2d}\u{6587}");
        key(&input, KeyCode::Char('X'));
        assert_eq!(input.get_value(), "\u{4e2d}\u{6587}X");

        // Nor may moving/inserting inside a multibyte string land mid-char.
        input.set_value("a\u{4e2d}b");
        key(&input, KeyCode::Left);
        key(&input, KeyCode::Char('X'));
        assert_eq!(input.get_value(), "a\u{4e2d}Xb");
        key(&input, KeyCode::Backspace);
        assert_eq!(input.get_value(), "a\u{4e2d}b");
        key(&input, KeyCode::Backspace);
        assert_eq!(input.get_value(), "ab");
    }
}