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 ddd9f66b8e32e7e6ce379a7b5170e8b61ad46120 2629 bytes (plain)
1
#[derive(Debug, Clone, 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 move_cursor_left(&mut self) {
26
        let cursor_moved_left = self.cx.saturating_sub(1);
27
        self.cx = self.clamp_cursor(cursor_moved_left);
28
    }
29
30
    pub fn move_cursor_right(&mut self) {
31
        let cursor_moved_right = self.cx.saturating_add(1);
32
        self.cx = self.clamp_cursor(cursor_moved_right);
33
    }
34
35
    pub fn enter_char(&mut self, new_char: char) {
36
        let index = self.byte_index();
37
        self.value.insert(index, new_char);
38
        self.move_cursor_right();
39
    }
40
41
    // Returns the byte index based on the character position.
42
    // Since each character in a string can contain multiple bytes, it's necessary to calculate
43
    // the byte index based on the index of the character.
44
    fn byte_index(&mut self) -> usize {
45
        self.value
46
            .char_indices()
47
            .map(|(i, _)| i)
48
            .nth(self.cx)
49
            .unwrap_or(self.value.len())
50
    }
51
52
    pub fn delete_char(&mut self) {
53
        let is_not_cursor_leftmost = self.cx != 0;
54
        if is_not_cursor_leftmost {
55
            // Method "remove" is not used on the saved text for deleting the selected char.
56
            // Reason: Using remove on String works on bytes instead of the chars.
57
            // Using remove would require special care because of char boundaries.
58
59
            let current_index = self.cx;
60
            let from_left_to_current_index = current_index - 1;
61
62
            // Getting all characters before the selected character.
63
            let before_char_to_delete = self.value.chars().take(from_left_to_current_index);
64
            // Getting all characters after selected character.
65
            let after_char_to_delete = self.value.chars().skip(current_index);
66
67
            // Put all characters together except the selected one.
68
            // By leaving the selected one out, it is forgotten and therefore deleted.
69
            self.value = before_char_to_delete.chain(after_char_to_delete).collect();
70
            self.move_cursor_left();
71
        }
72
    }
73
74
    fn clamp_cursor(&self, new_cursor_pos: usize) -> usize {
75
        new_cursor_pos.clamp(0, self.value.chars().count())
76
    }
77
78
    pub const fn reset_cursor(&mut self) {
79
        self.cx = 0;
80
    }
81
}