From b91056d68ae6dc0d65e26663dc6cd0dbdc1a234b Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 6 Dec 2024 00:00:20 -0500 Subject: [PATCH] basic randomized default implementation --- Cargo.toml | 1 + man/pride.6 | 4 ++-- src/main.rs | 13 +++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 01cd8e6..5cd3666 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ name = "pride" path = "src/main.rs" [dependencies] +oorandom = '11.1.4' pico-args = "0.5.0" termion = "4.0.3" diff --git a/man/pride.6 b/man/pride.6 index 0dad758..bf9f913 100644 --- a/man/pride.6 +++ b/man/pride.6 @@ -85,9 +85,9 @@ The transgender pride flag designed by Monica Helms in 1999. .Sh ENVIRONMENT .Bl -tag -width Ds .It PRIDE_DEFAULT -A string containing the name of a flag to use as the default when no +A string containing the name of one or more flags to use as the default when no .Ar flag -argument is provided. +argument is provided. If the string contains a space-separated list of flags, a random one will be selected from the list. .El .Sh EXIT STATUS .Bl -tag -width Ds diff --git a/src/main.rs b/src/main.rs index 16b05f0..e4d5011 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ //! main method module -use std::env::var; +use std::{ + env::var, + time::UNIX_EPOCH +}; use pico_args::Arguments; @@ -55,7 +58,13 @@ fn main() { if let Ok(Some(subcommand)) = args.subcommand() { Some(subcommand) } else if let Ok(default) = var("PRIDE_DEFAULT") { if default.is_empty() { None } - else { Some(default) } + else if default.contains(' ') { + let split: Vec<&str> = default.split(' ').collect(); + if let Ok(time) = UNIX_EPOCH.elapsed() { + let index = oorandom::Rand32::new(time.as_secs()).rand_u32(); + Some(split[index as usize % split.len()].to_owned()) + } else { None } + } else { Some(default) } } else { None }; let variant = args.subcommand().unwrap();