Skip to content

Commit

Permalink
feat ✨: wrapper render pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
BQXBQX committed Sep 30, 2024
1 parent e5228c7 commit 235478a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 30 deletions.
1 change: 1 addition & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod utils;
37 changes: 7 additions & 30 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
}
Empty file added cli/src/utils/command/mod.rs
Empty file.
2 changes: 2 additions & 0 deletions cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod command;
pub mod tui;
2 changes: 2 additions & 0 deletions cli/src/utils/tui/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod tui_base;
pub mod wrapper;
13 changes: 13 additions & 0 deletions cli/src/utils/tui/tui_base.rs
Original file line number Diff line number Diff line change
@@ -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) {}
}
57 changes: 57 additions & 0 deletions cli/src/utils/tui/wrapper.rs
Original file line number Diff line number Diff line change
@@ -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(),
"<Left>".blue().bold(),
" Increment ".into(),
"<Right>".blue().bold(),
" Quit ".into(),
"<Q> ".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());
}
}
35 changes: 35 additions & 0 deletions cli/tests/view_port.rs
Original file line number Diff line number Diff line change
@@ -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 <Left> Increment <Right> Quit <Q> ━━┛",
]);
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);
}
}

0 comments on commit 235478a

Please sign in to comment.