Skip to content

Commit

Permalink
Merge pull request #299 from jacobtread-contrib/master
Browse files Browse the repository at this point in the history
Accept items by iterator instead of slice
  • Loading branch information
pksunkara authored Jan 18, 2024
2 parents 3186228 + c834f0e commit 97a840f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
20 changes: 16 additions & 4 deletions src/prompts/fuzzy_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ impl FuzzySelect<'_> {
}

/// Adds multiple items to the fuzzy selector.
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
for item in items {
self.items.push(item.to_string());
}
pub fn items<T, I>(mut self, items: I) -> Self
where
T: ToString,
I: IntoIterator<Item = T>,
{
self.items
.extend(items.into_iter().map(|item| item.to_string()));

self
}

Expand Down Expand Up @@ -404,4 +408,12 @@ mod tests {

let _ = fuzzy_select.clone();
}

#[test]
fn test_iterator() {
let items = ["First", "Second", "Third"];
let iterator = items.iter().skip(1);

assert_eq!(FuzzySelect::new().items(iterator).items, &items[1..]);
}
}
28 changes: 20 additions & 8 deletions src/prompts/multi_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,21 @@ impl MultiSelect<'_> {
}

/// Adds multiple items to the selector.
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
for item in items {
self.items.push(item.to_string());
self.defaults.push(false);
}
self
pub fn items<T, I>(self, items: I) -> Self
where
T: ToString,
I: IntoIterator<Item = T>,
{
self.items_checked(items.into_iter().map(|item| (item, false)))
}

/// Adds multiple items to the selector with checked state
pub fn items_checked<T: ToString>(mut self, items: &[(T, bool)]) -> Self {
for &(ref item, checked) in items {
pub fn items_checked<T, I>(mut self, items: I) -> Self
where
T: ToString,
I: IntoIterator<Item = (T, bool)>,
{
for (item, checked) in items.into_iter() {
self.items.push(item.to_string());
self.defaults.push(checked);
}
Expand Down Expand Up @@ -382,4 +386,12 @@ mod tests {

let _ = multi_select.clone();
}

#[test]
fn test_iterator() {
let items = ["First", "Second", "Third"];
let iterator = items.iter().skip(1);

assert_eq!(MultiSelect::new().items(iterator).items, &items[1..]);
}
}
28 changes: 19 additions & 9 deletions src/prompts/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ impl Select<'_> {
/// ```
pub fn item<T: ToString>(mut self, item: T) -> Self {
self.items.push(item.to_string());

self
}

/// Adds multiple items to the selector.
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
for item in items {
self.items.push(item.to_string());
}
pub fn items<T, I>(mut self, items: I) -> Self
where
T: ToString,
I: IntoIterator<Item = T>,
{
self.items
.extend(items.into_iter().map(|item| item.to_string()));

self
}

Expand Down Expand Up @@ -363,7 +368,7 @@ mod tests {
let selections = vec!["a".to_string(), "b".to_string()];

assert_eq!(
Select::new().default(0).items(&selections[..]).items,
Select::new().default(0).items(&selections).items,
selections
);
}
Expand All @@ -375,9 +380,14 @@ mod tests {

let selections = &[a, b];

assert_eq!(
Select::new().default(0).items(&selections[..]).items,
selections
);
assert_eq!(Select::new().default(0).items(selections).items, selections);
}

#[test]
fn test_iterator() {
let items = ["First", "Second", "Third"];
let iterator = items.iter().skip(1);

assert_eq!(Select::new().default(0).items(iterator).items, &items[1..]);
}
}
20 changes: 16 additions & 4 deletions src/prompts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ impl Sort<'_> {
}

/// Adds multiple items to the selector.
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
for item in items {
self.items.push(item.to_string());
}
pub fn items<T, I>(mut self, items: I) -> Self
where
T: ToString,
I: IntoIterator<Item = T>,
{
self.items
.extend(items.into_iter().map(|item| item.to_string()));

self
}

Expand Down Expand Up @@ -373,4 +377,12 @@ mod tests {

let _ = sort.clone();
}

#[test]
fn test_iterator() {
let items = ["First", "Second", "Third"];
let iterator = items.iter().skip(1);

assert_eq!(Sort::new().items(iterator).items, &items[1..]);
}
}

0 comments on commit 97a840f

Please sign in to comment.