Expand description
Interactive components a plugin registers by name (0.0.14), with
PluginRegistrar::component.
A plugin depends on this crate and core only, never on
rs-rich-interact, so the contract here is its own small one: events
arrive as ComponentEvents, with keys by name (down, ctrl+k); a
component answers with a ComponentFlow and renders a
ComponentView of core Segments. A host adapts it to its own
component model: rs-rich-interact’s PluginView mounts one in a split,
tabs or a modal beside the built-ins, and runs it through the same
drivers, headless included.
A component finishes with text (ComponentFlow::Done): whatever it
chose or typed, for the app that mounted it to interpret.
use rich_plugin_api::component::{
ComponentContext, ComponentEvent, ComponentFlow, ComponentView, PluginComponent,
};
/// Counts Up presses; Enter answers with the count.
#[derive(Default)]
struct Counter(u32);
impl PluginComponent for Counter {
fn handle(&mut self, event: &ComponentEvent, _: &ComponentContext<'_>) -> ComponentFlow {
match event.key() {
Some("up") => self.0 += 1,
Some("enter") => return ComponentFlow::Done(self.0.to_string()),
_ => return ComponentFlow::Ignored,
}
ComponentFlow::Continue
}
fn render(&self, context: &ComponentContext<'_>) -> ComponentView {
ComponentView::new(context.markup(&format!("count: [bold]{}[/]", self.0)))
}
}
let console = rich::Console::new();
let context = ComponentContext::new(&console, 20, 1);
let mut counter = Counter::default();
counter.handle(&ComponentEvent::Key("up".into()), &context);
assert!(matches!(
counter.handle(&ComponentEvent::Key("enter".into()), &context),
ComponentFlow::Done(count) if count == "1"
));Structs§
- Component
Binding - One thing a key does in a component, for a help overlay, a shortcut sheet or status-bar hints. A host lists it under the component’s registered name, and configuration can rebind it there.
- Component
Context - What a component renders and handles events for: the console to render with, and the space it has.
- Component
View - A rendered view: lines of segments, and where the terminal cursor goes
(a text caret), or
Noneto hide it.
Enums§
- Component
Event - An event for a
PluginComponent. - Component
Flow - What a component wants after an event.
Traits§
- Plugin
Component - An interactive component: a state machine that handles one event at a
time and renders its state. Registered with
PluginRegistrar::componentthrough aComponentFactory.
Type Aliases§
- Component
Factory - Makes a fresh component each time a host mounts one, so one registration can be mounted many times.