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
//! ## Input
//!
//! `Input` represents a read-write input field. This component supports different input types, input length
//! and handles input events related to cursor position, backspace, canc, ...

/**
 * MIT License
 *
 * tui-realm - Copyright (C) 2021 Christian Visintin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
use crate::utils::calc_utf8_cursor_position;
use tuirealm::event::{Event, KeyCode, KeyModifiers};
use tuirealm::props::{
    Alignment, BlockTitle, BordersProps, PropPayload, PropValue, Props, PropsBuilder,
};
use tuirealm::tui::{
    layout::Rect,
    style::{Color, Style},
    widgets::{Block, BorderType, Borders, Paragraph},
};
use tuirealm::{Component, Frame, InputType, Msg, Payload, Value};

// -- Props
const PROP_VALUE: &str = "value";
const PROP_INPUT_TYPE: &str = "input_type";
const PROP_INPUT_LENGHT: &str = "input_length";

pub struct InputPropsBuilder {
    props: Option<Props>,
}

impl Default for InputPropsBuilder {
    fn default() -> Self {
        InputPropsBuilder {
            props: Some(Props::default()),
        }
    }
}

impl PropsBuilder for InputPropsBuilder {
    fn build(&mut self) -> Props {
        self.props.take().unwrap()
    }

    fn hidden(&mut self) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.visible = false;
        }
        self
    }

    fn visible(&mut self) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.visible = true;
        }
        self
    }
}

impl From<Props> for InputPropsBuilder {
    fn from(props: Props) -> Self {
        InputPropsBuilder { props: Some(props) }
    }
}

impl InputPropsBuilder {
    /// ### with_foreground
    ///
    /// Set foreground color for component
    pub fn with_foreground(&mut self, color: Color) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.foreground = color;
        }
        self
    }

    /// ### with_background
    ///
    /// Set background color for component
    pub fn with_background(&mut self, color: Color) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.background = color;
        }
        self
    }

    /// ### with_borders
    ///
    /// Set component borders style
    pub fn with_borders(
        &mut self,
        borders: Borders,
        variant: BorderType,
        color: Color,
    ) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.borders = BordersProps {
                borders,
                variant,
                color,
            }
        }
        self
    }

    /// ### with_label
    ///
    /// Set input label
    pub fn with_label<S: AsRef<str>>(&mut self, title: S, alignment: Alignment) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.title = Some(BlockTitle::new(title, alignment));
        }
        self
    }

    /// ### with_input
    ///
    /// Set input type for component
    pub fn with_input(&mut self, input_type: InputType) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_INPUT_TYPE,
                PropPayload::One(PropValue::InputType(input_type)),
            );
        }
        self
    }

    /// ### with_input_len
    ///
    /// Set max input len
    pub fn with_input_len(&mut self, len: usize) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props
                .own
                .insert(PROP_INPUT_LENGHT, PropPayload::One(PropValue::Usize(len)));
        }
        self
    }

    /// ### with_value
    ///
    /// Set initial value for component
    pub fn with_value(&mut self, value: String) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props
                .own
                .insert(PROP_VALUE, PropPayload::One(PropValue::Str(value)));
        }
        self
    }
}

// -- states

#[derive(Clone)]
struct OwnStates {
    input: Vec<char>, // Current input
    cursor: usize,    // Input position
    focus: bool,      // Focus
}

impl Default for OwnStates {
    fn default() -> Self {
        OwnStates {
            input: Vec::new(),
            cursor: 0,
            focus: false,
        }
    }
}

impl OwnStates {
    /// ### append
    ///
    /// Append, if possible according to input type, the character to the input vec
    pub fn append(&mut self, ch: char, itype: InputType, max_len: Option<usize>) {
        // Check if max length has been reached
        if self.input.len() < max_len.unwrap_or(usize::MAX) {
            match itype {
                InputType::Number => {
                    if ch.is_digit(10) {
                        // Must be digit
                        self.input.insert(self.cursor, ch);
                        // Increment cursor
                        self.cursor += 1;
                    }
                }
                _ => {
                    // No rule
                    self.input.insert(self.cursor, ch);
                    // Increment cursor
                    self.cursor += 1;
                }
            }
        }
    }

    /// ### backspace
    ///
    /// Delete element at cursor -1; then decrement cursor by 1
    pub fn backspace(&mut self) {
        if self.cursor > 0 && !self.input.is_empty() {
            self.input.remove(self.cursor - 1);
            // Decrement cursor
            self.cursor -= 1;
        }
    }

    /// ### delete
    ///
    /// Delete element at cursor
    pub fn delete(&mut self) {
        if self.cursor < self.input.len() {
            self.input.remove(self.cursor);
        }
    }

    /// ### incr_cursor
    ///
    /// Increment cursor value by one if possible
    pub fn incr_cursor(&mut self) {
        if self.cursor < self.input.len() {
            self.cursor += 1;
        }
    }

    /// ### cursoro_at_begin
    ///
    /// Place cursor at the begin of the input
    pub fn cursor_at_begin(&mut self) {
        self.cursor = 0;
    }

    /// ### cursor_at_end
    ///
    /// Place cursor at the end of the input
    pub fn cursor_at_end(&mut self) {
        self.cursor = self.input.len();
    }

    /// ### decr_cursor
    ///
    /// Decrement cursor value by one if possible
    pub fn decr_cursor(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// ### render_value
    ///
    /// Get value as string to render
    pub fn render_value(&self, itype: InputType) -> String {
        self.render_value_chars(itype).iter().collect::<String>()
    }

    /// ### render_value_chars
    ///
    /// Render value as a vec of chars
    pub fn render_value_chars(&self, itype: InputType) -> Vec<char> {
        match itype {
            InputType::Password => (0..self.input.len()).map(|_| '*').collect(),
            _ => self.input.clone(),
        }
    }

    /// ### get_value
    ///
    /// Get value as string
    pub fn get_value(&self) -> String {
        self.input.iter().collect()
    }
}

// -- Component

/// ## Input
///
/// Input list component
pub struct Input {
    props: Props,
    states: OwnStates,
}

impl Input {
    /// ### new
    ///
    /// Instantiates a new Input starting from Props
    /// The method also initializes the component states.
    pub fn new(props: Props) -> Self {
        // Initialize states
        let mut states: OwnStates = OwnStates::default();
        // Input type
        // Set state value from props
        if let Some(PropPayload::One(PropValue::Str(val))) = props.own.get(PROP_VALUE) {
            for ch in val.chars() {
                states.append(
                    ch,
                    Self::get_input_type(&props),
                    Self::get_input_len(&props),
                );
            }
        }
        Input { props, states }
    }

    fn get_input_type(props: &Props) -> InputType {
        match props.own.get(PROP_INPUT_TYPE) {
            Some(PropPayload::One(PropValue::InputType(itype))) => *itype,
            _ => InputType::Text, // Default
        }
    }

    fn get_input_len(props: &Props) -> Option<usize> {
        match props.own.get(PROP_INPUT_LENGHT) {
            Some(PropPayload::One(PropValue::Usize(ilen))) => Some(*ilen),
            _ => None, // Default
        }
    }
}

impl Component for Input {
    /// ### render
    ///
    /// Based on the current properties and states, renders a widget using the provided render engine in the provided Area
    /// If focused, cursor is also set (if supported by widget)
    fn render(&self, render: &mut Frame, area: Rect) {
        if self.props.visible {
            let div: Block = crate::utils::get_block(
                &self.props.borders,
                self.props.title.as_ref(),
                self.states.focus,
            );
            let p: Paragraph =
                Paragraph::new(self.states.render_value(Self::get_input_type(&self.props)))
                    .style(match self.states.focus {
                        true => Style::default().fg(self.props.foreground),
                        false => Style::default(),
                    })
                    .block(div);
            render.render_widget(p, area);
            // Set cursor, if focus
            if self.states.focus {
                let x: u16 = area.x
                    + calc_utf8_cursor_position(
                        &self
                            .states
                            .render_value_chars(Self::get_input_type(&self.props))
                            [0..self.states.cursor],
                    )
                    + 1;
                render.set_cursor(x, area.y + 1);
            }
        }
    }

    /// ### update
    ///
    /// Update component properties
    /// Properties should first be retrieved through `get_props` which creates a builder from
    /// existing properties and then edited before calling update.
    /// Returns a Msg to the view
    fn update(&mut self, props: Props) -> Msg {
        self.props = props;
        // Set value from props
        if let Some(PropPayload::One(PropValue::Str(val))) = self.props.own.get(PROP_VALUE) {
            let prev_input = self.states.input.clone();
            self.states.input = Vec::new();
            self.states.cursor = 0;
            for ch in val.chars() {
                self.states.append(
                    ch,
                    Self::get_input_type(&self.props),
                    Self::get_input_len(&self.props),
                );
            }
            if prev_input != self.states.input {
                Msg::OnChange(self.get_state())
            } else {
                Msg::None
            }
        } else {
            Msg::None
        }
    }

    /// ### get_props
    ///
    /// Returns a props builder starting from component properties.
    /// This returns a prop builder in order to make easier to create
    /// new properties for the element.
    fn get_props(&self) -> Props {
        // Make properties with value from states
        let mut props: Props = self.props.clone();
        props.own.insert(
            PROP_VALUE,
            PropPayload::One(PropValue::Str(self.states.get_value())),
        );
        props
    }

    /// ### on
    ///
    /// Handle input event and update internal states.
    /// Returns a Msg to the view
    fn on(&mut self, ev: Event) -> Msg {
        if let Event::Key(key) = ev {
            match key.code {
                KeyCode::Backspace => {
                    // Backspace and None
                    let prev_input = self.states.input.clone();
                    self.states.backspace();
                    if prev_input != self.states.input {
                        Msg::OnChange(self.get_state())
                    } else {
                        Msg::None
                    }
                }
                KeyCode::Delete => {
                    // Delete and None
                    let prev_input = self.states.input.clone();
                    self.states.delete();
                    if prev_input != self.states.input {
                        Msg::OnChange(self.get_state())
                    } else {
                        Msg::None
                    }
                }
                KeyCode::Enter => Msg::OnSubmit(self.get_state()),
                KeyCode::Left => {
                    // Move cursor left; msg None
                    self.states.decr_cursor();
                    Msg::None
                }
                KeyCode::Right => {
                    // Move cursor right; Msg None
                    self.states.incr_cursor();
                    Msg::None
                }
                KeyCode::End => {
                    // Cursor at last position
                    self.states.cursor_at_end();
                    Msg::None
                }
                KeyCode::Home => {
                    // Cursor at first positon
                    self.states.cursor_at_begin();
                    Msg::None
                }
                KeyCode::Char(ch) => {
                    // Check if modifiers is NOT CTRL OR ALT, BUT CAN BE CRTL+ALT
                    if (!key.modifiers.intersects(KeyModifiers::CONTROL)
                        && !key.modifiers.intersects(KeyModifiers::ALT))
                        || key.modifiers == KeyModifiers::CONTROL | KeyModifiers::ALT
                        || key.modifiers
                            == KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SHIFT
                    {
                        // Push char to input
                        let prev_input = self.states.input.clone();
                        self.states.append(
                            ch,
                            Self::get_input_type(&self.props),
                            Self::get_input_len(&self.props),
                        );
                        // Message on change
                        if prev_input != self.states.input {
                            Msg::OnChange(self.get_state())
                        } else {
                            Msg::None
                        }
                    } else {
                        // Return key
                        Msg::OnKey(key)
                    }
                }
                _ => Msg::OnKey(key),
            }
        } else {
            Msg::None
        }
    }

    /// ### get_state
    ///
    /// Get current state from component
    /// For this component returns Unsigned if the input type is a number, otherwise a text
    /// The value is always the current input.
    fn get_state(&self) -> Payload {
        match Self::get_input_type(&self.props) {
            InputType::Number => Payload::One(Value::Usize(
                self.states.get_value().parse::<usize>().ok().unwrap_or(0),
            )),
            _ => Payload::One(Value::Str(self.states.get_value())),
        }
    }

    // -- events

    /// ### blur
    ///
    /// Blur component; basically remove focus
    fn blur(&mut self) {
        self.states.focus = false;
    }

    /// ### active
    ///
    /// Active component; basically give focus
    fn active(&mut self) {
        self.states.focus = true;
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    use crossterm::event::KeyEvent;
    use pretty_assertions::assert_eq;
    use tuirealm::tui::style::Color;

    #[test]
    fn test_components_input_states() {
        let mut states: OwnStates = OwnStates::default();
        states.append('a', InputType::Text, Some(3));
        assert_eq!(states.input, vec!['a']);
        states.append('b', InputType::Text, Some(3));
        assert_eq!(states.input, vec!['a', 'b']);
        states.append('c', InputType::Text, Some(3));
        assert_eq!(states.input, vec!['a', 'b', 'c']);
        // Reached length
        states.append('d', InputType::Text, Some(3));
        assert_eq!(states.input, vec!['a', 'b', 'c']);
        // Push char to numbers
        states.append('d', InputType::Number, None);
        assert_eq!(states.input, vec!['a', 'b', 'c']);
        // move cursor
        // decr cursor
        states.decr_cursor();
        assert_eq!(states.cursor, 2);
        states.cursor = 1;
        states.decr_cursor();
        assert_eq!(states.cursor, 0);
        states.decr_cursor();
        assert_eq!(states.cursor, 0);
        // Incr
        states.incr_cursor();
        assert_eq!(states.cursor, 1);
        states.incr_cursor();
        assert_eq!(states.cursor, 2);
        states.incr_cursor();
        assert_eq!(states.cursor, 3);
        // Render value
        assert_eq!(states.render_value(InputType::Text).as_str(), "abc");
        assert_eq!(states.render_value(InputType::Password).as_str(), "***");
    }

    #[test]
    fn test_components_input_text() {
        // Instantiate Input with value
        let mut component: Input = Input::new(
            InputPropsBuilder::default()
                .with_input(InputType::Text)
                .visible()
                .with_input_len(5)
                .with_value(String::from("home"))
                .with_label("input", Alignment::Center)
                .with_foreground(Color::Red)
                .with_background(Color::White)
                .with_borders(Borders::ALL, BorderType::Double, Color::Red)
                .build(),
        );
        assert_eq!(component.props.foreground, Color::Red);
        assert_eq!(component.props.background, Color::White);
        assert_eq!(component.props.visible, true);
        assert_eq!(component.props.borders.borders, Borders::ALL);
        assert_eq!(component.props.borders.variant, BorderType::Double);
        assert_eq!(component.props.borders.color, Color::Red);
        assert_eq!(component.props.title.as_ref().unwrap().text(), "input");
        assert_eq!(
            component.props.title.as_ref().unwrap().alignment(),
            Alignment::Center
        );
        assert_eq!(
            *component.props.own.get(PROP_VALUE).unwrap(),
            PropPayload::One(PropValue::Str(String::from("home")))
        );
        // Verify initial state
        assert_eq!(component.states.cursor, 4);
        assert_eq!(component.states.input.len(), 4);
        // Focus
        assert_eq!(component.states.focus, false);
        component.active();
        assert_eq!(component.states.focus, true);
        component.blur();
        assert_eq!(component.states.focus, false);
        // Update
        let props = InputPropsBuilder::from(component.get_props())
            .with_foreground(Color::Red)
            .hidden()
            .build();
        assert_eq!(component.update(props), Msg::None);
        assert_eq!(component.props.visible, false);
        assert_eq!(component.props.foreground, Color::Red);
        // Get value
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("home")))
        );
        // RenderData
        //assert_eq!(component.render().unwrap().cursor, 4);
        assert_eq!(component.states.cursor, 4);
        // Handle events
        // Try key with ctrl
        assert_eq!(
            component.on(Event::Key(KeyEvent::new(
                KeyCode::Char('a'),
                KeyModifiers::CONTROL
            ))),
            Msg::OnKey(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL)),
        );
        // String shouldn't have changed
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("home")))
        );
        // CTRL+ALT
        assert_eq!(
            component.on(Event::Key(KeyEvent::new(
                KeyCode::Char('a'),
                KeyModifiers::CONTROL | KeyModifiers::ALT
            ))),
            Msg::OnChange(Payload::One(Value::Str(String::from("homea"))))
        );
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Backspace))),
            Msg::OnChange(Payload::One(Value::Str(String::from("home"))))
        );
        // CTRL + ALT + SHIFT
        assert_eq!(
            component.on(Event::Key(KeyEvent::new(
                KeyCode::Char('a'),
                KeyModifiers::CONTROL | KeyModifiers::SHIFT | KeyModifiers::ALT
            ))),
            Msg::OnChange(Payload::One(Value::Str(String::from("homea"))))
        );
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Backspace))),
            Msg::OnChange(Payload::One(Value::Str(String::from("home"))))
        );
        //assert_eq!(component.render().unwrap().cursor, 4);
        assert_eq!(component.states.cursor, 4);
        // Character
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('/')))),
            Msg::OnChange(Payload::One(Value::Str(String::from("home/"))))
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("home/")))
        );
        //assert_eq!(component.render().unwrap().cursor, 5);
        assert_eq!(component.states.cursor, 5);
        // Verify max length (shouldn't push any character)
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('a')))),
            Msg::None
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("home/")))
        );
        //assert_eq!(component.render().unwrap().cursor, 5);
        assert_eq!(component.states.cursor, 5);
        // Enter
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Enter))),
            Msg::OnSubmit(Payload::One(Value::Str(String::from("home/"))))
        );
        // Backspace
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Backspace))),
            Msg::OnChange(Payload::One(Value::Str(String::from("home"))))
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("home")))
        );
        //assert_eq!(component.render().unwrap().cursor, 4);
        assert_eq!(component.states.cursor, 4);
        // Check backspace at 0
        component.states.input = vec!['h'];
        component.states.cursor = 1;
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Backspace))),
            Msg::OnChange(Payload::One(Value::Str(String::from(""))))
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("")))
        );
        //assert_eq!(component.render().unwrap().cursor, 0);
        assert_eq!(component.states.cursor, 0);
        // Another one...
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Backspace))),
            Msg::None
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("")))
        );
        //assert_eq!(component.render().unwrap().cursor, 0);
        assert_eq!(component.states.cursor, 0);
        // See del behaviour here
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Delete))),
            Msg::None
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("")))
        );
        //assert_eq!(component.render().unwrap().cursor, 0);
        assert_eq!(component.states.cursor, 0);
        // Check del behaviour
        component.states.input = vec!['h', 'e'];
        component.states.cursor = 1;
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Delete))),
            Msg::OnChange(Payload::One(Value::Str(String::from("h"))))
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("h")))
        );
        //assert_eq!(component.render().unwrap().cursor, 1); // Shouldn't move
        assert_eq!(component.states.cursor, 1);
        // Another one (should do nothing)
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Delete))),
            Msg::None
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("h")))
        );
        //assert_eq!(component.render().unwrap().cursor, 1); // Shouldn't move
        assert_eq!(component.states.cursor, 1);
        // Move cursor right
        component.states.input = vec!['h', 'e', 'l', 'l', 'o'];
        let props = InputPropsBuilder::from(component.get_props())
            .with_input_len(16)
            .hidden()
            .build();
        assert_eq!(component.update(props), Msg::None);
        component.states.cursor = 1;
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Right))), // between 'e' and 'l'
            Msg::None
        );
        //assert_eq!(component.render().unwrap().cursor, 2); // Should increment
        assert_eq!(component.states.cursor, 2);
        // Put a character here
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('a')))),
            Msg::OnChange(Payload::One(Value::Str(String::from("heallo"))))
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("heallo")))
        );
        //assert_eq!(component.render().unwrap().cursor, 3);
        assert_eq!(component.states.cursor, 3);
        // Move left
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Left))),
            Msg::None
        );
        //assert_eq!(component.render().unwrap().cursor, 2); // Should decrement
        assert_eq!(component.states.cursor, 2);
        // Go at the end
        component.states.cursor = 6;
        // Move right
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Right))),
            Msg::None
        );
        //assert_eq!(component.render().unwrap().cursor, 6); // Should stay
        assert_eq!(component.states.cursor, 6);
        // Move left
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Left))),
            Msg::None
        );
        //assert_eq!(component.render().unwrap().cursor, 5); // Should decrement
        assert_eq!(component.states.cursor, 5);
        // Go at the beginning
        component.states.cursor = 0;
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Left))),
            Msg::None
        );
        //assert_eq!(component.render().unwrap().cursor, 0); // Should stay
        assert_eq!(component.states.cursor, 0);
        // End - begin
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::End))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 6);
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Home))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 0);
        // Update value
        assert_eq!(
            component.update(
                InputPropsBuilder::from(component.get_props())
                    .with_value("new-value".to_string())
                    .build(),
            ),
            Msg::OnChange(Payload::One(Value::Str(String::from("new-value"))))
        );
        assert_eq!(
            component.update(
                InputPropsBuilder::from(component.get_props())
                    .with_value("new-value".to_string())
                    .build(),
            ),
            Msg::None // Didn't change at all
        );
        assert_eq!(
            component.get_state(),
            Payload::One(Value::Str(String::from("new-value")))
        );
        assert_eq!(component.on(Event::Resize(0, 0)), Msg::None);
    }

    #[test]
    fn test_components_input_number() {
        // Instantiate Input with value
        let mut component: Input = Input::new(
            InputPropsBuilder::default()
                .with_input(InputType::Number)
                .with_input_len(5)
                .with_value(String::from("3000"))
                .build(),
        );
        // Verify initial state
        assert_eq!(component.states.cursor, 4);
        assert_eq!(component.states.input.len(), 4);
        // Push a non numeric value
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('a')))),
            Msg::None
        );
        assert_eq!(component.get_state(), Payload::One(Value::Usize(3000)));
        //assert_eq!(component.render().unwrap().cursor, 4);
        assert_eq!(component.states.cursor, 4);
        // Push a number
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('1')))),
            Msg::OnChange(Payload::One(Value::Usize(30001)))
        );
        assert_eq!(component.get_state(), Payload::One(Value::Usize(30001)));
        //assert_eq!(component.render().unwrap().cursor, 5);
        assert_eq!(component.states.cursor, 5);
    }
}