Skip to main content

mermaid_cli/tui/state/
input.rs

1/// Input state management
2///
3/// User input buffer and cursor handling.
4
5/// Input state - user input buffer and cursor
6pub struct InputBuffer {
7    /// User input buffer
8    pub content: String,
9    /// Cursor position in the input string
10    pub cursor_position: usize,
11}
12
13impl InputBuffer {
14    /// Create a new empty input buffer
15    pub fn new() -> Self {
16        Self {
17            content: String::new(),
18            cursor_position: 0,
19        }
20    }
21
22    /// Clear the input buffer
23    pub fn clear(&mut self) {
24        self.content.clear();
25        self.cursor_position = 0;
26    }
27
28    /// Check if input is empty
29    pub fn is_empty(&self) -> bool {
30        self.content.is_empty()
31    }
32
33    /// Get the input content
34    pub fn get(&self) -> &str {
35        &self.content
36    }
37
38    /// Set the input content
39    pub fn set(&mut self, content: impl Into<String>) {
40        self.content = content.into();
41        self.cursor_position = self.content.len();
42    }
43
44    /// Insert a character at cursor position
45    pub fn insert(&mut self, c: char) {
46        self.content.insert(self.cursor_position, c);
47        self.cursor_position += 1;
48    }
49
50    /// Insert a string at cursor position
51    pub fn insert_str(&mut self, s: &str) {
52        self.content.insert_str(self.cursor_position, s);
53        self.cursor_position += s.len();
54    }
55
56    /// Delete character before cursor (backspace)
57    pub fn backspace(&mut self) -> bool {
58        if self.cursor_position > 0 {
59            self.cursor_position -= 1;
60            self.content.remove(self.cursor_position);
61            true
62        } else {
63            false
64        }
65    }
66
67    /// Delete character at cursor (delete key)
68    pub fn delete(&mut self) -> bool {
69        if self.cursor_position < self.content.len() {
70            self.content.remove(self.cursor_position);
71            true
72        } else {
73            false
74        }
75    }
76
77    /// Move cursor left
78    pub fn move_left(&mut self) {
79        if self.cursor_position > 0 {
80            self.cursor_position -= 1;
81        }
82    }
83
84    /// Move cursor right
85    pub fn move_right(&mut self) {
86        if self.cursor_position < self.content.len() {
87            self.cursor_position += 1;
88        }
89    }
90
91    /// Move cursor to start
92    pub fn move_home(&mut self) {
93        self.cursor_position = 0;
94    }
95
96    /// Move cursor to end
97    pub fn move_end(&mut self) {
98        self.cursor_position = self.content.len();
99    }
100}
101
102impl Default for InputBuffer {
103    fn default() -> Self {
104        Self::new()
105    }
106}