index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / ui / input.rs
-rw-r--r-- blob b6bd9b13f91526713f54295dd476ca9edd455058 2765 bytes (plain)
1
#[derive(PartialEq)]
2
pub enum InputMode {
3
    Normal,
4
    Editing,
5
}
6
7
pub struct Input {
8
    pub mode: InputMode,
9
    pub value: String,
10
    pub hidden_password: bool,
11
    // cursor postion x.
12
    pub cx: usize,
13
}
14
15
impl Input {
16
    pub fn new() -> Self {
17
        Self {
18
            mode: InputMode::Normal,
19
            value: String::new(),
20
            hidden_password: true,
21
            cx: 0,
22
        }
23
    }
24
25
    pub fn change_mode(&mut self, new_mode: InputMode) {
26
        self.mode = new_mode;
27
        self.value.clear();
28
        self.reset_cursor();
29
    }
30
31
    pub fn move_cursor_left(&mut self) {
32
        let cursor_moved_left = self.cx.saturating_sub(1);
33
        self.cx = self.clamp_cursor(cursor_moved_left);
34
    }
35
36
    pub fn move_cursor_right(&mut self) {
37
        let cursor_moved_right = self.cx.saturating_add(1);
38
        self.cx = self.clamp_cursor(cursor_moved_right);
39
    }
40
41
    pub fn enter_char(&mut self, new_char: char) {
42
        let index = self.byte_index();
43
        self.value.insert(index, new_char);
44
        self.move_cursor_right();
45
    }
46
47
    // Returns the byte index based on the character position.
48
    // Since each character in a string can contain multiple bytes, it's necessary to calculate
49
    // the byte index based on the index of the character.
50
    fn byte_index(&mut self) -> usize {
51
        self.value
52
            .char_indices()
53
            .map(|(i, _)| i)
54
            .nth(self.cx)
55
            .unwrap_or(self.value.len())
56
    }
57
58
    pub fn delete_char(&mut self) {
59
        let is_not_cursor_leftmost = self.cx != 0;
60
        if is_not_cursor_leftmost {
61
            // Method "remove" is not used on the saved text for deleting the selected char.
62
            // Reason: Using remove on String works on bytes instead of the chars.
63
            // Using remove would require special care because of char boundaries.
64
65
            let current_index = self.cx;
66
            let from_left_to_current_index = current_index - 1;
67
68
            // Getting all characters before the selected character.
69
            let before_char_to_delete = self.value.chars().take(from_left_to_current_index);
70
            // Getting all characters after selected character.
71
            let after_char_to_delete = self.value.chars().skip(current_index);
72
73
            // Put all characters together except the selected one.
74
            // By leaving the selected one out, it is forgotten and therefore deleted.
75
            self.value = before_char_to_delete.chain(after_char_to_delete).collect();
76
            self.move_cursor_left();
77
        }
78
    }
79
80
    fn clamp_cursor(&self, new_cursor_pos: usize) -> usize {
81
        new_cursor_pos.clamp(0, self.value.chars().count())
82
    }
83
84
    pub const fn reset_cursor(&mut self) {
85
        self.cx = 0;
86
    }
87
}