CursorMove

Enum CursorMove 

Source
#[non_exhaustive]
pub enum CursorMove {
Show 15 variants Forward, Back, Up, Down, Head, End, Top, Bottom, WordForward, WordEnd, WordBack, ParagraphForward, ParagraphBack, Jump(u16, u16), InViewport,
}
Expand description

Specify how to move the cursor.

This type is marked as #[non_exhaustive] since more variations may be supported in the future.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Forward

Move cursor forward by one character. When the cursor is at the end of line, it moves to the head of next line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["abc"]);

textarea.move_cursor(CursorMove::Forward);
assert_eq!(textarea.cursor(), (0, 1));
textarea.move_cursor(CursorMove::Forward);
assert_eq!(textarea.cursor(), (0, 2));
§

Back

Move cursor backward by one character. When the cursor is at the head of line, it moves to the end of previous line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["abc"]);

textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Back);
assert_eq!(textarea.cursor(), (0, 1));
§

Up

Move cursor up by one line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["a", "b", "c"]);

textarea.move_cursor(CursorMove::Down);
textarea.move_cursor(CursorMove::Down);
textarea.move_cursor(CursorMove::Up);
assert_eq!(textarea.cursor(), (1, 0));
§

Down

Move cursor down by one line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["a", "b", "c"]);

textarea.move_cursor(CursorMove::Down);
assert_eq!(textarea.cursor(), (1, 0));
textarea.move_cursor(CursorMove::Down);
assert_eq!(textarea.cursor(), (2, 0));
§

Head

Move cursor to the head of line. When the cursor is at the head of line, it moves to the end of previous line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["abc"]);

textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Forward);
textarea.move_cursor(CursorMove::Head);
assert_eq!(textarea.cursor(), (0, 0));
§

End

Move cursor to the end of line. When the cursor is at the end of line, it moves to the head of next line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["abc"]);

textarea.move_cursor(CursorMove::End);
assert_eq!(textarea.cursor(), (0, 3));
§

Top

Move cursor to the top of lines.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["a", "b", "c"]);

textarea.move_cursor(CursorMove::Down);
textarea.move_cursor(CursorMove::Down);
textarea.move_cursor(CursorMove::Top);
assert_eq!(textarea.cursor(), (0, 0));
§

Bottom

Move cursor to the bottom of lines.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["a", "b", "c"]);

textarea.move_cursor(CursorMove::Bottom);
assert_eq!(textarea.cursor(), (2, 0));
§

WordForward

Move cursor forward by one word. Word boundary appears at spaces, punctuations, and others. For example fn foo(a) consists of words fn, foo, (, a, ). When the cursor is at the end of line, it moves to the head of next line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["aaa bbb ccc"]);

textarea.move_cursor(CursorMove::WordForward);
assert_eq!(textarea.cursor(), (0, 4));
textarea.move_cursor(CursorMove::WordForward);
assert_eq!(textarea.cursor(), (0, 8));
§

WordEnd

Move cursor forward to the next end of word. Word boundary appears at spaces, punctuations, and others. For example fn foo(a) consists of words fn, foo, (, a, ). When the cursor is at the end of line, it moves to the end of the first word of the next line. This is similar to the ‘e’ mapping of Vim in normal mode.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from([
    "aaa bbb [[[ccc]]]",
    "",
    " ddd",
]);


textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (0, 2));      // At the end of 'aaa'
textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (0, 6));      // At the end of 'bbb'
textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (0, 10));     // At the end of '[[['
textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (0, 13));     // At the end of 'ccc'
textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (0, 16));     // At the end of ']]]'
textarea.move_cursor(CursorMove::WordEnd);
assert_eq!(textarea.cursor(), (2, 3));      // At the end of 'ddd'
§

WordBack

Move cursor backward by one word. Word boundary appears at spaces, punctuations, and others. For example fn foo(a) consists of words fn, foo, (, a, ).When the cursor is at the head of line, it moves to the end of previous line.

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["aaa bbb ccc"]);

textarea.move_cursor(CursorMove::End);
textarea.move_cursor(CursorMove::WordBack);
assert_eq!(textarea.cursor(), (0, 8));
textarea.move_cursor(CursorMove::WordBack);
assert_eq!(textarea.cursor(), (0, 4));
textarea.move_cursor(CursorMove::WordBack);
assert_eq!(textarea.cursor(), (0, 0));
§

ParagraphForward

Move cursor down by one paragraph. Paragraph is a chunk of non-empty lines. Cursor moves to the first line of paragraph.

use tui_textarea::{TextArea, CursorMove};

// aaa
//
// bbb
//
// ccc
// ddd
let mut textarea = TextArea::from(["aaa", "", "bbb", "", "ccc", "ddd"]);

textarea.move_cursor(CursorMove::ParagraphForward);
assert_eq!(textarea.cursor(), (2, 0));
textarea.move_cursor(CursorMove::ParagraphForward);
assert_eq!(textarea.cursor(), (4, 0));
§

ParagraphBack

Move cursor up by one paragraph. Paragraph is a chunk of non-empty lines. Cursor moves to the first line of paragraph.

use tui_textarea::{TextArea, CursorMove};

// aaa
//
// bbb
//
// ccc
// ddd
let mut textarea = TextArea::from(["aaa", "", "bbb", "", "ccc", "ddd"]);

textarea.move_cursor(CursorMove::Bottom);
textarea.move_cursor(CursorMove::ParagraphBack);
assert_eq!(textarea.cursor(), (4, 0));
textarea.move_cursor(CursorMove::ParagraphBack);
assert_eq!(textarea.cursor(), (2, 0));
textarea.move_cursor(CursorMove::ParagraphBack);
assert_eq!(textarea.cursor(), (0, 0));
§

Jump(u16, u16)

Move cursor to (row, col) position. When the position points outside the text, the cursor position is made fit within the text. Note that row and col are 0-based. (0, 0) means the first character of the first line.

When there are 10 lines, jumping to row 15 moves the cursor to the last line (row is 9 in the case). When there are 10 characters in the line, jumping to col 15 moves the cursor to end of the line (col is 10 in the case).

use tui_textarea::{TextArea, CursorMove};

let mut textarea = TextArea::from(["aaaa", "bbbb", "cccc"]);

textarea.move_cursor(CursorMove::Jump(1, 2));
assert_eq!(textarea.cursor(), (1, 2));

textarea.move_cursor(CursorMove::Jump(10,  10));
assert_eq!(textarea.cursor(), (2, 4));
§

InViewport

Move cursor to keep it within the viewport. For example, when a viewport displays line 8 to line 16:

  • cursor at line 4 is moved to line 8
  • cursor at line 20 is moved to line 16
  • cursor at line 12 is not moved

This is useful when you moved a cursor but you don’t want to move the viewport.

use tui_textarea::{TextArea, CursorMove};

// Let's say terminal height is 8.

// Create textarea with 20 lines "0", "1", "2", "3", ...
// The viewport is displaying from line 1 to line 8.
let mut textarea: TextArea = (0..20).into_iter().map(|i| i.to_string()).collect();

// Move cursor to the end of lines (line 20). It is outside the viewport (line 1 to line 8)
textarea.move_cursor(CursorMove::Bottom);
assert_eq!(textarea.cursor(), (19, 0));

// Cursor is moved to line 8 to enter the viewport
textarea.move_cursor(CursorMove::InViewport);
assert_eq!(textarea.cursor(), (7, 0));

Trait Implementations§

Source§

impl Clone for CursorMove

Source§

fn clone(&self) -> CursorMove

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CursorMove

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for CursorMove

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for CursorMove

Source§

fn eq(&self, other: &CursorMove) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for CursorMove

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for CursorMove

Source§

impl Eq for CursorMove

Source§

impl StructuralPartialEq for CursorMove

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,