Skip to content

Commit

Permalink
One step closer to the actual tree-building implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 2, 2019
1 parent 0774ecc commit 7c3743d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
28 changes: 19 additions & 9 deletions src/interactive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod app {
use crate::{WalkOptions, WalkResult};
use failure::Error;
use petgraph::{prelude::NodeIndex, Directed, Graph};
use petgraph::{prelude::NodeIndex, Directed, Direction, Graph};
use std::{ffi::OsString, io, path::PathBuf};
use termion::input::{Keys, TermReadEventsAndRaw};
use tui::{backend::Backend, Terminal};
Expand Down Expand Up @@ -55,19 +55,17 @@ mod app {
let mut tree = Tree::new();
let mut io_errors = 0u64;
let mut entries_traversed = 0u64;

let root_index = tree.add_node(EntryData::default());
let (mut previous_node_idx, mut parent_node_idx) = (root_index, root_index);
let mut previous_depth = 0;

for path in input.into_iter() {
let path_idx = tree.add_node(EntryData {
name: path.file_name().unwrap_or_default().into(),
..Default::default()
});
tree.add_edge(root_index, path_idx, ());
for entry in options.iter_from_path(path.as_ref()) {
entries_traversed += 1;
let mut data = EntryData::default();
match entry {
Ok(entry) => {
dbg!((&entry.file_name, entry.depth));
data.name = entry.file_name;
let file_size = match entry.metadata {
Some(Ok(ref m)) if !m.is_dir() => m.len(),
Expand All @@ -81,9 +79,21 @@ mod app {
"we ask for metadata, so we at least have Some(Err(..))). Issue in jwalk?"
),
};

parent_node_idx = match (entry.depth, previous_depth) {
(n, p) if n > p => previous_node_idx,
(n, p) if n < p => tree
.neighbors_directed(parent_node_idx, Direction::Incoming)
.next()
.expect("every node in the iteration has a parent"),
_ => parent_node_idx,
};

previous_depth = entry.depth;
data.size = file_size;
let entry_node = tree.add_node(data);
tree.add_edge(path_idx, entry_node, ());
let entry_index = tree.add_node(data);
tree.add_edge(parent_node_idx, entry_index, ());
previous_node_idx = entry_index;
}
Err(_) => io_errors += 1,
}
Expand Down
38 changes: 27 additions & 11 deletions tests/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ mod app {

#[test]
fn journey_with_single_path() -> Result<(), Error> {
let (_, app) = initialized_app_and_terminal("sample-01")?;
let expected_tree = sample_01_tree();
let (_, mut app) = initialized_app_and_terminal("sample-01")?;
let mut expected_tree = sample_01_tree();

assert_eq!(
debug(app.tree),
debug(expected_tree),
debug(app.tree.node_weights_mut().collect::<Vec<_>>()),
debug(expected_tree.node_weights_mut().collect::<Vec<_>>()),
"filesystem graph is stable and matches the directory structure"
);
Ok(())
Expand All @@ -45,12 +45,28 @@ mod app {
}

fn sample_01_tree() -> Tree {
let mut expected_tree = Tree::new();
expected_tree.add_node(EntryData {
name: OsString::from("foo"),
size: 231,
metadata_io_error: false,
});
expected_tree
let mut t = Tree::new();
let mut add_node = |name, size| {
t.add_node(EntryData {
name: OsString::from(name),
size,
metadata_io_error: false,
});
};
add_node("", 0);
add_node("sample-01", 0);
add_node(".hidden.666", 666);
add_node("a", 256);
add_node("b.empty", 0);
add_node("c.lnk", 1);
add_node("dir", 0);
add_node("1000bytes", 1000);
add_node("dir-a.1mb", 1_000_000);
add_node("dir-a.kb", 1024);
add_node("empty-dir", 0);
add_node(".gitkeep", 0);
add_node("sub", 0);
add_node("dir-sub-a.256kb", 256_000);
t
}
}

0 comments on commit 7c3743d

Please sign in to comment.