diff --git a/src/display.rs b/src/display.rs index 2dc9b87..ac005de 100644 --- a/src/display.rs +++ b/src/display.rs @@ -34,32 +34,16 @@ pub struct DisplayData { } impl DisplayData { - #[allow(clippy::collapsible_if)] fn get_tree_chars(&self, was_i_last: bool, has_children: bool) -> &'static str { - if self.is_reversed { - if was_i_last { - if has_children { - "┌─┴" - } else { - "┌──" - } - } else if has_children { - "├─┴" - } else { - "├──" - } - } else { - if was_i_last { - if has_children { - "└─┬" - } else { - "└──" - } - } else if has_children { - "├─┬" - } else { - "├──" - } + match (self.is_reversed, was_i_last, has_children) { + (true, true, true) => "┌─┴", + (true, true, false) => "┌──", + (true, false, true) => "├─┴", + (true, _, _) => "├──", + (false, true, true) => "└─┬", + (false, true, false) => "└──", + (false, false, true) => "├─┬", + (false, false, false) => "├──", } } @@ -89,15 +73,6 @@ impl DisplayData { } } -fn get_children_from_node(node: Node, is_reversed: bool) -> impl Iterator { - if is_reversed { - let n: Vec = node.children.into_iter().rev().map(|a| a).collect(); - n.into_iter() - } else { - node.children.into_iter() - } -} - struct DrawData<'a> { indent: String, percent_bar: String, @@ -177,7 +152,7 @@ pub fn draw_it( let first_size_bar = repeat(BLOCKS[0]).take(max_bar_length).collect::(); - for c in get_children_from_node(root_node, is_reversed) { + for c in root_node.get_children_from_node(is_reversed) { let display_data = DisplayData { short_paths: !use_full_path, is_reversed, @@ -234,9 +209,13 @@ fn display_node(node: Node, draw_data: &DrawData, is_biggest: bool, is_last: boo percent_bar: bar_text, display_data: draw_data.display_data, }; - let num_siblings = node.children.len() as u64; - for (count, c) in get_children_from_node(node, draw_data.display_data.is_reversed).enumerate() { + let num_siblings = node.num_siblings(); + + for (count, c) in node + .get_children_from_node(draw_data.display_data.is_reversed) + .enumerate() + { let is_biggest = dd.display_data.is_biggest(count, num_siblings); let was_i_last = dd.display_data.is_last(count, num_siblings); display_node(c, &dd, is_biggest, was_i_last); @@ -400,7 +379,7 @@ mod tests { by_filecount: false, num_chars_needed_on_left_most: 5, base_size: 1, - longest_string_length: longest_string_length, + longest_string_length, ls_colors: LsColors::from_env().unwrap_or_default(), } } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d7d173c..7d63f0b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -17,7 +17,7 @@ use self::platform::*; type PathData = (PathBuf, u64, Option<(u64, u64)>); -#[derive(Debug, Default, Eq)] +#[derive(Debug, Default, Eq, Clone)] pub struct Node { pub name: PathBuf, pub size: u64, @@ -46,6 +46,22 @@ impl PartialEq for Node { } } +impl Node { + pub fn num_siblings(&self) -> u64 { + self.children.len() as u64 + } + + pub fn get_children_from_node(&self, is_reversed: bool) -> impl Iterator { + let children = self.children.clone(); + if is_reversed { + let children: Vec = children.into_iter().rev().collect(); + return children.into_iter(); + } + + children.into_iter() + } +} + pub fn is_a_parent_of>(parent: P, child: P) -> bool { let parent = parent.as_ref(); let child = child.as_ref();