From b91056d68ae6dc0d65e26663dc6cd0dbdc1a234b Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 6 Dec 2024 00:00:20 -0500 Subject: [PATCH 1/3] 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(); From 9bf53f1d270fbe7afc10ddf79cfa11319038ffcf Mon Sep 17 00:00:00 2001 From: Valerie Date: Sat, 7 Dec 2024 23:00:39 -0500 Subject: [PATCH 2/3] default string now splits on semicolons, and spaces are used for variants in default --- man/pride.6 | 6 +++++- src/main.rs | 34 ++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/man/pride.6 b/man/pride.6 index bf9f913..2ddde90 100644 --- a/man/pride.6 +++ b/man/pride.6 @@ -87,7 +87,11 @@ The transgender pride flag designed by Monica Helms in 1999. .It PRIDE_DEFAULT A string containing the name of one or more flags to use as the default when no .Ar flag -argument is provided. If the string contains a space-separated list of flags, a random one will be selected from the list. +argument is provided. If the string contains a semicolon-separated list of flags, a random one will be selected from the list. If a space is found in the chosen substring, it will be used to provide the +.Ar flag +and +.Ar variant +arguments. .El .Sh EXIT STATUS .Bl -tag -width Ds diff --git a/src/main.rs b/src/main.rs index e4d5011..f93820c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,10 @@ use crate::{ static VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const FLAG_HELP: [&str;2] = [ "-h", "--help" ]; -pub const FLAG_LIST: [&str;2] = [ "-l", "--list" ]; -pub const FLAG_SIZE: [&str;2] = [ "-s", "--size"]; -pub const FLAG_VERSION: [&str;2] = [ "-v", "--version" ]; +pub const FLAG_HELP: [&str;2] = [ "-h", "--help" ]; +pub const FLAG_LIST: [&str;2] = [ "-l", "--list" ]; +pub const FLAG_SIZE: [&str;2] = [ "-s", "--size" ]; +pub const FLAG_VERSION: [&str;2] = [ "-v", "--version" ]; fn main() { // collect args @@ -54,19 +54,25 @@ fn main() { let state = State::new(&mut args); - let subcommand = - if let Ok(Some(subcommand)) = args.subcommand() { Some(subcommand) } + let ( subcommand, variant ) = + if let Ok(Some(subcommand)) = args.subcommand() { ( Some(subcommand), args.subcommand().unwrap()) } else if let Ok(default) = var("PRIDE_DEFAULT") { - if default.is_empty() { None } - else if default.contains(' ') { - let split: Vec<&str> = default.split(' ').collect(); + if default.is_empty() { ( None, None ) } + 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(); + let chosen = split[index as usize % split.len()].to_owned(); + if chosen.contains(' ') { + let split: Vec<&str> = chosen.split(' ').collect(); + ( Some(split[0].to_owned()), Some(split[1].to_owned()) ) + } else { ( Some(chosen), None ) } + } else { ( None, None ) } + } else if default.contains(' ') { + let split: Vec<&str> = default.split(' ').collect(); + ( Some(split[0].to_owned()), Some(split[1].to_owned()) ) + } else { ( Some(default), None ) } + } else { ( None, None ) }; // get color vec from matched flag let flag: Flag = match subcommand.as_deref() { From d41df133b1fa312a369976a84bd72ea41d5940f5 Mon Sep 17 00:00:00 2001 From: Valerie Date: Sat, 7 Dec 2024 23:02:06 -0500 Subject: [PATCH 3/3] version bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5cd3666..e96c994 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pride" -version = "0.4.2" +version = "0.4.3" edition = "2021" authors = [ "Valerie Wolfe " ] description = "Pride flags in the terminal."