Struct cursive::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
sourceimpl<T> SelectView<T>where
T: 'static,
impl<T> SelectView<T>where
T: 'static,
sourcepub fn disabled(self) -> SelectView<T>
pub fn disabled(self) -> SelectView<T>
Disables this view.
Chainable variant.
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) -> SelectView<T>
pub fn with_enabled(self, is_enabled: bool) -> SelectView<T>
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 new() -> SelectView<T>
pub fn new() -> SelectView<T>
Creates a new empty SelectView.
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) -> SelectView<T>
pub fn autojump(self) -> SelectView<T>
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 popup(self) -> SelectView<T>
pub fn popup(self) -> SelectView<T>
Turns self
into a popup select view.
Chainable variant.
sourcepub fn set_on_select<F>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &T),
pub fn set_on_select<F>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &T),
Sets a callback to be used when an item is selected.
sourcepub fn on_select<F>(self, cb: F) -> SelectView<T>where
F: 'static + Fn(&mut Cursive, &T),
pub fn on_select<F>(self, cb: F) -> SelectView<T>where
F: 'static + Fn(&mut Cursive, &T),
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>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
V: ?Sized,
pub fn set_on_submit<F, R, V>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
V: ?Sized,
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>(self, cb: F) -> SelectView<T>where
F: 'static + Fn(&mut Cursive, &V),
T: Borrow<V>,
V: ?Sized,
pub fn on_submit<F, V>(self, cb: F) -> SelectView<T>where
F: 'static + Fn(&mut Cursive, &V),
T: Borrow<V>,
V: ?Sized,
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) -> SelectView<T>
pub fn align(self, align: Align) -> SelectView<T>
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) -> SelectView<T>
pub fn v_align(self, v: VAlign) -> SelectView<T>
Sets the vertical alignment for this view. (If the view is given too much space vertically.)
sourcepub fn h_align(self, h: HAlign) -> SelectView<T>
pub fn h_align(self, h: HAlign) -> SelectView<T>
Sets the horizontal alignment for this view.
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>(&mut self, label: S, value: T)where
S: Into<SpannedString<Style>>,
pub fn add_item<S>(&mut self, label: S, value: T)where
S: Into<SpannedString<Style>>,
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 SpannedString<Style>, &mut T)>
pub fn get_item_mut(
&mut self,
i: usize
) -> Option<(&mut SpannedString<Style>, &mut T)>
Gets a mut item at given idx or None.
sourcepub fn iter_mut(
&mut self
) -> impl Iterator<Item = (&mut SpannedString<Style>, &mut T)>where
T: Clone,
pub fn iter_mut(
&mut self
) -> impl Iterator<Item = (&mut SpannedString<Style>, &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 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()
.
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<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.
sourcepub fn item<S>(self, label: S, value: T) -> SelectView<T>where
S: Into<SpannedString<Style>>,
pub fn item<S>(self, label: S, value: T) -> SelectView<T>where
S: Into<SpannedString<Style>>,
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<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.
sourcepub 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)));
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) -> SelectView<T>
pub fn selected(self, i: usize) -> SelectView<T>
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
.
sourceimpl SelectView<String>
impl SelectView<String>
sourcepub fn add_item_str<S>(&mut self, label: S)where
S: Into<String>,
pub fn add_item_str<S>(&mut self, label: S)where
S: Into<String>,
Convenient method to use the label as value.
sourcepub fn item_str<S>(self, label: S) -> SelectView<String>where
S: Into<String>,
pub fn item_str<S>(self, label: S) -> SelectView<String>where
S: Into<String>,
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) -> 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());
sourceimpl<T> SelectView<T>where
T: 'static + Ord,
impl<T> SelectView<T>where
T: 'static + Ord,
Trait Implementations
sourceimpl<T> Default for SelectView<T>where
T: 'static,
impl<T> Default for SelectView<T>where
T: 'static,
sourcefn default() -> SelectView<T>
fn default() -> SelectView<T>
Returns the “default value” for a type. Read more
sourceimpl<T> View for SelectView<T>where
T: 'static,
impl<T> View for SelectView<T>where
T: 'static,
sourcefn draw(&self, printer: &Printer<'_, '_>)
fn draw(&self, printer: &Printer<'_, '_>)
Draws the view with the given printer (includes bounds) and focus. Read more
sourcefn required_size(&mut self, XY<usize>) -> XY<usize>
fn required_size(&mut self, XY<usize>) -> XY<usize>
Returns the minimum size the view requires with the given restrictions. Read more
sourcefn on_event(&mut self, event: Event) -> EventResult
fn on_event(&mut self, event: Event) -> EventResult
Called when an event is received (key press, mouse event, …). Read more
sourcefn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
Attempt to give this view the focus. Read more
sourcefn layout(&mut self, size: XY<usize>)
fn layout(&mut self, size: XY<usize>)
Called once the size for this view has been decided. Read more
sourcefn important_area(&self, size: XY<usize>) -> Rect
fn important_area(&self, size: XY<usize>) -> Rect
What part of the view is important and should be visible? Read more
sourcefn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
Should return true
if the view content changed since the last call
to layout()
. Read more
sourcefn call_on_any(
&mut self,
&Selector<'_>,
&'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
fn call_on_any(
&mut self,
&Selector<'_>,
&'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
Runs a closure on the view identified by the given selector. Read more
sourcefn focus_view(&mut self, &Selector<'_>) -> Result<EventResult, ViewNotFound>
fn focus_view(&mut self, &Selector<'_>) -> Result<EventResult, ViewNotFound>
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
sourceimpl<T> AnyView for Twhere
T: View,
impl<T> AnyView for Twhere
T: View,
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Downcast self to a mutable Any
.
sourcefn as_boxed_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn as_boxed_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Returns a boxed any from a boxed self. Read more
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Finder for Twhere
T: View,
impl<T> Finder for Twhere
T: View,
sourcefn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)where
V: View,
F: FnMut(&mut V),
fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)where
V: View,
F: FnMut(&mut V),
Runs a callback on all views identified by sel
. Read more
sourcefn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>where
V: View,
F: FnOnce(&mut V) -> R,
fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>where
V: View,
F: FnOnce(&mut V) -> R,
Runs a callback on the view identified by sel
. Read more
sourceimpl<T> IntoBoxedView for Twhere
T: View,
impl<T> IntoBoxedView for Twhere
T: View,
sourcefn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Returns a Box<View>
.
sourceimpl<T> Resizable for Twhere
T: View,
impl<T> Resizable for Twhere
T: View,
sourcefn resized(
self,
width: SizeConstraint,
height: SizeConstraint
) -> ResizedView<Self>
fn resized(
self,
width: SizeConstraint,
height: SizeConstraint
) -> ResizedView<Self>
Wraps self
in a ResizedView
with the given size constraints.
sourcefn fixed_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn fixed_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a fixed-size ResizedView
.
sourcefn fixed_width(self, width: usize) -> ResizedView<Self>
fn fixed_width(self, width: usize) -> ResizedView<Self>
Wraps self
into a fixed-width ResizedView
.
sourcefn fixed_height(self, height: usize) -> ResizedView<Self>
fn fixed_height(self, height: usize) -> ResizedView<Self>
Wraps self
into a fixed-width ResizedView
.
sourcefn full_screen(self) -> ResizedView<Self>
fn full_screen(self) -> ResizedView<Self>
Wraps self
into a full-screen ResizedView
.
sourcefn full_width(self) -> ResizedView<Self>
fn full_width(self) -> ResizedView<Self>
Wraps self
into a full-width ResizedView
.
sourcefn full_height(self) -> ResizedView<Self>
fn full_height(self) -> ResizedView<Self>
Wraps self
into a full-height ResizedView
.
sourcefn max_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn max_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a limited-size ResizedView
.
sourcefn max_width(self, max_width: usize) -> ResizedView<Self>
fn max_width(self, max_width: usize) -> ResizedView<Self>
Wraps self
into a limited-width ResizedView
.
sourcefn max_height(self, max_height: usize) -> ResizedView<Self>
fn max_height(self, max_height: usize) -> ResizedView<Self>
Wraps self
into a limited-height ResizedView
.
sourcefn min_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn min_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a ResizedView
at least sized size
.
sourcefn min_width(self, min_width: usize) -> ResizedView<Self>
fn min_width(self, min_width: usize) -> ResizedView<Self>
Wraps self
in a ResizedView
at least min_width
wide.
sourcefn min_height(self, min_height: usize) -> ResizedView<Self>
fn min_height(self, min_height: usize) -> ResizedView<Self>
Wraps self
in a ResizedView
at least min_height
tall.
sourceimpl<T> Scrollable for Twhere
T: View,
impl<T> Scrollable for Twhere
T: View,
sourcefn scrollable(self) -> ScrollView<Self>
fn scrollable(self) -> ScrollView<Self>
Wraps self
in a ScrollView
.