Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sponsor benefits #343

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/alejandra_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions src/alejandra_cli/src/ads/contributor_thanks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
👏 Special thanks to {name} for helping improve this project:
https://github.com/{github}
2 changes: 0 additions & 2 deletions src/alejandra_cli/src/ads/donation.txt

This file was deleted.

93 changes: 88 additions & 5 deletions src/alejandra_cli/src/ads/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
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<T>(choices: &[(f64, T)]) -> &T {
let weights = choices.iter().map(|(weight, _)| *weight);
let index: usize = WeightedIndex::new(weights).unwrap().sample(&mut OsRng);

ads[ads_index]
&choices[index].1
}
2 changes: 2 additions & 0 deletions src/alejandra_cli/src/ads/please_sponsor.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/alejandra_cli/src/ads/please_star.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
⭐ If Alejandra is useful to you, please add your star to the repository.
Thank you! https://github.com/kamadorueda/alejandra
1 change: 1 addition & 0 deletions src/alejandra_cli/src/ads/sponsor_advertisement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
👉 [Sponsored] {ad}
1 change: 1 addition & 0 deletions src/alejandra_cli/src/ads/sponsor_thanks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
👏 Special thanks to {name} for being a sponsor of Alejandra!
2 changes: 0 additions & 2 deletions src/alejandra_cli/src/ads/star.txt

This file was deleted.

7 changes: 4 additions & 3 deletions src/alejandra_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
);
Expand Down Expand Up @@ -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());
}
Expand Down