Struct dialoguer::MultiSelect
source · pub struct MultiSelect<'a> { /* private fields */ }
Expand description
Renders a multi select prompt.
Example
use dialoguer::MultiSelect;
fn main() {
let items = vec!["foo", "bar", "baz"];
let selection = MultiSelect::new()
.with_prompt("What do you choose?")
.items(&items)
.interact()
.unwrap();
println!("You chose:");
for i in selection {
println!("{}", items[i]);
}
}
Implementations§
source§impl MultiSelect<'static>
impl MultiSelect<'static>
source§impl MultiSelect<'_>
impl MultiSelect<'_>
sourcepub fn clear(self, val: bool) -> Self
pub fn clear(self, val: bool) -> Self
Sets the clear behavior of the menu.
The default is to clear the menu.
sourcepub fn defaults(self, val: &[bool]) -> Self
pub fn defaults(self, val: &[bool]) -> Self
Sets a defaults for the menu.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
sourcepub fn max_length(self, val: usize) -> Self
pub fn max_length(self, val: usize) -> Self
Sets an optional max length for a page
Max length is disabled by None
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
sourcepub fn item_checked<T: ToString>(self, item: T, checked: bool) -> Self
pub fn item_checked<T: ToString>(self, item: T, checked: bool) -> Self
Add a single item to the selector with a default checked state.
sourcepub fn items<T: ToString>(self, items: &[T]) -> Self
pub fn items<T: ToString>(self, items: &[T]) -> Self
Adds multiple items to the selector.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let items = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let term = Term::buffered_stderr();
let theme = ColorfulTheme::default();
println!("All the following controls are run in a buffered terminal");
Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact_on(&term)
.unwrap();
let _: String = Input::with_theme(&theme)
.with_prompt("Your name")
.interact_on(&term)
.unwrap();
Select::with_theme(&theme)
.with_prompt("Pick an item")
.items(items)
.interact_on(&term)
.unwrap();
MultiSelect::with_theme(&theme)
.with_prompt("Pick some items")
.items(items)
.interact_on(&term)
.unwrap();
Sort::with_theme(&theme)
.with_prompt("Order these items")
.items(items)
.interact_on(&term)
.unwrap();
}
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
sourcepub fn items_checked<T: ToString>(self, items: &[(T, bool)]) -> Self
pub fn items_checked<T: ToString>(self, items: &[(T, bool)]) -> Self
Adds multiple items to the selector with checked state
sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
Prefaces the menu with a prompt.
By default, when a prompt is set the system also prints out a confirmation after
the selection. You can opt-out of this with report
.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let items = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let term = Term::buffered_stderr();
let theme = ColorfulTheme::default();
println!("All the following controls are run in a buffered terminal");
Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact_on(&term)
.unwrap();
let _: String = Input::with_theme(&theme)
.with_prompt("Your name")
.interact_on(&term)
.unwrap();
Select::with_theme(&theme)
.with_prompt("Pick an item")
.items(items)
.interact_on(&term)
.unwrap();
MultiSelect::with_theme(&theme)
.with_prompt("Pick some items")
.items(items)
.interact_on(&term)
.unwrap();
Sort::with_theme(&theme)
.with_prompt("Order these items")
.items(items)
.interact_on(&term)
.unwrap();
}
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report the selected values after interaction.
The default is to report the selections.
sourcepub fn interact(self) -> Result<Vec<usize>>
pub fn interact(self) -> Result<Vec<usize>>
Enables user interaction and returns the result.
The user can select the items with the ‘Space’ bar and on ‘Enter’ the indices of selected items will be returned.
The dialog is rendered on stderr.
Result contains Vec<index>
if user hit ‘Enter’.
This unlike interact_opt
does not allow to quit with ‘Esc’ or ‘q’.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
sourcepub fn interact_opt(self) -> Result<Option<Vec<usize>>>
pub fn interact_opt(self) -> Result<Option<Vec<usize>>>
Enables user interaction and returns the result.
The user can select the items with the ‘Space’ bar and on ‘Enter’ the indices of selected items will be returned.
The dialog is rendered on stderr.
Result contains Some(Vec<index>)
if user hit ‘Enter’ or None
if user cancelled with ‘Esc’ or ‘q’.
Example
use dialoguer::MultiSelect;
fn main() {
let items = vec!["foo", "bar", "baz"];
let ordered = MultiSelect::new()
.items(&items)
.interact_opt()
.unwrap();
match ordered {
Some(positions) => {
println!("You chose:");
for i in positions {
println!("{}", items[i]);
}
},
None => println!("You did not choose anything.")
}
}
sourcepub fn interact_on(self, term: &Term) -> Result<Vec<usize>>
pub fn interact_on(self, term: &Term) -> Result<Vec<usize>>
Like interact
but allows a specific terminal to be set.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let items = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let term = Term::buffered_stderr();
let theme = ColorfulTheme::default();
println!("All the following controls are run in a buffered terminal");
Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact_on(&term)
.unwrap();
let _: String = Input::with_theme(&theme)
.with_prompt("Your name")
.interact_on(&term)
.unwrap();
Select::with_theme(&theme)
.with_prompt("Pick an item")
.items(items)
.interact_on(&term)
.unwrap();
MultiSelect::with_theme(&theme)
.with_prompt("Pick some items")
.items(items)
.interact_on(&term)
.unwrap();
Sort::with_theme(&theme)
.with_prompt("Order these items")
.items(items)
.interact_on(&term)
.unwrap();
}
sourcepub fn interact_on_opt(self, term: &Term) -> Result<Option<Vec<usize>>>
pub fn interact_on_opt(self, term: &Term) -> Result<Option<Vec<usize>>>
Like interact_opt
but allows a specific terminal to be set.
source§impl<'a> MultiSelect<'a>
impl<'a> MultiSelect<'a>
sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates a multi select prompt with a specific theme.
Example
use dialoguer::{theme::ColorfulTheme, MultiSelect};
fn main() {
let selection = MultiSelect::with_theme(&ColorfulTheme::default())
.items(&["foo", "bar", "baz"])
.interact()
.unwrap();
}
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let items = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let term = Term::buffered_stderr();
let theme = ColorfulTheme::default();
println!("All the following controls are run in a buffered terminal");
Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact_on(&term)
.unwrap();
let _: String = Input::with_theme(&theme)
.with_prompt("Your name")
.interact_on(&term)
.unwrap();
Select::with_theme(&theme)
.with_prompt("Pick an item")
.items(items)
.interact_on(&term)
.unwrap();
MultiSelect::with_theme(&theme)
.with_prompt("Pick some items")
.items(items)
.interact_on(&term)
.unwrap();
Sort::with_theme(&theme)
.with_prompt("Order these items")
.items(items)
.interact_on(&term)
.unwrap();
}
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let multiselected = &[
"Ice Cream",
"Vanilla Cupcake",
"Chocolate Muffin",
"A Pile of sweet, sweet mustard",
];
let defaults = &[false, false, true, false];
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Pick your food")
.items(&multiselected[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.is_empty() {
println!("You did not select anything :(");
} else {
println!("You selected these things:");
for selection in selections {
println!(" {}", multiselected[selection]);
}
}
}
Trait Implementations§
source§impl<'a> Clone for MultiSelect<'a>
impl<'a> Clone for MultiSelect<'a>
source§fn clone(&self) -> MultiSelect<'a>
fn clone(&self) -> MultiSelect<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more