Add support for -d depth flag

Following a user request the option '-d N' allows a user to output N
levels of sub directories.

Fixed bug: so that trailing slashes are now removed.
This commit is contained in:
andy.boot
2018-04-23 15:39:20 +01:00
parent 0bded9698a
commit e6c777fb8b
3 changed files with 96 additions and 31 deletions
+23 -5
View File
@@ -9,20 +9,35 @@ use std::path::PathBuf;
mod platform;
use self::platform::*;
pub fn get_dir_tree(filenames: &Vec<&str>, apparent_size: bool) -> (bool, HashMap<String, u64>) {
pub fn get_dir_tree(
filenames: &Vec<&str>,
apparent_size: bool,
) -> (bool, HashMap<String, u64>, Vec<String>) {
let mut permissions = 0;
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
let mut data: HashMap<String, u64> = HashMap::new();
let mut top_level_names = Vec::new();
for b in filenames {
let top_level_name = strip_end_slashes(b);
examine_dir(
&Path::new(b).to_path_buf(),
&Path::new(&top_level_name).to_path_buf(),
apparent_size,
&mut inodes,
&mut data,
&mut permissions,
);
top_level_names.push(top_level_name);
}
(permissions == 0, data)
(permissions == 0, data, top_level_names)
}
fn strip_end_slashes(s: &str) -> String {
let mut new_name = String::from(s);
while new_name.chars().last() == Some('/') && new_name.len() != 1 {
new_name.pop();
}
new_name
}
fn examine_dir(
@@ -66,11 +81,14 @@ fn examine_dir(
}
}
pub fn find_big_ones<'a>(data: HashMap<String, u64>, max_to_show: usize) -> Vec<(String, u64)> {
pub fn sort<'a>(data: HashMap<String, u64>) -> 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| b.1.cmp(&a.1));
new_l
}
if new_l.len() > max_to_show {
pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<(String, u64)> {
if max_to_show > 0 && new_l.len() > max_to_show {
new_l[0..max_to_show + 1].to_vec()
} else {
new_l