From 0a8228d073fd2b8dbe1233e283ff76c3c5d4f639 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 20 Jul 2016 22:08:06 -0700 Subject: [PATCH] Add examples to documentation --- src/view/button.rs | 10 +++++++++- src/view/edit_view.rs | 36 ++++++++++++++++++++++++++++++++++++ src/view/full_view.rs | 7 +++++++ src/view/key_event_view.rs | 11 ++++++++++- src/view/mod.rs | 4 +++- src/view/scroll.rs | 3 +++ src/view/select_view.rs | 26 ++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/view/button.rs b/src/view/button.rs index d7c8af1..96f068a 100644 --- a/src/view/button.rs +++ b/src/view/button.rs @@ -10,8 +10,16 @@ use event::*; use Printer; use unicode_width::UnicodeWidthStr; -/// Simple text label with a callback when ENTER is pressed. +/// Simple text label with a callback when is pressed. +/// /// A button shows its content in a single line and has a fixed size. +/// +/// # Examples +/// +/// ``` +/// # use cursive::prelude::*; +/// let quit_button = Button::new("Quit", |s| s.quit()); +/// ``` pub struct Button { label: String, callback: Callback, diff --git a/src/view/edit_view.rs b/src/view/edit_view.rs index 6355b3d..e502394 100644 --- a/src/view/edit_view.rs +++ b/src/view/edit_view.rs @@ -11,6 +11,42 @@ use Printer; /// Input box where the user can enter and edit text. +/// +/// # Examples +/// +/// From the [edit example]. +/// +/// [edit example]: https://github.com/gyscos/Cursive/blob/master/examples/edit.rs +/// +/// ``` +/// # extern crate cursive; +/// # use cursive::prelude::*; +/// # fn main() { +/// let mut siv = Cursive::new(); +/// +/// // Create a dialog with an edit text and a button. +/// siv.add_layer(Dialog::new(EditView::new().min_length(20).with_id("edit")) +/// .padding((1, 1, 1, 0)) +/// .title("Enter your name") +/// .button("Ok", |s| { +/// // When the button is clicked, +/// // read the text and print it in a new dialog. +/// let name = s.find_id::("edit") +/// .unwrap() +/// .get_content() +/// .to_string(); +/// if name.is_empty() { +/// s.add_layer(Dialog::new(TextView::new("Please enter a name!")) +/// .dismiss_button("Ok")); +/// } else { +/// let content = format!("Hello {}!", name); +/// s.pop_layer(); +/// s.add_layer(Dialog::new(TextView::new(&content)) +/// .button("Quit", |s| s.quit())); +/// } +/// })); +/// # } +/// ``` pub struct EditView { /// Current content. content: String, diff --git a/src/view/full_view.rs b/src/view/full_view.rs index 00e2851..322831e 100644 --- a/src/view/full_view.rs +++ b/src/view/full_view.rs @@ -3,6 +3,13 @@ use direction::Orientation; use vec::Vec2; /// Simple wrapper view that asks for all the space it can get. +/// +/// # Examples +/// +/// ``` +/// # use cursive::prelude::*; +/// let view = FullView::new(TextView::new("Big box for little text!")); +/// ``` pub struct FullView { view: T, orientation: Option, diff --git a/src/view/key_event_view.rs b/src/view/key_event_view.rs index e224919..93f62c2 100644 --- a/src/view/key_event_view.rs +++ b/src/view/key_event_view.rs @@ -7,7 +7,16 @@ use super::{View, ViewWrapper}; /// A simple wrapper view that catches some ignored event from its child. /// -/// Events ignored by its child without a callback will stay ignored. +/// If the event doesn't have a corresponding callback, it will stay ignored. +/// +/// # Examples +/// +/// ``` +/// # use cursive::prelude::*; +/// let view = KeyEventView::new(TextView::new("This view has an event!")) +/// .register('q', |s| s.quit()) +/// .register(Key::Esc, |s| s.quit()); +/// ``` pub struct KeyEventView { content: Box, callbacks: HashMap, diff --git a/src/view/mod.rs b/src/view/mod.rs index d57f4da..bfd9448 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -157,7 +157,9 @@ pub trait View { } } -/// Cache around a one-dimensional layout result +/// Cache around a one-dimensional layout result. +/// +/// This is not a View, but something to help you if you create your own Views. #[derive(PartialEq, Debug, Clone, Copy)] pub struct SizeCache { /// Cached value diff --git a/src/view/scroll.rs b/src/view/scroll.rs index f7622f5..13ce7cf 100644 --- a/src/view/scroll.rs +++ b/src/view/scroll.rs @@ -5,6 +5,9 @@ use vec::Vec2; use Printer; /// Provide scrolling functionalities to a view. +/// +/// You're not supposed to use this directly, +/// but it can be helpful if you create your own Views. #[derive(Default)] pub struct ScrollBase { /// First line visible diff --git a/src/view/select_view.rs b/src/view/select_view.rs index 6fd07cd..d6b53ba 100644 --- a/src/view/select_view.rs +++ b/src/view/select_view.rs @@ -35,6 +35,32 @@ impl Item { /// View to select an item among a list. /// /// It contains a list of values of type T, with associated labels. +/// +/// # Examples +/// +/// ``` +/// # extern crate cursive; +/// # use cursive::prelude::*; +/// # use cursive::align::HAlign; +/// # fn main() { +/// 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_select(|s, time| { +/// s.pop_layer(); +/// let text = format!("You will wait for {} minutes...", time); +/// s.add_layer(Dialog::new(TextView::new(&text)) +/// .button("Quit", |s| s.quit())); +/// }); +/// +/// let mut siv = Cursive::new(); +/// siv.add_layer(Dialog::new(time_select) +/// .title("How long is your wait?")); +/// # } +/// +/// ``` pub struct SelectView { items: Vec>, enabled: bool,