Struct tui_textarea::TextArea
source · [−]pub struct TextArea<'a> { /* private fields */ }Expand description
A type to manage state of textarea.
TextArea::default creates an empty textarea. TextArea::new creates a textarea with given text lines.
TextArea::from creates a textarea from an iterator of lines. TextArea::input handles key input.
TextArea::widget builds a widget to render. And TextArea::lines returns line texts.
use tui_textarea::{TextArea, Input, Key};
let mut textarea = TextArea::default();
// Input 'a'
let input = Input { key: Key::Char('a'), ctrl: false, alt: false };
textarea.input(input);
// Get widget to render.
let widget = textarea.widget();
// Get lines as String.
println!("Lines: {:?}", textarea.lines());Implementations
sourceimpl<'a> TextArea<'a>
impl<'a> TextArea<'a>
sourcepub fn new(lines: Vec<String>) -> Self
pub fn new(lines: Vec<String>) -> Self
Create TextArea instance with given lines. If you have value other than Vec<String>, TextArea::from
may be more useful.
use tui_textarea::TextArea;
let lines = vec!["hello".to_string(), "...".to_string(), "goodbye".to_string()];
let textarea = TextArea::new(lines);
assert_eq!(textarea.lines(), ["hello", "...", "goodbye"]);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 and termion 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 backend-agnostic key input
let input = Input { key: Key::Char('a'), ctrl: false, alt: 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: Into<String>>(&mut self, s: S) -> bool
pub fn insert_str<S: Into<String>>(&mut self, s: S) -> bool
Insert a string at current cursor position. Currently the string must not contain any newlines. This method returns if some text was inserted or not in the textarea.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.insert_str("hello");
assert_eq!(textarea.lines(), ["hello"]);sourcepub fn delete_str(&mut self, col: usize, chars: usize) -> bool
pub fn delete_str(&mut self, col: usize, chars: usize) -> bool
Delete a string in current cursor line. The chars parameter means number of characters, not a byte length of
the string. This method returns if some text was deleted or not in the textarea.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["🐱🐶🐰🐮"]);
textarea.delete_str(1, 2);
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. textarea.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["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.
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.
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 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
pub fn widget(&'a self) -> impl Widget + 'a
Build a tui-rs widget to render the current state of the textarea. The widget instance returned from this
method can be rendered with tui::terminal::Frame::render_widget.
use tui::backend::CrosstermBackend;
use tui::layout::{Constraint, Direction, Layout};
use tui::Terminal;
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1)].as_ref());
let backend = CrosstermBackend::new(std::io::stdout());
let mut term = Terminal::new(backend).unwrap();
loop {
term.draw(|f| {
let chunks = layout.split(f.size());
let widget = textarea.widget();
f.render_widget(widget, chunks[0]);
}).unwrap();
// ...
}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 tui::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 tui::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 tui::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. Due to limitation of tui-rs, hard tab is not supported. 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 };
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 tui::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 tui::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 tui::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_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 tui::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 into_lines(self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn into_lines(self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Convert TextArea instance into line texts.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
textarea.insert_char('a');
textarea.insert_newline();
textarea.insert_char('b');
assert_eq!(textarea.into_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 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(&'a self) -> &'a str
pub fn yank_text(&'a self) -> &'a str
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");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. 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"]);sourcepub fn set_search_pattern(&mut self, query: impl AsRef<str>) -> Result<(), Error>
pub fn set_search_pattern(&mut self, query: impl AsRef<str>) -> Result<(), Error>
Set a regular expression pattern for text search. Setting an empty string stops the text search.
When a valid pattern is set, all matches will be highlighted in the textarea. Note that the cursor does not
move. To move the cursor, use TextArea::search_forward and TextArea::search_back.
Grammar of regular expression follows regex crate. Patterns don’t match to newlines so match passes across no newline.
When the pattern is invalid, the search pattern will not be updated and an error will be returned.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["hello, world", "goodbye, world"]);
// Search "world"
textarea.set_search_pattern("world").unwrap();
assert_eq!(textarea.cursor(), (0, 0));
textarea.search_forward(false);
assert_eq!(textarea.cursor(), (0, 7));
textarea.search_forward(false);
assert_eq!(textarea.cursor(), (1, 9));
// Stop the text search
textarea.set_search_pattern("");
// Invalid search pattern
assert!(textarea.set_search_pattern("(hello").is_err());sourcepub fn search_pattern(&self) -> Option<&Regex>
pub fn search_pattern(&self) -> Option<&Regex>
Get a regular expression which was set by TextArea::set_search_pattern. When no text search is ongoing, this
method returns None.
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
assert!(textarea.search_pattern().is_none());
textarea.set_search_pattern("hello+").unwrap();
assert!(textarea.search_pattern().is_some());
assert_eq!(textarea.search_pattern().unwrap().as_str(), "hello+");sourcepub fn search_forward(&mut self, match_cursor: bool) -> bool
pub fn search_forward(&mut self, match_cursor: bool) -> bool
Search the pattern set by TextArea::set_search_pattern forward and move the cursor to the next match
position based on the current cursor position. Text search wraps around a text buffer. It returns true when
some match was found. Otherwise it returns false.
The match_cursor parameter represents if the search matches to the current cursor position or not. When true
is set and the cursor position matches to the pattern, the cursor will not move. When false, the cursor will
move to the next match ignoring the match at the current position.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["hello", "helloo", "hellooo"]);
textarea.set_search_pattern("hello+").unwrap();
// Move to next position
let match_found = textarea.search_forward(false);
assert!(match_found);
assert_eq!(textarea.cursor(), (1, 0));
// Since the cursor position matches to "hello+", it does not move
textarea.search_forward(true);
assert_eq!(textarea.cursor(), (1, 0));
// When `match_current` parameter is set to `false`, match at the cursor position is ignored
textarea.search_forward(false);
assert_eq!(textarea.cursor(), (2, 0));
// Text search wrap around the buffer
textarea.search_forward(false);
assert_eq!(textarea.cursor(), (0, 0));
// `false` is returned when no match was found
textarea.set_search_pattern("bye+").unwrap();
let match_found = textarea.search_forward(false);
assert!(!match_found);sourcepub fn search_back(&mut self, match_cursor: bool) -> bool
pub fn search_back(&mut self, match_cursor: bool) -> bool
Search the pattern set by TextArea::set_search_pattern backward and move the cursor to the next match
position based on the current cursor position. Text search wraps around a text buffer. It returns true when
some match was found. Otherwise it returns false.
The match_cursor parameter represents if the search matches to the current cursor position or not. When true
is set and the cursor position matches to the pattern, the cursor will not move. When false, the cursor will
move to the next match ignoring the match at the current position.
use tui_textarea::TextArea;
let mut textarea = TextArea::from(["hello", "helloo", "hellooo"]);
textarea.set_search_pattern("hello+").unwrap();
// Move to next position with wrapping around the text buffer
let match_found = textarea.search_back(false);
assert!(match_found);
assert_eq!(textarea.cursor(), (2, 0));
// Since the cursor position matches to "hello+", it does not move
textarea.search_back(true);
assert_eq!(textarea.cursor(), (2, 0));
// When `match_current` parameter is set to `false`, match at the cursor position is ignored
textarea.search_back(false);
assert_eq!(textarea.cursor(), (1, 0));
// `false` is returned when no match was found
textarea.set_search_pattern("bye+").unwrap();
let match_found = textarea.search_back(false);
assert!(!match_found);sourcepub fn search_style(&self) -> Style
pub fn search_style(&self) -> Style
Get the text style at matches of text search. The default style is colored with blue in background.
use tui::style::{Style, Color};
use tui_textarea::TextArea;
let textarea = TextArea::default();
assert_eq!(textarea.search_style(), Style::default().bg(Color::Blue));sourcepub fn set_search_style(&mut self, style: Style)
pub fn set_search_style(&mut self, style: Style)
Set the text style at matches of text search. The default style is colored with blue in background.
use tui::style::{Style, Color};
use tui_textarea::TextArea;
let mut textarea = TextArea::default();
let red_bg = Style::default().bg(Color::Red);
textarea.set_search_style(red_bg);
assert_eq!(textarea.search_style(), red_bg);Trait Implementations
sourceimpl<'a> Default for TextArea<'a>
impl<'a> Default for TextArea<'a>
Create TextArea instance with empty text content.
use tui_textarea::TextArea;
let textarea = TextArea::default();
assert_eq!(textarea.lines(), [""]);
assert!(textarea.is_empty());sourceimpl<'a, I> From<I> for TextArea<'a> where
I: IntoIterator,
I::Item: Into<String>,
impl<'a, I> From<I> for TextArea<'a> where
I: IntoIterator,
I::Item: Into<String>,
Convert any iterator whose elements can be converted into String into TextArea. Each String element is
handled as line. Ensure that the strings don’t contain any newlines. This method is useful to create TextArea
from std::str::Lines.
use tui_textarea::TextArea;
// From `String`
let text = "hello\nworld";
let textarea = TextArea::from(text.lines());
assert_eq!(textarea.lines(), ["hello", "world"]);
// From array of `&str`
let textarea = TextArea::from(["hello", "world"]);
assert_eq!(textarea.lines(), ["hello", "world"]);
// From slice of `&str`
let slice = &["hello", "world"];
let textarea = TextArea::from(slice.iter().copied());
assert_eq!(textarea.lines(), ["hello", "world"]);sourceimpl<'a, S: Into<String>> FromIterator<S> for TextArea<'a>
impl<'a, S: Into<String>> FromIterator<S> for TextArea<'a>
Collect line texts from iterator as TextArea. It is useful when creating a textarea with text read from a file.
Iterator::collect handles errors which may happen on reading each lines. The following example reads text from
a file efficiently line-by-line.
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;
use tui_textarea::TextArea;
fn read_from_file<'a>(path: impl AsRef<Path>) -> io::Result<TextArea<'a>> {
let file = fs::File::open(path.as_ref())?;
io::BufReader::new(file).lines().collect()
}sourcefn from_iter<I: IntoIterator<Item = S>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = S>>(iter: I) -> Self
Creates a value from an iterator. Read more
Auto Trait Implementations
impl<'a> RefUnwindSafe for TextArea<'a>
impl<'a> Send for TextArea<'a>
impl<'a> Sync for TextArea<'a>
impl<'a> Unpin for TextArea<'a>
impl<'a> UnwindSafe for TextArea<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more