From 51a75a86bf72abda7fe765698a20fdc522b504d0 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 20 Jun 2022 00:36:11 +0900 Subject: [PATCH] add `TextArea::yank_text` and `TextArea::set_yank_text` for operating yanked text --- src/textarea.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/textarea.rs b/src/textarea.rs index 32e5fb7..3227059 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -1217,6 +1217,35 @@ impl<'a> TextArea<'a> { pub fn is_empty(&self) -> bool { self.lines == [""] } + + /// Get the yanked text. Text is automatically yanked when deleting strings by [`TextArea::delete_line_by_head`], + /// [`TextArea::delete_line_by_end`], [`TextArea::delete_word`], [`TextArea::delete_next_word`]. + /// ``` + /// use tui_textarea::TextArea; + /// + /// let mut textarea = TextArea::from(["abc"]); + /// + /// textarea.delete_next_word(); + /// assert_eq!(textarea.yank_text(), "abc"); + /// ``` + pub fn yank_text(&'a self) -> &'a str { + &self.yank + } + + /// Set a yanked text. The text can be inserted by [`TextArea::paste`]. The string passed to method must not contain + /// any newlines. + /// ``` + /// use tui_textarea::TextArea; + /// + /// let mut textarea = TextArea::default(); + /// + /// textarea.set_yank_text("hello, world"); + /// textarea.paste(); + /// assert_eq!(textarea.lines(), ["hello, world"]); + /// ``` + pub fn set_yank_text(&mut self, text: impl Into) { + self.yank = text.into(); + } } struct Renderer<'a> {