1#![windows_subsystem = "windows"]
8use taut::{Align, Chart, Color, Font, Length, Markdown, Paper, Path, Pen, PrintJob, PrintOutcome, Role, Series, Ui, Window, geom::rect};
9
10const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
12const SOLD: [f32; 12] = [412.0, 388.0, 455.0, 501.0, 532.0, 610.0, 588.0, 571.0, 640.0, 702.0, 765.0, 820.0];
13const BACK: [f32; 12] = [21.0, 18.0, 25.0, 22.0, 30.0, 28.0, 35.0, 31.0, 27.0, 33.0, 38.0, 41.0];
14
15#[derive(Default)]
17pub struct Report {
18 saved: Option<String>,
19 printing: Option<PrintJob>,
20 status: String,
21}
22
23pub fn words() -> String {
25 let (sold, back): (f32, f32) = (SOLD.iter().sum(), BACK.iter().sum());
26 let best = (0..12).max_by(|a, b| SOLD[*a].total_cmp(&SOLD[*b])).unwrap_or(0);
27 let mut text = format!(
28 "# The year in sales\n\n{sold:.0} things were sold over the year, and {back:.0} came back: {:.1} in every hundred. \
29 The best month was **{}**, with {:.0}; the year ended higher than it began in every quarter.\n\n## Each quarter\n\n| Quarter | Sold | Back |\n| --- | ---: | ---: |\n",
30 back / sold * 100.0,
31 MONTHS[best],
32 SOLD[best]
33 );
34 for quarter in 0..4 {
35 let months = quarter * 3..quarter * 3 + 3;
36 text += &format!("| {} | {:.0} | {:.0} |\n", ["First", "Second", "Third", "Fourth"][quarter], SOLD[months.clone()].iter().sum::<f32>(), BACK[months].iter().sum::<f32>());
37 }
38 text += "\n## What is next\n\n- Keep what sold best in the autumn.\n- Look into what came back in November and December.\n- Write the next report in April.\n";
39 text
40}
41
42pub fn paper() -> Paper {
45 let mut paper = Paper::new();
46 paper.title = "The year in sales".into();
47 paper.page(Paper::A4);
48 let (black, dim) = (Color::rgb(0x1a202c), Color::rgb(0x4a5568));
49 paper.begin_heading(1);
52 paper.text("The year in sales", 72.0, 72.0, Font::new(28.0).bold(), black);
53 paper.end();
54 paper.text("Things sold and things that came back, by month", 72.0, 116.0, Font::new(14.0), dim);
55 paper.begin(Role::Picture);
56 paper.describe("A chart by month: sales rose from 412 in January to 820 in December; returns from 21 to 41.");
57 chart(&mut paper, rect(72.0, 170.0, Paper::A4.w - 144.0, 420.0));
58 paper.end();
59 paper.markdown(&Markdown::parse(&words()), Paper::A4, 72.0);
60 let pages = paper.pages();
62 for page in 0..pages {
63 paper.set_page(page);
64 let number = format!("{} of {pages}", page + 1);
65 let wide = paper.measure(&number, Font::new(11.0));
66 paper.begin_decoration();
67 paper.text(&number, (Paper::A4.w - wide) / 2.0, Paper::A4.h - 48.0, Font::new(11.0), dim);
68 paper.end();
69 }
70 paper
71}
72
73fn chart(paper: &mut Paper, r: taut::Rect) {
77 let (sold, back, line, dim) = (Color::rgb(0x2b6cb0), Color::rgb(0xdd6b20), Color::rgb(0xe2e8f0), Color::rgb(0x4a5568));
78 let small = Font::new(12.0);
79 let plot = rect(r.x + 48.0, r.y, r.w - 96.0, r.h - 64.0);
80 let (most, most_back) = (1000.0, 50.0);
81 for step in 0..=5 {
82 let y = plot.bottom() - plot.h * step as f32 / 5.0;
83 paper.rect(rect(plot.x, y, plot.w, 1.0), line);
84 let label = format!("{}", step * 200);
85 let wide = paper.measure(&label, small);
86 paper.text(&label, plot.x - 8.0 - wide, y - 8.0, small, dim);
87 paper.text(&format!("{}", step * 10), plot.right() + 8.0, y - 8.0, small, back);
88 }
89 let slot = plot.w / 12.0;
90 let mut path = Path::new();
91 for (month, name) in MONTHS.iter().enumerate() {
92 let x = plot.x + slot * month as f32;
93 let high = plot.h * SOLD[month] / most;
94 paper.rounded(rect(x + slot * 0.2, plot.bottom() - high, slot * 0.6, high), 3.0, sold);
95 let wide = paper.measure(name, small);
96 paper.text(name, x + (slot - wide) / 2.0, plot.bottom() + 8.0, small, dim);
97 let y = plot.bottom() - plot.h * BACK[month] / most_back;
98 match month {
99 0 => _ = path.move_to(x + slot / 2.0, y),
100 _ => _ = path.line_to(x + slot / 2.0, y),
101 }
102 }
103 paper.outline(&path, Pen::new(3.0), back);
104 for (month, came) in BACK.iter().enumerate() {
105 let mut dot = Path::new();
106 dot.circle(plot.x + slot * (month as f32 + 0.5), plot.bottom() - plot.h * came / most_back, 4.5);
107 paper.shape(&dot, back);
108 }
109 let key = plot.bottom() + 40.0;
110 paper.rounded(rect(plot.x, key, 14.0, 14.0), 3.0, sold);
111 paper.text("Sold (left)", plot.x + 22.0, key - 2.0, small, dim);
112 paper.rounded(rect(plot.x + 140.0, key + 5.0, 14.0, 4.0), 2.0, back);
113 paper.text("Came back (right)", plot.x + 162.0, key - 2.0, small, dim);
114}
115
116pub fn describe(report: &mut Report, ui: &mut Ui) {
119 let t = ui.theme;
120 ui.column().pad(16.0).gap(12.0).grow().open();
121 ui.text("The year in sales", t.font.bold(), t.text);
122 let rows = [Series::bars("Sold", &SOLD), Series::line("Came back", &BACK).color(t.red)];
123 ui.chart(&Chart::of(&rows).categories(&MONTHS), 560.0, 260.0);
124 ui.row().gap(8.0).align(Align::Start, Align::Center).open();
125 if ui.primary_button("Save as PDF").clicked {
126 let file = std::env::temp_dir().join("report.pdf");
127 report.status = match std::fs::write(&file, paper().to_pdf()) {
128 Ok(()) => format!("Saved as {}", file.display()),
129 Err(error) => format!("Not saved: {error}"),
130 };
131 report.saved = Some(file.display().to_string());
132 }
133 if ui.button("Print").clicked && report.printing.is_none() {
134 report.printing = Some(taut::print(paper(), "The year in sales"));
135 }
136 if let Some(done) = report.printing.as_ref().and_then(PrintJob::outcome) {
137 report.printing = None;
138 report.status = match done {
139 PrintOutcome::Sent => "Sent to the printer".into(),
140 PrintOutcome::Closed => String::new(),
141 _ => "There is no printing here".into(),
142 };
143 }
144 ui.el().w(Length::Grow(1.0)).text(&report.status, t.font, t.text_dim).clip().show();
145 ui.close();
146 ui.close();
147}
148
149fn main() {
150 if let Some(name) = std::env::args().nth(1) {
152 std::fs::write(&name, paper().to_pdf()).expect("the file is written");
153 return;
154 }
155 let mut report = Report::default();
156 taut::run(Window { title: "Report", width: 600.0, height: 400.0, ..Default::default() }, move |ui| describe(&mut report, ui));
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162 use taut::testing::Trial;
163
164 #[test]
165 fn the_report_is_a_chart_and_a_page_of_words() {
166 let paper = paper();
167 assert!(paper.pages() >= 2, "{} pages", paper.pages());
168 let file = paper.to_pdf();
169 assert!(file.starts_with(b"%PDF-") && file.len() > 2_000);
170 }
171
172 #[test]
173 fn the_report_is_saved_from_its_window() {
174 let mut trial = Trial::new(Report::default(), describe);
175 trial.resize(600.0, 400.0, 1.0);
176 trial.press(taut::Role::Button, "Save as PDF");
177 let saved = trial.app.saved.clone().expect("saved");
178 assert!(std::fs::read(&saved).expect("the file is there").starts_with(b"%PDF-"));
179 assert!(trial.app.status.starts_with("Saved as"));
180 }
181}