mermaid_cli/tui/state/
input.rs1pub struct InputBuffer {
7 pub content: String,
9 pub cursor_position: usize,
11}
12
13impl InputBuffer {
14 pub fn new() -> Self {
16 Self {
17 content: String::new(),
18 cursor_position: 0,
19 }
20 }
21
22 pub fn clear(&mut self) {
24 self.content.clear();
25 self.cursor_position = 0;
26 }
27
28 pub fn is_empty(&self) -> bool {
30 self.content.is_empty()
31 }
32
33 pub fn get(&self) -> &str {
35 &self.content
36 }
37
38 pub fn set(&mut self, content: impl Into<String>) {
40 self.content = content.into();
41 self.cursor_position = self.content.len();
42 }
43
44 pub fn insert(&mut self, c: char) {
46 self.content.insert(self.cursor_position, c);
47 self.cursor_position += 1;
48 }
49
50 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 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 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 pub fn move_left(&mut self) {
79 if self.cursor_position > 0 {
80 self.cursor_position -= 1;
81 }
82 }
83
84 pub fn move_right(&mut self) {
86 if self.cursor_position < self.content.len() {
87 self.cursor_position += 1;
88 }
89 }
90
91 pub fn move_home(&mut self) {
93 self.cursor_position = 0;
94 }
95
96 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}