From 58bd274df0360e83c53a5fbaea21e5f0180d8bbe Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 25 Jun 2016 16:36:22 -0700 Subject: [PATCH] Update Readme & run rustfmt --- Readme.md | 12 ++--- examples/mutation.rs | 18 +++---- src/event.rs | 102 +++++++++++++++++++------------------ src/theme.rs | 34 ++++++++----- src/utf8.rs | 2 +- src/vec.rs | 18 +++---- src/view/box_view.rs | 5 +- src/view/button.rs | 1 - src/view/dialog.rs | 97 +++++++++++++++++++---------------- src/view/edit_view.rs | 30 ++++++----- src/view/full_view.rs | 4 +- src/view/id_view.rs | 2 +- src/view/key_event_view.rs | 12 ++--- src/view/linear_layout.rs | 62 +++++++++++----------- src/view/mod.rs | 3 +- src/view/select_view.rs | 12 ++--- src/view/shadow_view.rs | 5 +- src/view/sized_view.rs | 2 +- src/view/text_view.rs | 15 +++--- src/view/view_path.rs | 2 +- src/view/view_wrapper.rs | 2 +- 21 files changed, 228 insertions(+), 212 deletions(-) diff --git a/Readme.md b/Readme.md index c23a3c7..66c81cb 100644 --- a/Readme.md +++ b/Readme.md @@ -59,8 +59,6 @@ _(Colors may depend on your terminal configuration.)_ First off, terminals are messy. A small set of features is standard, but beyond that, almost every terminal has its own implementation. -I mostly test VTE-based terminals (Gnome & Xfce), with the occasional Konsole and xterm checks. - ### Output * **Colors**: the basic 8-colors palette should be broadly supported. User-defined colors is not supported in the raw linux TTY, but should work in most terminals, although it's still kinda experimental. @@ -73,7 +71,7 @@ Also, Cursive currently expects every codepoint to be a one-column character, so * Keep in mind that if the terminal has shortcuts registered, they probably won't be transmitted to the app. * UTF-8 input should work fine in a unicode-enabled terminal emulator, but raw linux TTY may be more capricious. -Here is the support table for input keys (All means Linux TTY and terminal emulators): +Here is the support table for input keys. Tested terminals are mostly Gnome terminal and Linux TTY, xterm, and a few others now and then. | | Key | Shift+Key | Ctrl+Key | Shift+Ctrl+Key | |--------------------------|:----:|:----------------------:|:--------------------------:|:---------------:| @@ -86,9 +84,10 @@ Here is the support table for input keys (All means Linux TTY and terminal emula | Ins | All | None (paste clipboard) | Xterm | None | | Del | All | VTE+Xterm | VTE+Xterm | VTE+Xterm | | Home, End | All | Xterm | Xterm | Xterm | -| PageUp, PageDown | All | None | Xterm | None | -| Fn keys: F1-F4 | All | None (WIP) | None (WIP) | None (WIP) | -| Fn keys: F5-F12 | All | VTE+Xterm (WIP) | VTE+Xterm (WIP) | VTE+Xterm (WIP) | +| PageUp, PageDown | All | All | All | None | +| Fn keys: F1-F4 | All | All except Konsole | Gnome+XTerm | Gnome+Xterm | +| Fn keys: F5-F8 | All | All | All except TTY | All except TTY | +| Fn keys: F9-F12 | All | All except TTY | All except TTY | All except TTY | | PrtScn, ScrollLock | None | None | None | None | | Window, Menu | None | None | None | None | @@ -97,5 +96,6 @@ Here is the support table for input keys (All means Linux TTY and terminal emula You want to help? Great! Here is a non-exhaustive list of things you could do: * Provide example use-case: a good idea of application for existing or new components. +* Check compatibility: run the `key_codes` example on your favorite terminal, and report the results! * Test and reports issues: a bug won't get fixed if we don't know it's there. * Hack the code! If you feel confident with rust, pick an issue you like and hack away! diff --git a/examples/mutation.rs b/examples/mutation.rs index ff5010c..8f7f58a 100644 --- a/examples/mutation.rs +++ b/examples/mutation.rs @@ -1,18 +1,18 @@ extern crate cursive; use cursive::Cursive; -use cursive::view::{IdView,TextView,Dialog,KeyEventView}; +use cursive::view::{IdView, TextView, Dialog, KeyEventView}; fn show_popup(siv: &mut Cursive) { siv.add_layer(Dialog::new(TextView::new("Tak!")) - .button("Change", |s| { - // Look for a view tagged "text". We _know_ it's there, so unwrap it. - let view = s.find_id::("text").unwrap(); - let content: String = view.get_content().chars().rev().collect(); - view.set_content(&content); - }) - .dismiss_button("Ok")); + .button("Change", |s| { + // Look for a view tagged "text". We _know_ it's there, so unwrap it. + let view = s.find_id::("text").unwrap(); + let content: String = view.get_content().chars().rev().collect(); + view.set_content(&content); + }) + .dismiss_button("Ok")); } @@ -27,7 +27,7 @@ fn main() { // 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))); + .register('p', |s| show_popup(s))); siv.run(); diff --git a/src/event.rs b/src/event.rs index 845f846..af60a81 100644 --- a/src/event.rs +++ b/src/event.rs @@ -131,15 +131,15 @@ impl Key { ncurses::KEY_SEND => Key::ShiftEnd, ncurses::KEY_SDC => Key::ShiftDel, // All Fn keys use the same enum with associated number - f @ ncurses::KEY_F1 ... ncurses::KEY_F15 => Key::F((f - ncurses::KEY_F0) as u8), - f @ 281 ... 291 => Key::ShiftF((f - 281 + 5) as u8), - f @ 293 ... 303 => Key::CtrlF((f - 293 + 5) as u8), - f @ 305 ... 315 => Key::CtrlShiftF((f - 305 + 5) as u8), + f @ ncurses::KEY_F1...ncurses::KEY_F12 => Key::F((f - ncurses::KEY_F0) as u8), + f @ 277...288 => Key::ShiftF((f - 281 + 5) as u8), + f @ 289...300 => Key::CtrlF((f - 293 + 5) as u8), + f @ 301...312 => Key::CtrlShiftF((f - 305 + 5) as u8), // Shift and Ctrl F{1-4} need escape sequences... // // TODO: shift and ctrl Fn keys // Avoids 8-10 (H,I,J), they are used by other commands. - c @ 1 ... 7 | c @ 11 ... 25 => Key::CtrlChar(('a' as u8 + (c - 1) as u8) as char), + c @ 1...7 | c @ 11...25 => Key::CtrlChar(('a' as u8 + (c - 1) as u8) as char), _ => Key::Unknown(ch), } } @@ -154,51 +154,53 @@ impl fmt::Display for Key { Key::ShiftF(n) => write!(f, "Shift-F{}", n), Key::CtrlF(n) => write!(f, "Ctrl-F{}", n), Key::CtrlShiftF(n) => write!(f, "Ctrl-Shift-F{}", n), - key => write!(f, - "{}", - match key { - Key::NumpadCenter => "Numpad center", - Key::Left => "Left", - Key::Right => "Right", - Key::Down => "Down", - Key::Up => "Up", - Key::CtrlShiftLeft => "Ctrl-Shift-Left", - Key::CtrlShiftRight => "Ctrl-Shift-Right", - Key::CtrlShiftUp => "Ctrl-Shift-Up", - Key::CtrlShiftDown => "Ctrl-Shift-Down", - Key::ShiftUp => "Shift-Up", - Key::ShiftDown => "Shift-Down", - Key::ShiftLeft => "Shift-Left", - Key::ShiftRight => "Shift-Right", - Key::ShiftDel => "Shift-Del", - Key::CtrlDel => "Ctrl-Del", - Key::CtrlShiftDel => "Ctrl-Shift-Del", - Key::CtrlLeft => "Ctrl-Left", - Key::CtrlRight => "Ctrl-Right", - Key::CtrlUp => "Ctrl-Up", - Key::CtrlDown => "Ctrl-Down", - Key::CtrlPageUp => "Ctrl-PageUp", - Key::CtrlPageDown => "Ctrl-PageDown", - Key::PageUp => "PageUp", - Key::PageDown => "PageDown", - Key::Home => "Home", - Key::ShiftHome => "Shift-Home", - Key::CtrlHome => "Ctrl-Home", - Key::CtrlShiftHome => "Ctrl-Shift-Home", - Key::End => "End", - Key::ShiftEnd => "Shift-End", - Key::CtrlEnd => "Ctrl-End", - Key::CtrlShiftEnd => "Ctrl-Shift-End", - Key::Backspace => "Backspace", - Key::Del => "Del", - Key::Enter => "Enter", - Key::ShiftTab => "Shift-Tab", - Key::Tab => "Tab", - Key::Ins => "Ins", - Key::CtrlIns => "Ctrl-Ins", - Key::Esc => "Esc", - _ => "Missing key label", - }), + key => { + write!(f, + "{}", + match key { + Key::NumpadCenter => "Numpad center", + Key::Left => "Left", + Key::Right => "Right", + Key::Down => "Down", + Key::Up => "Up", + Key::CtrlShiftLeft => "Ctrl-Shift-Left", + Key::CtrlShiftRight => "Ctrl-Shift-Right", + Key::CtrlShiftUp => "Ctrl-Shift-Up", + Key::CtrlShiftDown => "Ctrl-Shift-Down", + Key::ShiftUp => "Shift-Up", + Key::ShiftDown => "Shift-Down", + Key::ShiftLeft => "Shift-Left", + Key::ShiftRight => "Shift-Right", + Key::ShiftDel => "Shift-Del", + Key::CtrlDel => "Ctrl-Del", + Key::CtrlShiftDel => "Ctrl-Shift-Del", + Key::CtrlLeft => "Ctrl-Left", + Key::CtrlRight => "Ctrl-Right", + Key::CtrlUp => "Ctrl-Up", + Key::CtrlDown => "Ctrl-Down", + Key::CtrlPageUp => "Ctrl-PageUp", + Key::CtrlPageDown => "Ctrl-PageDown", + Key::PageUp => "PageUp", + Key::PageDown => "PageDown", + Key::Home => "Home", + Key::ShiftHome => "Shift-Home", + Key::CtrlHome => "Ctrl-Home", + Key::CtrlShiftHome => "Ctrl-Shift-Home", + Key::End => "End", + Key::ShiftEnd => "Shift-End", + Key::CtrlEnd => "Ctrl-End", + Key::CtrlShiftEnd => "Ctrl-Shift-End", + Key::Backspace => "Backspace", + Key::Del => "Del", + Key::Enter => "Enter", + Key::ShiftTab => "Shift-Tab", + Key::Tab => "Tab", + Key::Ins => "Ins", + Key::CtrlIns => "Ctrl-Ins", + Key::Esc => "Esc", + _ => "Missing key label", + }) + } } } } diff --git a/src/theme.rs b/src/theme.rs index 35c7b1e..67d2100 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -86,10 +86,12 @@ impl Theme { } match table.get("borders") { - Some(&toml::Value::String(ref borders)) => match BorderStyle::from(borders) { - Some(borders) => self.borders = borders, - None => (), - }, + Some(&toml::Value::String(ref borders)) => { + match BorderStyle::from(borders) { + Some(borders) => self.borders = borders, + None => (), + } + } _ => (), } @@ -261,14 +263,18 @@ impl Color { Some(&toml::Value::String(ref value)) => { self.load_value(value, new_id); } - Some(&toml::Value::Array(ref array)) => for color in array.iter() { - match color { - &toml::Value::String(ref color) => if self.load_value(color, new_id) { - return; - }, - _ => (), + Some(&toml::Value::Array(ref array)) => { + for color in array.iter() { + match color { + &toml::Value::String(ref color) => { + if self.load_value(color, new_id) { + return; + } + } + _ => (), + } } - }, + } _ => (), } } @@ -411,9 +417,9 @@ fn load_hex(s: &str) -> i16 { for c in s.chars() { sum *= 16; sum += match c { - n @ '0' ... '9' => n as i16 - '0' as i16, - n @ 'a' ... 'f' => n as i16 - 'a' as i16 + 10, - n @ 'A' ... 'F' => n as i16 - 'A' as i16 + 10, + n @ '0'...'9' => n as i16 - '0' as i16, + n @ 'a'...'f' => n as i16 - 'a' as i16 + 10, + n @ 'A'...'F' => n as i16 - 'A' as i16 + 10, _ => 0, }; } diff --git a/src/utf8.rs b/src/utf8.rs index a2608cf..e99ee4a 100644 --- a/src/utf8.rs +++ b/src/utf8.rs @@ -12,7 +12,7 @@ pub fn read_char(first: u8, next: F) -> Result // Number of leading 1s determines the number of bytes we'll have to read let n_bytes = match (!first).leading_zeros() { - n @ 2 ... 6 => n as usize, + n @ 2...6 => n as usize, 1 => return Err("First byte is continuation byte.".to_string()), 7...8 => return Err("WTF is this byte??".to_string()), _ => unreachable!(), diff --git a/src/vec.rs b/src/vec.rs index feb72fe..bf9b6a2 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -80,25 +80,25 @@ impl ToVec2 for Vec2 { } } -impl ToVec2 for (i32,i32) { +impl ToVec2 for (i32, i32) { fn to_vec2(self) -> Vec2 { (self.0 as usize, self.1 as usize).to_vec2() } } -impl ToVec2 for (usize,usize) { +impl ToVec2 for (usize, usize) { fn to_vec2(self) -> Vec2 { Vec2::new(self.0, self.1) } } -impl ToVec2 for (u32,u32) { +impl ToVec2 for (u32, u32) { fn to_vec2(self) -> Vec2 { Vec2::new(self.0 as usize, self.1 as usize) } } -impl Add for Vec2 { +impl Add for Vec2 { type Output = Vec2; fn add(self, other: T) -> Vec2 { @@ -110,7 +110,7 @@ impl Add for Vec2 { } } -impl Sub for Vec2 { +impl Sub for Vec2 { type Output = Vec2; fn sub(self, other: T) -> Vec2 { @@ -206,13 +206,13 @@ impl ToVec4 for Vec4 { } } -impl ToVec4 for (usize,usize,usize,usize) { +impl ToVec4 for (usize, usize, usize, usize) { fn to_vec4(self) -> Vec4 { Vec4::new(self.0, self.1, self.2, self.3) } } -impl ToVec4 for (i32,i32,i32,i32) { +impl ToVec4 for (i32, i32, i32, i32) { fn to_vec4(self) -> Vec4 { Vec4::new(self.0 as usize, self.1 as usize, @@ -221,7 +221,7 @@ impl ToVec4 for (i32,i32,i32,i32) { } } -impl Add for Vec4 { +impl Add for Vec4 { type Output = Vec4; fn add(self, other: T) -> Vec4 { @@ -236,7 +236,7 @@ impl Add for Vec4 { } } -impl Sub for Vec4 { +impl Sub for Vec4 { type Output = Vec4; fn sub(self, other: T) -> Vec4 { diff --git a/src/view/box_view.rs b/src/view/box_view.rs index 1beb2a0..a7eddee 100644 --- a/src/view/box_view.rs +++ b/src/view/box_view.rs @@ -7,7 +7,7 @@ pub struct BoxView { view: T, } -impl BoxView { +impl BoxView { /// Creates a new BoxView with the given minimum size and content /// /// # Example @@ -25,8 +25,7 @@ impl BoxView { } } -impl ViewWrapper for BoxView { - +impl ViewWrapper for BoxView { wrap_impl!(&self.view); fn wrap_get_min_size(&self, mut req: SizeRequest) -> Vec2 { diff --git a/src/view/button.rs b/src/view/button.rs index 806bcab..7e35453 100644 --- a/src/view/button.rs +++ b/src/view/button.rs @@ -27,7 +27,6 @@ impl Button { } impl View for Button { - fn draw(&mut self, printer: &Printer) { let style = if !printer.focused { ColorPair::Primary diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 3ca5989..94b369d 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -97,7 +97,6 @@ impl Dialog { self } - } impl View for Dialog { @@ -207,54 +206,62 @@ impl View for Dialog { fn on_event(&mut self, event: Event) -> EventResult { match self.focus { // If we are on the content, we can only go down. - Focus::Content => match self.content.on_event(event) { - EventResult::Ignored if !self.buttons.is_empty() => match event { - Event::KeyEvent(Key::Down) => { - // Default to leftmost button when going down. - self.focus = Focus::Button(0); - EventResult::Consumed(None) + Focus::Content => { + match self.content.on_event(event) { + EventResult::Ignored if !self.buttons.is_empty() => { + match event { + Event::KeyEvent(Key::Down) => { + // Default to leftmost button when going down. + self.focus = Focus::Button(0); + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Tab) | Event::KeyEvent(Key::ShiftTab) => { + self.focus = Focus::Button(0); + EventResult::Consumed(None) + } + _ => EventResult::Ignored, + } } - Event::KeyEvent(Key::Tab) | Event::KeyEvent(Key::ShiftTab) => { - self.focus = Focus::Button(0); - EventResult::Consumed(None) - } - _ => EventResult::Ignored, - }, - res => res, - }, + res => res, + } + } // If we are on a button, we have more choice - Focus::Button(i) => match self.buttons[i].on_event(event) { - EventResult::Ignored => match event { - // Up goes back to the content - Event::KeyEvent(Key::Up) => { - if self.content.take_focus() { - self.focus = Focus::Content; - EventResult::Consumed(None) - } else { - EventResult::Ignored + Focus::Button(i) => { + match self.buttons[i].on_event(event) { + EventResult::Ignored => { + match event { + // Up goes back to the content + Event::KeyEvent(Key::Up) => { + if self.content.take_focus() { + self.focus = Focus::Content; + EventResult::Consumed(None) + } else { + EventResult::Ignored + } + } + Event::KeyEvent(Key::Tab) | Event::KeyEvent(Key::ShiftTab) => { + if self.content.take_focus() { + self.focus = Focus::Content; + EventResult::Consumed(None) + } else { + EventResult::Ignored + } + } + // Left and Right move to other buttons + Event::KeyEvent(Key::Right) if i + 1 < self.buttons.len() => { + self.focus = Focus::Button(i + 1); + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Left) if i > 0 => { + self.focus = Focus::Button(i - 1); + EventResult::Consumed(None) + } + _ => EventResult::Ignored, } } - Event::KeyEvent(Key::Tab) | Event::KeyEvent(Key::ShiftTab) => { - if self.content.take_focus() { - self.focus = Focus::Content; - EventResult::Consumed(None) - } else { - EventResult::Ignored - } - } - // Left and Right move to other buttons - Event::KeyEvent(Key::Right) if i + 1 < self.buttons.len() => { - self.focus = Focus::Button(i + 1); - EventResult::Consumed(None) - } - Event::KeyEvent(Key::Left) if i > 0 => { - self.focus = Focus::Button(i - 1); - EventResult::Consumed(None) - } - _ => EventResult::Ignored, - }, - res => res, - }, + res => res, + } + } } } diff --git a/src/view/edit_view.rs b/src/view/edit_view.rs index 61345f9..1218d2e 100644 --- a/src/view/edit_view.rs +++ b/src/view/edit_view.rs @@ -32,7 +32,7 @@ impl EditView { cursor: 0, offset: 0, min_length: 1, - last_length: 0, /* scrollable: false, */ + last_length: 0, // scrollable: false, } } @@ -138,20 +138,22 @@ impl View for EditView { // TODO: handle wide (CJK) chars self.cursor += 1; } - Event::KeyEvent(key) => match key { - Key::Home => self.cursor = 0, - Key::End => self.cursor = self.content.chars().count(), - Key::Left if self.cursor > 0 => self.cursor -= 1, - Key::Right if self.cursor < self.content.chars().count() => self.cursor += 1, - Key::Backspace if self.cursor > 0 => { - self.cursor -= 1; - remove_char(&mut self.content, self.cursor); + Event::KeyEvent(key) => { + match key { + Key::Home => self.cursor = 0, + Key::End => self.cursor = self.content.chars().count(), + Key::Left if self.cursor > 0 => self.cursor -= 1, + Key::Right if self.cursor < self.content.chars().count() => self.cursor += 1, + Key::Backspace if self.cursor > 0 => { + self.cursor -= 1; + remove_char(&mut self.content, self.cursor); + } + Key::Del if self.cursor < self.content.chars().count() => { + remove_char(&mut self.content, self.cursor); + } + _ => return EventResult::Ignored, } - Key::Del if self.cursor < self.content.chars().count() => { - remove_char(&mut self.content, self.cursor); - } - _ => return EventResult::Ignored, - }, + } } // Keep cursor in [offset, offset+last_length] by changing offset diff --git a/src/view/full_view.rs b/src/view/full_view.rs index 8069a28..b111464 100644 --- a/src/view/full_view.rs +++ b/src/view/full_view.rs @@ -6,14 +6,14 @@ pub struct FullView { view: T, } -impl FullView { +impl FullView { /// Wraps the given view into a new FullView. pub fn new(view: T) -> Self { FullView { view: view } } } -impl ViewWrapper for FullView { +impl ViewWrapper for FullView { wrap_impl!(&self.view); fn wrap_get_min_size(&self, req: SizeRequest) -> Vec2 { diff --git a/src/view/id_view.rs b/src/view/id_view.rs index eb8baa9..649fe4e 100644 --- a/src/view/id_view.rs +++ b/src/view/id_view.rs @@ -18,7 +18,7 @@ impl IdView { } } -impl ViewWrapper for IdView { +impl ViewWrapper for IdView { wrap_impl!(&self.view); fn wrap_find(&mut self, selector: &Selector) -> Option<&mut Any> { diff --git a/src/view/key_event_view.rs b/src/view/key_event_view.rs index 9054ba5..b4cfc6a 100644 --- a/src/view/key_event_view.rs +++ b/src/view/key_event_view.rs @@ -33,17 +33,17 @@ impl KeyEventView { } impl ViewWrapper for KeyEventView { - wrap_impl!(self.content); fn wrap_on_event(&mut self, event: Event) -> EventResult { match self.content.on_event(event) { - EventResult::Ignored => match self.callbacks.get(&event) { - None => EventResult::Ignored, - Some(cb) => EventResult::Consumed(Some(cb.clone())), - }, + EventResult::Ignored => { + match self.callbacks.get(&event) { + None => EventResult::Ignored, + Some(cb) => EventResult::Consumed(Some(cb.clone())), + } + } res => res, } } - } diff --git a/src/view/linear_layout.rs b/src/view/linear_layout.rs index 099a7fd..58a78b4 100644 --- a/src/view/linear_layout.rs +++ b/src/view/linear_layout.rs @@ -180,37 +180,39 @@ impl View for LinearLayout { fn on_event(&mut self, event: Event) -> EventResult { match self.children[self.focus].view.on_event(event) { - EventResult::Ignored => match event { - Event::KeyEvent(Key::Tab) if self.focus > 0 => { - self.focus -= 1; - EventResult::Consumed(None) + EventResult::Ignored => { + match event { + Event::KeyEvent(Key::Tab) if self.focus > 0 => { + self.focus -= 1; + EventResult::Consumed(None) + } + Event::KeyEvent(Key::ShiftTab) if self.focus + 1 < self.children.len() => { + self.focus += 1; + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Left) if self.orientation == Orientation::Horizontal && + self.focus > 0 => { + self.focus -= 1; + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Up) if self.orientation == Orientation::Vertical && + self.focus > 0 => { + self.focus -= 1; + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Right) if self.orientation == Orientation::Horizontal && + self.focus + 1 < self.children.len() => { + self.focus += 1; + EventResult::Consumed(None) + } + Event::KeyEvent(Key::Down) if self.orientation == Orientation::Vertical && + self.focus + 1 < self.children.len() => { + self.focus += 1; + EventResult::Consumed(None) + } + _ => EventResult::Ignored, } - Event::KeyEvent(Key::ShiftTab) if self.focus + 1 < self.children.len() => { - self.focus += 1; - EventResult::Consumed(None) - } - Event::KeyEvent(Key::Left) if self.orientation == Orientation::Horizontal && - self.focus > 0 => { - self.focus -= 1; - EventResult::Consumed(None) - } - Event::KeyEvent(Key::Up) if self.orientation == Orientation::Vertical && - self.focus > 0 => { - self.focus -= 1; - EventResult::Consumed(None) - } - Event::KeyEvent(Key::Right) if self.orientation == Orientation::Horizontal && - self.focus + 1 < self.children.len() => { - self.focus += 1; - EventResult::Consumed(None) - } - Event::KeyEvent(Key::Down) if self.orientation == Orientation::Vertical && - self.focus + 1 < self.children.len() => { - self.focus += 1; - EventResult::Consumed(None) - } - _ => EventResult::Ignored, - }, + } res => res, } } diff --git a/src/view/mod.rs b/src/view/mod.rs index c3d1dc8..120f2d1 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -60,8 +60,7 @@ pub trait View { /// Called once the size for this view has been decided, so it can /// propagate the information to its children. - fn layout(&mut self, Vec2) { - } + fn layout(&mut self, Vec2) {} /// Draws the view with the given printer (includes bounds) and focus. fn draw(&mut self, printer: &Printer); diff --git a/src/view/select_view.rs b/src/view/select_view.rs index c08d373..d3c34d1 100644 --- a/src/view/select_view.rs +++ b/src/view/select_view.rs @@ -16,7 +16,7 @@ struct Item { value: Rc, } -impl Item { +impl Item { fn new(label: &str, value: T) -> Self { Item { label: label.to_string(), @@ -36,7 +36,7 @@ pub struct SelectView { align: Align, } -impl SelectView { +impl SelectView { /// Creates a new empty SelectView. pub fn new() -> Self { SelectView { @@ -118,10 +118,9 @@ impl SelectView { pub fn item_str(self, label: &str) -> Self { self.item(label, label.to_string()) } - } -impl View for SelectView { +impl View for SelectView { fn draw(&mut self, printer: &Printer) { let h = self.items.len(); @@ -175,8 +174,9 @@ impl View for SelectView { Event::KeyEvent(Key::Up) if self.focus > 0 => self.focus -= 1, Event::KeyEvent(Key::Down) if self.focus + 1 < self.items.len() => self.focus += 1, Event::KeyEvent(Key::PageUp) => self.focus -= min(self.focus, 10), - Event::KeyEvent(Key::PageDown) => - self.focus = min(self.focus + 10, self.items.len() - 1), + Event::KeyEvent(Key::PageDown) => { + self.focus = min(self.focus + 10, self.items.len() - 1) + } Event::KeyEvent(Key::Home) => self.focus = 0, Event::KeyEvent(Key::End) => self.focus = self.items.len() - 1, Event::KeyEvent(Key::Enter) if self.select_cb.is_some() => { diff --git a/src/view/shadow_view.rs b/src/view/shadow_view.rs index 99bdc58..b2d00ba 100644 --- a/src/view/shadow_view.rs +++ b/src/view/shadow_view.rs @@ -11,15 +11,14 @@ pub struct ShadowView { view: T, } -impl ShadowView { +impl ShadowView { /// Wraps the given view. pub fn new(view: T) -> Self { ShadowView { view: view } } } -impl ViewWrapper for ShadowView { - +impl ViewWrapper for ShadowView { wrap_impl!(&self.view); fn wrap_get_min_size(&self, req: SizeRequest) -> Vec2 { diff --git a/src/view/sized_view.rs b/src/view/sized_view.rs index 550603b..52fe27e 100644 --- a/src/view/sized_view.rs +++ b/src/view/sized_view.rs @@ -20,7 +20,7 @@ impl SizedView { } } -impl ViewWrapper for SizedView { +impl ViewWrapper for SizedView { wrap_impl!(&self.view); fn wrap_layout(&mut self, size: Vec2) { diff --git a/src/view/text_view.rs b/src/view/text_view.rs index 0356d2a..c227c88 100644 --- a/src/view/text_view.rs +++ b/src/view/text_view.rs @@ -134,7 +134,7 @@ struct LinesIterator<'a> { width: usize, } -impl <'a> LinesIterator<'a> { +impl<'a> LinesIterator<'a> { // Start an iterator on the given content. fn new(content: &'a str, width: usize) -> Self { LinesIterator { @@ -145,8 +145,7 @@ impl <'a> LinesIterator<'a> { } } -impl <'a> Iterator for LinesIterator<'a> { - +impl<'a> Iterator for LinesIterator<'a> { type Item = Row; fn next(&mut self) -> Option { @@ -230,10 +229,12 @@ impl View for TextView { match event { Event::KeyEvent(Key::Home) => self.scrollbase.scroll_top(), Event::KeyEvent(Key::End) => self.scrollbase.scroll_bottom(), - Event::KeyEvent(Key::Up) if self.scrollbase.can_scroll_up() => - self.scrollbase.scroll_up(1), - Event::KeyEvent(Key::Down) if self.scrollbase.can_scroll_down() => - self.scrollbase.scroll_down(1), + Event::KeyEvent(Key::Up) if self.scrollbase.can_scroll_up() => { + self.scrollbase.scroll_up(1) + } + Event::KeyEvent(Key::Down) if self.scrollbase.can_scroll_down() => { + self.scrollbase.scroll_down(1) + } Event::KeyEvent(Key::PageDown) => self.scrollbase.scroll_down(10), Event::KeyEvent(Key::PageUp) => self.scrollbase.scroll_up(10), _ => return EventResult::Ignored, diff --git a/src/view/view_path.rs b/src/view/view_path.rs index 7e21bfd..909859d 100644 --- a/src/view/view_path.rs +++ b/src/view/view_path.rs @@ -23,7 +23,7 @@ pub trait ToPath { fn to_path(self) -> ViewPath; } -impl <'a> ToPath for &'a [usize] { +impl<'a> ToPath for &'a [usize] { fn to_path(self) -> ViewPath { ViewPath { path: self.to_owned() } } diff --git a/src/view/view_wrapper.rs b/src/view/view_wrapper.rs index 56e9008..d460f8d 100644 --- a/src/view/view_wrapper.rs +++ b/src/view/view_wrapper.rs @@ -45,7 +45,7 @@ pub trait ViewWrapper { } } -impl View for T { +impl View for T { fn draw(&mut self, printer: &Printer) { self.wrap_draw(printer); }