Struct tty_text::Text

source ·
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

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());

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());

This editor’s current cursor position as (columns, lines).

This editor’s current value.

This editor’s value’s lines.

Update this editor’s cursor position. The position will be clamped to the editor’s current value.

Update this editor’s state from the specified input.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.