pub struct TerminalInput<'stdout> { /* private fields */ }
Expand description

Struct that stores an specific platform implementation for input related actions.

Check /examples/input the examples folder on github for more info.

extern crate crossterm;
use self::crossterm::Screen;
use self::crossterm::input;

let input = input();
let result = input.read_line();
let pressed_char = input.read_char();

!! Take note when using input with raw mode you should use the Screen type. !!

let screen = Screen::new(true);
let input = crossterm::input::from_screen(&screen);///

Implementations§

Create new instance of TerminalInput whereon input related actions could be preformed.

Create new instance of TerminalInput whereon input related actions could be preformed.

Read one line from the user input.

let input = input();
 match input.read_line() {
    Ok(s) => println!("string typed: {}", s),
    Err(e) => println!("error: {}", e),
 }

Read one character from the user input

let input = input();

 match input.read_char() {
    Ok(c) => println!("character pressed: {}", c),
    Err(e) => println!("error: {}", e),
  }

Read the input asynchronously from the user.

This call will not block the current thread. Under the hood a thread is fired which will read input on unix systems from TTY and on windows systems with ‘_getwch’ and ‘_getwche’

// we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
let screen = Screen::new(true);
let input = crossterm::input::from_screen(&screen);

let mut stdin = input.read_async().bytes();

for i in 0..100 {

    // Get the next character typed. This is None if nothing is pressed. And Some(Ok(u8 value of character))
    let a = stdin.next();

    println!("pressed key: {:?}", a);

    if let Some(Ok(b'x')) = a {
        println!("The key: `x` was pressed and program is terminated.");
        break;
    }
    // simulate some timeout so that we can see the character on the screen.
    thread::sleep(time::Duration::from_millis(50));
}

Read the input asynchronously until a certain character is hit.

This is the same as read_async() but stops reading when a certain character is hit.

// we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
let screen = Screen::new(true);

// create an instance of `Crossterm` which will preform the actions on the raw screen.
let crossterm = Crossterm::from_screen(&screen);
let input = crossterm.input();
let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor();

let mut stdin = input.read_until_async(b'\r').bytes();

for i in 0..100 {
    terminal.clear(ClearType::All);
    cursor.goto(1, 1);
    let a = stdin.next();

    println!("pressed key: {:?}", a);

    if let Some(Ok(b'\r')) = a {
        println!("The enter key is hit and program is not listening to input anymore.");
        break;
    }

    if let Some(Ok(b'x')) = a {
         println!("The key: x was pressed and program is terminated.");
        break;
    }

    thread::sleep(time::Duration::from_millis(100));
}

This will prevent the current thread from continuing until the passed KeyEvent has happened.

use crossterm::input::{TerminalInput, KeyEvent};

fn main() {
    println!("Press 'x' to quit...");
    TerminalInput::wait_until(KeyEvent::OnKeyPress(b'x'));
}

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.