Text

Struct 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§

Source§

impl Text

Source

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

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

pub fn cursor(&self) -> (usize, usize)

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

Source

pub fn value(&self) -> String

This editor’s current value.

Source

pub fn lines(&self) -> &Vec<String>

This editor’s value’s lines.

Source

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.

Source

pub fn handle_input(&mut self, input: Key)

Update this editor’s state from the specified input.

Auto Trait Implementations§

§

impl Freeze for Text

§

impl RefUnwindSafe for Text

§

impl Send for Text

§

impl Sync for Text

§

impl Unpin for Text

§

impl UnwindSafe for Text

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.