use std::io;
use std::time::Duration;
use eye_declare::{Application, Elements, Span, Spinner, Text, element};
use ratatui_core::style::{Color, Modifier, Style};
struct AppState {
messages: Vec<(String, Style)>,
thinking: bool,
}
fn app_view(state: &AppState) -> Elements {
element! {
#(for (text, style) in &state.messages {
Text {
Span(text: text.clone(), style: *style)
}
})
#(if state.thinking {
Spinner(label: "Processing...")
})
}
}
#[tokio::main]
async fn main() -> io::Result<()> {
let (mut app, handle) = Application::builder()
.state(AppState {
messages: vec![],
thinking: false,
})
.view(app_view)
.build()?;
tokio::spawn(async move {
handle.update(|s| {
s.messages.push((
"Application wrapper demo".into(),
Style::default()
.fg(Color::White)
.add_modifier(Modifier::BOLD),
));
s.messages.push((
"Updates flow through the Handle".into(),
Style::default().fg(Color::DarkGray),
));
});
tokio::time::sleep(Duration::from_millis(800)).await;
handle.update(|s| {
s.messages.push((
"Starting background work...".into(),
Style::default().fg(Color::Yellow),
));
s.thinking = true;
});
tokio::time::sleep(Duration::from_millis(1500)).await;
handle.update(|s| {
s.thinking = false;
s.messages.push((
"✓ Background work complete".into(),
Style::default().fg(Color::Green),
));
s.messages.push(("".into(), Style::default()));
});
});
app.run().await?;
println!();
Ok(())
}