diff options
author | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-06 16:44:47 +0530 |
---|---|---|
committer | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-06 16:45:01 +0530 |
commit | 04f5324b17efe4c7b62a0afc7d2b34304a9a4407 (patch) | |
tree | e4aaadfffefd0c0dcb84990c5915a9760edaef95 | |
parent | 4cde0f6892f29a16694155ec25d94f4ce3c3d0c9 (diff) |
reactor help: move event handling closer to where it belongs
-rw-r--r-- | src/interactive/app/eventloop.rs | 18 | ||||
-rw-r--r-- | src/interactive/app/handlers.rs | 7 | ||||
-rw-r--r-- | src/interactive/common.rs | 5 | ||||
-rw-r--r-- | src/interactive/mod.rs | 2 | ||||
-rw-r--r-- | src/interactive/widgets/help.rs | 21 | ||||
-rw-r--r-- | src/interactive/widgets/mark.rs | 7 |
6 files changed, 39 insertions, 21 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 69f16a9..606a423 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -1,7 +1,7 @@ use crate::interactive::{ sorted_entries, widgets::{MainWindow, MainWindowProps}, - ByteVisualization, CursorDirection, DisplayOptions, EntryDataBundle, SortMode, + ByteVisualization, CursorDirection, DisplayOptions, EntryDataBundle, Handle, SortMode, }; use dua::{ traverse::{Traversal, TreeIndex}, @@ -85,7 +85,7 @@ impl TerminalApp { B: Backend, R: io::Read + TermReadEventsAndRaw, { - use termion::event::Key::{Backspace, Char, Ctrl, Down, Esc, PageDown, PageUp, Up}; + use termion::event::Key::{Backspace, Char, Ctrl, Esc}; use FocussedPane::*; self.draw(terminal)?; @@ -110,13 +110,13 @@ impl TerminalApp { match self.state.focussed { FocussedPane::Mark => {} - FocussedPane::Help => match key { - Ctrl('u') | PageUp => self.scroll_help(CursorDirection::PageUp), - Char('k') | Up => self.scroll_help(CursorDirection::Up), - Char('j') | Down => self.scroll_help(CursorDirection::Down), - Ctrl('d') | PageDown => self.scroll_help(CursorDirection::PageDown), - _ => {} - }, + FocussedPane::Help => { + self.window + .help_pane + .as_mut() + .expect("help window") + .key(key); + } FocussedPane::Main => match key { Char('O') => self.open_that(), Char(' ') => self.mark_entry(false), diff --git a/src/interactive/app/handlers.rs b/src/interactive/app/handlers.rs index a2e8137..af1a38c 100644 --- a/src/interactive/app/handlers.rs +++ b/src/interactive/app/handlers.rs @@ -100,13 +100,6 @@ impl TerminalApp { } } } - - pub fn scroll_help(&mut self, direction: CursorDirection) { - if let Some(ref mut pane) = self.window.help_pane { - pane.scroll = direction.move_cursor(pane.scroll as usize) as u16; - } - } - pub fn change_entry_selection(&mut self, direction: CursorDirection) { let entries = sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting); let next_selected_pos = match self.state.selected { diff --git a/src/interactive/common.rs b/src/interactive/common.rs new file mode 100644 index 0000000..83bb711 --- /dev/null +++ b/src/interactive/common.rs @@ -0,0 +1,5 @@ +use termion::event::Key; + +pub trait Handle { + fn key(&mut self, key: Key); +} diff --git a/src/interactive/mod.rs b/src/interactive/mod.rs index ef02372..0bc81d7 100644 --- a/src/interactive/mod.rs +++ b/src/interactive/mod.rs @@ -1,7 +1,9 @@ mod app; +mod common; pub mod widgets; pub use self::app::*; +pub use common::*; #[cfg(test)] mod app_test; diff --git a/src/interactive/widgets/help.rs b/src/interactive/widgets/help.rs index da261e7..07ca3e6 100644 --- a/src/interactive/widgets/help.rs +++ b/src/interactive/widgets/help.rs @@ -1,9 +1,12 @@ +use crate::interactive::{CursorDirection, Handle}; use std::borrow::Borrow; use std::cell::{Cell, RefCell}; -use tui::style::Color; +use termion::event::Key; +use termion::event::Key::*; use tui::{ buffer::Buffer, layout::Rect, + style::Color, style::{Modifier, Style}, widgets::{Block, Borders, Paragraph, Text, Widget}, }; @@ -17,7 +20,23 @@ pub struct HelpPaneProps { pub border_style: Style, } +impl Handle for HelpPane { + fn key(&mut self, key: Key) { + match key { + Ctrl('u') | PageUp => self.scroll_help(CursorDirection::PageUp), + Char('k') | Up => self.scroll_help(CursorDirection::Up), + Char('j') | Down => self.scroll_help(CursorDirection::Down), + Ctrl('d') | PageDown => self.scroll_help(CursorDirection::PageDown), + _ => {} + }; + } +} + impl HelpPane { + fn scroll_help(&mut self, direction: CursorDirection) { + self.scroll = direction.move_cursor(self.scroll as usize) as u16; + } + pub fn render(&mut self, props: impl Borrow<HelpPaneProps>, area: Rect, buf: &mut Buffer) { let (texts, num_lines) = { let num_lines = Cell::new(0u16); diff --git a/src/interactive/widgets/mark.rs b/src/interactive/widgets/mark.rs index da43424..d04adb8 100644 --- a/src/interactive/widgets/mark.rs +++ b/src/interactive/widgets/mark.rs @@ -1,10 +1,9 @@ -use crate::interactive::EntryMarkMap; +use crate::interactive::{widgets::COLOR_MARKED_LIGHT, EntryMarkMap}; use dua::traverse::TreeIndex; use itertools::Itertools; use std::borrow::Borrow; use tui::{ - buffer::Buffer, layout::Rect, style::Color, style::Style, widgets::Block, widgets::Borders, - widgets::Text, + buffer::Buffer, layout::Rect, style::Style, widgets::Block, widgets::Borders, widgets::Text, }; use tui_react::{List, ListProps}; @@ -47,7 +46,7 @@ impl MarkPane { let name = Text::Styled( v.path.to_string_lossy(), Style { - fg: Color::LightRed, + fg: COLOR_MARKED_LIGHT, ..Style::default() }, ); |