starry/md/
change_report.rs1use {
2 crate::*,
3 minimad::{
4 OwningTemplateExpander,
5 TextTemplate,
6 },
7 termimad::*,
8};
9
10static TEMPLATE: &str = r#"
11${cropped
12${kept-count} most significant changes:
13}
14|:-:|:-:|:-:|
15|**owner**|**name**|**last**|**trend**|**now**|**url** (ctrl-click to open)|
16|-:|:-|-:|:-:|-:|:-|
17${changes
18|${owner}|**${name}**|${last}|${trend}|**${now}|${url}|
19}
20|-|-|-|-|-|-|
21"#;
22
23pub struct ChangeReport<'c> {
24 changes: &'c [RepoChange],
25 max_rows: usize,
26}
27
28impl<'c> ChangeReport<'c> {
29 pub fn new(
30 changes: &'c [RepoChange],
31 max_rows: usize,
32 ) -> Self {
33 Self { changes, max_rows }
34 }
35 pub fn print(
36 &self,
37 skin: &MadSkin,
38 ) {
39 if self.changes.is_empty() {
40 println!("no change");
41 return;
42 }
43 let mut expander = OwningTemplateExpander::new();
44 expander
45 .set_default("")
46 .set("change-count", self.changes.len());
47 for change in self.changes.iter().take(self.max_rows) {
48 expander
49 .sub("changes")
50 .set("owner", &change.repo_id.owner)
51 .set("name", &change.repo_id.name)
52 .set(
53 "last",
54 change.old_stars.map_or("".to_string(), |s| s.to_string()),
55 )
56 .set_md("trend", change.trend_markdown())
58 .set("now", change.new_stars)
59 .set("url", change.url());
60 }
61 if self.changes.len() > self.max_rows {
62 expander.sub("cropped").set("kept-count", self.max_rows);
63 }
64 let template = TextTemplate::from(TEMPLATE);
65 let text = expander.expand(&template);
66 let (width, _) = terminal_size();
67 let fmt_text = FmtText::from_text(skin, text, Some(width as usize));
68 print!("{}", fmt_text);
69 }
70}