mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Merge pull request #15 from bootandy/depth2
Add support for -d depth flag
This commit is contained in:
@@ -7,11 +7,11 @@ du + rust = dust. Like du but more intuitive
|
|||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
### Cargo:
|
### Cargo
|
||||||
|
|
||||||
* cargo install du-dust
|
* cargo install du-dust
|
||||||
|
|
||||||
### Download:
|
### Download
|
||||||
|
|
||||||
* Download linux / mac binary from [Releases](https://github.com/bootandy/dust/releases)
|
* Download linux / mac binary from [Releases](https://github.com/bootandy/dust/releases)
|
||||||
* unzip file: tar -xvf _downloaded_file.tar.gz_
|
* unzip file: tar -xvf _downloaded_file.tar.gz_
|
||||||
@@ -21,7 +21,7 @@ du + rust = dust. Like du but more intuitive
|
|||||||
|
|
||||||
Dust is meant to give you an instant overview of which directories are using disk space without requiring sort or head. Dust will print a maximum of 1 'Did not have permissions message'.
|
Dust is meant to give you an instant overview of which directories are using disk space without requiring sort or head. Dust will print a maximum of 1 'Did not have permissions message'.
|
||||||
|
|
||||||
Dust will list the 15 biggest sub directories or files and will smartly recurse down the tree to find the larger ones. There is no need for a '-d' flag or a '-h' flag. The largest sub directory will have its size shown in *red*
|
Dust will list the 20 biggest sub directories or files and will smartly recurse down the tree to find the larger ones. There is no need for a '-d' flag or a '-h' flag. The largest sub directory will have its size shown in *red*
|
||||||
|
|
||||||
## Why?
|
## Why?
|
||||||
|
|
||||||
@@ -36,7 +36,8 @@ Usage: dust <dir>
|
|||||||
Usage: dust <dir> <another_dir> <and_more>
|
Usage: dust <dir> <another_dir> <and_more>
|
||||||
Usage: dust -p <dir> (full-path - does not shorten the path of the subdirectories)
|
Usage: dust -p <dir> (full-path - does not shorten the path of the subdirectories)
|
||||||
Usage: dust -s <dir> (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
|
Usage: dust -s <dir> (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
|
||||||
Usage: dust -n 30 <dir> (Shows 30 directories not 15)
|
Usage: dust -n 30 <dir> (Shows 30 directories not 20)
|
||||||
|
Usage: dust -d 3 <dir> (Shows 3 levels of subdirectories)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
+23
-16
@@ -7,7 +7,8 @@ static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
|||||||
pub fn draw_it(
|
pub fn draw_it(
|
||||||
permissions: bool,
|
permissions: bool,
|
||||||
short_paths: bool,
|
short_paths: bool,
|
||||||
base_dirs: Vec<&str>,
|
depth: Option<u64>,
|
||||||
|
base_dirs: Vec<String>,
|
||||||
to_display: Vec<(String, u64)>,
|
to_display: Vec<(String, u64)>,
|
||||||
) -> () {
|
) -> () {
|
||||||
if !permissions {
|
if !permissions {
|
||||||
@@ -15,13 +16,13 @@ pub fn draw_it(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for f in base_dirs {
|
for f in base_dirs {
|
||||||
display_node(f, &to_display, true, short_paths, "")
|
display_node(f, &to_display, true, short_paths, depth, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: String) -> Option<u64> {
|
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
|
||||||
for &(ref k, ref v) in nodes.iter() {
|
for &(ref k, ref v) in nodes.iter() {
|
||||||
if *k == node_to_print {
|
if *k == *node_to_print {
|
||||||
return Some(*v);
|
return Some(*v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,18 +30,25 @@ 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: &str,
|
node_to_print: 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>,
|
||||||
indentation_str: S,
|
indentation_str: S,
|
||||||
) {
|
) {
|
||||||
let mut is = indentation_str.into();
|
let new_depth = match depth {
|
||||||
let size = get_size(to_display, node_to_print.to_string());
|
None => None,
|
||||||
match size {
|
Some(0) => return,
|
||||||
|
Some(d) => Some(d - 1),
|
||||||
|
};
|
||||||
|
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) => {
|
||||||
print_this_node(node_to_print, size, is_biggest, short_paths, is.as_ref());
|
let mut is = indentation_str.into();
|
||||||
|
let ntp: &str = node_to_print.as_ref();
|
||||||
|
|
||||||
|
print_this_node(ntp, size, is_biggest, short_paths, is.as_ref());
|
||||||
|
|
||||||
is = is.replace("└─┬", " ");
|
is = is.replace("└─┬", " ");
|
||||||
is = is.replace("└──", " ");
|
is = is.replace("└──", " ");
|
||||||
@@ -50,9 +58,7 @@ fn display_node<S: Into<String>>(
|
|||||||
let printable_node_slashes = node_to_print.matches('/').count();
|
let printable_node_slashes = node_to_print.matches('/').count();
|
||||||
|
|
||||||
let mut num_siblings = to_display.iter().fold(0, |a, b| {
|
let mut num_siblings = to_display.iter().fold(0, |a, b| {
|
||||||
if b.0.starts_with(node_to_print)
|
if b.0.starts_with(ntp) && b.0.matches('/').count() == printable_node_slashes + 1 {
|
||||||
&& b.0.matches('/').count() == printable_node_slashes + 1
|
|
||||||
{
|
|
||||||
a + 1
|
a + 1
|
||||||
} else {
|
} else {
|
||||||
a
|
a
|
||||||
@@ -61,12 +67,11 @@ fn display_node<S: Into<String>>(
|
|||||||
|
|
||||||
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(node_to_print)
|
if k.starts_with(ntp) && k.matches('/').count() == printable_node_slashes + 1 {
|
||||||
&& k.matches('/').count() == printable_node_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 {
|
||||||
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)
|
||||||
@@ -75,12 +80,14 @@ fn display_node<S: Into<String>>(
|
|||||||
has_children = true;
|
has_children = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
display_node(
|
display_node(
|
||||||
&k,
|
k.to_string(),
|
||||||
to_display,
|
to_display,
|
||||||
is_biggest,
|
is_biggest,
|
||||||
short_paths,
|
short_paths,
|
||||||
|
new_depth,
|
||||||
is.to_string() + get_tree_chars(num_siblings, has_children),
|
is.to_string() + get_tree_chars(num_siblings, has_children),
|
||||||
);
|
);
|
||||||
is_biggest = false;
|
is_biggest = false;
|
||||||
|
|||||||
+49
-5
@@ -5,19 +5,28 @@ 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};
|
use std::io::{self, Write};
|
||||||
|
use utils::{find_big_ones, get_dir_tree, sort};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
static DEFAULT_NUMBER_OF_LINES: &'static str = "15";
|
static DEFAULT_NUMBER_OF_LINES: &'static str = "20";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let options = App::new("Dust")
|
let options = App::new("Dust")
|
||||||
.setting(AppSettings::TrailingVarArg)
|
.setting(AppSettings::TrailingVarArg)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("depth")
|
||||||
|
.short("d")
|
||||||
|
.long("depth")
|
||||||
|
.help("Depth to show")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("number_of_lines")
|
Arg::with_name("number_of_lines")
|
||||||
.short("n")
|
.short("n")
|
||||||
|
.long("number-of-lines")
|
||||||
.help("Number of lines of output to show")
|
.help("Number of lines of output to show")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.default_value(DEFAULT_NUMBER_OF_LINES),
|
.default_value(DEFAULT_NUMBER_OF_LINES),
|
||||||
@@ -25,11 +34,13 @@ fn main() {
|
|||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("display_full_paths")
|
Arg::with_name("display_full_paths")
|
||||||
.short("p")
|
.short("p")
|
||||||
|
.long("full-paths")
|
||||||
.help("If set sub directories will not have their path shortened"),
|
.help("If set sub directories will not have their path shortened"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("display_apparent_size")
|
Arg::with_name("display_apparent_size")
|
||||||
.short("s")
|
.short("s")
|
||||||
|
.long("apparent-size")
|
||||||
.help("If set will use file length. Otherwise we use blocks"),
|
.help("If set will use file length. Otherwise we use blocks"),
|
||||||
)
|
)
|
||||||
.arg(Arg::with_name("inputs").multiple(true))
|
.arg(Arg::with_name("inputs").multiple(true))
|
||||||
@@ -41,13 +52,46 @@ fn main() {
|
|||||||
Some(r) => r.collect(),
|
Some(r) => r.collect(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
||||||
|
let depth = {
|
||||||
|
if options.is_present("depth") {
|
||||||
|
match value_t!(options.value_of("depth"), u64) {
|
||||||
|
Ok(v) => Some(v + 1),
|
||||||
|
Err(_) => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if options.is_present("depth")
|
||||||
|
&& options.value_of("number_of_lines").unwrap() != DEFAULT_NUMBER_OF_LINES
|
||||||
|
{
|
||||||
|
io::stderr()
|
||||||
|
.write(b"Use either -n for number of directories to show. Or -d for depth. Not both")
|
||||||
|
.expect("Error writing to stderr. Oh the irony!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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) = get_dir_tree(&filenames, use_apparent_size);
|
let (permissions, nodes, top_level_names) = get_dir_tree(&filenames, use_apparent_size);
|
||||||
let biggest_ones = find_big_ones(nodes, number_of_lines);
|
let sorted_data = sort(nodes);
|
||||||
draw_it(permissions, !use_full_path, filenames, biggest_ones);
|
let biggest_ones = {
|
||||||
|
if depth.is_none() {
|
||||||
|
find_big_ones(sorted_data, number_of_lines)
|
||||||
|
} else {
|
||||||
|
sorted_data
|
||||||
|
}
|
||||||
|
};
|
||||||
|
draw_it(
|
||||||
|
permissions,
|
||||||
|
!use_full_path,
|
||||||
|
depth,
|
||||||
|
top_level_names,
|
||||||
|
biggest_ones,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
+7
-28
@@ -123,60 +123,39 @@ pub fn test_soft_sym_link() {
|
|||||||
.output();
|
.output();
|
||||||
assert!(c.is_ok());
|
assert!(c.is_ok());
|
||||||
|
|
||||||
let (r, r2) = soft_sym_link_output(dir_s, file_path_s, link_name_s);
|
let r = soft_sym_link_output(dir_s, file_path_s, link_name_s);
|
||||||
|
|
||||||
// We cannot guarantee which version will appear first.
|
// We cannot guarantee which version will appear first.
|
||||||
// TODO: Consider adding predictable itteration order (sort file entries by name?)
|
// TODO: Consider adding predictable itteration order (sort file entries by name?)
|
||||||
let result = panic::catch_unwind(|| {
|
|
||||||
assert_cli::Assert::main_binary()
|
assert_cli::Assert::main_binary()
|
||||||
.with_args(&[dir_s])
|
.with_args(&[dir_s])
|
||||||
.stdout()
|
.stdout()
|
||||||
.contains(r)
|
.contains(r)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
|
||||||
if result.is_err() {
|
|
||||||
assert_cli::Assert::main_binary()
|
|
||||||
.with_args(&[dir_s])
|
|
||||||
.stdout()
|
|
||||||
.contains(r2)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> (String, String) {
|
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||||
let r = format!(
|
format!(
|
||||||
"{}
|
"{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
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", "└──",),
|
||||||
);
|
)
|
||||||
let r2 = format!(
|
|
||||||
"{}
|
|
||||||
{}
|
|
||||||
{}",
|
|
||||||
format_string(dir, true, true, " 8.0K", ""),
|
|
||||||
format_string(link_name, true, true, " 4.0K", "├──",),
|
|
||||||
format_string(file_path, false, true, " 4.0K", "└──",),
|
|
||||||
);
|
|
||||||
(r, r2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> (String, String) {
|
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||||
let r = format!(
|
format!(
|
||||||
"{}
|
"{}
|
||||||
{}
|
{}
|
||||||
{}",
|
{}",
|
||||||
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", "└──",),
|
||||||
);
|
)
|
||||||
// I have not yet seen the output appear the other way round on linux. If this happens
|
|
||||||
// then add in an alterate ordering like in the mac version of this function.
|
|
||||||
(r, "".to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hard links are ignored as the inode is the same as the file
|
// Hard links are ignored as the inode is the same as the file
|
||||||
|
|||||||
+33
-6
@@ -1,5 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
@@ -9,20 +10,35 @@ use std::path::PathBuf;
|
|||||||
mod platform;
|
mod platform;
|
||||||
use self::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 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 filenames {
|
||||||
|
let top_level_name = strip_end_slashes(b);
|
||||||
examine_dir(
|
examine_dir(
|
||||||
&Path::new(b).to_path_buf(),
|
&Path::new(&top_level_name).to_path_buf(),
|
||||||
apparent_size,
|
apparent_size,
|
||||||
&mut inodes,
|
&mut inodes,
|
||||||
&mut data,
|
&mut data,
|
||||||
&mut permissions,
|
&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(
|
fn examine_dir(
|
||||||
@@ -65,12 +81,23 @@ fn examine_dir(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn compare_tuple(a :&(String, u64), b: &(String, u64)) -> Ordering {
|
||||||
|
let result = b.1.cmp(&a.1);
|
||||||
|
if result == Ordering::Equal {
|
||||||
|
a.0.cmp(&b.0)
|
||||||
|
} else {
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
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.sort_by(|a, b| compare_tuple(&a, &b));
|
||||||
|
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()
|
new_l[0..max_to_show + 1].to_vec()
|
||||||
} else {
|
} else {
|
||||||
new_l
|
new_l
|
||||||
|
|||||||
Reference in New Issue
Block a user