diff --git a/examples/lorem.rs b/examples/lorem.rs index 638c68710..dd9898f04 100644 --- a/examples/lorem.rs +++ b/examples/lorem.rs @@ -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. diff --git a/examples/menubar.rs b/examples/menubar.rs index 2f506551c..9f320c7f6 100644 --- a/examples/menubar.rs +++ b/examples/menubar.rs @@ -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); diff --git a/examples/mutation.rs b/examples/mutation.rs index 593c4d742..3c705a485 100644 --- a/examples/mutation.rs +++ b/examples/mutation.rs @@ -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() { diff --git a/examples/select.rs b/examples/select.rs index c53fe0bd0..f011573b5 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -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?"), @@ -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); diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 3252d1bde..542cd24ce 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -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), @@ -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 diff --git a/src/cursive.rs b/src/cursive.rs index 9676a1e58..a1eec45e5 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -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( @@ -513,9 +518,7 @@ impl Cursive { } if let Event::Mouse { - event, - position, - .. + event, position, .. } = event { if event.grabs_focus() && !self.menubar.autohide diff --git a/src/event.rs b/src/event.rs index ee57a79c3..4ac63b95b 100644 --- a/src/event.rs +++ b/src/event.rs @@ -303,8 +303,8 @@ pub enum Event { /// An unknown event was received. Unknown(Vec), - // 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, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c2679448c..11f9d9a9d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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: /// @@ -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, { @@ -74,13 +71,13 @@ 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`. /// @@ -88,24 +85,24 @@ where /// 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, { 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, "") } diff --git a/src/view/mod.rs b/src/view/mod.rs index 3867f0de0..d9a1c9947 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -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 /// diff --git a/src/view/size_cache.rs b/src/view/size_cache.rs index bef7c215d..021a6b346 100644 --- a/src/view/size_cache.rs +++ b/src/view/size_cache.rs @@ -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: /// diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index 584dab955..06e1a949a 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -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; @@ -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 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() { @@ -145,8 +154,7 @@ impl EditView { /// /// ```rust /// # use cursive::views::EditView; - /// let edit = EditView::new() - /// .filler(" "); + /// let edit = EditView::new().filler(" "); /// ``` pub fn filler>(self, filler: S) -> Self { self.with(|s| s.set_filler(filler)) diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 26a67faef..4c1d42906 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -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()) { diff --git a/src/views/list_view.rs b/src/views/list_view.rs index 1ed4877e8..08ef85a9c 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -2,7 +2,7 @@ use Cursive; use Printer; use With; use direction; -use event::{Callback, Event, EventResult, Key, MouseEvent, MouseButton}; +use event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent}; use std::any::Any; use std::rc::Rc; use unicode_width::UnicodeWidthStr; @@ -96,10 +96,8 @@ impl ListView { /// Adds a view to the end of the list. pub fn add_child(&mut self, label: &str, mut view: V) { view.take_focus(direction::Direction::none()); - self.children.push(ListChild::Row( - label.to_string(), - Box::new(view), - )); + self.children + .push(ListChild::Row(label.to_string(), Box::new(view))); } /// Removes all children from this view. @@ -152,8 +150,9 @@ impl ListView { self.focus } - fn iter_mut<'a>(&'a mut self, from_focus: bool, source: direction::Relative) - -> Box + 'a> { + fn iter_mut<'a>( + &'a mut self, from_focus: bool, source: direction::Relative + ) -> Box + 'a> { match source { direction::Relative::Front => { let start = if from_focus { self.focus } else { 0 }; @@ -171,8 +170,9 @@ impl ListView { } } - fn move_focus(&mut self, n: usize, source: direction::Direction) - -> EventResult { + fn move_focus( + &mut self, n: usize, source: direction::Direction + ) -> EventResult { let i = if let Some(i) = source .relative(direction::Orientation::Vertical) .and_then(|rel| { @@ -183,8 +183,7 @@ impl ListView { .filter_map(|p| try_focus(p, source)) .take(n) .last() - }) - { + }) { i } else { return EventResult::Ignored; @@ -241,17 +240,16 @@ impl ListView { } } -fn try_focus((i, child): (usize, &mut ListChild), source: direction::Direction) - -> Option { +fn try_focus( + (i, child): (usize, &mut ListChild), source: direction::Direction +) -> Option { match *child { ListChild::Delimiter => None, - ListChild::Row(_, ref mut view) => { - if view.take_focus(source) { - Some(i) - } else { - None - } - } + ListChild::Row(_, ref mut view) => if view.take_focus(source) { + Some(i) + } else { + None + }, } } @@ -264,16 +262,14 @@ impl View for ListView { let offset = self.labels_width() + 1; debug!("Offset: {}", offset); - self.scrollbase.draw( - printer, - |printer, i| match self.children[i] { + self.scrollbase + .draw(printer, |printer, i| match self.children[i] { ListChild::Row(ref label, ref view) => { printer.print((0, 0), label); view.draw(&printer.offset((offset, 0), i == self.focus)); } ListChild::Delimiter => (), - }, - ); + }); } fn required_size(&mut self, req: Vec2) -> Vec2 { @@ -314,9 +310,8 @@ impl View for ListView { let spacing = 1; let scrollbar_width = if self.children.len() > size.y { 2 } else { 0 }; - let available = size.x.saturating_sub( - label_width + spacing + scrollbar_width, - ); + let available = size.x + .saturating_sub(label_width + spacing + scrollbar_width); debug!("Available: {}", available); @@ -336,33 +331,35 @@ impl View for ListView { event: MouseEvent::Press(MouseButton::Left), position, offset, - } - if position - .checked_sub(offset) - .map(|position| { + } if position + .checked_sub(offset) + .map(|position| { self.scrollbase.start_drag(position, self.last_size.x) }) - .unwrap_or(false) => { + .unwrap_or(false) => + { return EventResult::Consumed(None); } Event::Mouse { event: MouseEvent::Hold(MouseButton::Left), position, offset, - } if self.scrollbase.is_dragging() => { - position.checked_sub(offset).map(|position| { - self.scrollbase.drag(position) - }); + } if self.scrollbase.is_dragging() => + { + position + .checked_sub(offset) + .map(|position| self.scrollbase.drag(position)); return EventResult::Consumed(None); } Event::Mouse { - event: MouseEvent::Release(MouseButton::Left), .. - } if self.scrollbase.is_dragging() => { + event: MouseEvent::Release(MouseButton::Left), + .. + } if self.scrollbase.is_dragging() => + { self.scrollbase.release_grab(); return EventResult::Consumed(None); } _ => (), - } @@ -394,15 +391,13 @@ impl View for ListView { Event::Key(Key::PageDown) => { self.move_focus(10, direction::Direction::up()) } - Event::Key(Key::Home) | - Event::Ctrl(Key::Home) => { + Event::Key(Key::Home) | Event::Ctrl(Key::Home) => { self.move_focus( usize::max_value(), direction::Direction::back(), ) } - Event::Key(Key::End) | - Event::Ctrl(Key::End) => { + Event::Key(Key::End) | Event::Ctrl(Key::End) => { self.move_focus( usize::max_value(), direction::Direction::front(), @@ -414,13 +409,19 @@ impl View for ListView { Event::Shift(Key::Tab) => { self.move_focus(1, direction::Direction::back()) } - Event::Mouse { event: MouseEvent::WheelDown, .. } - if self.scrollbase.can_scroll_down() => { + Event::Mouse { + event: MouseEvent::WheelDown, + .. + } if self.scrollbase.can_scroll_down() => + { self.scrollbase.scroll_down(5); EventResult::Consumed(None) } - Event::Mouse { event: MouseEvent::WheelUp, .. } - if self.scrollbase.can_scroll_up() => { + Event::Mouse { + event: MouseEvent::WheelUp, + .. + } if self.scrollbase.can_scroll_up() => + { self.scrollbase.scroll_up(5); EventResult::Consumed(None) } @@ -430,24 +431,26 @@ impl View for ListView { fn take_focus(&mut self, source: direction::Direction) -> bool { let rel = source.relative(direction::Orientation::Vertical); - let i = - if let Some(i) = self.iter_mut( - rel.is_none(), - rel.unwrap_or(direction::Relative::Front), - ).filter_map(|p| try_focus(p, source)) - .next() - { - i - } else { - // No one wants to be in focus - return false; - }; + let i = if let Some(i) = self.iter_mut( + rel.is_none(), + rel.unwrap_or(direction::Relative::Front), + ).filter_map(|p| try_focus(p, source)) + .next() + { + i + } else { + // No one wants to be in focus + return false; + }; self.focus = i; self.scrollbase.scroll_to(self.focus); true } - fn call_on_any<'a>(&mut self, selector: &Selector, mut callback: Box) { + fn call_on_any<'a>( + &mut self, selector: &Selector, + mut callback: Box, + ) { for view in self.children.iter_mut().filter_map(ListChild::view) { view.call_on_any(selector, Box::new(|any| callback(any))); } diff --git a/src/views/on_event_view.rs b/src/views/on_event_view.rs index ac83548bf..657902df2 100644 --- a/src/views/on_event_view.rs +++ b/src/views/on_event_view.rs @@ -81,8 +81,10 @@ impl OnEventView { /// Registers a callback when the given event is received. /// /// The given callback will be run before the child view sees the event. - /// If the result is `None`, then the child view is given the event as usual. - /// Otherwise, it bypasses the child view and directly processes the result. + /// * If the result is `None`, then the child view is given the event as + /// usual. + /// * Otherwise, it bypasses the child view and directly processes the + /// result. /// /// Chainable variant. pub fn on_pre_event_inner(self, event: E, cb: F) -> Self @@ -139,8 +141,10 @@ impl OnEventView { /// Registers a callback when the given event is received. /// /// The given callback will be run before the child view sees the event. - /// If the result is `None`, then the child view is given the event as usual. - /// Otherwise, it bypasses the child view and directly processes the result. + /// * If the result is `None`, then the child view is given the event as + /// usual. + /// * Otherwise, it bypasses the child view and directly processes the + /// result. pub fn set_on_pre_event_inner(&mut self, event: E, cb: F) where E: Into, diff --git a/src/views/progress_bar.rs b/src/views/progress_bar.rs index 848c7f205..ea7fde916 100644 --- a/src/views/progress_bar.rs +++ b/src/views/progress_bar.rs @@ -140,7 +140,7 @@ impl ProgressBar { /// The default one shows a percentage progress: /// /// ``` - /// fn make_percentage(value: usize, (min, max): (usize, usize)) -> String { + /// fn make_progress(value: usize, (min, max): (usize, usize)) -> String { /// let percent = 101 * (value - min) / (1 + max - min); /// format!("{} %", percent) /// } diff --git a/src/views/select_view.rs b/src/views/select_view.rs index d7d918b3f..19424f369 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -339,13 +339,19 @@ impl SelectView { Event::Key(Key::End) => { self.focus.set(self.items.len().saturating_sub(1)) } - Event::Mouse { event: MouseEvent::WheelDown, .. } - if self.scrollbase.can_scroll_down() => { + Event::Mouse { + event: MouseEvent::WheelDown, + .. + } if self.scrollbase.can_scroll_down() => + { fix_scroll = false; self.scrollbase.scroll_down(5); } - Event::Mouse { event: MouseEvent::WheelUp, .. } - if self.scrollbase.can_scroll_up() => { + Event::Mouse { + event: MouseEvent::WheelUp, + .. + } if self.scrollbase.can_scroll_up() => + { fix_scroll = false; self.scrollbase.scroll_up(5); } @@ -353,13 +359,13 @@ impl SelectView { event: MouseEvent::Press(MouseButton::Left), position, offset, - } - if position - .checked_sub(offset) - .map(|position| { + } if position + .checked_sub(offset) + .map(|position| { self.scrollbase.start_drag(position, self.last_size.x) }) - .unwrap_or(false) => { + .unwrap_or(false) => + { fix_scroll = false; } Event::Mouse { @@ -369,31 +375,27 @@ impl SelectView { } => { // If the mouse is dragged, we always consume the event. fix_scroll = false; - position.checked_sub(offset).map(|position| { - self.scrollbase.drag(position) - }); + position + .checked_sub(offset) + .map(|position| self.scrollbase.drag(position)); } Event::Mouse { event: MouseEvent::Press(_), position, offset, - } => { - if let Some(position) = position.checked_sub(offset) { - let scrollbar_size = if self.scrollbase.scrollable() { - (2, 0) - } else { - (0, 0) - }; - let clickable_size = - self.last_size.saturating_sub(scrollbar_size); - if position < clickable_size { - fix_scroll = false; - self.focus.set( - position.y + self.scrollbase.start_line, - ); - } + } => if let Some(position) = position.checked_sub(offset) { + let scrollbar_size = if self.scrollbase.scrollable() { + (2, 0) + } else { + (0, 0) + }; + let clickable_size = + self.last_size.saturating_sub(scrollbar_size); + if position < clickable_size { + fix_scroll = false; + self.focus.set(position.y + self.scrollbase.start_line); } - } + }, Event::Mouse { event: MouseEvent::Release(MouseButton::Left), position, @@ -410,9 +412,9 @@ impl SelectView { }; let clickable_size = self.last_size.saturating_sub(scrollbar_size); - if position < clickable_size && - (position.y + self.scrollbase.start_line) == - self.focus() + if position < clickable_size + && (position.y + self.scrollbase.start_line) + == self.focus() { return self.submit(); } @@ -429,12 +431,9 @@ impl SelectView { // the list when we reach the end. // This is achieved by chaining twice the iterator let iter = self.items.iter().chain(self.items.iter()); - if let Some((i, _)) = - iter.enumerate().skip(self.focus() + 1).find( - |&(_, item)| { - item.label.starts_with(c) - }, - ) + if let Some((i, _)) = iter.enumerate() + .skip(self.focus() + 1) + .find(|&(_, item)| item.label.starts_with(c)) { // Apply modulo in case we have a hit // from the chained iterator @@ -516,7 +515,8 @@ impl SelectView { event: MouseEvent::Release(MouseButton::Left), position, offset, - } if position.fits_in_rect(offset, self.last_size) => { + } if position.fits_in_rect(offset, self.last_size) => + { self.open_popup() } _ => EventResult::Ignored, @@ -605,16 +605,15 @@ impl View for SelectView { &printer.sub_printer(Vec2::new(0, offset), printer.size, true); self.scrollbase.draw(printer, |printer, i| { - printer.with_selection( - i == self.focus(), - |printer| if i != self.focus() && !self.enabled { + printer.with_selection(i == self.focus(), |printer| { + if i != self.focus() && !self.enabled { printer.with_color(ColorStyle::Secondary, |printer| { self.draw_item(printer, i) }); } else { self.draw_item(printer, i); - }, - ); + } + }); }); } } diff --git a/src/views/text_area.rs b/src/views/text_area.rs index 4179aad6b..2d3ce05ac 100644 --- a/src/views/text_area.rs +++ b/src/views/text_area.rs @@ -1,8 +1,8 @@ -use std::cmp::min; use {Printer, With, XY}; use direction::Direction; -use event::{Event, EventResult, Key, MouseEvent, MouseButton}; +use event::{Event, EventResult, Key, MouseButton, MouseEvent}; use odds::vec::VecExt; +use std::cmp::min; use theme::{ColorStyle, Effect}; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; @@ -180,10 +180,11 @@ impl TextArea { } } - // If we are editing the text, we add a fake "space" character for the cursor to indicate - // where the next character will appear. - // If the current line is full, adding a character will overflow into the next line. To - // show that, we need to add a fake "ghost" row, just for the cursor. + // If we are editing the text, we add a fake "space" character for the + // cursor to indicate where the next character will appear. + // If the current line is full, adding a character will overflow into the + // next line. To show that, we need to add a fake "ghost" row, just for + // the cursor. fn fix_ghost_row(&mut self) { if self.rows.is_empty() || self.rows.last().unwrap().end != self.content.len() @@ -198,7 +199,6 @@ impl TextArea { } fn soft_compute_rows(&mut self, size: Vec2) { - if self.is_cache_valid(size) { return; } @@ -297,7 +297,7 @@ impl TextArea { /// The only damages are assumed to have occured around the cursor. fn fix_damages(&mut self) { if self.size_cache.is_none() { - // If we don't know our size, it means we'll get a layout command soon. + // If we don't know our size, we'll get a layout command soon. // So no need to do that here. return; } @@ -470,14 +470,16 @@ impl View for TextArea { Event::Mouse { event: MouseEvent::WheelUp, .. - } if self.scrollbase.can_scroll_up() => { + } if self.scrollbase.can_scroll_up() => + { fix_scroll = false; self.scrollbase.scroll_up(5); } Event::Mouse { event: MouseEvent::WheelDown, .. - } if self.scrollbase.can_scroll_down() => { + } if self.scrollbase.can_scroll_down() => + { fix_scroll = false; self.scrollbase.scroll_down(5); } @@ -485,15 +487,19 @@ impl View for TextArea { event: MouseEvent::Press(MouseButton::Left), position, offset, - } if position.checked_sub(offset).map(|position| { - self.scrollbase.start_drag(position, self.last_size.x) - }).unwrap_or(false) => { + } if position + .checked_sub(offset) + .map(|position| { + self.scrollbase.start_drag(position, self.last_size.x) + }) + .unwrap_or(false) => + { fix_scroll = false; } Event::Mouse { event: MouseEvent::Hold(MouseButton::Left), position, - offset + offset, } => { fix_scroll = false; position @@ -504,10 +510,7 @@ impl View for TextArea { event: MouseEvent::Press(_), position, offset, - } if position.fits_in_rect( - offset, - self.last_size, - ) => + } if position.fits_in_rect(offset, self.last_size) => { position.checked_sub(offset).map(|position| { if self.rows.is_empty() { diff --git a/src/views/text_view.rs b/src/views/text_view.rs index 0098be37b..a02e564c5 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -205,7 +205,8 @@ impl TextView { if self.rows.is_empty() && !self.content.is_empty() { // We have some content, we we didn't find any row for it? // This probably means we couldn't even make a single row - // (for instance we only have 1 column and we have a wide character). + // (for instance we only have 1 column and we have a wide + // character). return; } } @@ -258,7 +259,7 @@ impl View for TextView { return EventResult::Ignored; } - // We know we are scrollable, otherwise the event would just be ignored. + // We have a scrollbar, otherwise the event would just be ignored. match event { Event::Key(Key::Home) => self.scrollbase.scroll_top(), Event::Key(Key::End) => self.scrollbase.scroll_bottom(), @@ -289,7 +290,7 @@ impl View for TextView { } if position .checked_sub(offset) .map(|position| { - self.scrollbase.start_drag(position, self.last_size.x) + self.scrollbase.start_drag(position, self.last_size.x) }) .unwrap_or(false) => {