Skip to content

Commit

Permalink
Minor code cleanup - extracting render funcs in weekly.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmadrid committed Jan 5, 2023
1 parent f138c2c commit 8816724
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/bin/active.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use printpdf::*;
use weekly::{
save_one_page_document, ToPdfLine, Attributes, GridDescription, Instructions, NumericUnit,
TGrid, Unit, WRect,
save_one_page_document, Attributes, GridDescription, Instructions, NumericUnit, TGrid,
ToPdfLine, Unit, WRect,
};

struct ActiveDescription {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cornell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, ToPdfLine, Attributes, Colors, GridDescription, Instructions, TGrid,
save_one_page_document, Attributes, Colors, GridDescription, Instructions, TGrid, ToPdfLine,
Unit, WLine, WRect,
};

Expand Down
4 changes: 2 additions & 2 deletions src/bin/daily.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use printpdf::{Color, PdfDocumentReference};
use std::borrow::Cow;
use std::path::PathBuf;
use weekly::{
save_one_page_document, sizes, ToPdfLine, Attributes, Colors, Datetools, LineModifiers,
NumericUnit, Result, TGrid, Unit, WRect,
save_one_page_document, sizes, Attributes, Colors, Datetools, LineModifiers, NumericUnit,
Result, TGrid, ToPdfLine, Unit, WRect,
};
use weekly::{GridDescription, Instructions};

Expand Down
2 changes: 1 addition & 1 deletion src/bin/projects.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, ToPdfLine, Colors, Instructions, LineModifiers, NumericUnit, Unit,
save_one_page_document, Colors, Instructions, LineModifiers, NumericUnit, ToPdfLine, Unit,
WLine, WRect,
};

Expand Down
2 changes: 1 addition & 1 deletion src/bin/remtest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, ToPdfLine, Colors, Instructions, LineModifiers, NumericUnit, WRect,
save_one_page_document, Colors, Instructions, LineModifiers, NumericUnit, ToPdfLine, WRect,
};

const REMARKABLE_WIDTH: f64 = 157.2;
Expand Down
84 changes: 56 additions & 28 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use argh::FromArgs;
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, sizes, ToPdfLine, Attributes, Circle, Colors, FontProxy,
GridDescription, Instructions, LineModifiers, NumericUnit, Result, TGrid, Unit, WLine, WRect,
save_one_page_document, sizes, Attributes, Circle, Colors, FontProxy, GridDescription,
Instructions, LineModifiers, NumericUnit, Result, TGrid, ToPdfLine, Unit, WLine, WRect,
};

const GOLDEN_RATIO: f64 = 1.618033988749894;
Expand Down Expand Up @@ -150,9 +150,9 @@ fn render_left_circle(rect: &WRect, instructions: &mut Instructions) {
instructions.push_shape(circle);
}

fn render_days(rect: &WRect) -> Result<Instructions> {
fn render_days(rect: &WRect, instructions: &mut Instructions) -> Result<()> {
let day_width = rect.width() / DAY_ABBREVS.len() as f64;
let mut instructions = Instructions::default();
// let mut instructions = Instructions::default();

let day_rect = rect.resize(day_width, rect.height());
for (i, abbrev) in DAY_ABBREVS.iter().enumerate() {
Expand Down Expand Up @@ -184,7 +184,7 @@ fn render_days(rect: &WRect) -> Result<Instructions> {
)?);
}

Ok(instructions)
Ok(())
}

fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instructions> {
Expand All @@ -208,21 +208,47 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct
let top_text_offset = 5.0.mm();

let priorities_rect = print_rect.resize(grid_x * 2.0, top_height);
render_priorities(&priorities_rect, top_text_offset, &mut instructions)?;

let tracker_rect = priorities_rect.move_by(grid_x * 2.0, Unit::zero());
render_tracker(&tracker_rect, top_text_offset, &mut instructions)?;

let weekend_rect = tracker_rect
.move_by(grid_x * 2.0, Unit::zero())
.resize(grid_x, priorities_rect.height());
render_weekend(&weekend_rect, top_text_offset, &mut instructions)?;

let calendar_rect = print_rect
.resize(print_rect.width(), bottom_height)
.move_by(Unit::zero(), -top_height);
render_days(&calendar_rect, &mut instructions)?;

Ok(instructions)
}

fn render_weekend(
weekend_rect: &WRect,
top_text_offset: Unit,
instructions: &mut Instructions,
) -> Result<()> {
instructions.append(render_lines(
&priorities_rect,
"Weekly Priorities",
weekend_rect,
"Weekend Plans",
8,
top_text_offset,
|rect, row, instructions| {
if row > 0 {
render_left_circle(rect, instructions)
}
},
|_, _, _| {},
)?);

let tracker_rect = priorities_rect.move_by(grid_x * 2.0, Unit::zero());
Ok(())
}

fn render_tracker(
tracker_rect: &WRect,
top_text_offset: Unit,
instructions: &mut Instructions,
) -> Result<()> {
instructions.append(render_lines(
&tracker_rect,
tracker_rect,
"Habit Tracker",
8,
top_text_offset,
Expand Down Expand Up @@ -271,24 +297,26 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct
}
},
)?);
Ok(())
}

let weekend_rect = tracker_rect
.move_by(grid_x * 2.0, Unit::zero())
.resize(grid_x, priorities_rect.height());
fn render_priorities(
priorities_rect: &WRect,
top_text_offset: Unit,
instructions: &mut Instructions,
) -> Result<()> {
instructions.append(render_lines(
&weekend_rect,
"Weekend Plans",
priorities_rect,
"Weekly Priorities",
8,
top_text_offset,
|_, _, _| {},
|rect, row, instructions| {
if row > 0 {
render_left_circle(rect, instructions)
}
},
)?);

let calendar_rect = print_rect
.resize(print_rect.width(), bottom_height)
.move_by(Unit::zero(), -top_height);
instructions.append(render_days(&calendar_rect)?);

Ok(instructions)
Ok(())
}

fn render_dotted(_: &PdfDocumentReference, dotted_rect: &WRect) -> Result<Instructions> {
Expand Down Expand Up @@ -316,7 +344,7 @@ fn render_dotted(_: &PdfDocumentReference, dotted_rect: &WRect) -> Result<Instru
let mut y = dotted_rect.top() - grid_spacing;

while y >= dotted_rect.bottom_q1() + grid_spacing {
instructions.push_shape(
instructions.push_shape(
base_circle
.move_to(x, y)
.to_pdf_line()
Expand Down
2 changes: 1 addition & 1 deletion src/pdfutils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::units::Unit;
use crate::{ToPdfLine, Result, WRect};
use crate::{Result, ToPdfLine, WRect};
use printpdf::*;
use std::collections::HashMap;
use std::fs::File;
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/circle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ToPdfLine, NumericUnit, Unit};
use crate::{NumericUnit, ToPdfLine, Unit};
use printpdf::Line;

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/tgrid/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::tgrid::renderparams::RenderParams;
use crate::{ToPdfLine, Colors, Instructions, NumericUnit, Unit, WLine, WRect};
use crate::{Colors, Instructions, NumericUnit, ToPdfLine, Unit, WLine, WRect};
use description::GridDescription;

pub mod description;
Expand Down

0 comments on commit 8816724

Please sign in to comment.