Struct promptuity::InputCursor

source ·
pub struct InputCursor { /* private fields */ }
Expand description

A struct to represent the input cursor.

InputCursor is used in prompts like crate::prompts::Input, crate::prompts::Password, and crate::prompts::Number, proving useful in prompts where users input text.
It supports maintaining and manipulating cursor position, as well as inserting text at the cursor location.

Examples

use promptuity::InputCursor;

let mut cursor = InputCursor::default();

cursor.insert('f');
cursor.insert('o');
cursor.insert('o');
assert_eq!(cursor.value(), "foo");
assert_eq!(cursor.cursor(), 3);
assert_eq!(cursor.col(), 3);

cursor.move_home();
cursor.insert('b');
cursor.insert('a');
cursor.insert('r');
cursor.insert(' ');
assert_eq!(cursor.value(), "bar foo");
assert_eq!(cursor.cursor(), 4);
assert_eq!(cursor.col(), 4);

Supports multi-byte characters. (Does not support emojis)

use promptuity::InputCursor;

let mut cursor = InputCursor::default();

cursor.insert('日');
cursor.insert('本');
cursor.insert('語');
cursor.delete_left_char();
assert_eq!(cursor.value(), "日本");
assert_eq!(cursor.cursor(), 2);
assert_eq!(cursor.col(), 4);

Implementations§

source§

impl InputCursor

source

pub fn new(value: String, cursor: usize) -> Self

Creates a new InputCursor with the given value and cursor position.

source

pub fn from(value: String) -> Self

Creates a new InputCursor with the given value.
The cursor position is set at the end of the input string.

Examples
use promptuity::InputCursor;

let mut cursor = InputCursor::from("foo".into());
assert_eq!(cursor.value(), "foo");
assert_eq!(cursor.cursor(), 3);
source

pub fn col(&self) -> u16

Returns the column position of the cursor.

source

pub fn value(&self) -> String

Returns the value.

source

pub fn set_value(&mut self, value: String) -> &mut Self

Sets the value.

source

pub fn cursor(&self) -> usize

Returns the cursor position.

source

pub fn set_cursor(&mut self, cursor: usize) -> &mut Self

Sets the cursor position.

source

pub fn split(&self) -> (String, String, String)

Returns a tuple split into the string at the cursor position and its surrounding text.
split is useful when styling the cursor.

Examples
use promptuity::InputCursor;

let mut cursor = InputCursor::from("Hello".into());
cursor.move_left();
cursor.move_left();
assert_eq!(cursor.split(), ("Hel".into(), "l".into(), "o".into()));
source

pub fn is_empty(&self) -> bool

Returns whether the input is empty or not.

source

pub fn move_left(&mut self)

Moves the cursor one character to the left.

source

pub fn move_right(&mut self)

Moves the cursor one character to the right.

source

pub fn move_home(&mut self)

Moves the cursor to the beginning of the line.

source

pub fn move_end(&mut self)

Moves the cursor to the end of the line.

source

pub fn insert(&mut self, chr: char)

Inserts a character at the cursor position.

source

pub fn delete_left_char(&mut self)

Deletes the character to the left of the cursor.

source

pub fn delete_left_word(&mut self)

Deletes the word to the left of the cursor.

source

pub fn delete_right_char(&mut self)

Deletes the character to the right of the cursor.

source

pub fn delete_rest_line(&mut self)

Deletes characters to the right of the cursor up to the end of the line.

source

pub fn delete_line(&mut self)

Deletes the entire line.

Trait Implementations§

source§

impl Clone for InputCursor

source§

fn clone(&self) -> InputCursor

Returns a copy 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 InputCursor

source§

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

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

impl Default for InputCursor

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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

§

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>,

§

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>,

§

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.