diff --git a/src/display.rs b/src/display.rs index e603667..510c251 100644 --- a/src/display.rs +++ b/src/display.rs @@ -60,15 +60,18 @@ impl DisplayData { num_siblings == max_siblings - 1 } } + + fn get_children_from_node(&self, node: Node) -> impl Iterator> { + if self.is_reversed { + let n: Vec> = node.children.into_iter().rev().map(|a| a).collect(); + return n.into_iter(); + } else { + return node.children.into_iter(); + } + } } -pub fn draw_it( - permissions: bool, - depth: Option, - use_full_path: bool, - is_reversed: bool, - root_node: Node, -) { +pub fn draw_it(permissions: bool, use_full_path: bool, is_reversed: bool, root_node: Node) { if !permissions { eprintln!("Did not have permissions for all directories"); } @@ -77,37 +80,35 @@ pub fn draw_it( is_reversed, }; - for c in root_node.children { + for c in display_data.get_children_from_node(root_node) { let first_tree_chars = display_data.get_first_chars(); - display_node(*c, true, depth, first_tree_chars, &display_data) + display_node(*c, true, first_tree_chars, &display_data) } } -fn display_node( - node: Node, - is_biggest: bool, - depth: Option, - indent: &str, - display_data: &DisplayData, -) { - let new_depth = match depth { - None => None, - Some(0) => return, - Some(d) => Some(d - 1), - }; +fn display_node(node: Node, is_biggest: bool, indent: &str, display_data: &DisplayData) { let short = display_data.short_paths; - print_this_node(&node, is_biggest, short, indent); let mut num_siblings = node.children.len() as u64; let max_sibling = num_siblings; let new_indent = clean_indentation_string(indent); + let name = node.name.clone(); + let size = node.size; - for c in node.children { + if !display_data.is_reversed { + print_this_node(&*name, size, is_biggest, short, indent); + } + + for c in display_data.get_children_from_node(node) { num_siblings -= 1; let chars = display_data.get_tree_chars(num_siblings, max_sibling, c.children.len() > 0); let is_biggest = display_data.biggest(num_siblings, max_sibling); - let full_indent = (new_indent.clone() + chars); - display_node(*c, is_biggest, new_depth, &*full_indent, display_data) + let full_indent = new_indent.clone() + chars; + display_node(*c, is_biggest, &*full_indent, display_data) + } + + if display_data.is_reversed { + print_this_node(&*name, size, is_biggest, short, indent); } } @@ -128,12 +129,12 @@ fn clean_indentation_string(s: &str) -> String { is } -fn print_this_node(node: &Node, is_biggest: bool, short_paths: bool, indentation: &str) { - let pretty_size = format!("{:>5}", human_readable_number(node.size),); +fn print_this_node(name: &str, size: u64, is_biggest: bool, short_paths: bool, indentation: &str) { + let pretty_size = format!("{:>5}", human_readable_number(size),); println!( "{}", format_string( - &*node.name, + name, is_biggest, short_paths, pretty_size.as_ref(), diff --git a/src/main.rs b/src/main.rs index d7948d5..3fc2d4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,7 @@ extern crate walkdir; use self::display::draw_it; use clap::{App, AppSettings, Arg}; -use utils::{ - compare_tuple_smallest_first, find_big_ones, get_dir_tree, simplify_dir_names, sort, - trim_deep_ones, Node, -}; +use utils::{find_big_ones, get_dir_tree, simplify_dir_names, sort, trim_deep_ones, Node}; mod display; mod utils; @@ -94,28 +91,24 @@ fn main() { let simplified_dirs = simplify_dir_names(target_dirs); let (permissions, nodes) = get_dir_tree(&simplified_dirs, use_apparent_size); let sorted_data = sort(nodes); - let mut biggest_ones = { + let biggest_ones = { match depth { None => find_big_ones(sorted_data, number_of_lines + simplified_dirs.len()), Some(d) => trim_deep_ones(sorted_data, d, &simplified_dirs), } }; - if options.is_present("reverse") { - biggest_ones.sort_by(compare_tuple_smallest_first); - } - let tree = build_tree(&biggest_ones); + let tree = build_tree(biggest_ones, depth); //println!("{:?}", tree); draw_it( permissions, - depth, use_full_path, options.is_present("reverse"), tree, ); } -fn build_tree(biggest_ones: &Vec<(String, u64)>) -> Node { +fn build_tree(biggest_ones: Vec<(String, u64)>, depth: Option) -> Node { let mut top_parent = Node { name: "".to_string(), size: 0, @@ -123,21 +116,26 @@ fn build_tree(biggest_ones: &Vec<(String, u64)>) -> Node { }; // assume sorted order - for b in biggest_ones.clone() { + for b in biggest_ones { let n = Node { name: b.0, size: b.1, children: vec![], }; - recursively_build_tree(&mut top_parent, n) + recursively_build_tree(&mut top_parent, n, depth) } top_parent } -fn recursively_build_tree(parent_node: &mut Node, new_node: Node) { +fn recursively_build_tree(parent_node: &mut Node, new_node: Node, depth: Option) { + let new_depth = match depth { + None => None, + Some(0) => return, + Some(d) => Some(d - 1), + }; for c in parent_node.children.iter_mut() { if new_node.name.starts_with(&c.name) { - return recursively_build_tree(&mut *c, new_node); + return recursively_build_tree(&mut *c, new_node, new_depth); } } let temp = Box::::new(new_node); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d4d89dc..6ab741d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -122,15 +122,6 @@ pub fn sort_by_size_first_name_second(a: &(String, u64), b: &(String, u64)) -> O } } -pub fn compare_tuple_smallest_first(a: &(String, u64), b: &(String, u64)) -> Ordering { - let result = a.1.cmp(&b.1); - if result == Ordering::Equal { - b.0.cmp(&a.0) - } else { - result - } -} - pub fn sort(data: HashMap) -> Vec<(String, u64)> { let mut new_l: Vec<(String, u64)> = data.iter().map(|(a, b)| (a.clone(), *b)).collect(); new_l.sort_by(|a, b| sort_by_size_first_name_second(&a, &b));