Skip to content

Commit

Permalink
refactor - still ain't pretty, but it's good enough for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 2, 2019
1 parent 59b2930 commit d4918ba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
83 changes: 37 additions & 46 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ mod app {
use termion::input::{Keys, TermReadEventsAndRaw};
use tui::{backend::Backend, Terminal};

pub type GraphIndexType = u32;
pub type Tree = Graph<EntryData, (), Directed, GraphIndexType>;
pub type TreeIndexType = u32;
pub type TreeIndex = NodeIndex<TreeIndexType>;
pub type Tree = Graph<EntryData, (), Directed, TreeIndexType>;

#[derive(Eq, PartialEq, Debug, Default)]
pub struct EntryData {
Expand All @@ -24,7 +25,7 @@ mod app {
/// A tree representing the entire filestem traversal
pub tree: Tree,
/// The top-level node of the tree.
pub root_index: NodeIndex<GraphIndexType>,
pub root_index: TreeIndex,
/// Amount of files or directories we have seen during the filesystem traversal
pub entries_traversed: u64,
/// Total amount of IO errors encountered when traversing the filesystem
Expand Down Expand Up @@ -52,6 +53,20 @@ mod app {
where
B: Backend,
{
fn set_size_or_panic(
tree: &mut Tree,
parent_node_idx: TreeIndex,
current_size_at_depth: u64,
) {
tree.node_weight_mut(parent_node_idx)
.expect("node for parent index we just retrieved")
.size = current_size_at_depth;
}
fn parent_or_panic(tree: &mut Tree, parent_node_idx: TreeIndex) -> TreeIndex {
tree.neighbors_directed(parent_node_idx, Direction::Incoming)
.next()
.expect("every node in the iteration has a parent")
}
let mut tree = Tree::new();
let mut io_errors = 0u64;
let mut entries_traversed = 0u64;
Expand Down Expand Up @@ -93,17 +108,14 @@ mod app {
let size_at_level_above = sizes_per_depth_level
.pop()
.expect("sizes per level to be in sync with graph");
tree.node_weight_mut(parent_node_idx)
.expect("node for parent index we just retrieved")
.size = current_size_at_depth;
set_size_or_panic(
&mut tree,
parent_node_idx,
current_size_at_depth,
);
current_size_at_depth += size_at_level_above;
parent_node_idx = tree
.neighbors_directed(
parent_node_idx,
Direction::Incoming,
)
.next()
.expect("every node in the iteration has a parent");
parent_node_idx =
parent_or_panic(&mut tree, parent_node_idx);
}
current_size_at_depth += file_size;
tree.node_weight_mut(parent_node_idx)
Expand All @@ -115,50 +127,29 @@ mod app {
}
};

previous_depth = entry.depth;
data.size = file_size;
let entry_index = tree.add_node(data);

tree.add_edge(parent_node_idx, entry_index, ());
previous_node_idx = entry_index;
previous_depth = entry.depth;
}
Err(_) => io_errors += 1,
}
}
}

dbg!(previous_depth);
dbg!(&sizes_per_depth_level);
dbg!(current_size_at_depth);
if previous_depth == 1 {
for node in &[parent_node_idx, root_index] {
tree.node_weight_mut(*node)
.expect("node for parent index we just retrieved")
.size = current_size_at_depth;
}
} else {
sizes_per_depth_level.push(current_size_at_depth);
current_size_at_depth = 0;
for _ in 0..previous_depth {
let size_at_level_above = sizes_per_depth_level
.pop()
.expect("sizes per level to be in sync with graph");
dbg!((
size_at_level_above,
tree.node_weight_mut(parent_node_idx).unwrap()
));
current_size_at_depth += size_at_level_above;
tree.node_weight_mut(parent_node_idx)
.expect("node for parent index we just retrieved")
.size = current_size_at_depth;
parent_node_idx = tree
.neighbors_directed(parent_node_idx, Direction::Incoming)
.next()
.expect("every node in the iteration has a parent (outer)");
}
tree.node_weight_mut(root_index)
.expect("node for parent index we just retrieved")
.size = current_size_at_depth;
sizes_per_depth_level.push(current_size_at_depth);
current_size_at_depth = 0;
for _ in 0..previous_depth {
let size_at_level_above = sizes_per_depth_level
.pop()
.expect("sizes per level to be in sync with graph");
current_size_at_depth += size_at_level_above;
set_size_or_panic(&mut tree, parent_node_idx, current_size_at_depth);
parent_node_idx = parent_or_panic(&mut tree, parent_node_idx);
}
set_size_or_panic(&mut tree, root_index, current_size_at_depth);

Ok(TerminalApp {
tree,
Expand Down
4 changes: 2 additions & 2 deletions tests/interactive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod app {
use dua::interactive::{EntryData, GraphIndexType, TerminalApp, Tree};
use dua::interactive::{EntryData, TerminalApp, Tree, TreeIndexType};
use dua::{ByteFormat, Color, Sorting, WalkOptions};
use failure::Error;
use petgraph::prelude::NodeIndex;
Expand Down Expand Up @@ -123,7 +123,7 @@ mod app {

fn make_add_node<'a>(
t: &'a mut Tree,
) -> impl FnMut(&str, u64, Option<NodeIndex<GraphIndexType>>) -> NodeIndex<GraphIndexType> + 'a
) -> impl FnMut(&str, u64, Option<NodeIndex<TreeIndexType>>) -> NodeIndex<TreeIndexType> + 'a
{
move |name, size, maybe_from_idx| {
let n = t.add_node(EntryData {
Expand Down

0 comments on commit d4918ba

Please sign in to comment.