basic randomized default implementation

This commit is contained in:
Valerie Wolfe 2024-12-06 00:00:20 -05:00
parent ad7fc4e0cd
commit b91056d68a
3 changed files with 14 additions and 4 deletions

View file

@ -19,6 +19,7 @@ name = "pride"
path = "src/main.rs"
[dependencies]
oorandom = '11.1.4'
pico-args = "0.5.0"
termion = "4.0.3"

View file

@ -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

View file

@ -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();