diff options
| author | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-02-27 20:37:19 +0000 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-02-27 20:41:05 +0000 |
| commit | 9bff235dca50603d6a745cc127ba8d36b4928e1b (patch) | |
| tree | b3b6c1b3fc131930149c3cd25859cf2f48a991ec /src | |
| parent | 7b912e839a7e7a30e88cb7876d62c9fc754aa489 (diff) | |
| download | equity-tracker-9bff235dca50603d6a745cc127ba8d36b4928e1b.tar.gz equity-tracker-9bff235dca50603d6a745cc127ba8d36b4928e1b.tar.bz2 equity-tracker-9bff235dca50603d6a745cc127ba8d36b4928e1b.zip | |
Split draw and Runtime into separate UI file
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/rust/main.rs | 82 | ||||
| -rw-r--r-- | src/main/rust/ui.rs | 85 |
2 files changed, 89 insertions, 78 deletions
diff --git a/src/main/rust/main.rs b/src/main/rust/main.rs index db401fe..043702c 100644 --- a/src/main/rust/main.rs +++ b/src/main/rust/main.rs @@ -1,18 +1,10 @@ mod app; pub mod store; +mod ui; use app::App; use miette::{IntoDiagnostic, Result}; -use ratatui::{ - Frame, Terminal, - backend::Backend, - buffer::Buffer, - layout::Rect, - style::Stylize, - symbols::border, - text::{Line, Text}, - widgets::{Block, Paragraph, Widget}, -}; +use ratatui::{Terminal, backend::Backend}; use ratatui::crossterm::{ event::{ @@ -52,9 +44,10 @@ fn main() -> Result<()> { Ok(()) } +#[cfg(not(tarpaulin_include))] fn run_app<B: Backend>(term: &mut Terminal<B>, app: &mut App) -> Result<()> { while !app.get_exit_status() { - term.draw(|frame| draw(frame, app)) + term.draw(|frame| ui::draw(frame, app)) .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "idk")) .into_diagnostic()?; @@ -70,11 +63,6 @@ fn run_app<B: Backend>(term: &mut Terminal<B>, app: &mut App) -> Result<()> { Ok(()) } -fn draw(frame: &mut Frame, app: &App) { - let run = Runtime { app }; - frame.render_widget(&run, frame.area()); -} - /// Process user input and run associated app functions fn handle_key_event(app: &mut App, key_event: KeyEvent) -> Result<()> { match key_event.code { @@ -86,73 +74,11 @@ fn handle_key_event(app: &mut App, key_event: KeyEvent) -> Result<()> { Ok(()) } -struct Runtime<'a> { - app: &'a App, -} - -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); - - let counter_text = Text::from(vec![Line::from(vec![ - "Value: ".into(), - self.app.display_counter().yellow(), - ])]); - - Paragraph::new(counter_text) - .centered() - .block(block) - .render(area, buf); - } -} - #[cfg(test)] mod tests { use super::*; use app::OverflowError; - use ratatui::style::Style; - - #[test] - fn render() { - 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)); - - runtime.render(buf.area, &mut buf); - - let mut expected = Buffer::with_lines(vec![ - "┏━━━━━━━━━━━━━ Counter App Tutorial ━━━━━━━━━━━━━┓", - "┃ 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(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); - } #[test] fn handle_key_event_inc_dec() { diff --git a/src/main/rust/ui.rs b/src/main/rust/ui.rs new file mode 100644 index 0000000..2f69cea --- /dev/null +++ b/src/main/rust/ui.rs @@ -0,0 +1,85 @@ +use crate::{app::App, ui}; +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 = ui::Runtime { app }; + frame.render_widget(&run, 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); + + let counter_text = Text::from(vec![Line::from(vec![ + "Value: ".into(), + self.app.display_counter().yellow(), + ])]); + + Paragraph::new(counter_text) + .centered() + .block(block) + .render(area, buf); + } +} + +#[cfg(test)] +mod tests { + + use super::*; + use ratatui::style::Style; + + #[test] + fn render() { + 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)); + + runtime.render(buf.area, &mut buf); + + let mut expected = Buffer::with_lines(vec![ + "┏━━━━━━━━━━━━━ Counter App Tutorial ━━━━━━━━━━━━━┓", + "┃ 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(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); + } +} |
