Merge branch 'random-default'

This commit is contained in:
Valerie Wolfe 2024-12-13 11:01:07 -05:00
commit a161aab0ca
3 changed files with 34 additions and 14 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "pride"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
description = "Pride flags in the terminal."
@ -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,13 @@ 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 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

View file

@ -1,5 +1,8 @@
//! main method module
use std::env::var;
use std::{
env::var,
time::UNIX_EPOCH
};
use pico_args::Arguments;
@ -20,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
@ -51,13 +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 { Some(default) }
} else { None };
let variant = args.subcommand().unwrap();
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();
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() {