use crate::app::App; use ratatui::{ Frame, style::Stylize, symbols::border, text::{Line, Text}, widgets::{Block, Paragraph, Widget}, }; pub fn draw(frame: &mut Frame, app: &App) { let rect = draw_layout(app); frame.render_widget(rect, frame.area()); } fn draw_layout(app: &App) -> impl Widget { let title = Line::from(" Counter App Tutorial ".bold()); let instructions = Line::from(vec![ " Decrement ".into(), "".blue().bold(), " Increment ".into(), "".blue().bold(), " Quit ".into(), " ".blue().bold(), ]); let block = Block::bordered() .title(title.centered()) .title_bottom(instructions.centered()) .border_set(border::THICK); let counter_text = Text::from(vec![Line::from(vec![ "Value: ".into(), app.display_counter().yellow(), ])]); Paragraph::new(counter_text).centered().block(block) } #[cfg(test)] mod tests { use super::*; use ratatui::{buffer::Buffer, layout::Rect, style::Style}; #[test] fn render_success() { let mut app = App::default(); assert!(app.init_context().is_ok()); let rect = Rect::new(0, 0, 50, 4); let mut buf = Buffer::empty(rect); let res = draw_layout(&app); res.render(rect, &mut buf); let mut expected = Buffer::with_lines(vec![ "┏━━━━━━━━━━━━━ Counter App Tutorial ━━━━━━━━━━━━━┓", "┃ 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(14, 0, 22, 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); } }