From ff151faefd26fe18866b7b4db99e0f05a5529200 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 16 May 2024 15:20:33 +0200 Subject: [PATCH] lib: Add example --- examples/hello.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/hello.rs diff --git a/examples/hello.rs b/examples/hello.rs new file mode 100644 index 00000000..0bb89379 --- /dev/null +++ b/examples/hello.rs @@ -0,0 +1,64 @@ +use anyhow::Result; + +use radicle_tui as tui; + +use termion::event::Key; +use tui::store; +use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{Properties, Widget}; +use tui::{Channel, Exit}; + +#[derive(Clone, Debug)] +struct State { + welcome: String, +} + +enum Message { + Quit, + ReverseWelcome, +} + +impl store::State<()> for State { + type Message = Message; + + fn update(&mut self, message: Self::Message) -> Option> { + match message { + Message::Quit => Some(Exit { value: None }), + Message::ReverseWelcome => { + self.welcome = self.welcome.chars().rev().collect::(); + None + } + } + } + + fn tick(&self) {} +} + +#[tokio::main] +pub async fn main() -> Result<()> { + let channel = Channel::default(); + let state = State { + welcome: "Hello TUI".to_string(), + }; + + let welcome = Paragraph::new(&state, channel.tx.clone()) + .on_update(|state| { + ParagraphProps::default() + .text(&state.welcome.clone().into()) + .to_boxed() + }) + .on_event(|paragraph, key| { + paragraph + .downcast_mut::>() + .and_then(|paragraph| match key { + Key::Char('r') => paragraph.send(Message::ReverseWelcome).ok(), + Key::Char('q') => paragraph.send(Message::Quit).ok(), + _ => None, + }); + }) + .to_boxed(); + + tui::run(channel, state, welcome).await?; + + Ok(()) +}