mermaid_cli/tui/state/
input.rs1pub struct InputBuffer {
11 pub content: String,
13 pub cursor_position: usize,
15}
16
17impl InputBuffer {
18 pub fn new() -> Self {
20 Self {
21 content: String::new(),
22 cursor_position: 0,
23 }
24 }
25
26 pub fn clear(&mut self) {
28 self.content.clear();
29 self.cursor_position = 0;
30 }
31
32 pub fn is_empty(&self) -> bool {
34 self.content.is_empty()
35 }
36
37 pub fn get(&self) -> &str {
39 &self.content
40 }
41
42 pub fn set(&mut self, content: impl Into<String>) {
44 self.content = content.into();
45 self.cursor_position = self.content.len();
46 }
47
48 pub fn insert(&mut self, c: char) {
50 self.content.insert(self.cursor_position, c);
51 self.cursor_position += c.len_utf8();
52 }
53
54 pub fn insert_str(&mut self, s: &str) {
56 self.content.insert_str(self.cursor_position, s);
57 self.cursor_position += s.len();
58 }
59
60 pub fn backspace(&mut self) -> bool {
62 if self.cursor_position > 0 {
63 let prev_boundary = self.content[..self.cursor_position]
65 .char_indices()
66 .next_back()
67 .map(|(idx, _)| idx)
68 .unwrap_or(0);
69 self.content.remove(prev_boundary);
70 self.cursor_position = prev_boundary;
71 true
72 } else {
73 false
74 }
75 }
76
77 pub fn delete(&mut self) -> bool {
79 if self.cursor_position < self.content.len() {
80 self.content.remove(self.cursor_position);
81 true
82 } else {
83 false
84 }
85 }
86
87 pub fn move_left(&mut self) {
89 if self.cursor_position > 0 {
90 self.cursor_position = self.content[..self.cursor_position]
92 .char_indices()
93 .next_back()
94 .map(|(idx, _)| idx)
95 .unwrap_or(0);
96 }
97 }
98
99 pub fn move_right(&mut self) {
101 if self.cursor_position < self.content.len() {
102 self.cursor_position = self.content[self.cursor_position..]
104 .char_indices()
105 .nth(1)
106 .map(|(idx, _)| self.cursor_position + idx)
107 .unwrap_or(self.content.len());
108 }
109 }
110
111 pub fn move_home(&mut self) {
113 self.cursor_position = 0;
114 }
115
116 pub fn move_end(&mut self) {
118 self.cursor_position = self.content.len();
119 }
120}
121
122impl Default for InputBuffer {
123 fn default() -> Self {
124 Self::new()
125 }
126}