From 41218695e65b40bac8419dbd39e2d7bd025cacca Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 29 Jan 2022 14:12:01 +0000 Subject: [PATCH] Add for-the-badge style and docs (#139) --- README.md | 7 +++++++ libs/badge/badge.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c06fd29..f653d62 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,15 @@ To analyze the state of your dependencies you can use the following URLs: - for projects on crates.io: `https://deps.rs/crate/` - for projects on Github, Gitlab, Bitbucket, SourceHut, or Codeberg: `https://deps.rs/repo///` (where `` is either `github`, `gitlab`, `bitbucket`, `sourcehut`, or `codeberg`) +## Badges + On the analysis page, you will also find the markdown code to include a fancy badge in your project README so visitors (and you) can see at a glance if your dependencies are still up to date! +Badges have a few style options, specified with query parameters, that match the styles from `shields.io`: +- `?style=flat` (default) +- `?style=flat-square` +- `?style=for-the-badge` + ## Contributing We are always looking for help from the community! Feel like a feature is missing? Found a bug? [Open an issue](https://github.com/deps-rs/deps.rs/issues/new)! diff --git a/libs/badge/badge.rs b/libs/badge/badge.rs index 8e5d19d..c0b89fa 100644 --- a/libs/badge/badge.rs +++ b/libs/badge/badge.rs @@ -22,6 +22,7 @@ const SCALE: Scale = Scale { pub enum BadgeStyle { Flat, FlatSquare, + ForTheBadge, } impl Default for BadgeStyle { @@ -95,6 +96,7 @@ impl Badge { match self.options.style { BadgeStyle::Flat => self.to_flat_svg(), BadgeStyle::FlatSquare => self.to_flat_square_svg(), + BadgeStyle::ForTheBadge => self.to_for_the_badge_svg(), } } @@ -127,7 +129,7 @@ impl Badge { - + {subject} {subject} {status} @@ -152,8 +154,8 @@ impl Badge { let status = &self.options.status; let svg = format!( - r###" - + r###" + @@ -169,6 +171,36 @@ impl Badge { svg } + pub fn to_for_the_badge_svg(&self) -> String { + let left_width = self.calculate_width(&self.options.subject) + 38; + let right_width = self.calculate_width(&self.options.status) + 38; + let total_width = left_width + right_width; + + let left_center = left_width / 2; + let right_center = left_width + (right_width / 2); + + let color = &self.options.color; + let subject = self.options.subject.to_uppercase(); + let status = self.options.status.to_uppercase(); + + let svg = format!( + r###" + + + + + + + {subject} + {status} + + +"###, + ); + + svg + } + fn calculate_width(&self, text: &str) -> u32 { let glyphs: Vec = DATA.font.layout(text, DATA.scale, DATA.offset).collect(); @@ -207,9 +239,10 @@ mod tests { use std::io::Write; let mut file = File::create("test.svg").unwrap(); let options = BadgeOptions { - subject: "build".to_owned(), - status: "passing".to_owned(), - ..BadgeOptions::default() + subject: "latest".to_owned(), + status: "v4.0.0-beta.21".to_owned(), + style: BadgeStyle::ForTheBadge, + color: "#fe7d37".to_owned(), }; let badge = Badge::new(options); file.write_all(badge.to_svg().as_bytes()).unwrap();