Struct cursive::views::SelectView [−][src]
pub struct SelectView<T = String> { /* fields omitted */ }
Expand description
View to select an item among a list.
It contains a list of values of type T, with associated labels.
Examples
let mut time_select = SelectView::new().h_align(HAlign::Center);
time_select.add_item("Short", 1);
time_select.add_item("Medium", 5);
time_select.add_item("Long", 10);
time_select.set_on_submit(|s, time| {
s.pop_layer();
let text = format!("You will wait for {} minutes...", time);
s.add_layer(
Dialog::around(TextView::new(text)).button("Quit", |s| s.quit()),
);
});
let mut siv = Cursive::new();
siv.add_layer(Dialog::around(time_select).title("How long is your wait?"));
Implementations
Disables this view.
Chainable variant.
Enable or disable this view.
Enable or disable this view.
Chainable variant.
Returns true
if this view is enabled.
Creates a new empty SelectView.
Sets the “auto-jump” property for this view.
If enabled, when a key is pressed, the selection will jump to the next item beginning with the pressed letter.
Sets the “auto-jump” property for this view.
If enabled, when a key is pressed, the selection will jump to the next item beginning with the pressed letter.
Chainable variant.
Turns self
into a popup select view.
Chainable variant.
Sets a callback to be used when an item is selected.
Sets a callback to be used when an item is selected.
Chainable variant.
Examples
use cursive_core::traits::Nameable;
use cursive_core::views::{SelectView, TextView};
let text_view = TextView::new("").with_name("text");
let select_view = SelectView::new()
.item("One", 1)
.item("Two", 2)
.on_select(|s, item| {
let content = match *item {
1 => "Content number one",
2 => "Content number two! Much better!",
_ => unreachable!("no such item"),
};
// Update the textview with the currently selected item.
s.call_on_name("text", |v: &mut TextView| {
v.set_content(content);
})
.unwrap();
});
Sets a callback to be used when <Enter>
is pressed.
Also happens if the user clicks an item.
The item currently selected will be given to the callback.
Here, V
can be T
itself, or a type that can be borrowed from T
.
Sets a callback to be used when <Enter>
is pressed.
Also happens if the user clicks an item.
The item currently selected will be given to the callback.
Chainable variant.
Examples
use cursive_core::views::{Dialog, SelectView};
let select_view = SelectView::new()
.item("One", 1)
.item("Two", 2)
.on_submit(|s, item| {
let content = match *item {
1 => "Content number one",
2 => "Content number two! Much better!",
_ => unreachable!("no such item"),
};
// Show a popup whenever the user presses <Enter>.
s.add_layer(Dialog::info(content));
});
Sets the alignment for this view.
Examples
use cursive_core::align;
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("One", 1)
.align(align::Align::top_center());
Sets the vertical alignment for this view. (If the view is given too much space vertically.)
Sets the horizontal alignment for this view.
Returns the value of the currently selected item.
Returns None
if the list is empty.
Adds a item to the list, with given label and value.
Examples
use cursive_core::views::SelectView;
let mut select_view = SelectView::new();
select_view.add_item("Item 1", 1);
select_view.add_item("Item 2", 2);
Gets an item at given idx or None.
use cursive_core::views::{SelectView, TextView};
use cursive_core::Cursive;
let select = SelectView::new().item("Short", 1);
assert_eq!(select.get_item(0), Some(("Short", &1)));
Gets a mut item at given idx or None.
Iterate mutably on the items in this view.
Returns an iterator with each item and their labels.
In some cases some items will need to be cloned (for example if a
Rc<T>
is still alive after calling SelectView::selection()
).
If T
does not implement Clone
, check SelectView::try_iter_mut()
.
pub fn try_iter_mut(
&mut self
) -> impl Iterator<Item = (&mut SpannedString<Style>, Option<&mut T>)>
pub fn try_iter_mut(
&mut self
) -> impl Iterator<Item = (&mut SpannedString<Style>, Option<&mut T>)>
Try to iterate mutably on the items in this view.
Returns an iterator with each item and their labels.
Some items may not be returned mutably, for example if a Rc<T>
is
still alive after calling SelectView::selection()
.
Iterate on the items in this view.
Returns an iterator with each item and their labels.
Removes an item from the list.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
pub fn insert_item<S>(&mut self, index: usize, label: S, value: T) where
S: Into<SpannedString<Style>>,
pub fn insert_item<S>(&mut self, index: usize, label: S, value: T) where
S: Into<SpannedString<Style>>,
Inserts an item at position index
, shifting all elements after it to
the right.
Chainable variant of add_item
Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Surprise item", 42);
pub fn add_all<S, I>(&mut self, iter: I) where
S: Into<SpannedString<Style>>,
I: IntoIterator<Item = (S, T)>,
pub fn add_all<S, I>(&mut self, iter: I) where
S: Into<SpannedString<Style>>,
I: IntoIterator<Item = (S, T)>,
Adds all items from from an iterator.
pub fn with_all<S, I>(self, iter: I) -> SelectView<T> where
S: Into<SpannedString<Style>>,
I: IntoIterator<Item = (S, T)>,
pub fn with_all<S, I>(self, iter: I) -> SelectView<T> where
S: Into<SpannedString<Style>>,
I: IntoIterator<Item = (S, T)>,
Adds all items from from an iterator.
Chainable variant.
Examples
use cursive_core::views::SelectView;
// Create a SelectView with 100 items
let select_view = SelectView::new()
.with_all((1u8..100).into_iter().map(|i| (format!("Item {}", i), i)));
Returns the id of the item currently selected.
Returns None
if the list is empty.
Returns the number of items in this list.
Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Item 3", 3);
assert_eq!(select_view.len(), 3);
Returns true
if this list has no item.
Examples
use cursive_core::views::SelectView;
let mut select_view = SelectView::new();
assert!(select_view.is_empty());
select_view.add_item("Item 1", 1);
select_view.add_item("Item 2", 2);
assert!(!select_view.is_empty());
select_view.clear();
assert!(select_view.is_empty());
Sort the current items lexicographically by their label.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
This sort is stable: items with identical label will not be reordered.
Sort the current items with the given comparator function.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
The given comparator function must define a total order for the items.
If the comparator function does not define a total order, then the order after the sort is unspecified.
This sort is stable: equal items will not be reordered.
Sort the current items with the given key extraction function.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
This sort is stable: items with equal keys will not be reordered.
Moves the selection to the given position.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
Sets the selection to the given position.
Chainable variant.
Does not apply on_select
callbacks.
Moves the selection up by the given number of rows.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
:
fn select_up(siv: &mut Cursive, view: &mut SelectView<()>) {
let cb = view.select_up(1);
cb(siv);
}
Moves the selection down by the given number of rows.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
Convenient method to use the label as value.
Chainable variant of add_item_str
Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item_str("Paris")
.item_str("New York")
.item_str("Tokyo");
Convenient method to use the label as value.
Adds all strings from an iterator.
Examples
let mut select_view = SelectView::new();
select_view.add_all_str(vec!["a", "b", "c"]);
pub fn with_all_str<S, I>(self, iter: I) -> SelectView<String> where
S: Into<String>,
I: IntoIterator<Item = S>,
pub fn with_all_str<S, I>(self, iter: I) -> SelectView<String> where
S: Into<String>,
I: IntoIterator<Item = S>,
Adds all strings from an iterator.
Chainable variant.
Examples
use cursive_core::views::SelectView;
let text = "..."; // Maybe read some config file
let select_view = SelectView::new().with_all_str(text.lines());
Trait Implementations
Returns the “default value” for a type. Read more
Draws the view with the given printer (includes bounds) and focus. Read more
Returns the minimum size the view requires with the given restrictions. Read more
Called when an event is received (key press, mouse event, …). Read more
Attempt to give this view the focus. Read more
Called once the size for this view has been decided. Read more
What part of the view is important and should be visible? Read more
Should return true
if the view content changed since the last call
to layout()
. Read more
Runs a closure on the view identified by the given selector. Read more
Moves the focus to the view identified by the given selector. Read more
Auto Trait Implementations
impl<T = String> !RefUnwindSafe for SelectView<T>
impl<T = String> !Send for SelectView<T>
impl<T = String> !Sync for SelectView<T>
impl<T> Unpin for SelectView<T>
impl<T = String> !UnwindSafe for SelectView<T>
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘ
pub fn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘ
Returns a Box<View>
.
Wraps self
in a ResizedView
with the given size constraints.
Wraps self
into a fixed-size ResizedView
.
Wraps self
into a fixed-width ResizedView
.
Wraps self
into a fixed-width ResizedView
.
Wraps self
into a full-screen ResizedView
.
Wraps self
into a full-width ResizedView
.
Wraps self
into a full-height ResizedView
.
Wraps self
into a limited-size ResizedView
.
Wraps self
into a limited-width ResizedView
.
Wraps self
into a limited-height ResizedView
.
Wraps self
into a ResizedView
at least sized size
.
Wraps self
in a ResizedView
at least min_width
wide.
Wraps self
in a ResizedView
at least min_height
tall.
Wraps self
in a ScrollView
.
Calls the given closure and return the result. Read more
Calls the given closure on self
.