From 23dfde75bd1e1d91f38a0c8e86862233c0d31870 Mon Sep 17 00:00:00 2001 From: achristmascarl Date: Tue, 6 Aug 2024 12:33:10 -0400 Subject: [PATCH 1/3] expose selection_start --- src/textarea.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/textarea.rs b/src/textarea.rs index 0efd170..92d8f8c 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -2003,6 +2003,26 @@ impl<'a> TextArea<'a> { self.cursor } + /// Get the current selection start position. 0-base character-wise (row, col) selection start position. + /// ``` + /// use tui_textarea::TextArea; + /// + /// let mut textarea = TextArea::default(); + /// assert_eq!(textarea.selection_start(), None); + + /// textarea.insert_char('a'); + /// textarea.insert_newline(); + /// textarea.insert_char('b'); + /// + /// textarea.start_selection(); + /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// textarea.move_cursor(CursorMove::Forward); + /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// ``` + pub fn selection_start(&self) -> Option<(usize, usize)> { + self.selection_start + } + /// Set text alignment. When [`Alignment::Center`] or [`Alignment::Right`] is set, line number is automatically /// disabled because those alignments don't work well with line numbers. /// ``` From 31012c6a010c9a66d705163d71991ae8eb12c79a Mon Sep 17 00:00:00 2001 From: achristmascarl Date: Tue, 6 Aug 2024 12:45:18 -0400 Subject: [PATCH 2/3] fix tests --- src/textarea.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/textarea.rs b/src/textarea.rs index 92d8f8c..442f510 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -2006,18 +2006,19 @@ impl<'a> TextArea<'a> { /// Get the current selection start position. 0-base character-wise (row, col) selection start position. /// ``` /// use tui_textarea::TextArea; + /// use tui_textarea::CursorMove; /// - /// let mut textarea = TextArea::default(); + /// let mut textarea = TextArea::from(["abc"]); /// assert_eq!(textarea.selection_start(), None); - - /// textarea.insert_char('a'); - /// textarea.insert_newline(); - /// textarea.insert_char('b'); /// /// textarea.start_selection(); /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// /// textarea.move_cursor(CursorMove::Forward); /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// + /// textarea.cancel_selection(); + /// assert_eq!(textarea.selection_start(), None); /// ``` pub fn selection_start(&self) -> Option<(usize, usize)> { self.selection_start From 75f51111e1c20e4a69bab70fe589b33daf870988 Mon Sep 17 00:00:00 2001 From: achristmascarl Date: Wed, 7 Aug 2024 16:34:13 -0400 Subject: [PATCH 3/3] switch to selection range --- src/textarea.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/textarea.rs b/src/textarea.rs index 442f510..6650f87 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -1402,7 +1402,7 @@ impl<'a> TextArea<'a> { self.select_style } - fn selection_range(&self) -> Option<(Pos, Pos)> { + fn selection_range_(&self) -> Option<(Pos, Pos)> { let (sr, sc) = self.selection_start?; let (er, ec) = self.cursor; let (so, eo) = (self.line_offset(sr, sc), self.line_offset(er, ec)); @@ -1416,7 +1416,7 @@ impl<'a> TextArea<'a> { } fn take_selection_range(&mut self) -> Option<(Pos, Pos)> { - let range = self.selection_range(); + let range = self.selection_range_(); self.cancel_selection(); range } @@ -1580,7 +1580,7 @@ impl<'a> TextArea<'a> { hl.search(matches, self.search.style); } - if let Some((start, end)) = self.selection_range() { + if let Some((start, end)) = self.selection_range_() { hl.selection(row, start.row, start.offset, end.row, end.offset); } @@ -2003,25 +2003,32 @@ impl<'a> TextArea<'a> { self.cursor } - /// Get the current selection start position. 0-base character-wise (row, col) selection start position. + /// Get the current selection range. A tuple (selection_start, cursor) of 0-base character-wise (row, col) + /// positions. The selection start position will always come first, even when it is ahead of + /// the cursor. /// ``` /// use tui_textarea::TextArea; /// use tui_textarea::CursorMove; /// /// let mut textarea = TextArea::from(["abc"]); - /// assert_eq!(textarea.selection_start(), None); + /// assert_eq!(textarea.selection_range(), None); /// /// textarea.start_selection(); - /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 0)))); /// /// textarea.move_cursor(CursorMove::Forward); - /// assert_eq!(textarea.selection_start(), Some((0, 0))); + /// assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 1)))); /// /// textarea.cancel_selection(); - /// assert_eq!(textarea.selection_start(), None); + /// assert_eq!(textarea.selection_range(), None); + /// + /// textarea.start_selection(); + /// textarea.move_cursor(CursorMove::Back); + /// assert_eq!(textarea.selection_range(), Some(((0, 1), (0, 0)))); /// ``` - pub fn selection_start(&self) -> Option<(usize, usize)> { + pub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))> { self.selection_start + .map(|selection_start| (selection_start, self.cursor)) } /// Set text alignment. When [`Alignment::Center`] or [`Alignment::Right`] is set, line number is automatically