pub struct Input { /* private fields */ }Expand description
The input buffer with cursor support.
Example:
use tui_input::Input;
let input: Input = "Hello World".into();
assert_eq!(input.cursor(), 11);
assert_eq!(input.to_string(), "Hello World");Implementations§
source§impl Input
impl Input
sourcepub fn new(value: String) -> Self
pub fn new(value: String) -> Self
Initialize a new instance with a given value Cursor will be set to the given value’s length.
sourcepub fn with_value(self, value: String) -> Self
pub fn with_value(self, value: String) -> Self
Set the value manually. Cursor will be set to the given value’s length.
sourcepub fn with_cursor(self, cursor: usize) -> Self
pub fn with_cursor(self, cursor: usize) -> Self
Set the cursor manually. If the input is larger than the value length, it’ll be auto adjusted.
pub fn reset(&mut self)
sourcepub fn handle(&mut self, req: InputRequest) -> InputResponse
pub fn handle(&mut self, req: InputRequest) -> InputResponse
Handle request and emit response.
sourcepub fn value(&self) -> &str
pub fn value(&self) -> &str
Get a reference to the current value.
Examples found in repository?
examples/crossterm_input.rs (line 22)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() -> Result<()> {
enable_raw_mode()?;
let stdout = stdout();
let mut stdout = stdout.lock();
execute!(stdout, Hide, EnterAlternateScreen, EnableMouseCapture)?;
let mut input: Input = "Hello ".into();
backend::write(&mut stdout, input.value(), input.cursor(), (0, 0), 15)?;
stdout.flush()?;
loop {
let event = read()?;
if let Event::Key(KeyEvent { code, .. }) = event {
match code {
KeyCode::Esc | KeyCode::Enter => {
break;
}
_ => {
if input.handle_event(&event).is_some() {
backend::write(
&mut stdout,
input.value(),
input.cursor(),
(0, 0),
15,
)?;
stdout.flush()?;
}
}
}
}
}
execute!(stdout, Show, LeaveAlternateScreen, DisableMouseCapture)?;
disable_raw_mode()?;
println!("{}", input);
Ok(())
}sourcepub fn cursor(&self) -> usize
pub fn cursor(&self) -> usize
Get the currect cursor placement.
Examples found in repository?
examples/crossterm_input.rs (line 22)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() -> Result<()> {
enable_raw_mode()?;
let stdout = stdout();
let mut stdout = stdout.lock();
execute!(stdout, Hide, EnterAlternateScreen, EnableMouseCapture)?;
let mut input: Input = "Hello ".into();
backend::write(&mut stdout, input.value(), input.cursor(), (0, 0), 15)?;
stdout.flush()?;
loop {
let event = read()?;
if let Event::Key(KeyEvent { code, .. }) = event {
match code {
KeyCode::Esc | KeyCode::Enter => {
break;
}
_ => {
if input.handle_event(&event).is_some() {
backend::write(
&mut stdout,
input.value(),
input.cursor(),
(0, 0),
15,
)?;
stdout.flush()?;
}
}
}
}
}
execute!(stdout, Show, LeaveAlternateScreen, DisableMouseCapture)?;
disable_raw_mode()?;
println!("{}", input);
Ok(())
}sourcepub fn visual_cursor(&self) -> usize
pub fn visual_cursor(&self) -> usize
Get the current cursor position with account for multispace characters.
sourcepub fn visual_scroll(&self, width: usize) -> usize
pub fn visual_scroll(&self, width: usize) -> usize
Get the scroll position with account for multispace characters.
Trait Implementations§
source§impl EventHandler for Input
impl EventHandler for Input
source§fn handle_event(&mut self, evt: &CrosstermEvent) -> Option<StateChanged>
fn handle_event(&mut self, evt: &CrosstermEvent) -> Option<StateChanged>
Handle crossterm event.
Auto Trait Implementations§
impl RefUnwindSafe for Input
impl Send for Input
impl Sync for Input
impl Unpin for Input
impl UnwindSafe for Input
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more