Add random preset option (port of 1211dc6)
This commit is contained in:
parent
e78ae3233c
commit
99d652383a
1 changed files with 29 additions and 9 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::iter;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr as _;
|
use std::str::FromStr as _;
|
||||||
|
|
||||||
|
|
@ -6,7 +7,8 @@ use anyhow::Context as _;
|
||||||
use bpaf::ShellComp;
|
use bpaf::ShellComp;
|
||||||
use bpaf::{construct, long, OptionParser, Parser as _};
|
use bpaf::{construct, long, OptionParser, Parser as _};
|
||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
use strum::VariantNames as _;
|
use itertools::Itertools as _;
|
||||||
|
use strum::{VariantArray, VariantNames};
|
||||||
|
|
||||||
use crate::color_util::{color, Lightness};
|
use crate::color_util::{color, Lightness};
|
||||||
use crate::presets::Preset;
|
use crate::presets::Preset;
|
||||||
|
|
@ -53,19 +55,36 @@ pub fn options() -> OptionParser<Options> {
|
||||||
.help(&*format!(
|
.help(&*format!(
|
||||||
"Use preset
|
"Use preset
|
||||||
PRESET={{{presets}}}",
|
PRESET={{{presets}}}",
|
||||||
presets = Preset::VARIANTS.join(",")
|
presets = <Preset as VariantNames>::VARIANTS
|
||||||
|
.iter()
|
||||||
|
.chain(iter::once(&"random"))
|
||||||
|
.join(",")
|
||||||
))
|
))
|
||||||
.argument::<String>("PRESET");
|
.argument::<String>("PRESET");
|
||||||
#[cfg(feature = "autocomplete")]
|
#[cfg(feature = "autocomplete")]
|
||||||
let preset = preset.complete(complete_preset);
|
let preset = preset.complete(complete_preset);
|
||||||
let preset = preset
|
let preset = preset
|
||||||
.parse(|s| {
|
.parse(|s| {
|
||||||
Preset::from_str(&s).with_context(|| {
|
Preset::from_str(&s)
|
||||||
format!(
|
.or_else(|e| {
|
||||||
"PRESET should be one of {{{presets}}}",
|
if s == "random" {
|
||||||
presets = Preset::VARIANTS.join(",")
|
let mut rng = fastrand::Rng::new();
|
||||||
)
|
Ok(*rng
|
||||||
})
|
.choice(<Preset as VariantArray>::VARIANTS)
|
||||||
|
.expect("preset iterator should not be empty"))
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"PRESET should be one of {{{presets}}}",
|
||||||
|
presets = <Preset as VariantNames>::VARIANTS
|
||||||
|
.iter()
|
||||||
|
.chain(iter::once(&"random"))
|
||||||
|
.join(",")
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.optional();
|
.optional();
|
||||||
let mode = long("mode")
|
let mode = long("mode")
|
||||||
|
|
@ -177,8 +196,9 @@ BACKEND={{{backends}}}",
|
||||||
|
|
||||||
#[cfg(feature = "autocomplete")]
|
#[cfg(feature = "autocomplete")]
|
||||||
fn complete_preset(input: &String) -> Vec<(String, Option<String>)> {
|
fn complete_preset(input: &String) -> Vec<(String, Option<String>)> {
|
||||||
Preset::VARIANTS
|
<Preset as VariantNames>::VARIANTS
|
||||||
.iter()
|
.iter()
|
||||||
|
.chain(iter::once(&"random"))
|
||||||
.filter_map(|&name| {
|
.filter_map(|&name| {
|
||||||
if name.starts_with(input) {
|
if name.starts_with(input) {
|
||||||
Some((name.to_owned(), None))
|
Some((name.to_owned(), None))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue