forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

timeline.rs114 lines, 3.9 KiBHistoryRawDownload
1//! The history strip: one bar per recent commit, oldest on the left and newest on the right. Each
2//! bar is a link to its commit. Heights are log-scaled against the window's largest commit. A
3//! commit whose latest CI run failed is red, and the newest commit is live while its run goes.
4
5use forge_core::ci::RunState;
6use topcoat::Result;
7use topcoat::context::Cx;
8use topcoat::view::{View, component, view};
9
10use super::{repo_href, short_oid};
11use crate::i18n::Lang;
12use crate::i18n::timeline::{self as tl, K};
13
14/// The shortest bar, as a share of the strip, so a tiny commit still shows.
15pub const MIN_BAR_PERCENT: u32 = 6;
16
17/// One commit on the strip. `ci` is the state of the commit's latest CI run, if it has one.
18pub struct StripBar {
19 pub oid: String,
20 pub lines: u64,
21 pub ci: Option<RunState>,
22}
23
24/// A bar's height in percent of the strip. Log-scaled against `max`, the window's largest
25/// commit, so one huge commit does not flatten the rest. Never below [`MIN_BAR_PERCENT`].
26pub fn bar_percent(lines: u64, max: u64) -> u32 {
27 if max == 0 {
28 return MIN_BAR_PERCENT;
29 }
30 let share = (lines as f64 + 1.0).ln() / (max as f64 + 1.0).ln();
31 let pct = (share * 100.0).round() as u32;
32 pct.clamp(MIN_BAR_PERCENT, 100)
33}
34
35/// The tone class of a bar, the colour of its commit's latest run as the CI pages draw it: live for
36/// the newest commit while its run goes, ok when it passed, bad when it failed, neutral with no run.
37fn tone(ci: Option<RunState>, newest: bool) -> &'static str {
38 match ci {
39 Some(RunState::Running | RunState::Queued) if newest => "hs-live",
40 Some(RunState::Success) => "hs-ok",
41 Some(RunState::Failure | RunState::Error) => "hs-bad",
42 _ => "hs-neutral",
43 }
44}
45
46/// One bar as the view draws it: its classes, link and accessible name.
47struct BarView {
48 class: String,
49 href: String,
50 label: String,
51 oid: String,
52}
53
54/// The strip for `bars`, given oldest first. Fewer than two commits draw nothing. The label
55/// counts the bars drawn.
56#[component]
57pub async fn history_strip(
58 cx: &Cx,
59 lang: Lang,
60 owner: String,
61 repo: String,
62 bars: Vec<StripBar>,
63) -> Result<impl View> {
64 let show = bars.len() >= 2;
65 let max = bars.iter().map(|bar| bar.lines).max().unwrap_or(0);
66 let newest = bars.len().saturating_sub(1);
67 let heading = tl::last_commits(lang, bars.len());
68 let aria = tl::t(lang, K::Timeline);
69 let items: Vec<BarView> = bars
70 .into_iter()
71 .enumerate()
72 .map(|(i, bar)| BarView {
73 class: format!(
74 "hs-bar {} hs-h-{}",
75 tone(bar.ci, i == newest),
76 bar_percent(bar.lines, max)
77 ),
78 href: repo_href(&owner, &repo, "commit", &bar.oid),
79 label: format!(
80 "{} \u{b7} {}",
81 short_oid(&bar.oid),
82 tl::lines_changed(lang, bar.lines)
83 ),
84 oid: bar.oid,
85 })
86 .collect();
87 Ok(view! {
88 if show {
89 <div class="history-strip">
90 <p class="hint history-label">(heading.as_str())</p>
91 <div class="history-bars" role="group" aria-label=(aria)>
92 for item in items.iter() {
93 <a class=(item.class.as_str()) href=(item.href.as_str()) aria-label=(item.label.as_str()) title=(item.label.as_str()) data-oid=(item.oid.as_str()) data-arrive=(item.oid.as_str())></a>
94 }
95 </div>
96 </div>
97 }
98 })
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn bar_heights_are_log_scaled_with_a_floor() {
107 assert_eq!(bar_percent(0, 0), MIN_BAR_PERCENT);
108 assert_eq!(bar_percent(0, 1000), MIN_BAR_PERCENT);
109 assert_eq!(bar_percent(1000, 1000), 100);
110 // ln 32 / ln 1000 is about half.
111 assert_eq!(bar_percent(31, 999), 50);
112 assert!(bar_percent(10, 1000) > bar_percent(2, 1000));
113 }
114}