From 235478a854e1d80883ec58560838f1bd43c22b70 Mon Sep 17 00:00:00 2001 From: bqxbqx Date: Mon, 30 Sep 2024 15:49:11 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E2=9C=A8:=20wrapper=20render=20pass=20t?= =?UTF-8?q?est?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/src/lib.rs | 1 + cli/src/main.rs | 37 +++++------------------ cli/src/utils/command/mod.rs | 0 cli/src/utils/mod.rs | 2 ++ cli/src/utils/tui/mod.rs | 2 ++ cli/src/utils/tui/tui_base.rs | 13 ++++++++ cli/src/utils/tui/wrapper.rs | 57 +++++++++++++++++++++++++++++++++++ cli/tests/view_port.rs | 35 +++++++++++++++++++++ 8 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 cli/src/lib.rs create mode 100644 cli/src/utils/command/mod.rs create mode 100644 cli/src/utils/mod.rs create mode 100644 cli/src/utils/tui/mod.rs create mode 100644 cli/src/utils/tui/tui_base.rs create mode 100644 cli/src/utils/tui/wrapper.rs create mode 100644 cli/tests/view_port.rs diff --git a/cli/src/lib.rs b/cli/src/lib.rs new file mode 100644 index 0000000..fab870e --- /dev/null +++ b/cli/src/lib.rs @@ -0,0 +1 @@ +pub mod utils; \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index 713f1fd..bce4885 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,31 +1,8 @@ -use ratatui::{ - crossterm::event::{self, KeyCode, KeyEventKind}, - style::Stylize, - widgets::Paragraph, - DefaultTerminal, -}; -fn main() -> Result<(), std::io::Error> { - println!("Hello, world!"); - let mut terminal = ratatui::init(); - terminal.clear()?; - let app_result = run(terminal); - ratatui::restore(); - app_result -} - -fn run(mut terminal: DefaultTerminal) -> Result<(), std::io::Error> { - loop { - terminal.draw(|frame| { - let greeting = Paragraph::new("Hello Ratatui! (press 'q' to quit)") - .white() - .on_blue(); - frame.render_widget(greeting, frame.area()); - })?; - - if let event::Event::Key(key) = event::read()? { - if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { - return Ok(()); - } - } - } +fn main() { + // println!("Hello, world!"); + // let mut terminal = ratatui::init(); + // terminal.clear()?; + // // let app_result = run(terminal); + // ratatui::restore(); + // app_result } diff --git a/cli/src/utils/command/mod.rs b/cli/src/utils/command/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/cli/src/utils/mod.rs b/cli/src/utils/mod.rs new file mode 100644 index 0000000..0d3e077 --- /dev/null +++ b/cli/src/utils/mod.rs @@ -0,0 +1,2 @@ +pub mod command; +pub mod tui; diff --git a/cli/src/utils/tui/mod.rs b/cli/src/utils/tui/mod.rs new file mode 100644 index 0000000..70c0c3d --- /dev/null +++ b/cli/src/utils/tui/mod.rs @@ -0,0 +1,2 @@ +pub mod tui_base; +pub mod wrapper; diff --git a/cli/src/utils/tui/tui_base.rs b/cli/src/utils/tui/tui_base.rs new file mode 100644 index 0000000..e464e64 --- /dev/null +++ b/cli/src/utils/tui/tui_base.rs @@ -0,0 +1,13 @@ +use ratatui::{DefaultTerminal, Frame}; + +pub trait TuiBase { + fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<(), std::io::Error> { + Ok(()) + } + + fn handle_event(&mut self) -> Result<(), std::io::Error> { + Ok(()) + } + + fn draw(&self, frame: &mut Frame) {} +} diff --git a/cli/src/utils/tui/wrapper.rs b/cli/src/utils/tui/wrapper.rs new file mode 100644 index 0000000..9d6e9d5 --- /dev/null +++ b/cli/src/utils/tui/wrapper.rs @@ -0,0 +1,57 @@ +use ratatui::{ + layout::Alignment, + style::Stylize, + symbols::border, + text::{Line, Text}, + widgets::{ + block::{Position, Title}, + Block, Paragraph, Widget, + }, +}; + +use super::tui_base::TuiBase; + +#[derive(Debug, Default)] +pub struct Wrapper { + counter: u8, + exit: bool, +} + +impl Widget for &Wrapper { + fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { + let title = Title::from(" aurora ui cli ".bold()); + let instructions = Title::from(Line::from(vec![ + " Decrement ".into(), + "".blue().bold(), + " Increment ".into(), + "".blue().bold(), + " Quit ".into(), + " ".blue().bold(), + ])); + + let block = Block::bordered() + .title(title.alignment(Alignment::Center)) + .title( + instructions + .alignment(Alignment::Center) + .position(Position::Bottom), + ) + .border_set(border::THICK); + + let counter_text = Text::from(vec![Line::from(vec![ + "Value: ".into(), + self.counter.to_string().yellow(), + ])]); + + Paragraph::new(counter_text) + .centered() + .block(block) + .render(area, buf); + } +} + +impl TuiBase for Wrapper { + fn draw(&self, frame: &mut ratatui::Frame) { + frame.render_widget(self, ratatui::layout::Rect::default()); + } +} diff --git a/cli/tests/view_port.rs b/cli/tests/view_port.rs new file mode 100644 index 0000000..4eb8df3 --- /dev/null +++ b/cli/tests/view_port.rs @@ -0,0 +1,35 @@ +#[cfg(test)] +mod tests { + use cli::utils::tui::wrapper::Wrapper; + use ratatui::{ + buffer::Buffer, + layout::Rect, + style::{Style, Stylize}, + widgets::Widget, + }; + + #[test] + fn render() { + let app = Wrapper::default(); + let mut buf = Buffer::empty(Rect::new(0, 0, 50, 4)); + + app.render(buf.area, &mut buf); + + let mut expected = Buffer::with_lines(vec![ + "┏━━━━━━━━━━━━━━━━ aurora ui cli ━━━━━━━━━━━━━━━━━┓", + "┃ Value: 0 ┃", + "┃ ┃", + "┗━ Decrement Increment Quit ━━┛", + ]); + let title_style = Style::new().bold(); + let counter_style = Style::new().yellow(); + let key_style = Style::new().blue().bold(); + expected.set_style(Rect::new(17, 0, 15, 1), title_style); + expected.set_style(Rect::new(28, 1, 1, 1), counter_style); + expected.set_style(Rect::new(13, 3, 6, 1), key_style); + expected.set_style(Rect::new(30, 3, 7, 1), key_style); + expected.set_style(Rect::new(43, 3, 4, 1), key_style); + + assert_eq!(buf, expected); + } +}