Struct tui_input::Input

source ·
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

source

pub fn new(value: String) -> Self

Initialize a new instance with a given value Cursor will be set to the given value’s length.

source

pub fn with_value(self, value: String) -> Self

Set the value manually. Cursor will be set to the given value’s length.

source

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.

source

pub fn reset(&mut self)

source

pub fn handle(&mut self, req: InputRequest) -> InputResponse

Handle request and emit response.

source

pub fn value(&self) -> &str

Get a reference to the current value.

Examples found in repository?
examples/crossterm_input.rs (line 21)
14
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
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(())
}
source

pub fn cursor(&self) -> usize

Get the currect cursor placement.

Examples found in repository?
examples/crossterm_input.rs (line 21)
14
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
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(())
}
source

pub fn visual_cursor(&self) -> usize

Get the current cursor position with account for multispace characters.

source

pub fn visual_scroll(&self, width: usize) -> usize

Get the scroll position with account for multispace characters.

Trait Implementations§

source§

impl Clone for Input

source§

fn clone(&self) -> Input

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 Input

source§

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

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

impl Default for Input

source§

fn default() -> Input

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

impl Display for Input

source§

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

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

impl EventHandler for Input

source§

fn handle_event(&mut self, evt: &CrosstermEvent) -> Option<StateChanged>

Handle crossterm event.

source§

impl From<&str> for Input

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl From<Input> for String

source§

fn from(input: Input) -> Self

Converts to this type from the input type.
source§

impl From<String> for Input

source§

fn from(value: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Input

§

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

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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> ToCompactString for T
where T: Display,

source§

impl<T> ToLine for T
where T: Display,

source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
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> ToSpan for T
where T: Display,

source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T> ToText for T
where T: Display,

source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
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.