[][src]Module unsegen::input

Raw terminal input events, common abstractions for application (component) behavior and means to easily distribute events.

Example:

use unsegen::input::*;
use std::io::Read;

struct Scroller {
    line_number: u32,
    end: u32,
}

impl Scrollable for Scroller {
    fn scroll_backwards(&mut self) -> OperationResult {
        if self.line_number > 0 {
            self.line_number -= 1;
            Ok(())
        } else {
            Err(())
        }
    }
    fn scroll_forwards(&mut self) -> OperationResult {
        if self.line_number < self.end - 1 {
            self.line_number += 1;
            Ok(())
        } else {
            Err(())
        }
    }
}

fn main() {
    let mut scroller = Scroller {
        line_number: 0,
        end: 5,
    };

    // Read all inputs from something that implements Read
    for input in Input::read_all(&[b'1', b'2', b'3', b'4'][..]) {
        let input = input.unwrap();

        // Define a chain of handlers for different kinds of events!
        // If a handler (Behavior) cannot process an input, it is passed down the chain.
        let leftover = input
            .chain((Key::Char('1'), || println!("Got a 1!")))
            .chain(
                ScrollBehavior::new(&mut scroller)
                    .backwards_on(Key::Char('2'))
                    .forwards_on(Key::Char('3')),
            )
            .chain(|i: Input| {
                if let Event::Key(Key::Char(c)) = i.event {
                    println!("Got some char: {}", c);
                    None // matches! event will be consumed
                } else {
                    Some(i)
                }
            })
            .finish();
        if let Some(e) = leftover {
            println!("Could not handle input {:?}", e);
        }
    }

    // We could not scroll back first, but one line forwards later!
    assert_eq!(scroller.line_number, 1);
}

Structs

EditBehavior

Collection of triggers for functions of something Editable implementing Behavior.

Input

A structure corresponding to a single input event, e.g., a single keystroke or mouse event.

InputChain

An intermediate element in a chain of Behaviors that are matched against the event and executed if applicable.

InputIter

An iterator of Input events.

NavigateBehavior

Collection of triggers for functions of something Navigatable implementing Behavior.

ScrollBehavior

Collection of triggers for functions of something Scrollable implementing Behavior.

WriteBehavior

Collection of triggers for functions of something Writable implementing Behavior.

Enums

Event

An event reported by the terminal.

Key

A key.

MouseButton

A mouse button.

MouseEvent

A mouse related event.

Traits

Behavior

Something that reacts to input and possibly consumes it.

Editable

Something that acts like a text editor.

Navigatable

Something that can be navigated like a cursor in a text editor or character in a simple 2D game.

Scrollable

Something that can be scrolled. Use in conjunction with ScrollBehavior to manipulate when input arrives.

ToEvent

Used conveniently supply Event-like arguments to a number of functions in the input module. For example, you can supply Key::Up instead of Event::Key(Key::Up).

Writable

Something that can be written to in the sense of a text box, editor or text input.

Type Definitions

OperationResult

A common return type for Operations such as functions of Scrollable, Writable, Navigatable, etc.