Compare commits

...

1 Commits

Author SHA1 Message Date
andy.boot
17fe56bfd2 Refactor use of depth 2022-07-13 10:37:42 +01:00
4 changed files with 11 additions and 6 deletions

View File

@@ -143,7 +143,7 @@ fn walk(
data.is_symlink(),
data.is_file(),
walk_data.by_filecount,
depth,
depth + 1,
);
}
}

View File

@@ -84,13 +84,13 @@ impl DrawData<'_> {
}
// TODO: can we test this?
fn generate_bar(&self, node: &DisplayNode, level: usize) -> String {
fn generate_bar(&self, node: &DisplayNode) -> String {
let chars_in_bar = self.percent_bar.chars().count();
let num_bars = chars_in_bar as f32 * self.display_data.percent_size(node);
let mut num_not_my_bar = (chars_in_bar as i32) - num_bars as i32;
let mut new_bar = "".to_string();
let idx = 5 - min(4, max(1, level));
let idx = 5 - min(4, max(1, node.depth));
for c in self.percent_bar.chars() {
num_not_my_bar -= 1;
@@ -181,10 +181,8 @@ fn find_longest_dir_name(
}
fn display_node(node: DisplayNode, draw_data: &DrawData, is_biggest: bool, is_last: bool) {
// hacky way of working out how deep we are in the tree
let indent = draw_data.get_new_indent(!node.children.is_empty(), is_last);
let level = ((indent.chars().count() - 1) / 2) - 1;
let bar_text = draw_data.generate_bar(&node, level);
let bar_text = draw_data.generate_bar(&node);
let to_print = format_string(
&node,
@@ -398,6 +396,7 @@ mod tests {
let n = DisplayNode {
name: PathBuf::from("/short"),
size: 2_u64.pow(12), // This is 4.0K
depth: 1,
children: vec![],
};
let indent = "┌─┴";
@@ -420,6 +419,7 @@ mod tests {
let n = DisplayNode {
name: PathBuf::from(name),
size: 2_u64.pow(12), // This is 4.0K
depth: 1,
children: vec![],
};
let indent = "┌─┴";

View File

@@ -5,6 +5,7 @@ use std::path::PathBuf;
pub struct DisplayNode {
pub name: PathBuf, //todo: consider moving to a string?
pub size: u64,
pub depth: usize,
pub children: Vec<DisplayNode>,
}

View File

@@ -51,6 +51,7 @@ pub fn get_all_file_types(top_level_nodes: Vec<Node>, n: usize) -> Option<Displa
let remaining = DisplayNode {
name: PathBuf::from("(others)"),
size: rest.iter().map(|a| a.size).sum(),
depth: 1,
children: vec![],
};
@@ -62,6 +63,7 @@ pub fn get_all_file_types(top_level_nodes: Vec<Node>, n: usize) -> Option<Displa
let result = DisplayNode {
name: PathBuf::from("(total)"),
size: displayed.iter().map(|a| a.size).sum(),
depth: 0,
children: displayed,
};
Some(result)
@@ -98,6 +100,7 @@ fn build_by_all_file_types(top_level_nodes: Vec<Node>, counter: &mut HashMap<Str
let mut display_node = counter.entry(key.clone()).or_insert(DisplayNode {
name: PathBuf::from(key),
size: 0,
depth: node.depth,
children: vec![],
});
display_node.size += node.size;
@@ -141,6 +144,7 @@ fn recursive_rebuilder<'a>(
let newnode = DisplayNode {
name: current.name.clone(),
size: current.size,
depth: current.depth,
children: new_children,
};
Some(newnode)