Add prelude import module

Update examples to use it.
This commit is contained in:
Alexandre Bury 2016-07-20 21:25:14 -07:00
parent 64d176ffa5
commit f28ac264f1
16 changed files with 147 additions and 38 deletions

View File

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

View File

@ -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();

View File

@ -1,7 +1,6 @@
extern crate cursive;
use cursive::Cursive;
use cursive::view::TextView;
use cursive::prelude::*;
fn main() {
let mut siv = Cursive::new();

View File

@ -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();

View File

@ -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() {

View File

@ -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();

View File

@ -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();

View File

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

View File

@ -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() {

View File

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

View File

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

View File

@ -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();

View File

@ -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::<TextView>(&Selector::Id("text"))
/// .unwrap()
/// .set_content("Text #2");
/// });
/// # }
/// ```
pub fn find<V: View + Any>(&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::<TextView>("text")
/// .unwrap()
/// .set_content("Text #2");
/// });
/// # }
/// ```
pub fn find_id<V: View + Any>(&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<F, E: Into<Event>>(&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<T: 'static + View>(&mut self, view: T) {
self.screen_mut().add_layer(view);
}

15
src/prelude.rs Normal file
View File

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

View File

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

View File

@ -235,7 +235,7 @@ impl<T: 'static> View for SelectView<T> {
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;