Struct InputHandler

Source
pub struct InputHandler {
    pub event_tx: EventSender,
    pub input_source: InputSource,
}
Expand description

InputHandler is responsible for processing terminal events and sending them as messages to the Program’s event loop.

It continuously reads events from the crossterm event stream and converts them into appropriate Msg types.

Fields§

§event_tx: EventSender

The sender half of an MPSC channel used to send messages to the Program’s event loop.

§input_source: InputSource

The input source to read from.

Implementations§

Source§

impl InputHandler

Source

pub fn new<T>(event_tx: T) -> Self
where T: Into<EventSender>,

Creates a new InputHandler with the given message sender using terminal input.

This constructor sets up the input handler to read from the standard terminal using crossterm’s event stream. This is the most common usage pattern.

§Arguments
  • event_tx - An EventSender to send processed events to the main program loop
§Examples
use bubbletea_rs::input::InputHandler;
use tokio::sync::mpsc;

let (tx, rx) = mpsc::unbounded_channel();
let input_handler = InputHandler::new(tx);
Source

pub fn with_source<T>(event_tx: T, input_source: InputSource) -> Self
where T: Into<EventSender>,

Creates a new InputHandler with a custom input source.

This constructor allows you to specify a custom input source instead of the default terminal input. This is useful for testing, reading from files, or processing input from network streams.

§Arguments
  • event_tx - An EventSender to send processed events to the main program loop
  • input_source - The InputSource to read from (terminal or custom reader)
§Examples

Reading from a file:

use bubbletea_rs::input::{InputHandler, InputSource};
use tokio::sync::mpsc;
use std::pin::Pin;

let (tx, rx) = mpsc::unbounded_channel();
let file_content = std::io::Cursor::new("test input\n");
let custom_source = InputSource::Custom(Box::pin(file_content));
let input_handler = InputHandler::with_source(tx, custom_source);
Source

pub async fn run(self) -> Result<(), Error>

Runs the input handler loop asynchronously.

This method continuously reads events from the configured input source and processes them until the loop terminates. It converts raw terminal events into typed Msg objects and sends them through the event channel to the main program loop.

The loop terminates when:

  • The event sender channel is closed (receiver dropped)
  • An I/O error occurs while reading input
  • EOF is reached for custom input sources
§Returns

Returns Ok(()) on normal termination, or an Error if an I/O error occurs.

§Errors

This function returns an error if:

  • There’s an I/O error reading from the input source
  • The underlying crossterm event stream encounters an error
§Examples
use bubbletea_rs::input::InputHandler;
use tokio::sync::mpsc;

let (tx, mut rx) = mpsc::unbounded_channel();
let input_handler = InputHandler::new(tx);

// Run the input handler in a separate task
let input_task = tokio::spawn(async move {
    input_handler.run().await
});

// Process incoming messages
while let Some(msg) = rx.recv().await {
    // Handle the message...
}

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.