summaryrefslogtreecommitdiffstats
path: root/src/interactive/widgets/mark.rs
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-06 20:05:07 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-06 20:05:07 +0530
commit9ffacd03e256b45ecd40744e5507f37c30ae9b5e (patch)
tree6c1a22a85ae2f60116727829f89a98ba2372cc84 /src/interactive/widgets/mark.rs
parent4c354f475bfe841f3797be0a3341212aeeaa60c8 (diff)
Move ownership of marked entries to the MarkPane
interesting experience... it's easier to just keep state central, and have one place where mutation happens. Now that things are all over the place, it becomes more difficult to handle. However, I believe there is a way, the problem here is the hybrid state the program is currenlty in... partly centralized, partly localized.
Diffstat (limited to 'src/interactive/widgets/mark.rs')
-rw-r--r--src/interactive/widgets/mark.rs63
1 files changed, 37 insertions, 26 deletions
diff --git a/src/interactive/widgets/mark.rs b/src/interactive/widgets/mark.rs
index 42650aa..d600b34 100644
--- a/src/interactive/widgets/mark.rs
+++ b/src/interactive/widgets/mark.rs
@@ -1,5 +1,6 @@
-use crate::interactive::{widgets::COLOR_MARKED_LIGHT, CursorDirection, EntryMarkMap};
-use dua::traverse::TreeIndex;
+use crate::interactive::{widgets::COLOR_MARKED_LIGHT, CursorDirection, EntryMark, EntryMarkMap};
+use dua::path_of;
+use dua::traverse::{Tree, TreeIndex};
use itertools::Itertools;
use std::borrow::Borrow;
use termion::{event::Key, event::Key::*};
@@ -13,47 +14,57 @@ use tui::{
};
use tui_react::{List, ListProps};
+#[derive(Default)]
pub struct MarkPane {
- list: List,
selected: Option<TreeIndex>,
+ marked: EntryMarkMap,
+ list: List,
}
-pub struct MarkPaneProps<'a> {
+pub struct MarkPaneProps {
pub border_style: Style,
- pub marked: &'a EntryMarkMap,
}
impl MarkPane {
- pub fn new(_marked: &EntryMarkMap) -> MarkPane {
- MarkPane {
- list: Default::default(),
- selected: None,
+ pub fn toggle_index(mut self, index: TreeIndex, tree: &Tree) -> Option<Self> {
+ if self.marked.get(&index).is_some() {
+ self.marked.remove(&index);
+ } else {
+ if let Some(e) = tree.node_weight(index) {
+ self.marked.insert(
+ index,
+ EntryMark {
+ size: e.size,
+ path: path_of(tree, index),
+ },
+ );
+ }
+ }
+ if self.marked.is_empty() {
+ None
+ } else {
+ Some(self)
}
}
-
- pub fn key(&mut self, key: Key, marked: &EntryMarkMap) {
+ pub fn marked(&self) -> &EntryMarkMap {
+ &self.marked
+ }
+ pub fn key(&mut self, key: Key) {
match key {
- Ctrl('u') | PageUp => self.change_selection(CursorDirection::PageUp, marked),
- Char('k') | Up => self.change_selection(CursorDirection::Up, marked),
- Char('j') | Down => self.change_selection(CursorDirection::Down, marked),
- Ctrl('d') | PageDown => self.change_selection(CursorDirection::PageDown, marked),
+ 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),
_ => {}
};
}
- fn change_selection(&mut self, _direction: CursorDirection, _marked: &EntryMarkMap) {}
+ fn change_selection(&mut self, _direction: CursorDirection) {}
- pub fn render<'a>(
- &mut self,
- props: impl Borrow<MarkPaneProps<'a>>,
- area: Rect,
- buf: &mut Buffer,
- ) {
- let MarkPaneProps {
- border_style,
- marked,
- } = props.borrow();
+ pub fn render(&mut self, props: impl Borrow<MarkPaneProps>, area: Rect, buf: &mut Buffer) {
+ let MarkPaneProps { border_style } = props.borrow();
+ let marked: &_ = &self.marked;
let block = Block::default()
.title("Marked Entries")
.border_style(*border_style)