Skip to content

Commit

Permalink
Show directories very similar to ncdu
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 3, 2019
1 parent 25ceae2 commit 74e5116
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use jwalk::WalkDir;
use petgraph::Direction;
use std::{fmt, path::Path};

pub(crate) fn get_size_or_panic(tree: &Tree, node_idx: TreeIndex) -> u64 {
pub(crate) fn get_entry_or_panic(tree: &Tree, node_idx: TreeIndex) -> &EntryData {
tree.node_weight(node_idx)
.expect("node should always be retrievable with valid index")
.size
}

pub(crate) fn get_size_or_panic(tree: &Tree, node_idx: TreeIndex) -> u64 {
get_entry_or_panic(tree, node_idx).size
}

pub(crate) fn sorted_entries(
Expand Down
27 changes: 25 additions & 2 deletions src/interactive/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{DisplayOptions, Traversal, Tree, TreeIndex};
use crate::{sorted_entries, ByteFormat};
use crate::{get_entry_or_panic, sorted_entries, ByteFormat};
use std::path::PathBuf;
use tui::layout::{Constraint, Direction, Layout};
use tui::style::{Color, Style};
use tui::{
Expand Down Expand Up @@ -128,6 +129,27 @@ impl<'a> Widget for Entries<'a> {
sorting,
selected,
} = self;
let is_dir = |mut node_idx| {
const THE_ROOT: usize = 1;
let mut entries = Vec::new();

while let Some(parent_idx) =
tree.neighbors_directed(node_idx, petgraph::Incoming).next()
{
entries.push(get_entry_or_panic(tree, node_idx));
node_idx = parent_idx;
}
entries.push(get_entry_or_panic(tree, node_idx));
entries
.iter()
.rev()
.skip(THE_ROOT)
.fold(PathBuf::new(), |mut acc, entry| {
acc.push(&entry.name);
acc
})
.is_dir()
};
let entries = sorted_entries(tree, *root, *sorting);
let total: u64 = entries.iter().map(|(_, w)| w.size).sum();
List::new(entries.iter().map(|(node_idx, w)| {
Expand All @@ -145,9 +167,10 @@ impl<'a> Widget for Entries<'a> {
};
Text::Styled(
format!(
"{} | {:.02}% | {}",
"{} | {:>5.02}% | {}{}",
display.byte_format.display(w.size),
(w.size as f64 / total as f64) * 100.0,
if is_dir(*node_idx) { "/" } else { " " },
w.name.to_string_lossy()
)
.into(),
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ fn run() -> Result<(), Error> {
Terminal::new(backend)
.with_context(|_| "Interactive mode requires a connected terminal")?
};
let mut app =
dua::interactive::TerminalApp::initialize(&mut terminal, walk_options, input)?;
let mut app = dua::interactive::TerminalApp::initialize(
&mut terminal,
walk_options,
paths_from(input)?,
)?;
app.process_events(&mut terminal, io::stdin().keys())?
}
Some(Aggregate {
Expand Down

0 comments on commit 74e5116

Please sign in to comment.