Insert text at visual cursor position in styled mode, with escaping. The input `s` should already be escaped if needed.
(&mut self, s: &str, max_length: Option<usize>)
| 614 | /// Insert text at visual cursor position in styled mode, with escaping. |
| 615 | /// The input `s` should already be escaped if needed. |
| 616 | pub fn insert_text_styled(&mut self, s: &str, max_length: Option<usize>) { |
| 617 | self.delete_selection_styled(); |
| 618 | let visual_count = self.cursor_len_styled(); |
| 619 | let insert_cursor_len = styling::cursor_len(s); |
| 620 | let allowed = if let Some(max) = max_length { |
| 621 | if visual_count >= max { |
| 622 | 0 |
| 623 | } else { |
| 624 | insert_cursor_len.min(max - visual_count) |
| 625 | } |
| 626 | } else { |
| 627 | insert_cursor_len |
| 628 | }; |
| 629 | if allowed == 0 { |
| 630 | return; |
| 631 | } |
| 632 | // If we need to truncate the insertion, work on the visual chars |
| 633 | let insert_str = if allowed < insert_cursor_len { |
| 634 | // Build truncated escaped string |
| 635 | let stripped = styling::strip_styling(s); |
| 636 | let truncated: String = stripped.chars().take(allowed).collect(); |
| 637 | styling::escape_str(&truncated) |
| 638 | } else { |
| 639 | s.to_string() |
| 640 | }; |
| 641 | let (new_text, new_cursor) = styling::insert_at_visual(&self.text, self.cursor_pos, &insert_str); |
| 642 | self.text = new_text; |
| 643 | self.cursor_pos = new_cursor; |
| 644 | // Clean up any empty style tags the cursor has passed |
| 645 | self.cleanup_after_move(); |
| 646 | self.reset_blink(); |
| 647 | } |
| 648 | |
| 649 | /// Insert a single typed character in styled mode (auto-escapes). |
| 650 | pub fn insert_char_styled(&mut self, ch: char, max_length: Option<usize>) { |
no test coverage detected