Files
pages-tui/examples/simple_input.rs
2026-01-11 12:50:31 +01:00

56 lines
1.5 KiB
Rust

extern crate alloc;
use tui_orchestrator::input::{default_bindings, ComponentAction, InputError, InputSource, Key};
struct MockInput {
keys: alloc::vec::Vec<Key>,
index: usize,
}
impl MockInput {
fn new(keys: alloc::vec::Vec<Key>) -> Self {
Self { keys, index: 0 }
}
}
impl InputSource for MockInput {
fn read_key(&mut self) -> Result<Key, InputError> {
if self.index < self.keys.len() {
let key = self.keys[self.index];
self.index += 1;
Ok(key)
} else {
Err(InputError::BackendError)
}
}
}
fn main() {
let bindings = default_bindings();
let mut input = MockInput::new(alloc::vec![Key::tab(), Key::enter(), Key::esc(),]);
loop {
match input.read_key() {
Ok(key) => {
if let Some(action) = bindings.get(&key) {
match action {
ComponentAction::Next => println!("Next"),
ComponentAction::Select => println!("Select"),
ComponentAction::Cancel => {
println!("Cancel - exiting");
break;
}
_ => println!("Other action: {:?}", action),
}
} else {
println!("No binding for key: {:?}", key);
}
}
Err(e) => {
println!("Error: {:?}", e);
break;
}
}
}
}