Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Improve examples
Erik Kundt committed 1 year ago
commit cc1f2c1d701c9b2d6bfa88fbab6ba4c7e628b6f4
parent 80d5bbac532d49b9ffb42624ad57308362fae4bb
2 files changed +142 -41
added examples/basic.rs
@@ -0,0 +1,111 @@
+
use anyhow::Result;
+

+
use termion::event::Key;
+

+
use ratatui::layout::Constraint;
+

+
use radicle_tui as tui;
+

+
use tui::store;
+
use tui::ui::widget::container::{Column, Container, Header, HeaderProps};
+
use tui::ui::widget::text::{Paragraph, ParagraphProps};
+
use tui::ui::widget::window::{Page, Shortcuts, ShortcutsProps, Window, WindowProps};
+
use tui::ui::widget::ToWidget;
+
use tui::{BoxedAny, Channel, Exit};
+

+
const CONTENT: &str = r#"
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
+
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
+
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
+
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
+

+
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt 
+
mollit anim id est laborum.
+
"#;
+

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

+
enum Message {
+
    Quit,
+
    ReverseContent,
+
}
+

+
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 }),
+
            Message::ReverseContent => {
+
                self.content = self.content.chars().rev().collect::<String>();
+
                None
+
            }
+
        }
+
    }
+

+
    fn tick(&mut self) {}
+
}
+

+
#[tokio::main]
+
pub async fn main() -> Result<()> {
+
    let channel = Channel::default();
+
    let sender = channel.tx.clone();
+
    let state = State {
+
        content: CONTENT.to_string(),
+
    };
+

+
    let page = Page::default()
+
        .content(
+
            Container::default()
+
                .header(Header::default().to_widget(sender.clone()).on_update(|_| {
+
                    HeaderProps::default()
+
                        .columns(vec![
+
                            Column::new("", Constraint::Length(0)),
+
                            Column::new(
+
                                "The standard Lorem Ipsum passage, used since the 1500s",
+
                                Constraint::Fill(1),
+
                            ),
+
                        ])
+
                        .to_boxed_any()
+
                        .into()
+
                }))
+
                .content(Paragraph::default().to_widget(sender.clone()).on_update(
+
                    |state: &State| {
+
                        ParagraphProps::default()
+
                            .text(&state.content.clone().into())
+
                            .can_scroll(false)
+
                            .to_boxed_any()
+
                            .into()
+
                    },
+
                ))
+
                .to_widget(sender.clone()),
+
        )
+
        .shortcuts(
+
            Shortcuts::default()
+
                .to_widget(sender.clone())
+
                .on_update(|_| {
+
                    ShortcutsProps::default()
+
                        .shortcuts(&[("q", "quit"), ("r", "reverse")])
+
                        .to_boxed_any()
+
                        .into()
+
                }),
+
        )
+
        .to_widget(sender.clone());
+

+
    let window = Window::default()
+
        .page(0, page)
+
        .to_widget(sender.clone())
+
        .on_event(|key, _, _| match key {
+
            Key::Char('r') => Some(Message::ReverseContent),
+
            Key::Char('q') => Some(Message::Quit),
+
            _ => None,
+
        })
+
        .on_update(|_| WindowProps::default().current_page(0).to_boxed_any().into());
+

+
    tui::run(channel, state, window).await?;
+

+
    Ok(())
+
}
modified examples/hello.rs
@@ -1,23 +1,39 @@
use anyhow::Result;

+
use termion::event::Key;
+

+
use ratatui::style::Color;
+
use ratatui::text::Text;
+

use radicle_tui as tui;

-
use termion::event::Key;
use tui::store;
-
use tui::ui::widget::container::Container;
use tui::ui::widget::text::{Paragraph, ParagraphProps};
-
use tui::ui::widget::window::{Page, Shortcuts, ShortcutsProps, Window, WindowProps};
use tui::ui::widget::ToWidget;
use tui::{BoxedAny, Channel, Exit};

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

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

enum Message {
    Quit,
-
    ReverseWelcome,
}

impl store::State<()> for State {
@@ -26,14 +42,10 @@ impl store::State<()> for State {
    fn update(&mut self, message: Self::Message) -> Option<tui::Exit<()>> {
        match message {
            Message::Quit => Some(Exit { value: None }),
-
            Message::ReverseWelcome => {
-
                self.welcome = self.welcome.chars().rev().collect::<String>();
-
                None
-
            }
        }
    }

-
    fn tick(&self) {}
+
    fn tick(&mut self) {}
}

#[tokio::main]
@@ -41,45 +53,23 @@ pub async fn main() -> Result<()> {
    let channel = Channel::default();
    let sender = channel.tx.clone();
    let state = State {
-
        welcome: "Hello TUI".to_string(),
+
        alien: ALIEN.to_string(),
    };

-
    let welcome = Page::default()
-
        .content(
-
            Container::default()
-
                .content(Paragraph::default().to_widget(sender.clone()).on_update(
-
                    |state: &State| {
-
                        ParagraphProps::default()
-
                            .text(&state.welcome.clone().into())
-
                            .to_boxed_any()
-
                            .into()
-
                    },
-
                ))
-
                .to_widget(sender.clone()),
-
        )
-
        .shortcuts(
-
            Shortcuts::default()
-
                .to_widget(sender.clone())
-
                .on_update(|_| {
-
                    ShortcutsProps::default()
-
                        .shortcuts(&[("q", "quit"), ("r", "reverse")])
-
                        .to_boxed_any()
-
                        .into()
-
                }),
-
        )
-
        .to_widget(sender.clone());
-

-
    let window = Window::default()
-
        .page(0, welcome)
+
    let scene = Paragraph::default()
        .to_widget(sender.clone())
        .on_event(|key, _, _| match key {
-
            Key::Char('r') => Some(Message::ReverseWelcome),
            Key::Char('q') => Some(Message::Quit),
            _ => None,
        })
-
        .on_update(|_| WindowProps::default().current_page(0).to_boxed_any().into());
+
        .on_update(|state: &State| {
+
            ParagraphProps::default()
+
                .text(&Text::styled(state.alien.clone(), Color::Rgb(85, 85, 255)))
+
                .to_boxed_any()
+
                .into()
+
        });

-
    tui::run(channel, state, window).await?;
+
    tui::run(channel, state, scene).await?;

    Ok(())
}