SelectView: add methods to add items from iterators

This commit is contained in:
Alexandre Bury 2016-07-30 01:52:34 -07:00
parent 1165e70e15
commit 42a377d92b
2 changed files with 60 additions and 20 deletions

View File

@ -9,12 +9,10 @@ fn main() {
// Read the list of cities from separate file, and fill the view with it. // Read the list of cities from separate file, and fill the view with it.
// (We include the file at compile-time to avoid runtime read errors.) // (We include the file at compile-time to avoid runtime read errors.)
let content = include_str!("../assets/cities.txt"); let content = include_str!("../assets/cities.txt");
for line in content.split('\n') { select.add_all_str(content.split('\n'));
select.add_item_str(line);
}
// Sets the callback for when "Enter" is pressed. // Sets the callback for when "Enter" is pressed.
select.set_on_select(show_next_window); select.set_on_submit(show_next_window);
let mut siv = Cursive::new(); let mut siv = Cursive::new();

View File

@ -7,7 +7,7 @@ use With;
use menu::MenuTree; use menu::MenuTree;
use direction::Direction; use direction::Direction;
use view::{Position, ScrollBase, View}; use view::{Position, ScrollBase, View};
use views::{IdView, MenuPopup}; use views::MenuPopup;
use align::{Align, HAlign, VAlign}; use align::{Align, HAlign, VAlign};
use event::{Callback, Event, EventResult, Key}; use event::{Callback, Event, EventResult, Key};
use theme::ColorStyle; use theme::ColorStyle;
@ -191,20 +191,33 @@ impl<T: 'static> SelectView<T> {
} }
/// Adds a item to the list, with given label and value. /// Adds a item to the list, with given label and value.
pub fn add_item(&mut self, label: &str, value: T) { pub fn add_item<S: Into<String>>(&mut self, label: S, value: T) {
self.items.push(Item::new(label, value)); self.items.push(Item::new(label.into(), value));
} }
/// Chainable variant of add_item /// Chainable variant of add_item
pub fn item(mut self, label: &str, value: T) -> Self { pub fn item<S: Into<String>>(self, label: S, value: T) -> Self {
self.add_item(label, value); self.with(|s| s.add_item(label, value))
self
} }
/// Wraps this view into an IdView with the given id. /// Adds all items from from an iterator.
pub fn with_id(self, label: &str) -> IdView<Self> { pub fn add_all<S, I>(&mut self, iter: I)
IdView::new(label, self) where S: Into<String>,
I: IntoIterator<Item = (S, T)>
{
for (s, t) in iter {
self.add_item(s, t);
}
}
/// Adds all items from from an iterator.
///
/// Chainable variant.
pub fn with_all<S, I>(self, iter: I) -> Self
where S: Into<String>,
I: IntoIterator<Item = (S, T)>
{
self.with(|s| s.add_all(iter))
} }
fn draw_item(&self, printer: &Printer, i: usize) { fn draw_item(&self, printer: &Printer, i: usize) {
@ -235,13 +248,42 @@ impl<T: 'static> SelectView<T> {
impl SelectView<String> { impl SelectView<String> {
/// Convenient method to use the label as value. /// Convenient method to use the label as value.
pub fn add_item_str(&mut self, label: &str) { pub fn add_item_str<S: Into<String>>(&mut self, label: S) {
self.add_item(label, label.to_string()); let label = label.into();
self.add_item(label.clone(), label);
} }
/// Chainable variant of add_item_str /// Chainable variant of add_item_str
pub fn item_str(self, label: &str) -> Self { pub fn item_str<S: Into<String>>(self, label: S) -> Self {
self.item(label, label.to_string()) self.with(|s| s.add_item_str(label))
}
/// Adds all strings from an iterator.
///
/// # Examples
///
/// ```
/// # use cursive::views::SelectView;
/// let mut select_view = SelectView::new();
/// select_view.add_all_str(vec!["a", "b", "c"]);
/// ```
pub fn add_all_str<S, I>(&mut self, iter: I)
where S: Into<String>,
I: IntoIterator<Item = S>
{
for s in iter {
self.add_item_str(s);
}
}
/// Adds all strings from an iterator.
///
/// Chainable variant.
pub fn with_all_str<S, I>(self, iter: I) -> Self
where S: Into<String>,
I: IntoIterator<Item = S>
{
self.with(|s| s.add_all_str(iter))
} }
} }
@ -441,9 +483,9 @@ struct Item<T> {
} }
impl<T> Item<T> { impl<T> Item<T> {
fn new(label: &str, value: T) -> Self { fn new(label: String, value: T) -> Self {
Item { Item {
label: label.to_string(), label: label,
value: Rc::new(value), value: Rc::new(value),
} }
} }