use envision::prelude::*;
struct DividerApp;
#[derive(Clone)]
struct State {
horizontal: DividerState,
horizontal_labeled: DividerState,
vertical: DividerState,
vertical_labeled: DividerState,
}
#[derive(Clone, Debug)]
enum Msg {
Quit,
}
impl App for DividerApp {
type State = State;
type Message = Msg;
fn init() -> (State, Command<Msg>) {
let state = State {
horizontal: DividerState::new(),
horizontal_labeled: DividerState::new().with_label("Settings"),
vertical: DividerState::vertical(),
vertical_labeled: DividerState::vertical().with_label("V"),
};
(state, Command::none())
}
fn update(_state: &mut State, msg: Msg) -> Command<Msg> {
match msg {
Msg::Quit => Command::quit(),
}
}
fn view(state: &State, frame: &mut Frame) {
let theme = Theme::default();
let area = frame.area();
let main_chunks = Layout::vertical([
Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Min(5), ])
.split(area);
Divider::view(
&state.horizontal,
&mut RenderContext::new(frame, main_chunks[0], &theme),
);
Divider::view(
&state.horizontal_labeled,
&mut RenderContext::new(frame, main_chunks[2], &theme),
);
let vertical_chunks = Layout::horizontal([
Constraint::Length(1), Constraint::Length(2), Constraint::Length(1), Constraint::Min(0), ])
.split(main_chunks[4]);
Divider::view(
&state.vertical,
&mut RenderContext::new(frame, vertical_chunks[0], &theme),
);
Divider::view(
&state.vertical_labeled,
&mut RenderContext::new(frame, vertical_chunks[2], &theme),
);
}
fn handle_event(event: &Event) -> Option<Msg> {
if let Some(key) = event.as_key() {
match key.code {
Key::Char('q') | Key::Esc => Some(Msg::Quit),
_ => None,
}
} else {
None
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut vt = Runtime::<DividerApp, _>::virtual_builder(40, 10).build()?;
println!("=== Divider Example ===\n");
vt.tick()?;
println!("Horizontal and vertical dividers:");
println!("{}\n", vt.display());
Ok(())
}