Skip to content

Commit

Permalink
Split list inputs by spaces and commas
Browse files Browse the repository at this point in the history
Resolves #410
Resolves #412
  • Loading branch information
russellbanks committed Jan 29, 2024
1 parent f3851b2 commit def1eb5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/prompts/list_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use inquire::validator::Validation;
use inquire::Text;
use std::collections::{BTreeSet, HashSet};
use std::collections::BTreeSet;
use std::fmt::{Debug, Display};
use std::str::FromStr;

Expand All @@ -15,13 +15,14 @@ where
T: FromStr + ListPrompt + Ord,
<T as FromStr>::Err: Display + Debug + Sync + Send + 'static,
{
const DELIMITERS: [char; 2] = [' ', ','];
let items = Text::new(T::MESSAGE)
.with_help_message(T::HELP_MESSAGE)
.with_validator(|input: &str| {
let items = input
.split(|char: char| !char.is_alphanumeric())
.split(|char| DELIMITERS.contains(&char))
.filter(|str| !str.is_empty())
.collect::<HashSet<_>>();
.collect::<BTreeSet<_>>();
let items_len = items.len();
if items_len > T::MAX_ITEMS as usize {
return Ok(Validation::Invalid(
Expand All @@ -33,14 +34,14 @@ where
));
}
for item in items {
if let Err(error) = T::from_str(input) {
if let Err(error) = T::from_str(item) {
return Ok(Validation::Invalid(format!("{item}: {error}").into()));
}
}
Ok(Validation::Valid)
})
.prompt()?
.split(|char: char| !char.is_alphanumeric())
.split(|char| DELIMITERS.contains(&char))
.filter_map(|str| T::from_str(str).ok())
.collect::<BTreeSet<_>>();
if items.is_empty() {
Expand Down

0 comments on commit def1eb5

Please sign in to comment.