Skip to content

Commit

Permalink
Auto-restore previously selected entries; quality of life!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 16, 2019
1 parent 251ea53 commit 52f40ca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dua interactive

* windows support
* additional key-bindings
* auto-restore previous selection in each visited directory

#### ✅ v2.0.1- bug fixes and improvements

Expand Down
2 changes: 2 additions & 0 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use dua::{
WalkOptions, WalkResult,
};
use failure::Error;
use std::collections::BTreeMap;
use std::{io, path::PathBuf};
use termion::event::Key;
use tui::backend::Backend;
Expand All @@ -34,6 +35,7 @@ pub struct AppState {
pub sorting: SortMode,
pub message: Option<String>,
pub focussed: FocussedPane,
pub bookmarks: BTreeMap<TreeIndex, TreeIndex>,
}

/// State and methods representing the interactive disk usage analyser for the terminal
Expand Down
38 changes: 32 additions & 6 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,41 @@ impl TerminalApp {
self.state.root = parent_idx;
self.state.entries =
sorted_entries(&self.traversal.tree, parent_idx, self.state.sorting);
self.state.selected = self.state.entries.get(0).map(|b| b.index);
self.state.selected = self
.state
.bookmarks
.get(&parent_idx)
.map(|v| *v)
.or_else(|| self.state.entries.get(0).map(|b| b.index));
}
None => self.state.message = Some("Top level reached".into()),
}
}

pub fn enter_node(&mut self) {
if let Some(new_root) = self.state.selected {
let new_entries = sorted_entries(&self.traversal.tree, new_root, self.state.sorting);
match new_entries.get(0) {
if let Some(previously_selected) = self.state.selected {
let new_entries = sorted_entries(
&self.traversal.tree,
previously_selected,
self.state.sorting,
);
match new_entries.get(
self.state
.bookmarks
.get(&previously_selected)
.and_then(|selected| {
new_entries
.iter()
.find_position(|b| b.index == *selected)
.map(|(pos, _)| pos)
})
.unwrap_or(0),
) {
Some(b) => {
self.state.root = new_root;
self.state
.bookmarks
.insert(self.state.root, previously_selected);
self.state.root = previously_selected;
self.state.selected = Some(b.index);
self.state.entries = new_entries;
}
Expand All @@ -124,7 +147,10 @@ impl TerminalApp {
.get(next_selected_pos)
.or_else(|| entries.last())
.map(|b| b.index)
.or(self.state.selected)
.or(self.state.selected);
if let Some(selected) = self.state.selected {
self.state.bookmarks.insert(self.state.root, selected);
}
}

pub fn cycle_sorting(&mut self) {
Expand Down

0 comments on commit 52f40ca

Please sign in to comment.