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
impl InputCursor
sourcepub fn new(value: String, cursor: usize) -> Self
pub fn new(value: String, cursor: usize) -> Self
Creates a new InputCursor with the given value and cursor position.
sourcepub fn from(value: String) -> Self
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);sourcepub fn set_cursor(&mut self, cursor: usize) -> &mut Self
pub fn set_cursor(&mut self, cursor: usize) -> &mut Self
Sets the cursor position.
sourcepub fn split(&self) -> (String, String, String)
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()));sourcepub fn move_right(&mut self)
pub fn move_right(&mut self)
Moves the cursor one character to the right.
sourcepub fn delete_left_char(&mut self)
pub fn delete_left_char(&mut self)
Deletes the character to the left of the cursor.
sourcepub fn delete_left_word(&mut self)
pub fn delete_left_word(&mut self)
Deletes the word to the left of the cursor.
sourcepub fn delete_right_char(&mut self)
pub fn delete_right_char(&mut self)
Deletes the character to the right of the cursor.
sourcepub fn delete_rest_line(&mut self)
pub fn delete_rest_line(&mut self)
Deletes characters to the right of the cursor up to the end of the line.
sourcepub fn delete_line(&mut self)
pub fn delete_line(&mut self)
Deletes the entire line.
Trait Implementations§
source§impl Clone for InputCursor
impl Clone for InputCursor
source§fn clone(&self) -> InputCursor
fn clone(&self) -> InputCursor
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more