From 12e4d7df51579dc55f4794e39f5d07e9a7516237 Mon Sep 17 00:00:00 2001 From: Sam Reis Date: Wed, 14 Feb 2018 12:49:46 +1100 Subject: [PATCH] display number of outdated dependencies in badge --- libs/badge/badge.rs | 2 +- src/engine/mod.rs | 6 ++++++ src/models/crates.rs | 17 +++++++++++++++++ src/server/views/badge.rs | 14 +++++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libs/badge/badge.rs b/libs/badge/badge.rs index 0194ae8..1fc1db1 100644 --- a/libs/badge/badge.rs +++ b/libs/badge/badge.rs @@ -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 } } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index ca33033..0e78039 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -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 { diff --git a/src/models/crates.rs b/src/models/crates.rs index e75706f..d6320af 100644 --- a/src/models/crates.rs +++ b/src/models/crates.rs @@ -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()); diff --git a/src/server/views/badge.rs b/src/server/views/badge.rs index 7ffc6ec..fca3a48 100644 --- a/src/server/views/badge.rs +++ b/src/server/views/badge.rs @@ -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() } }