aboutsummaryrefslogtreecommitdiff
path: root/src/main/rust/ui.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/rust/ui.rs')
-rw-r--r--src/main/rust/ui.rs66
1 files changed, 27 insertions, 39 deletions
diff --git a/src/main/rust/ui.rs b/src/main/rust/ui.rs
index 5f2c603..daf00d4 100644
--- a/src/main/rust/ui.rs
+++ b/src/main/rust/ui.rs
@@ -1,68 +1,56 @@
use crate::app::App;
use ratatui::{
Frame,
- buffer::Buffer,
- layout::Rect,
style::Stylize,
symbols::border,
text::{Line, Text},
widgets::{Block, Paragraph, Widget},
};
-/// Explicitly declare lifetime as App is a reference;
-/// Runtime is only instantiated for render
-struct Runtime<'a> {
- app: &'a App,
-}
-
pub fn draw(frame: &mut Frame, app: &App) {
- let run = Runtime { app };
- frame.render_widget(&run, frame.area());
+ let rect = draw_layout(app);
+ frame.render_widget(rect, frame.area());
}
-impl<'a> Widget for &Runtime<'a> {
- fn render(self, area: Rect, buf: &mut Buffer) {
- let title = Line::from(" Counter App Tutorial ".bold());
- let instructions = Line::from(vec![
- " Decrement ".into(),
- "<Left>".blue().bold(),
- " Increment ".into(),
- "<Right>".blue().bold(),
- " Quit ".into(),
- "<Q> ".blue().bold(),
- ]);
- let block = Block::bordered()
- .title(title.centered())
- .title_bottom(instructions.centered())
- .border_set(border::THICK);
+fn draw_layout(app: &App) -> impl Widget {
+ let title = Line::from(" Counter App Tutorial ".bold());
+ let instructions = Line::from(vec![
+ " Decrement ".into(),
+ "<Left>".blue().bold(),
+ " Increment ".into(),
+ "<Right>".blue().bold(),
+ " Quit ".into(),
+ "<Q> ".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(),
- self.app.display_counter().yellow(),
- ])]);
+ let counter_text = Text::from(vec![Line::from(vec![
+ "Value: ".into(),
+ app.display_counter().yellow(),
+ ])]);
- Paragraph::new(counter_text)
- .centered()
- .block(block)
- .render(area, buf);
- }
+ Paragraph::new(counter_text).centered().block(block)
}
#[cfg(test)]
mod tests {
use super::*;
- use ratatui::style::Style;
+ use ratatui::{buffer::Buffer, layout::Rect, style::Style};
#[test]
- fn render() {
+ fn render_success() {
let mut app = App::default();
assert!(app.init_context().is_ok());
- let runtime = Runtime { app: &app };
- let mut buf = Buffer::empty(Rect::new(0, 0, 50, 4));
+ let rect = Rect::new(0, 0, 50, 4);
+ let mut buf = Buffer::empty(rect);
- runtime.render(buf.area, &mut buf);
+ let res = draw_layout(&app);
+ res.render(rect, &mut buf);
let mut expected = Buffer::with_lines(vec![
"┏━━━━━━━━━━━━━ Counter App Tutorial ━━━━━━━━━━━━━┓",