~neon/activity-graph

ref: 61f5fd06626984d2be93da26b1ddfd513511d5da activity-graph/src/render.rs -rw-r--r-- 9.7 KiB
61f5fd06Jens Pitkanen Improve css 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Contains the functionality to render the visualizations out of
//! dated commit data.
use chrono::naive::NaiveDate;
use chrono::{DateTime, Datelike, Utc};

use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Component, PathBuf};

use crate::{log, Day, ExternalResources, ProjectMetadata, Year};

static HTML_HEAD: &str = include_str!("head.html");
static CSS: &str = include_str!("activity-graph.css");
static WEEKS: usize = 53;

pub fn gather_years(mut commit_dates: Vec<(DateTime<Utc>, ProjectMetadata)>) -> Vec<Year> {
    if commit_dates.is_empty() {
        return Vec::new();
    }

    commit_dates.sort_by(|(a, _), (b, _)| a.cmp(b));

    let get_year = |date: DateTime<Utc>| date.date().year();
    let first_year = get_year(commit_dates[0].0);
    let last_year = get_year(commit_dates[commit_dates.len() - 1].0);

    // Years is a vec containing vecs of years, which consist
    // of weekday-major grids of days: eg. the first row
    // represents all of the mondays in the year, in order.
    let mut years = Vec::with_capacity((last_year - first_year + 1) as usize);
    for year in first_year..=last_year {
        years.push(Year {
            year,
            days: vec![Day::default(); WEEKS * 7],
        });
    }

    let mut commit_dates = commit_dates.into_iter().peekable();
    let mut counted_commits = 0;
    for year in first_year..=last_year {
        // Loop through the years

        let weekday_offset = NaiveDate::from_ymd(year, 1, 1)
            .weekday()
            .num_days_from_monday() as usize;
        let last_day =
            weekday_offset + NaiveDate::from_ymd(year + 1, 1, 1).pred().ordinal() as usize;
        let last_week = (last_day - (last_day % 7)) / 7;

        let (before, after) = years.split_at_mut((year + 1 - first_year) as usize);
        let (before, current) = before.split_at_mut(before.len() - 1);
        let days = &mut current[0].days;
        let mut last_year_days = if year > first_year {
            Some(&mut before[before.len() - 1].days)
        } else {
            None
        };
        let mut next_year_days = if year < last_year {
            Some(&mut after[0].days)
        } else {
            None
        };
        while let Some((date, _)) = commit_dates.peek() {
            // Loop through the days until the commit is from
            // next year or commits run out

            if date.year() != year {
                break;
            }

            let ordinal_with_offset = (date.ordinal0()) as usize + weekday_offset;
            let weekday_index = ordinal_with_offset % 7;
            let week_index = ordinal_with_offset / 7;
            if week_index < WEEKS {
                let day = &mut days[weekday_index * WEEKS + week_index];
                // This branch should always be taken because of the peek()
                if let Some((_, metadata)) = commit_dates.next() {
                    // Add the commit to the next/last year as well,
                    // to achieve consistency in the duplicated days
                    if week_index == last_week {
                        if let Some(days) = &mut next_year_days {
                            let next_year_today = &mut days[weekday_index * WEEKS];
                            next_year_today.commits.push(metadata.clone());
                        }
                    }
                    if week_index == 0 {
                        if let Some(days) = &mut last_year_days {
                            let last_year_today = &mut days[weekday_index * WEEKS + WEEKS - 1];
                            last_year_today.commits.push(metadata.clone());
                        }
                    }
                    day.commits.push(metadata);
                    counted_commits += 1;
                }
            }
        }

        // Set the first and last days as filler
        let first_day = weekday_offset;
        for ordinal_with_offset in (0..first_day).chain(last_day..days.len()) {
            let weekday_index = ordinal_with_offset % 7;
            let week_index = ordinal_with_offset / 7;
            days[weekday_index * WEEKS + week_index].filler = true;
        }

        log::verbose_println(
            &format!(
                "prepared year {} for rendering, {} commits processed so far",
                year, counted_commits
            ),
            true,
        );
    }

    let year_range = if first_year == last_year {
        format!(" {}", first_year)
    } else {
        format!("s {}-{}", first_year, last_year)
    };
    log::verbose_println(
        &format!(
            "prepared year{} for rendering, {} commits processed",
            year_range, counted_commits
        ),
        false,
    );

    years
}

/// Renders a HTML visualization of the commits based on the
/// arguments.
pub fn html(
    ext: &ExternalResources,
    html_path: &PathBuf,
    css_path: Option<&PathBuf>,
    years: &[Year],
) -> String {
    // Prepare the html scaffolding around the tables
    let external_head = read_optional_file(&ext.external_head).unwrap_or_else(String::new);
    let external_header = read_optional_file(&ext.external_header).unwrap_or_else(String::new);
    let external_footer = read_optional_file(&ext.external_footer).unwrap_or_else(String::new);
    let external_css = read_optional_file(&ext.external_css).unwrap_or_else(String::new);

    let mut style = None;
    if let (Some(base), Some(css_path)) = (html_path.parent(), &css_path) {
        if let Some(relative_path) = pathdiff::diff_paths(&css_path, base) {
            // Add the <link> element instead of <style> if using external css
            let path = create_web_path(relative_path);
            style = Some(format!("<link href=\"{}\" rel=\"stylesheet\">", path));
        }
    }
    if style.is_none() {
        style = Some(format!("<style>\n{}\n{}</style>", CSS, external_css));
    }
    let style = style.unwrap();

    let head = format!(
        "<!DOCTYPE html>\n<html>\n<head>\n{}\n{}\n{}\n</head>\n<body>\n{}\n",
        HTML_HEAD, style, external_head, external_header
    );
    let tail = format!("{}</body></html>", external_footer);

    // Render the tables
    let mut result = String::with_capacity(1024);
    log::verbose_println("rendering html...", true);
    result += &head;
    for year in years.iter().rev() {
        let max_count = get_max_count(year);
        result += &format!(
            "<div class=\"activity-table\" title=\"A table containing the commit visualization for {}.\">\n<div class=\"activity-header-year\" colspan=\"{}\">{}</div>\n <div>\n",
            year.year, WEEKS, year.year
        );
        for day in 0..7 {
            result += "  <div class=\"blob-row\">";
            for week in 0..WEEKS {
                let metadata = &year.days[day * WEEKS + week];
                let commit_count = metadata.commits.len();
                let shade = get_shade_class(commit_count, max_count);
                let tooltip = if commit_count == 0 {
                    String::from("No commits")
                } else {
                    format!("{} commits", commit_count)
                };
                let filler = if metadata.filler { "filler-day" } else { "" };
                result += &format!(
                    "<span class=\"blob lvl{} {}\" title=\"{}\"></span>",
                    shade, filler, tooltip
                );
            }
            result += " </div>\n";
        }
        result += " </div>\n</div>\n";
    }
    result += &tail;
    log::verbose_println("rendered html", false);
    result
}

pub fn css(ext: &ExternalResources) -> String {
    let external_css = read_optional_file(&ext.external_css).unwrap_or_else(String::new);
    format!("{}\n{}", CSS, external_css)
}

/// Renders an ASCII visualization of the commits.
pub fn ascii(years: &[Year]) -> String {
    let mut result = String::with_capacity(512);
    log::verbose_println("rendering ascii visualization...", true);
    for year in years.iter().rev() {
        let max_count = get_max_count(year);
        result.push('\n');
        for day in 0..7 {
            for week in 0..WEEKS {
                let metadata = &year.days[day * WEEKS + week];
                if metadata.filler {
                    result.push(' ');
                } else {
                    let shade = metadata.commits.len() as f32 / max_count as f32;
                    result.push(get_shaded_char(shade));
                }
            }
            result.push('\n');
        }
    }
    log::verbose_println("rendered ascii visualization", false);
    result
}

fn create_web_path(path: PathBuf) -> String {
    path.components()
        .filter_map(|component| match component {
            Component::Normal(s) => s.to_str(),
            Component::CurDir => Some("."),
            Component::ParentDir => Some(".."),
            _ => None,
        })
        .fold(String::new(), |mut a, b| {
            if a.len() > 0 {
                a += "/";
            }
            a += b;
            a
        })
}

fn read_optional_file(path: &Option<PathBuf>) -> Option<String> {
    let path = path.as_ref()?;
    let file = File::open(path).ok()?;
    let mut reader = BufReader::new(file);
    let mut result = Vec::new();
    reader.read_to_end(&mut result).ok()?;
    String::from_utf8(result).ok()
}

fn get_max_count(year: &Year) -> usize {
    year.days
        .iter()
        .map(|metadata| metadata.commits.len())
        .max()
        .unwrap_or(0)
        .max(1)
}

fn get_shade_class(commits: usize, max_count: usize) -> usize {
    let norm = commits as f32 / max_count as f32;
    match norm {
        x if x == 0.0 => 0,
        x if x < 0.25 => 1,
        x if x < 0.5 => 2,
        x if x < 0.75 => 3,
        _ => 4,
    }
}

fn get_shaded_char(shade: f32) -> char {
    match shade {
        x if x > 0.5 => '\u{2593}',
        x if x > 0.0 => '\u{2592}',
        _ => '\u{2591}',
    }
}