diff options
-rw-r--r-- | src/interactive/app/eventloop.rs | 14 | ||||
-rw-r--r-- | src/interactive/app/handlers.rs | 2 | ||||
-rw-r--r-- | src/interactive/common.rs | 5 | ||||
-rw-r--r-- | src/interactive/mod.rs | 2 | ||||
-rw-r--r-- | src/interactive/widgets/help.rs | 9 | ||||
-rw-r--r-- | src/interactive/widgets/mark.rs | 62 |
6 files changed, 54 insertions, 40 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 6e54689..6227448 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, Handle, SortMode, + ByteVisualization, CursorDirection, DisplayOptions, EntryDataBundle, SortMode, }; use dua::{ traverse::{Traversal, TreeIndex}, @@ -86,6 +86,7 @@ impl TerminalApp { R: io::Read + TermReadEventsAndRaw, { use termion::event::Key::{Backspace, Char, Ctrl, Esc}; + use CursorDirection::*; use FocussedPane::*; self.draw(terminal)?; @@ -109,7 +110,12 @@ impl TerminalApp { } match self.state.focussed { - FocussedPane::Mark => self.window.mark_pane.as_mut().expect("mark pane").key(key), + FocussedPane::Mark => self + .window + .mark_pane + .as_mut() + .expect("mark pane") + .key(key, &self.state.marked), FocussedPane::Help => { self.window.help_pane.as_mut().expect("help pane").key(key); } @@ -120,8 +126,8 @@ impl TerminalApp { Char('u') | Backspace => self.exit_node(), Char('o') | Char('\n') => self.enter_node(), Ctrl('u') => self.change_entry_selection(CursorDirection::PageUp), - Char('k') => self.change_entry_selection(CursorDirection::Up), - Char('j') => self.change_entry_selection(CursorDirection::Down), + Char('k') => self.change_entry_selection(Up), + Char('j') => self.change_entry_selection(Down), Ctrl('d') => self.change_entry_selection(CursorDirection::PageDown), Char('s') => self.cycle_sorting(), Char('g') => self.display.byte_vis.cycle(), diff --git a/src/interactive/app/handlers.rs b/src/interactive/app/handlers.rs index 982ffa4..11becc1 100644 --- a/src/interactive/app/handlers.rs +++ b/src/interactive/app/handlers.rs @@ -143,7 +143,7 @@ impl TerminalApp { if self.state.marked.is_empty() { self.window.mark_pane = None; } else { - self.window.mark_pane = Some(MarkPane::default()); + self.window.mark_pane = Some(MarkPane::new(&self.state.marked)); } if advance_cursor { self.change_entry_selection(CursorDirection::Down) diff --git a/src/interactive/common.rs b/src/interactive/common.rs deleted file mode 100644 index 83bb711..0000000 --- a/src/interactive/common.rs +++ /dev/null @@ -1,5 +0,0 @@ -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 0bc81d7..ef02372 100644 --- a/src/interactive/mod.rs +++ b/src/interactive/mod.rs @@ -1,9 +1,7 @@ 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 07ca3e6..02698e6 100644 --- a/src/interactive/widgets/help.rs +++ b/src/interactive/widgets/help.rs @@ -1,4 +1,4 @@ -use crate::interactive::{CursorDirection, Handle}; +use crate::interactive::CursorDirection; use std::borrow::Borrow; use std::cell::{Cell, RefCell}; use termion::event::Key; @@ -20,8 +20,8 @@ pub struct HelpPaneProps { pub border_style: Style, } -impl Handle for HelpPane { - fn key(&mut self, key: Key) { +impl HelpPane { + pub fn key(&mut self, key: Key) { match key { Ctrl('u') | PageUp => self.scroll_help(CursorDirection::PageUp), Char('k') | Up => self.scroll_help(CursorDirection::Up), @@ -30,9 +30,6 @@ impl Handle for HelpPane { _ => {} }; } -} - -impl HelpPane { fn scroll_help(&mut self, direction: CursorDirection) { self.scroll = direction.move_cursor(self.scroll as usize) as u16; } diff --git a/src/interactive/widgets/mark.rs b/src/interactive/widgets/mark.rs index ed31ece..42650aa 100644 --- a/src/interactive/widgets/mark.rs +++ b/src/interactive/widgets/mark.rs @@ -1,17 +1,21 @@ -use crate::interactive::{widgets::COLOR_MARKED_LIGHT, CursorDirection, EntryMarkMap, Handle}; +use crate::interactive::{widgets::COLOR_MARKED_LIGHT, CursorDirection, EntryMarkMap}; use dua::traverse::TreeIndex; use itertools::Itertools; use std::borrow::Borrow; use termion::{event::Key, event::Key::*}; use tui::{ - buffer::Buffer, layout::Rect, style::Style, widgets::Block, widgets::Borders, widgets::Text, + buffer::Buffer, + layout::Rect, + style::{Modifier, Style}, + widgets::Block, + widgets::Borders, + widgets::Text, }; use tui_react::{List, ListProps}; -#[derive(Default)] pub struct MarkPane { - pub list: List, - pub selected: Option<TreeIndex>, + list: List, + selected: Option<TreeIndex>, } pub struct MarkPaneProps<'a> { @@ -19,20 +23,26 @@ pub struct MarkPaneProps<'a> { pub marked: &'a EntryMarkMap, } -impl Handle for MarkPane { - fn key(&mut self, key: Key) { +impl MarkPane { + pub fn new(_marked: &EntryMarkMap) -> MarkPane { + MarkPane { + list: Default::default(), + selected: None, + } + } + + pub fn key(&mut self, key: Key, marked: &EntryMarkMap) { match key { - 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), + 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), _ => {} }; } -} -impl MarkPane { - fn change_selection(&mut self, _direction: CursorDirection) {} + fn change_selection(&mut self, _direction: CursorDirection, _marked: &EntryMarkMap) {} + pub fn render<'a>( &mut self, props: impl Borrow<MarkPaneProps<'a>>, @@ -48,19 +58,27 @@ impl MarkPane { .title("Marked Entries") .border_style(*border_style) .borders(Borders::ALL); - let entry_in_view = self.selected.and_then(|idx| { - marked - .iter() - .enumerate() - .find_position(|(_pos, (&node_index, _))| node_index == idx) - .map(|(pos, _)| pos) - }); + let entry_in_view = self + .selected + .and_then(|selected| { + marked + .keys() + .find_position(|&&index| index == selected) + .map(|(pos, _)| pos) + }) + .or_else(|| marked.keys().enumerate().last().map(|(pos, _)| pos)); - let entries = marked.iter().map(|(_, v)| { + let selected = self.selected.clone(); + let entries = marked.iter().map(|(idx, v)| { + let modifier = match selected { + Some(selected) if *idx == selected => Modifier::BOLD, + _ => Modifier::empty(), + }; let name = Text::Styled( v.path.to_string_lossy(), Style { fg: COLOR_MARKED_LIGHT, + modifier, ..Style::default() }, ); |