charmed-bubbles 0.2.0

Common TUI components for bubbletea applications
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
//! Single-line text input component.
//!
//! This module provides a text input field for TUI applications with features
//! like password masking, suggestions, and validation.
//!
//! # Example
//!
//! ```rust
//! use bubbles::textinput::TextInput;
//!
//! let mut input = TextInput::new();
//! input.set_placeholder("Enter your name");
//! input.set_value("Hello");
//!
//! // Render the input
//! let view = input.view();
//! ```

use crate::cursor::{Cursor, blink_cmd};
use crate::key::{Binding, matches};
use crate::runeutil::Sanitizer;
use bubbletea::{Cmd, KeyMsg, Message, Model};
use lipgloss::{Color, Style};
use unicode_width::UnicodeWidthChar;

/// Echo mode for the text input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EchoMode {
    /// Display text as-is (default).
    #[default]
    Normal,
    /// Display echo character instead of actual text (for passwords).
    Password,
    /// Display nothing (hidden input).
    None,
}

/// Validation function type.
pub type ValidateFn = Box<dyn Fn(&str) -> Option<String> + Send + Sync>;

/// Key bindings for text input navigation.
#[derive(Debug, Clone)]
pub struct KeyMap {
    /// Move cursor forward one character.
    pub character_forward: Binding,
    /// Move cursor backward one character.
    pub character_backward: Binding,
    /// Move cursor forward one word.
    pub word_forward: Binding,
    /// Move cursor backward one word.
    pub word_backward: Binding,
    /// Delete word backward.
    pub delete_word_backward: Binding,
    /// Delete word forward.
    pub delete_word_forward: Binding,
    /// Delete text after cursor.
    pub delete_after_cursor: Binding,
    /// Delete text before cursor.
    pub delete_before_cursor: Binding,
    /// Delete character backward.
    pub delete_character_backward: Binding,
    /// Delete character forward.
    pub delete_character_forward: Binding,
    /// Move to start of line.
    pub line_start: Binding,
    /// Move to end of line.
    pub line_end: Binding,
    /// Paste from clipboard.
    pub paste: Binding,
    /// Accept current suggestion.
    pub accept_suggestion: Binding,
    /// Next suggestion.
    pub next_suggestion: Binding,
    /// Previous suggestion.
    pub prev_suggestion: Binding,
}

impl Default for KeyMap {
    fn default() -> Self {
        Self {
            character_forward: Binding::new().keys(&["right", "ctrl+f"]),
            character_backward: Binding::new().keys(&["left", "ctrl+b"]),
            word_forward: Binding::new().keys(&["alt+right", "ctrl+right", "alt+f"]),
            word_backward: Binding::new().keys(&["alt+left", "ctrl+left", "alt+b"]),
            delete_word_backward: Binding::new().keys(&["alt+backspace", "ctrl+w"]),
            delete_word_forward: Binding::new().keys(&["alt+delete", "alt+d"]),
            delete_after_cursor: Binding::new().keys(&["ctrl+k"]),
            delete_before_cursor: Binding::new().keys(&["ctrl+u"]),
            delete_character_backward: Binding::new().keys(&["backspace", "ctrl+h"]),
            delete_character_forward: Binding::new().keys(&["delete", "ctrl+d"]),
            line_start: Binding::new().keys(&["home", "ctrl+a"]),
            line_end: Binding::new().keys(&["end", "ctrl+e"]),
            paste: Binding::new().keys(&["ctrl+v"]),
            accept_suggestion: Binding::new().keys(&["tab"]),
            next_suggestion: Binding::new().keys(&["down", "ctrl+n"]),
            prev_suggestion: Binding::new().keys(&["up", "ctrl+p"]),
        }
    }
}

/// Message for paste operations.
#[derive(Debug, Clone)]
pub struct PasteMsg(pub String);

/// Message for paste errors.
#[derive(Debug, Clone)]
pub struct PasteErrMsg(pub String);

/// Single-line text input model.
pub struct TextInput {
    /// Current error from validation.
    pub err: Option<String>,
    /// Prompt displayed before input.
    pub prompt: String,
    /// Placeholder text when empty.
    pub placeholder: String,
    /// Echo mode (normal, password, none).
    pub echo_mode: EchoMode,
    /// Character to display in password mode.
    pub echo_character: char,
    /// Cursor model.
    pub cursor: Cursor,
    /// Style for the prompt.
    pub prompt_style: Style,
    /// Style for the text.
    pub text_style: Style,
    /// Style for the placeholder.
    pub placeholder_style: Style,
    /// Style for completions.
    pub completion_style: Style,
    /// Maximum characters allowed (0 = no limit).
    pub char_limit: usize,
    /// Maximum display width (0 = no limit).
    pub width: usize,
    /// Key bindings.
    pub key_map: KeyMap,
    /// Whether to show suggestions.
    pub show_suggestions: bool,
    /// Underlying text value.
    value: Vec<char>,
    /// Focus state.
    focus: bool,
    /// Cursor position.
    pos: usize,
    /// Viewport offset (left).
    offset: usize,
    /// Viewport offset (right).
    offset_right: usize,
    /// Validation function.
    validate: Option<ValidateFn>,
    /// Rune sanitizer.
    sanitizer: Sanitizer,
    /// Available suggestions.
    suggestions: Vec<Vec<char>>,
    /// Matched suggestions.
    matched_suggestions: Vec<Vec<char>>,
    /// Current suggestion index.
    current_suggestion_index: usize,
}

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

impl std::fmt::Debug for TextInput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TextInput")
            .field("err", &self.err)
            .field("prompt", &self.prompt)
            .field("placeholder", &self.placeholder)
            .field("echo_mode", &self.echo_mode)
            .field("cursor", &self.cursor)
            .field("char_limit", &self.char_limit)
            .field("width", &self.width)
            .field("focus", &self.focus)
            .field("pos", &self.pos)
            .field("value_len", &self.value.len())
            .field("validate", &self.validate.as_ref().map(|_| "<fn>"))
            .finish()
    }
}

impl Clone for TextInput {
    fn clone(&self) -> Self {
        Self {
            err: self.err.clone(),
            prompt: self.prompt.clone(),
            placeholder: self.placeholder.clone(),
            echo_mode: self.echo_mode,
            echo_character: self.echo_character,
            cursor: self.cursor.clone(),
            prompt_style: self.prompt_style.clone(),
            text_style: self.text_style.clone(),
            placeholder_style: self.placeholder_style.clone(),
            completion_style: self.completion_style.clone(),
            char_limit: self.char_limit,
            width: self.width,
            key_map: self.key_map.clone(),
            show_suggestions: self.show_suggestions,
            value: self.value.clone(),
            focus: self.focus,
            pos: self.pos,
            offset: self.offset,
            offset_right: self.offset_right,
            validate: None, // Can't clone Box<dyn Fn>
            sanitizer: self.sanitizer.clone(),
            suggestions: self.suggestions.clone(),
            matched_suggestions: self.matched_suggestions.clone(),
            current_suggestion_index: self.current_suggestion_index,
        }
    }
}

impl TextInput {
    /// Creates a new text input with default settings.
    #[must_use]
    pub fn new() -> Self {
        let sanitizer = Sanitizer::new()
            .with_tab_replacement(" ")
            .with_newline_replacement(" ");

        Self {
            err: None,
            prompt: "> ".to_string(),
            placeholder: String::new(),
            echo_mode: EchoMode::Normal,
            echo_character: '*',
            cursor: Cursor::new(),
            prompt_style: Style::new(),
            text_style: Style::new(),
            placeholder_style: Style::new().foreground_color(Color::from("240")),
            completion_style: Style::new().foreground_color(Color::from("240")),
            char_limit: 0,
            width: 0,
            key_map: KeyMap::default(),
            show_suggestions: false,
            value: Vec::new(),
            focus: false,
            pos: 0,
            offset: 0,
            offset_right: 0,
            validate: None,
            sanitizer,
            suggestions: Vec::new(),
            matched_suggestions: Vec::new(),
            current_suggestion_index: 0,
        }
    }

    /// Sets the prompt string.
    pub fn set_prompt(&mut self, prompt: impl Into<String>) {
        self.prompt = prompt.into();
    }

    /// Sets the placeholder text.
    pub fn set_placeholder(&mut self, placeholder: impl Into<String>) {
        self.placeholder = placeholder.into();
    }

    /// Sets the echo mode.
    pub fn set_echo_mode(&mut self, mode: EchoMode) {
        self.echo_mode = mode;
    }

    /// Sets the value of the text input.
    pub fn set_value(&mut self, s: &str) {
        let mut runes = self.sanitizer.sanitize(&s.chars().collect::<Vec<_>>());
        if self.char_limit > 0 && runes.len() > self.char_limit {
            runes.truncate(self.char_limit);
        }
        let err = self.do_validate(&runes);
        self.set_value_internal(runes, err);
    }

    fn set_value_internal(&mut self, runes: Vec<char>, err: Option<String>) {
        self.err = err;
        let empty = self.value.is_empty();

        if self.char_limit > 0 && runes.len() > self.char_limit {
            self.value = runes[..self.char_limit].to_vec();
        } else {
            self.value = runes;
        }

        if (self.pos == 0 && empty) || self.pos > self.value.len() {
            self.set_cursor(self.value.len());
        }
        self.handle_overflow();
        self.update_suggestions();
    }

    /// Returns the current value as a string.
    #[must_use]
    pub fn value(&self) -> String {
        self.value.iter().collect()
    }

    /// Returns the cursor position.
    #[must_use]
    pub fn position(&self) -> usize {
        self.pos
    }

    /// Sets the cursor position.
    pub fn set_cursor(&mut self, pos: usize) {
        self.pos = pos.min(self.value.len());
        self.handle_overflow();
    }

    /// Moves cursor to start of input.
    pub fn cursor_start(&mut self) {
        self.set_cursor(0);
    }

    /// Moves cursor to end of input.
    pub fn cursor_end(&mut self) {
        self.set_cursor(self.value.len());
    }

    /// Returns whether the input is focused.
    #[must_use]
    pub fn focused(&self) -> bool {
        self.focus
    }

    /// Focuses the input and returns the cursor blink command.
    pub fn focus(&mut self) -> Option<Cmd> {
        self.focus = true;
        self.cursor.focus()
    }

    /// Blurs the input.
    pub fn blur(&mut self) {
        self.focus = false;
        self.cursor.blur();
    }

    /// Resets the input to empty.
    pub fn reset(&mut self) {
        self.value.clear();
        self.err = self.do_validate(&self.value);
        self.set_cursor(0);
        self.update_suggestions();
    }

    /// Sets the suggestions list.
    pub fn set_suggestions(&mut self, suggestions: &[&str]) {
        self.suggestions = suggestions.iter().map(|s| s.chars().collect()).collect();
        self.update_suggestions();
    }

    /// Sets the validation function.
    pub fn set_validate<F>(&mut self, f: F)
    where
        F: Fn(&str) -> Option<String> + Send + Sync + 'static,
    {
        self.validate = Some(Box::new(f));
    }

    /// Returns available suggestions as strings.
    #[must_use]
    pub fn available_suggestions(&self) -> Vec<String> {
        self.suggestions
            .iter()
            .map(|s| s.iter().collect())
            .collect()
    }

    /// Returns matched suggestions as strings.
    #[must_use]
    pub fn matched_suggestions(&self) -> Vec<String> {
        self.matched_suggestions
            .iter()
            .map(|s| s.iter().collect())
            .collect()
    }

    /// Returns the current suggestion index.
    #[must_use]
    pub fn current_suggestion_index(&self) -> usize {
        self.current_suggestion_index
    }

    /// Returns the current suggestion.
    #[must_use]
    pub fn current_suggestion(&self) -> String {
        self.matched_suggestions
            .get(self.current_suggestion_index)
            .map(|s| s.iter().collect())
            .unwrap_or_default()
    }

    fn do_validate(&self, v: &[char]) -> Option<String> {
        self.validate
            .as_ref()
            .and_then(|f| f(&v.iter().collect::<String>()))
    }

    fn insert_runes_from_user_input(&mut self, v: &[char]) {
        let paste = self.sanitizer.sanitize(v);

        let mut available = if self.char_limit > 0 {
            let avail = self.char_limit.saturating_sub(self.value.len());
            if avail == 0 {
                return;
            }
            avail
        } else {
            usize::MAX
        };

        let paste = if paste.len() > available {
            &paste[..available]
        } else {
            &paste
        };

        // Split at cursor
        let head = &self.value[..self.pos];
        let tail = &self.value[self.pos..];

        let mut new_value = head.to_vec();
        for &c in paste {
            if available == 0 {
                break;
            }
            new_value.push(c);
            self.pos += 1;
            available = available.saturating_sub(1);
        }
        new_value.extend_from_slice(tail);

        let err = self.do_validate(&new_value);
        self.set_value_internal(new_value, err);
    }

    fn handle_overflow(&mut self) {
        let total_width: usize = self.value.iter().map(|c| c.width().unwrap_or(0)).sum();
        if self.width == 0 || total_width <= self.width {
            self.offset = 0;
            self.offset_right = self.value.len();
            return;
        }

        self.offset_right = self.offset_right.min(self.value.len());

        if self.pos < self.offset {
            self.offset = self.pos;
            let mut w = 0;
            let mut i = 0;
            let runes = &self.value[self.offset..];

            while i < runes.len() {
                let cw = runes[i].width().unwrap_or(0);
                if w + cw > self.width {
                    break;
                }
                w += cw;
                i += 1;
            }
            self.offset_right = self.offset + i;
        } else if self.pos >= self.offset_right {
            self.offset_right = self.pos;
            let mut w = 0;
            let runes = &self.value[..self.offset_right];
            let mut start_index = self.offset_right;

            // Scan backwards from offset_right
            while start_index > 0 {
                let prev = start_index - 1;
                let cw = runes[prev].width().unwrap_or(0);
                if w + cw > self.width {
                    break;
                }
                w += cw;
                start_index = prev;
            }
            self.offset = start_index;
        }
    }

    fn delete_before_cursor(&mut self) {
        self.value = self.value[self.pos..].to_vec();
        self.err = self.do_validate(&self.value);
        self.offset = 0;
        self.set_cursor(0);
    }

    fn delete_after_cursor(&mut self) {
        self.value = self.value[..self.pos].to_vec();
        self.err = self.do_validate(&self.value);
        self.set_cursor(self.value.len());
    }

    fn delete_word_backward(&mut self) {
        if self.pos == 0 || self.value.is_empty() {
            return;
        }

        if self.echo_mode != EchoMode::Normal {
            self.delete_before_cursor();
            return;
        }

        let old_pos = self.pos;
        self.set_cursor(self.pos.saturating_sub(1));

        // Skip whitespace backward
        while self.pos > 0 {
            let prev = self.pos - 1;
            if let Some(c) = self.value.get(prev) {
                if c.is_whitespace() {
                    self.set_cursor(prev);
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        // Skip non-whitespace backward
        while self.pos > 0 {
            let prev = self.pos - 1;
            if let Some(c) = self.value.get(prev) {
                if !c.is_whitespace() {
                    self.set_cursor(prev);
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        if old_pos > self.value.len() {
            self.value = self.value[..self.pos].to_vec();
        } else {
            let mut new_value = self.value[..self.pos].to_vec();
            new_value.extend_from_slice(&self.value[old_pos..]);
            self.value = new_value;
        }
        self.err = self.do_validate(&self.value);
        self.handle_overflow();
    }

    fn delete_word_forward(&mut self) {
        if self.pos >= self.value.len() || self.value.is_empty() {
            return;
        }

        if self.echo_mode != EchoMode::Normal {
            self.delete_after_cursor();
            return;
        }

        let old_pos = self.pos;
        self.set_cursor(self.pos + 1);

        // Skip whitespace
        while self.pos < self.value.len()
            && self.value.get(self.pos).is_some_and(|c| c.is_whitespace())
        {
            self.set_cursor(self.pos + 1);
        }

        // Skip non-whitespace
        while self.pos < self.value.len() {
            if !self.value.get(self.pos).is_some_and(|c| c.is_whitespace()) {
                self.set_cursor(self.pos + 1);
            } else {
                break;
            }
        }

        if self.pos > self.value.len() {
            self.value = self.value[..old_pos].to_vec();
        } else {
            let mut new_value = self.value[..old_pos].to_vec();
            new_value.extend_from_slice(&self.value[self.pos..]);
            self.value = new_value;
        }
        self.err = self.do_validate(&self.value);
        self.set_cursor(old_pos);
    }

    fn word_backward(&mut self) {
        if self.pos == 0 || self.value.is_empty() {
            return;
        }

        if self.echo_mode != EchoMode::Normal {
            self.cursor_start();
            return;
        }

        // Skip whitespace backward
        while self.pos > 0 {
            let prev = self.pos - 1;
            if let Some(c) = self.value.get(prev) {
                if c.is_whitespace() {
                    self.set_cursor(prev);
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        // Skip non-whitespace backward
        while self.pos > 0 {
            let prev = self.pos - 1;
            if let Some(c) = self.value.get(prev) {
                if !c.is_whitespace() {
                    self.set_cursor(prev);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }

    fn word_forward(&mut self) {
        if self.pos >= self.value.len() || self.value.is_empty() {
            return;
        }

        if self.echo_mode != EchoMode::Normal {
            self.cursor_end();
            return;
        }

        let mut i = self.pos;

        // Skip whitespace
        while i < self.value.len() && self.value.get(i).is_some_and(|c| c.is_whitespace()) {
            self.set_cursor(self.pos + 1);
            i += 1;
        }

        // Skip non-whitespace
        while i < self.value.len() {
            if !self.value.get(i).is_some_and(|c| c.is_whitespace()) {
                self.set_cursor(self.pos + 1);
                i += 1;
            } else {
                break;
            }
        }
    }

    fn echo_transform(&self, v: &str) -> String {
        match self.echo_mode {
            EchoMode::Normal => v.to_string(),
            EchoMode::Password => self.echo_character.to_string().repeat(v.chars().count()),
            EchoMode::None => String::new(),
        }
    }

    fn can_accept_suggestion(&self) -> bool {
        !self.matched_suggestions.is_empty()
    }

    fn update_suggestions(&mut self) {
        if !self.show_suggestions {
            return;
        }

        if self.value.is_empty() || self.suggestions.is_empty() {
            self.matched_suggestions.clear();
            return;
        }

        let value_str: String = self.value.iter().collect();
        let value_lower = value_str.to_lowercase();

        let matches: Vec<Vec<char>> = self
            .suggestions
            .iter()
            .filter(|s| {
                let suggestion: String = s.iter().collect();
                suggestion.to_lowercase().starts_with(&value_lower)
            })
            .cloned()
            .collect();

        if matches != self.matched_suggestions {
            self.current_suggestion_index = 0;
        }

        self.matched_suggestions = matches;
    }

    fn next_suggestion(&mut self) {
        if self.matched_suggestions.is_empty() {
            return;
        }
        self.current_suggestion_index =
            (self.current_suggestion_index + 1) % self.matched_suggestions.len();
    }

    fn previous_suggestion(&mut self) {
        if self.matched_suggestions.is_empty() {
            return;
        }
        if self.current_suggestion_index == 0 {
            self.current_suggestion_index = self.matched_suggestions.len().saturating_sub(1);
        } else {
            self.current_suggestion_index -= 1;
        }
    }

    /// Updates the text input based on messages.
    pub fn update(&mut self, msg: Message) -> Option<Cmd> {
        if !self.focus {
            return None;
        }

        // Handle paste message
        if let Some(paste) = msg.downcast_ref::<PasteMsg>() {
            self.insert_runes_from_user_input(&paste.0.chars().collect::<Vec<_>>());
            return None;
        }

        if let Some(paste_err) = msg.downcast_ref::<PasteErrMsg>() {
            self.err = Some(paste_err.0.clone());
            return None;
        }

        let old_pos = self.pos;

        if let Some(key) = msg.downcast_ref::<KeyMsg>() {
            let key_str = key.to_string();

            // Check for suggestion acceptance first
            if matches(&key_str, &[&self.key_map.accept_suggestion])
                && self.can_accept_suggestion()
                && let Some(suggestion) =
                    self.matched_suggestions.get(self.current_suggestion_index)
                && self.value.len() < suggestion.len()
            {
                self.value
                    .extend_from_slice(&suggestion[self.value.len()..]);
                self.cursor_end();
            }

            if matches(&key_str, &[&self.key_map.delete_word_backward]) {
                self.delete_word_backward();
            } else if matches(&key_str, &[&self.key_map.delete_character_backward]) {
                self.err = None;
                if !self.value.is_empty() && self.pos > 0 {
                    self.value.remove(self.pos - 1);
                    self.err = self.do_validate(&self.value);
                    self.set_cursor(self.pos.saturating_sub(1));
                }
            } else if matches(&key_str, &[&self.key_map.word_backward]) {
                self.word_backward();
            } else if matches(&key_str, &[&self.key_map.character_backward]) {
                if self.pos > 0 {
                    self.set_cursor(self.pos - 1);
                }
            } else if matches(&key_str, &[&self.key_map.word_forward]) {
                self.word_forward();
            } else if matches(&key_str, &[&self.key_map.character_forward]) {
                if self.pos < self.value.len() {
                    self.set_cursor(self.pos + 1);
                }
            } else if matches(&key_str, &[&self.key_map.line_start]) {
                self.cursor_start();
            } else if matches(&key_str, &[&self.key_map.delete_character_forward]) {
                if !self.value.is_empty() && self.pos < self.value.len() {
                    self.value.remove(self.pos);
                    self.err = self.do_validate(&self.value);
                }
            } else if matches(&key_str, &[&self.key_map.line_end]) {
                self.cursor_end();
            } else if matches(&key_str, &[&self.key_map.delete_after_cursor]) {
                self.delete_after_cursor();
            } else if matches(&key_str, &[&self.key_map.delete_before_cursor]) {
                self.delete_before_cursor();
            } else if matches(&key_str, &[&self.key_map.delete_word_forward]) {
                self.delete_word_forward();
            } else if matches(&key_str, &[&self.key_map.next_suggestion]) {
                self.next_suggestion();
            } else if matches(&key_str, &[&self.key_map.prev_suggestion]) {
                self.previous_suggestion();
            } else if !matches(
                &key_str,
                &[&self.key_map.paste, &self.key_map.accept_suggestion],
            ) {
                // Input regular characters
                let runes: Vec<char> = key.runes.clone();
                if !runes.is_empty() {
                    self.insert_runes_from_user_input(&runes);
                }
            }

            self.update_suggestions();
        }

        let mut cmds: Vec<Option<Cmd>> = Vec::new();

        if let Some(cmd) = self.cursor.update(msg) {
            cmds.push(Some(cmd));
        }

        if old_pos != self.pos && self.cursor.mode() == crate::cursor::Mode::Blink {
            // Reset blink state when cursor moves - trigger blink cycle
            cmds.push(Some(blink_cmd()));
        }

        self.handle_overflow();

        bubbletea::batch(cmds)
    }

    /// Renders the text input.
    #[must_use]
    pub fn view(&self) -> String {
        if self.value.is_empty() && !self.placeholder.is_empty() {
            return self.placeholder_view();
        }

        let value: Vec<char> = self.value[self.offset..self.offset_right].to_vec();
        let pos = self.pos.saturating_sub(self.offset);

        let before: String = value[..pos.min(value.len())].iter().collect();
        let mut v = self
            .text_style
            .clone()
            .inline()
            .render(&self.echo_transform(&before));

        if pos < value.len() {
            let char_at_cursor: String = value[pos..pos + 1].iter().collect();
            let char_display = self.echo_transform(&char_at_cursor);

            let mut cursor = self.cursor.clone();
            cursor.set_char(&char_display);
            v.push_str(&cursor.view());

            let after: String = value[pos + 1..].iter().collect();
            v.push_str(
                &self
                    .text_style
                    .clone()
                    .inline()
                    .render(&self.echo_transform(&after)),
            );
            v.push_str(&self.completion_view(0));
        } else if self.focus && self.can_accept_suggestion() {
            if let Some(suggestion) = self.matched_suggestions.get(self.current_suggestion_index) {
                if self.value.len() < suggestion.len() && self.pos < suggestion.len() {
                    let mut cursor = self.cursor.clone();
                    cursor.text_style = self.completion_style.clone();
                    let char_display: String = suggestion[self.pos..self.pos + 1].iter().collect();
                    cursor.set_char(&self.echo_transform(&char_display));
                    v.push_str(&cursor.view());
                    v.push_str(&self.completion_view(1));
                } else {
                    let mut cursor = self.cursor.clone();
                    cursor.set_char(" ");
                    v.push_str(&cursor.view());
                }
            }
        } else {
            let mut cursor = self.cursor.clone();
            cursor.set_char(" ");
            v.push_str(&cursor.view());
        }

        // Padding for width
        if self.width > 0 {
            let val_width: usize = value.iter().map(|c| c.width().unwrap_or(0)).sum();
            if val_width <= self.width {
                let padding = self.width.saturating_sub(val_width);
                v.push_str(
                    &self
                        .text_style
                        .clone()
                        .inline()
                        .render(&" ".repeat(padding)),
                );
            }
        }

        format!("{}{}", self.prompt_style.render(&self.prompt), v)
    }

    fn placeholder_view(&self) -> String {
        let prompt = self.prompt_style.render(&self.prompt);

        let mut cursor = self.cursor.clone();
        cursor.text_style = self.placeholder_style.clone();

        let first_char: String = self.placeholder.chars().take(1).collect();
        let rest: String = self.placeholder.chars().skip(1).collect();

        cursor.set_char(&first_char);
        let v = cursor.view();

        let styled_rest = self.placeholder_style.clone().inline().render(&rest);

        format!("{}{}{}", prompt, v, styled_rest)
    }

    fn completion_view(&self, offset: usize) -> String {
        if self.can_accept_suggestion()
            && let Some(suggestion) = self.matched_suggestions.get(self.current_suggestion_index)
            && self.value.len() + offset <= suggestion.len()
        {
            let completion: String = suggestion[self.value.len() + offset..].iter().collect();
            return self.placeholder_style.clone().inline().render(&completion);
        }
        String::new()
    }
}

impl Model for TextInput {
    /// Initialize the text input.
    ///
    /// If focused and cursor is in blink mode, returns a blink command.
    fn init(&self) -> Option<Cmd> {
        if self.focus { Some(blink_cmd()) } else { None }
    }

    /// Update the text input state based on incoming messages.
    ///
    /// Handles:
    /// - `KeyMsg` - Keyboard input for text entry and navigation
    /// - `PasteMsg` - Paste operations
    /// - Cursor blink messages
    fn update(&mut self, msg: Message) -> Option<Cmd> {
        TextInput::update(self, msg)
    }

    /// Render the text input.
    fn view(&self) -> String {
        TextInput::view(self)
    }
}

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

    #[test]
    fn test_textinput_new() {
        let input = TextInput::new();
        assert_eq!(input.prompt, "> ");
        assert_eq!(input.echo_character, '*');
        assert!(!input.focused());
    }

    #[test]
    fn test_textinput_set_value() {
        let mut input = TextInput::new();
        input.set_value("hello");
        assert_eq!(input.value(), "hello");
    }

    #[test]
    fn test_textinput_cursor_position() {
        let mut input = TextInput::new();
        input.set_value("hello");
        assert_eq!(input.position(), 5);

        input.set_cursor(2);
        assert_eq!(input.position(), 2);

        input.cursor_start();
        assert_eq!(input.position(), 0);

        input.cursor_end();
        assert_eq!(input.position(), 5);
    }

    #[test]
    fn test_textinput_focus_blur() {
        let mut input = TextInput::new();
        assert!(!input.focused());

        input.focus();
        assert!(input.focused());

        input.blur();
        assert!(!input.focused());
    }

    #[test]
    fn test_textinput_reset() {
        let mut input = TextInput::new();
        input.set_value("hello");
        assert!(!input.value.is_empty());

        input.reset();
        assert!(input.value.is_empty());
    }

    #[test]
    fn test_textinput_reset_clears_error_and_suggestions() {
        let mut input = TextInput::new();
        input.show_suggestions = true;
        input.set_suggestions(&["apple", "apricot"]);
        input.set_validate(|v| (!v.is_empty()).then(|| "err".to_string()));

        input.set_value("ap");
        assert!(input.err.is_some());
        assert!(!input.matched_suggestions().is_empty());

        input.reset();
        assert!(input.err.is_none());
        assert!(input.matched_suggestions().is_empty());
    }

    #[test]
    fn test_textinput_char_limit() {
        let mut input = TextInput::new();
        input.char_limit = 5;
        input.set_value("hello world");
        assert_eq!(input.value(), "hello");
    }

    #[test]
    fn test_textinput_echo_mode() {
        let mut input = TextInput::new();
        input.set_value("secret");

        assert_eq!(input.echo_transform("secret"), "secret");

        input.echo_mode = EchoMode::Password;
        assert_eq!(input.echo_transform("secret"), "******");

        input.echo_mode = EchoMode::None;
        assert_eq!(input.echo_transform("secret"), "");
    }

    #[test]
    fn test_textinput_placeholder() {
        let mut input = TextInput::new();
        input.set_placeholder("Enter text...");
        assert_eq!(input.placeholder, "Enter text...");
    }

    #[test]
    fn test_textinput_suggestions() {
        let mut input = TextInput::new();
        input.show_suggestions = true;
        input.set_suggestions(&["apple", "apricot", "banana"]);
        input.set_value("ap");
        input.update_suggestions();

        assert_eq!(input.matched_suggestions().len(), 2);
        assert!(input.matched_suggestions().contains(&"apple".to_string()));
        assert!(input.matched_suggestions().contains(&"apricot".to_string()));
    }

    #[test]
    fn test_textinput_set_value_updates_suggestions() {
        let mut input = TextInput::new();
        input.show_suggestions = true;
        input.set_suggestions(&["apple", "banana"]);

        input.set_value("ap");

        assert_eq!(input.matched_suggestions().len(), 1);
        assert!(input.matched_suggestions().contains(&"apple".to_string()));
    }

    #[test]
    fn test_textinput_suggestion_overflow_uses_global_position() {
        let mut input = TextInput::new();
        input.width = 5;
        input.show_suggestions = true;
        input.set_value("abcdefghij");
        input.set_suggestions(&["abcdefghijZ"]);
        input.focus();
        input.cursor_end();

        let view = input.view();
        assert!(
            view.contains("Z"),
            "Expected suggestion character to render at cursor when scrolled"
        );
    }

    #[test]
    fn test_textinput_validation() {
        let mut input = TextInput::new();
        input.set_validate(|s| {
            if s.contains("bad") {
                Some("Contains bad word".to_string())
            } else {
                None
            }
        });

        input.set_value("good");
        assert!(input.err.is_none());

        input.set_value("bad");
        assert!(input.err.is_some());
    }

    #[test]
    fn test_textinput_view() {
        let mut input = TextInput::new();
        input.set_value("hello");
        let view = input.view();
        assert!(view.contains("> "));
        assert!(view.contains("hello"));
    }

    #[test]
    fn test_textinput_placeholder_view() {
        let mut input = TextInput::new();
        input.set_placeholder("Type here...");
        let view = input.view();
        assert!(view.contains("> "));
    }

    #[test]
    fn test_keymap_default() {
        let km = KeyMap::default();
        assert!(!km.character_forward.get_keys().is_empty());
        assert!(!km.delete_character_backward.get_keys().is_empty());
    }

    // Model trait implementation tests
    #[test]
    fn test_model_init_unfocused() {
        let input = TextInput::new();
        // Unfocused input should not return init command
        let cmd = Model::init(&input);
        assert!(cmd.is_none());
    }

    #[test]
    fn test_model_init_focused() {
        let mut input = TextInput::new();
        input.focus();
        // Focused input should return init command for cursor blink
        let cmd = Model::init(&input);
        assert!(cmd.is_some());
    }

    #[test]
    fn test_model_view() {
        let mut input = TextInput::new();
        input.set_value("test");
        // Model::view should return same result as TextInput::view
        let model_view = Model::view(&input);
        let textinput_view = TextInput::view(&input);
        assert_eq!(model_view, textinput_view);
    }

    #[test]
    fn test_model_update_handles_paste_msg() {
        let mut input = TextInput::new();
        input.focus();
        assert_eq!(input.value(), "");

        // Use Model::update to handle a paste message
        let paste_msg = Message::new(PasteMsg("hello world".to_string()));
        let _ = Model::update(&mut input, paste_msg);

        assert_eq!(input.value(), "hello world");
    }

    #[test]
    fn test_model_update_unfocused_ignores_input() {
        let mut input = TextInput::new();
        assert!(!input.focused());
        assert_eq!(input.value(), "");

        // Unfocused input should ignore paste
        let paste_msg = Message::new(PasteMsg("ignored".to_string()));
        let _ = Model::update(&mut input, paste_msg);

        assert_eq!(input.value(), "", "Unfocused input should ignore messages");
    }

    #[test]
    fn test_model_update_handles_key_input() {
        let mut input = TextInput::new();
        input.focus();
        input.set_value("hello");
        assert_eq!(input.position(), 5);

        // Create a left arrow key message to move cursor
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Left,
            runes: vec![],
            alt: false,
            paste: false,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.position(), 4, "Cursor should have moved left");
    }

    #[test]
    fn test_textinput_satisfies_model_bounds() {
        // Verify TextInput can be used where Model + Send + 'static is required
        fn accepts_model<M: Model + Send + 'static>(_model: M) {}
        let input = TextInput::new();
        accepts_model(input);
    }

    #[test]
    fn test_word_backward_boundary() {
        let mut input = TextInput::new();
        input.set_value("abc");
        input.set_cursor(1); // "a|bc" (cursor is at 1, so 'b' is to the right, 'a' to the left)
        input.word_backward();
        assert_eq!(input.position(), 0); // Should move to start
    }

    #[test]
    fn test_delete_word_backward_boundary() {
        let mut input = TextInput::new();
        input.set_value("abc");
        input.set_cursor(1); // "a|bc"
        input.delete_word_backward();
        assert_eq!(input.value(), "bc");
        assert_eq!(input.position(), 0);
    }

    #[test]
    fn test_handle_overflow_wide_chars() {
        let mut input = TextInput::new();
        input.width = 3;
        input.set_value("aπŸ˜€b"); // 'a' (1), 'πŸ˜€' (2), 'b' (1). Total 4.
        // pos=0. offset=0. offset_right=?
        // w=0. 'a'(1) -> w=1. 'πŸ˜€'(2) -> w=3. 'b'(1) -> w=4 > 3. Break.
        // offset_right should be 2 ("aπŸ˜€").

        // Force overflow update
        input.set_cursor(0);
        // internal update triggers handle_overflow

        // Can't check internal state easily without exposing or deducing from view
        // But let's check view length
        let view = input.view();
        // view should contain "aπŸ˜€" (char count 2) or something fitting width 3.
        // But view strips ANSI from result to check?
        // Let's rely on logic correctness.
        // width=3. "aπŸ˜€" is width 3. "aπŸ˜€b" is 4.
        // So it should show "aπŸ˜€".
        assert!(view.contains("aπŸ˜€"));
        assert!(!view.contains("b")); // 'b' is clipped
    }

    #[test]
    fn test_delete_word_backward_on_whitespace() {
        let mut input = TextInput::new();
        input.set_value("abc   ");
        input.set_cursor(6); // At end

        input.delete_word_backward();

        // Current implementation: deletes whitespace AND word -> ""
        // "Standard" (bash) behavior: deletes whitespace -> "abc"
        // Let's see what it does.
        assert_eq!(
            input.value(),
            "",
            "Aggressive deletion: deleted both whitespace and word"
        );
    }

    // === Bracketed Paste Tests ===
    // These tests verify paste behavior when receiving KeyMsg with paste=true,
    // which is how terminals deliver bracketed paste sequences.

    #[test]
    fn test_bracketed_paste_basic() {
        let mut input = TextInput::new();
        input.focus();

        // Simulate bracketed paste: KeyMsg with paste=true and runes
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: vec!['h', 'e', 'l', 'l', 'o'],
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.value(), "hello");
    }

    #[test]
    fn test_bracketed_paste_multiline_converts_newlines() {
        let mut input = TextInput::new();
        input.focus();

        // Paste with newlines - should be converted to spaces for single-line input
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "line1\nline2\nline3".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value(),
            "line1 line2 line3",
            "Newlines should be converted to spaces in single-line input"
        );
    }

    #[test]
    fn test_bracketed_paste_crlf_converts_to_space() {
        let mut input = TextInput::new();
        input.focus();

        // Windows-style CRLF should also be converted
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "line1\r\nline2".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value(),
            "line1 line2",
            "CRLF should be converted to single space"
        );
    }

    #[test]
    fn test_bracketed_paste_respects_char_limit() {
        let mut input = TextInput::new();
        input.focus();
        input.char_limit = 10;

        // Try to paste more than the limit
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "this is a very long paste that exceeds the limit"
                .chars()
                .collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value().len(),
            10,
            "Paste should be truncated at char_limit"
        );
        assert_eq!(input.value(), "this is a ");
    }

    #[test]
    fn test_bracketed_paste_respects_remaining_capacity() {
        let mut input = TextInput::new();
        input.focus();
        input.char_limit = 15;
        input.set_value("hello ");

        // Paste should only insert up to the remaining capacity
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "world and more text".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.value().len(), 15);
        assert_eq!(input.value(), "hello world and");
    }

    #[test]
    fn test_bracketed_paste_at_full_capacity_ignored() {
        let mut input = TextInput::new();
        input.focus();
        input.char_limit = 5;
        input.set_value("hello");

        // Input is at capacity, paste should be ignored
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "world".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value(),
            "hello",
            "Paste at full capacity should be ignored"
        );
    }

    #[test]
    fn test_bracketed_paste_unfocused_ignored() {
        let mut input = TextInput::new();
        // Not focused!
        assert_eq!(input.value(), "");

        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "ignored".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.value(), "", "Unfocused input should ignore paste");
    }

    #[test]
    fn test_bracketed_paste_inserts_at_cursor() {
        let mut input = TextInput::new();
        input.focus();
        input.set_value("helloworld");
        input.set_cursor(5); // Position after "hello"

        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: " ".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.value(), "hello world");
        assert_eq!(input.position(), 6, "Cursor should be after pasted content");
    }

    #[test]
    fn test_bracketed_paste_strips_control_chars() {
        let mut input = TextInput::new();
        input.focus();

        // Paste with control characters that should be stripped
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "hello\x01\x02world".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value(),
            "helloworld",
            "Control characters should be stripped"
        );
    }

    #[test]
    fn test_bracketed_paste_preserves_unicode() {
        let mut input = TextInput::new();
        input.focus();

        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "hello δΈ–η•Œ 🌍".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(input.value(), "hello δΈ–η•Œ 🌍");
    }

    #[test]
    fn test_bracketed_paste_tabs_to_spaces() {
        let mut input = TextInput::new();
        input.focus();

        // Tabs should be converted to spaces
        let key_msg = Message::new(KeyMsg {
            key_type: bubbletea::KeyType::Runes,
            runes: "col1\tcol2".chars().collect(),
            alt: false,
            paste: true,
        });
        let _ = Model::update(&mut input, key_msg);

        assert_eq!(
            input.value(),
            "col1 col2",
            "Tabs should be converted to single space"
        );
    }

    #[test]
    fn test_set_value_validates_after_truncation() {
        let mut input = TextInput::new();
        input.char_limit = 3;
        // Validator fails if length > 3
        input.set_validate(|s| {
            if s.len() > 3 {
                Some("Too long".to_string())
            } else {
                None
            }
        });

        // "1234" -> truncated to "123"
        // Validation should see "123", which is valid
        input.set_value("1234");

        assert_eq!(input.value(), "123");
        assert!(
            input.err.is_none(),
            "Validation should run on truncated value"
        );
    }
}