From 6b781684efd746ba0923386544a398895e4bf247 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 30 May 2015 21:32:24 -0700 Subject: [PATCH] Rename ListView -> SelectView And added documentation. --- Cargo.toml | 4 ++-- examples/{list.rs => select.rs} | 6 +++--- src/view/mod.rs | 4 ++-- src/view/{list_view.rs => select_view.rs} | 18 +++++++++++++----- 4 files changed, 20 insertions(+), 12 deletions(-) rename examples/{list.rs => select.rs} (75%) rename src/view/{list_view.rs => select_view.rs} (76%) diff --git a/Cargo.toml b/Cargo.toml index 192c27d..d120790 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,5 +46,5 @@ name = "key_codes" path = "examples/key_codes.rs" [[example]] -name = "list" -path = "examples/list.rs" +name = "select" +path = "examples/select.rs" diff --git a/examples/list.rs b/examples/select.rs similarity index 75% rename from examples/list.rs rename to examples/select.rs index f1ff9d7..6b7bd71 100644 --- a/examples/list.rs +++ b/examples/select.rs @@ -1,12 +1,12 @@ extern crate cursive; use cursive::Cursive; -use cursive::view::{Dialog,ListView,TextView,Selector}; +use cursive::view::{Dialog,SelectView,TextView,Selector}; fn main() { let mut siv = Cursive::new(); - siv.add_layer(Dialog::new(ListView::new() + siv.add_layer(Dialog::new(SelectView::new() .item_str("Berlin") .item_str("London") .item_str("New York") @@ -14,7 +14,7 @@ fn main() { .with_id("city")) .title("Where are you from?") .button("Ok", |s| { - let city = s.find::(&Selector::Id("city")).unwrap().selection().to_string(); + let city = s.find::(&Selector::Id("city")).unwrap().selection().to_string(); s.pop_layer(); s.add_layer(Dialog::new(TextView::new(&format!("{} is a great city!", city))) .button("Quit", |s| s.quit())); diff --git a/src/view/mod.rs b/src/view/mod.rs index 782285f..04e942b 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -13,7 +13,7 @@ mod full_view; mod id_view; mod shadow_view; mod edit_view; -mod list_view; +mod select_view; use std::any::Any; @@ -30,7 +30,7 @@ pub use self::full_view::FullView; pub use self::id_view::IdView; pub use self::shadow_view::ShadowView; pub use self::edit_view::EditView; -pub use self::list_view::ListView; +pub use self::select_view::SelectView; use event::{Event,EventResult}; use vec::{Vec2,ToVec2}; diff --git a/src/view/list_view.rs b/src/view/select_view.rs similarity index 76% rename from src/view/list_view.rs rename to src/view/select_view.rs index 2c3d12e..e9f8086 100644 --- a/src/view/list_view.rs +++ b/src/view/select_view.rs @@ -18,41 +18,49 @@ impl Item { } } -pub struct ListView { +/// View to select an item among a list. +/// +/// It contains a list of values of type T, with associated labels. +pub struct SelectView { items: Vec>, focus: usize, } -impl ListView { +impl SelectView { + /// Creates a new empty SelectView. pub fn new() -> Self { - ListView { + SelectView { items: Vec::new(), focus: 0, } } + /// Returns the value of the currently selected item. Panics if the list is empty. pub fn selection(&self) -> &T { &self.items[self.focus].value } + /// Adds a item to the list, with given label and value. pub fn item(mut self, label: &str, value: T) -> Self { self.items.push(Item::new(label,value)); self } + /// Wraps this view into an IdView with the given id. pub fn with_id(self, label: &str) -> IdView { IdView::new(label, self) } } -impl ListView { +impl SelectView { + /// For String-based SelectView, this is a convenient method to use the label as value. pub fn item_str(self, label: &str) -> Self { self.item(label, label.to_string()) } } -impl View for ListView { +impl View for SelectView { fn draw(&mut self, printer: &Printer) { for (i,item) in self.items.iter().enumerate() { let style = if i == self.focus { if printer.focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE } } else { color::PRIMARY };