Skip to content

Commit

Permalink
reactor help: move event handling closer to where it belongs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 6, 2019
1 parent 4cde0f6 commit 04f5324
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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)?;
Expand All @@ -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),
Expand Down
7 changes: 0 additions & 7 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/interactive/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use termion::event::Key;

pub trait Handle {
fn key(&mut self, key: Key);
}
2 changes: 2 additions & 0 deletions src/interactive/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod app;
mod common;
pub mod widgets;

pub use self::app::*;
pub use common::*;

#[cfg(test)]
mod app_test;
21 changes: 20 additions & 1 deletion src/interactive/widgets/help.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/interactive/widgets/mark.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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()
},
);
Expand Down

0 comments on commit 04f5324

Please sign in to comment.