diff --git a/src/display.rs b/src/display.rs index 6966394..ac9a6ab 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,7 +1,7 @@ extern crate ansi_term; -use std::collections::HashSet; use self::ansi_term::Colour::Fixed; +use std::collections::HashSet; static UNITS: [char; 4] = ['T', 'G', 'M', 'K']; @@ -43,7 +43,7 @@ fn display_node>( indentation_str: S, ) { if found.contains(node_to_print) { - return + return; } found.insert(node_to_print.to_string()); @@ -55,39 +55,25 @@ fn display_node>( match get_size(to_display, node_to_print) { None => println!("Can not find path: {}", node_to_print), Some(size) => { - let mut is = indentation_str.into(); + let is = indentation_str.into(); let ntp: &str = node_to_print.as_ref(); print_this_node(ntp, size, is_biggest, short_paths, is.as_ref()); + let new_indent_str = clean_indentation_string(is); - is = is.replace("└─┬", " "); - is = is.replace("└──", " "); - is = is.replace("├──", "│ "); - is = is.replace("├─┬", "│ "); - is = is.replace("─┬", " "); - - let printable_node_slashes = node_to_print.matches('/').count(); - - let mut num_siblings = to_display.iter().fold(0, |a, b| { - if b.0.starts_with(ntp) && b.0.matches('/').count() == printable_node_slashes + 1 { - a + 1 - } else { - a - } - }); + let num_slashes = node_to_print.matches('/').count(); + let mut num_siblings = count_siblings(to_display, num_slashes, ntp); let mut is_biggest = true; for &(ref k, _) in to_display.iter() { - if k.starts_with(ntp) && k.matches('/').count() == printable_node_slashes + 1 { + if k.starts_with(ntp) && k.matches('/').count() == num_slashes + 1 { num_siblings -= 1; let mut has_children = false; if new_depth.is_none() || new_depth.unwrap() != 1 { for &(ref k2, _) in to_display.iter() { let kk: &str = k.as_ref(); - if k2.starts_with(kk) - && k2.matches('/').count() == printable_node_slashes + 2 - { + if k2.starts_with(kk) && k2.matches('/').count() == num_slashes + 2 { has_children = true; } } @@ -100,7 +86,7 @@ fn display_node>( is_biggest, short_paths, new_depth, - is.to_string() + get_tree_chars(num_siblings, has_children), + new_indent_str.to_string() + get_tree_chars(num_siblings, has_children), ); is_biggest = false; } @@ -109,6 +95,26 @@ fn display_node>( } } +fn clean_indentation_string>(s: S) -> String { + let mut is = s.into(); + is = is.replace("└─┬", " "); + is = is.replace("└──", " "); + is = is.replace("├──", "│ "); + is = is.replace("├─┬", "│ "); + is = is.replace("─┬", " "); + is +} + +fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str) -> u64 { + to_display.iter().fold(0, |a, b| { + if b.0.starts_with(ntp) && b.0.matches('/').count() == num_slashes + 1 { + a + 1 + } else { + a + } + }) +} + fn get_tree_chars(num_siblings: u64, has_children: bool) -> &'static str { if num_siblings == 0 { if has_children { diff --git a/src/main.rs b/src/main.rs index 966935e..61e613d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate walkdir; use self::display::draw_it; use clap::{App, AppSettings, Arg}; -use utils::{find_big_ones, get_dir_tree, sort, simplify_dir_names}; +use utils::{find_big_ones, get_dir_tree, simplify_dir_names, sort}; mod display; mod utils; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f133e15..a276b11 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -43,13 +43,7 @@ pub fn get_dir_tree( let mut data: HashMap = HashMap::new(); for b in top_level_names.iter() { - examine_dir( - &b, - apparent_size, - &mut inodes, - &mut data, - &mut permissions, - ); + examine_dir(&b, apparent_size, &mut inodes, &mut data, &mut permissions); } (permissions == 0, data, top_level_names) } @@ -125,7 +119,6 @@ pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<( } } - mod tests { #[allow(unused_imports)] use super::*; @@ -159,4 +152,4 @@ mod tests { correct.insert("b".to_string()); assert!(simplify_dir_names(vec!["a/b", "c/a/b/", "b"]) == correct); } -} \ No newline at end of file +}