Struct cursive_core::views::SelectView
source · pub struct SelectView<T = String> { /* private fields */ }
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§
source§impl<T: 'static> SelectView<T>
impl<T: 'static> SelectView<T>
sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable this view.
sourcepub fn with_enabled(self, is_enabled: bool) -> Self
pub fn with_enabled(self, is_enabled: bool) -> Self
Enable or disable this view.
Chainable variant.
sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true
if this view is enabled.
sourcepub fn set_autojump(&mut self, autojump: bool)
pub fn set_autojump(&mut self, autojump: bool)
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.
sourcepub fn autojump(self) -> Self
pub fn autojump(self) -> Self
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.
sourcepub fn set_on_select<F>(&mut self, cb: F)where
F: Fn(&mut Cursive, &T) + 'static,
pub fn set_on_select<F>(&mut self, cb: F)where
F: Fn(&mut Cursive, &T) + 'static,
Sets a callback to be used when an item is selected.
sourcepub fn on_select<F>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &T) + 'static,
pub fn on_select<F>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &T) + 'static,
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();
});
sourcepub fn set_on_submit<F, R, V: ?Sized>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
pub fn set_on_submit<F, R, V: ?Sized>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
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
.
sourcepub fn on_submit<F, V: ?Sized>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>,
pub fn on_submit<F, V: ?Sized>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>,
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));
});
sourcepub fn align(self, align: Align) -> Self
pub fn align(self, align: Align) -> Self
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());
sourcepub fn v_align(self, v: VAlign) -> Self
pub fn v_align(self, v: VAlign) -> Self
Sets the vertical alignment for this view. (If the view is given too much space vertically.)
sourcepub fn selection(&self) -> Option<Rc<T>>
pub fn selection(&self) -> Option<Rc<T>>
Returns the value of the currently selected item.
Returns None
if the list is empty.
sourcepub fn add_item<S: Into<StyledString>>(&mut self, label: S, value: T)
pub fn add_item<S: Into<StyledString>>(&mut self, label: S, value: T)
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);
sourcepub fn get_item(&self, i: usize) -> Option<(&str, &T)>
pub fn get_item(&self, i: usize) -> Option<(&str, &T)>
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)));
sourcepub fn get_item_mut(&mut self, i: usize) -> Option<(&mut StyledString, &mut T)>
pub fn get_item_mut(&mut self, i: usize) -> Option<(&mut StyledString, &mut T)>
Gets a mut item at given idx or None.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&mut StyledString, &mut T)>where
T: Clone,
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&mut StyledString, &mut T)>where
T: Clone,
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()
.
sourcepub fn try_iter_mut(
&mut self
) -> impl Iterator<Item = (&mut StyledString, Option<&mut T>)>
pub fn try_iter_mut(
&mut self
) -> impl Iterator<Item = (&mut StyledString, 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()
.
sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &T)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &T)>
Iterate on the items in this view.
Returns an iterator with each item and their labels.
sourcepub fn remove_item(&mut self, id: usize) -> Callback
pub fn remove_item(&mut self, id: usize) -> Callback
Removes an item from the list.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
sourcepub fn insert_item<S>(&mut self, index: usize, label: S, value: T)where
S: Into<StyledString>,
pub fn insert_item<S>(&mut self, index: usize, label: S, value: T)where
S: Into<StyledString>,
Inserts an item at position index
, shifting all elements after it to
the right.
sourcepub fn item<S: Into<StyledString>>(self, label: S, value: T) -> Self
pub fn item<S: Into<StyledString>>(self, label: S, value: T) -> Self
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);
sourcepub fn add_all<S, I>(&mut self, iter: I)where
S: Into<StyledString>,
I: IntoIterator<Item = (S, T)>,
pub fn add_all<S, I>(&mut self, iter: I)where
S: Into<StyledString>,
I: IntoIterator<Item = (S, T)>,
Adds all items from from an iterator.
sourcepub fn with_all<S, I>(self, iter: I) -> Selfwhere
S: Into<StyledString>,
I: IntoIterator<Item = (S, T)>,
pub fn with_all<S, I>(self, iter: I) -> Selfwhere
S: Into<StyledString>,
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)));
sourcepub fn selected_id(&self) -> Option<usize>
pub fn selected_id(&self) -> Option<usize>
Returns the id of the item currently selected.
Returns None
if the list is empty.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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());
sourcepub fn sort_by_label(&mut self)
pub fn sort_by_label(&mut self)
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.
sourcepub fn sort_by<F>(&mut self, compare: F)where
F: FnMut(&T, &T) -> Ordering,
pub fn sort_by<F>(&mut self, compare: F)where
F: FnMut(&T, &T) -> Ordering,
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.
sourcepub fn sort_by_key<K, F>(&mut self, key_of: F)where
F: FnMut(&T) -> K,
K: Ord,
pub fn sort_by_key<K, F>(&mut self, key_of: F)where
F: FnMut(&T) -> K,
K: Ord,
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.
sourcepub fn set_selection(&mut self, i: usize) -> Callback
pub fn set_selection(&mut self, i: usize) -> Callback
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
.
sourcepub fn selected(self, i: usize) -> Self
pub fn selected(self, i: usize) -> Self
Sets the selection to the given position.
Chainable variant.
Does not apply on_select
callbacks.
sourcepub fn select_up(&mut self, n: usize) -> Callback
pub fn select_up(&mut self, n: usize) -> Callback
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);
}
sourcepub fn select_down(&mut self, n: usize) -> Callback
pub fn select_down(&mut self, n: usize) -> Callback
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
.
source§impl SelectView<String>
impl SelectView<String>
sourcepub fn add_item_str<S: Into<String>>(&mut self, label: S)
pub fn add_item_str<S: Into<String>>(&mut self, label: S)
Convenient method to use the label as value.
sourcepub fn item_str<S: Into<String>>(self, label: S) -> Self
pub fn item_str<S: Into<String>>(self, label: S) -> Self
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");
sourcepub fn insert_item_str<S>(&mut self, index: usize, label: S)where
S: Into<String>,
pub fn insert_item_str<S>(&mut self, index: usize, label: S)where
S: Into<String>,
Convenient method to use the label as value.
sourcepub fn add_all_str<S, I>(&mut self, iter: I)where
S: Into<String>,
I: IntoIterator<Item = S>,
pub fn add_all_str<S, I>(&mut self, iter: I)where
S: Into<String>,
I: IntoIterator<Item = S>,
Adds all strings from an iterator.
Examples
let mut select_view = SelectView::new();
select_view.add_all_str(vec!["a", "b", "c"]);
sourcepub fn with_all_str<S, I>(self, iter: I) -> Selfwhere
S: Into<String>,
I: IntoIterator<Item = S>,
pub fn with_all_str<S, I>(self, iter: I) -> Selfwhere
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());