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] [dev-dependencies]
rand = "0.3" rand = "0.3"
skeptic = "0.5" skeptic = "*"
[build-dependencies] [build-dependencies]
skeptic = "0.5" skeptic = "*"
[lib] [lib]
name = "cursive" name = "cursive"

View File

@ -29,7 +29,8 @@ cursive = { git = "https://github.com/gyscos/Cursive" }
```rust,no_run ```rust,no_run
extern crate cursive; extern crate cursive;
use cursive::prelude::*; use cursive::Cursive;
use cursive::views::{Dialog, TextView};
fn main() { fn main() {
// Creates the cursive root - required for every application. // 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: In the end, we have:
```rust,no_run ```rust,no_run
# src/main.rs
extern crate cursive; extern crate cursive;
use cursive::Cursive; use cursive::Cursive;

View File

@ -11,7 +11,10 @@ Here is the code we'll end up with:
```rust,no_run ```rust,no_run
extern crate cursive; extern crate cursive;
use cursive::prelude::*; use cursive::Cursive;
use cursive::views::{Button, Dialog, DummyView, EditView,
LinearLayout, SelectView};
use cursive::traits::*;
fn main() { fn main() {
let mut siv = Cursive::new(); 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 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 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 ```rust,ignore
use cursive::traits::*;
let select = SelectView::<String>::new() let select = SelectView::<String>::new()
.fixed_size((10, 5)); .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 [`SelectView`]: http://gyscos.github.io/Cursive/cursive/views/struct.SelectView.html
[`BoxView`]: http://gyscos.github.io/Cursive/cursive/views/struct.BoxView.html [`BoxView`]: http://gyscos.github.io/Cursive/cursive/views/struct.BoxView.html
[`Boxable`]: http://gyscos.github.io/Cursive/cursive/view/trait.Boxable.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 [`SelectView::on_submit`]: http://gyscos.github.io/Cursive/cursive/views/struct.SelectView.html#method.on_submit
## Linear layouts ## Linear layouts

View File

@ -1,6 +1,7 @@
extern crate cursive; extern crate cursive;
use cursive::prelude::*; use cursive::Cursive;
use cursive::views::{Dialog, TextView};
fn main() { fn main() {
// Creates the cursive root - required for every application. // Creates the cursive root - required for every application.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,10 @@
extern crate cursive; 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() { fn main() {
@ -10,7 +14,8 @@ fn main() {
.add("File", .add("File",
MenuTree::new() MenuTree::new()
.leaf("New", |s| s.add_layer(Dialog::info("New file!"))) .leaf("New", |s| s.add_layer(Dialog::info("New file!")))
.subtree("Recent", MenuTree::new().with(|tree| { .subtree("Recent",
MenuTree::new().with(|tree| {
for i in 1..100 { for i in 1..100 {
tree.add_leaf(&format!("Item {}", i), |_| ()) tree.add_leaf(&format!("Item {}", i), |_| ())
} }

View File

@ -1,7 +1,9 @@
extern crate cursive; extern crate cursive;
use cursive::prelude::*; use cursive::Cursive;
use cursive::view::{Position, Offset}; use cursive::views::{Dialog, KeyEventView, TextView};
use cursive::view::{Offset, Position};
use cursive::traits::*;
fn show_popup(siv: &mut Cursive) { fn show_popup(siv: &mut Cursive) {
@ -22,14 +24,15 @@ fn show_popup(siv: &mut Cursive) {
fn main() { fn main() {
let mut siv = Cursive::new(); 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()); 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. // 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), // 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. // so that we can't call it when the popup is already visible.
siv.add_layer(KeyEventView::new(IdView::new("text", TextView::new(content))) siv.add_layer(KeyEventView::new(TextView::new(content).with_id("text"))
.register('p', |s| show_popup(s))); .register('p', |s| show_popup(s)));

View File

@ -3,8 +3,10 @@ extern crate rand;
use rand::Rng; use rand::Rng;
use cursive::prelude::*; use cursive::Cursive;
use cursive::views::{Button, Dialog, LinearLayout, ProgressBar, TextView};
use cursive::views::Counter; use cursive::views::Counter;
use cursive::traits::*;
use std::thread; use std::thread;
use std::cmp::min; use std::cmp::min;
@ -88,8 +90,7 @@ fn phase_2(s: &mut Cursive) {
} }
s.pop_layer(); s.pop_layer();
s.add_layer(Dialog::new(linear.full_width()) s.add_layer(Dialog::new(linear.full_width()).title("Just a moment..."));
.title("Just a moment..."));
// And we start the worker thread. // And we start the worker thread.
thread::spawn(move || { thread::spawn(move || {

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
extern crate cursive; extern crate cursive;
use cursive::prelude::*; use cursive::Cursive;
use cursive::views::Dialog;
use cursive::theme::BorderStyle; use cursive::theme::BorderStyle;
fn main() { fn main() {

View File

@ -33,12 +33,12 @@
//! ```no_run //! ```no_run
//! extern crate cursive; //! extern crate cursive;
//! //!
//! use cursive::prelude::*; //! use cursive::*;
//! //!
//! fn main() { //! fn main() {
//! let mut siv = Cursive::new(); //! 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()); //! 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; pub mod event;
#[macro_use] #[macro_use]
@ -216,10 +216,12 @@ impl Cursive {
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # /// #
/// # use cursive::prelude::*; /// # use cursive::{Cursive, event};
/// # use cursive::views::{Dialog};
/// # use cursive::traits::*;
/// # use cursive::menu::*;
/// # /// #
/// # fn main() { /// # fn main() {
/// let mut siv = Cursive::new(); /// let mut siv = Cursive::new();
/// ///
/// siv.menubar() /// siv.menubar()
@ -252,7 +254,7 @@ impl Cursive {
/// .leaf("About", /// .leaf("About",
/// |s| s.add_layer(Dialog::info("Cursive v0.0.0")))); /// |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 { pub fn menubar(&mut self) -> &mut views::Menubar {
@ -343,14 +345,16 @@ impl Cursive {
/// ///
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # use cursive::prelude::*; /// # use cursive::{Cursive, views, view};
/// # use cursive::traits::*;
/// # fn main() { /// # fn main() {
/// let mut siv = Cursive::new(); /// 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| { /// siv.add_global_callback('p', |s| {
/// s.find::<TextView>(&Selector::Id("text")) /// s.find::<views::TextView>(&view::Selector::Id("text"))
/// .unwrap() /// .unwrap()
/// .set_content("Text #2"); /// .set_content("Text #2");
/// }); /// });
@ -367,14 +371,16 @@ impl Cursive {
/// ///
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # use cursive::prelude::*; /// # use cursive::{Cursive, views};
/// # use cursive::traits::*;
/// # fn main() { /// # fn main() {
/// let mut siv = Cursive::new(); /// 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| { /// siv.add_global_callback('p', |s| {
/// s.find_id::<TextView>("text") /// s.find_id::<views::TextView>("text")
/// .unwrap() /// .unwrap()
/// .set_content("Text #2"); /// .set_content("Text #2");
/// }); /// });
@ -392,7 +398,7 @@ impl Cursive {
/// ///
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # use cursive::prelude::*; /// # use cursive::*;
/// # fn main() { /// # fn main() {
/// let mut siv = Cursive::new(); /// let mut siv = Cursive::new();
/// ///
@ -411,11 +417,11 @@ impl Cursive {
/// ///
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # use cursive::prelude::*; /// # use cursive::*;
/// # fn main() { /// # fn main() {
/// let mut siv = Cursive::new(); /// 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) { 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 /// # Examples
/// ///
/// ``` /// ```
/// # use cursive::prelude::*; /// # use cursive::views::Button;
/// let quit_button = Button::new("Quit", |s| s.quit()); /// let quit_button = Button::new("Quit", |s| s.quit());
/// ``` /// ```
pub struct Button { pub struct Button {

View File

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

View File

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

View File

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

View File

@ -25,7 +25,8 @@ use unicode_width::UnicodeWidthStr;
/// ///
/// ``` /// ```
/// # extern crate cursive; /// # extern crate cursive;
/// # use cursive::prelude::*; /// # use cursive::Cursive;
/// # use cursive::views::{SelectView, Dialog, TextView};
/// # use cursive::align::HAlign; /// # use cursive::align::HAlign;
/// # fn main() { /// # fn main() {
/// let mut time_select = SelectView::new().h_align(HAlign::Center); /// let mut time_select = SelectView::new().h_align(HAlign::Center);