Insert text at the current cursor position, replacing any selection. Respects max_length if provided.
(&mut self, s: &str, max_length: Option<usize>)
| 121 | /// Insert text at the current cursor position, replacing any selection. |
| 122 | /// Respects max_length if provided. |
| 123 | pub fn insert_text(&mut self, s: &str, max_length: Option<usize>) { |
| 124 | self.delete_selection(); |
| 125 | let char_count = self.text.chars().count(); |
| 126 | let insert_count = s.chars().count(); |
| 127 | let allowed = if let Some(max) = max_length { |
| 128 | if char_count >= max { |
| 129 | 0 |
| 130 | } else { |
| 131 | insert_count.min(max - char_count) |
| 132 | } |
| 133 | } else { |
| 134 | insert_count |
| 135 | }; |
| 136 | if allowed == 0 { |
| 137 | return; |
| 138 | } |
| 139 | let insert_str: String = s.chars().take(allowed).collect(); |
| 140 | let byte_pos = char_index_to_byte(&self.text, self.cursor_pos); |
| 141 | self.text.insert_str(byte_pos, &insert_str); |
| 142 | self.cursor_pos += allowed; |
| 143 | self.reset_blink(); |
| 144 | } |
| 145 | |
| 146 | /// Move cursor left by one character. |
| 147 | pub fn move_left(&mut self, shift: bool) { |