Skip to content

Commit

Permalink
feat: many items by iterator instead of slice
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtread committed Jan 18, 2024
1 parent 3d0ba2d commit b5b3780
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
12 changes: 8 additions & 4 deletions src/prompts/fuzzy_select.rs
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
20 changes: 12 additions & 8 deletions src/prompts/multi_select.rs
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
13 changes: 9 additions & 4 deletions src/prompts/select.rs
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
12 changes: 8 additions & 4 deletions src/prompts/sort.rs
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

0 comments on commit b5b3780

Please sign in to comment.