diff --git a/README.md b/README.md index a2befee..f6a42fc 100644 --- a/README.md +++ b/README.md @@ -489,6 +489,7 @@ notify how to move the cursor. | `textarea.paste()` | Paste yanked text | | `textarea.start_selection()` | Start text selection | | `textarea.cancel_selection()` | Cancel text selection | +| `textarea.select_all()` | Select entire text | | `textarea.move_cursor(CursorMove::Forward)` | Move cursor forward by one character | | `textarea.move_cursor(CursorMove::Back)` | Move cursor backward by one character | | `textarea.move_cursor(CursorMove::Up)` | Move cursor up by one line | diff --git a/src/textarea.rs b/src/textarea.rs index 3163c99..bfb65d9 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -1312,6 +1312,26 @@ impl<'a> TextArea<'a> { self.selection_start = None; } + /// Select the entire text. Cursor moves to the end of the text buffer. When text selection is already ongoing, + /// it is canceled. + /// ``` + /// use tui_textarea::{TextArea, CursorMove}; + /// + /// let mut textarea = TextArea::from(["aaa", "bbb", "ccc"]); + /// + /// textarea.select_all(); + /// + /// // Cut the entire text; + /// textarea.cut(); + /// + /// assert_eq!(textarea.lines(), [""]); // Buffer is now empty + /// assert_eq!(textarea.yank_text(), "aaa\nbbb\nccc"); + /// ``` + pub fn select_all(&mut self) { + self.move_cursor(CursorMove::Jump(u16::MAX, u16::MAX)); + self.selection_start = Some((0, 0)); + } + /// Return if text selection is ongoing or not. /// ``` /// use tui_textarea::{TextArea}; diff --git a/tests/textarea.rs b/tests/textarea.rs index 6d1d82b..e18f60f 100644 --- a/tests/textarea.rs +++ b/tests/textarea.rs @@ -1420,6 +1420,18 @@ fn test_set_yank_paste_text() { } } +#[test] +fn test_select_all() { + let mut t = TextArea::from(["aaa", "bbb", "ccc"]); + t.select_all(); + assert!(t.is_selecting()); + assert_eq!(t.cursor(), (2, 3)); + t.cut(); + assert_eq!(t.lines(), [""]); + assert_eq!(t.yank_text(), "aaa\nbbb\nccc"); + assert_undo_redo((2, 3), &["aaa", "bbb", "ccc"], &[""], &mut t, ""); +} + struct DeleteTester(&'static [&'static str], fn(&mut TextArea) -> bool); impl DeleteTester { fn test(&self, before: (usize, usize), after: (usize, usize, &[&str], &str)) {