display number of outdated dependencies in badge

This commit is contained in:
Sam Reis 2018-02-14 12:49:46 +11:00
parent 1c4f933887
commit 12e4d7df51
4 changed files with 35 additions and 4 deletions

View file

@ -126,7 +126,7 @@ impl Badge {
})
.next()
.unwrap_or(0.0);
(width + ((text.len() as f32 - 1f32) * 1.5)).ceil() as u32
(width + ((text.len() as f32 - 1f32) * 1.3)).ceil() as u32
}
}

View file

@ -63,6 +63,12 @@ impl AnalyzeDependenciesOutcome {
pub fn any_outdated(&self) -> bool {
self.crates.iter().any(|&(_, ref deps)| deps.any_outdated())
}
pub fn outdated_ratio(&self) -> (usize, usize) {
self.crates.iter().fold((0, 0), |(outdated, total), &(_, ref deps)| {
(outdated + deps.count_outdated(), total + deps.count_total())
})
}
}
impl Engine {

View file

@ -127,6 +127,23 @@ impl AnalyzedDependencies {
AnalyzedDependencies { main, dev, build }
}
pub fn count_total(&self) -> usize {
self.main.len() + self.dev.len() + self.build.len()
}
pub fn count_outdated(&self) -> usize {
let main_outdated = self.main.iter()
.filter(|&(_, dep)| dep.is_outdated())
.count();
let dev_outdated = self.dev.iter()
.filter(|&(_, dep)| dep.is_outdated())
.count();
let build_outdated = self.build.iter()
.filter(|&(_, dep)| dep.is_outdated())
.count();
main_outdated + dev_outdated + build_outdated
}
pub fn any_outdated(&self) -> bool {
let main_any_outdated = self.main.iter()
.any(|(_, dep)| dep.is_outdated());

View file

@ -7,16 +7,24 @@ use ::engine::AnalyzeDependenciesOutcome;
pub fn badge(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Badge {
let opts = match analysis_outcome {
Some(outcome) => {
if outcome.any_outdated() {
let (outdated, total) = outcome.outdated_ratio();
if outdated > 0 {
BadgeOptions {
subject: "dependencies".into(),
status: "outdated".into(),
status: format!("{} of {} outdated", outdated, total),
color: "#dfb317".into()
}
} else if total > 0 {
BadgeOptions {
subject: "dependencies".into(),
status: "up to date".into(),
color: "#4c1".into()
}
} else {
BadgeOptions {
subject: "dependencies".into(),
status: "up to date".into(),
status: "none".into(),
color: "#4c1".into()
}
}