ratatui_input 0.1.3

Simple to use input widget for ratatui
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
use std::{
    cmp::{max, min},
    ops::{Deref, Range},
};

use clipboard::ClipboardProvider;

use crate::Message;

/// Stored state of the input widget. Used for the cursor position, text selection and windowing/scrolling
#[derive(Debug, PartialEq, Eq)]
pub struct InputState {
    value: String,
    cursor_char_idx: usize,
    in_focus: bool,
    insert_mode: bool,
    selection_start_char_idx: Option<usize>,
    pub(crate) view_window: ViewWindow,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ViewWindow {
    /// Width of the window
    pub(crate) width: usize,
    /// Offsett from the left for the window
    pub(crate) offsett: usize,
}

impl ViewWindow {
    pub fn contains(&self, idx: usize) -> bool {
        self.offsett <= idx && self.offsett + self.width > idx
    }
}

impl From<Range<usize>> for ViewWindow {
    fn from(value: Range<usize>) -> Self {
        ViewWindow {
            offsett: value.start,
            width: value.len(),
        }
    }
}

impl From<ViewWindow> for Range<usize> {
    fn from(val: ViewWindow) -> Self {
        val.offsett..(val.offsett + val.width)
    }
}

impl Default for InputState {
    fn default() -> Self {
        Self {
            value: String::new(),
            cursor_char_idx: 0,
            in_focus: false,
            insert_mode: false,
            selection_start_char_idx: None,
            view_window: ViewWindow {
                width: 1,
                offsett: 0,
            },
        }
    }
}

impl InputState {
    /// Update the [`InputState`] with the given message
    pub fn handle_message(&mut self, msg: Message) {
        match msg {
            Message::Empty => {}
            Message::Focus => self.in_focus = true,
            Message::RemoveFocus => self.in_focus = false,
            Message::DeleteOnCursor => {
                match self.selection() {
                    Some(selection) => {
                        // Replace the whole selection with a empty string
                        self.value.replace_range(selection.byte_range.clone(), "");

                        // Keep the cursor on the same character (not the same index)
                        self.cursor_char_idx = selection.char_range.start;

                        // End the selection
                        self.selection_start_char_idx = None;

                        // Cursor might fall outside the view window, so we move it to the left as needed
                        self.view_window.offsett =
                            min(self.view_window.offsett, self.cursor_char_idx);
                    }
                    None => {
                        if self.cursor_char_idx == self.value.chars().count() {
                            // Do nothing because we are not currently on a character
                        } else {
                            // Remove the character from the string
                            let idx = char_idx_to_byte_idx(&self.value, self.cursor_char_idx);
                            let _ = self.value.remove(idx);

                            // Cursor might fall outside the view window, so we move it to the left as needed
                            self.view_window.offsett =
                                min(self.view_window.offsett, self.cursor_char_idx);
                        }
                    }
                }
            }
            Message::DeleteBeforeCursor => {
                match self.selection() {
                    Some(selection) => {
                        // Replace the selection with a empty string
                        self.value.replace_range(selection.byte_range, "");

                        // Keep the cursor on same character (not character index)
                        self.cursor_char_idx = selection.char_range.start;

                        // End selection
                        self.selection_start_char_idx = None;

                        // Cursor might fall outside the view window, so we move it to the left as needed
                        self.view_window.offsett =
                            min(self.view_window.offsett, self.cursor_char_idx);
                    }
                    None => {
                        if self.cursor_char_idx == 0 {
                            // Do nothing because we are at the start
                        } else {
                            let idx = self
                                .value
                                .char_indices()
                                .enumerate()
                                .find(|(char_idx, _)| char_idx == &(self.cursor_char_idx - 1))
                                .map(|(_, (byte_idx, _))| byte_idx)
                                .unwrap();

                            let _ = self.value.remove(idx);
                            self.cursor_char_idx -= 1;

                            // Cursor might fall outside the view window, so we move it to the left as needed
                            self.view_window.offsett =
                                min(self.view_window.offsett, self.cursor_char_idx);
                        }
                    }
                }
            }
            Message::MoveLeft => {
                // End selection
                self.selection_start_char_idx = None;

                if self.cursor_char_idx == 0 {
                    // We are at the start already so we cannot move anymore
                } else {
                    self.cursor_char_idx -= 1;
                    self.view_window.offsett = min(self.view_window.offsett, self.cursor_char_idx);
                }
            }
            Message::MoveRight => {
                // End selection
                self.selection_start_char_idx = None;

                if self.cursor_char_idx == self.value.chars().count() {
                    // We are already 1 step ahead of the value, so we cannot move anymore
                } else {
                    self.cursor_char_idx += 1;
                    if !self.view_window.contains(self.cursor_char_idx) {
                        self.view_window.offsett =
                            self.cursor_char_idx + 1 - self.view_window.width;
                    }
                }
            }
            Message::JumpToEnd => {
                self.cursor_char_idx = self.value.chars().count();
                if !self.view_window.contains(self.cursor_char_idx) {
                    self.view_window.offsett = self.cursor_char_idx + 1 - self.view_window.width;
                }
            }
            Message::JumpToStart => {
                self.cursor_char_idx = 0;
                self.view_window.offsett = min(self.view_window.offsett, self.cursor_char_idx);
            }
            Message::Char(c) => {
                match self.selection() {
                    Some(selection) => {
                        // Replace the entire selection with the input
                        self.value
                            .replace_range(selection.byte_range, c.to_string().as_str());
                        self.cursor_char_idx = selection.char_range.start + 1;
                        self.selection_start_char_idx = None;
                        self.view_window.offsett =
                            min(self.view_window.offsett, self.cursor_char_idx);
                    }
                    None => {
                        if self.cursor_char_idx == self.value.chars().count() {
                            // We are outside the string, so we push onto it
                            self.value.push(c);
                        } else if self.insert_mode {
                            // The cursor is on a character so we replace that character
                            let start_idx = self
                                .value
                                .char_indices()
                                .enumerate()
                                .find(|(char_idx, _)| char_idx == &self.cursor_char_idx)
                                .map(|(_, (byte_idx, _))| byte_idx)
                                .unwrap();

                            let end_idx = self
                                .value
                                .char_indices()
                                .enumerate()
                                .find(|(char_idx, _)| char_idx + 1 == self.cursor_char_idx)
                                .map(|(_, (byte_idx, _))| byte_idx)
                                .unwrap_or(self.value.len());

                            self.value.replace_range(
                                start_idx..end_idx,
                                Into::<String>::into(c).as_str(),
                            );
                        } else {
                            // The cursor is on a character inside the string so we insert the character at that position
                            let idx = self
                                .value
                                .char_indices()
                                .enumerate()
                                .find(|(char_idx, _)| char_idx == &self.cursor_char_idx)
                                .map(|(_, (byte_idx, _))| byte_idx)
                                .unwrap();

                            self.value.insert(idx, c);
                        }
                        self.cursor_char_idx += 1;
                        if !self.view_window.contains(self.cursor_char_idx) {
                            self.view_window.offsett =
                                self.cursor_char_idx + 1 - self.view_window.width;
                        }
                    }
                }
            }
            Message::Paste(str) => match self.selection() {
                Some(selection) => {
                    self.value.replace_range(selection.byte_range, &str);
                    self.cursor_char_idx = selection.char_range.end;
                    self.selection_start_char_idx = None;
                    self.view_window.offsett =
                        if selection.text.chars().count() > str.chars().count() {
                            // Replaced text was longer than pasted text, so view window moves left
                            min(self.view_window.offsett, self.cursor_char_idx)
                        } else {
                            // Replaced text was shorter than pasted text, so view window moves right
                            self.cursor_char_idx + 1 - self.view_window.width
                        };
                }
                None => {
                    if self.cursor_char_idx == self.value.chars().count() {
                        self.value.push_str(str.as_str());
                    } else {
                        self.value
                            .insert_str(self.cursor_char_idx() + 1, str.as_str());
                    }
                    self.cursor_char_idx += str.chars().count();
                    if !self.view_window.contains(self.cursor_char_idx) {
                        self.view_window.offsett =
                            self.cursor_char_idx + 1 - self.view_window.width;
                    }
                }
            },
            Message::ToggleInsertMode => self.insert_mode = !self.insert_mode,
            Message::MoveLeftWithSelection => {
                if self.cursor_char_idx == 0 {
                    // We are at the very, start and cannot move anywhere
                } else {
                    self.selection_start_char_idx = match self.selection_start_char_idx {
                        Some(selection_start_char_idx) => {
                            if selection_start_char_idx == self.cursor_char_idx - 1 {
                                // The cursor has come back to the selection start so we have nothing selected anymore
                                None
                            } else {
                                // Start of the selection stays the same
                                Some(selection_start_char_idx)
                            }
                        }
                        None => {
                            // Start selection
                            if self.cursor_char_idx == self.value.chars().count() {
                                Some(self.cursor_char_idx - 1)
                            } else {
                                Some(self.cursor_char_idx)
                            }
                        }
                    };

                    self.cursor_char_idx -= 1;
                    self.view_window.offsett = min(self.view_window.offsett, self.cursor_char_idx);
                }
            }
            Message::MoveRightWithSelection => {
                if self.cursor_char_idx + 1 == self.value.chars().count() {
                    // Cannot move anymore
                } else {
                    self.selection_start_char_idx = match self.selection_start_char_idx {
                        Some(selection_start_char_idx) => {
                            if self.cursor_char_idx + 1 == selection_start_char_idx {
                                // The cursor has come back to the selection start so we have nothing selected anymore
                                None
                            } else {
                                // Start of the selection stays the same
                                Some(selection_start_char_idx)
                            }
                        }
                        None => {
                            // Start selection
                            Some(self.cursor_char_idx)
                        }
                    };

                    self.cursor_char_idx += 1;
                    if !self.view_window.contains(self.cursor_char_idx) {
                        self.view_window.offsett =
                            self.cursor_char_idx + 1 - self.view_window.width;
                    }
                }
            }
            Message::JumpToEndWithSelection => {
                if self.cursor_char_idx == self.value.chars().count() {
                    return;
                }

                if self.selection_start_char_idx.is_none() {
                    self.selection_start_char_idx = Some(self.cursor_char_idx);
                }

                self.cursor_char_idx = self.value.chars().count() - 1;
                if !self.view_window.contains(self.cursor_char_idx) {
                    self.view_window.offsett = self.cursor_char_idx + 1 - self.view_window.width;
                }
            }
            Message::JumpToStartWithSelection => {
                if self.cursor_char_idx == 0 {
                    return;
                }

                if self.selection_start_char_idx.is_none() {
                    if self.cursor_char_idx == self.value.chars().count() {
                        self.selection_start_char_idx = Some(self.cursor_char_idx - 1);
                    } else {
                        self.selection_start_char_idx = Some(self.cursor_char_idx);
                    }
                }

                self.cursor_char_idx = 0;
                self.view_window.offsett = min(self.view_window.offsett, self.cursor_char_idx);
            }
            Message::Copy => match self.selection() {
                Some(selection) => {
                    // Copy the selection
                    let _ = clipboard::ClipboardContext::new()
                        .and_then(|mut cc| cc.set_contents(selection.to_string()));
                }
                None => {
                    // No selection, so we copy the entire value
                    let _ = clipboard::ClipboardContext::new()
                        .and_then(|mut cc| cc.set_contents(self.value.clone()));
                }
            },
            Message::Cut => {
                match self.selection() {
                    Some(selection) => {
                        // Cut the selection and set cursor to the start of the selecion
                        let _ = clipboard::ClipboardContext::new()
                            .and_then(|mut cc| cc.set_contents(selection.to_string()));
                        self.cursor_char_idx = selection.char_range.start;
                        self.selection_start_char_idx = None;
                        let mut taken_iter = (0..self.value.chars().count())
                            .map(|char_idx| selection.char_range.contains(&char_idx));
                        self.value.retain(|_| !taken_iter.next().unwrap());
                    }
                    None => {
                        // Copy the entire value and then clear it
                        let _ = clipboard::ClipboardContext::new()
                            .and_then(|mut cc| cc.set_contents(self.value.clone()));
                        self.value.clear();
                        self.cursor_char_idx = 0;
                        self.selection_start_char_idx = None;
                    }
                };
                self.view_window.offsett = min(self.view_window.offsett, self.cursor_char_idx);
            }
        }
    }

    /// Current value of the input
    pub fn text(&self) -> &str {
        &self.value
    }

    #[allow(unused)]
    pub(crate) fn cursor_byte_idx(&self) -> usize {
        char_idx_to_byte_idx(&self.value, self.cursor_char_idx)
    }

    pub(crate) fn cursor_char_idx(&self) -> usize {
        self.cursor_char_idx
    }

    /// Currently selected text
    pub fn selection(&self) -> Option<Selection> {
        match self.selection_start_char_idx {
            Some(start_char_idx) => {
                let min_char_idx = min(start_char_idx, self.cursor_char_idx);
                let min_byte_idx = char_idx_to_byte_idx(&self.value, min_char_idx);
                let max_char_idx = max(start_char_idx, self.cursor_char_idx) + 1;
                let max_byte_idx = char_idx_to_byte_idx(&self.value, max_char_idx);

                Some(Selection {
                    char_range: min_char_idx..max_char_idx,
                    byte_range: min_byte_idx..max_byte_idx,
                    text: self.value[min_byte_idx..max_byte_idx].to_string(),
                })
            }
            None => None,
        }
    }
}

fn char_idx_to_byte_idx(str: &str, char_idx: usize) -> usize {
    str.char_indices()
        .enumerate()
        .find(|(idx, _)| idx == &char_idx)
        .map(|(_, (idx, _))| idx)
        .unwrap_or(str.len())
}

/// Selected text inside the [`InputState`]
#[derive(Debug)]
pub struct Selection {
    pub(crate) char_range: Range<usize>,
    pub(crate) byte_range: Range<usize>,
    text: String,
}

impl Deref for Selection {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.text
    }
}

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

    #[test]
    fn empty_message() {
        let mut state = InputState::default();
        state.handle_message(Message::Empty);
        assert_eq!(state, InputState::default());
    }

    #[test]
    fn focus() {
        let mut state = InputState::default();
        assert!(!state.in_focus);

        state.handle_message(Message::Focus);
        assert!(state.in_focus);

        state.handle_message(Message::RemoveFocus);
        assert!(!state.in_focus);
    }

    #[test]
    fn delete_on_cursor() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            ..Default::default()
        };

        state.handle_message(Message::DeleteOnCursor);

        assert_eq!(state.text(), "žđščć🎈👓");
        assert_eq!(state.cursor_char_idx(), 6);
    }

    #[test]
    fn delete_on_cursor_with_selection() {
        //žđščć[🎈🎨]👓
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            selection_start_char_idx: Some(5),
            ..Default::default()
        };

        assert_eq!(&*state.selection().unwrap(), "🎈🎨");
        state.handle_message(Message::DeleteOnCursor);

        //žđšč[ć]👓
        assert_eq!(state.text(), "žđščć👓");
        assert_eq!(state.cursor_char_idx(), 5);
    }

    #[test]
    fn delete_before_cursor() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            ..Default::default()
        };

        state.handle_message(Message::DeleteBeforeCursor);

        assert_eq!(state.text(), "žđščć🎨👓");
    }

    #[test]
    fn delete_before_cursor_with_selection() {
        //žđšč[ć🎈🎨]👓
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            selection_start_char_idx: Some(4),
            ..Default::default()
        };

        assert_eq!(&*state.selection().unwrap(), "ć🎈🎨");

        state.handle_message(Message::DeleteBeforeCursor);

        assert_eq!(state.text(), "žđšč👓");
        assert_eq!(state.cursor_char_idx, 4);
    }

    #[test]
    fn move_left() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 7,
            ..Default::default()
        };

        state.handle_message(Message::MoveLeft);
        assert_eq!(state.cursor_char_idx, 6);

        state.handle_message(Message::MoveLeft);
        assert_eq!(state.cursor_char_idx, 5);
    }

    #[test]
    fn move_left_cancles_selection() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 7,
            selection_start_char_idx: Some(5),
            ..Default::default()
        };

        state.handle_message(Message::MoveLeft);
        assert!(state.selection().is_none());
    }

    #[test]
    fn move_left_on_start() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            ..Default::default()
        };

        state.handle_message(Message::MoveLeft);

        assert_eq!(state.cursor_char_idx(), 0);
    }

    #[test]
    fn move_left_with_selection() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            ..Default::default()
        };

        state.handle_message(Message::MoveLeftWithSelection);

        assert_eq!(&*state.selection().unwrap(), "🎈🎨");
        assert_eq!(state.cursor_char_idx(), 5);
    }

    #[test]
    fn move_right() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            ..Default::default()
        };

        state.handle_message(Message::MoveRight);
        assert_eq!(state.cursor_char_idx(), 1);

        state.handle_message(Message::MoveRight);
        assert_eq!(state.cursor_char_idx(), 2);
    }

    #[test]
    fn moving_right_cancles_selection() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            selection_start_char_idx: Some(5),
            ..Default::default()
        };

        state.handle_message(Message::MoveRight);
        assert!(state.selection().is_none());
    }

    #[test]
    fn move_right_on_end() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 8,
            ..Default::default()
        };

        state.handle_message(Message::MoveRight);

        assert_eq!(state.cursor_char_idx(), 8);
    }

    #[test]
    fn move_right_with_selecion() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            ..Default::default()
        };

        state.handle_message(Message::MoveRightWithSelection);

        assert_eq!(&*state.selection().unwrap(), "žđ");
        assert_eq!(state.cursor_char_idx(), 1);
    }

    #[test]
    fn jump_to_end() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            ..Default::default()
        };

        state.handle_message(Message::JumpToEnd);

        assert_eq!(state.cursor_byte_idx(), state.text().len());
    }

    #[test]
    fn jump_to_end_with_selection() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            ..Default::default()
        };

        state.handle_message(Message::JumpToEndWithSelection);

        assert_eq!(&*state.selection().unwrap(), state.text());
        assert_eq!(state.cursor_char_idx(), 7);
    }

    #[test]
    fn jump_to_start() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 7,
            ..Default::default()
        };

        state.handle_message(Message::JumpToStart);

        assert_eq!(state.cursor_char_idx(), 0);
    }

    #[test]
    fn jump_to_start_with_selection() {
        let mut state = InputState {
            value: String::from("žđščć🎈🎨👓"),
            cursor_char_idx: 6,
            ..Default::default()
        };

        state.handle_message(Message::JumpToStartWithSelection);

        assert_eq!(state.cursor_char_idx(), 0);
        assert_eq!(&*state.selection().unwrap(), "žđščć🎈🎨");
    }

    #[test]
    fn character_input_at_end() {
        let mut state = InputState {
            value: String::new(),
            ..Default::default()
        };

        state.handle_message(Message::Char('0'));
        assert_eq!(state.text(), "0");
        assert_eq!(state.cursor_byte_idx(), 1);

        state.handle_message(Message::Char('1'));
        assert_eq!(state.text(), "01");
        assert_eq!(state.cursor_byte_idx(), 2);
    }

    #[test]
    fn character_input_in_middle() {
        let mut state = InputState {
            value: String::from("foo bar"),
            cursor_char_idx: 3,
            ..Default::default()
        };

        state.handle_message(Message::Char(' '));
        state.handle_message(Message::Char('😎'));

        assert_eq!(state.text(), "foo 😎 bar");
        assert_eq!(state.cursor_char_idx(), 5);
    }

    #[test]
    fn character_input_at_end_with_insert_mode() {
        let mut state = InputState {
            value: String::new(),
            insert_mode: true,
            ..Default::default()
        };

        state.handle_message(Message::Char('0'));
        assert_eq!(state.text(), "0");
        assert_eq!(state.cursor_byte_idx(), 1);

        state.handle_message(Message::Char('1'));
        assert_eq!(state.text(), "01");
        assert_eq!(state.cursor_byte_idx(), 2);
    }

    #[test]
    fn character_input_in_insert_mode() {
        let mut state = InputState {
            value: String::from("foo bar"),
            insert_mode: true,
            ..Default::default()
        };

        state.handle_message(Message::Char('h'));
        state.handle_message(Message::Char('e'));
        state.handle_message(Message::Char('l'));
        state.handle_message(Message::Char('l'));
        state.handle_message(Message::Char('o'));
        state.handle_message(Message::Char(' '));
        state.handle_message(Message::Char('w'));
        state.handle_message(Message::Char('o'));
        state.handle_message(Message::Char('r'));
        state.handle_message(Message::Char('l'));
        state.handle_message(Message::Char('d'));

        assert_eq!(state.text(), "hello world");
        assert_eq!(state.cursor_char_idx(), 11);
    }

    #[test]
    fn character_input_on_selection() {
        let mut state = InputState {
            value: String::from("foo bar"),
            cursor_char_idx: 5,
            selection_start_char_idx: Some(1),
            ..Default::default()
        };

        state.handle_message(Message::Char('a'));

        assert_eq!(state.text(), "far");
        assert_eq!(state.cursor_char_idx(), 2);
    }

    #[test]
    fn character_input_at_end_in_insert_mode() {
        let mut state = InputState {
            value: String::new(),
            insert_mode: true,
            ..Default::default()
        };

        state.handle_message(Message::Char('0'));
        assert_eq!(state.text(), "0");
        assert_eq!(state.cursor_byte_idx(), 1);

        state.handle_message(Message::Char('1'));
        assert_eq!(state.text(), "01");
        assert_eq!(state.cursor_byte_idx(), 2);
    }

    #[test]
    fn paste_at_end() {
        let mut state = InputState {
            value: String::from("foo"),
            cursor_char_idx: 3,
            ..Default::default()
        };

        state.handle_message(Message::Paste(String::from(" bar")));

        assert_eq!(state.text(), "foo bar");
        assert_eq!(state.cursor_char_idx(), 7);
    }

    #[test]
    fn paste_in_middle() {
        let mut state = InputState {
            value: String::from("foo bar"),
            cursor_char_idx: 3,
            ..Default::default()
        };

        state.handle_message(Message::Paste(String::from("baz ")));

        assert_eq!(state.text(), "foo baz bar");
        assert_eq!(state.cursor_char_idx(), 7);
    }

    #[test]
    fn paste_on_selection() {
        let mut state = InputState {
            value: String::from("foo bar"),
            cursor_char_idx: 4,
            selection_start_char_idx: Some(2),
            ..Default::default()
        };

        state.handle_message(Message::Paste(String::from("faz")));

        assert_eq!(state.text(), "fofazar");
        assert_eq!(state.cursor_char_idx(), 5);
        assert!(state.selection().is_none());
    }

    #[test]
    fn insert_mode_toggle() {
        let mut state = InputState::default();

        assert!(!state.insert_mode);

        state.handle_message(Message::ToggleInsertMode);

        assert!(state.insert_mode);
    }
}