Remove prelude module

Add `traits` module instead
This commit is contained in:
Alexandre Bury 2016-09-28 22:45:27 -07:00
parent 51e955808c
commit d684a5bc1e
28 changed files with 161 additions and 126 deletions

View File

@ -23,10 +23,10 @@ version = "5.82.0"
[dev-dependencies]
rand = "0.3"
skeptic = "0.5"
skeptic = "*"
[build-dependencies]
skeptic = "0.5"
skeptic = "*"
[lib]
name = "cursive"

View File

@ -29,7 +29,8 @@ cursive = { git = "https://github.com/gyscos/Cursive" }
```rust,no_run
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, TextView};
fn main() {
// Creates the cursive root - required for every application.

View File

@ -112,8 +112,6 @@ This method takes 2 arguments: a trigger, and a callback.
In the end, we have:
```rust,no_run
# src/main.rs
extern crate cursive;
use cursive::Cursive;

View File

@ -11,7 +11,10 @@ Here is the code we'll end up with:
```rust,no_run
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Button, Dialog, DummyView, EditView,
LinearLayout, SelectView};
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();
@ -98,9 +101,12 @@ let select = BoxView::with_fixed_size((10, 5), SelectView::<String>::new());
But there is another shorter way: the [`Boxable`] trait is conveniently
implemented for any `View`, and allow to wrap in a `BoxView` with a chainable
call:
call. `Boxable`, and a few other useful traits, are conveniently bundled in
the [`traits`] prelude, ready to be imported:
```rust,ignore
use cursive::traits::*;
let select = SelectView::<String>::new()
.fixed_size((10, 5));
```
@ -132,6 +138,7 @@ replace the layer with a simple dialog.
[`SelectView`]: http://gyscos.github.io/Cursive/cursive/views/struct.SelectView.html
[`BoxView`]: http://gyscos.github.io/Cursive/cursive/views/struct.BoxView.html
[`Boxable`]: http://gyscos.github.io/Cursive/cursive/view/trait.Boxable.html
[`traits`]: http://gyscos.github.io/Cursive/cursive/traits/index.html
[`SelectView::on_submit`]: http://gyscos.github.io/Cursive/cursive/views/struct.SelectView.html#method.on_submit
## Linear layouts

View File

@ -1,6 +1,7 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, TextView};
fn main() {
// Creates the cursive root - required for every application.
@ -8,8 +9,8 @@ fn main() {
// Creates a dialog with a single "Quit" button
siv.add_layer(Dialog::new(TextView::new("Hello Dialog!"))
.title("Cursive")
.button("Quit", |s| s.quit()));
.title("Cursive")
.button("Quit", |s| s.quit()));
// Starts the event loop.
siv.run();

View File

@ -1,6 +1,8 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, EditView, TextView};
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();

View File

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

View File

@ -1,7 +1,8 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::event::EventResult;
use cursive::{Cursive, Printer};
use cursive::traits::*;
use cursive::event::{Event, EventResult};
fn main() {
let mut siv = Cursive::new();

View File

@ -1,7 +1,9 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, LinearLayout, TextView};
use cursive::align::HAlign;
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();

View File

@ -1,6 +1,9 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Checkbox, Dialog, EditView, LinearLayout, ListView,
SelectView, TextView};
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();

View File

@ -1,6 +1,8 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::{Cursive, Printer};
use cursive::vec::Vec2;
use cursive::traits::*;
use std::sync::mpsc;
use std::thread;

View File

@ -1,6 +1,7 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, TextView};
use cursive::align::HAlign;
fn main() {

View File

@ -1,42 +1,47 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::menu::MenuTree;
use cursive::views::{Dialog, TextView};
use cursive::event::Key;
use cursive::traits::*;
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("Google it \
yourself!\nKids, \
these days..."))
}))
.leaf("About",
|s| s.add_layer(Dialog::info("Cursive v0.0.0"))));
.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("Google it \
yourself!\nKids, \
these days..."))
}))
.leaf("About",
|s| s.add_layer(Dialog::info("Cursive v0.0.0"))));
// siv.set_autohide_menu(false);

View File

@ -1,7 +1,9 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::view::{Position, Offset};
use cursive::Cursive;
use cursive::views::{Dialog, KeyEventView, TextView};
use cursive::view::{Offset, Position};
use cursive::traits::*;
fn show_popup(siv: &mut Cursive) {
@ -22,15 +24,16 @@ fn show_popup(siv: &mut Cursive) {
fn main() {
let mut siv = Cursive::new();
let content = "Press Q to quit the application.\n\nPress P to open the popup.";
let content = "Press Q to quit the application.\n\nPress P to open the \
popup.";
siv.add_global_callback('q', |s| s.quit());
// Let's wrap the view to give it a recognizable ID, so we can look for it.
// We add the P callback on the textview only (and not globally),
// so that we can't call it when the popup is already visible.
siv.add_layer(KeyEventView::new(IdView::new("text", TextView::new(content)))
.register('p', |s| show_popup(s)));
siv.add_layer(KeyEventView::new(TextView::new(content).with_id("text"))
.register('p', |s| show_popup(s)));
siv.run();

View File

@ -3,8 +3,10 @@ extern crate rand;
use rand::Rng;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Button, Dialog, LinearLayout, ProgressBar, TextView};
use cursive::views::Counter;
use cursive::traits::*;
use std::thread;
use std::cmp::min;
@ -88,8 +90,7 @@ fn phase_2(s: &mut Cursive) {
}
s.pop_layer();
s.add_layer(Dialog::new(linear.full_width())
.title("Just a moment..."));
s.add_layer(Dialog::new(linear.full_width()).title("Just a moment..."));
// And we start the worker thread.
thread::spawn(move || {
@ -119,6 +120,6 @@ fn final_step(s: &mut Cursive) {
.title("Report")
.content(TextView::new("Time travel was a success!\n\
We went forward a few seconds!!")
.center())
.center())
.button("That's it?", |s| s.quit()));
}

View File

@ -1,7 +1,9 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, SelectView, TextView};
use cursive::align::HAlign;
use cursive::traits::*;
fn main() {
let mut select = SelectView::new().h_align(HAlign::Center);

View File

@ -1,6 +1,8 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, SliderView};
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();

View File

@ -1,6 +1,8 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, TextArea};
use cursive::traits::*;
fn main() {
let mut siv = Cursive::new();

View File

@ -1,6 +1,7 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::{Dialog, TextView};
fn main() {
let mut siv = Cursive::new();
@ -10,9 +11,10 @@ fn main() {
// Or you can directly load it from a string for easy deployment.
// siv.load_theme(include_str!("../assets/style.toml")).unwrap();
siv.add_layer(Dialog::new(TextView::new("This application uses a custom theme!"))
.title("Themed dialog")
.button("Quit", |s| s.quit()));
siv.add_layer(Dialog::new(TextView::new("This application uses a \
custom theme!"))
.title("Themed dialog")
.button("Quit", |s| s.quit()));
siv.run();
}

View File

@ -1,25 +1,26 @@
extern crate cursive;
use cursive::prelude::*;
use cursive::Cursive;
use cursive::views::Dialog;
use cursive::theme::BorderStyle;
fn main() {
let mut siv = Cursive::new();
siv.add_layer(Dialog::text("This is a dynamic theme example!")
.button("Change", |s| {
let mut theme = s.current_theme().clone();
.button("Change", |s| {
let mut theme = s.current_theme().clone();
theme.shadow = !theme.shadow;
theme.borders = match theme.borders {
Some(BorderStyle::Simple) => Some(BorderStyle::Outset),
Some(BorderStyle::Outset) => None,
None => Some(BorderStyle::Simple),
};
theme.shadow = !theme.shadow;
theme.borders = match theme.borders {
Some(BorderStyle::Simple) => Some(BorderStyle::Outset),
Some(BorderStyle::Outset) => None,
None => Some(BorderStyle::Simple),
};
s.set_theme(theme);
})
.button("Quit", Cursive::quit));
s.set_theme(theme);
})
.button("Quit", Cursive::quit));
siv.run();
}

View File

@ -33,12 +33,12 @@
//! ```no_run
//! extern crate cursive;
//!
//! use cursive::prelude::*;
//! use cursive::*;
//!
//! fn main() {
//! let mut siv = Cursive::new();
//!
//! siv.add_layer(TextView::new("Hello World!\nPress q to quit."));
//! siv.add_layer(views::TextView::new("Hello World!\nPress q to quit."));
//!
//! siv.add_global_callback('q', |s| s.quit());
//!
@ -80,7 +80,7 @@ macro_rules! new_default(
}
);
pub mod prelude;
pub mod traits;
pub mod event;
#[macro_use]
@ -216,10 +216,12 @@ impl Cursive {
/// ```
/// # extern crate cursive;
/// #
/// # use cursive::prelude::*;
/// # use cursive::{Cursive, event};
/// # use cursive::views::{Dialog};
/// # use cursive::traits::*;
/// # use cursive::menu::*;
/// #
/// # fn main() {
/// let mut siv = Cursive::new();
///
/// siv.menubar()
@ -252,7 +254,7 @@ impl Cursive {
/// .leaf("About",
/// |s| s.add_layer(Dialog::info("Cursive v0.0.0"))));
///
/// siv.add_global_callback(Key::Esc, |s| s.select_menubar());
/// siv.add_global_callback(event::Key::Esc, |s| s.select_menubar());
/// # }
/// ```
pub fn menubar(&mut self) -> &mut views::Menubar {
@ -343,14 +345,16 @@ impl Cursive {
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::{Cursive, views, view};
/// # use cursive::traits::*;
/// # fn main() {
/// let mut siv = Cursive::new();
///
/// siv.add_layer(IdView::new("text", TextView::new("Text #1")));
/// siv.add_layer(views::TextView::new("Text #1")
/// .with_id("text"));
///
/// siv.add_global_callback('p', |s| {
/// s.find::<TextView>(&Selector::Id("text"))
/// s.find::<views::TextView>(&view::Selector::Id("text"))
/// .unwrap()
/// .set_content("Text #2");
/// });
@ -367,14 +371,16 @@ impl Cursive {
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::{Cursive, views};
/// # use cursive::traits::*;
/// # fn main() {
/// let mut siv = Cursive::new();
///
/// siv.add_layer(IdView::new("text", TextView::new("Text #1")));
/// siv.add_layer(views::TextView::new("Text #1")
/// .with_id("text"));
///
/// siv.add_global_callback('p', |s| {
/// s.find_id::<TextView>("text")
/// s.find_id::<views::TextView>("text")
/// .unwrap()
/// .set_content("Text #2");
/// });
@ -392,7 +398,7 @@ impl Cursive {
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::*;
/// # fn main() {
/// let mut siv = Cursive::new();
///
@ -411,11 +417,11 @@ impl Cursive {
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::*;
/// # fn main() {
/// let mut siv = Cursive::new();
///
/// siv.add_layer(TextView::new("Hello world!"));
/// siv.add_layer(views::TextView::new("Hello world!"));
/// # }
/// ```
pub fn add_layer<T: 'static + View>(&mut self, view: T) {

View File

@ -1,27 +0,0 @@
//! Commonly used imports, conveniently grouped.
//!
//! To easily import a bunch of commonly-used definitions, import this bundle:
//!
//! ```
//! use cursive::prelude::*;
//! ```
#[doc(no_inline)]
pub use {Cursive, Printer, With};
#[doc(no_inline)]
pub use event::{Event, Key};
#[doc(no_inline)]
pub use view::{Boxable, Finder, Identifiable, Selector, View};
#[doc(no_inline)]
pub use views::{BoxView, Button, Checkbox, Dialog, DummyView, EditView,
IdView, KeyEventView, LinearLayout, ListView, Panel,
ProgressBar, SelectView, SliderView, TextArea, TextView};
#[doc(no_inline)]
pub use vec::Vec2;
#[doc(no_inline)]
pub use menu::MenuTree;

15
src/traits.rs Normal file
View File

@ -0,0 +1,15 @@
//! Commonly used traits bundled for easy import.
//!
//! This module provides an easy way to import some traits.
//!
//! # Examples
//!
//! ```
//! use cursive::traits::*;
//! ```
#[doc(no_inline)]
pub use With;
#[doc(no_inline)]
pub use view::{Boxable, Finder, Identifiable, View};

View File

@ -15,7 +15,7 @@ use view::View;
/// # Examples
///
/// ```
/// # use cursive::prelude::*;
/// # use cursive::views::Button;
/// let quit_button = Button::new("Quit", |s| s.quit());
/// ```
pub struct Button {

View File

@ -22,7 +22,9 @@ use utils::simple_suffix_length;
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::Cursive;
/// # use cursive::traits::*;
/// # use cursive::views::{Dialog, EditView, TextView};
/// # fn main() {
/// let mut siv = Cursive::new();
///

View File

@ -11,10 +11,11 @@ use view::{View, ViewWrapper};
/// # Examples
///
/// ```
/// # use cursive::prelude::*;
/// # use cursive::event;;
/// # use cursive::views::{KeyEventView, TextView};
/// let view = KeyEventView::new(TextView::new("This view has an event!"))
/// .register('q', |s| s.quit())
/// .register(Key::Esc, |s| s.quit());
/// .register(event::Key::Esc, |s| s.quit());
/// ```
pub struct KeyEventView<T: View> {
content: T,

View File

@ -49,7 +49,7 @@ impl Counter {
/// # Example
///
/// ```
/// # use cursive::prelude::*;
/// # use cursive::views::ProgressBar;
/// let bar = ProgressBar::new()
/// .with_task(|counter| {
/// // This closure is called in parallel.

View File

@ -25,7 +25,8 @@ use unicode_width::UnicodeWidthStr;
///
/// ```
/// # extern crate cursive;
/// # use cursive::prelude::*;
/// # use cursive::Cursive;
/// # use cursive::views::{SelectView, Dialog, TextView};
/// # use cursive::align::HAlign;
/// # fn main() {
/// let mut time_select = SelectView::new().h_align(HAlign::Center);
@ -42,7 +43,7 @@ use unicode_width::UnicodeWidthStr;
///
/// let mut siv = Cursive::new();
/// siv.add_layer(Dialog::new(time_select)
/// .title("How long is your wait?"));
/// .title("How long is your wait?"));
/// # }
///
/// ```