pub struct CustomTextArea<'a> { /* private fields */ }
Expand description
A custom text area widget
Implementations§
Source§impl<'a> CustomTextArea<'a>
impl<'a> CustomTextArea<'a>
Sourcepub fn new(
style: impl Into<Style>,
inline: bool,
multiline: bool,
text: impl Into<String>,
) -> Self
pub fn new( style: impl Into<Style>, inline: bool, multiline: bool, text: impl Into<String>, ) -> Self
Creates a new custom text area
Sourcepub fn is_multiline(&self) -> bool
pub fn is_multiline(&self) -> bool
Returns whether the text area supports multiple lines or not
Sourcepub fn is_focused(&self) -> bool
pub fn is_focused(&self) -> bool
Returns whether the text area is currently focused or not
Sourcepub fn set_secret(&mut self, secret: bool)
pub fn set_secret(&mut self, secret: bool)
Sets or clear the the text area mask char
Sourcepub fn set_title(&mut self, new_title: impl Into<Cow<'a, str>>)
pub fn set_title(&mut self, new_title: impl Into<Cow<'a, str>>)
Updates the title of the text area
Sourcepub fn lines_as_string(&self) -> String
pub fn lines_as_string(&self) -> String
Retrieves the current text in the text area as a single string
Sourcepub fn move_cursor_left(&mut self, word: bool)
pub fn move_cursor_left(&mut self, word: bool)
Moves the cursor to the left, optionally by word
Sourcepub fn move_cursor_right(&mut self, word: bool)
pub fn move_cursor_right(&mut self, word: bool)
Moves the cursor to the right, optionally by word
Sourcepub fn move_home(&mut self, absolute: bool)
pub fn move_home(&mut self, absolute: bool)
Moves the cursor to the head of the line, or the absolute head
Sourcepub fn move_end(&mut self, absolute: bool)
pub fn move_end(&mut self, absolute: bool)
Moves the cursor to the end of the line, or the absolute end
Sourcepub fn insert_char(&mut self, c: char)
pub fn insert_char(&mut self, c: char)
Inserts a char at the current cursor position
Sourcepub fn insert_str<S>(&mut self, text: S)
pub fn insert_str<S>(&mut self, text: S)
Inserts a text at the current cursor position
Sourcepub fn insert_newline(&mut self)
pub fn insert_newline(&mut self)
Inserts a newline at the current cursor position, if multiline is enabled
Methods from Deref<Target = TextArea<'a>>§
Sourcepub fn input(&mut self, input: impl Into<Input>) -> bool
pub fn input(&mut self, input: impl Into<Input>) -> bool
Handle a key input with default key mappings. For default key mappings, see the table in
the module document.
crossterm
, termion
, and termwiz
features enable conversion from their own key event types into
Input
so this method can take the event values directly.
This method returns if the input modified text contents or not in the textarea.
use tui_textarea::{TextArea, Key, Input};
let mut textarea = TextArea::default();
// Handle crossterm key events
let event: crossterm::event::Event = ...;
textarea.input(event);
if let crossterm::event::Event::Key(key) = event {
textarea.input(key);
}
// Handle termion key events
let event: termion::event::Event = ...;
textarea.input(event);
if let termion::event::Event::Key(key) = event {
textarea.input(key);
}
// Handle termwiz key events
let event: termwiz::input::InputEvent = ...;
textarea.input(event);
if let termwiz::input::InputEvent::Key(key) = event {
textarea.input(key);
}
// Handle backend-agnostic key input
let input = Input { key: Key::Char('a'), ctrl: false, alt: false, shift: false };
let modified = textarea.input(input);
assert!(modified);
Sourcepub fn input_without_shortcuts(&mut self, input: impl Into<Input>) -> bool
pub fn input_without_shortcuts(&mut self, input: impl Into<Input>) -> bool
Handle a key input without default key mappings. This method handles only
- Single character input without modifier keys
- Tab
- Enter
- Backspace
- Delete
This method returns if the input modified text contents or not in the textarea.
This method is useful when you want to define your own key mappings and don’t want default key mappings. See ‘Define your own key mappings’ section in the module document.
Sourcepub fn insert_char(&mut self, c: char)
pub fn insert_char(&mut self, c: char)
Insert a single character at current cursor position.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.insert_char('a');
assert_eq!(textarea.lines(), ["a"]);
Sourcepub fn insert_str<S>(&mut self, s: S) -> bool
pub fn insert_str<S>(&mut self, s: S) -> bool
Insert a string at current cursor position. This method returns if some text was inserted or not in the textarea.
Both \n
and \r\n
are recognized as newlines but \r
isn’t.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.insert_str("hello");
assert_eq!(textarea.lines(), ["hello"]);
textarea.insert_str(", world\ngoodbye, world");
assert_eq!(textarea.lines(), ["hello, world", "goodbye, world"]);
Sourcepub fn delete_str(&mut self, chars: usize) -> bool
pub fn delete_str(&mut self, chars: usize) -> bool
Delete a string from the current cursor position. The chars
parameter means number of characters, not a byte
length of the string. Newlines at the end of lines are counted in the number. This method returns if some text
was deleted or not.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["🐱🐶🐰🐮"]);
textarea.move_cursor(CursorMove::Forward);
textarea.delete_str(2);
assert_eq!(textarea.lines(), ["🐱🐮"]);
let mut textarea = TextArea::from(["🐱", "🐶", "🐰", "🐮"]);
textarea.move_cursor(CursorMove::Down);
textarea.delete_str(4); // Deletes 🐶, \n, 🐰, \n
assert_eq!(textarea.lines(), ["🐱", "🐮"]);
Sourcepub fn insert_tab(&mut self) -> bool
pub fn insert_tab(&mut self) -> bool
Insert a tab at current cursor position. Note that this method does nothing when the tab length is 0. This method returns if a tab string was inserted or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["hi"]);
textarea.move_cursor(CursorMove::End); // Move to the end of line
textarea.insert_tab();
assert_eq!(textarea.lines(), ["hi "]);
textarea.insert_tab();
assert_eq!(textarea.lines(), ["hi "]);
Sourcepub fn insert_newline(&mut self)
pub fn insert_newline(&mut self)
Insert a newline at current cursor position.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["hi"]);
textarea.move_cursor(CursorMove::Forward);
textarea.insert_newline();
assert_eq!(textarea.lines(), ["h", "i"]);
Sourcepub fn delete_newline(&mut self) -> bool
pub fn delete_newline(&mut self) -> bool
Delete a newline from head of current cursor line. This method returns if a newline was deleted or not in the textarea. When some text is selected, it is deleted instead.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["hello", "world"]);
textarea.move_cursor(CursorMove::Down);
textarea.delete_newline();
assert_eq!(textarea.lines(), ["helloworld"]);
Sourcepub fn delete_char(&mut self) -> bool
pub fn delete_char(&mut self) -> bool
Delete one character before cursor. When the cursor is at head of line, the newline before the cursor will be removed. This method returns if some text was deleted or not in the textarea. When some text is selected, it is deleted instead.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abc"]);
textarea.move_cursor(CursorMove::Forward);
textarea.delete_char();
assert_eq!(textarea.lines(), ["bc"]);
Sourcepub fn delete_next_char(&mut self) -> bool
pub fn delete_next_char(&mut self) -> bool
Delete one character next to cursor. When the cursor is at end of line, the newline next to the cursor will be removed. This method returns if a character was deleted or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abc"]);
textarea.move_cursor(CursorMove::Forward);
textarea.delete_next_char();
assert_eq!(textarea.lines(), ["ac"]);
Sourcepub fn delete_line_by_end(&mut self) -> bool
pub fn delete_line_by_end(&mut self) -> bool
Delete string from cursor to end of the line. When the cursor is at end of line, the newline next to the cursor is removed. This method returns if some text was deleted or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abcde"]);
// Move to 'c'
textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Forward);
textarea.delete_line_by_end();
assert_eq!(textarea.lines(), ["ab"]);
Sourcepub fn delete_line_by_head(&mut self) -> bool
pub fn delete_line_by_head(&mut self) -> bool
Delete string from cursor to head of the line. When the cursor is at head of line, the newline before the cursor will be removed. This method returns if some text was deleted or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abcde"]);
// Move to 'c'
textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Forward);
textarea.delete_line_by_head();
assert_eq!(textarea.lines(), ["cde"]);
Sourcepub fn delete_word(&mut self) -> bool
pub fn delete_word(&mut self) -> bool
Delete a word before cursor. Word boundary appears at spaces, punctuations, and others. For example fn foo(a)
consists of words fn
, foo
, (
, a
, )
. When the cursor is at head of line, the newline before the cursor
will be removed.
This method returns if some text was deleted or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["aaa bbb ccc"]);
textarea.move_cursor(CursorMove::End);
textarea.delete_word();
assert_eq!(textarea.lines(), ["aaa bbb "]);
textarea.delete_word();
assert_eq!(textarea.lines(), ["aaa "]);
Sourcepub fn delete_next_word(&mut self) -> bool
pub fn delete_next_word(&mut self) -> bool
Delete a word next to cursor. Word boundary appears at spaces, punctuations, and others. For example fn foo(a)
consists of words fn
, foo
, (
, a
, )
. When the cursor is at end of line, the newline next to the cursor
will be removed.
This method returns if some text was deleted or not in the textarea.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["aaa bbb ccc"]);
textarea.delete_next_word();
assert_eq!(textarea.lines(), [" bbb ccc"]);
textarea.delete_next_word();
assert_eq!(textarea.lines(), [" ccc"]);
Sourcepub fn paste(&mut self) -> bool
pub fn paste(&mut self) -> bool
Paste a string previously deleted by TextArea::delete_line_by_head
, TextArea::delete_line_by_end
,
TextArea::delete_word
, TextArea::delete_next_word
. This method returns if some text was inserted or not
in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["aaa bbb ccc"]);
textarea.delete_next_word();
textarea.move_cursor(CursorMove::End);
textarea.paste();
assert_eq!(textarea.lines(), [" bbb cccaaa"]);
Sourcepub fn start_selection(&mut self)
pub fn start_selection(&mut self)
Start text selection at the cursor position. If text selection is already ongoing, the start position is reset.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["aaa bbb ccc"]);
textarea.start_selection();
textarea.move_cursor(CursorMove::WordForward);
textarea.copy();
assert_eq!(textarea.yank_text(), "aaa ");
Sourcepub fn cancel_selection(&mut self)
pub fn cancel_selection(&mut self)
Stop the current text selection. This method does nothing if text selection is not ongoing.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["aaa bbb ccc"]);
textarea.start_selection();
textarea.move_cursor(CursorMove::WordForward);
// Cancel the ongoing text selection
textarea.cancel_selection();
// As the result, this `copy` call does nothing
textarea.copy();
assert_eq!(textarea.yank_text(), "");
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
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");
Sourcepub fn is_selecting(&self) -> bool
pub fn is_selecting(&self) -> bool
Return if text selection is ongoing or not.
use tui_textarea::{TextArea};
let mut textarea = TextArea::default();
assert!(!textarea.is_selecting());
textarea.start_selection();
assert!(textarea.is_selecting());
textarea.cancel_selection();
assert!(!textarea.is_selecting());
Sourcepub fn set_selection_style(&mut self, style: Style)
pub fn set_selection_style(&mut self, style: Style)
Set the style used for text selection. The default style is light blue.
use tui_textarea::TextArea;
use ratatui::style::{Style, Color};
let mut textarea = TextArea::default();
// Change the selection color from the default to Red
textarea.set_selection_style(Style::default().bg(Color::Red));
assert_eq!(textarea.selection_style(), Style::default().bg(Color::Red));
Sourcepub fn selection_style(&mut self) -> Style
pub fn selection_style(&mut self) -> Style
Get the style used for text selection.
use tui_textarea::TextArea;
use ratatui::style::{Style, Color};
let mut textarea = TextArea::default();
assert_eq!(textarea.selection_style(), Style::default().bg(Color::LightBlue));
Sourcepub fn copy(&mut self)
pub fn copy(&mut self)
Copy the selection text to the yank buffer. When nothing is selected, this method does nothing.
To get the yanked text, use TextArea::yank_text
.
use tui_textarea::{TextArea, Key, Input, CursorMove};
let mut textarea = TextArea::from(["Hello World"]);
// Start text selection at 'W'
textarea.move_cursor(CursorMove::WordForward);
textarea.start_selection();
// Select the word "World" and copy the selected text
textarea.move_cursor(CursorMove::End);
textarea.copy();
assert_eq!(textarea.yank_text(), "World");
assert_eq!(textarea.lines(), ["Hello World"]); // Text does not change
Sourcepub fn cut(&mut self) -> bool
pub fn cut(&mut self) -> bool
Cut the selected text and place it in the yank buffer. This method returns whether the text was modified.
The cursor will move to the start position of the text selection.
To get the yanked text, use TextArea::yank_text
.
use tui_textarea::{TextArea, Key, Input, CursorMove};
let mut textarea = TextArea::from(["Hello World"]);
// Start text selection at 'W'
textarea.move_cursor(CursorMove::WordForward);
textarea.start_selection();
// Select the word "World" and copy the selected text
textarea.move_cursor(CursorMove::End);
textarea.cut();
assert_eq!(textarea.yank_text(), "World");
assert_eq!(textarea.lines(), ["Hello "]);
Sourcepub fn move_cursor(&mut self, m: CursorMove)
pub fn move_cursor(&mut self, m: CursorMove)
Move the cursor to the position specified by the CursorMove
parameter. For each kind of cursor moves, see
the document of CursorMove
.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abc", "def"]);
textarea.move_cursor(CursorMove::Forward);
assert_eq!(textarea.cursor(), (0, 1));
textarea.move_cursor(CursorMove::Down);
assert_eq!(textarea.cursor(), (1, 1));
Sourcepub fn undo(&mut self) -> bool
pub fn undo(&mut self) -> bool
Undo the last modification. This method returns if the undo modified text contents or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abc def"]);
textarea.delete_next_word();
assert_eq!(textarea.lines(), [" def"]);
textarea.undo();
assert_eq!(textarea.lines(), ["abc def"]);
Sourcepub fn redo(&mut self) -> bool
pub fn redo(&mut self) -> bool
Redo the last undo change. This method returns if the redo modified text contents or not in the textarea.
use tui_textarea::{TextArea, CursorMove};
let mut textarea = TextArea::from(["abc def"]);
textarea.delete_next_word();
assert_eq!(textarea.lines(), [" def"]);
textarea.undo();
assert_eq!(textarea.lines(), ["abc def"]);
textarea.redo();
assert_eq!(textarea.lines(), [" def"]);
Sourcepub fn widget(&'a self) -> impl Widget + 'a
👎Deprecated since 0.5.3: calling this method is no longer necessary on rendering a textarea. pass &TextArea reference to Frame::render_widget
method call directly
pub fn widget(&'a self) -> impl Widget + 'a
Frame::render_widget
method call directlyBuild a ratatui (or tui-rs) widget to render the current state of the textarea. The widget instance returned
from this method can be rendered with ratatui::Frame::render_widget
.
This method was deprecated at v0.5.3 and is no longer necessary. Instead you can directly pass &TextArea
reference to the Frame::render_widget
method call.
// v0.5.2 or earlier
f.render_widget(textarea.widget(), rect);
// v0.5.3 or later
f.render_widget(&textarea, rect);
Sourcepub fn set_style(&mut self, style: Style)
pub fn set_style(&mut self, style: Style)
Set the style of textarea. By default, textarea is not styled.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
let style = Style::default().fg(Color::Red);
textarea.set_style(style);
assert_eq!(textarea.style(), style);
Sourcepub fn set_block(&mut self, block: Block<'a>)
pub fn set_block(&mut self, block: Block<'a>)
Set the block of textarea. By default, no block is set.
use tui_textarea::TextArea;
use ratatui::widgets::{Block, Borders};
let mut textarea = TextArea::default();
let block = Block::default().borders(Borders::ALL).title("Block Title");
textarea.set_block(block);
assert!(textarea.block().is_some());
Sourcepub fn remove_block(&mut self)
pub fn remove_block(&mut self)
Remove the block of textarea which was set by TextArea::set_block
.
use tui_textarea::TextArea;
use ratatui::widgets::{Block, Borders};
let mut textarea = TextArea::default();
let block = Block::default().borders(Borders::ALL).title("Block Title");
textarea.set_block(block);
textarea.remove_block();
assert!(textarea.block().is_none());
Sourcepub fn set_tab_length(&mut self, len: u8)
pub fn set_tab_length(&mut self, len: u8)
Set the length of tab character. Setting 0 disables tab inputs.
use tui_textarea::{TextArea, Input, Key};
let mut textarea = TextArea::default();
let tab_input = Input { key: Key::Tab, ctrl: false, alt: false, shift: false };
textarea.set_tab_length(8);
textarea.input(tab_input.clone());
assert_eq!(textarea.lines(), [" "]);
textarea.set_tab_length(2);
textarea.input(tab_input);
assert_eq!(textarea.lines(), [" "]);
Sourcepub fn tab_length(&self) -> u8
pub fn tab_length(&self) -> u8
Get how many spaces are used for representing tab character. The default value is 4.
Sourcepub fn set_hard_tab_indent(&mut self, enabled: bool)
pub fn set_hard_tab_indent(&mut self, enabled: bool)
Set if a hard tab is used or not for indent. When true
is set, typing a tab key inserts a hard tab instead of
spaces. By default, hard tab is disabled.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_hard_tab_indent(true);
textarea.insert_tab();
assert_eq!(textarea.lines(), ["\t"]);
Sourcepub fn hard_tab_indent(&self) -> bool
pub fn hard_tab_indent(&self) -> bool
Get if a hard tab is used for indent or not.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert!(!textarea.hard_tab_indent());
textarea.set_hard_tab_indent(true);
assert!(textarea.hard_tab_indent());
Sourcepub fn indent(&self) -> &'static str
pub fn indent(&self) -> &'static str
Get a string for indent. It consists of spaces by default. When hard tab is enabled, it is a tab character.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.indent(), " ");
textarea.set_tab_length(2);
assert_eq!(textarea.indent(), " ");
textarea.set_hard_tab_indent(true);
assert_eq!(textarea.indent(), "\t");
Sourcepub fn set_max_histories(&mut self, max: usize)
pub fn set_max_histories(&mut self, max: usize)
Set how many modifications are remembered for undo/redo. Setting 0 disables undo/redo.
Sourcepub fn max_histories(&self) -> usize
pub fn max_histories(&self) -> usize
Get how many modifications are remembered for undo/redo. The default value is 50.
Sourcepub fn set_cursor_line_style(&mut self, style: Style)
pub fn set_cursor_line_style(&mut self, style: Style)
Set the style of line at cursor. By default, the cursor line is styled with underline. To stop styling the cursor line, set the default style.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
let style = Style::default().fg(Color::Red);
textarea.set_cursor_line_style(style);
assert_eq!(textarea.cursor_line_style(), style);
// Disable cursor line style
textarea.set_cursor_line_style(Style::default());
Sourcepub fn cursor_line_style(&self) -> Style
pub fn cursor_line_style(&self) -> Style
Get the style of cursor line. By default it is styled with underline.
Sourcepub fn set_line_number_style(&mut self, style: Style)
pub fn set_line_number_style(&mut self, style: Style)
Set the style of line number. By setting the style with this method, line numbers are drawn in textarea, meant that line numbers are disabled by default. If you want to show line numbers but don’t want to style them, set the default style.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
// Show line numbers in dark gray background
let style = Style::default().bg(Color::DarkGray);
textarea.set_line_number_style(style);
assert_eq!(textarea.line_number_style(), Some(style));
Sourcepub fn remove_line_number(&mut self)
pub fn remove_line_number(&mut self)
Remove the style of line number which was set by TextArea::set_line_number_style
. After calling this
method, Line numbers will no longer be shown.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_line_number_style(Style::default().bg(Color::DarkGray));
textarea.remove_line_number();
assert_eq!(textarea.line_number_style(), None);
Sourcepub fn line_number_style(&self) -> Option<Style>
pub fn line_number_style(&self) -> Option<Style>
Get the style of line number if set.
Sourcepub fn set_placeholder_text(&mut self, placeholder: impl Into<String>)
pub fn set_placeholder_text(&mut self, placeholder: impl Into<String>)
Set the placeholder text. The text is set in the textarea when no text is input. Setting a non-empty string ""
enables the placeholder. The default value is an empty string so the placeholder is disabled by default.
To customize the text style, see TextArea::set_placeholder_style
.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.placeholder_text(), "");
assert!(textarea.placeholder_style().is_none());
textarea.set_placeholder_text("Hello");
assert_eq!(textarea.placeholder_text(), "Hello");
assert!(textarea.placeholder_style().is_some());
Sourcepub fn set_placeholder_style(&mut self, style: Style)
pub fn set_placeholder_style(&mut self, style: Style)
Set the style of the placeholder text. The default style is a dark gray text.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.placeholder_style(), None); // When the placeholder is disabled
textarea.set_placeholder_text("Enter your message"); // Enable placeholder by setting non-empty text
let style = Style::default().bg(Color::Blue);
textarea.set_placeholder_style(style);
assert_eq!(textarea.placeholder_style(), Some(style));
Sourcepub fn placeholder_text(&self) -> &str
pub fn placeholder_text(&self) -> &str
Get the placeholder text. An empty string means the placeholder is disabled. The default value is an empty string.
use tui_textarea::TextArea;
let textarea = TextArea::default();
assert_eq!(textarea.placeholder_text(), "");
Sourcepub fn placeholder_style(&self) -> Option<Style>
pub fn placeholder_style(&self) -> Option<Style>
Get the placeholder style. When the placeholder text is empty, it returns None
since the placeholder is disabled.
The default style is a dark gray text.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.placeholder_style(), None);
textarea.set_placeholder_text("hello");
assert!(textarea.placeholder_style().is_some());
Sourcepub fn set_mask_char(&mut self, mask: char)
pub fn set_mask_char(&mut self, mask: char)
Specify a character masking the text. All characters in the textarea will be replaced by this character. This API is useful for making a kind of credentials form such as a password input.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_mask_char('*');
assert_eq!(textarea.mask_char(), Some('*'));
textarea.set_mask_char('●');
assert_eq!(textarea.mask_char(), Some('●'));
Sourcepub fn clear_mask_char(&mut self)
pub fn clear_mask_char(&mut self)
Clear the masking character previously set by TextArea::set_mask_char
.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_mask_char('*');
assert_eq!(textarea.mask_char(), Some('*'));
textarea.clear_mask_char();
assert_eq!(textarea.mask_char(), None);
Sourcepub fn mask_char(&self) -> Option<char>
pub fn mask_char(&self) -> Option<char>
Get the character to mask text. When no character is set, None
is returned.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.mask_char(), None);
textarea.set_mask_char('*');
assert_eq!(textarea.mask_char(), Some('*'));
Sourcepub fn set_cursor_style(&mut self, style: Style)
pub fn set_cursor_style(&mut self, style: Style)
Set the style of cursor. By default, a cursor is rendered in the reversed color. Setting the same style as cursor line hides a cursor.
use ratatui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
let style = Style::default().bg(Color::Red);
textarea.set_cursor_style(style);
assert_eq!(textarea.cursor_style(), style);
Sourcepub fn cursor_style(&self) -> Style
pub fn cursor_style(&self) -> Style
Get the style of cursor.
Sourcepub fn lines(&'a self) -> &'a [String]
pub fn lines(&'a self) -> &'a [String]
Get slice of line texts. This method borrows the content, but not moves. Note that the returned slice will never be empty because an empty text means a slice containing one empty line. This is correct since any text file must end with a newline.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.lines(), [""]);
textarea.insert_char('a');
assert_eq!(textarea.lines(), ["a"]);
textarea.insert_newline();
assert_eq!(textarea.lines(), ["a", ""]);
textarea.insert_char('b');
assert_eq!(textarea.lines(), ["a", "b"]);
Sourcepub fn cursor(&self) -> (usize, usize)
pub fn cursor(&self) -> (usize, usize)
Get the current cursor position. 0-base character-wise (row, col) cursor position.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.cursor(), (0, 0));
textarea.insert_char('a');
textarea.insert_newline();
textarea.insert_char('b');
assert_eq!(textarea.cursor(), (1, 1));
Sourcepub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))>
pub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))>
Get the current selection range as a pair of the start position and the end position. The range is bounded
inclusively below and exclusively above. The positions are 0-base character-wise (row, col) values.
The first element of the pair is always smaller than the second one even when it is ahead of the cursor.
When no text is selected, this method returns None
.
use tui_textarea::TextArea;
use tui_textarea::CursorMove;
let mut textarea = TextArea::from([
"aaa",
"bbb",
"ccc",
]);
// It returns `None` when the text selection is not ongoing
assert_eq!(textarea.selection_range(), None);
textarea.start_selection();
assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 0))));
textarea.move_cursor(CursorMove::Forward);
assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 1))));
textarea.move_cursor(CursorMove::Down);
assert_eq!(textarea.selection_range(), Some(((0, 0), (1, 1))));
// Start selection at (1, 1)
textarea.cancel_selection();
textarea.start_selection();
// The first element of the pair is always smaller than the second one
textarea.move_cursor(CursorMove::Back);
assert_eq!(textarea.selection_range(), Some(((1, 0), (1, 1))));
Sourcepub fn set_alignment(&mut self, alignment: Alignment)
pub fn set_alignment(&mut self, alignment: Alignment)
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.
use ratatui::layout::Alignment;
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_alignment(Alignment::Center);
assert_eq!(textarea.alignment(), Alignment::Center);
Sourcepub fn alignment(&self) -> Alignment
pub fn alignment(&self) -> Alignment
Get current text alignment. The default alignment is Alignment::Left
.
use ratatui::layout::Alignment;
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert_eq!(textarea.alignment(), Alignment::Left);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the textarea has a empty content.
use tui_textarea::TextArea;
let textarea = TextArea::default();
assert!(textarea.is_empty());
let textarea = TextArea::from(["hello"]);
assert!(!textarea.is_empty());
Sourcepub fn yank_text(&self) -> String
pub fn yank_text(&self) -> String
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
,
TextArea::delete_str
, TextArea::copy
, and TextArea::cut
. When multiple lines were yanked, they are
always joined with \n
.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["abc"]);
textarea.delete_next_word();
assert_eq!(textarea.yank_text(), "abc");
// Multiple lines are joined with \n
let mut textarea = TextArea::from(["abc", "def"]);
textarea.delete_str(5);
assert_eq!(textarea.yank_text(), "abc\nd");
Sourcepub fn set_yank_text(&mut self, text: impl Into<String>)
pub fn set_yank_text(&mut self, text: impl Into<String>)
Set a yanked text. The text can be inserted by TextArea::paste
. \n
and \r\n
are recognized as newline
but \r
isn’t.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.set_yank_text("hello\nworld");
textarea.paste();
assert_eq!(textarea.lines(), ["hello", "world"]);
Sourcepub fn scroll(&mut self, scrolling: impl Into<Scrolling>)
pub fn scroll(&mut self, scrolling: impl Into<Scrolling>)
Scroll the textarea. See Scrolling
for the argument.
The cursor will not move until it goes out the viewport. When the cursor position is outside the viewport after scroll,
the cursor position will be adjusted to stay in the viewport using the same logic as CursorMove::InViewport
.
use tui_textarea::TextArea;
// Let's say terminal height is 8.
// Create textarea with 20 lines "0", "1", "2", "3", ...
let mut textarea: TextArea = (0..20).into_iter().map(|i| i.to_string()).collect();
// Scroll down by 15 lines. Since terminal height is 8, cursor will go out
// the viewport.
textarea.scroll((15, 0));
// So the cursor position was updated to stay in the viewport after the scrolling.
assert_eq!(textarea.cursor(), (15, 0));
// Scroll up by 5 lines. Since the scroll amount is smaller than the terminal
// height, cursor position will not be updated.
textarea.scroll((-5, 0));
assert_eq!(textarea.cursor(), (15, 0));
// Scroll up by 5 lines again. The terminal height is 8. So a cursor reaches to
// the top of viewport after scrolling up by 7 lines. Since we have already
// scrolled up by 5 lines, scrolling up by 5 lines again makes the cursor overrun
// the viewport by 5 - 2 = 3 lines. To keep the cursor stay in the viewport, the
// cursor position will be adjusted from line 15 to line 12.
textarea.scroll((-5, 0));
assert_eq!(textarea.cursor(), (12, 0));
Trait Implementations§
Source§impl<'a> Clone for CustomTextArea<'a>
impl<'a> Clone for CustomTextArea<'a>
Source§fn clone(&self) -> CustomTextArea<'a>
fn clone(&self) -> CustomTextArea<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Deref for CustomTextArea<'a>
impl<'a> Deref for CustomTextArea<'a>
Source§impl<'a> DerefMut for CustomTextArea<'a>
impl<'a> DerefMut for CustomTextArea<'a>
Source§impl<'a> From<NewVariableValue<'a>> for CustomTextArea<'a>
impl<'a> From<NewVariableValue<'a>> for CustomTextArea<'a>
Source§fn from(val: NewVariableValue<'a>) -> Self
fn from(val: NewVariableValue<'a>) -> Self
Auto Trait Implementations§
impl<'a> !Freeze for CustomTextArea<'a>
impl<'a> RefUnwindSafe for CustomTextArea<'a>
impl<'a> Send for CustomTextArea<'a>
impl<'a> Sync for CustomTextArea<'a>
impl<'a> Unpin for CustomTextArea<'a>
impl<'a> UnwindSafe for CustomTextArea<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more