Add examples to documentation

This commit is contained in:
Alexandre Bury 2016-07-20 22:08:06 -07:00
parent 536321ebda
commit 0a8228d073
7 changed files with 94 additions and 3 deletions

View File

@ -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 <Enter> 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,

View File

@ -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::<EditView>("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,

View File

@ -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<T: View> {
view: T,
orientation: Option<Orientation>,

View File

@ -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<View>,
callbacks: HashMap<Event, Callback>,

View File

@ -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

View File

@ -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

View File

@ -35,6 +35,32 @@ impl<T> Item<T> {
/// 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<T = String> {
items: Vec<Item<T>>,
enabled: bool,