mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
wip: code to remove duplicate arguments
Also handle case where an argument is a substring of another argument
This commit is contained in:
+17
-6
@@ -1,5 +1,6 @@
|
||||
extern crate ansi_term;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use self::ansi_term::Colour::Fixed;
|
||||
|
||||
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
||||
@@ -8,15 +9,18 @@ pub fn draw_it(
|
||||
permissions: bool,
|
||||
short_paths: bool,
|
||||
depth: Option<u64>,
|
||||
base_dirs: Vec<String>,
|
||||
base_dirs: HashSet<String>,
|
||||
to_display: Vec<(String, u64)>,
|
||||
) -> () {
|
||||
if !permissions {
|
||||
eprintln!("Did not have permissions for all directories");
|
||||
}
|
||||
let mut found = HashSet::new();
|
||||
|
||||
for f in base_dirs {
|
||||
display_node(f, &to_display, true, short_paths, depth, "")
|
||||
for &(ref k, _) in to_display.iter() {
|
||||
if base_dirs.contains(k) {
|
||||
display_node(&k, &mut found, &to_display, true, short_paths, depth, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,19 +34,25 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
|
||||
}
|
||||
|
||||
fn display_node<S: Into<String>>(
|
||||
node_to_print: String,
|
||||
node_to_print: &String,
|
||||
found: &mut HashSet<String>,
|
||||
to_display: &Vec<(String, u64)>,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
depth: Option<u64>,
|
||||
indentation_str: S,
|
||||
) {
|
||||
if found.contains(node_to_print) {
|
||||
return
|
||||
}
|
||||
found.insert(node_to_print.to_string());
|
||||
|
||||
let new_depth = match depth {
|
||||
None => None,
|
||||
Some(0) => return,
|
||||
Some(d) => Some(d - 1),
|
||||
};
|
||||
match get_size(to_display, &node_to_print) {
|
||||
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();
|
||||
@@ -83,7 +93,8 @@ fn display_node<S: Into<String>>(
|
||||
}
|
||||
|
||||
display_node(
|
||||
k.to_string(),
|
||||
k,
|
||||
found,
|
||||
to_display,
|
||||
is_biggest,
|
||||
short_paths,
|
||||
|
||||
+4
-3
@@ -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};
|
||||
use utils::{find_big_ones, get_dir_tree, sort, simplify_dir_names};
|
||||
|
||||
mod display;
|
||||
mod utils;
|
||||
@@ -46,7 +46,7 @@ fn main() {
|
||||
.arg(Arg::with_name("inputs").multiple(true))
|
||||
.get_matches();
|
||||
|
||||
let filenames = {
|
||||
let target_dirs = {
|
||||
match options.values_of("inputs") {
|
||||
None => vec!["."],
|
||||
Some(r) => r.collect(),
|
||||
@@ -82,7 +82,8 @@ fn main() {
|
||||
let use_apparent_size = options.is_present("display_apparent_size");
|
||||
let use_full_path = options.is_present("display_full_paths");
|
||||
|
||||
let (permissions, nodes, top_level_names) = get_dir_tree(&filenames, use_apparent_size);
|
||||
let simplified_dirs = simplify_dir_names(target_dirs);
|
||||
let (permissions, nodes, top_level_names) = get_dir_tree(simplified_dirs, use_apparent_size);
|
||||
let sorted_data = sort(nodes);
|
||||
let biggest_ones = {
|
||||
if depth.is_none() {
|
||||
|
||||
+9
-1
@@ -28,6 +28,15 @@ pub fn test_main_long_paths() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_main_multi_arg() {
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&["src/test_dir/many/", "src/test_dir/", "src/test_dir"])
|
||||
.stdout()
|
||||
.is(main_output(true))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn main_output(short_paths: bool) -> String {
|
||||
format!(
|
||||
@@ -271,4 +280,3 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: add test for bad path
|
||||
|
||||
+67
-7
@@ -7,25 +7,49 @@ use walkdir::WalkDir;
|
||||
mod platform;
|
||||
use self::platform::*;
|
||||
|
||||
pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet<String> {
|
||||
let mut top_level_names: HashSet<String> = HashSet::new();
|
||||
|
||||
for t in filenames {
|
||||
let top_level_name = strip_end_slashes(t);
|
||||
let mut can_add = true;
|
||||
let mut to_remove: Vec<String> = Vec::new();
|
||||
|
||||
for tt in top_level_names.iter() {
|
||||
let temp = tt.to_string();
|
||||
if top_level_name.starts_with(&temp) {
|
||||
can_add = false;
|
||||
} else if tt.starts_with(&top_level_name) {
|
||||
to_remove.push(temp);
|
||||
}
|
||||
}
|
||||
for tr in to_remove {
|
||||
top_level_names.remove(&tr);
|
||||
}
|
||||
if can_add {
|
||||
top_level_names.insert(top_level_name);
|
||||
}
|
||||
}
|
||||
|
||||
top_level_names
|
||||
}
|
||||
|
||||
pub fn get_dir_tree(
|
||||
filenames: &Vec<&str>,
|
||||
top_level_names: HashSet<String>,
|
||||
apparent_size: bool,
|
||||
) -> (bool, HashMap<String, u64>, Vec<String>) {
|
||||
) -> (bool, HashMap<String, u64>, HashSet<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);
|
||||
for b in top_level_names.iter() {
|
||||
examine_dir(
|
||||
&top_level_name,
|
||||
&b,
|
||||
apparent_size,
|
||||
&mut inodes,
|
||||
&mut data,
|
||||
&mut permissions,
|
||||
);
|
||||
top_level_names.push(top_level_name);
|
||||
}
|
||||
(permissions == 0, data, top_level_names)
|
||||
}
|
||||
@@ -100,3 +124,39 @@ pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<(
|
||||
new_l
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_simplify_dir() {
|
||||
let mut correct = HashSet::new();
|
||||
correct.insert("a".to_string());
|
||||
assert!(simplify_dir_names(vec!["a"]) == correct);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simplify_dir_rm_subdir() {
|
||||
let mut correct = HashSet::new();
|
||||
correct.insert("a/b".to_string());
|
||||
assert!(simplify_dir_names(vec!["a/b", "a/b/c", "a/b/d/f"]) == correct);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simplify_dir_duplicates() {
|
||||
let mut correct = HashSet::new();
|
||||
correct.insert("a/b".to_string());
|
||||
correct.insert("c".to_string());
|
||||
assert!(simplify_dir_names(vec!["a/b", "a/b//", "c", "c/"]) == correct);
|
||||
}
|
||||
#[test]
|
||||
fn test_simplify_dir_rm_subdir_and_not_substrings() {
|
||||
let mut correct = HashSet::new();
|
||||
correct.insert("a/b".to_string());
|
||||
correct.insert("c/a/b".to_string());
|
||||
correct.insert("b".to_string());
|
||||
assert!(simplify_dir_names(vec!["a/b", "c/a/b/", "b"]) == correct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user