Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept items by iterator instead of slice #299

Merged
merged 2 commits into from Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 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 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
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
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
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..]);
}
}