Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Add imUI example
Erik Kundt committed 1 year ago
commit 75cfeae169a5099976f692a16c7dfd6158f7226c
parent 1b22baf4d15e31e1fd3b7e288fc44bfc726e375a
1 file changed +78 -0
added examples/hello_im.rs
@@ -0,0 +1,78 @@
+
use anyhow::Result;
+

+
use termion::event::Key;
+

+
use ratatui::Frame;
+

+
use radicle_tui as tui;
+

+
use tui::store;
+
use tui::ui::im;
+
use tui::ui::im::widget::Window;
+
use tui::ui::im::{Borders, Context};
+
use tui::{Channel, Exit};
+

+
const ALIEN: &str = r#"
+
     ///             ///    ,---------------------------------. 
+
     ///             ///    | Hey there, press (q) to quit... |
+
        //         //       '---------------------------------'  
+
        //,,,///,,,//      .. 
+
     ///////////////////  .  
+
  //////@@@@@//////@@@@@///  
+
  //////@@###//////@@###///  
+
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
+
     ,,,  ///   ///  ,,,     
+
     ,,,  ///   ///  ,,,     
+
          ///   ///          
+
        /////   /////
+
"#;
+

+
#[derive(Clone, Debug)]
+
struct State {
+
    alien: String,
+
}
+

+
#[derive(Clone)]
+
enum Message {
+
    Quit,
+
}
+

+
impl store::State<()> for State {
+
    type Message = Message;
+

+
    fn update(&mut self, message: Self::Message) -> Option<tui::Exit<()>> {
+
        match message {
+
            Message::Quit => Some(Exit { value: None }),
+
        }
+
    }
+
}
+

+
#[derive(Default)]
+
struct App {}
+

+
impl im::App for App {
+
    type State = State;
+
    type Message = Message;
+

+
    fn update(&self, ctx: &Context<Message>, frame: &mut Frame, state: &State) -> Result<()> {
+
        Window::default().show(ctx, |ui| {
+
            ui.text_view(frame, state.alien.clone(), &mut (0, 0), Some(Borders::None));
+

+
            if ui.input_global(|key| key == Key::Char('q')) {
+
                ui.send_message(Message::Quit);
+
            }
+
        });
+

+
        Ok(())
+
    }
+
}
+

+
#[tokio::main]
+
pub async fn main() -> Result<()> {
+
    let state = State {
+
        alien: ALIEN.to_string(),
+
    };
+
    tui::im(Channel::default(), state, App::default()).await?;
+

+
    Ok(())
+
}