Skip to content

Commit

Permalink
More rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Oct 15, 2017
1 parent 294a410 commit f906218
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 202 deletions.
2 changes: 1 addition & 1 deletion examples/lorem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate cursive;
use cursive::Cursive;
use cursive::align::HAlign;
use cursive::view::Boxable;
use cursive::views::{Dialog, TextView, Panel};
use cursive::views::{Dialog, Panel, TextView};

fn main() {
// Read some long text from a file.
Expand Down
2 changes: 1 addition & 1 deletion examples/menubar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() {
.leaf("About",
|s| s.add_layer(Dialog::info("Cursive v0.0.0"))));

// When `autohide` is on (default), the menu only appears when it is active.
// When `autohide` is on (default), the menu only appears when active.
// Turning it off will leave the menu always visible.

// siv.set_autohide_menu(false);
Expand Down
28 changes: 17 additions & 11 deletions examples/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ use cursive::views::{Dialog, OnEventView, TextView};

fn show_popup(siv: &mut Cursive) {
// Let's center the popup horizontally, but offset it down a few rows
siv.screen_mut()
.add_layer_at(Position::new(Offset::Center, Offset::Parent(3)),
Dialog::around(TextView::new("Tak!"))
.button("Change", |s| {
// Look for a view tagged "text". We _know_ it's there, so unwrap it.
s.call_on_id("text", |view: &mut TextView| {
let content: String = view.get_content().chars().rev().collect();
view.set_content(content);
});
})
.dismiss_button("Ok"));
siv.screen_mut().add_layer_at(
Position::new(Offset::Center, Offset::Parent(3)),
Dialog::around(TextView::new("Tak!"))
.button("Change", |s| {
// Look for a view tagged "text".
// We _know_ it's there, so unwrap it.
s.call_on_id("text", |view: &mut TextView| {
let content = reverse(view.get_content());
view.set_content(content);
});
})
.dismiss_button("Ok"),
);
}

fn reverse(text: &str) -> String {
text.chars().rev().collect()
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions examples/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn main() {

let mut siv = Cursive::new();

// Let's add a BoxView to keep the list at a reasonable size - it can scroll anyway.
// Let's add a BoxView to keep the list at a reasonable size
// (it can scroll anyway).
siv.add_layer(
Dialog::around(select.fixed_size((20, 10)))
.title("Where are you from?"),
Expand All @@ -39,7 +40,8 @@ fn main() {
siv.run();
}

// Let's put the callback in a separate function to keep it clean, but it's not required.
// Let's put the callback in a separate function to keep it clean,
// but it's not required.
fn show_next_window(siv: &mut Cursive, city: &str) {
siv.pop_layer();
let text = format!("{} is a great city!", city);
Expand Down
6 changes: 4 additions & 2 deletions src/backend/curses/n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ impl Concrete {
// Treat '\n' and the numpad Enter the same
10 | ncurses::KEY_ENTER => Event::Key(Key::Enter),
// This is the escape key when pressed by itself.
// When used for control sequences, it should have been caught earlier.
// When used for control sequences,
// it should have been caught earlier.
27 => Event::Key(Key::Esc),
// `Backspace` sends 127, but Ctrl-H sends `Backspace`
127 | ncurses::KEY_BACKSPACE => Event::Key(Key::Backspace),
Expand Down Expand Up @@ -267,7 +268,8 @@ impl Concrete {

impl backend::Backend for Concrete {
fn init() -> Self {
// Change the locale. For some reasons it's mandatory to get some UTF-8 support.
// Change the locale.
// For some reasons it's mandatory to get some UTF-8 support.
ncurses::setlocale(ncurses::LcCategory::all, "");

// The delay is the time ncurses wait after pressing ESC
Expand Down
27 changes: 15 additions & 12 deletions src/cursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,21 @@ impl Cursive {
/// # use cursive::{Cursive, views, view};
/// # use cursive::traits::*;
/// # fn main() {
/// let mut siv = Cursive::new();
///
/// siv.add_layer(views::TextView::new("Text #1")
/// .with_id("text"));
///
/// siv.add_global_callback('p', |s| {
/// s.call_on(&view::Selector::Id("text"), |view: &mut views::TextView| {
/// view.set_content("Text #2");
/// fn main() {
/// let mut siv = Cursive::new();
///
/// siv.add_layer(views::TextView::new("Text #1").with_id("text"));
///
/// siv.add_global_callback('p', |s| {
/// s.call_on(
/// &view::Selector::Id("text"),
/// |view: &mut views::TextView| {
/// view.set_content("Text #2");
/// },
/// );
/// });
/// });
///
/// }
/// # }
/// ```
pub fn call_on<V, F, R>(
Expand Down Expand Up @@ -513,9 +518,7 @@ impl Cursive {
}

if let Event::Mouse {
event,
position,
..
event, position, ..
} = event
{
if event.grabs_focus() && !self.menubar.autohide
Expand Down
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ pub enum Event {
/// An unknown event was received.
Unknown(Vec<u8>),

// Having a doc-hidden event prevents people from having exhaustive matches,
// allowing us to add events in the future.
// Having a doc-hidden event prevents people from having exhaustive
// matches, allowing us to add events in the future.
#[doc(hidden)]
/// The application is about to exit.
Exit,
Expand Down
33 changes: 15 additions & 18 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ pub use self::lines_iterator::{LinesIterator, Row};
pub use self::reader::ProgressReader;

/// The length and width of a part of a string.
pub struct Prefix {
pub struct Span {
/// The length (in bytes) of the string.
pub length: usize,
/// The unicode-width of the string.
pub width: usize,
}

/// Computes the length (number of bytes) and width of a prefix that fits in the given `width`.
/// Computes a prefix that fits in the given `width`.
///
/// Takes non-breakable elements from `iter`, while keeping the
/// string width under `width` (and adding the length of `delimiter`
/// between each element).
/// Takes non-breakable elements from `iter`, while keeping the string width
/// under `width` (and adding `delimiter` between each element).
///
/// Given `total_text = iter.collect().join(delimiter)`, the result
/// is the length of the longest prefix of `width` or less cells,
/// without breaking inside an element.
/// Given `total_text = iter.collect().join(delimiter)`, the result is the
/// length of the longest prefix of `width` or less cells, without breaking
/// inside an element.
///
/// Example:
///
Expand All @@ -42,9 +41,7 @@ pub struct Prefix {
/// prefix(my_text.graphemes(true), 5, "");
/// # }
/// ```
pub fn prefix<'a, I>(
iter: I, available_width: usize, delimiter: &str
) -> Prefix
pub fn prefix<'a, I>(iter: I, available_width: usize, delimiter: &str) -> Span
where
I: Iterator<Item = &'a str>,
{
Expand Down Expand Up @@ -74,38 +71,38 @@ where
// `current_width` includes a delimiter after the last token
debug_assert!(current_width <= available_width + delimiter_width);

Prefix {
Span {
length: length,
width: current_width,
}
}

/// Computes the length (number of bytes) and width of a suffix that fits in the given `width`.
/// Computes the longest suffix that fits in the given `width`.
///
/// Doesn't break inside elements returned by `iter`.
///
/// Returns the number of bytes of the longest
/// suffix from `text` that fits in `width`.
///
/// This is a shortcut for `prefix_length(iter.rev(), width, delimiter)`
pub fn suffix<'a, I>(iter: I, width: usize, delimiter: &str) -> Prefix
pub fn suffix<'a, I>(iter: I, width: usize, delimiter: &str) -> Span
where
I: DoubleEndedIterator<Item = &'a str>,
{
prefix(iter.rev(), width, delimiter)
}

/// Computes a suffix that fits in the given `width`.
/// Computes the longest suffix that fits in the given `width`.
///
/// Breaks between any two graphemes.
pub fn simple_suffix(text: &str, width: usize) -> Prefix {
pub fn simple_suffix(text: &str, width: usize) -> Span {
suffix(text.graphemes(true), width, "")
}

/// Computes the longest prefix of the given text that fits in the given width.
/// Computes the longest prefix that fits in the given width.
///
/// Breaks between any two graphemes.
pub fn simple_prefix(text: &str, width: usize) -> Prefix {
pub fn simple_prefix(text: &str, width: usize) -> Span {
prefix(text.graphemes(true), width, "")
}

Expand Down
3 changes: 2 additions & 1 deletion src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ pub trait View {

/// Runs a closure on the view identified by the given selector.
///
/// See [`Finder::call_on`] for a nicer interface, implemented for all views.
/// See [`Finder::call_on`] for a nicer interface, implemented for all
/// views.
///
/// [`Finder::call_on`]: trait.Finder.html#method.call_on
///
Expand Down
3 changes: 2 additions & 1 deletion src/view/size_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl SizeCache {
/// A compatible request is one where, for each axis, either:
///
/// * the request is equal to the cached size, or
/// * the request is larger than the cached size and the cache is unconstrained
/// * the request is larger than the cached size and the cache is
/// unconstrained
///
/// Notes:
///
Expand Down
48 changes: 28 additions & 20 deletions src/views/edit_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ use utils::{simple_prefix, simple_suffix};
use vec::Vec2;
use view::View;

/// closure type for callbacks when the content is modified. Arguments are the
/// `Cursive`, current content of the input and cursor position
/// Closure type for callbacks when the content is modified.
///
/// Arguments are the `Cursive`, current content of the input and cursor
/// position
pub type OnEdit = Fn(&mut Cursive, &str, usize);

/// closure type for callbacks when Enter is pressed. Arguments are the `Cursive`
/// and the content of the input.
/// Closure type for callbacks when Enter is pressed.
///
/// Arguments are the `Cursive` and the content of the input.
pub type OnSubmit = Fn(&mut Cursive, &str);

/// Input box where the user can enter and edit text.
///
/// # Examples
///
/// From the [edit example].
/// From the [edit example][1].
///
/// [edit example]: https://github.com/gyscos/Cursive/blob/master/examples/edit.rs
/// [1]: https://github.com/gyscos/Cursive/blob/master/examples/edit.rs
///
/// ```no_run
/// # extern crate cursive;
Expand All @@ -39,18 +42,24 @@ pub type OnSubmit = Fn(&mut Cursive, &str);
/// // Create a dialog with an edit text and a button.
/// // The user can either hit the <Ok> button,
/// // or press Enter on the edit text.
/// siv.add_layer(Dialog::new()
/// .title("Enter your name")
/// .padding((1, 1, 1, 0))
/// .content(EditView::new()
/// .on_submit(show_popup)
/// .with_id("name")
/// .fixed_width(20))
/// .button("Ok", |s| {
/// let name = s.call_on_id("name", |view: &mut EditView| view.get_content())
/// .unwrap();
/// show_popup(s, &name);
/// }));
/// siv.add_layer(
/// Dialog::new()
/// .title("Enter your name")
/// .padding((1, 1, 1, 0))
/// .content(
/// EditView::new()
/// .on_submit(show_popup)
/// .with_id("name")
/// .fixed_width(20),
/// )
/// .button("Ok", |s| {
/// let name = s.call_on_id(
/// "name",
/// |view: &mut EditView| view.get_content(),
/// ).unwrap();
/// show_popup(s, &name);
/// }),
/// );
///
/// fn show_popup(s: &mut Cursive, name: &str) {
/// if name.is_empty() {
Expand Down Expand Up @@ -145,8 +154,7 @@ impl EditView {
///
/// ```rust
/// # use cursive::views::EditView;
/// let edit = EditView::new()
/// .filler(" ");
/// let edit = EditView::new().filler(" ");
/// ```
pub fn filler<S: Into<String>>(self, filler: S) -> Self {
self.with(|s| s.set_filler(filler))
Expand Down
7 changes: 6 additions & 1 deletion src/views/linear_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ impl LinearLayout {
};
for (i, (offset, child)) in iterator.enumerate() {
let child_size = child.size.get(self.orientation);
// eprintln!("Offset {:?}, size {:?}, position: {:?}", offset, child_size, position);
// eprintln!(
// "Offset {:?}, size {:?}, position: {:?}",
// offset,
// child_size,
// position
// );
if (offset + child_size > position)
&& child.view.take_focus(direction::Direction::none())
{
Expand Down
Loading

0 comments on commit f906218

Please sign in to comment.