pub struct Text { /* private fields */ }Expand description
A multi-line text editor with cursor management capabilities.
Examples
Single-line mode
use tty_text::{Text, Key};
let mut text = Text::new(false);
text.handle_input(Key::Char('a'));
text.handle_input(Key::Enter);
text.handle_input(Key::Char('b'));
assert_eq!((2, 0), text.cursor());
assert_eq!("ab", text.value());
assert_eq!(&vec![
"ab".to_string(),
], text.lines());Multi-line mode
use tty_text::{Text, Key};
let mut text = Text::new(true);
text.handle_input(Key::Char('a'));
text.handle_input(Key::Enter);
text.handle_input(Key::Char('b'));
assert_eq!((1, 1), text.cursor());
assert_eq!("a\nb", text.value());
assert_eq!(&vec![
"a".to_string(),
"b".to_string(),
], text.lines());Implementations
sourceimpl Text
impl Text
sourcepub fn new(multi_line: bool) -> Self
pub fn new(multi_line: bool) -> Self
Create a new, empty editor in the specified mode.
Examples
use tty_text::{Text, Key};
let mut text = Text::new(false);
text.handle_input(Key::Char('a'));
text.handle_input(Key::Char('b'));
text.handle_input(Key::Char('d'));
text.set_cursor((2, 0));
text.handle_input(Key::Char('c'));
assert_eq!((3, 0), text.cursor());
assert_eq!("abcd", text.value());sourcepub fn from(value: &str, cursor: (usize, usize), multi_line: bool) -> Self
pub fn from(value: &str, cursor: (usize, usize), multi_line: bool) -> Self
Create a new editor from the specified value and cursor state and in the specified mode.
Examples
Multi-line value and mode
use tty_text::{Text, Key};
let mut text = Text::from("Hello,\nworld!", (2, 1), true);
assert_eq!("Hello,\nworld!", text.value());
assert_eq!((2, 1), text.cursor());
assert_eq!(&vec![
"Hello,".to_string(),
"world!".to_string(),
], text.lines());Multi-line value collapsed by single-line mode
use tty_text::{Text, Key};
let mut text = Text::from("Hello,\n world!", (7, 1), false);
assert_eq!("Hello, world!", text.value());
assert_eq!((7, 0), text.cursor());
assert_eq!(&vec![
"Hello, world!".to_string(),
], text.lines());sourcepub fn cursor(&self) -> (usize, usize)
pub fn cursor(&self) -> (usize, usize)
This editor’s current cursor position as (columns, lines).
sourcepub fn set_cursor(&mut self, position: (usize, usize))
pub fn set_cursor(&mut self, position: (usize, usize))
Update this editor’s cursor position. The position will be clamped to the editor’s current value.
sourcepub fn handle_input(&mut self, input: Key)
pub fn handle_input(&mut self, input: Key)
Update this editor’s state from the specified input.
Auto Trait Implementations
impl RefUnwindSafe for Text
impl Send for Text
impl Sync for Text
impl Unpin for Text
impl UnwindSafe for Text
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
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