From ec2d9e19d4669a3919cd68b04dd2de4c6c94bc86 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Wed, 2 Oct 2019 20:14:13 +0100 Subject: [PATCH] Run format, introduce new function. strip_end_slash_including_root will remove end slashes including the root directory. The root directory has been a long running problem because if we strip the final slash we will run dust on no directory instead of the root. --- src/display.rs | 37 +++++++++++++++++++++++-------------- src/utils/mod.rs | 8 ++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/display.rs b/src/display.rs index 49170c1..d12b02a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -3,7 +3,7 @@ extern crate ansi_term; use self::ansi_term::Colour::Fixed; use self::ansi_term::Style; use std::collections::HashSet; -use utils::{ensure_end_slash, strip_end_slash}; +use utils::{ensure_end_slash, strip_end_slash_including_root}; static UNITS: [char; 4] = ['T', 'G', 'M', 'K']; @@ -58,8 +58,21 @@ fn display_node( match get_size(to_display, node_to_print) { None => println!("Can not find path: {}", node_to_print), Some(size) => { - print_this_node(node_to_print, size, is_biggest, short_paths, indentation_str); - fan_out(node_to_print, found, to_display, short_paths, new_depth, indentation_str); + print_this_node( + node_to_print, + size, + is_biggest, + short_paths, + indentation_str, + ); + fan_out( + node_to_print, + found, + to_display, + short_paths, + new_depth, + indentation_str, + ); } } } @@ -73,22 +86,18 @@ fn fan_out( indentation_str: &str, ) { let new_indent = clean_indentation_string(indentation_str); - let ntp_with_slash = strip_end_slash(node_to_print); + let num_slashes = strip_end_slash_including_root(node_to_print) + .matches('/') + .count(); - // Annoying edge case for when run on root directory - let num_slashes = if ntp_with_slash == "/" { - 1 - } else { - ntp_with_slash.matches('/').count() + 1 - }; - let mut num_siblings = count_siblings(to_display, num_slashes - 1, node_to_print); + let mut num_siblings = count_siblings(to_display, num_slashes, node_to_print); let max_siblings = num_siblings; for &(ref k, _) in to_display.iter() { let temp = String::from(ensure_end_slash(node_to_print)); - if k.starts_with(temp.as_str()) && k.matches('/').count() == num_slashes { + if k.starts_with(temp.as_str()) && k.matches('/').count() == num_slashes + 1 { num_siblings -= 1; - let has_children = has_children(to_display, new_depth, k, num_slashes); + let has_children = has_children(to_display, new_depth, k, num_slashes + 1); display_node( k, found, @@ -103,7 +112,7 @@ fn fan_out( } fn clean_indentation_string(s: &str) -> String { - let mut is :String = s.into(); + let mut is: String = s.into(); is = is.replace("└─┬", " "); is = is.replace("└──", " "); is = is.replace("├──", "│ "); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e33f08d..c6c813c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -64,6 +64,14 @@ pub fn strip_end_slash(s: &str) -> String { new_name } +pub fn strip_end_slash_including_root(s: &str) -> String { + let mut new_name = String::from(s); + while new_name.ends_with('/') || new_name.ends_with("/.") { + new_name.pop(); + } + new_name +} + fn examine_dir( top_dir: &str, apparent_size: bool,