Skip to content

Commit

Permalink
first sketch of the delete-draw-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 14, 2019
1 parent c67abae commit 60ba3e7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl TerminalApp {
terminal.post_render()?;
Ok(())
}
fn draw<B>(&mut self, terminal: &mut Terminal<B>) -> Result<(), Error>
pub fn draw<B>(&mut self, terminal: &mut Terminal<B>) -> Result<(), Error>
where
B: Backend,
{
Expand Down Expand Up @@ -101,7 +101,7 @@ impl TerminalApp {
}

match self.state.focussed {
FocussedPane::Mark => self.dispatch_to_mark_pane(key),
FocussedPane::Mark => self.dispatch_to_mark_pane(key, terminal),
FocussedPane::Help => {
self.window.help_pane.as_mut().expect("help pane").key(key);
}
Expand Down
32 changes: 27 additions & 5 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::interactive::widgets::MarkMode;
use crate::interactive::{
app::{FocussedPane::*, TerminalApp},
sorted_entries,
Expand All @@ -7,6 +8,8 @@ use dua::path_of;
use itertools::Itertools;
use petgraph::Direction;
use termion::event::Key;
use tui::backend::Backend;
use tui_react::Terminal;

pub enum CursorDirection {
PageDown,
Expand Down Expand Up @@ -145,10 +148,29 @@ impl TerminalApp {
}
}

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;
}
pub fn dispatch_to_mark_pane<B>(&mut self, key: Key, terminal: &mut Terminal<B>) -> ()
where
B: Backend,
{
let res = self.window.mark_pane.take().and_then(|p| p.key(key));
self.window.mark_pane = match res {
Some((mut pane, mode)) => match mode {
Some(MarkMode::Delete) => {
while let Some(entry_to_delete) = pane.next_entry_for_deletion() {
self.draw(terminal).ok();
match pane.delete_entry(entry_to_delete) {
Some(p) => pane = p,
None => break,
}
}
None
}
None => Some(pane),
},
None => {
self.state.focussed = Main;
None
}
};
}
}
9 changes: 2 additions & 7 deletions src/interactive/app_test/journeys_with_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,9 @@ fn basic_user_journey_with_deletion() -> Result<(), Error> {
true,
"the marker pane is gone as all entries have been removed"
);
assert_eq!(app.state.selected, None, "nothing is left to be selected");
assert_eq!(
app.state.selected,
None,
"nothing is left to be selected"
);
assert_eq!(
app.state.root,
app.traversal.root_index,
app.state.root, app.traversal.root_index,
"the only root left is the top-level"
);
Ok(())
Expand Down
21 changes: 18 additions & 3 deletions src/interactive/widgets/mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use tui::{
use tui_react::{List, ListProps};
use unicode_segmentation::UnicodeSegmentation;

pub enum MarkMode {
Delete,
}

pub type EntryMarkMap = BTreeMap<TreeIndex, EntryMark>;
pub struct EntryMark {
pub size: u64,
Expand Down Expand Up @@ -84,18 +88,29 @@ impl MarkPane {
pub fn marked(&self) -> &EntryMarkMap {
&self.marked
}
pub fn key(mut self, key: Key) -> Option<Self> {
pub fn key(mut self, key: Key) -> Option<(Self, Option<MarkMode>)> {
let action = None;
match key {
Char('d') | Char(' ') => return self.remove_selected(),
Ctrl('R') => return self.prepare_deletion(),
Char('d') | Char(' ') => return self.remove_selected().map(|s| (s, action)),
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)
Some((self, action))
}

pub(crate) fn next_entry_for_deletion(&mut self) -> Option<usize> {
None
}
pub(crate) fn delete_entry(self, _index: usize) -> Option<Self> {
Some(self)
}
fn prepare_deletion(self) -> Option<(Self, Option<MarkMode>)> {
Some((self, Some(MarkMode::Delete)))
}
fn remove_selected(mut self) -> Option<Self> {
if let Some(mut selected) = self.selected {
let (idx, se_len) = {
Expand Down

0 comments on commit 60ba3e7

Please sign in to comment.