mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Merge pull request #19 from bootandy/rm_dup_input
code to remove duplicate arguments
This commit is contained in:
+44
-26
@@ -1,6 +1,7 @@
|
|||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
|
|
||||||
use self::ansi_term::Colour::Fixed;
|
use self::ansi_term::Colour::Fixed;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
||||||
|
|
||||||
@@ -8,15 +9,18 @@ pub fn draw_it(
|
|||||||
permissions: bool,
|
permissions: bool,
|
||||||
short_paths: bool,
|
short_paths: bool,
|
||||||
depth: Option<u64>,
|
depth: Option<u64>,
|
||||||
base_dirs: Vec<String>,
|
base_dirs: HashSet<String>,
|
||||||
to_display: Vec<(String, u64)>,
|
to_display: Vec<(String, u64)>,
|
||||||
) -> () {
|
) -> () {
|
||||||
if !permissions {
|
if !permissions {
|
||||||
eprintln!("Did not have permissions for all directories");
|
eprintln!("Did not have permissions for all directories");
|
||||||
}
|
}
|
||||||
|
let mut found = HashSet::new();
|
||||||
|
|
||||||
for f in base_dirs {
|
for &(ref k, _) in to_display.iter() {
|
||||||
display_node(f, &to_display, true, short_paths, depth, "")
|
if base_dirs.contains(k) {
|
||||||
|
display_node(&k, &mut found, &to_display, true, short_paths, depth, "─┬")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,65 +34,59 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn display_node<S: Into<String>>(
|
fn display_node<S: Into<String>>(
|
||||||
node_to_print: String,
|
node_to_print: &String,
|
||||||
|
found: &mut HashSet<String>,
|
||||||
to_display: &Vec<(String, u64)>,
|
to_display: &Vec<(String, u64)>,
|
||||||
is_biggest: bool,
|
is_biggest: bool,
|
||||||
short_paths: bool,
|
short_paths: bool,
|
||||||
depth: Option<u64>,
|
depth: Option<u64>,
|
||||||
indentation_str: S,
|
indentation_str: S,
|
||||||
) {
|
) {
|
||||||
|
if found.contains(node_to_print) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
found.insert(node_to_print.to_string());
|
||||||
|
|
||||||
let new_depth = match depth {
|
let new_depth = match depth {
|
||||||
None => None,
|
None => None,
|
||||||
Some(0) => return,
|
Some(0) => return,
|
||||||
Some(d) => Some(d - 1),
|
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),
|
None => println!("Can not find path: {}", node_to_print),
|
||||||
Some(size) => {
|
Some(size) => {
|
||||||
let mut is = indentation_str.into();
|
let is = indentation_str.into();
|
||||||
let ntp: &str = node_to_print.as_ref();
|
let ntp: &str = node_to_print.as_ref();
|
||||||
|
|
||||||
print_this_node(ntp, size, is_biggest, short_paths, is.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("└─┬", " ");
|
let num_slashes = node_to_print.matches('/').count();
|
||||||
is = is.replace("└──", " ");
|
let mut num_siblings = count_siblings(to_display, num_slashes, ntp);
|
||||||
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 mut is_biggest = true;
|
let mut is_biggest = true;
|
||||||
for &(ref k, _) in to_display.iter() {
|
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;
|
num_siblings -= 1;
|
||||||
|
|
||||||
let mut has_children = false;
|
let mut has_children = false;
|
||||||
if new_depth.is_none() || new_depth.unwrap() != 1 {
|
if new_depth.is_none() || new_depth.unwrap() != 1 {
|
||||||
for &(ref k2, _) in to_display.iter() {
|
for &(ref k2, _) in to_display.iter() {
|
||||||
let kk: &str = k.as_ref();
|
let kk: &str = k.as_ref();
|
||||||
if k2.starts_with(kk)
|
if k2.starts_with(kk) && k2.matches('/').count() == num_slashes + 2 {
|
||||||
&& k2.matches('/').count() == printable_node_slashes + 2
|
|
||||||
{
|
|
||||||
has_children = true;
|
has_children = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
display_node(
|
display_node(
|
||||||
k.to_string(),
|
k,
|
||||||
|
found,
|
||||||
to_display,
|
to_display,
|
||||||
is_biggest,
|
is_biggest,
|
||||||
short_paths,
|
short_paths,
|
||||||
new_depth,
|
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;
|
is_biggest = false;
|
||||||
}
|
}
|
||||||
@@ -97,6 +95,26 @@ fn display_node<S: Into<String>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clean_indentation_string<S: Into<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 {
|
fn get_tree_chars(num_siblings: u64, has_children: bool) -> &'static str {
|
||||||
if num_siblings == 0 {
|
if num_siblings == 0 {
|
||||||
if has_children {
|
if has_children {
|
||||||
|
|||||||
+4
-3
@@ -5,7 +5,7 @@ extern crate walkdir;
|
|||||||
|
|
||||||
use self::display::draw_it;
|
use self::display::draw_it;
|
||||||
use clap::{App, AppSettings, Arg};
|
use clap::{App, AppSettings, Arg};
|
||||||
use utils::{find_big_ones, get_dir_tree, sort};
|
use utils::{find_big_ones, get_dir_tree, simplify_dir_names, sort};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
mod utils;
|
mod utils;
|
||||||
@@ -46,7 +46,7 @@ fn main() {
|
|||||||
.arg(Arg::with_name("inputs").multiple(true))
|
.arg(Arg::with_name("inputs").multiple(true))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let filenames = {
|
let target_dirs = {
|
||||||
match options.values_of("inputs") {
|
match options.values_of("inputs") {
|
||||||
None => vec!["."],
|
None => vec!["."],
|
||||||
Some(r) => r.collect(),
|
Some(r) => r.collect(),
|
||||||
@@ -82,7 +82,8 @@ fn main() {
|
|||||||
let use_apparent_size = options.is_present("display_apparent_size");
|
let use_apparent_size = options.is_present("display_apparent_size");
|
||||||
let use_full_path = options.is_present("display_full_paths");
|
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 sorted_data = sort(nodes);
|
||||||
let biggest_ones = {
|
let biggest_ones = {
|
||||||
if depth.is_none() {
|
if depth.is_none() {
|
||||||
|
|||||||
+36
-28
@@ -28,6 +28,15 @@ pub fn test_main_long_paths() {
|
|||||||
.unwrap();
|
.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")]
|
#[cfg(target_os = "macos")]
|
||||||
fn main_output(short_paths: bool) -> String {
|
fn main_output(short_paths: bool) -> String {
|
||||||
format!(
|
format!(
|
||||||
@@ -35,21 +44,21 @@ fn main_output(short_paths: bool) -> String {
|
|||||||
{}
|
{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
format_string("src/test_dir", true, short_paths, " 4.0K", ""),
|
format_string("src/test_dir", true, short_paths, " 4.0K", "─┬"),
|
||||||
format_string("src/test_dir/many", true, short_paths, " 4.0K", "└─┬",),
|
format_string("src/test_dir/many", true, short_paths, " 4.0K", " └─┬",),
|
||||||
format_string(
|
format_string(
|
||||||
"src/test_dir/many/hello_file",
|
"src/test_dir/many/hello_file",
|
||||||
true,
|
true,
|
||||||
short_paths,
|
short_paths,
|
||||||
" 4.0K",
|
" 4.0K",
|
||||||
" ├──",
|
" ├──",
|
||||||
),
|
),
|
||||||
format_string(
|
format_string(
|
||||||
"src/test_dir/many/a_file",
|
"src/test_dir/many/a_file",
|
||||||
false,
|
false,
|
||||||
short_paths,
|
short_paths,
|
||||||
" 0B",
|
" 0B",
|
||||||
" └──",
|
" └──",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -61,21 +70,21 @@ fn main_output(short_paths: bool) -> String {
|
|||||||
{}
|
{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
format_string("src/test_dir", true, short_paths, " 12K", ""),
|
format_string("src/test_dir", true, short_paths, " 12K", "─┬"),
|
||||||
format_string("src/test_dir/many", true, short_paths, " 8.0K", "└─┬",),
|
format_string("src/test_dir/many", true, short_paths, " 8.0K", " └─┬",),
|
||||||
format_string(
|
format_string(
|
||||||
"src/test_dir/many/hello_file",
|
"src/test_dir/many/hello_file",
|
||||||
true,
|
true,
|
||||||
short_paths,
|
short_paths,
|
||||||
" 4.0K",
|
" 4.0K",
|
||||||
" ├──",
|
" ├──",
|
||||||
),
|
),
|
||||||
format_string(
|
format_string(
|
||||||
"src/test_dir/many/a_file",
|
"src/test_dir/many/a_file",
|
||||||
false,
|
false,
|
||||||
short_paths,
|
short_paths,
|
||||||
" 0B",
|
" 0B",
|
||||||
" └──",
|
" └──",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -89,7 +98,7 @@ pub fn test_apparent_size() {
|
|||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
" 6B",
|
" 6B",
|
||||||
" ├──",
|
" ├──",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -140,9 +149,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
|||||||
"{}
|
"{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir, true, true, " 8.0K", ""),
|
format_string(dir, true, true, " 8.0K", "─┬"),
|
||||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
format_string(file_path, true, true, " 4.0K", " ├──",),
|
||||||
format_string(link_name, false, true, " 4.0K", "└──",),
|
format_string(link_name, false, true, " 4.0K", " └──",),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,9 +161,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
|||||||
"{}
|
"{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir, true, true, " 8.0K", ""),
|
format_string(dir, true, true, " 8.0K", "─┬"),
|
||||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
format_string(file_path, true, true, " 4.0K", " ├──",),
|
||||||
format_string(link_name, false, true, " 0B", "└──",),
|
format_string(link_name, false, true, " 0B", " └──",),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,14 +209,14 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
|
|||||||
let r = format!(
|
let r = format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir_s, true, true, " 4.0K", ""),
|
format_string(dir_s, true, true, " 4.0K", "─┬"),
|
||||||
format_string(file_path_s, true, true, " 4.0K", "└──")
|
format_string(file_path_s, true, true, " 4.0K", " └──")
|
||||||
);
|
);
|
||||||
let r2 = format!(
|
let r2 = format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir_s, true, true, " 4.0K", ""),
|
format_string(dir_s, true, true, " 4.0K", "─┬"),
|
||||||
format_string(link_name_s, true, true, " 4.0K", "└──")
|
format_string(link_name_s, true, true, " 4.0K", " └──")
|
||||||
);
|
);
|
||||||
(r, r2)
|
(r, r2)
|
||||||
}
|
}
|
||||||
@@ -217,14 +226,14 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
|
|||||||
let r = format!(
|
let r = format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir_s, true, true, " 8.0K", ""),
|
format_string(dir_s, true, true, " 8.0K", "─┬"),
|
||||||
format_string(file_path_s, true, true, " 4.0K", "└──")
|
format_string(file_path_s, true, true, " 4.0K", " └──")
|
||||||
);
|
);
|
||||||
let r2 = format!(
|
let r2 = format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir_s, true, true, " 8.0K", ""),
|
format_string(dir_s, true, true, " 8.0K", "─┬"),
|
||||||
format_string(link_name_s, true, true, " 4.0K", "└──")
|
format_string(link_name_s, true, true, " 4.0K", " └──")
|
||||||
);
|
);
|
||||||
(r, r2)
|
(r, r2)
|
||||||
}
|
}
|
||||||
@@ -257,8 +266,8 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
|
|||||||
format!(
|
format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir, true, true, " 4.0K", ""),
|
format_string(dir, true, true, " 4.0K", "─┬"),
|
||||||
format_string(link_name, true, true, " 4.0K", "└──",),
|
format_string(link_name, true, true, " 4.0K", " └──",),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@@ -266,9 +275,8 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
|
|||||||
format!(
|
format!(
|
||||||
"{}
|
"{}
|
||||||
{}",
|
{}",
|
||||||
format_string(dir, true, true, " 4.0K", ""),
|
format_string(dir, true, true, " 4.0K", "─┬"),
|
||||||
format_string(link_name, true, true, " 0B", "└──",),
|
format_string(link_name, true, true, " 0B", " └──",),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add test for bad path
|
|
||||||
|
|||||||
+66
-13
@@ -7,25 +7,43 @@ use walkdir::WalkDir;
|
|||||||
mod platform;
|
mod platform;
|
||||||
use self::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(
|
pub fn get_dir_tree(
|
||||||
filenames: &Vec<&str>,
|
top_level_names: HashSet<String>,
|
||||||
apparent_size: bool,
|
apparent_size: bool,
|
||||||
) -> (bool, HashMap<String, u64>, Vec<String>) {
|
) -> (bool, HashMap<String, u64>, HashSet<String>) {
|
||||||
let mut permissions = 0;
|
let mut permissions = 0;
|
||||||
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
|
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
|
||||||
let mut data: HashMap<String, u64> = HashMap::new();
|
let mut data: HashMap<String, u64> = HashMap::new();
|
||||||
let mut top_level_names = Vec::new();
|
|
||||||
|
|
||||||
for b in filenames {
|
for b in top_level_names.iter() {
|
||||||
let top_level_name = strip_end_slashes(b);
|
examine_dir(&b, apparent_size, &mut inodes, &mut data, &mut permissions);
|
||||||
examine_dir(
|
|
||||||
&top_level_name,
|
|
||||||
apparent_size,
|
|
||||||
&mut inodes,
|
|
||||||
&mut data,
|
|
||||||
&mut permissions,
|
|
||||||
);
|
|
||||||
top_level_names.push(top_level_name);
|
|
||||||
}
|
}
|
||||||
(permissions == 0, data, top_level_names)
|
(permissions == 0, data, top_level_names)
|
||||||
}
|
}
|
||||||
@@ -100,3 +118,38 @@ pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<(
|
|||||||
new_l
|
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