diff --git a/examples/dialog.rs b/examples/dialog.rs index 00535a0..fc40116 100644 --- a/examples/dialog.rs +++ b/examples/dialog.rs @@ -1,7 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::{TextView, Dialog}; +use cursive::prelude::*; fn main() { // Creates the cursive root - required for every application. diff --git a/examples/edit.rs b/examples/edit.rs index 397d009..8df9e8d 100644 --- a/examples/edit.rs +++ b/examples/edit.rs @@ -1,7 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::{Dialog, EditView, TextView}; +use cursive::prelude::*; fn main() { let mut siv = Cursive::new(); diff --git a/examples/hello_world.rs b/examples/hello_world.rs index dc4b49a..6c00e1b 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,7 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::TextView; +use cursive::prelude::*; fn main() { let mut siv = Cursive::new(); diff --git a/examples/key_codes.rs b/examples/key_codes.rs index 9656fd0..d961c6f 100644 --- a/examples/key_codes.rs +++ b/examples/key_codes.rs @@ -1,10 +1,7 @@ extern crate cursive; -use cursive::Cursive; - -use cursive::view::{View, BoxView}; -use cursive::Printer; -use cursive::event::{EventResult, Event}; +use cursive::prelude::*; +use cursive::event::EventResult; fn main() { let mut siv = Cursive::new(); diff --git a/examples/linear.rs b/examples/linear.rs index 63ea903..645a708 100644 --- a/examples/linear.rs +++ b/examples/linear.rs @@ -1,7 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::{BoxView, Dialog, LinearLayout, TextView}; +use cursive::prelude::*; use cursive::align::HAlign; fn main() { diff --git a/examples/list_view.rs b/examples/list_view.rs index 28527da..eb36d94 100644 --- a/examples/list_view.rs +++ b/examples/list_view.rs @@ -1,9 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::With; -use cursive::view::{Checkbox, Dialog, EditView, LinearLayout, ListView, - TextView, SelectView}; +use cursive::prelude::*; fn main() { let mut siv = Cursive::new(); diff --git a/examples/logs.rs b/examples/logs.rs index 2ab1249..f39337c 100644 --- a/examples/logs.rs +++ b/examples/logs.rs @@ -1,14 +1,11 @@ extern crate cursive; +use cursive::prelude::*; + use std::sync::mpsc; use std::thread; use std::time::Duration; -use cursive::Cursive; -use cursive::vec::Vec2; -use cursive::Printer; -use cursive::view::{View, FullView}; - fn main() { // As usual, create the Cursive root let mut siv = Cursive::new(); diff --git a/examples/lorem.rs b/examples/lorem.rs index da594dd..078fb0b 100644 --- a/examples/lorem.rs +++ b/examples/lorem.rs @@ -1,8 +1,7 @@ extern crate cursive; -use cursive::Cursive; +use cursive::prelude::*; use cursive::align::HAlign; -use cursive::view::{TextView, Dialog}; fn main() { // Read some long text from a file. diff --git a/examples/menubar.rs b/examples/menubar.rs index 001f240..658de2f 100644 --- a/examples/menubar.rs +++ b/examples/menubar.rs @@ -1,11 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::With; -use cursive::menu::MenuTree; -use cursive::view::Dialog; -use cursive::view::TextView; -use cursive::event::Key; +use cursive::prelude::*; fn main() { diff --git a/examples/mutation.rs b/examples/mutation.rs index 91f912d..65fac41 100644 --- a/examples/mutation.rs +++ b/examples/mutation.rs @@ -1,7 +1,7 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::{IdView, TextView, Dialog, KeyEventView, Position, Offset}; +use cursive::prelude::*; +use cursive::view::{Position, Offset}; fn show_popup(siv: &mut Cursive) { diff --git a/examples/select.rs b/examples/select.rs index b8397d6..7551d03 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -1,8 +1,7 @@ extern crate cursive; -use cursive::Cursive; +use cursive::prelude::*; use cursive::align::HAlign; -use cursive::view::{Dialog, SelectView, TextView, BoxView}; fn main() { let mut select = SelectView::new().h_align(HAlign::Center); diff --git a/examples/theme.rs b/examples/theme.rs index 1028cee..279542c 100644 --- a/examples/theme.rs +++ b/examples/theme.rs @@ -1,7 +1,6 @@ extern crate cursive; -use cursive::Cursive; -use cursive::view::{Dialog, TextView}; +use cursive::prelude::*; fn main() { let mut siv = Cursive::new(); diff --git a/src/lib.rs b/src/lib.rs index 28c5ff7..68561b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,7 @@ macro_rules! new_default( } ); +pub mod prelude; pub mod event; pub mod view; @@ -171,6 +172,58 @@ impl Cursive { } /// Retrieve the menu tree used by the menubar. + /// + /// This allows to add menu items to the menubar. + /// + /// # Examples + /// + /// ``` + /// # extern crate cursive; + /// # + /// # use cursive::Cursive; + /// # use cursive::With; + /// # use cursive::menu::MenuTree; + /// # use cursive::view::Dialog; + /// # use cursive::view::TextView; + /// # use cursive::event::Key; + /// # + /// # fn main() { + + /// let mut siv = Cursive::new(); + /// + /// siv.menubar() + /// .add("File", + /// MenuTree::new() + /// .leaf("New", |s| s.add_layer(Dialog::info("New file!"))) + /// .subtree("Recent", MenuTree::new().with(|tree| { + /// for i in 1..100 { + /// tree.add_leaf(&format!("Item {}", i), |_| ()) + /// } + /// })) + /// .delimiter() + /// .with(|tree| { + /// for i in 1..10 { + /// tree.add_leaf(&format!("Option {}", i), |_| ()); + /// } + /// }) + /// .delimiter() + /// .leaf("Quit", |s| s.quit())) + /// .add("Help", + /// MenuTree::new() + /// .subtree("Help", + /// MenuTree::new() + /// .leaf("General", |s| { + /// s.add_layer(Dialog::info("Help message!")) + /// }) + /// .leaf("Online", |s| { + /// s.add_layer(Dialog::info("Online help?")) + /// })) + /// .leaf("About", + /// |s| s.add_layer(Dialog::info("Cursive v0.0.0")))); + /// + /// siv.add_global_callback(Key::Esc, |s| s.select_menubar()); + /// # } + /// ``` pub fn menubar(&mut self) -> &mut view::Menubar { &mut self.menubar } @@ -243,8 +296,27 @@ impl Cursive { } /// Tries to find the view pointed to by the given path. + /// /// If the view is not found, or if it is not of the asked type, /// it returns None. + /// + /// # Examples + /// + /// ``` + /// # extern crate cursive; + /// # use cursive::prelude::*; + /// # fn main() { + /// let mut siv = Cursive::new(); + /// + /// siv.add_layer(IdView::new("text", TextView::new("Text #1"))); + /// + /// siv.add_global_callback('p', |s| { + /// s.find::(&Selector::Id("text")) + /// .unwrap() + /// .set_content("Text #2"); + /// }); + /// # } + /// ``` pub fn find(&mut self, sel: &Selector) -> Option<&mut V> { match self.find_any(sel) { None => None, @@ -253,6 +325,24 @@ impl Cursive { } /// Convenient method to use `find` with a `Selector::Id`. + /// + /// # Examples + /// + /// ``` + /// # extern crate cursive; + /// # use cursive::prelude::*; + /// # fn main() { + /// let mut siv = Cursive::new(); + /// + /// siv.add_layer(IdView::new("text", TextView::new("Text #1"))); + /// + /// siv.add_global_callback('p', |s| { + /// s.find_id::("text") + /// .unwrap() + /// .set_content("Text #2"); + /// }); + /// # } + /// ``` pub fn find_id(&mut self, id: &str) -> Option<&mut V> { self.find(&Selector::Id(id)) } @@ -260,6 +350,18 @@ impl Cursive { /// Adds a global callback. /// /// Will be triggered on the given key press when no view catches it. + /// + /// # Examples + /// + /// ``` + /// # extern crate cursive; + /// # use cursive::prelude::*; + /// # fn main() { + /// let mut siv = Cursive::new(); + /// + /// siv.add_global_callback('q', |s| s.quit()); + /// # } + /// ``` pub fn add_global_callback>(&mut self, event: E, cb: F) where F: Fn(&mut Cursive) + 'static { @@ -267,6 +369,18 @@ impl Cursive { } /// Convenient method to add a layer to the current screen. + /// + /// # Examples + /// + /// ``` + /// # extern crate cursive; + /// # use cursive::prelude::*; + /// # fn main() { + /// let mut siv = Cursive::new(); + /// + /// siv.add_layer(TextView::new("Hello world!")); + /// # } + /// ``` pub fn add_layer(&mut self, view: T) { self.screen_mut().add_layer(view); } diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..3e205d1 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,15 @@ +//! Commonly used imports, conveniently grouped. +//! +//! To easily import a bunch of commonly-used definitions, import this bundle: +//! +//! ``` +//! use cursive::prelude::*; +//! ``` + +pub use {Cursive, Printer, With}; +pub use event::{Event, Key}; +pub use view::{BoxView, Checkbox, Dialog, EditView, FullView, IdView, + KeyEventView, LinearLayout, ListView, SelectView, Selector, + TextView, View}; +pub use vec::Vec2; +pub use menu::MenuTree; diff --git a/src/view/menubar.rs b/src/view/menubar.rs index 4571264..6207389 100644 --- a/src/view/menubar.rs +++ b/src/view/menubar.rs @@ -33,7 +33,8 @@ enum State { /// Shows a single-line list of items, with pop-up menus when one is selected. /// -/// The [`Cursive`] root already includes a menubar that you just need to configure. +/// The [`Cursive`] root already includes a menubar +/// that you just need to configure. /// /// [`Cursive`]: ../struct.Cursive.html#method.menubar pub struct Menubar { diff --git a/src/view/select_view.rs b/src/view/select_view.rs index 9b6d523..6fd07cd 100644 --- a/src/view/select_view.rs +++ b/src/view/select_view.rs @@ -235,7 +235,7 @@ impl View for SelectView { printer.print_hline((1, 0), x - 1, " "); // Draw the borders printer.print((0, 0), "<"); - printer.print((x-1, 0), ">"); + printer.print((x - 1, 0), ">"); let label = &self.items[self.focus()].label;