From 42a377d92bae480430812d970a19468d058b4a83 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 30 Jul 2016 01:52:34 -0700 Subject: [PATCH] SelectView: add methods to add items from iterators --- examples/select.rs | 6 ++-- src/views/select_view.rs | 74 +++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/examples/select.rs b/examples/select.rs index 7551d03..2532c4b 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -9,12 +9,10 @@ fn main() { // 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.) let content = include_str!("../assets/cities.txt"); - for line in content.split('\n') { - select.add_item_str(line); - } + select.add_all_str(content.split('\n')); // 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(); diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 9d1b29e..6edafae 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -7,7 +7,7 @@ use With; use menu::MenuTree; use direction::Direction; use view::{Position, ScrollBase, View}; -use views::{IdView, MenuPopup}; +use views::MenuPopup; use align::{Align, HAlign, VAlign}; use event::{Callback, Event, EventResult, Key}; use theme::ColorStyle; @@ -191,20 +191,33 @@ impl SelectView { } /// Adds a item to the list, with given label and value. - pub fn add_item(&mut self, label: &str, value: T) { - self.items.push(Item::new(label, value)); + pub fn add_item>(&mut self, label: S, value: T) { + self.items.push(Item::new(label.into(), value)); } /// Chainable variant of add_item - pub fn item(mut self, label: &str, value: T) -> Self { - self.add_item(label, value); - - self + pub fn item>(self, label: S, value: T) -> Self { + self.with(|s| s.add_item(label, value)) } - /// Wraps this view into an IdView with the given id. - pub fn with_id(self, label: &str) -> IdView { - IdView::new(label, self) + /// Adds all items from from an iterator. + pub fn add_all(&mut self, iter: I) + where S: Into, + I: IntoIterator + { + for (s, t) in iter { + self.add_item(s, t); + } + } + + /// Adds all items from from an iterator. + /// + /// Chainable variant. + pub fn with_all(self, iter: I) -> Self + where S: Into, + I: IntoIterator + { + self.with(|s| s.add_all(iter)) } fn draw_item(&self, printer: &Printer, i: usize) { @@ -235,13 +248,42 @@ impl SelectView { impl SelectView { /// Convenient method to use the label as value. - pub fn add_item_str(&mut self, label: &str) { - self.add_item(label, label.to_string()); + pub fn add_item_str>(&mut self, label: S) { + let label = label.into(); + self.add_item(label.clone(), label); } /// Chainable variant of add_item_str - pub fn item_str(self, label: &str) -> Self { - self.item(label, label.to_string()) + pub fn item_str>(self, label: S) -> Self { + 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(&mut self, iter: I) + where S: Into, + I: IntoIterator + { + for s in iter { + self.add_item_str(s); + } + } + + /// Adds all strings from an iterator. + /// + /// Chainable variant. + pub fn with_all_str(self, iter: I) -> Self + where S: Into, + I: IntoIterator + { + self.with(|s| s.add_all_str(iter)) } } @@ -441,9 +483,9 @@ struct Item { } impl Item { - fn new(label: &str, value: T) -> Self { + fn new(label: String, value: T) -> Self { Item { - label: label.to_string(), + label: label, value: Rc::new(value), } }