Skip to content

Commit

Permalink
implement TextArea::select_all
Browse files Browse the repository at this point in the history
but it is not assigned to default key shortcuts because C-a is already
in use.
  • Loading branch information
rhysd committed Nov 14, 2023
1 parent 9d03463 commit 523a888
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
20 changes: 20 additions & 0 deletions src/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
12 changes: 12 additions & 0 deletions tests/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 523a888

Please sign in to comment.