Skip to content

Commit

Permalink
Pune/India: Rustic way of handling the mark panes disappearance
Browse files Browse the repository at this point in the history
Interesting: before that fix, the GUI was in an invalid state,
as the mark pane disappeared without setting the focus back to the
main window.
Thanks to everything being encoded in the typesystem and no assumptions
made, the program wouldn't crash when buttons are pressed, which would
otherwise be attempted to dispatch to the now gone mark pane.

Instead one could press 'q' or tab, as fortunately, the pane
handling is always done before any of the panes get a chance.
  • Loading branch information
Sebastian Thiel committed Jun 7, 2019
1 parent fcde457 commit b4669c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl TerminalApp {
}

match self.state.focussed {
FocussedPane::Mark => self.window.mark_pane.as_mut().expect("mark pane").key(key),
FocussedPane::Mark => self.dispatch_to_mark_pane(key),
FocussedPane::Help => {
self.window.help_pane.as_mut().expect("help pane").key(key);
}
Expand Down
12 changes: 9 additions & 3 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::interactive::{
app::{FocussedPane, TerminalApp},
app::{FocussedPane::*, TerminalApp},
sorted_entries,
widgets::{HelpPane, MarkPane},
};
use dua::path_of;
use itertools::Itertools;
use petgraph::Direction;
use termion::event::Key;

pub enum CursorDirection {
PageDown,
Expand All @@ -28,7 +29,6 @@ impl CursorDirection {

impl TerminalApp {
pub fn cycle_focus(&mut self) {
use FocussedPane::*;
if let Some(p) = self.window.mark_pane.as_mut() {
p.set_focus(false)
};
Expand All @@ -53,7 +53,6 @@ impl TerminalApp {
}

pub fn toggle_help_pane(&mut self) {
use FocussedPane::*;
self.state.focussed = match self.state.focussed {
Main | Mark => {
self.window.help_pane = Some(HelpPane::default());
Expand Down Expand Up @@ -145,4 +144,11 @@ impl TerminalApp {
self.change_entry_selection(CursorDirection::Down)
}
}

pub fn dispatch_to_mark_pane(&mut self, key: Key) -> () {
self.window.mark_pane = self.window.mark_pane.take().and_then(|p| p.key(key));
if self.window.mark_pane.is_none() {
self.state.focussed = Main;
}
}
}
17 changes: 11 additions & 6 deletions src/interactive/widgets/mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,19 @@ impl MarkPane {
pub fn marked(&self) -> &EntryMarkMap {
&self.marked
}
pub fn key(&mut self, key: Key) {
pub fn key(mut self, key: Key) -> Option<Self> {
match key {
Char('d') | Char(' ') => self.remove_selected_and_move_down(),
Char('d') | Char(' ') => return self.remove_selected_and_move_down(),
Ctrl('u') | PageUp => self.change_selection(CursorDirection::PageUp),
Char('k') | Up => self.change_selection(CursorDirection::Up),
Char('j') | Down => self.change_selection(CursorDirection::Down),
Ctrl('d') | PageDown => self.change_selection(CursorDirection::PageDown),
_ => {}
};
Some(self)
}

fn remove_selected_and_move_down(&mut self) {
fn remove_selected_and_move_down(mut self) -> Option<Self> {
if let Some(selected) = self.selected {
let (idx, se_len) = {
let sorted_entries: Vec<_> = self
Expand All @@ -108,10 +109,14 @@ impl MarkPane {
};
if let Some(idx) = idx {
self.marked.remove(&idx);
if selected == se_len.saturating_sub(1) {
self.selected = selected.checked_sub(1);
if se_len.saturating_sub(1) == 0 {
return None;
}
self.selected = selected.checked_sub(1);
}
Some(self)
} else {
Some(self)
}
}

Expand Down Expand Up @@ -208,7 +213,7 @@ impl MarkPane {
let inner_area = block.inner(area);
block.draw(area, buf);

let list_area = if self.has_focus && !self.marked.is_empty() {
let list_area = if self.has_focus {
let (help_line_area, list_area) = {
let regions = Layout::default()
.direction(Direction::Vertical)
Expand Down

0 comments on commit b4669c0

Please sign in to comment.