Struct 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> SelectView<T>
where T: 'static + Send + Sync,

Source

pub fn disable(&mut self)

Disables this view.

A disabled view cannot be selected.

Source

pub fn disabled(self) -> SelectView<T>

Disables this view.

Chainable variant.

Source

pub fn enable(&mut self)

Re-enables this view.

Source

pub fn set_enabled(&mut self, enabled: bool)

Enable or disable this view.

Source

pub fn with_enabled(self, is_enabled: bool) -> SelectView<T>

Enable or disable this view.

Chainable variant.

Source

pub fn is_enabled(&self) -> bool

Returns true if this view is enabled.

Source

pub fn new() -> SelectView<T>

Creates a new empty SelectView.

Source

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.

Source

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.

Source

pub fn set_inactive_highlight(&mut self, inactive_highlight: bool)

Sets the “inactive highlight” property for this view.

  • If true (the default), the selected row will be highlighted when the view is not focused.
  • If false, the selected row will be printed like the others if inactive.
Source

pub fn with_inactive_highlight(self, inactive_highlight: bool) -> SelectView<T>

Sets the “inactive highlight” property for this view.

  • If true (the default), the selected row will be highlighted when the view is not focused.
  • If false, the selected row will be printed like the others if inactive.

Chainable variant.

Source

pub fn get_inactive_highlight(&self) -> bool

Returns the current status of the “inactive highlight” property.

Source

pub fn popup(self) -> SelectView<T>

Turns self into a popup select view.

Chainable variant.

Source

pub fn set_popup(&mut self, popup: bool)

Turns self into a popup select view.

Source

pub fn decorators<S>(self, start: S, end: S) -> SelectView<T>
where S: Into<String>,

Use custom decorators around the popup button instead of “<” and “>”.

Chainable variant.

Source

pub fn set_decorators<S>(&mut self, start: S, end: S)
where S: Into<String>,

Use custom decorators around the popup button instead of “<” and “>”.

Source

pub fn set_on_select<F>(&mut self, cb: F)
where F: Fn(&mut Cursive, &T) + 'static + Send + Sync,

Sets a callback to be used when an item is selected.

Source

pub fn on_select_cb<F>(cb: F) -> Arc<dyn Fn(&mut Cursive, &T) + Sync + Send>
where F: Fn(&mut Cursive, &T) + 'static + Send + Sync,

Helper method to store a callback of the correct type for Self::set_on_select.

This is mostly useful when using this view in a template.

Source

pub fn set_on_select_cb( &mut self, cb: Arc<dyn Fn(&mut Cursive, &T) + Sync + Send>, )

Helper method to call Self::set_on_select with a variable from a config.

This is mostly useful when writing a cursive blueprint for this view.

Source

pub fn on_select<F>(self, cb: F) -> SelectView<T>
where F: Fn(&mut Cursive, &T) + 'static + Send + Sync,

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

pub fn set_on_submit<F, V>(&mut self, cb: F)
where F: 'static + Fn(&mut Cursive, &V) + Send + Sync, 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.

Source

pub fn on_submit<F, V>(self, cb: F) -> SelectView<T>
where F: Fn(&mut Cursive, &V) + 'static + Send + Sync, 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));
    });
Source

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

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

Source

pub fn h_align(self, h: HAlign) -> SelectView<T>

Sets the horizontal alignment for this view.

Source

pub fn selection(&self) -> Option<Arc<T>>

Returns the value of the currently selected item.

Returns None if the list is empty.

Source

pub fn clear(&mut self)

Removes all items from this view.

Source

pub fn add_item<S>(&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);
Source

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

pub fn get_item_mut( &mut self, i: usize, ) -> Option<(&mut SpannedString<Style>, &mut T)>

Gets a mut item at given idx or None.

Source

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 Arc<T> is still alive after calling SelectView::selection()).

If T does not implement Clone, check SelectView::try_iter_mut().

Source

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 Arc<T> is still alive after calling SelectView::selection().

Source

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.

Source

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.

Source

pub fn insert_item<S>(&mut self, index: usize, label: S, value: T)

Inserts an item at position index, shifting all elements after it to the right.

Source

pub fn item<S>(self, label: S, value: T) -> SelectView<T>

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

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.

Source

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

pub fn selected_id(&self) -> Option<usize>

Returns the id of the item currently selected.

Returns None if the list is empty.

Source

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

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

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.

Source

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.

Source

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.

Source

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.

Source

pub fn selected(self, i: usize) -> SelectView<T>

Sets the selection to the given position.

Chainable variant.

Does not apply on_select callbacks.

Source

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

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

Source

pub fn add_item_str<S>(&mut self, label: S)
where S: Into<String>,

Convenient method to use the label as value.

Source

pub fn add_item_styled<S>(&mut self, label: S)

Convenient method to use the label unstyled text as value.

Source

pub fn item_str<S>(self, label: S) -> SelectView
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");
Source

pub fn item_styled<S>(self, label: S) -> SelectView

Chainable variant of add_item_styled.

Source

pub fn insert_item_str<S>(&mut self, index: usize, label: S)
where S: Into<String>,

Convenient method to use the label as value.

Source

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

pub fn with_all_str<S, I>(self, iter: I) -> SelectView
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());
Source§

impl<T> SelectView<T>
where T: 'static + Ord,

Source

pub fn sort(&mut self)

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§

Source§

impl<T> Default for SelectView<T>
where T: 'static + Send + Sync,

Source§

fn default() -> SelectView<T>

Returns the “default value” for a type. Read more
Source§

impl<T> View for SelectView<T>
where T: 'static + Send + Sync,

Source§

fn draw(&self, printer: &Printer<'_, '_>)

Draws the view with the given printer (includes bounds) and focus. Read more
Source§

fn required_size(&mut self, _: XY<usize>) -> XY<usize>

Returns the minimum size the view requires with the given restrictions. Read more
Source§

fn on_event(&mut self, event: Event) -> EventResult

Called when an event is received (key press, mouse event, …). Read more
Source§

fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>

Attempt to give this view the focus. Read more
Source§

fn layout(&mut self, size: XY<usize>)

Called once the size for this view has been decided. Read more
Source§

fn important_area(&self, size: XY<usize>) -> Rect

What part of the view is important and should be visible? Read more
Source§

fn needs_relayout(&self) -> bool

Should return true if the view content changed since the last call to layout(). Read more
Source§

fn call_on_any( &mut self, _: &Selector<'_>, _: &mut dyn FnMut(&mut (dyn View + 'static)), )

Runs a closure on the view identified by the given selector. Read more
Source§

fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>

Moves the focus to the view identified by the given selector. Read more
Source§

fn type_name(&self) -> &'static str

Returns the type of this view. Read more

Auto Trait Implementations§

§

impl<T = String> !Freeze for SelectView<T>

§

impl<T = String> !RefUnwindSafe for SelectView<T>

§

impl<T> Send for SelectView<T>
where T: Sync + Send,

§

impl<T> Sync for SelectView<T>
where T: Sync + Send,

§

impl<T> Unpin for SelectView<T>

§

impl<T = String> !UnwindSafe for SelectView<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnyView for T
where T: View,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Downcast self to a Any.

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Downcast self to a mutable Any.

Source§

fn as_boxed_any(self: Box<T>) -> Box<dyn Any>

Returns a boxed any from a boxed self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Finder for T
where T: View,

Source§

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
Source§

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
Source§

fn call_on_name<V, F, R>(&mut self, name: &str, callback: F) -> Option<R>
where V: View, F: FnOnce(&mut V) -> R,

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

fn find_name<V>(&mut self, name: &str) -> Option<ViewRef<V>>
where V: View,

Convenient method to find a view wrapped in an NamedView.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoBoxedView for T
where T: View,

Source§

fn into_boxed_view(self) -> Box<dyn View>

Returns a Box<View>.
Source§

impl<T> Nameable for T
where T: View,

Source§

fn with_name<S>(self, name: S) -> NamedView<Self>
where S: Into<String>,

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

impl<T> Resizable for T
where T: View,

Source§

fn resized( self, width: SizeConstraint, height: SizeConstraint, ) -> ResizedView<Self>

Wraps self in a ResizedView with the given size constraints.
Source§

fn fixed_size<S>(self, size: S) -> ResizedView<Self>
where S: Into<XY<usize>>,

Wraps self into a fixed-size ResizedView.
Source§

fn fixed_width(self, width: usize) -> ResizedView<Self>

Wraps self into a fixed-width ResizedView.
Source§

fn fixed_height(self, height: usize) -> ResizedView<Self>

Wraps self into a fixed-width ResizedView.
Source§

fn full_screen(self) -> ResizedView<Self>

Wraps self into a full-screen ResizedView.
Source§

fn full_width(self) -> ResizedView<Self>

Wraps self into a full-width ResizedView.
Source§

fn full_height(self) -> ResizedView<Self>

Wraps self into a full-height ResizedView.
Source§

fn max_size<S>(self, size: S) -> ResizedView<Self>
where S: Into<XY<usize>>,

Wraps self into a limited-size ResizedView.
Source§

fn max_width(self, max_width: usize) -> ResizedView<Self>

Wraps self into a limited-width ResizedView.
Source§

fn max_height(self, max_height: usize) -> ResizedView<Self>

Wraps self into a limited-height ResizedView.
Source§

fn min_size<S>(self, size: S) -> ResizedView<Self>
where S: Into<XY<usize>>,

Wraps self into a ResizedView at least sized size.
Source§

fn min_width(self, min_width: usize) -> ResizedView<Self>

Wraps self in a ResizedView at least min_width wide.
Source§

fn min_height(self, min_height: usize) -> ResizedView<Self>

Wraps self in a ResizedView at least min_height tall.
Source§

impl<T> Scrollable for T
where T: View,

Source§

fn scrollable(self) -> ScrollView<Self>

Wraps self in a ScrollView.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> With for T

Source§

fn wrap_with<U, F>(self, f: F) -> U
where F: FnOnce(Self) -> U,

Calls the given closure and return the result. Read more
Source§

fn with<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure on self.
Source§

fn try_with<E, F>(self, f: F) -> Result<Self, E>
where F: FnOnce(&mut Self) -> Result<(), E>,

Calls the given closure on self.
Source§

fn with_if<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure if condition == true.