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

Disables this view.

A disabled view cannot be selected.

Disables this view.

Chainable variant.

Re-enables this view.

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.

Turns self into a popup select view.

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.

Removes all items from this view.

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().

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.

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);

Adds all items from from an iterator.

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"]);

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());

Sort the current items by their natural ordering.

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 that are equal will not be reordered.

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

Returns the type of this view. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Downcast self to a Any.

Downcast self to a mutable Any.

Returns a boxed any from a boxed self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Runs a callback on all views identified by sel. Read more

Runs a callback on the view identified by sel. Read more

Convenient method to use call_on with a view::Selector::Name.

Convenient method to find a view wrapped in an NamedView.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns a Box<View>.

Wraps this view into an NamedView with the given id. Read more

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.

Should always be Self

Wraps self in a ScrollView.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Calls the given closure and return the result. Read more

Calls the given closure on self.

Calls the given closure on self.

Calls the given closure if condition == true.