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: EventSenderThe sender half of an MPSC channel used to send messages
to the Program’s event loop.
input_source: InputSourceThe input source to read from.
Implementations§
Source§impl InputHandler
impl InputHandler
Sourcepub fn new<T>(event_tx: T) -> Selfwhere
T: Into<EventSender>,
pub fn new<T>(event_tx: T) -> Selfwhere
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- AnEventSenderto 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);Sourcepub fn with_source<T>(event_tx: T, input_source: InputSource) -> Selfwhere
T: Into<EventSender>,
pub fn with_source<T>(event_tx: T, input_source: InputSource) -> Selfwhere
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- AnEventSenderto send processed events to the main program loopinput_source- TheInputSourceto 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);Sourcepub async fn run(self) -> Result<(), Error>
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...
}