basic randomized default implementation
This commit is contained in:
parent
ad7fc4e0cd
commit
b91056d68a
3 changed files with 14 additions and 4 deletions
|
@ -19,6 +19,7 @@ name = "pride"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
oorandom = '11.1.4'
|
||||||
pico-args = "0.5.0"
|
pico-args = "0.5.0"
|
||||||
termion = "4.0.3"
|
termion = "4.0.3"
|
||||||
|
|
||||||
|
|
|
@ -85,9 +85,9 @@ The transgender pride flag designed by Monica Helms in 1999.
|
||||||
.Sh ENVIRONMENT
|
.Sh ENVIRONMENT
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
.It PRIDE_DEFAULT
|
.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
|
.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
|
.El
|
||||||
.Sh EXIT STATUS
|
.Sh EXIT STATUS
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -1,5 +1,8 @@
|
||||||
//! main method module
|
//! main method module
|
||||||
use std::env::var;
|
use std::{
|
||||||
|
env::var,
|
||||||
|
time::UNIX_EPOCH
|
||||||
|
};
|
||||||
|
|
||||||
use pico_args::Arguments;
|
use pico_args::Arguments;
|
||||||
|
|
||||||
|
@ -55,7 +58,13 @@ fn main() {
|
||||||
if let Ok(Some(subcommand)) = args.subcommand() { Some(subcommand) }
|
if let Ok(Some(subcommand)) = args.subcommand() { Some(subcommand) }
|
||||||
else if let Ok(default) = var("PRIDE_DEFAULT") {
|
else if let Ok(default) = var("PRIDE_DEFAULT") {
|
||||||
if default.is_empty() { None }
|
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 };
|
} else { None };
|
||||||
let variant = args.subcommand().unwrap();
|
let variant = args.subcommand().unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue