[][src]Struct crossterm_input::SyncReader

pub struct SyncReader { /* fields omitted */ }

A synchronous input reader (blocking).

SyncReader implements the Iterator trait. Documentation says:

An iterator has a method, next, which when called, returns an Option<Item>. next will return Some(Item) as long as there are elements, and once they've all been exhausted, will return None to indicate that iteration is finished. Individual iterators may choose to resume iteration, and so calling next again may or may not eventually start returning Some(Item) again at some point.

SyncReader is an individual iterator and it doesn't use None to indicate that the iteration is finished. You can expect additional Some(InputEvent) after calling next even if you have already received None. Unfortunately, None means that an error occurred, but you're free to call next again. This behavior will be changed in the future to avoid errors consumption.

Notes

  • It requires enabled raw mode (see the crossterm_screen crate documentation to learn more).
  • See the AsyncReader if you want a non blocking reader.

Examples

use std::{thread, time::Duration};

use crossterm_input::{input, InputEvent, KeyEvent, RawScreen};

fn main() {
    println!("Press 'ESC' to quit.");

    // Enable raw mode and keep the `_raw` around otherwise the raw mode will be disabled
    let _raw = RawScreen::into_raw_mode();

    // Create an input from our screen
    let input = input();

    // Create a sync reader
    let mut reader = input.read_sync();

    loop {
        if let Some(event) = reader.next() { // Blocking call
            match event {
                InputEvent::Keyboard(KeyEvent::Esc) => {
                    println!("Program closing ...");
                    break;
                 }
                 InputEvent::Mouse(event) => { /* Mouse event */ }
                 _ => { /* Other events */ }
            }
        }
        thread::sleep(Duration::from_millis(50));
    }
} // `_raw` dropped <- raw mode disabled

Trait Implementations

impl Iterator for SyncReader[src]

type Item = InputEvent

The type of the elements being iterated over.

fn next(&mut self) -> Option<Self::Item>[src]

Tries to read the next input event (blocking).

None doesn't mean that the iteration is finished. See the SyncReader documentation for more information.

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]