diff --git a/README.md b/README.md index 845c691..be63b57 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,7 @@ Thank you ❤️ - [Norbert Melzer](https://github.com/NobbZ). - [Patrick Stevens](https://github.com/Smaug123). - [Connor Baker](https://github.com/ConnorBaker). +- [Matthew Kenigsberg](https://github.com/mkenigs). - [Florian Finkernagel](https://github.com/TyberiusPrime). ## Footnotes diff --git a/src/alejandra_cli/Cargo.toml b/src/alejandra_cli/Cargo.toml index 4a938b8..fc6a30e 100644 --- a/src/alejandra_cli/Cargo.toml +++ b/src/alejandra_cli/Cargo.toml @@ -15,7 +15,10 @@ futures = { version = "*", default_features = false, features = [ "thread-pool" ] } num_cpus = { version = "*", default_features = false, features = [] } -rand = { version = "*", default-features = false, features = ["getrandom"] } +rand = { version = "*", default-features = false, features = [ + "alloc", + "getrandom" +] } walkdir = { version = "*", default_features = false, features = [] } [package] diff --git a/src/alejandra_cli/src/ads/contributor_thanks.txt b/src/alejandra_cli/src/ads/contributor_thanks.txt new file mode 100644 index 0000000..fea55b8 --- /dev/null +++ b/src/alejandra_cli/src/ads/contributor_thanks.txt @@ -0,0 +1,2 @@ +👏 Special thanks to {name} for helping improve this project: +https://github.com/{github} diff --git a/src/alejandra_cli/src/ads/donation.txt b/src/alejandra_cli/src/ads/donation.txt deleted file mode 100644 index 9c9bb69..0000000 --- a/src/alejandra_cli/src/ads/donation.txt +++ /dev/null @@ -1,2 +0,0 @@ -If Alejandra is useful to you, please sponsor its author. -Thank you! https://github.com/sponsors/kamadorueda diff --git a/src/alejandra_cli/src/ads/mod.rs b/src/alejandra_cli/src/ads/mod.rs index c7857b1..5a770c4 100644 --- a/src/alejandra_cli/src/ads/mod.rs +++ b/src/alejandra_cli/src/ads/mod.rs @@ -1,11 +1,94 @@ +use rand::distributions::weighted::WeightedIndex; use rand::distributions::Distribution; -use rand::distributions::Uniform; use rand::rngs::OsRng; -pub(crate) fn random_ad() -> &'static str { - let ads = [include_str!("star.txt"), include_str!("donation.txt")]; +pub(crate) fn random_ad() -> String { + let ads = [ + // 90% distributed proportional to total past and present contributions + // (0.9001, sponsor_benefits as fn() -> String), + // 10% is reserved for everything else + (0.0333, contributor_thanks as fn() -> String), + (0.0333, please_sponsor as fn() -> String), + (0.0333, please_star as fn() -> String), + ]; - let ads_index = Uniform::from(0..ads.len()).sample(&mut OsRng); - - ads[ads_index] + random_weighted_choice(&ads[..])() +} + +fn sponsor_benefits() -> String { + #[allow(dead_code)] + enum Sponsor<'a> { + Individual { name: &'a str }, + Company { ad: &'a str }, + } + + let sponsors = []; + + match random_weighted_choice(&sponsors[..]) { + Sponsor::Company { ad } => { + include_str!("sponsor_advertisement.txt").replace("{ad}", ad) + }, + Sponsor::Individual { name } => { + include_str!("sponsor_thanks.txt").replace("{name}", name) + }, + } +} + +fn contributor_thanks() -> String { + let names = [ + (1.0, ("Connor Baker", "ConnorBaker")), + (1.0, ("David Arnold", "blaggacao")), + (1.0, ("David Hauer", "DavHau")), + (1.0, ("Fabian Möller", "B4dM4n")), + (1.0, ("Florian Finkernagel", "TyberiusPrime")), + (1.0, ("Jamie Quigley", "Sciencentistguy")), + (1.0, ("Joachim Ernst", "0x4A6F")), + (1.0, ("Jörg Thalheim", "Mic92")), + (1.0, ("Kevin Amado", "kamadorueda")), + (1.0, ("Loïc Reynier", "loicreynier")), + (1.0, ("Matthew Kenigsberg", "mkenigs")), + (1.0, ("Mr Hedgehog", "ModdedGamers")), + (1.0, ("Norbert Melzer", "NobbZ")), + (1.0, ("Patrick Stevens", "Smaug123")), + (1.0, ("Piegames", "piegamesde")), + (1.0, ("Rehno Lindeque", "rehno-lindeque")), + (1.0, ("Rok Garbas", "garbas")), + (1.0, ("Ryan Mulligan", "ryantm")), + (1.0, ("Thomas Bereknyei", "tomberek")), + (1.0, ("Tristan Maat", "TLATER")), + (1.0, ("Vincent Ambo", "tazjin")), + (1.0, ("Yorick van Pelt", "yorickvP")), + ]; + + let (name, github) = random_weighted_choice(&names[..]); + + include_str!("contributor_thanks.txt") + .replace("{github}", github) + .replace("{name}", name) +} + +fn please_sponsor() -> String { + let messages = [ + (0.5000, include_str!("sponsor_advertisement.txt").replace( + "{ad}", + concat!( + "Advertise your company here, reach lots of active Nix users!\n", + "More information at: https://github.com/sponsors/kamadorueda\n" + ), + )), + (0.5000, include_str!("please_sponsor.txt").to_string()), + ]; + + random_weighted_choice(&messages[..]).clone() +} + +fn please_star() -> String { + include_str!("please_star.txt").to_string() +} + +fn random_weighted_choice(choices: &[(f64, T)]) -> &T { + let weights = choices.iter().map(|(weight, _)| *weight); + let index: usize = WeightedIndex::new(weights).unwrap().sample(&mut OsRng); + + &choices[index].1 } diff --git a/src/alejandra_cli/src/ads/please_sponsor.txt b/src/alejandra_cli/src/ads/please_sponsor.txt new file mode 100644 index 0000000..7133e0d --- /dev/null +++ b/src/alejandra_cli/src/ads/please_sponsor.txt @@ -0,0 +1,2 @@ +🤟 If Alejandra is useful to you, please consider sponsoring its author! +There are cool benefits for you: https://github.com/sponsors/kamadorueda diff --git a/src/alejandra_cli/src/ads/please_star.txt b/src/alejandra_cli/src/ads/please_star.txt new file mode 100644 index 0000000..eb0f70c --- /dev/null +++ b/src/alejandra_cli/src/ads/please_star.txt @@ -0,0 +1,2 @@ +⭐ If Alejandra is useful to you, please add your star to the repository. +Thank you! https://github.com/kamadorueda/alejandra diff --git a/src/alejandra_cli/src/ads/sponsor_advertisement.txt b/src/alejandra_cli/src/ads/sponsor_advertisement.txt new file mode 100644 index 0000000..0b25e69 --- /dev/null +++ b/src/alejandra_cli/src/ads/sponsor_advertisement.txt @@ -0,0 +1 @@ +👉 [Sponsored] {ad} diff --git a/src/alejandra_cli/src/ads/sponsor_thanks.txt b/src/alejandra_cli/src/ads/sponsor_thanks.txt new file mode 100644 index 0000000..3db3737 --- /dev/null +++ b/src/alejandra_cli/src/ads/sponsor_thanks.txt @@ -0,0 +1 @@ +👏 Special thanks to {name} for being a sponsor of Alejandra! diff --git a/src/alejandra_cli/src/ads/star.txt b/src/alejandra_cli/src/ads/star.txt deleted file mode 100644 index 71cf35a..0000000 --- a/src/alejandra_cli/src/ads/star.txt +++ /dev/null @@ -1,2 +0,0 @@ -If Alejandra is useful to you, please add your star to the repository. -Thank you! https://github.com/kamadorueda/alejandra diff --git a/src/alejandra_cli/src/cli.rs b/src/alejandra_cli/src/cli.rs index 1ad4449..24eac2b 100644 --- a/src/alejandra_cli/src/cli.rs +++ b/src/alejandra_cli/src/cli.rs @@ -87,8 +87,7 @@ fn format_paths( if verbosity.allows_info() { eprintln!( - "{} {paths_len} file{} using {threads} thread{}.", - "Checking style in", + "Checking style in {paths_len} file{} using {threads} thread{}.", if paths_len == 1 { "" } else { "s" }, if threads == 1 { "" } else { "s" }, ); @@ -218,7 +217,9 @@ pub fn main() -> std::io::Result<()> { if verbosity.allows_info() { eprintln!(); - eprintln!("Congratulations! Your code complies with the Alejandra style."); + eprintln!( + "Congratulations! Your code complies with the Alejandra style." + ); eprintln!(); eprint!("{}", random_ad()); }